| 1 | /* |
| 2 | * Copyright 2012 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 | |
| 8 | #ifndef GrSurface_DEFINED |
| 9 | #define GrSurface_DEFINED |
| 10 | |
| 11 | #include "include/core/SkImageInfo.h" |
| 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/gpu/GpuTypes.h" |
| 14 | #include "src/gpu/RefCntedCallback.h" |
| 15 | #include "src/gpu/ganesh/GrGpuResource.h" |
| 16 | |
| 17 | class GrBackendFormat; |
| 18 | class GrDirectContext; |
| 19 | class GrRenderTarget; |
| 20 | class GrTexture; |
| 21 | |
| 22 | class GrSurface : public GrGpuResource { |
| 23 | public: |
| 24 | /** |
| 25 | * Retrieves the dimensions of the surface. |
| 26 | */ |
| 27 | SkISize dimensions() const { return fDimensions; } |
| 28 | |
| 29 | /** |
| 30 | * Retrieves the width of the surface. |
| 31 | */ |
| 32 | int width() const { return fDimensions.width(); } |
| 33 | |
| 34 | /** |
| 35 | * Retrieves the height of the surface. |
| 36 | */ |
| 37 | int height() const { return fDimensions.height(); } |
| 38 | |
| 39 | /** |
| 40 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 41 | */ |
| 42 | SkRect getBoundsRect() const { return SkRect::Make(size: this->dimensions()); } |
| 43 | |
| 44 | virtual GrBackendFormat backendFormat() const = 0; |
| 45 | |
| 46 | void setRelease(sk_sp<skgpu::RefCntedCallback> releaseHelper); |
| 47 | |
| 48 | // These match the definitions in SkImage, from whence they came. |
| 49 | // TODO: Remove Chrome's need to call this on a GrTexture |
| 50 | typedef void* ReleaseCtx; |
| 51 | typedef void (*ReleaseProc)(ReleaseCtx); |
| 52 | void setRelease(ReleaseProc proc, ReleaseCtx ctx) { |
| 53 | this->setRelease(skgpu::RefCntedCallback::Make(proc, ctx)); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return the texture associated with the surface, may be null. |
| 58 | */ |
| 59 | virtual GrTexture* asTexture() { return nullptr; } |
| 60 | virtual const GrTexture* asTexture() const { return nullptr; } |
| 61 | |
| 62 | /** |
| 63 | * @return the render target underlying this surface, may be null. |
| 64 | */ |
| 65 | virtual GrRenderTarget* asRenderTarget() { return nullptr; } |
| 66 | virtual const GrRenderTarget* asRenderTarget() const { return nullptr; } |
| 67 | |
| 68 | GrInternalSurfaceFlags flags() const { return fSurfaceFlags; } |
| 69 | |
| 70 | static size_t ComputeSize(const GrBackendFormat&, SkISize dimensions, int colorSamplesPerPixel, |
| 71 | GrMipmapped, bool binSize = false); |
| 72 | |
| 73 | /** |
| 74 | * The pixel values of this surface cannot be modified (e.g. doesn't support write pixels or |
| 75 | * MIP map level regen). |
| 76 | */ |
| 77 | bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; } |
| 78 | |
| 79 | bool framebufferOnly() const { |
| 80 | return fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly; |
| 81 | } |
| 82 | |
| 83 | // Returns true if we are working with protected content. |
| 84 | bool isProtected() const { return fIsProtected == skgpu::Protected::kYes; } |
| 85 | |
| 86 | void setFramebufferOnly() { |
| 87 | SkASSERT(this->asRenderTarget()); |
| 88 | fSurfaceFlags |= GrInternalSurfaceFlags::kFramebufferOnly; |
| 89 | } |
| 90 | |
| 91 | class RefCntedReleaseProc : public SkNVRefCnt<RefCntedReleaseProc> { |
| 92 | public: |
| 93 | RefCntedReleaseProc(sk_sp<skgpu::RefCntedCallback> callback, |
| 94 | sk_sp<GrDirectContext> directContext); |
| 95 | |
| 96 | ~RefCntedReleaseProc(); |
| 97 | |
| 98 | private: |
| 99 | sk_sp<skgpu::RefCntedCallback> fCallback; |
| 100 | sk_sp<GrDirectContext> fDirectContext; |
| 101 | }; |
| 102 | |
| 103 | #if GR_TEST_UTILS |
| 104 | const GrSurface* asSurface() const override { return this; } |
| 105 | #endif |
| 106 | |
| 107 | protected: |
| 108 | void setGLRTFBOIDIs0() { |
| 109 | SkASSERT(!this->requiresManualMSAAResolve()); |
| 110 | SkASSERT(!this->asTexture()); |
| 111 | SkASSERT(this->asRenderTarget()); |
| 112 | fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0; |
| 113 | } |
| 114 | bool glRTFBOIDis0() const { |
| 115 | return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0; |
| 116 | } |
| 117 | |
| 118 | void setRequiresManualMSAAResolve() { |
| 119 | SkASSERT(!this->glRTFBOIDis0()); |
| 120 | SkASSERT(this->asRenderTarget()); |
| 121 | fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve; |
| 122 | } |
| 123 | bool requiresManualMSAAResolve() const { |
| 124 | return fSurfaceFlags & GrInternalSurfaceFlags::kRequiresManualMSAAResolve; |
| 125 | } |
| 126 | |
| 127 | void setReadOnly() { |
| 128 | SkASSERT(!this->asRenderTarget()); |
| 129 | fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly; |
| 130 | } |
| 131 | |
| 132 | void setVkRTSupportsInputAttachment() { |
| 133 | SkASSERT(this->asRenderTarget()); |
| 134 | fSurfaceFlags |= GrInternalSurfaceFlags::kVkRTSupportsInputAttachment; |
| 135 | } |
| 136 | |
| 137 | GrSurface(GrGpu* gpu, |
| 138 | const SkISize& dimensions, |
| 139 | skgpu::Protected isProtected, |
| 140 | std::string_view label) |
| 141 | : INHERITED(gpu, label) |
| 142 | , fDimensions(dimensions) |
| 143 | , fSurfaceFlags(GrInternalSurfaceFlags::kNone) |
| 144 | , fIsProtected(isProtected) {} |
| 145 | |
| 146 | ~GrSurface() override { |
| 147 | // check that invokeReleaseProc has been called (if needed) |
| 148 | SkASSERT(!fReleaseHelper); |
| 149 | } |
| 150 | |
| 151 | void onRelease() override; |
| 152 | void onAbandon() override; |
| 153 | |
| 154 | private: |
| 155 | const char* getResourceType() const override { return "Surface" ; } |
| 156 | |
| 157 | // Unmanaged backends (e.g. Vulkan) may want to specially handle the release proc in order to |
| 158 | // ensure it isn't called until GPU work related to the resource is completed. |
| 159 | virtual void onSetRelease(sk_sp<RefCntedReleaseProc>) {} |
| 160 | |
| 161 | void invokeReleaseProc() { |
| 162 | // Depending on the ref count of fReleaseHelper this may or may not actually trigger the |
| 163 | // ReleaseProc to be called. |
| 164 | fReleaseHelper.reset(); |
| 165 | } |
| 166 | |
| 167 | SkISize fDimensions; |
| 168 | GrInternalSurfaceFlags fSurfaceFlags; |
| 169 | skgpu::Protected fIsProtected; |
| 170 | sk_sp<RefCntedReleaseProc> fReleaseHelper; |
| 171 | |
| 172 | using INHERITED = GrGpuResource; |
| 173 | }; |
| 174 | |
| 175 | #endif |
| 176 | |