blob: 8c6930a7a78c8070c35a5f94bbd8680f233c0880 [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
8
9#ifndef GrSurface_DEFINED
10#define GrSurface_DEFINED
11
12#include "GrTypes.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 */
26 int width() const { return fDesc.fWidth; }
27
28 /**
29 * Retrieves the height of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000030 */
31 int height() const { return fDesc.fHeight; }
32
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 */
36 void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
37 SkIntToScalar(this->height())); }
38
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +000039 GrSurfaceOrigin origin() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000040 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000041 return fDesc.fOrigin;
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000042 }
43
44 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000045 * Retrieves the pixel config specified when the surface was created.
46 * For render targets this can be kUnknown_GrPixelConfig
47 * if client asked us to render to a target that has a pixel
48 * config that isn't equivalent with one of our configs.
49 */
50 GrPixelConfig config() const { return fDesc.fConfig; }
51
52 /**
53 * Return the descriptor describing the surface
54 */
bsalomonf2703d82014-10-28 14:33:06 -070055 const GrSurfaceDesc& desc() const { return fDesc; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000056
57 /**
58 * @return the texture associated with the surface, may be NULL.
59 */
bsalomon37dd3312014-11-03 08:47:23 -080060 virtual GrTexture* asTexture() { return NULL; }
61 virtual const GrTexture* asTexture() const { return NULL; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000062
63 /**
64 * @return the render target underlying this surface, may be NULL.
65 */
bsalomon37dd3312014-11-03 08:47:23 -080066 virtual GrRenderTarget* asRenderTarget() { return NULL; }
67 virtual const GrRenderTarget* asRenderTarget() const { return NULL; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000068
69 /**
70 * Reads a rectangle of pixels from the surface.
71 * @param left left edge of the rectangle to read (inclusive)
72 * @param top top edge of the rectangle to read (inclusive)
73 * @param width width of rectangle to read in pixels.
74 * @param height height of rectangle to read in pixels.
75 * @param config the pixel config of the destination buffer
76 * @param buffer memory to read the rectangle into.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000077 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +000078 * packed.
79 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000080 *
bsalomon@google.com0342a852012-08-20 19:22:38 +000081 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
82 * pixel config.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000083 */
bsalomon81beccc2014-10-13 12:32:55 -070084 bool readPixels(int left, int top, int width, int height,
85 GrPixelConfig config,
86 void* buffer,
87 size_t rowBytes = 0,
88 uint32_t pixelOpsFlags = 0);
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000089
90 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +000091 * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
92 * rectangle.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000093 * @param left left edge of the rectangle to write (inclusive)
94 * @param top top edge of the rectangle to write (inclusive)
95 * @param width width of rectangle to write in pixels.
96 * @param height height of rectangle to write in pixels.
97 * @param config the pixel config of the source buffer
98 * @param buffer memory to read the rectangle from.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000099 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000100 * packed.
101 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
bsalomone3059732014-10-14 11:47:22 -0700102 *
brianosman2d1ee792016-05-05 12:24:31 -0700103 * @return true if the write succeeded, false if not. The write can fail because of an
cblume61214052016-01-26 09:10:48 -0800104 * unsupported pixel config.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000105 */
bsalomon81beccc2014-10-13 12:32:55 -0700106 bool writePixels(int left, int top, int width, int height,
107 GrPixelConfig config,
108 const void* buffer,
109 size_t rowBytes = 0,
110 uint32_t pixelOpsFlags = 0);
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000111
bsalomonf80bfed2014-10-07 05:56:02 -0700112 /**
113 * After this returns any pending writes to the surface will be issued to the backend 3D API.
114 */
115 void flushWrites();
116
bsalomon87a94eb2014-11-03 14:28:32 -0800117
118 /**
bsalomonc49e8682015-06-30 11:37:35 -0700119 * After this returns any pending surface IO will be issued to the backend 3D API and
bsalomon87a94eb2014-11-03 14:28:32 -0800120 * if the surface has MSAA it will be resolved.
121 */
bsalomonc49e8682015-06-30 11:37:35 -0700122 void prepareForExternalIO();
mtklein2766c002015-06-26 11:45:03 -0700123
bsalomonafbf2d62014-09-30 12:18:44 -0700124 /** Access methods that are only to be used within Skia code. */
125 inline GrSurfacePriv surfacePriv();
126 inline const GrSurfacePriv surfacePriv() const;
robertphillipsb06e5a22014-09-30 06:58:20 -0700127
reedde499882015-06-18 13:41:40 -0700128 typedef void* ReleaseCtx;
129 typedef void (*ReleaseProc)(ReleaseCtx);
mtklein2766c002015-06-26 11:45:03 -0700130
reedde499882015-06-18 13:41:40 -0700131 void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
132 fReleaseProc = proc;
133 fReleaseCtx = ctx;
134 }
135
robertphillips6e83ac72015-08-13 05:19:14 -0700136 static size_t WorseCaseSize(const GrSurfaceDesc& desc);
137
bsalomonafbf2d62014-09-30 12:18:44 -0700138protected:
139 // Methods made available via GrSurfacePriv
bsalomon74f681d2015-06-23 14:38:48 -0700140 SkImageInfo info(SkAlphaType) const;
bsalomonafbf2d62014-09-30 12:18:44 -0700141 bool savePixels(const char* filename);
bsalomon8d034a12014-09-22 12:21:08 -0700142 bool hasPendingRead() const;
143 bool hasPendingWrite() const;
144 bool hasPendingIO() const;
145
bsalomonafbf2d62014-09-30 12:18:44 -0700146 // Provides access to methods that should be public within Skia code.
147 friend class GrSurfacePriv;
148
kkinnunen2e6055b2016-04-22 01:48:29 -0700149 GrSurface(GrGpu* gpu, const GrSurfaceDesc& desc)
150 : INHERITED(gpu)
reedde499882015-06-18 13:41:40 -0700151 , fDesc(desc)
152 , fReleaseProc(NULL)
153 , fReleaseCtx(NULL)
154 {}
155
156 ~GrSurface() override {
157 // check that invokeReleaseProc has been called (if needed)
158 SkASSERT(NULL == fReleaseProc);
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000159 }
160
bsalomonf2703d82014-10-28 14:33:06 -0700161 GrSurfaceDesc fDesc;
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000162
reed35a52612015-06-18 14:05:07 -0700163 void onRelease() override;
164 void onAbandon() override;
165
166private:
reedde499882015-06-18 13:41:40 -0700167 void invokeReleaseProc() {
168 if (fReleaseProc) {
169 fReleaseProc(fReleaseCtx);
170 fReleaseProc = NULL;
171 }
172 }
mtklein2766c002015-06-26 11:45:03 -0700173
reedde499882015-06-18 13:41:40 -0700174 ReleaseProc fReleaseProc;
175 ReleaseCtx fReleaseCtx;
176
bsalomon6d3fe022014-07-25 08:35:45 -0700177 typedef GrGpuResource INHERITED;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000178};
179
bsalomonafbf2d62014-09-30 12:18:44 -0700180#endif