epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrTexture_DEFINED |
| 12 | #define GrTexture_DEFINED |
| 13 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 14 | #include "GrResource.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 16 | class GrRenderTarget; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame^] | 17 | class GrResourceKey; |
| 18 | class GrSamplerState; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 20 | class GrTexture : public GrResource { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 21 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | public: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | /** |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame^] | 24 | * Key generated by client. Should be a unique key on the texture data. |
| 25 | * Does not need to consider that width and height of the texture. Two |
| 26 | * textures with the same TextureKey but different bounds will not collide. |
| 27 | */ |
| 28 | typedef uint64_t TextureKey; |
| 29 | |
| 30 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 31 | * Retrieves the width of the texture. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 32 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | * @return the width in texels |
| 34 | */ |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 35 | int width() const { return fDesc.fWidth; } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 36 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 38 | * Retrieves the height of the texture. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 39 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | * @return the height in texels |
| 41 | */ |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 42 | int height() const { return fDesc.fHeight; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Convert from texels to normalized texture coords for POT textures |
| 46 | * only. |
| 47 | */ |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 48 | GrFixed normalizeFixedX(GrFixed x) const { |
| 49 | GrAssert(GrIsPow2(fDesc.fWidth)); |
| 50 | return x >> fShiftFixedX; |
| 51 | } |
| 52 | GrFixed normalizeFixedY(GrFixed y) const { |
| 53 | GrAssert(GrIsPow2(fDesc.fHeight)); |
| 54 | return y >> fShiftFixedY; |
| 55 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 57 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | * Retrieves the pixel config specified when the texture was created. |
| 59 | */ |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 60 | GrPixelConfig config() const { return fDesc.fConfig; } |
| 61 | |
| 62 | /** |
| 63 | * Return the descriptor describing the texture |
| 64 | */ |
| 65 | const GrTextureDesc& desc() const { return fDesc; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | |
| 67 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 68 | * Approximate number of bytes used by the texture |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | */ |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 70 | virtual size_t sizeInBytes() const { |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 71 | return (size_t) fDesc.fWidth * |
| 72 | fDesc.fHeight * |
| 73 | GrBytesPerPixel(fDesc.fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 77 | * Read a rectangle of pixels from the texture. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 78 | * @param left left edge of the rectangle to read (inclusive) |
| 79 | * @param top top edge of the rectangle to read (inclusive) |
| 80 | * @param width width of rectangle to read in pixels. |
| 81 | * @param height height of rectangle to read in pixels. |
| 82 | * @param config the pixel config of the destination buffer |
| 83 | * @param buffer memory to read the rectangle into. |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 84 | * @param rowBytes number of bytes bewtween consecutive rows. Zero |
| 85 | * means rows are tightly packed. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 86 | * |
| 87 | * @return true if the read succeeded, false if not. The read can fail |
| 88 | * because of a unsupported pixel config. |
| 89 | */ |
| 90 | bool readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 91 | GrPixelConfig config, void* buffer, |
| 92 | size_t rowBytes); |
| 93 | |
| 94 | /** |
| 95 | * Writes a rectangle of pixels to the texture. |
| 96 | * @param left left edge of the rectangle to write (inclusive) |
| 97 | * @param top top edge of the rectangle to write (inclusive) |
| 98 | * @param width width of rectangle to write in pixels. |
| 99 | * @param height height of rectangle to write in pixels. |
| 100 | * @param config the pixel config of the source buffer |
| 101 | * @param buffer memory to read pixels from |
robertphillips@google.com | 09042b8 | 2012-04-06 20:01:46 +0000 | [diff] [blame] | 102 | * @param rowBytes number of bytes between consecutive rows. Zero |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 103 | * means rows are tightly packed. |
| 104 | */ |
| 105 | void writePixels(int left, int top, int width, int height, |
| 106 | GrPixelConfig config, const void* buffer, |
| 107 | size_t rowBytes); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | * Retrieves the render target underlying this texture that can be passed to |
| 111 | * GrGpu::setRenderTarget(). |
| 112 | * |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 113 | * @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] | 114 | * render target |
| 115 | */ |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 116 | GrRenderTarget* asRenderTarget() { return fRenderTarget; } |
bsalomon@google.com | b4725b4 | 2012-03-30 17:24:17 +0000 | [diff] [blame] | 117 | const GrRenderTarget* asRenderTarget() const { return fRenderTarget; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 120 | * Removes the reference on the associated GrRenderTarget held by this |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 121 | * texture. Afterwards asRenderTarget() will return NULL. The |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 122 | * GrRenderTarget survives the release if another ref is held on it. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | */ |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 124 | void releaseRenderTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Return the native ID or handle to the texture, depending on the |
| 128 | * platform. e.g. on opengl, return the texture ID. |
| 129 | */ |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 130 | virtual intptr_t getTextureHandle() const = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | |
| 132 | #if GR_DEBUG |
| 133 | void validate() const { |
| 134 | this->INHERITED::validate(); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 135 | |
| 136 | this->validateDesc(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | } |
| 138 | #else |
| 139 | void validate() const {} |
| 140 | #endif |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 141 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame^] | 142 | static GrResourceKey ComputeKey(const GrGpu* gpu, |
| 143 | const GrSamplerState* sampler, |
| 144 | TextureKey clientKey, |
| 145 | const GrTextureDesc& desc, |
| 146 | bool scratch); |
| 147 | |
| 148 | static bool NeedsResizing(const GrResourceKey& key); |
| 149 | static bool IsScratchTexture(const GrResourceKey& key); |
| 150 | static bool NeedsFiltering(const GrResourceKey& key); |
| 151 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 152 | protected: |
| 153 | GrRenderTarget* fRenderTarget; // texture refs its rt representation |
| 154 | // base class cons sets to NULL |
| 155 | // subclass cons can create and set |
| 156 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 157 | GrTexture(GrGpu* gpu, const GrTextureDesc& desc) |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 158 | : INHERITED(gpu) |
| 159 | , fRenderTarget(NULL) |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 160 | , fDesc(desc) { |
| 161 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 162 | // only make sense if alloc size is pow2 |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 163 | fShiftFixedX = 31 - Gr_clz(fDesc.fWidth); |
| 164 | fShiftFixedY = 31 - Gr_clz(fDesc.fHeight); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 165 | } |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 166 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 167 | // GrResource overrides |
| 168 | virtual void onRelease() { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 169 | this->releaseRenderTarget(); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 170 | } |
| 171 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 172 | virtual void onAbandon(); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 173 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 174 | void validateDesc() const; |
| 175 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 176 | private: |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 177 | GrTextureDesc fDesc; |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 178 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 | // these two shift a fixed-point value into normalized coordinates |
| 180 | // for this texture if the texture is power of two sized. |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 181 | int fShiftFixedX; |
| 182 | int fShiftFixedY; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 183 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 184 | typedef GrResource INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | #endif |
| 188 | |