blob: 2d3928cb25548dbd93ed4250b236f15bf74a18bf [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"
bsalomon@google.comd302f142011-03-03 13:54:13 +000022#include "GrClip.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24class GrTexture;
25
26/**
27 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
28 * A context's render target is set by setRenderTarget(). Render targets are
bsalomon@google.com1c13c962011-02-14 16:51:21 +000029 * created by a createTexture with the kRenderTarget_TextureFlag flag.
30 * Additionally, GrContext provides methods for creating GrRenderTargets
31 * that wrap externally created render targets.
reed@google.comac10a2d2010-12-22 21:39:39 +000032 */
33class GrRenderTarget : public GrRefCnt {
34public:
35 /**
36 * @return the width of the rendertarget
37 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000038 int width() const { return fWidth; }
reed@google.comac10a2d2010-12-22 21:39:39 +000039 /**
40 * @return the height of the rendertarget
41 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000042 int height() const { return fHeight; }
43
44 /**
45 * @return the number of stencil bits in the rendertarget
46 */
47 int stencilBits() const { return fStencilBits; }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000048
reed@google.comac10a2d2010-12-22 21:39:39 +000049 /**
50 * @return the texture associated with the rendertarget, may be NULL.
51 */
52 GrTexture* asTexture() {return fTexture;}
53
54protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +000055 GrRenderTarget(GrTexture* texture,
56 int width,
57 int height,
58 int stencilBits)
59 : fTexture(texture),
60 fWidth(width),
61 fHeight(height),
62 fStencilBits(stencilBits) {}
63
64
reed@google.comac10a2d2010-12-22 21:39:39 +000065 GrTexture* fTexture;
bsalomon@google.comd302f142011-03-03 13:54:13 +000066 int fWidth;
67 int fHeight;
68 int fStencilBits;
69
70private:
71 // GrGpu keeps a cached clip in the render target to avoid redundantly
72 // rendering the clip into the same stencil buffer.
73 friend class GrGpu;
74 GrClip fLastStencilClip;
75
76 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +000077};
78
79class GrTexture : public GrRefCnt {
80public:
81 enum PixelConfig {
82 kUnknown_PixelConfig,
83 kAlpha_8_PixelConfig,
84 kIndex_8_PixelConfig,
85 kRGB_565_PixelConfig,
86 kRGBA_4444_PixelConfig, //!< premultiplied
87 kRGBA_8888_PixelConfig, //!< premultiplied
88 kRGBX_8888_PixelConfig, //!< treat the alpha channel as opaque
89 };
90 static size_t BytesPerPixel(PixelConfig);
91 static bool PixelConfigIsOpaque(PixelConfig);
92
93protected:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000094 GrTexture(int width,
95 int height,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000096 PixelConfig config) :
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000097 fWidth(width),
98 fHeight(height),
reed@google.comac10a2d2010-12-22 21:39:39 +000099 fConfig(config) {
100 // only make sense if alloc size is pow2
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000101 fShiftFixedX = 31 - Gr_clz(fWidth);
102 fShiftFixedY = 31 - Gr_clz(fHeight);
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 }
104public:
105 virtual ~GrTexture();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000106
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000108 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000109 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 * @return the width in texels
111 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000112 int width() const { return fWidth; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000114 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 * @return the height in texels
117 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000118 int height() const { return fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000119
120 /**
121 * Convert from texels to normalized texture coords for POT textures
122 * only.
123 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000124 GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth));
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 return x >> fShiftFixedX; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000126 GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 return y >> fShiftFixedY; }
128
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000129 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 * Retrieves the pixel config specified when the texture was created.
131 */
132 PixelConfig config() const { return fConfig; }
133
134 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000135 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 */
137 size_t sizeInBytes() const {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000138 return fWidth * fHeight * BytesPerPixel(fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 }
140
141 /**
142 * Updates a subrectangle of texels in the texture.
143 *
144 * @param x left edge of rectangle to update
145 * @param y top edge of rectangle to update
146 * @param width width of rectangle to update
147 * @param height height of rectangle to update
148 * @param srcData width*height texels of data in same format that was used
149 * at texture creation.
150 */
151 virtual void uploadTextureData(uint32_t x,
152 uint32_t y,
153 uint32_t width,
154 uint32_t height,
155 const void* srcData) = 0;
156 /**
157 * Indicates that GPU context in which this texture was created is destroyed
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000158 * and that Ganesh should not attempt to free the texture with the
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 * underlying API.
160 */
161 virtual void abandon() = 0;
162
163 /**
164 * Queries whether the texture was created as a render target.
165 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000166 * Use asRenderTarget() to use the texture as a render target if this
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 * returns true.
168 *
169 * @return true if the texture was created as a render target.
170 */
171 virtual bool isRenderTarget() const = 0;
172
173 /**
174 * Retrieves the render target underlying this texture that can be passed to
175 * GrGpu::setRenderTarget().
176 *
177 * If isRenderTarget() is false then the returned handle is undefined.
178 *
179 * @return handle to render target or undefined if the texture is not a
180 * render target
181 */
182 virtual GrRenderTarget* asRenderTarget() = 0;
183
184 /**
185 * Removes the "rendertargetness" from a texture. This may or may not
186 * actually do anything with the underlying 3D API.
187 */
188 virtual void removeRenderTarget() = 0;
189
190 /**
191 * Return the native ID or handle to the texture, depending on the
192 * platform. e.g. on opengl, return the texture ID.
193 */
194 virtual intptr_t getTextureHandle() = 0;
195
196#if GR_DEBUG
197 void validate() const {
198 this->INHERITED::validate();
199 }
200#else
201 void validate() const {}
202#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000203
204private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000205 int fWidth;
206 int fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 // these two shift a fixed-point value into normalized coordinates
208 // for this texture if the texture is power of two sized.
209 int fShiftFixedX;
210 int fShiftFixedY;
211 PixelConfig fConfig;
212
213 typedef GrRefCnt INHERITED;
214};
215
216#endif
217