blob: 5b3ed095178f85519d0886a6289e57fbb51acbde [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
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"
22
23class GrTexture;
24
25/**
26 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
27 * A context's render target is set by setRenderTarget(). Render targets are
bsalomon@google.com1c13c962011-02-14 16:51:21 +000028 * created by a createTexture with the kRenderTarget_TextureFlag flag.
29 * Additionally, GrContext provides methods for creating GrRenderTargets
30 * that wrap externally created render targets.
reed@google.comac10a2d2010-12-22 21:39:39 +000031 */
32class GrRenderTarget : public GrRefCnt {
33public:
34 /**
35 * @return the width of the rendertarget
36 */
37 virtual uint32_t width() const = 0;
38 /**
39 * @return the height of the rendertarget
40 */
41 virtual uint32_t height() const = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000042
reed@google.comac10a2d2010-12-22 21:39:39 +000043 /**
44 * @return the texture associated with the rendertarget, may be NULL.
45 */
46 GrTexture* asTexture() {return fTexture;}
47
48protected:
49 GrRenderTarget(GrTexture* texture) : fTexture(texture) {}
50 GrTexture* fTexture;
51};
52
53class GrTexture : public GrRefCnt {
54public:
55 enum PixelConfig {
56 kUnknown_PixelConfig,
57 kAlpha_8_PixelConfig,
58 kIndex_8_PixelConfig,
59 kRGB_565_PixelConfig,
60 kRGBA_4444_PixelConfig, //!< premultiplied
61 kRGBA_8888_PixelConfig, //!< premultiplied
62 kRGBX_8888_PixelConfig, //!< treat the alpha channel as opaque
63 };
64 static size_t BytesPerPixel(PixelConfig);
65 static bool PixelConfigIsOpaque(PixelConfig);
66
67protected:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000068 GrTexture(int width,
69 int height,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000070 PixelConfig config) :
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000071 fWidth(width),
72 fHeight(height),
reed@google.comac10a2d2010-12-22 21:39:39 +000073 fConfig(config) {
74 // only make sense if alloc size is pow2
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000075 fShiftFixedX = 31 - Gr_clz(fWidth);
76 fShiftFixedY = 31 - Gr_clz(fHeight);
reed@google.comac10a2d2010-12-22 21:39:39 +000077 }
78public:
79 virtual ~GrTexture();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000080
reed@google.comac10a2d2010-12-22 21:39:39 +000081 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000082 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000083 *
reed@google.comac10a2d2010-12-22 21:39:39 +000084 * @return the width in texels
85 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000086 int width() const { return fWidth; }
reed@google.comac10a2d2010-12-22 21:39:39 +000087 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000088 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000089 *
reed@google.comac10a2d2010-12-22 21:39:39 +000090 * @return the height in texels
91 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000092 int height() const { return fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +000093
94 /**
95 * Convert from texels to normalized texture coords for POT textures
96 * only.
97 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000098 GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth));
reed@google.comac10a2d2010-12-22 21:39:39 +000099 return x >> fShiftFixedX; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000100 GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 return y >> fShiftFixedY; }
102
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000103 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 * Retrieves the pixel config specified when the texture was created.
105 */
106 PixelConfig config() const { return fConfig; }
107
108 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000109 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 */
111 size_t sizeInBytes() const {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000112 return fWidth * fHeight * BytesPerPixel(fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 }
114
115 /**
116 * Updates a subrectangle of texels in the texture.
117 *
118 * @param x left edge of rectangle to update
119 * @param y top edge of rectangle to update
120 * @param width width of rectangle to update
121 * @param height height of rectangle to update
122 * @param srcData width*height texels of data in same format that was used
123 * at texture creation.
124 */
125 virtual void uploadTextureData(uint32_t x,
126 uint32_t y,
127 uint32_t width,
128 uint32_t height,
129 const void* srcData) = 0;
130 /**
131 * Indicates that GPU context in which this texture was created is destroyed
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000132 * and that Ganesh should not attempt to free the texture with the
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 * underlying API.
134 */
135 virtual void abandon() = 0;
136
137 /**
138 * Queries whether the texture was created as a render target.
139 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000140 * Use asRenderTarget() to use the texture as a render target if this
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 * returns true.
142 *
143 * @return true if the texture was created as a render target.
144 */
145 virtual bool isRenderTarget() const = 0;
146
147 /**
148 * Retrieves the render target underlying this texture that can be passed to
149 * GrGpu::setRenderTarget().
150 *
151 * If isRenderTarget() is false then the returned handle is undefined.
152 *
153 * @return handle to render target or undefined if the texture is not a
154 * render target
155 */
156 virtual GrRenderTarget* asRenderTarget() = 0;
157
158 /**
159 * Removes the "rendertargetness" from a texture. This may or may not
160 * actually do anything with the underlying 3D API.
161 */
162 virtual void removeRenderTarget() = 0;
163
164 /**
165 * Return the native ID or handle to the texture, depending on the
166 * platform. e.g. on opengl, return the texture ID.
167 */
168 virtual intptr_t getTextureHandle() = 0;
169
170#if GR_DEBUG
171 void validate() const {
172 this->INHERITED::validate();
173 }
174#else
175 void validate() const {}
176#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000177
178private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000179 int fWidth;
180 int fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000181 // these two shift a fixed-point value into normalized coordinates
182 // for this texture if the texture is power of two sized.
183 int fShiftFixedX;
184 int fShiftFixedY;
185 PixelConfig fConfig;
186
187 typedef GrRefCnt INHERITED;
188};
189
190#endif
191