blob: 4776539d961c83893fe946bc8f3543c35857ebe3 [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:
68 GrTexture(uint32_t contentWidth,
69 uint32_t contentHeight,
70 uint32_t allocWidth,
71 uint32_t allocHeight,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000072 PixelConfig config) :
73 fAllocWidth(allocWidth),
reed@google.comac10a2d2010-12-22 21:39:39 +000074 fAllocHeight(allocHeight),
bsalomon@google.com1c13c962011-02-14 16:51:21 +000075 fContentWidth(contentWidth),
reed@google.comac10a2d2010-12-22 21:39:39 +000076 fContentHeight(contentHeight),
77 fConfig(config) {
78 // only make sense if alloc size is pow2
79 fShiftFixedX = 31 - Gr_clz(allocWidth);
80 fShiftFixedY = 31 - Gr_clz(allocHeight);
81 }
82public:
83 virtual ~GrTexture();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000084
reed@google.comac10a2d2010-12-22 21:39:39 +000085 /**
86 * Retrieves the width of the content area of the texture. Reflects the
87 * width passed to GrGpu::createTexture().
bsalomon@google.com1c13c962011-02-14 16:51:21 +000088 *
reed@google.comac10a2d2010-12-22 21:39:39 +000089 * @return the width in texels
90 */
91 uint32_t contentWidth() const { return fContentWidth; }
92 /**
93 * Retrieves the height of the content area of the texture. Reflects the
94 * height passed to GrGpu::createTexture().
bsalomon@google.com1c13c962011-02-14 16:51:21 +000095 *
reed@google.comac10a2d2010-12-22 21:39:39 +000096 * @return the height in texels
97 */
98 uint32_t contentHeight() const { return fContentHeight; }
99
100 /**
101 * Retrieves the texture width actually allocated in texels.
102 *
103 * @return the width in texels
104 */
105 uint32_t allocWidth() const { return fAllocWidth; }
106 /**
107 * Retrieves the texture height actually allocated in texels.
108 *
109 * @return the height in texels
110 */
111 uint32_t allocHeight() const { return fAllocHeight; }
112
113 /**
114 * Convert from texels to normalized texture coords for POT textures
115 * only.
116 */
117 GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fAllocWidth));
118 return x >> fShiftFixedX; }
119 GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fAllocHeight));
120 return y >> fShiftFixedY; }
121
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000122 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 * Retrieves the pixel config specified when the texture was created.
124 */
125 PixelConfig config() const { return fConfig; }
126
127 /**
128 * The number of bytes used by the texture
129 */
130 size_t sizeInBytes() const {
131 return fAllocWidth * fAllocHeight * BytesPerPixel(fConfig);
132 }
133
134 /**
135 * Updates a subrectangle of texels in the texture.
136 *
137 * @param x left edge of rectangle to update
138 * @param y top edge of rectangle to update
139 * @param width width of rectangle to update
140 * @param height height of rectangle to update
141 * @param srcData width*height texels of data in same format that was used
142 * at texture creation.
143 */
144 virtual void uploadTextureData(uint32_t x,
145 uint32_t y,
146 uint32_t width,
147 uint32_t height,
148 const void* srcData) = 0;
149 /**
150 * Indicates that GPU context in which this texture was created is destroyed
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000151 * and that Ganesh should not attempt to free the texture with the
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 * underlying API.
153 */
154 virtual void abandon() = 0;
155
156 /**
157 * Queries whether the texture was created as a render target.
158 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000159 * Use asRenderTarget() to use the texture as a render target if this
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 * returns true.
161 *
162 * @return true if the texture was created as a render target.
163 */
164 virtual bool isRenderTarget() const = 0;
165
166 /**
167 * Retrieves the render target underlying this texture that can be passed to
168 * GrGpu::setRenderTarget().
169 *
170 * If isRenderTarget() is false then the returned handle is undefined.
171 *
172 * @return handle to render target or undefined if the texture is not a
173 * render target
174 */
175 virtual GrRenderTarget* asRenderTarget() = 0;
176
177 /**
178 * Removes the "rendertargetness" from a texture. This may or may not
179 * actually do anything with the underlying 3D API.
180 */
181 virtual void removeRenderTarget() = 0;
182
183 /**
184 * Return the native ID or handle to the texture, depending on the
185 * platform. e.g. on opengl, return the texture ID.
186 */
187 virtual intptr_t getTextureHandle() = 0;
188
189#if GR_DEBUG
190 void validate() const {
191 this->INHERITED::validate();
192 }
193#else
194 void validate() const {}
195#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000196
197private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000198 uint32_t fAllocWidth;
199 uint32_t fAllocHeight;
200 uint32_t fContentWidth;
201 uint32_t fContentHeight;
202 // these two shift a fixed-point value into normalized coordinates
203 // for this texture if the texture is power of two sized.
204 int fShiftFixedX;
205 int fShiftFixedY;
206 PixelConfig fConfig;
207
208 typedef GrRefCnt INHERITED;
209};
210
211#endif
212