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" |
| 22 | |
| 23 | class GrTexture; |
| 24 | |
| 25 | /** |
| 26 | * GrRenderTarget represents a 2D buffer of pixels that can be rendered to. |
| 27 | * A context's render target is set by setRenderTarget(). Render targets are |
| 28 | * created by a createTexture with the kRenderTarget_TextureFlag flag. |
| 29 | * Additionally, the rendering destination set in the underlying 3D API at the |
| 30 | * time of GrContext's creation can be retrieved by calling |
| 31 | * currentRenderTarget() after creation before any calles to setRenderTarget(). |
| 32 | */ |
| 33 | class GrRenderTarget : public GrRefCnt { |
| 34 | public: |
| 35 | /** |
| 36 | * @return the width of the rendertarget |
| 37 | */ |
| 38 | virtual uint32_t width() const = 0; |
| 39 | /** |
| 40 | * @return the height of the rendertarget |
| 41 | */ |
| 42 | virtual uint32_t height() const = 0; |
| 43 | |
| 44 | /** |
| 45 | * @return the texture associated with the rendertarget, may be NULL. |
| 46 | */ |
| 47 | GrTexture* asTexture() {return fTexture;} |
| 48 | |
| 49 | protected: |
| 50 | GrRenderTarget(GrTexture* texture) : fTexture(texture) {} |
| 51 | GrTexture* fTexture; |
| 52 | }; |
| 53 | |
| 54 | class GrTexture : public GrRefCnt { |
| 55 | public: |
| 56 | enum PixelConfig { |
| 57 | kUnknown_PixelConfig, |
| 58 | kAlpha_8_PixelConfig, |
| 59 | kIndex_8_PixelConfig, |
| 60 | kRGB_565_PixelConfig, |
| 61 | kRGBA_4444_PixelConfig, //!< premultiplied |
| 62 | kRGBA_8888_PixelConfig, //!< premultiplied |
| 63 | kRGBX_8888_PixelConfig, //!< treat the alpha channel as opaque |
| 64 | }; |
| 65 | static size_t BytesPerPixel(PixelConfig); |
| 66 | static bool PixelConfigIsOpaque(PixelConfig); |
| 67 | |
| 68 | protected: |
| 69 | GrTexture(uint32_t contentWidth, |
| 70 | uint32_t contentHeight, |
| 71 | uint32_t allocWidth, |
| 72 | uint32_t allocHeight, |
| 73 | PixelConfig config) : |
| 74 | fAllocWidth(allocWidth), |
| 75 | fAllocHeight(allocHeight), |
| 76 | fContentWidth(contentWidth), |
| 77 | fContentHeight(contentHeight), |
| 78 | fConfig(config) { |
| 79 | // only make sense if alloc size is pow2 |
| 80 | fShiftFixedX = 31 - Gr_clz(allocWidth); |
| 81 | fShiftFixedY = 31 - Gr_clz(allocHeight); |
| 82 | } |
| 83 | public: |
| 84 | virtual ~GrTexture(); |
| 85 | |
| 86 | /** |
| 87 | * Retrieves the width of the content area of the texture. Reflects the |
| 88 | * width passed to GrGpu::createTexture(). |
| 89 | * |
| 90 | * @return the width in texels |
| 91 | */ |
| 92 | uint32_t contentWidth() const { return fContentWidth; } |
| 93 | /** |
| 94 | * Retrieves the height of the content area of the texture. Reflects the |
| 95 | * height passed to GrGpu::createTexture(). |
| 96 | * |
| 97 | * @return the height in texels |
| 98 | */ |
| 99 | uint32_t contentHeight() const { return fContentHeight; } |
| 100 | |
| 101 | /** |
| 102 | * Retrieves the texture width actually allocated in texels. |
| 103 | * |
| 104 | * @return the width in texels |
| 105 | */ |
| 106 | uint32_t allocWidth() const { return fAllocWidth; } |
| 107 | /** |
| 108 | * Retrieves the texture height actually allocated in texels. |
| 109 | * |
| 110 | * @return the height in texels |
| 111 | */ |
| 112 | uint32_t allocHeight() const { return fAllocHeight; } |
| 113 | |
| 114 | /** |
| 115 | * Convert from texels to normalized texture coords for POT textures |
| 116 | * only. |
| 117 | */ |
| 118 | GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fAllocWidth)); |
| 119 | return x >> fShiftFixedX; } |
| 120 | GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fAllocHeight)); |
| 121 | return y >> fShiftFixedY; } |
| 122 | |
| 123 | /** |
| 124 | * Retrieves the pixel config specified when the texture was created. |
| 125 | */ |
| 126 | PixelConfig config() const { return fConfig; } |
| 127 | |
| 128 | /** |
| 129 | * The number of bytes used by the texture |
| 130 | */ |
| 131 | size_t sizeInBytes() const { |
| 132 | return fAllocWidth * fAllocHeight * BytesPerPixel(fConfig); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Updates a subrectangle of texels in the texture. |
| 137 | * |
| 138 | * @param x left edge of rectangle to update |
| 139 | * @param y top edge of rectangle to update |
| 140 | * @param width width of rectangle to update |
| 141 | * @param height height of rectangle to update |
| 142 | * @param srcData width*height texels of data in same format that was used |
| 143 | * at texture creation. |
| 144 | */ |
| 145 | virtual void uploadTextureData(uint32_t x, |
| 146 | uint32_t y, |
| 147 | uint32_t width, |
| 148 | uint32_t height, |
| 149 | const void* srcData) = 0; |
| 150 | /** |
| 151 | * Indicates that GPU context in which this texture was created is destroyed |
| 152 | * and that Ganesh should not attempt to free the texture with the |
| 153 | * underlying API. |
| 154 | */ |
| 155 | virtual void abandon() = 0; |
| 156 | |
| 157 | /** |
| 158 | * Queries whether the texture was created as a render target. |
| 159 | * |
| 160 | * Use asRenderTarget() to use the texture as a render target if this |
| 161 | * returns true. |
| 162 | * |
| 163 | * @return true if the texture was created as a render target. |
| 164 | */ |
| 165 | virtual bool isRenderTarget() const = 0; |
| 166 | |
| 167 | /** |
| 168 | * Retrieves the render target underlying this texture that can be passed to |
| 169 | * GrGpu::setRenderTarget(). |
| 170 | * |
| 171 | * If isRenderTarget() is false then the returned handle is undefined. |
| 172 | * |
| 173 | * @return handle to render target or undefined if the texture is not a |
| 174 | * render target |
| 175 | */ |
| 176 | virtual GrRenderTarget* asRenderTarget() = 0; |
| 177 | |
| 178 | /** |
| 179 | * Removes the "rendertargetness" from a texture. This may or may not |
| 180 | * actually do anything with the underlying 3D API. |
| 181 | */ |
| 182 | virtual void removeRenderTarget() = 0; |
| 183 | |
| 184 | /** |
| 185 | * Return the native ID or handle to the texture, depending on the |
| 186 | * platform. e.g. on opengl, return the texture ID. |
| 187 | */ |
| 188 | virtual intptr_t getTextureHandle() = 0; |
| 189 | |
| 190 | #if GR_DEBUG |
| 191 | void validate() const { |
| 192 | this->INHERITED::validate(); |
| 193 | } |
| 194 | #else |
| 195 | void validate() const {} |
| 196 | #endif |
| 197 | |
| 198 | private: |
| 199 | uint32_t fAllocWidth; |
| 200 | uint32_t fAllocHeight; |
| 201 | uint32_t fContentWidth; |
| 202 | uint32_t fContentHeight; |
| 203 | // these two shift a fixed-point value into normalized coordinates |
| 204 | // for this texture if the texture is power of two sized. |
| 205 | int fShiftFixedX; |
| 206 | int fShiftFixedY; |
| 207 | PixelConfig fConfig; |
| 208 | |
| 209 | typedef GrRefCnt INHERITED; |
| 210 | }; |
| 211 | |
| 212 | #endif |
| 213 | |