blob: 0e50d6a0dcd7653cda1efa5aae80aeff22dd03d0 [file] [log] [blame]
robertphillips@google.com7d501ab2012-06-21 21:09:06 +00001/*
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.com7d501ab2012-06-21 21:09:06 +00008#ifndef GrSurface_DEFINED
9#define GrSurface_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkImageInfo.h"
12#include "include/core/SkRect.h"
13#include "include/gpu/GrBackendSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrTypes.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000015#include "src/gpu/GrGpuResource.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000016
robertphillipsb06e5a22014-09-30 06:58:20 -070017class GrRenderTarget;
bsalomonafbf2d62014-09-30 12:18:44 -070018class GrTexture;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000019
Brian Salomon197c5862019-08-21 22:08:36 -040020class GrSurface : public GrGpuResource {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000021public:
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000022 /**
Brian Salomon9f2b86c2019-10-22 10:37:46 -040023 * Retrieves the dimensions of the surface.
24 */
25 SkISize dimensions() const { return fDimensions; }
26
27 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000028 * Retrieves the width of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000029 */
Brian Salomon9f2b86c2019-10-22 10:37:46 -040030 int width() const { return fDimensions.width(); }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000031
32 /**
33 * Retrieves the height of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000034 */
Brian Salomon9f2b86c2019-10-22 10:37:46 -040035 int height() const { return fDimensions.height(); }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000036
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000037 /**
38 * Helper that gets the width and height of the surface as a bounding rectangle.
39 */
Brian Salomon9f2b86c2019-10-22 10:37:46 -040040 SkRect getBoundsRect() const { return SkRect::Make(this->dimensions()); }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000041
Greg Daniel4065d452018-11-16 15:43:41 -050042 virtual GrBackendFormat backendFormat() const = 0;
43
Greg Daniel456f9b52020-03-05 19:14:18 +000044 void setRelease(sk_sp<GrRefCntedCallback> releaseHelper) {
Greg Daniel2d35a1c2019-02-01 14:48:10 -050045 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 Daniel456f9b52020-03-05 19:14:18 +000053 void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
Brian Salomon694ff172020-11-04 16:54:28 -050054 this->setRelease(GrRefCntedCallback::Make(proc, ctx));
Greg Daniel2d35a1c2019-02-01 14:48:10 -050055 }
56
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000057 /**
Robert Phillipsc589b0b2017-04-17 07:53:07 -040058 * @return the texture associated with the surface, may be null.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000059 */
Robert Phillipsc589b0b2017-04-17 07:53:07 -040060 virtual GrTexture* asTexture() { return nullptr; }
61 virtual const GrTexture* asTexture() const { return nullptr; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000062
63 /**
Robert Phillipsc589b0b2017-04-17 07:53:07 -040064 * @return the render target underlying this surface, may be null.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000065 */
Robert Phillipsc589b0b2017-04-17 07:53:07 -040066 virtual GrRenderTarget* asRenderTarget() { return nullptr; }
67 virtual const GrRenderTarget* asRenderTarget() const { return nullptr; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000068
Brian Salomonf7f54332020-07-28 09:23:35 -040069 GrInternalSurfaceFlags flags() const { return fSurfaceFlags; }
robertphillipsb06e5a22014-09-30 06:58:20 -070070
Greg Daniel0eca74c2020-10-01 13:46:00 -040071 static size_t ComputeSize(const GrBackendFormat&, SkISize dimensions, int colorSamplesPerPixel,
72 GrMipmapped, bool binSize = false);
Robert Phillipsd6214d42016-11-07 08:23:48 -050073
Brian Salomonc67c31c2018-12-06 10:00:03 -050074 /**
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 White3c0a50f2020-01-16 18:19:54 -050080 bool framebufferOnly() const {
81 return fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly;
82 }
83
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040084 // Returns true if we are working with protected content.
85 bool isProtected() const { return fIsProtected == GrProtected::kYes; }
86
Stephen White3c0a50f2020-01-16 18:19:54 -050087 void setFramebufferOnly() {
88 SkASSERT(this->asRenderTarget());
89 fSurfaceFlags |= GrInternalSurfaceFlags::kFramebufferOnly;
90 }
91
bsalomonafbf2d62014-09-30 12:18:44 -070092protected:
Jim Van Verth5fab9092019-11-25 21:23:53 +000093 void setGLRTFBOIDIs0() {
94 SkASSERT(!this->requiresManualMSAAResolve());
Chris Dalton3f7932e2019-08-19 00:39:13 -060095 SkASSERT(!this->asTexture());
Greg Daniela070ed72018-04-26 16:31:38 -040096 SkASSERT(this->asRenderTarget());
Jim Van Verth5fab9092019-11-25 21:23:53 +000097 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
Greg Daniela070ed72018-04-26 16:31:38 -040098 }
Jim Van Verth5fab9092019-11-25 21:23:53 +000099 bool glRTFBOIDis0() const {
100 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
Greg Daniela070ed72018-04-26 16:31:38 -0400101 }
102
Chris Dalton3f7932e2019-08-19 00:39:13 -0600103 void setRequiresManualMSAAResolve() {
Jim Van Verth5fab9092019-11-25 21:23:53 +0000104 SkASSERT(!this->glRTFBOIDis0());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600105 SkASSERT(this->asRenderTarget());
106 fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
107 }
108 bool requiresManualMSAAResolve() const {
109 return fSurfaceFlags & GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
110 }
111
Brian Salomonc67c31c2018-12-06 10:00:03 -0500112 void setReadOnly() {
113 SkASSERT(!this->asRenderTarget());
114 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
115 }
116
Greg Daniel638b2e82020-08-27 14:29:00 -0400117 void setVkRTSupportsInputAttachment() {
118 SkASSERT(this->asRenderTarget());
119 fSurfaceFlags |= GrInternalSurfaceFlags::kVkRTSupportsInputAttachment;
120 }
121
Greg Danield51fa2f2020-01-22 16:53:38 -0500122 GrSurface(GrGpu* gpu, const SkISize& dimensions, GrProtected isProtected)
Brian Salomond34edf32017-05-19 15:45:48 -0400123 : INHERITED(gpu)
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400124 , fDimensions(dimensions)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400125 , fSurfaceFlags(GrInternalSurfaceFlags::kNone)
Brian Salomone8a766b2019-07-19 14:24:36 -0400126 , fIsProtected(isProtected) {}
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400127
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500128 ~GrSurface() override {
129 // check that invokeReleaseProc has been called (if needed)
130 SkASSERT(!fReleaseHelper);
131 }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000132
reed35a52612015-06-18 14:05:07 -0700133 void onRelease() override;
134 void onAbandon() override;
135
136private:
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400137 const char* getResourceType() const override { return "Surface"; }
138
Brian Salomon1c1c6662019-03-04 10:25:42 -0500139 // 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 Daniel2d35a1c2019-02-01 14:48:10 -0500143 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 Salomon9f2b86c2019-10-22 10:37:46 -0400149 SkISize fDimensions;
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500150 GrInternalSurfaceFlags fSurfaceFlags;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400151 GrProtected fIsProtected;
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500152 sk_sp<GrRefCntedCallback> fReleaseHelper;
Brian Salomond34edf32017-05-19 15:45:48 -0400153
John Stiles7571f9e2020-09-02 22:42:33 -0400154 using INHERITED = GrGpuResource;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000155};
156
bsalomonafbf2d62014-09-30 12:18:44 -0700157#endif