blob: 4d40e2e8142cfdbe9fe1a8a9e360370553610cb3 [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
bsalomon6d3fe022014-07-25 08:35:45 -070021class GrSurface : public GrGpuResource {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000022public:
23 SK_DECLARE_INST_COUNT(GrSurface);
24
25 /**
26 * Retrieves the width of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000027 */
28 int width() const { return fDesc.fWidth; }
29
30 /**
31 * Retrieves the height of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000032 */
33 int height() const { return fDesc.fHeight; }
34
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000035 /**
36 * Helper that gets the width and height of the surface as a bounding rectangle.
37 */
38 void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
39 SkIntToScalar(this->height())); }
40
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +000041 GrSurfaceOrigin origin() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000042 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000043 return fDesc.fOrigin;
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000044 }
45
46 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000047 * Retrieves the pixel config specified when the surface was created.
48 * For render targets this can be kUnknown_GrPixelConfig
49 * if client asked us to render to a target that has a pixel
50 * config that isn't equivalent with one of our configs.
51 */
52 GrPixelConfig config() const { return fDesc.fConfig; }
53
54 /**
55 * Return the descriptor describing the surface
56 */
bsalomonf2703d82014-10-28 14:33:06 -070057 const GrSurfaceDesc& desc() const { return fDesc; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000058
59 /**
60 * @return the texture associated with the surface, may be NULL.
61 */
bsalomon37dd3312014-11-03 08:47:23 -080062 virtual GrTexture* asTexture() { return NULL; }
63 virtual const GrTexture* asTexture() const { return NULL; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000064
65 /**
66 * @return the render target underlying this surface, may be NULL.
67 */
bsalomon37dd3312014-11-03 08:47:23 -080068 virtual GrRenderTarget* asRenderTarget() { return NULL; }
69 virtual const GrRenderTarget* asRenderTarget() const { return NULL; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000070
71 /**
72 * Reads a rectangle of pixels from the surface.
73 * @param left left edge of the rectangle to read (inclusive)
74 * @param top top edge of the rectangle to read (inclusive)
75 * @param width width of rectangle to read in pixels.
76 * @param height height of rectangle to read in pixels.
77 * @param config the pixel config of the destination buffer
78 * @param buffer memory to read the rectangle into.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000079 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +000080 * packed.
81 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000082 *
bsalomon@google.com0342a852012-08-20 19:22:38 +000083 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
84 * pixel config.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000085 */
bsalomon81beccc2014-10-13 12:32:55 -070086 bool readPixels(int left, int top, int width, int height,
87 GrPixelConfig config,
88 void* buffer,
89 size_t rowBytes = 0,
90 uint32_t pixelOpsFlags = 0);
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000091
92 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +000093 * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
94 * rectangle.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000095 * @param left left edge of the rectangle to write (inclusive)
96 * @param top top edge of the rectangle to write (inclusive)
97 * @param width width of rectangle to write in pixels.
98 * @param height height of rectangle to write in pixels.
99 * @param config the pixel config of the source buffer
100 * @param buffer memory to read the rectangle from.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000101 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000102 * packed.
103 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
bsalomone3059732014-10-14 11:47:22 -0700104 *
105 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
106 * pixel config.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000107 */
bsalomon81beccc2014-10-13 12:32:55 -0700108 bool writePixels(int left, int top, int width, int height,
109 GrPixelConfig config,
110 const void* buffer,
111 size_t rowBytes = 0,
112 uint32_t pixelOpsFlags = 0);
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000113
bsalomonf80bfed2014-10-07 05:56:02 -0700114 /**
115 * After this returns any pending writes to the surface will be issued to the backend 3D API.
116 */
117 void flushWrites();
118
bsalomon87a94eb2014-11-03 14:28:32 -0800119
120 /**
121 * After this returns any pending writes to the surface will be issued to the backend 3D API and
122 * if the surface has MSAA it will be resolved.
123 */
124 void prepareForExternalRead();
125
bsalomonafbf2d62014-09-30 12:18:44 -0700126 /** Access methods that are only to be used within Skia code. */
127 inline GrSurfacePriv surfacePriv();
128 inline const GrSurfacePriv surfacePriv() const;
robertphillipsb06e5a22014-09-30 06:58:20 -0700129
reedde499882015-06-18 13:41:40 -0700130 typedef void* ReleaseCtx;
131 typedef void (*ReleaseProc)(ReleaseCtx);
132
133 void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
134 fReleaseProc = proc;
135 fReleaseCtx = ctx;
136 }
137
bsalomonafbf2d62014-09-30 12:18:44 -0700138protected:
139 // Methods made available via GrSurfacePriv
140 SkImageInfo info() const;
141 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
bsalomon5236cf42015-01-14 10:42:08 -0800149 GrSurface(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
150 : INHERITED(gpu, lifeCycle)
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 }
reed35a52612015-06-18 14:05:07 -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