blob: eeb63ad21bfd7f85d125430fb66b25360e6b36b7 [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"
13#include "GrResource.h"
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000014#include "SkRect.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000015
16class GrTexture;
17class GrRenderTarget;
18
19class GrSurface : public GrResource {
20public:
21 SK_DECLARE_INST_COUNT(GrSurface);
22
23 /**
24 * Retrieves the width of the surface.
25 *
26 * @return the width in texels
27 */
28 int width() const { return fDesc.fWidth; }
29
30 /**
31 * Retrieves the height of the surface.
32 *
33 * @return the height in texels
34 */
35 int height() const { return fDesc.fHeight; }
36
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000037 /**
38 * Helper that gets the width and height of the surface as a bounding rectangle.
39 */
40 void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
41 SkIntToScalar(this->height())); }
42
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +000043 GrSurfaceOrigin origin() const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000044 GrAssert(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
45 return fDesc.fOrigin;
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000046 }
47
48 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000049 * Retrieves the pixel config specified when the surface was created.
50 * For render targets this can be kUnknown_GrPixelConfig
51 * if client asked us to render to a target that has a pixel
52 * config that isn't equivalent with one of our configs.
53 */
54 GrPixelConfig config() const { return fDesc.fConfig; }
55
56 /**
57 * Return the descriptor describing the surface
58 */
59 const GrTextureDesc& desc() const { return fDesc; }
60
61 /**
62 * @return the texture associated with the surface, may be NULL.
63 */
64 virtual GrTexture* asTexture() = 0;
65 virtual const GrTexture* asTexture() const = 0;
66
67 /**
68 * @return the render target underlying this surface, may be NULL.
69 */
70 virtual GrRenderTarget* asRenderTarget() = 0;
71 virtual const GrRenderTarget* asRenderTarget() const = 0;
72
73 /**
bsalomon@google.com686bcb82013-04-09 15:04:12 +000074 * Checks whether this GrSurface refers to the same GPU object as other. This
75 * catches the case where a GrTexture and GrRenderTarget refer to the same
76 * GPU memory.
77 */
78 bool isSameAs(const GrSurface* other) const {
79 const GrRenderTarget* thisRT = this->asRenderTarget();
80 if (NULL != thisRT) {
81 return thisRT == other->asRenderTarget();
82 } else {
83 const GrTexture* thisTex = this->asTexture();
84 GrAssert(NULL != thisTex); // We must be one or the other
85 return thisTex == other->asTexture();
86 }
87 }
88
89 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000090 * Reads a rectangle of pixels from the surface.
91 * @param left left edge of the rectangle to read (inclusive)
92 * @param top top edge of the rectangle to read (inclusive)
93 * @param width width of rectangle to read in pixels.
94 * @param height height of rectangle to read in pixels.
95 * @param config the pixel config of the destination buffer
96 * @param buffer memory to read the rectangle into.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000097 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +000098 * packed.
99 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000100 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000101 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
102 * pixel config.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000103 */
104 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000105 GrPixelConfig config,
106 void* buffer,
107 size_t rowBytes = 0,
108 uint32_t pixelOpsFlags = 0) = 0;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000109
110 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000111 * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
112 * rectangle.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000113 * @param left left edge of the rectangle to write (inclusive)
114 * @param top top edge of the rectangle to write (inclusive)
115 * @param width width of rectangle to write in pixels.
116 * @param height height of rectangle to write in pixels.
117 * @param config the pixel config of the source buffer
118 * @param buffer memory to read the rectangle from.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000119 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000120 * packed.
121 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000122 */
123 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000124 GrPixelConfig config,
125 const void* buffer,
126 size_t rowBytes = 0,
127 uint32_t pixelOpsFlags = 0) = 0;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000128
129protected:
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000130 GrSurface(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
bsalomon@google.com72830222013-01-23 20:25:22 +0000131 : INHERITED(gpu, isWrapped)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000132 , fDesc(desc) {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000133 }
134
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000135 GrTextureDesc fDesc;
136
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000137private:
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000138 typedef GrResource INHERITED;
139};
140
141#endif // GrSurface_DEFINED