blob: 0a65ff75ce2dc15a345e0f4fb23aff8804ff5158 [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);
bsalomon@google.com080773c2011-03-15 19:09:25 +000092 static bool PixelConfigIsAlphaOnly(PixelConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000093
94protected:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000095 GrTexture(int width,
96 int height,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000097 PixelConfig config) :
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000098 fWidth(width),
99 fHeight(height),
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 fConfig(config) {
101 // only make sense if alloc size is pow2
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000102 fShiftFixedX = 31 - Gr_clz(fWidth);
103 fShiftFixedY = 31 - Gr_clz(fHeight);
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 }
105public:
106 virtual ~GrTexture();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000109 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000110 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 * @return the width in texels
112 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000113 int width() const { return fWidth; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000115 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 * @return the height in texels
118 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000119 int height() const { return fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000120
121 /**
122 * Convert from texels to normalized texture coords for POT textures
123 * only.
124 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000125 GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth));
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 return x >> fShiftFixedX; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000127 GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 return y >> fShiftFixedY; }
129
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000130 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 * Retrieves the pixel config specified when the texture was created.
132 */
133 PixelConfig config() const { return fConfig; }
134
135 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000136 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 */
138 size_t sizeInBytes() const {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000139 return fWidth * fHeight * BytesPerPixel(fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 }
141
142 /**
143 * Updates a subrectangle of texels in the texture.
144 *
145 * @param x left edge of rectangle to update
146 * @param y top edge of rectangle to update
147 * @param width width of rectangle to update
148 * @param height height of rectangle to update
149 * @param srcData width*height texels of data in same format that was used
150 * at texture creation.
151 */
152 virtual void uploadTextureData(uint32_t x,
153 uint32_t y,
154 uint32_t width,
155 uint32_t height,
156 const void* srcData) = 0;
157 /**
158 * Indicates that GPU context in which this texture was created is destroyed
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000159 * and that Ganesh should not attempt to free the texture with the
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 * underlying API.
161 */
162 virtual void abandon() = 0;
163
164 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 * Retrieves the render target underlying this texture that can be passed to
166 * GrGpu::setRenderTarget().
167 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 * @return handle to render target or undefined if the texture is not a
169 * render target
170 */
171 virtual GrRenderTarget* asRenderTarget() = 0;
172
173 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000174 * Removes the reference on the associated GrRenderTarget held by this
175 * texture. Afterwards asRenderTarget() will return NULL. The
176 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 */
bsalomon@google.com1da07462011-03-10 14:51:57 +0000178 virtual void releaseRenderTarget() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000179
180 /**
181 * Return the native ID or handle to the texture, depending on the
182 * platform. e.g. on opengl, return the texture ID.
183 */
184 virtual intptr_t getTextureHandle() = 0;
185
186#if GR_DEBUG
187 void validate() const {
188 this->INHERITED::validate();
189 }
190#else
191 void validate() const {}
192#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000193
194private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000195 int fWidth;
196 int fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 // these two shift a fixed-point value into normalized coordinates
198 // for this texture if the texture is power of two sized.
199 int fShiftFixedX;
200 int fShiftFixedY;
201 PixelConfig fConfig;
202
203 typedef GrRefCnt INHERITED;
204};
205
206#endif
207