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" |
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 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | * Retrieves the render target underlying this texture that can be passed to |
| 165 | * GrGpu::setRenderTarget(). |
| 166 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | * @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.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 173 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | */ |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 177 | virtual void releaseRenderTarget() = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | |
| 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 192 | |
| 193 | private: |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 194 | int fWidth; |
| 195 | int fHeight; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 196 | // 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 | |