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/GrBackendSurface.h"
14#include "include/gpu/GrTypes.h"
15#include "src/gpu/GrGpuResource.h"
16
17class GrRenderTarget;
18class GrTexture;
19
20class GrSurface : public GrGpuResource {
21public:
22 /**
23 * Retrieves the dimensions of the surface.
24 */
25 SkISize dimensions() const { return fDimensions; }
26
27 /**
28 * Retrieves the width of the surface.
29 */
30 int width() const { return fDimensions.width(); }
31
32 /**
33 * Retrieves the height of the surface.
34 */
35 int height() const { return fDimensions.height(); }
36
37 /**
38 * Helper that gets the width and height of the surface as a bounding rectangle.
39 */
40 SkRect getBoundsRect() const { return SkRect::Make(this->dimensions()); }
41
42 virtual GrBackendFormat backendFormat() const = 0;
43
44 void setRelease(sk_sp<GrRefCntedCallback> releaseHelper) {
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);
53 void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
54 sk_sp<GrRefCntedCallback> helper(new GrRefCntedCallback(proc, ctx));
55 this->setRelease(std::move(helper));
56 }
57
58 /**
59 * @return the texture associated with the surface, may be null.
60 */
61 virtual GrTexture* asTexture() { return nullptr; }
62 virtual const GrTexture* asTexture() const { return nullptr; }
63
64 /**
65 * @return the render target underlying this surface, may be null.
66 */
67 virtual GrRenderTarget* asRenderTarget() { return nullptr; }
68 virtual const GrRenderTarget* asRenderTarget() const { return nullptr; }
69
70 GrInternalSurfaceFlags flags() const { return fSurfaceFlags; }
71
72 static size_t ComputeSize(const GrCaps&, const GrBackendFormat&, SkISize dimensions,
73 int colorSamplesPerPixel, GrMipmapped, bool binSize = false);
74
75 /**
76 * The pixel values of this surface cannot be modified (e.g. doesn't support write pixels or
77 * MIP map level regen).
78 */
79 bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; }
80
81 bool framebufferOnly() const {
82 return fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly;
83 }
84
85 // Returns true if we are working with protected content.
86 bool isProtected() const { return fIsProtected == GrProtected::kYes; }
87
88 void setFramebufferOnly() {
89 SkASSERT(this->asRenderTarget());
90 fSurfaceFlags |= GrInternalSurfaceFlags::kFramebufferOnly;
91 }
92
93protected:
94 void setGLRTFBOIDIs0() {
95 SkASSERT(!this->requiresManualMSAAResolve());
96 SkASSERT(!this->asTexture());
97 SkASSERT(this->asRenderTarget());
98 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
99 }
100 bool glRTFBOIDis0() const {
101 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
102 }
103
104 void setRequiresManualMSAAResolve() {
105 SkASSERT(!this->glRTFBOIDis0());
106 SkASSERT(this->asRenderTarget());
107 fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
108 }
109 bool requiresManualMSAAResolve() const {
110 return fSurfaceFlags & GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
111 }
112
113 void setReadOnly() {
114 SkASSERT(!this->asRenderTarget());
115 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
116 }
117
118 GrSurface(GrGpu* gpu, const SkISize& dimensions, GrProtected isProtected)
119 : INHERITED(gpu)
120 , fDimensions(dimensions)
121 , fSurfaceFlags(GrInternalSurfaceFlags::kNone)
122 , fIsProtected(isProtected) {}
123
124 ~GrSurface() override {
125 // check that invokeReleaseProc has been called (if needed)
126 SkASSERT(!fReleaseHelper);
127 }
128
129 void onRelease() override;
130 void onAbandon() override;
131
132private:
133 const char* getResourceType() const override { return "Surface"; }
134
135 // Unmanaged backends (e.g. Vulkan) may want to specially handle the release proc in order to
136 // ensure it isn't called until GPU work related to the resource is completed.
137 virtual void onSetRelease(sk_sp<GrRefCntedCallback>) {}
138
139 void invokeReleaseProc() {
140 // Depending on the ref count of fReleaseHelper this may or may not actually trigger the
141 // ReleaseProc to be called.
142 fReleaseHelper.reset();
143 }
144
145 SkISize fDimensions;
146 GrInternalSurfaceFlags fSurfaceFlags;
147 GrProtected fIsProtected;
148 sk_sp<GrRefCntedCallback> fReleaseHelper;
149
150 typedef GrGpuResource INHERITED;
151};
152
153#endif
154