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 | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 19 | class GrTexture : public GrSurface { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 20 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | public: |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 22 | SK_DECLARE_INST_COUNT(GrTexture) |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 23 | GR_DECLARE_RESOURCE_CACHE_TYPE() |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 24 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 25 | // from GrResource |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | /** |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 27 | * Informational texture flags |
| 28 | */ |
| 29 | enum FlagBits { |
| 30 | kFirstBit = (kLastPublic_GrTextureFlagBit << 1), |
| 31 | |
| 32 | /** |
| 33 | * This texture should be returned to the texture cache when |
| 34 | * it is no longer reffed |
| 35 | */ |
| 36 | kReturnToCache_FlagBit = kFirstBit, |
| 37 | }; |
| 38 | |
| 39 | void setFlag(GrTextureFlags flags) { |
| 40 | fDesc.fFlags = fDesc.fFlags | flags; |
| 41 | } |
| 42 | void resetFlag(GrTextureFlags flags) { |
| 43 | fDesc.fFlags = fDesc.fFlags & ~flags; |
| 44 | } |
| 45 | bool isSetFlag(GrTextureFlags flags) const { |
| 46 | return 0 != (fDesc.fFlags & flags); |
| 47 | } |
| 48 | |
| 49 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 50 | * Approximate number of bytes used by the texture |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | */ |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 52 | virtual size_t sizeInBytes() const SK_OVERRIDE { |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 53 | return (size_t) fDesc.fWidth * |
| 54 | fDesc.fHeight * |
| 55 | GrBytesPerPixel(fDesc.fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | } |
| 57 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame^] | 58 | // GrSurface overrides |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 59 | virtual bool readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame^] | 60 | GrPixelConfig config, |
| 61 | void* buffer, |
| 62 | size_t rowBytes = 0, |
| 63 | uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 64 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 65 | virtual void writePixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame^] | 66 | GrPixelConfig config, |
| 67 | const void* buffer, |
| 68 | size_t rowBytes = 0, |
| 69 | uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * @return this texture |
| 73 | */ |
| 74 | virtual GrTexture* asTexture() SK_OVERRIDE { return this; } |
| 75 | virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 78 | * Retrieves the render target underlying this texture that can be passed to |
| 79 | * GrGpu::setRenderTarget(). |
| 80 | * |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 81 | * @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] | 82 | * render target |
| 83 | */ |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 84 | virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { |
| 85 | return fRenderTarget; |
| 86 | } |
| 87 | virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { |
| 88 | return fRenderTarget; |
| 89 | } |
| 90 | |
| 91 | // GrTexture |
| 92 | /** |
| 93 | * Convert from texels to normalized texture coords for POT textures |
| 94 | * only. |
| 95 | */ |
| 96 | GrFixed normalizeFixedX(GrFixed x) const { |
| 97 | GrAssert(GrIsPow2(fDesc.fWidth)); |
| 98 | return x >> fShiftFixedX; |
| 99 | } |
| 100 | GrFixed normalizeFixedY(GrFixed y) const { |
| 101 | GrAssert(GrIsPow2(fDesc.fHeight)); |
| 102 | return y >> fShiftFixedY; |
| 103 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | |
| 105 | /** |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 106 | * Removes the reference on the associated GrRenderTarget held by this |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 107 | * texture. Afterwards asRenderTarget() will return NULL. The |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 108 | * GrRenderTarget survives the release if another ref is held on it. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | */ |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 110 | void releaseRenderTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Return the native ID or handle to the texture, depending on the |
| 114 | * platform. e.g. on opengl, return the texture ID. |
| 115 | */ |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 116 | virtual intptr_t getTextureHandle() const = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | |
junov@chromium.org | 957ebdd | 2012-06-12 13:58:36 +0000 | [diff] [blame] | 118 | /** |
| 119 | * Call this when the state of the native API texture object is |
| 120 | * altered directly, without being tracked by skia. |
| 121 | */ |
| 122 | virtual void invalidateCachedState() = 0; |
| 123 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | #if GR_DEBUG |
| 125 | void validate() const { |
| 126 | this->INHERITED::validate(); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 127 | |
| 128 | this->validateDesc(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 129 | } |
| 130 | #else |
| 131 | void validate() const {} |
| 132 | #endif |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 133 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 134 | static GrResourceKey ComputeKey(const GrGpu* gpu, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 135 | const GrTextureParams* sampler, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 136 | const GrTextureDesc& desc, |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 137 | const GrCacheData& cacheData, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 138 | bool scratch); |
| 139 | |
| 140 | static bool NeedsResizing(const GrResourceKey& key); |
| 141 | static bool IsScratchTexture(const GrResourceKey& key); |
| 142 | static bool NeedsFiltering(const GrResourceKey& key); |
| 143 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 144 | protected: |
| 145 | GrRenderTarget* fRenderTarget; // texture refs its rt representation |
| 146 | // base class cons sets to NULL |
| 147 | // subclass cons can create and set |
| 148 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 149 | GrTexture(GrGpu* gpu, const GrTextureDesc& desc) |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 150 | : INHERITED(gpu, desc) |
| 151 | , fRenderTarget(NULL) { |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 152 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 153 | // only make sense if alloc size is pow2 |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 154 | fShiftFixedX = 31 - Gr_clz(fDesc.fWidth); |
| 155 | fShiftFixedY = 31 - Gr_clz(fDesc.fHeight); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 156 | } |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 157 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 158 | // GrResource overrides |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 159 | virtual void onRelease() SK_OVERRIDE; |
| 160 | virtual void onAbandon() SK_OVERRIDE; |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 161 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 162 | void validateDesc() const; |
| 163 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 164 | private: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 165 | // these two shift a fixed-point value into normalized coordinates |
| 166 | // for this texture if the texture is power of two sized. |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 167 | int fShiftFixedX; |
| 168 | int fShiftFixedY; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 169 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 170 | virtual void internal_dispose() const SK_OVERRIDE; |
| 171 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 172 | typedef GrSurface INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | #endif |
| 176 | |