blob: d7aa2672d88e99d24cc806938a5e30b32965077d [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"
14
15class GrTexture;
16class GrRenderTarget;
17
18class GrSurface : public GrResource {
19public:
20 SK_DECLARE_INST_COUNT(GrSurface);
21
22 /**
23 * Retrieves the width of the surface.
24 *
25 * @return the width in texels
26 */
27 int width() const { return fDesc.fWidth; }
28
29 /**
30 * Retrieves the height of the surface.
31 *
32 * @return the height in texels
33 */
34 int height() const { return fDesc.fHeight; }
35
36 /**
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000037 * Some surfaces will be stored such that the upper and left edges of the content meet at the
38 * the origin (in texture coord space) and for other surfaces the lower and left edges meet at
39 * the origin. Render-targets are always consistent with the convention of the underlying
40 * backend API to make it easier to mix native backend rendering with Skia rendering. Wrapped
41 * backend surfaces always use the backend's convention as well.
42 */
43 enum Origin {
44 kTopLeft_Origin,
45 kBottomLeft_Origin,
46 };
47 Origin origin() const {
48 GrAssert(kTopLeft_Origin == fOrigin || kBottomLeft_Origin == fOrigin);
49 return fOrigin;
50 }
51
52 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000053 * Retrieves the pixel config specified when the surface was created.
54 * For render targets this can be kUnknown_GrPixelConfig
55 * if client asked us to render to a target that has a pixel
56 * config that isn't equivalent with one of our configs.
57 */
58 GrPixelConfig config() const { return fDesc.fConfig; }
59
60 /**
61 * Return the descriptor describing the surface
62 */
63 const GrTextureDesc& desc() const { return fDesc; }
64
65 /**
66 * @return the texture associated with the surface, may be NULL.
67 */
68 virtual GrTexture* asTexture() = 0;
69 virtual const GrTexture* asTexture() const = 0;
70
71 /**
72 * @return the render target underlying this surface, may be NULL.
73 */
74 virtual GrRenderTarget* asRenderTarget() = 0;
75 virtual const GrRenderTarget* asRenderTarget() const = 0;
76
77 /**
78 * Reads a rectangle of pixels from the surface.
79 * @param left left edge of the rectangle to read (inclusive)
80 * @param top top edge of the rectangle to read (inclusive)
81 * @param width width of rectangle to read in pixels.
82 * @param height height of rectangle to read in pixels.
83 * @param config the pixel config of the destination buffer
84 * @param buffer memory to read the rectangle into.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000085 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +000086 * packed.
87 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000088 *
bsalomon@google.com0342a852012-08-20 19:22:38 +000089 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
90 * pixel config.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000091 */
92 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000093 GrPixelConfig config,
94 void* buffer,
95 size_t rowBytes = 0,
96 uint32_t pixelOpsFlags = 0) = 0;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000097
98 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +000099 * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
100 * rectangle.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000101 * @param left left edge of the rectangle to write (inclusive)
102 * @param top top edge of the rectangle to write (inclusive)
103 * @param width width of rectangle to write in pixels.
104 * @param height height of rectangle to write in pixels.
105 * @param config the pixel config of the source buffer
106 * @param buffer memory to read the rectangle from.
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000107 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000108 * packed.
109 * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000110 */
111 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000112 GrPixelConfig config,
113 const void* buffer,
114 size_t rowBytes = 0,
115 uint32_t pixelOpsFlags = 0) = 0;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000116
117protected:
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000118 GrSurface(GrGpu* gpu, const GrTextureDesc& desc, Origin origin)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000119 : INHERITED(gpu)
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000120 , fDesc(desc)
121 , fOrigin(origin) {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000122 }
123
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000124 GrTextureDesc fDesc;
125
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000126private:
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000127 Origin fOrigin;
128
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000129 typedef GrResource INHERITED;
130};
131
132#endif // GrSurface_DEFINED