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 |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 28 | * created by a createTexture with the kRenderTarget_TextureFlag flag. |
| 29 | * Additionally, GrContext provides methods for creating GrRenderTargets |
| 30 | * that wrap externally created render targets. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | */ |
| 32 | class GrRenderTarget : public GrRefCnt { |
| 33 | public: |
| 34 | /** |
| 35 | * @return the width of the rendertarget |
| 36 | */ |
| 37 | virtual uint32_t width() const = 0; |
| 38 | /** |
| 39 | * @return the height of the rendertarget |
| 40 | */ |
| 41 | virtual uint32_t height() const = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 42 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | /** |
| 44 | * @return the texture associated with the rendertarget, may be NULL. |
| 45 | */ |
| 46 | GrTexture* asTexture() {return fTexture;} |
| 47 | |
| 48 | protected: |
| 49 | GrRenderTarget(GrTexture* texture) : fTexture(texture) {} |
| 50 | GrTexture* fTexture; |
| 51 | }; |
| 52 | |
| 53 | class GrTexture : public GrRefCnt { |
| 54 | public: |
| 55 | enum PixelConfig { |
| 56 | kUnknown_PixelConfig, |
| 57 | kAlpha_8_PixelConfig, |
| 58 | kIndex_8_PixelConfig, |
| 59 | kRGB_565_PixelConfig, |
| 60 | kRGBA_4444_PixelConfig, //!< premultiplied |
| 61 | kRGBA_8888_PixelConfig, //!< premultiplied |
| 62 | kRGBX_8888_PixelConfig, //!< treat the alpha channel as opaque |
| 63 | }; |
| 64 | static size_t BytesPerPixel(PixelConfig); |
| 65 | static bool PixelConfigIsOpaque(PixelConfig); |
| 66 | |
| 67 | protected: |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 68 | GrTexture(int width, |
| 69 | int height, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 70 | PixelConfig config) : |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 71 | fWidth(width), |
| 72 | fHeight(height), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | fConfig(config) { |
| 74 | // only make sense if alloc size is pow2 |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 75 | fShiftFixedX = 31 - Gr_clz(fWidth); |
| 76 | fShiftFixedY = 31 - Gr_clz(fHeight); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 77 | } |
| 78 | public: |
| 79 | virtual ~GrTexture(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 80 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 82 | * Retrieves the width of the texture. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 83 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | * @return the width in texels |
| 85 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 86 | int width() const { return fWidth; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 87 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 88 | * Retrieves the height of the texture. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 89 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | * @return the height in texels |
| 91 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 92 | int height() const { return fHeight; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Convert from texels to normalized texture coords for POT textures |
| 96 | * only. |
| 97 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 98 | GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | return x >> fShiftFixedX; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 100 | GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 101 | return y >> fShiftFixedY; } |
| 102 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 103 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | * Retrieves the pixel config specified when the texture was created. |
| 105 | */ |
| 106 | PixelConfig config() const { return fConfig; } |
| 107 | |
| 108 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 109 | * Approximate number of bytes used by the texture |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | */ |
| 111 | size_t sizeInBytes() const { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 112 | return fWidth * fHeight * BytesPerPixel(fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Updates a subrectangle of texels in the texture. |
| 117 | * |
| 118 | * @param x left edge of rectangle to update |
| 119 | * @param y top edge of rectangle to update |
| 120 | * @param width width of rectangle to update |
| 121 | * @param height height of rectangle to update |
| 122 | * @param srcData width*height texels of data in same format that was used |
| 123 | * at texture creation. |
| 124 | */ |
| 125 | virtual void uploadTextureData(uint32_t x, |
| 126 | uint32_t y, |
| 127 | uint32_t width, |
| 128 | uint32_t height, |
| 129 | const void* srcData) = 0; |
| 130 | /** |
| 131 | * 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] | 132 | * 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] | 133 | * underlying API. |
| 134 | */ |
| 135 | virtual void abandon() = 0; |
| 136 | |
| 137 | /** |
| 138 | * Queries whether the texture was created as a render target. |
| 139 | * |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 140 | * 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] | 141 | * returns true. |
| 142 | * |
| 143 | * @return true if the texture was created as a render target. |
| 144 | */ |
| 145 | virtual bool isRenderTarget() const = 0; |
| 146 | |
| 147 | /** |
| 148 | * Retrieves the render target underlying this texture that can be passed to |
| 149 | * GrGpu::setRenderTarget(). |
| 150 | * |
| 151 | * If isRenderTarget() is false then the returned handle is undefined. |
| 152 | * |
| 153 | * @return handle to render target or undefined if the texture is not a |
| 154 | * render target |
| 155 | */ |
| 156 | virtual GrRenderTarget* asRenderTarget() = 0; |
| 157 | |
| 158 | /** |
| 159 | * Removes the "rendertargetness" from a texture. This may or may not |
| 160 | * actually do anything with the underlying 3D API. |
| 161 | */ |
| 162 | virtual void removeRenderTarget() = 0; |
| 163 | |
| 164 | /** |
| 165 | * Return the native ID or handle to the texture, depending on the |
| 166 | * platform. e.g. on opengl, return the texture ID. |
| 167 | */ |
| 168 | virtual intptr_t getTextureHandle() = 0; |
| 169 | |
| 170 | #if GR_DEBUG |
| 171 | void validate() const { |
| 172 | this->INHERITED::validate(); |
| 173 | } |
| 174 | #else |
| 175 | void validate() const {} |
| 176 | #endif |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 177 | |
| 178 | private: |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 179 | int fWidth; |
| 180 | int fHeight; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | // these two shift a fixed-point value into normalized coordinates |
| 182 | // for this texture if the texture is power of two sized. |
| 183 | int fShiftFixedX; |
| 184 | int fShiftFixedY; |
| 185 | PixelConfig fConfig; |
| 186 | |
| 187 | typedef GrRefCnt INHERITED; |
| 188 | }; |
| 189 | |
| 190 | #endif |
| 191 | |