blob: d1853e383782ae818d7cead8040003ceb60d5b29 [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"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000023#include "GrResource.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
25class GrTexture;
26
27/**
28 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
29 * A context's render target is set by setRenderTarget(). Render targets are
bsalomon@google.com1c13c962011-02-14 16:51:21 +000030 * created by a createTexture with the kRenderTarget_TextureFlag flag.
31 * Additionally, GrContext provides methods for creating GrRenderTargets
32 * that wrap externally created render targets.
reed@google.comac10a2d2010-12-22 21:39:39 +000033 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000034class GrRenderTarget : public GrResource {
reed@google.comac10a2d2010-12-22 21:39:39 +000035public:
36 /**
37 * @return the width of the rendertarget
38 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000039 int width() const { return fWidth; }
reed@google.comac10a2d2010-12-22 21:39:39 +000040 /**
41 * @return the height of the rendertarget
42 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000043 int height() const { return fHeight; }
44
45 /**
46 * @return the number of stencil bits in the rendertarget
47 */
48 int stencilBits() const { return fStencilBits; }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000049
reed@google.comac10a2d2010-12-22 21:39:39 +000050 /**
51 * @return the texture associated with the rendertarget, may be NULL.
52 */
53 GrTexture* asTexture() {return fTexture;}
54
55protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000056 GrRenderTarget(GrGpu* gpu,
57 GrTexture* texture,
bsalomon@google.comd302f142011-03-03 13:54:13 +000058 int width,
59 int height,
60 int stencilBits)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000061 : INHERITED(gpu)
62 , fTexture(texture)
63 , fWidth(width)
64 , fHeight(height)
65 , fStencilBits(stencilBits)
66 {}
bsalomon@google.comd302f142011-03-03 13:54:13 +000067
68
reed@google.comac10a2d2010-12-22 21:39:39 +000069 GrTexture* fTexture;
bsalomon@google.comd302f142011-03-03 13:54:13 +000070 int fWidth;
71 int fHeight;
72 int fStencilBits;
73
74private:
75 // GrGpu keeps a cached clip in the render target to avoid redundantly
76 // rendering the clip into the same stencil buffer.
77 friend class GrGpu;
78 GrClip fLastStencilClip;
79
bsalomon@google.com8fe72472011-03-30 21:26:44 +000080 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +000081};
82
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083class GrTexture : public GrResource {
reed@google.comac10a2d2010-12-22 21:39:39 +000084public:
85 enum PixelConfig {
86 kUnknown_PixelConfig,
87 kAlpha_8_PixelConfig,
88 kIndex_8_PixelConfig,
89 kRGB_565_PixelConfig,
90 kRGBA_4444_PixelConfig, //!< premultiplied
91 kRGBA_8888_PixelConfig, //!< premultiplied
92 kRGBX_8888_PixelConfig, //!< treat the alpha channel as opaque
93 };
94 static size_t BytesPerPixel(PixelConfig);
95 static bool PixelConfigIsOpaque(PixelConfig);
bsalomon@google.com080773c2011-03-15 19:09:25 +000096 static bool PixelConfigIsAlphaOnly(PixelConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000097
98protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000099 GrTexture(GrGpu* gpu,
100 int width,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000101 int height,
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102 PixelConfig config)
103 : INHERITED(gpu)
104 , fWidth(width)
105 , fHeight(height)
106 , fConfig(config) {
107 // only make sense if alloc size is pow2
108 fShiftFixedX = 31 - Gr_clz(fWidth);
109 fShiftFixedY = 31 - Gr_clz(fHeight);
110 }
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112public:
113 virtual ~GrTexture();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000116 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000117 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 * @return the width in texels
119 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 int width() const { return fWidth; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000123 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000124 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 * @return the height in texels
126 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000127 int height() const { return fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
129 /**
130 * Convert from texels to normalized texture coords for POT textures
131 * only.
132 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000133 GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth));
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 return x >> fShiftFixedX; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000135 GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 return y >> fShiftFixedY; }
137
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000138 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 * Retrieves the pixel config specified when the texture was created.
140 */
141 PixelConfig config() const { return fConfig; }
142
143 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000144 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 */
146 size_t sizeInBytes() const {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000147 return fWidth * fHeight * BytesPerPixel(fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 }
149
150 /**
151 * Updates a subrectangle of texels in the texture.
152 *
153 * @param x left edge of rectangle to update
154 * @param y top edge of rectangle to update
155 * @param width width of rectangle to update
156 * @param height height of rectangle to update
157 * @param srcData width*height texels of data in same format that was used
158 * at texture creation.
159 */
160 virtual void uploadTextureData(uint32_t x,
161 uint32_t y,
162 uint32_t width,
163 uint32_t height,
164 const void* srcData) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000165
166 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 * Retrieves the render target underlying this texture that can be passed to
168 * GrGpu::setRenderTarget().
169 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 * @return handle to render target or undefined if the texture is not a
171 * render target
172 */
173 virtual GrRenderTarget* asRenderTarget() = 0;
174
175 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000176 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000177 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000178 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000179 */
bsalomon@google.com1da07462011-03-10 14:51:57 +0000180 virtual void releaseRenderTarget() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000181
182 /**
183 * Return the native ID or handle to the texture, depending on the
184 * platform. e.g. on opengl, return the texture ID.
185 */
186 virtual intptr_t getTextureHandle() = 0;
187
188#if GR_DEBUG
189 void validate() const {
190 this->INHERITED::validate();
191 }
192#else
193 void validate() const {}
194#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000195
196private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000197 int fWidth;
198 int fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 // these two shift a fixed-point value into normalized coordinates
200 // for this texture if the texture is power of two sized.
201 int fShiftFixedX;
202 int fShiftFixedY;
203 PixelConfig fConfig;
204
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000205 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000206};
207
208#endif
209