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