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