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