| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 2 | Copyright 2011 Google Inc. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 22 | #include "GrClip.h" |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 23 | #include "GrResource.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | |
| 25 | class 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 30 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | */ |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 34 | class GrRenderTarget : public GrResource { |
| bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame^] | 35 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | public: |
| 37 | /** |
| 38 | * @return the width of the rendertarget |
| 39 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 40 | int width() const { return fWidth; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | /** |
| 42 | * @return the height of the rendertarget |
| 43 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 44 | int height() const { return fHeight; } |
| 45 | |
| 46 | /** |
| 47 | * @return the number of stencil bits in the rendertarget |
| 48 | */ |
| 49 | int stencilBits() const { return fStencilBits; } |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 50 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | /** |
| 52 | * @return the texture associated with the rendertarget, may be NULL. |
| 53 | */ |
| 54 | GrTexture* asTexture() {return fTexture;} |
| 55 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 56 | /** |
| 57 | * Reads a rectangle of pixels from the render target. |
| 58 | * @param left left edge of the rectangle to read (inclusive) |
| 59 | * @param top top edge of the rectangle to read (inclusive) |
| 60 | * @param width width of rectangle to read in pixels. |
| 61 | * @param height height of rectangle to read in pixels. |
| 62 | * @param config the pixel config of the destination buffer |
| 63 | * @param buffer memory to read the rectangle into. |
| 64 | * |
| 65 | * @return true if the read succeeded, false if not. The read can fail |
| 66 | * because of a unsupported pixel config. |
| 67 | */ |
| 68 | bool readPixels(int left, int top, int width, int height, |
| 69 | GrPixelConfig config, void* buffer); |
| 70 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | protected: |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 72 | GrRenderTarget(GrGpu* gpu, |
| 73 | GrTexture* texture, |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 74 | int width, |
| 75 | int height, |
| 76 | int stencilBits) |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 77 | : INHERITED(gpu) |
| 78 | , fTexture(texture) |
| 79 | , fWidth(width) |
| 80 | , fHeight(height) |
| 81 | , fStencilBits(stencilBits) |
| 82 | {} |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 83 | |
| bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame^] | 84 | friend class GrTexture; |
| 85 | // When a texture unrefs an owned rendertarget this func |
| 86 | // removes the back pointer. This could be done called from |
| 87 | // texture's destructor but would have to be done in derived |
| 88 | // class. By the time of texture base destructor it has already |
| 89 | // lost its pointer to the rt. |
| 90 | void onTextureReleaseRenderTarget() { |
| 91 | GrAssert(NULL != fTexture); |
| 92 | fTexture = NULL; |
| 93 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 94 | |
| bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame^] | 95 | GrTexture* fTexture; // not ref'ed |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 96 | int fWidth; |
| 97 | int fHeight; |
| 98 | int fStencilBits; |
| 99 | |
| 100 | private: |
| 101 | // GrGpu keeps a cached clip in the render target to avoid redundantly |
| 102 | // rendering the clip into the same stencil buffer. |
| 103 | friend class GrGpu; |
| 104 | GrClip fLastStencilClip; |
| 105 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 106 | typedef GrResource INHERITED; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 109 | class GrTexture : public GrResource { |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 110 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 111 | public: |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 112 | /** |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 113 | * Retrieves the width of the texture. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 114 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 115 | * @return the width in texels |
| 116 | */ |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 117 | int width() const { return fWidth; } |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 118 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | /** |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 120 | * Retrieves the height of the texture. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 121 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | * @return the height in texels |
| 123 | */ |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 124 | int height() const { return fHeight; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Convert from texels to normalized texture coords for POT textures |
| 128 | * only. |
| 129 | */ |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 130 | GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | return x >> fShiftFixedX; } |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 132 | GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | return y >> fShiftFixedY; } |
| 134 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 135 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 136 | * Retrieves the pixel config specified when the texture was created. |
| 137 | */ |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 138 | GrPixelConfig config() const { return fConfig; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | |
| 140 | /** |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 141 | * Approximate number of bytes used by the texture |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 142 | */ |
| 143 | size_t sizeInBytes() const { |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 144 | return fWidth * fHeight * GrBytesPerPixel(fConfig); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Updates a subrectangle of texels in the texture. |
| 149 | * |
| 150 | * @param x left edge of rectangle to update |
| 151 | * @param y top edge of rectangle to update |
| 152 | * @param width width of rectangle to update |
| 153 | * @param height height of rectangle to update |
| 154 | * @param srcData width*height texels of data in same format that was used |
| 155 | * at texture creation. |
| 156 | */ |
| 157 | virtual void uploadTextureData(uint32_t x, |
| 158 | uint32_t y, |
| 159 | uint32_t width, |
| 160 | uint32_t height, |
| 161 | const void* srcData) = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | |
| 163 | /** |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 164 | * Reads a rectangle of pixels from the texture. |
| 165 | * @param left left edge of the rectangle to read (inclusive) |
| 166 | * @param top top edge of the rectangle to read (inclusive) |
| 167 | * @param width width of rectangle to read in pixels. |
| 168 | * @param height height of rectangle to read in pixels. |
| 169 | * @param config the pixel config of the destination buffer |
| 170 | * @param buffer memory to read the rectangle into. |
| 171 | * |
| 172 | * @return true if the read succeeded, false if not. The read can fail |
| 173 | * because of a unsupported pixel config. |
| 174 | */ |
| 175 | bool readPixels(int left, int top, int width, int height, |
| 176 | GrPixelConfig config, void* buffer); |
| 177 | |
| 178 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 | * Retrieves the render target underlying this texture that can be passed to |
| 180 | * GrGpu::setRenderTarget(). |
| 181 | * |
| bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame^] | 182 | * @return handle to render target or NULL if the texture is not a |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 183 | * render target |
| 184 | */ |
| bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame^] | 185 | GrRenderTarget* asRenderTarget() { return fRenderTarget; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | |
| 187 | /** |
| bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 188 | * Removes the reference on the associated GrRenderTarget held by this |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 189 | * texture. Afterwards asRenderTarget() will return NULL. The |
| bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 190 | * GrRenderTarget survives the release if another ref is held on it. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 191 | */ |
| bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame^] | 192 | void releaseRenderTarget() { |
| 193 | if (NULL != fRenderTarget) { |
| 194 | GrAssert(fRenderTarget->asTexture() == this); |
| 195 | fRenderTarget->onTextureReleaseRenderTarget(); |
| 196 | fRenderTarget->unref(); |
| 197 | fRenderTarget = NULL; |
| 198 | } |
| 199 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * Return the native ID or handle to the texture, depending on the |
| 203 | * platform. e.g. on opengl, return the texture ID. |
| 204 | */ |
| 205 | virtual intptr_t getTextureHandle() = 0; |
| 206 | |
| 207 | #if GR_DEBUG |
| 208 | void validate() const { |
| 209 | this->INHERITED::validate(); |
| 210 | } |
| 211 | #else |
| 212 | void validate() const {} |
| 213 | #endif |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 214 | |
| bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame^] | 215 | protected: |
| 216 | GrRenderTarget* fRenderTarget; // texture refs its rt representation |
| 217 | // base class cons sets to NULL |
| 218 | // subclass cons can create and set |
| 219 | |
| 220 | GrTexture(GrGpu* gpu, |
| 221 | int width, |
| 222 | int height, |
| 223 | GrPixelConfig config) |
| 224 | : INHERITED(gpu) |
| 225 | , fRenderTarget(NULL) |
| 226 | , fWidth(width) |
| 227 | , fHeight(height) |
| 228 | , fConfig(config) { |
| 229 | // only make sense if alloc size is pow2 |
| 230 | fShiftFixedX = 31 - Gr_clz(fWidth); |
| 231 | fShiftFixedY = 31 - Gr_clz(fHeight); |
| 232 | } |
| 233 | |
| 234 | // GrResource overrides |
| 235 | virtual void onRelease() { |
| 236 | releaseRenderTarget(); |
| 237 | } |
| 238 | |
| 239 | virtual void onAbandon() { |
| 240 | if (NULL != fRenderTarget) { |
| 241 | fRenderTarget->abandon(); |
| 242 | } |
| 243 | } |
| 244 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 245 | private: |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 246 | int fWidth; |
| 247 | int fHeight; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | // these two shift a fixed-point value into normalized coordinates |
| 249 | // for this texture if the texture is power of two sized. |
| 250 | int fShiftFixedX; |
| 251 | int fShiftFixedY; |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 252 | |
| 253 | GrPixelConfig fConfig; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 254 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 255 | typedef GrResource INHERITED; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | #endif |
| 259 | |