| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 22 | #include "GrClip.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
| 24 | class 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 29 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | */ |
| 33 | class GrRenderTarget : public GrRefCnt { |
| 34 | public: |
| 35 | /** |
| 36 | * @return the width of the rendertarget |
| 37 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 38 | int width() const { return fWidth; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | /** |
| 40 | * @return the height of the rendertarget |
| 41 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 42 | 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 48 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | /** |
| 50 | * @return the texture associated with the rendertarget, may be NULL. |
| 51 | */ |
| 52 | GrTexture* asTexture() {return fTexture;} |
| 53 | |
| 54 | protected: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 55 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 65 | GrTexture* fTexture; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 66 | int fWidth; |
| 67 | int fHeight; |
| 68 | int fStencilBits; |
| 69 | |
| 70 | private: |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | class GrTexture : public GrRefCnt { |
| 80 | public: |
| 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 | |
| 93 | protected: |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 94 | GrTexture(int width, |
| 95 | int height, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 96 | PixelConfig config) : |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 97 | fWidth(width), |
| 98 | fHeight(height), |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | fConfig(config) { |
| 100 | // only make sense if alloc size is pow2 |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 101 | fShiftFixedX = 31 - Gr_clz(fWidth); |
| 102 | fShiftFixedY = 31 - Gr_clz(fHeight); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 103 | } |
| 104 | public: |
| 105 | virtual ~GrTexture(); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 106 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | /** |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 108 | * Retrieves the width of the texture. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 109 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | * @return the width in texels |
| 111 | */ |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 112 | int width() const { return fWidth; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | /** |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 114 | * Retrieves the height of the texture. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 115 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | * @return the height in texels |
| 117 | */ |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 118 | int height() const { return fHeight; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Convert from texels to normalized texture coords for POT textures |
| 122 | * only. |
| 123 | */ |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 124 | GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | return x >> fShiftFixedX; } |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 126 | GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | return y >> fShiftFixedY; } |
| 128 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 129 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | * Retrieves the pixel config specified when the texture was created. |
| 131 | */ |
| 132 | PixelConfig config() const { return fConfig; } |
| 133 | |
| 134 | /** |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 135 | * Approximate number of bytes used by the texture |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 136 | */ |
| 137 | size_t sizeInBytes() const { |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 138 | return fWidth * fHeight * BytesPerPixel(fConfig); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | } |
| 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 158 | * and that Ganesh should not attempt to free the texture with the |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | * underlying API. |
| 160 | */ |
| 161 | virtual void abandon() = 0; |
| 162 | |
| 163 | /** |
| 164 | * Queries whether the texture was created as a render target. |
| 165 | * |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 166 | * Use asRenderTarget() to use the texture as a render target if this |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | * 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 203 | |
| 204 | private: |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 205 | int fWidth; |
| 206 | int fHeight; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 207 | // 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 | |