GrRenderTargetProxy.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrRenderTargetProxy_DEFINED
  8. #define GrRenderTargetProxy_DEFINED
  9. #include "include/private/GrTypesPriv.h"
  10. #include "src/gpu/GrCaps.h"
  11. #include "src/gpu/GrSurfaceProxy.h"
  12. #include "src/gpu/GrSwizzle.h"
  13. class GrResourceProvider;
  14. class GrRenderTargetProxyPriv;
  15. // This class delays the acquisition of RenderTargets until they are actually
  16. // required
  17. // Beware: the uniqueID of the RenderTargetProxy will usually be different than
  18. // the uniqueID of the RenderTarget it represents!
  19. class GrRenderTargetProxy : virtual public GrSurfaceProxy {
  20. public:
  21. GrRenderTargetProxy* asRenderTargetProxy() override { return this; }
  22. const GrRenderTargetProxy* asRenderTargetProxy() const override { return this; }
  23. // Actually instantiate the backing rendertarget, if necessary.
  24. bool instantiate(GrResourceProvider*) override;
  25. bool canUseMixedSamples(const GrCaps& caps) const {
  26. return caps.mixedSamplesSupport() && !this->glRTFBOIDIs0() &&
  27. caps.internalMultisampleCount(this->config()) > 0 &&
  28. this->canChangeStencilAttachment();
  29. }
  30. /*
  31. * Indicate that a draw to this proxy requires stencil, and how many stencil samples it needs.
  32. * The number of stencil samples on this proxy will be equal to the largest sample count passed
  33. * to this method.
  34. */
  35. void setNeedsStencil(int8_t numStencilSamples) {
  36. SkASSERT(numStencilSamples >= fSampleCnt);
  37. fNumStencilSamples = SkTMax(numStencilSamples, fNumStencilSamples);
  38. }
  39. /**
  40. * Returns the number of stencil samples required by this proxy.
  41. * NOTE: Once instantiated, the actual render target may have more samples, but it is guaranteed
  42. * to have at least this many. (After a multisample stencil buffer has been attached to a render
  43. * target, we never "downgrade" it to one with fewer samples.)
  44. */
  45. int numStencilSamples() const { return fNumStencilSamples; }
  46. /**
  47. * Returns the number of samples/pixel in the color buffer (One if non-MSAA).
  48. */
  49. int numSamples() const { return fSampleCnt; }
  50. int maxWindowRectangles(const GrCaps& caps) const;
  51. const GrSwizzle& outputSwizzle() const { return fOutputSwizzle; }
  52. bool wrapsVkSecondaryCB() const { return fWrapsVkSecondaryCB == WrapsVkSecondaryCB::kYes; }
  53. // TODO: move this to a priv class!
  54. bool refsWrappedObjects() const;
  55. // Provides access to special purpose functions.
  56. GrRenderTargetProxyPriv rtPriv();
  57. const GrRenderTargetProxyPriv rtPriv() const;
  58. protected:
  59. friend class GrProxyProvider; // for ctors
  60. friend class GrRenderTargetProxyPriv;
  61. // Deferred version
  62. GrRenderTargetProxy(const GrCaps&, const GrBackendFormat&, const GrSurfaceDesc&,
  63. int sampleCount, GrSurfaceOrigin, const GrSwizzle& textureSwizzle,
  64. const GrSwizzle& outputSwizzle, SkBackingFit, SkBudgeted, GrProtected,
  65. GrInternalSurfaceFlags);
  66. enum class WrapsVkSecondaryCB : bool { kNo = false, kYes = true };
  67. // Lazy-callback version
  68. // There are two main use cases for lazily-instantiated proxies:
  69. // basic knowledge - width, height, config, samples, origin are known
  70. // minimal knowledge - only config is known.
  71. //
  72. // The basic knowledge version is used for DDL where we know the type of proxy we are going to
  73. // use, but we don't have access to the GPU yet to instantiate it.
  74. //
  75. // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
  76. // know the final size until flush time.
  77. GrRenderTargetProxy(LazyInstantiateCallback&&, LazyInstantiationType lazyType,
  78. const GrBackendFormat&, const GrSurfaceDesc&, int sampleCount,
  79. GrSurfaceOrigin, const GrSwizzle& textureSwizzle,
  80. const GrSwizzle& outputSwizzle, SkBackingFit, SkBudgeted, GrProtected,
  81. GrInternalSurfaceFlags, WrapsVkSecondaryCB wrapsVkSecondaryCB);
  82. // Wrapped version
  83. GrRenderTargetProxy(sk_sp<GrSurface>, GrSurfaceOrigin, const GrSwizzle& textureSwizzle,
  84. const GrSwizzle& outputSwizzle,
  85. WrapsVkSecondaryCB wrapsVkSecondaryCB = WrapsVkSecondaryCB::kNo);
  86. sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
  87. private:
  88. void setGLRTFBOIDIs0() {
  89. fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
  90. }
  91. bool glRTFBOIDIs0() const {
  92. return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
  93. }
  94. bool canChangeStencilAttachment() const;
  95. size_t onUninstantiatedGpuMemorySize() const override;
  96. SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
  97. // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
  98. // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
  99. // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
  100. // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
  101. // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
  102. // that particular class don't require it. Changing the size of this object can move the start
  103. // address of other types, leading to this problem.
  104. int8_t fSampleCnt;
  105. int8_t fNumStencilSamples = 0;
  106. WrapsVkSecondaryCB fWrapsVkSecondaryCB;
  107. GrSwizzle fOutputSwizzle;
  108. // This is to fix issue in large comment above. Without the padding we end 6 bytes into a 16
  109. // byte range, so the GrTextureProxy ends up starting 8 byte aligned by not 16. We add the
  110. // padding here to get us right up to the 16 byte alignment (technically any padding of 3-10
  111. // bytes would work since it always goes up to 8 byte alignment, but we use 10 to more explicit
  112. // about what we're doing).
  113. char fDummyPadding[10];
  114. // For wrapped render targets the actual GrRenderTarget is stored in the GrIORefProxy class.
  115. // For deferred proxies that pointer is filled in when we need to instantiate the
  116. // deferred resource.
  117. typedef GrSurfaceProxy INHERITED;
  118. };
  119. #endif