blob: 24eb39a81b5aef7c4c195906f0bb94a043d5359c [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"
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000014#include "SkRect.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000015
bsalomonc0eb9b92014-09-29 14:20:11 -070016class GrTexture;
robertphillipsb06e5a22014-09-30 06:58:20 -070017class GrRenderTarget;
18struct SkImageInfo;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000019
bsalomon6d3fe022014-07-25 08:35:45 -070020class GrSurface : public GrGpuResource {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000021public:
22 SK_DECLARE_INST_COUNT(GrSurface);
23
24 /**
25 * Retrieves the width of the surface.
26 *
27 * @return the width in texels
28 */
29 int width() const { return fDesc.fWidth; }
30
31 /**
32 * Retrieves the height of the surface.
33 *
34 * @return the height in texels
35 */
36 int height() const { return fDesc.fHeight; }
37
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000038 /**
39 * Helper that gets the width and height of the surface as a bounding rectangle.
40 */
41 void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
42 SkIntToScalar(this->height())); }
43
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +000044 GrSurfaceOrigin origin() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000045 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000046 return fDesc.fOrigin;
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000047 }
48
49 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000050 * Retrieves the pixel config specified when the surface was created.
51 * For render targets this can be kUnknown_GrPixelConfig
52 * if client asked us to render to a target that has a pixel
53 * config that isn't equivalent with one of our configs.
54 */
55 GrPixelConfig config() const { return fDesc.fConfig; }
56
57 /**
58 * Return the descriptor describing the surface
59 */
60 const GrTextureDesc& desc() const { return fDesc; }
61
robertphillipsb06e5a22014-09-30 06:58:20 -070062 SkImageInfo info() const;
63
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000064 /**
65 * @return the texture associated with the surface, may be NULL.
66 */
67 virtual GrTexture* asTexture() = 0;
68 virtual const GrTexture* asTexture() const = 0;
69
70 /**
71 * @return the render target underlying this surface, may be NULL.
72 */
73 virtual GrRenderTarget* asRenderTarget() = 0;
74 virtual const GrRenderTarget* asRenderTarget() const = 0;
75
76 /**
robertphillipsb06e5a22014-09-30 06:58:20 -070077 * Checks whether this GrSurface refers to the same GPU object as other. This
78 * catches the case where a GrTexture and GrRenderTarget refer to the same
79 * GPU memory.
80 */
81 bool isSameAs(const GrSurface* other) const {
82 const GrRenderTarget* thisRT = this->asRenderTarget();
83 if (thisRT) {
84 return thisRT == other->asRenderTarget();
85 } else {
86 const GrTexture* thisTex = this->asTexture();
87 SkASSERT(thisTex); // We must be one or the other
88 return thisTex == other->asTexture();
89 }
90 }
91
92 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000093 * Reads a rectangle of pixels from the surface.
94 * @param left left edge of the rectangle to read (inclusive)
95 * @param top top edge of the rectangle to read (inclusive)
96 * @param width width of rectangle to read in pixels.
97 * @param height height of rectangle to read in pixels.
98 * @param config the pixel config of the destination buffer
99 * @param buffer memory to read the rectangle into.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000100 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000101 * packed.
102 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000103 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000104 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
105 * pixel config.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000106 */
107 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000108 GrPixelConfig config,
109 void* buffer,
110 size_t rowBytes = 0,
111 uint32_t pixelOpsFlags = 0) = 0;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000112
113 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000114 * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
115 * rectangle.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000116 * @param left left edge of the rectangle to write (inclusive)
117 * @param top top edge of the rectangle to write (inclusive)
118 * @param width width of rectangle to write in pixels.
119 * @param height height of rectangle to write in pixels.
120 * @param config the pixel config of the source buffer
121 * @param buffer memory to read the rectangle from.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000122 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000123 * packed.
124 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000125 */
126 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000127 GrPixelConfig config,
128 const void* buffer,
129 size_t rowBytes = 0,
130 uint32_t pixelOpsFlags = 0) = 0;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000131
robertphillipsb06e5a22014-09-30 06:58:20 -0700132 /**
133 * Write the contents of the surface to a PNG. Returns true if successful.
134 * @param filename Full path to desired file
135 */
bsalomonc0eb9b92014-09-29 14:20:11 -0700136 bool savePixels(const char* filename);
robertphillipsb06e5a22014-09-30 06:58:20 -0700137
bsalomon8d034a12014-09-22 12:21:08 -0700138 bool hasPendingRead() const;
139 bool hasPendingWrite() const;
140 bool hasPendingIO() const;
141
robertphillipsb06e5a22014-09-30 06:58:20 -0700142protected:
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000143 GrSurface(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
bsalomon@google.com72830222013-01-23 20:25:22 +0000144 : INHERITED(gpu, isWrapped)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000145 , fDesc(desc) {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000146 }
147
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000148 GrTextureDesc fDesc;
149
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000150private:
bsalomon6d3fe022014-07-25 08:35:45 -0700151 typedef GrGpuResource INHERITED;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000152};
153
robertphillipsb06e5a22014-09-30 06:58:20 -0700154#endif // GrSurface_DEFINED