blob: c36c08ff9a9d73c7a3620243973abf3cbfbcf3ca [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
bsalomoncc97ece2015-07-08 07:53:10 -070021class SK_API 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 Salomonb2c5dae2019-03-04 10:25:17 -050048 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);
57 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
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040078 static size_t WorstCaseSize(const GrSurfaceDesc& desc, bool binSize = false);
Brian Salomonbb5711a2017-05-17 13:49:59 -040079 static size_t ComputeSize(GrPixelConfig config, int width, int height, int colorSamplesPerPixel,
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040080 GrMipMapped, bool binSize = false);
Robert Phillipsd6214d42016-11-07 08:23:48 -050081
Brian Salomonc67c31c2018-12-06 10:00:03 -050082 /**
83 * The pixel values of this surface cannot be modified (e.g. doesn't support write pixels or
84 * MIP map level regen).
85 */
86 bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; }
87
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040088 // Returns true if we are working with protected content.
89 bool isProtected() const { return fIsProtected == GrProtected::kYes; }
90
bsalomonafbf2d62014-09-30 12:18:44 -070091protected:
Greg Daniela070ed72018-04-26 16:31:38 -040092 void setGLRTFBOIDIs0() {
93 SkASSERT(this->asRenderTarget());
94 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
95 }
96 bool glRTFBOIDis0() const {
97 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
98 }
99
Brian Salomonc67c31c2018-12-06 10:00:03 -0500100 void setReadOnly() {
101 SkASSERT(!this->asRenderTarget());
102 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
103 }
104
bsalomonafbf2d62014-09-30 12:18:44 -0700105 // Methods made available via GrSurfacePriv
bsalomon8d034a12014-09-22 12:21:08 -0700106 bool hasPendingRead() const;
107 bool hasPendingWrite() const;
108 bool hasPendingIO() const;
109
bsalomonafbf2d62014-09-30 12:18:44 -0700110 // Provides access to methods that should be public within Skia code.
111 friend class GrSurfacePriv;
112
kkinnunen2e6055b2016-04-22 01:48:29 -0700113 GrSurface(GrGpu* gpu, const GrSurfaceDesc& desc)
Brian Salomond34edf32017-05-19 15:45:48 -0400114 : INHERITED(gpu)
115 , fConfig(desc.fConfig)
116 , fWidth(desc.fWidth)
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400117 , fHeight(desc.fHeight)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400118 , fSurfaceFlags(GrInternalSurfaceFlags::kNone)
119 , fIsProtected(desc.fIsProtected) {
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000120 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400121
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500122 ~GrSurface() override {
123 // check that invokeReleaseProc has been called (if needed)
124 SkASSERT(!fReleaseHelper);
125 }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000126
reed35a52612015-06-18 14:05:07 -0700127 void onRelease() override;
128 void onAbandon() override;
129
130private:
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400131 const char* getResourceType() const override { return "Surface"; }
132
Brian Salomon1c1c6662019-03-04 10:25:42 -0500133 // Unmanaged backends (e.g. Vulkan) may want to specially handle the release proc in order to
134 // ensure it isn't called until GPU work related to the resource is completed.
135 virtual void onSetRelease(sk_sp<GrRefCntedCallback>) {}
136
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500137 void invokeReleaseProc() {
138 // Depending on the ref count of fReleaseHelper this may or may not actually trigger the
139 // ReleaseProc to be called.
140 fReleaseHelper.reset();
141 }
142
143 GrPixelConfig fConfig;
144 int fWidth;
145 int fHeight;
146 GrInternalSurfaceFlags fSurfaceFlags;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400147 GrProtected fIsProtected;
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500148 sk_sp<GrRefCntedCallback> fReleaseHelper;
Brian Salomond34edf32017-05-19 15:45:48 -0400149
bsalomon6d3fe022014-07-25 08:35:45 -0700150 typedef GrGpuResource INHERITED;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000151};
152
bsalomonafbf2d62014-09-30 12:18:44 -0700153#endif