blob: 03aaf8273bce9b03b88dd4a623b2633b2145ecc6 [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"
14#include "include/gpu/GrGpuResource.h"
15#include "include/gpu/GrTypes.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 GrSurfacePriv;
19class GrTexture;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000020
Brian Salomon197c5862019-08-21 22:08:36 -040021class GrSurface : public GrGpuResource {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000022public:
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000023 /**
24 * Retrieves the width of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000025 */
Brian Salomond34edf32017-05-19 15:45:48 -040026 int width() const { return fWidth; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000027
28 /**
29 * Retrieves the height of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000030 */
Brian Salomond34edf32017-05-19 15:45:48 -040031 int height() const { return fHeight; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000032
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000033 /**
34 * Helper that gets the width and height of the surface as a bounding rectangle.
35 */
robertphillips13a7eee2016-08-31 15:06:24 -070036 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000037
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000038 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000039 * 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 */
Brian Salomond34edf32017-05-19 15:45:48 -040044 GrPixelConfig config() const { return fConfig; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000045
Greg Daniel4065d452018-11-16 15:43:41 -050046 virtual GrBackendFormat backendFormat() const = 0;
47
Brian Salomon197c5862019-08-21 22:08:36 -040048 SK_API void setRelease(sk_sp<GrRefCntedCallback> releaseHelper) {
Greg Daniel2d35a1c2019-02-01 14:48:10 -050049 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);
Brian Salomon197c5862019-08-21 22:08:36 -040057 SK_API void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -050058 sk_sp<GrRefCntedCallback> helper(new GrRefCntedCallback(proc, ctx));
Greg Daniel2d35a1c2019-02-01 14:48:10 -050059 this->setRelease(std::move(helper));
60 }
61
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000062 /**
Robert Phillipsc589b0b2017-04-17 07:53:07 -040063 * @return the texture associated with the surface, may be null.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000064 */
Robert Phillipsc589b0b2017-04-17 07:53:07 -040065 virtual GrTexture* asTexture() { return nullptr; }
66 virtual const GrTexture* asTexture() const { return nullptr; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000067
68 /**
Robert Phillipsc589b0b2017-04-17 07:53:07 -040069 * @return the render target underlying this surface, may be null.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000070 */
Robert Phillipsc589b0b2017-04-17 07:53:07 -040071 virtual GrRenderTarget* asRenderTarget() { return nullptr; }
72 virtual const GrRenderTarget* asRenderTarget() const { return nullptr; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000073
bsalomonafbf2d62014-09-30 12:18:44 -070074 /** Access methods that are only to be used within Skia code. */
75 inline GrSurfacePriv surfacePriv();
76 inline const GrSurfacePriv surfacePriv() const;
robertphillipsb06e5a22014-09-30 06:58:20 -070077
Brian Salomonbb5711a2017-05-17 13:49:59 -040078 static size_t ComputeSize(GrPixelConfig config, int width, int height, int colorSamplesPerPixel,
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040079 GrMipMapped, bool binSize = false);
Robert Phillipsd6214d42016-11-07 08:23:48 -050080
Brian Salomonc67c31c2018-12-06 10:00:03 -050081 /**
82 * The pixel values of this surface cannot be modified (e.g. doesn't support write pixels or
83 * MIP map level regen).
84 */
85 bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; }
86
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040087 // Returns true if we are working with protected content.
88 bool isProtected() const { return fIsProtected == GrProtected::kYes; }
89
bsalomonafbf2d62014-09-30 12:18:44 -070090protected:
Greg Daniela070ed72018-04-26 16:31:38 -040091 void setGLRTFBOIDIs0() {
Chris Dalton3f7932e2019-08-19 00:39:13 -060092 SkASSERT(!this->requiresManualMSAAResolve());
93 SkASSERT(!this->asTexture());
Greg Daniela070ed72018-04-26 16:31:38 -040094 SkASSERT(this->asRenderTarget());
95 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
96 }
97 bool glRTFBOIDis0() const {
98 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
99 }
100
Chris Dalton3f7932e2019-08-19 00:39:13 -0600101 void setRequiresManualMSAAResolve() {
102 SkASSERT(!this->glRTFBOIDis0());
103 SkASSERT(this->asRenderTarget());
104 fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
105 }
106 bool requiresManualMSAAResolve() const {
107 return fSurfaceFlags & GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
108 }
109
Brian Salomonc67c31c2018-12-06 10:00:03 -0500110 void setReadOnly() {
111 SkASSERT(!this->asRenderTarget());
112 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
113 }
114
bsalomonafbf2d62014-09-30 12:18:44 -0700115 // Provides access to methods that should be public within Skia code.
116 friend class GrSurfacePriv;
117
Brian Salomona9c22572019-08-05 12:57:09 -0400118 GrSurface(GrGpu* gpu, const SkISize& size, GrPixelConfig config, GrProtected isProtected)
Brian Salomond34edf32017-05-19 15:45:48 -0400119 : INHERITED(gpu)
Brian Salomona9c22572019-08-05 12:57:09 -0400120 , fConfig(config)
121 , fWidth(size.width())
122 , fHeight(size.height())
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400123 , fSurfaceFlags(GrInternalSurfaceFlags::kNone)
Brian Salomone8a766b2019-07-19 14:24:36 -0400124 , fIsProtected(isProtected) {}
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400125
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500126 ~GrSurface() override {
127 // check that invokeReleaseProc has been called (if needed)
128 SkASSERT(!fReleaseHelper);
129 }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000130
reed35a52612015-06-18 14:05:07 -0700131 void onRelease() override;
132 void onAbandon() override;
133
134private:
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400135 const char* getResourceType() const override { return "Surface"; }
136
Brian Salomon1c1c6662019-03-04 10:25:42 -0500137 // 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.
139 virtual void onSetRelease(sk_sp<GrRefCntedCallback>) {}
140
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500141 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;
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
bsalomon6d3fe022014-07-25 08:35:45 -0700154 typedef GrGpuResource INHERITED;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000155};
156
bsalomonafbf2d62014-09-30 12:18:44 -0700157#endif