blob: 3211a1e90a22aa8ef56307731c3462ae89de8b80 [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
11#include "GrTypes.h"
Greg Daniel4065d452018-11-16 15:43:41 -050012#include "GrBackendSurface.h"
bsalomon6d3fe022014-07-25 08:35:45 -070013#include "GrGpuResource.h"
bsalomonafbf2d62014-09-30 12:18:44 -070014#include "SkImageInfo.h"
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000015#include "SkRect.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
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000048 /**
Robert Phillipsc589b0b2017-04-17 07:53:07 -040049 * @return the texture associated with the surface, may be null.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000050 */
Robert Phillipsc589b0b2017-04-17 07:53:07 -040051 virtual GrTexture* asTexture() { return nullptr; }
52 virtual const GrTexture* asTexture() const { return nullptr; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000053
54 /**
Robert Phillipsc589b0b2017-04-17 07:53:07 -040055 * @return the render target underlying this surface, may be null.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000056 */
Robert Phillipsc589b0b2017-04-17 07:53:07 -040057 virtual GrRenderTarget* asRenderTarget() { return nullptr; }
58 virtual const GrRenderTarget* asRenderTarget() const { return nullptr; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000059
bsalomonafbf2d62014-09-30 12:18:44 -070060 /** Access methods that are only to be used within Skia code. */
61 inline GrSurfacePriv surfacePriv();
62 inline const GrSurfacePriv surfacePriv() const;
robertphillipsb06e5a22014-09-30 06:58:20 -070063
Robert Phillipsb4460882016-11-17 14:43:51 -050064 static size_t WorstCaseSize(const GrSurfaceDesc& desc, bool useNextPow2 = false);
Brian Salomonbb5711a2017-05-17 13:49:59 -040065 static size_t ComputeSize(GrPixelConfig config, int width, int height, int colorSamplesPerPixel,
Greg Daniele252f082017-10-23 16:05:23 -040066 GrMipMapped, bool useNextPow2 = false);
Robert Phillipsd6214d42016-11-07 08:23:48 -050067
Brian Salomonc67c31c2018-12-06 10:00:03 -050068 /**
69 * The pixel values of this surface cannot be modified (e.g. doesn't support write pixels or
70 * MIP map level regen).
71 */
72 bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; }
73
bsalomonafbf2d62014-09-30 12:18:44 -070074protected:
Robert Phillipsfe0253f2018-03-16 16:47:25 -040075 void setHasMixedSamples() {
76 SkASSERT(this->asRenderTarget());
77 fSurfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
78 }
79 bool hasMixedSamples() const { return fSurfaceFlags & GrInternalSurfaceFlags::kMixedSampled; }
80
81 void setSupportsWindowRects() {
82 SkASSERT(this->asRenderTarget());
83 fSurfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
84 }
85 bool supportsWindowRects() const {
86 return fSurfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport;
87 }
88
Greg Daniela070ed72018-04-26 16:31:38 -040089 void setGLRTFBOIDIs0() {
90 SkASSERT(this->asRenderTarget());
91 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
92 }
93 bool glRTFBOIDis0() const {
94 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
95 }
96
Brian Salomonc67c31c2018-12-06 10:00:03 -050097 void setReadOnly() {
98 SkASSERT(!this->asRenderTarget());
99 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
100 }
101
bsalomonafbf2d62014-09-30 12:18:44 -0700102 // Methods made available via GrSurfacePriv
bsalomon8d034a12014-09-22 12:21:08 -0700103 bool hasPendingRead() const;
104 bool hasPendingWrite() const;
105 bool hasPendingIO() const;
106
bsalomonafbf2d62014-09-30 12:18:44 -0700107 // Provides access to methods that should be public within Skia code.
108 friend class GrSurfacePriv;
109
kkinnunen2e6055b2016-04-22 01:48:29 -0700110 GrSurface(GrGpu* gpu, const GrSurfaceDesc& desc)
Brian Salomond34edf32017-05-19 15:45:48 -0400111 : INHERITED(gpu)
112 , fConfig(desc.fConfig)
113 , fWidth(desc.fWidth)
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400114 , fHeight(desc.fHeight)
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000115 , fSurfaceFlags(GrInternalSurfaceFlags::kNone) {
116 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400117
Greg Danielcef213c2017-04-21 11:52:27 -0400118 ~GrSurface() override {}
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000119
reed35a52612015-06-18 14:05:07 -0700120 void onRelease() override;
121 void onAbandon() override;
122
123private:
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400124 const char* getResourceType() const override { return "Surface"; }
125
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400126 GrPixelConfig fConfig;
127 int fWidth;
128 int fHeight;
129 GrInternalSurfaceFlags fSurfaceFlags;
Brian Salomond34edf32017-05-19 15:45:48 -0400130
bsalomon6d3fe022014-07-25 08:35:45 -0700131 typedef GrGpuResource INHERITED;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000132};
133
bsalomonafbf2d62014-09-30 12:18:44 -0700134#endif