blob: 2aa80ab85a278735b85c4413d38e817fce3f44a6 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
bsalomon@google.com1da07462011-03-10 14:51:57 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrTexture_DEFINED
19#define GrTexture_DEFINED
20
21#include "GrRefCnt.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000022#include "GrClip.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000023#include "GrResource.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
25class GrTexture;
26
27/**
28 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
29 * A context's render target is set by setRenderTarget(). Render targets are
bsalomon@google.com1c13c962011-02-14 16:51:21 +000030 * created by a createTexture with the kRenderTarget_TextureFlag flag.
31 * Additionally, GrContext provides methods for creating GrRenderTargets
32 * that wrap externally created render targets.
reed@google.comac10a2d2010-12-22 21:39:39 +000033 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000034class GrRenderTarget : public GrResource {
reed@google.comac10a2d2010-12-22 21:39:39 +000035public:
36 /**
37 * @return the width of the rendertarget
38 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000039 int width() const { return fWidth; }
reed@google.comac10a2d2010-12-22 21:39:39 +000040 /**
41 * @return the height of the rendertarget
42 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000043 int height() const { return fHeight; }
44
45 /**
46 * @return the number of stencil bits in the rendertarget
47 */
48 int stencilBits() const { return fStencilBits; }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000049
reed@google.comac10a2d2010-12-22 21:39:39 +000050 /**
51 * @return the texture associated with the rendertarget, may be NULL.
52 */
53 GrTexture* asTexture() {return fTexture;}
54
bsalomon@google.com669fdc42011-04-05 17:08:27 +000055 /**
56 * Reads a rectangle of pixels from the render target.
57 * @param left left edge of the rectangle to read (inclusive)
58 * @param top top edge of the rectangle to read (inclusive)
59 * @param width width of rectangle to read in pixels.
60 * @param height height of rectangle to read in pixels.
61 * @param config the pixel config of the destination buffer
62 * @param buffer memory to read the rectangle into.
63 *
64 * @return true if the read succeeded, false if not. The read can fail
65 * because of a unsupported pixel config.
66 */
67 bool readPixels(int left, int top, int width, int height,
68 GrPixelConfig config, void* buffer);
69
reed@google.comac10a2d2010-12-22 21:39:39 +000070protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000071 GrRenderTarget(GrGpu* gpu,
72 GrTexture* texture,
bsalomon@google.comd302f142011-03-03 13:54:13 +000073 int width,
74 int height,
75 int stencilBits)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076 : INHERITED(gpu)
77 , fTexture(texture)
78 , fWidth(width)
79 , fHeight(height)
80 , fStencilBits(stencilBits)
81 {}
bsalomon@google.comd302f142011-03-03 13:54:13 +000082
83
reed@google.comac10a2d2010-12-22 21:39:39 +000084 GrTexture* fTexture;
bsalomon@google.comd302f142011-03-03 13:54:13 +000085 int fWidth;
86 int fHeight;
87 int fStencilBits;
88
89private:
90 // GrGpu keeps a cached clip in the render target to avoid redundantly
91 // rendering the clip into the same stencil buffer.
92 friend class GrGpu;
93 GrClip fLastStencilClip;
94
bsalomon@google.com8fe72472011-03-30 21:26:44 +000095 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +000096};
97
bsalomon@google.com8fe72472011-03-30 21:26:44 +000098class GrTexture : public GrResource {
reed@google.comac10a2d2010-12-22 21:39:39 +000099protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100 GrTexture(GrGpu* gpu,
101 int width,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000102 int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000103 GrPixelConfig config)
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104 : INHERITED(gpu)
105 , fWidth(width)
106 , fHeight(height)
107 , fConfig(config) {
108 // only make sense if alloc size is pow2
109 fShiftFixedX = 31 - Gr_clz(fWidth);
110 fShiftFixedY = 31 - Gr_clz(fHeight);
111 }
112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113public:
114 virtual ~GrTexture();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000117 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000118 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 * @return the width in texels
120 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000121 int width() const { return fWidth; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000122
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000124 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000125 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 * @return the height in texels
127 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000128 int height() const { return fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000129
130 /**
131 * Convert from texels to normalized texture coords for POT textures
132 * only.
133 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth));
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 return x >> fShiftFixedX; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000136 GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 return y >> fShiftFixedY; }
138
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000139 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 * Retrieves the pixel config specified when the texture was created.
141 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000142 GrPixelConfig config() const { return fConfig; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000143
144 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000145 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 */
147 size_t sizeInBytes() const {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000148 return fWidth * fHeight * GrBytesPerPixel(fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 }
150
151 /**
152 * Updates a subrectangle of texels in the texture.
153 *
154 * @param x left edge of rectangle to update
155 * @param y top edge of rectangle to update
156 * @param width width of rectangle to update
157 * @param height height of rectangle to update
158 * @param srcData width*height texels of data in same format that was used
159 * at texture creation.
160 */
161 virtual void uploadTextureData(uint32_t x,
162 uint32_t y,
163 uint32_t width,
164 uint32_t height,
165 const void* srcData) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166
167 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000168 * Reads a rectangle of pixels from the texture.
169 * @param left left edge of the rectangle to read (inclusive)
170 * @param top top edge of the rectangle to read (inclusive)
171 * @param width width of rectangle to read in pixels.
172 * @param height height of rectangle to read in pixels.
173 * @param config the pixel config of the destination buffer
174 * @param buffer memory to read the rectangle into.
175 *
176 * @return true if the read succeeded, false if not. The read can fail
177 * because of a unsupported pixel config.
178 */
179 bool readPixels(int left, int top, int width, int height,
180 GrPixelConfig config, void* buffer);
181
182 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000183 * Retrieves the render target underlying this texture that can be passed to
184 * GrGpu::setRenderTarget().
185 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 * @return handle to render target or undefined if the texture is not a
187 * render target
188 */
189 virtual GrRenderTarget* asRenderTarget() = 0;
190
191 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000192 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000193 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000194 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 */
bsalomon@google.com1da07462011-03-10 14:51:57 +0000196 virtual void releaseRenderTarget() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000197
198 /**
199 * Return the native ID or handle to the texture, depending on the
200 * platform. e.g. on opengl, return the texture ID.
201 */
202 virtual intptr_t getTextureHandle() = 0;
203
204#if GR_DEBUG
205 void validate() const {
206 this->INHERITED::validate();
207 }
208#else
209 void validate() const {}
210#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000211
212private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000213 int fWidth;
214 int fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 // these two shift a fixed-point value into normalized coordinates
216 // for this texture if the texture is power of two sized.
217 int fShiftFixedX;
218 int fShiftFixedY;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000219
220 GrPixelConfig fConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000222 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000223};
224
225#endif
226