| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #ifndef GrTexture_DEFINED | 9 #ifndef GrTexture_DEFINED |
| 10 #define GrTexture_DEFINED | 10 #define GrTexture_DEFINED |
| 11 | 11 |
| 12 #include "GrSurface.h" | 12 #include "GrSurface.h" |
| 13 #include "SkPoint.h" | 13 #include "SkPoint.h" |
| 14 #include "GrRenderTarget.h" | 14 #include "GrRenderTarget.h" |
| 15 | 15 |
| 16 class GrResourceKey; | 16 class GrResourceKey; |
| 17 class GrTextureParams; | 17 class GrTextureParams; |
| 18 class GrTextureImpl; | |
| 19 | 18 |
| 20 class GrTexture : public GrSurface { | 19 class GrTexture : public GrSurface { |
| 20 |
| 21 public: | 21 public: |
| 22 SK_DECLARE_INST_COUNT(GrTexture) |
| 23 // from GrResource |
| 24 /** |
| 25 * Informational texture flags |
| 26 */ |
| 27 enum FlagBits { |
| 28 kFirstBit = (kLastPublic_GrTextureFlagBit << 1), |
| 29 |
| 30 /** |
| 31 * This texture should be returned to the texture cache when |
| 32 * it is no longer reffed |
| 33 */ |
| 34 kReturnToCache_FlagBit = kFirstBit, |
| 35 }; |
| 36 |
| 37 void setFlag(GrTextureFlags flags) { |
| 38 fDesc.fFlags = fDesc.fFlags | flags; |
| 39 } |
| 40 void resetFlag(GrTextureFlags flags) { |
| 41 fDesc.fFlags = fDesc.fFlags & ~flags; |
| 42 } |
| 43 bool isSetFlag(GrTextureFlags flags) const { |
| 44 return 0 != (fDesc.fFlags & flags); |
| 45 } |
| 46 |
| 47 void dirtyMipMaps(bool mipMapsDirty); |
| 48 |
| 49 bool mipMapsAreDirty() const { |
| 50 return kValid_MipMapsStatus != fMipMapsStatus; |
| 51 } |
| 52 |
| 22 /** | 53 /** |
| 23 * Approximate number of bytes used by the texture | 54 * Approximate number of bytes used by the texture |
| 24 */ | 55 */ |
| 25 virtual size_t gpuMemorySize() const SK_OVERRIDE; | 56 virtual size_t gpuMemorySize() const SK_OVERRIDE; |
| 26 | 57 |
| 27 // GrSurface overrides | 58 // GrSurface overrides |
| 28 virtual bool readPixels(int left, int top, int width, int height, | 59 virtual bool readPixels(int left, int top, int width, int height, |
| 29 GrPixelConfig config, | 60 GrPixelConfig config, |
| 30 void* buffer, | 61 void* buffer, |
| 31 size_t rowBytes = 0, | 62 size_t rowBytes = 0, |
| 32 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; | 63 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
| 33 | 64 |
| 34 virtual void writePixels(int left, int top, int width, int height, | 65 virtual void writePixels(int left, int top, int width, int height, |
| 35 GrPixelConfig config, | 66 GrPixelConfig config, |
| 36 const void* buffer, | 67 const void* buffer, |
| 37 size_t rowBytes = 0, | 68 size_t rowBytes = 0, |
| 38 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; | 69 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
| 39 | 70 |
| 71 /** |
| 72 * @return this texture |
| 73 */ |
| 40 virtual GrTexture* asTexture() SK_OVERRIDE { return this; } | 74 virtual GrTexture* asTexture() SK_OVERRIDE { return this; } |
| 41 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } | 75 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } |
| 42 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return fRenderTarget.
get(); } | |
| 43 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { return fR
enderTarget.get(); } | |
| 44 | 76 |
| 45 /** | 77 /** |
| 46 * Convert from texels to normalized texture coords for POT textures only. P
lease don't add | 78 * Retrieves the render target underlying this texture that can be passed to |
| 47 * new callsites for these functions. They are slated for removal. | 79 * GrGpu::setRenderTarget(). |
| 80 * |
| 81 * @return handle to render target or NULL if the texture is not a |
| 82 * render target |
| 83 */ |
| 84 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { |
| 85 return fRenderTarget.get(); |
| 86 } |
| 87 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { |
| 88 return fRenderTarget.get(); |
| 89 } |
| 90 |
| 91 // GrTexture |
| 92 /** |
| 93 * Convert from texels to normalized texture coords for POT textures |
| 94 * only. |
| 48 */ | 95 */ |
| 49 SkFixed normalizeFixedX(SkFixed x) const { | 96 SkFixed normalizeFixedX(SkFixed x) const { |
| 50 SkASSERT(GrIsPow2(fDesc.fWidth)); | 97 SkASSERT(GrIsPow2(fDesc.fWidth)); |
| 51 return x >> fShiftFixedX; | 98 return x >> fShiftFixedX; |
| 52 } | 99 } |
| 53 SkFixed normalizeFixedY(SkFixed y) const { | 100 SkFixed normalizeFixedY(SkFixed y) const { |
| 54 SkASSERT(GrIsPow2(fDesc.fHeight)); | 101 SkASSERT(GrIsPow2(fDesc.fHeight)); |
| 55 return y >> fShiftFixedY; | 102 return y >> fShiftFixedY; |
| 56 } | 103 } |
| 57 | 104 |
| 58 /** | 105 /** |
| 59 * Return the native ID or handle to the texture, depending on the | 106 * Return the native ID or handle to the texture, depending on the |
| 60 * platform. e.g. on OpenGL, return the texture ID. | 107 * platform. e.g. on OpenGL, return the texture ID. |
| 61 */ | 108 */ |
| 62 virtual GrBackendObject getTextureHandle() const = 0; | 109 virtual GrBackendObject getTextureHandle() const = 0; |
| 63 | 110 |
| 111 /** |
| 112 * Call this when the state of the native API texture object is |
| 113 * altered directly, without being tracked by skia. |
| 114 */ |
| 115 virtual void invalidateCachedState() = 0; |
| 116 |
| 64 #ifdef SK_DEBUG | 117 #ifdef SK_DEBUG |
| 65 void validate() const { | 118 void validate() const { |
| 66 this->INHERITED::validate(); | 119 this->INHERITED::validate(); |
| 67 | 120 |
| 68 this->validateDesc(); | 121 this->validateDesc(); |
| 69 } | 122 } |
| 70 #endif | 123 #endif |
| 71 | 124 |
| 72 GrTextureImpl* impl() { return reinterpret_cast<GrTextureImpl*>(this); } | |
| 73 const GrTextureImpl* impl() const { return reinterpret_cast<const GrTextureI
mpl*>(this); } | |
| 74 | |
| 75 protected: | |
| 76 // A texture refs its rt representation but not vice-versa. It is up to | |
| 77 // the subclass constructor to initialize this pointer. | |
| 78 SkAutoTUnref<GrRenderTarget> fRenderTarget; | |
| 79 | |
| 80 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) | |
| 81 : INHERITED(gpu, isWrapped, desc) | |
| 82 , fRenderTarget(NULL) { | |
| 83 // only make sense if alloc size is pow2 | |
| 84 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); | |
| 85 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); | |
| 86 } | |
| 87 | |
| 88 virtual ~GrTexture(); | |
| 89 | |
| 90 // GrResource overrides | |
| 91 virtual void onRelease() SK_OVERRIDE; | |
| 92 virtual void onAbandon() SK_OVERRIDE; | |
| 93 | |
| 94 void validateDesc() const; | |
| 95 | |
| 96 private: | |
| 97 virtual void internal_dispose() const SK_OVERRIDE; | |
| 98 | |
| 99 // these two shift a fixed-point value into normalized coordinates | |
| 100 // for this texture if the texture is power of two sized. | |
| 101 int fShiftFixedX; | |
| 102 int fShiftFixedY; | |
| 103 | |
| 104 typedef GrSurface INHERITED; | |
| 105 }; | |
| 106 | |
| 107 class GrTextureImpl : public GrTexture { | |
| 108 public: | |
| 109 SK_DECLARE_INST_COUNT(GrTextureImpl) | |
| 110 /** | |
| 111 * Informational texture flags | |
| 112 */ | |
| 113 enum FlagBits { | |
| 114 kFirstBit = (kLastPublic_GrTextureFlagBit << 1), | |
| 115 | |
| 116 /** | |
| 117 * This texture should be returned to the texture cache when | |
| 118 * it is no longer reffed | |
| 119 */ | |
| 120 kReturnToCache_FlagBit = kFirstBit, | |
| 121 }; | |
| 122 | |
| 123 void setFlag(GrTextureFlags flags) { | |
| 124 fDesc.fFlags = fDesc.fFlags | flags; | |
| 125 } | |
| 126 void resetFlag(GrTextureFlags flags) { | |
| 127 fDesc.fFlags = fDesc.fFlags & ~flags; | |
| 128 } | |
| 129 bool isSetFlag(GrTextureFlags flags) const { | |
| 130 return 0 != (fDesc.fFlags & flags); | |
| 131 } | |
| 132 | |
| 133 void dirtyMipMaps(bool mipMapsDirty); | |
| 134 | |
| 135 bool mipMapsAreDirty() const { | |
| 136 return kValid_MipMapsStatus != fMipMapsStatus; | |
| 137 } | |
| 138 | |
| 139 bool hasMipMaps() const { | |
| 140 return kNotAllocated_MipMapsStatus != fMipMapsStatus; | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * Call this when the state of the native API texture object is | |
| 145 * altered directly, without being tracked by skia. | |
| 146 */ | |
| 147 virtual void invalidateCachedState() = 0; | |
| 148 | |
| 149 static GrResourceKey ComputeKey(const GrGpu* gpu, | 125 static GrResourceKey ComputeKey(const GrGpu* gpu, |
| 150 const GrTextureParams* params, | 126 const GrTextureParams* params, |
| 151 const GrTextureDesc& desc, | 127 const GrTextureDesc& desc, |
| 152 const GrCacheID& cacheID); | 128 const GrCacheID& cacheID); |
| 153 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc); | 129 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc); |
| 154 static bool NeedsResizing(const GrResourceKey& key); | 130 static bool NeedsResizing(const GrResourceKey& key); |
| 155 static bool NeedsBilerp(const GrResourceKey& key); | 131 static bool NeedsBilerp(const GrResourceKey& key); |
| 156 | 132 |
| 157 protected: | 133 protected: |
| 158 GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) | 134 // A texture refs its rt representation but not vice-versa. It is up to |
| 135 // the subclass constructor to initialize this pointer. |
| 136 SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 137 |
| 138 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) |
| 159 : INHERITED(gpu, isWrapped, desc) | 139 : INHERITED(gpu, isWrapped, desc) |
| 140 , fRenderTarget(NULL) |
| 160 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { | 141 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { |
| 142 |
| 143 // only make sense if alloc size is pow2 |
| 144 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); |
| 145 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); |
| 161 } | 146 } |
| 147 virtual ~GrTexture(); |
| 148 |
| 149 // GrResource overrides |
| 150 virtual void onRelease() SK_OVERRIDE; |
| 151 virtual void onAbandon() SK_OVERRIDE; |
| 152 |
| 153 void validateDesc() const; |
| 162 | 154 |
| 163 private: | 155 private: |
| 164 enum MipMapsStatus { | 156 enum MipMapsStatus { |
| 165 kNotAllocated_MipMapsStatus, | 157 kNotAllocated_MipMapsStatus, |
| 166 kAllocated_MipMapsStatus, | 158 kAllocated_MipMapsStatus, |
| 167 kValid_MipMapsStatus | 159 kValid_MipMapsStatus |
| 168 }; | 160 }; |
| 169 | 161 |
| 162 // these two shift a fixed-point value into normalized coordinates |
| 163 // for this texture if the texture is power of two sized. |
| 164 int fShiftFixedX; |
| 165 int fShiftFixedY; |
| 166 |
| 170 MipMapsStatus fMipMapsStatus; | 167 MipMapsStatus fMipMapsStatus; |
| 171 | 168 |
| 172 typedef GrTexture INHERITED; | 169 virtual void internal_dispose() const SK_OVERRIDE; |
| 170 |
| 171 typedef GrSurface INHERITED; |
| 173 }; | 172 }; |
| 174 | 173 |
| 175 /** | 174 /** |
| 176 * Represents a texture that is intended to be accessed in device coords with an
offset. | 175 * Represents a texture that is intended to be accessed in device coords with an
offset. |
| 177 */ | 176 */ |
| 178 class GrDeviceCoordTexture { | 177 class GrDeviceCoordTexture { |
| 179 public: | 178 public: |
| 180 GrDeviceCoordTexture() { fOffset.set(0, 0); } | 179 GrDeviceCoordTexture() { fOffset.set(0, 0); } |
| 181 | 180 |
| 182 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) { | 181 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 198 | 197 |
| 199 void setOffset(const SkIPoint& offset) { fOffset = offset; } | 198 void setOffset(const SkIPoint& offset) { fOffset = offset; } |
| 200 void setOffset(int ox, int oy) { fOffset.set(ox, oy); } | 199 void setOffset(int ox, int oy) { fOffset.set(ox, oy); } |
| 201 | 200 |
| 202 GrTexture* texture() const { return fTexture.get(); } | 201 GrTexture* texture() const { return fTexture.get(); } |
| 203 | 202 |
| 204 GrTexture* setTexture(GrTexture* texture) { | 203 GrTexture* setTexture(GrTexture* texture) { |
| 205 fTexture.reset(SkSafeRef(texture)); | 204 fTexture.reset(SkSafeRef(texture)); |
| 206 return texture; | 205 return texture; |
| 207 } | 206 } |
| 208 | |
| 209 private: | 207 private: |
| 210 SkAutoTUnref<GrTexture> fTexture; | 208 SkAutoTUnref<GrTexture> fTexture; |
| 211 SkIPoint fOffset; | 209 SkIPoint fOffset; |
| 212 }; | 210 }; |
| 213 | 211 |
| 214 #endif | 212 #endif |
| OLD | NEW |