robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 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 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 8 | #ifndef GrSurface_DEFINED |
| 9 | #define GrSurface_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkImageInfo.h" |
| 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/gpu/GrBackendSurface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/gpu/GrTypes.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 15 | #include "src/gpu/GrGpuResource.h" |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 16 | |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame] | 17 | class GrRenderTarget; |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 18 | class GrTexture; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 19 | |
Brian Salomon | 197c586 | 2019-08-21 22:08:36 -0400 | [diff] [blame] | 20 | class GrSurface : public GrGpuResource { |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 21 | public: |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 22 | /** |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 23 | * Retrieves the dimensions of the surface. |
| 24 | */ |
| 25 | SkISize dimensions() const { return fDimensions; } |
| 26 | |
| 27 | /** |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 28 | * Retrieves the width of the surface. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 29 | */ |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 30 | int width() const { return fDimensions.width(); } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Retrieves the height of the surface. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 34 | */ |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 35 | int height() const { return fDimensions.height(); } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 39 | */ |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 40 | SkRect getBoundsRect() const { return SkRect::Make(this->dimensions()); } |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 41 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 42 | virtual GrBackendFormat backendFormat() const = 0; |
| 43 | |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 44 | void setRelease(sk_sp<GrRefCntedCallback> releaseHelper) { |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 45 | this->onSetRelease(releaseHelper); |
| 46 | fReleaseHelper = std::move(releaseHelper); |
| 47 | } |
| 48 | |
| 49 | // These match the definitions in SkImage, from whence they came. |
| 50 | // TODO: Remove Chrome's need to call this on a GrTexture |
| 51 | typedef void* ReleaseCtx; |
| 52 | typedef void (*ReleaseProc)(ReleaseCtx); |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 53 | void setRelease(ReleaseProc proc, ReleaseCtx ctx) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 54 | this->setRelease(GrRefCntedCallback::Make(proc, ctx)); |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 55 | } |
| 56 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 57 | /** |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 58 | * @return the texture associated with the surface, may be null. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 59 | */ |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 60 | virtual GrTexture* asTexture() { return nullptr; } |
| 61 | virtual const GrTexture* asTexture() const { return nullptr; } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 64 | * @return the render target underlying this surface, may be null. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 65 | */ |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 66 | virtual GrRenderTarget* asRenderTarget() { return nullptr; } |
| 67 | virtual const GrRenderTarget* asRenderTarget() const { return nullptr; } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 68 | |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 69 | GrInternalSurfaceFlags flags() const { return fSurfaceFlags; } |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame] | 70 | |
Greg Daniel | 0eca74c | 2020-10-01 13:46:00 -0400 | [diff] [blame] | 71 | static size_t ComputeSize(const GrBackendFormat&, SkISize dimensions, int colorSamplesPerPixel, |
| 72 | GrMipmapped, bool binSize = false); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 73 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 74 | /** |
| 75 | * The pixel values of this surface cannot be modified (e.g. doesn't support write pixels or |
| 76 | * MIP map level regen). |
| 77 | */ |
| 78 | bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; } |
| 79 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 80 | bool framebufferOnly() const { |
| 81 | return fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly; |
| 82 | } |
| 83 | |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 84 | // Returns true if we are working with protected content. |
| 85 | bool isProtected() const { return fIsProtected == GrProtected::kYes; } |
| 86 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 87 | void setFramebufferOnly() { |
| 88 | SkASSERT(this->asRenderTarget()); |
| 89 | fSurfaceFlags |= GrInternalSurfaceFlags::kFramebufferOnly; |
| 90 | } |
| 91 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 92 | protected: |
Jim Van Verth | 5fab909 | 2019-11-25 21:23:53 +0000 | [diff] [blame] | 93 | void setGLRTFBOIDIs0() { |
| 94 | SkASSERT(!this->requiresManualMSAAResolve()); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 95 | SkASSERT(!this->asTexture()); |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 96 | SkASSERT(this->asRenderTarget()); |
Jim Van Verth | 5fab909 | 2019-11-25 21:23:53 +0000 | [diff] [blame] | 97 | fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0; |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 98 | } |
Jim Van Verth | 5fab909 | 2019-11-25 21:23:53 +0000 | [diff] [blame] | 99 | bool glRTFBOIDis0() const { |
| 100 | return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0; |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 103 | void setRequiresManualMSAAResolve() { |
Jim Van Verth | 5fab909 | 2019-11-25 21:23:53 +0000 | [diff] [blame] | 104 | SkASSERT(!this->glRTFBOIDis0()); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 105 | SkASSERT(this->asRenderTarget()); |
| 106 | fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve; |
| 107 | } |
| 108 | bool requiresManualMSAAResolve() const { |
| 109 | return fSurfaceFlags & GrInternalSurfaceFlags::kRequiresManualMSAAResolve; |
| 110 | } |
| 111 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 112 | void setReadOnly() { |
| 113 | SkASSERT(!this->asRenderTarget()); |
| 114 | fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly; |
| 115 | } |
| 116 | |
Greg Daniel | 638b2e8 | 2020-08-27 14:29:00 -0400 | [diff] [blame] | 117 | void setVkRTSupportsInputAttachment() { |
| 118 | SkASSERT(this->asRenderTarget()); |
| 119 | fSurfaceFlags |= GrInternalSurfaceFlags::kVkRTSupportsInputAttachment; |
| 120 | } |
| 121 | |
Greg Daniel | d51fa2f | 2020-01-22 16:53:38 -0500 | [diff] [blame] | 122 | GrSurface(GrGpu* gpu, const SkISize& dimensions, GrProtected isProtected) |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 123 | : INHERITED(gpu) |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 124 | , fDimensions(dimensions) |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 125 | , fSurfaceFlags(GrInternalSurfaceFlags::kNone) |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 126 | , fIsProtected(isProtected) {} |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 127 | |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 128 | ~GrSurface() override { |
| 129 | // check that invokeReleaseProc has been called (if needed) |
| 130 | SkASSERT(!fReleaseHelper); |
| 131 | } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 132 | |
reed | 35a5261 | 2015-06-18 14:05:07 -0700 | [diff] [blame] | 133 | void onRelease() override; |
| 134 | void onAbandon() override; |
| 135 | |
| 136 | private: |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 137 | const char* getResourceType() const override { return "Surface"; } |
| 138 | |
Brian Salomon | 1c1c666 | 2019-03-04 10:25:42 -0500 | [diff] [blame] | 139 | // Unmanaged backends (e.g. Vulkan) may want to specially handle the release proc in order to |
| 140 | // ensure it isn't called until GPU work related to the resource is completed. |
| 141 | virtual void onSetRelease(sk_sp<GrRefCntedCallback>) {} |
| 142 | |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 143 | void invokeReleaseProc() { |
| 144 | // Depending on the ref count of fReleaseHelper this may or may not actually trigger the |
| 145 | // ReleaseProc to be called. |
| 146 | fReleaseHelper.reset(); |
| 147 | } |
| 148 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 149 | SkISize fDimensions; |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 150 | GrInternalSurfaceFlags fSurfaceFlags; |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 151 | GrProtected fIsProtected; |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 152 | sk_sp<GrRefCntedCallback> fReleaseHelper; |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 153 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 154 | using INHERITED = GrGpuResource; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 157 | #endif |