blob: 666aa570f8f25266ed71bd4e4b0fb4ac8420a87b [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"
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 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 * Retrieves the render target underlying this texture that can be passed to
165 * GrGpu::setRenderTarget().
166 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 * @return handle to render target or undefined if the texture is not a
168 * render target
169 */
170 virtual GrRenderTarget* asRenderTarget() = 0;
171
172 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000173 * Removes the reference on the associated GrRenderTarget held by this
174 * texture. Afterwards asRenderTarget() will return NULL. The
175 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 */
bsalomon@google.com1da07462011-03-10 14:51:57 +0000177 virtual void releaseRenderTarget() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
179 /**
180 * Return the native ID or handle to the texture, depending on the
181 * platform. e.g. on opengl, return the texture ID.
182 */
183 virtual intptr_t getTextureHandle() = 0;
184
185#if GR_DEBUG
186 void validate() const {
187 this->INHERITED::validate();
188 }
189#else
190 void validate() const {}
191#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000192
193private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000194 int fWidth;
195 int fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000196 // these two shift a fixed-point value into normalized coordinates
197 // for this texture if the texture is power of two sized.
198 int fShiftFixedX;
199 int fShiftFixedY;
200 PixelConfig fConfig;
201
202 typedef GrRefCnt INHERITED;
203};
204
205#endif
206