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" |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 13 | #include "SkPoint.h" |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 14 | #include "GrRenderTarget.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | |
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; |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 18 | class GrTextureImpl; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 20 | class GrTexture : public GrSurface { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | public: |
commit-bot@chromium.org | 59e7d23 | 2014-05-09 18:02:51 +0000 | [diff] [blame] | 22 | /** |
| 23 | * Approximate number of bytes used by the texture |
| 24 | */ |
| 25 | virtual size_t gpuMemorySize() const SK_OVERRIDE; |
| 26 | |
| 27 | // GrSurface overrides |
| 28 | virtual bool readPixels(int left, int top, int width, int height, |
| 29 | GrPixelConfig config, |
| 30 | void* buffer, |
| 31 | size_t rowBytes = 0, |
| 32 | uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
| 33 | |
| 34 | virtual void writePixels(int left, int top, int width, int height, |
| 35 | GrPixelConfig config, |
| 36 | const void* buffer, |
| 37 | size_t rowBytes = 0, |
| 38 | uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
| 39 | |
commit-bot@chromium.org | 59e7d23 | 2014-05-09 18:02:51 +0000 | [diff] [blame] | 40 | virtual GrTexture* asTexture() SK_OVERRIDE { return this; } |
| 41 | virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 42 | virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return fRenderTarget.get(); } |
| 43 | virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { return fRenderTarget.get(); } |
commit-bot@chromium.org | 59e7d23 | 2014-05-09 18:02:51 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 46 | * Convert from texels to normalized texture coords for POT textures only. Please don't add |
| 47 | * new callsites for these functions. They are slated for removal. |
commit-bot@chromium.org | 59e7d23 | 2014-05-09 18:02:51 +0000 | [diff] [blame] | 48 | */ |
| 49 | SkFixed normalizeFixedX(SkFixed x) const { |
tfarina | f9dae78 | 2014-06-06 06:35:28 -0700 | [diff] [blame] | 50 | SkASSERT(SkIsPow2(fDesc.fWidth)); |
commit-bot@chromium.org | 59e7d23 | 2014-05-09 18:02:51 +0000 | [diff] [blame] | 51 | return x >> fShiftFixedX; |
| 52 | } |
| 53 | SkFixed normalizeFixedY(SkFixed y) const { |
tfarina | f9dae78 | 2014-06-06 06:35:28 -0700 | [diff] [blame] | 54 | SkASSERT(SkIsPow2(fDesc.fHeight)); |
commit-bot@chromium.org | 59e7d23 | 2014-05-09 18:02:51 +0000 | [diff] [blame] | 55 | return y >> fShiftFixedY; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Return the native ID or handle to the texture, depending on the |
| 60 | * platform. e.g. on OpenGL, return the texture ID. |
| 61 | */ |
| 62 | virtual GrBackendObject getTextureHandle() const = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | |
junov@chromium.org | 957ebdd | 2012-06-12 13:58:36 +0000 | [diff] [blame] | 64 | /** |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 65 | * This function indicates that the texture parameters (wrap mode, filtering, ...) have been |
| 66 | * changed externally to Skia. |
junov@chromium.org | 957ebdd | 2012-06-12 13:58:36 +0000 | [diff] [blame] | 67 | */ |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 68 | virtual void textureParamsModified() = 0; |
| 69 | SK_ATTR_DEPRECATED("Renamed to textureParamsModified.") |
| 70 | void invalidateCachedState() { this->textureParamsModified(); } |
| 71 | |
| 72 | /** |
| 73 | * Informational texture flags. This will be moved to the private GrTextureImpl class soon. |
| 74 | */ |
| 75 | enum FlagBits { |
| 76 | kFirstBit = (kLastPublic_GrTextureFlagBit << 1), |
| 77 | |
| 78 | /** |
| 79 | * This texture should be returned to the texture cache when |
| 80 | * it is no longer reffed |
| 81 | */ |
| 82 | kReturnToCache_FlagBit = kFirstBit, |
| 83 | }; |
| 84 | |
| 85 | void resetFlag(GrTextureFlags flags) { |
| 86 | fDesc.fFlags = fDesc.fFlags & ~flags; |
| 87 | } |
junov@chromium.org | 957ebdd | 2012-06-12 13:58:36 +0000 | [diff] [blame] | 88 | |
commit-bot@chromium.org | 59e7d23 | 2014-05-09 18:02:51 +0000 | [diff] [blame] | 89 | #ifdef SK_DEBUG |
| 90 | void validate() const { |
| 91 | this->INHERITED::validate(); |
| 92 | |
| 93 | this->validateDesc(); |
| 94 | } |
| 95 | #endif |
| 96 | |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 97 | GrTextureImpl* impl() { return reinterpret_cast<GrTextureImpl*>(this); } |
| 98 | const GrTextureImpl* impl() const { return reinterpret_cast<const GrTextureImpl*>(this); } |
| 99 | |
| 100 | protected: |
| 101 | // A texture refs its rt representation but not vice-versa. It is up to |
| 102 | // the subclass constructor to initialize this pointer. |
| 103 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 104 | |
| 105 | GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) |
| 106 | : INHERITED(gpu, isWrapped, desc) |
| 107 | , fRenderTarget(NULL) { |
| 108 | // only make sense if alloc size is pow2 |
| 109 | fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); |
| 110 | fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); |
| 111 | } |
| 112 | |
| 113 | virtual ~GrTexture(); |
| 114 | |
| 115 | // GrResource overrides |
| 116 | virtual void onRelease() SK_OVERRIDE; |
| 117 | virtual void onAbandon() SK_OVERRIDE; |
| 118 | |
| 119 | void validateDesc() const; |
| 120 | |
| 121 | private: |
| 122 | virtual void internal_dispose() const SK_OVERRIDE; |
| 123 | |
| 124 | // these two shift a fixed-point value into normalized coordinates |
| 125 | // for this texture if the texture is power of two sized. |
| 126 | int fShiftFixedX; |
| 127 | int fShiftFixedY; |
| 128 | |
| 129 | typedef GrSurface INHERITED; |
| 130 | }; |
| 131 | |
| 132 | class GrTextureImpl : public GrTexture { |
| 133 | public: |
| 134 | SK_DECLARE_INST_COUNT(GrTextureImpl) |
| 135 | |
| 136 | void setFlag(GrTextureFlags flags) { |
| 137 | fDesc.fFlags = fDesc.fFlags | flags; |
| 138 | } |
| 139 | void resetFlag(GrTextureFlags flags) { |
| 140 | fDesc.fFlags = fDesc.fFlags & ~flags; |
| 141 | } |
| 142 | bool isSetFlag(GrTextureFlags flags) const { |
| 143 | return 0 != (fDesc.fFlags & flags); |
| 144 | } |
| 145 | |
| 146 | void dirtyMipMaps(bool mipMapsDirty); |
| 147 | |
| 148 | bool mipMapsAreDirty() const { |
| 149 | return kValid_MipMapsStatus != fMipMapsStatus; |
| 150 | } |
| 151 | |
| 152 | bool hasMipMaps() const { |
| 153 | return kNotAllocated_MipMapsStatus != fMipMapsStatus; |
| 154 | } |
| 155 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 156 | static GrResourceKey ComputeKey(const GrGpu* gpu, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 157 | const GrTextureParams* params, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 158 | const GrTextureDesc& desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 159 | const GrCacheID& cacheID); |
| 160 | static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 161 | static bool NeedsResizing(const GrResourceKey& key); |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 162 | static bool NeedsBilerp(const GrResourceKey& key); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 163 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 164 | protected: |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 165 | GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 166 | : INHERITED(gpu, isWrapped, desc) |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 167 | , fMipMapsStatus(kNotAllocated_MipMapsStatus) { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 168 | } |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 169 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 170 | private: |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 171 | enum MipMapsStatus { |
| 172 | kNotAllocated_MipMapsStatus, |
| 173 | kAllocated_MipMapsStatus, |
| 174 | kValid_MipMapsStatus |
| 175 | }; |
| 176 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 177 | MipMapsStatus fMipMapsStatus; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 179 | typedef GrTexture INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 182 | /** |
| 183 | * Represents a texture that is intended to be accessed in device coords with an offset. |
| 184 | */ |
| 185 | class GrDeviceCoordTexture { |
| 186 | public: |
| 187 | GrDeviceCoordTexture() { fOffset.set(0, 0); } |
| 188 | |
| 189 | GrDeviceCoordTexture(const GrDeviceCoordTexture& other) { |
| 190 | *this = other; |
| 191 | } |
| 192 | |
| 193 | GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset) |
| 194 | : fTexture(SkSafeRef(texture)) |
| 195 | , fOffset(offset) { |
| 196 | } |
| 197 | |
| 198 | GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) { |
| 199 | fTexture.reset(SkSafeRef(other.fTexture.get())); |
| 200 | fOffset = other.fOffset; |
| 201 | return *this; |
| 202 | } |
| 203 | |
| 204 | const SkIPoint& offset() const { return fOffset; } |
| 205 | |
| 206 | void setOffset(const SkIPoint& offset) { fOffset = offset; } |
| 207 | void setOffset(int ox, int oy) { fOffset.set(ox, oy); } |
| 208 | |
| 209 | GrTexture* texture() const { return fTexture.get(); } |
| 210 | |
| 211 | GrTexture* setTexture(GrTexture* texture) { |
| 212 | fTexture.reset(SkSafeRef(texture)); |
| 213 | return texture; |
| 214 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 215 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 216 | private: |
| 217 | SkAutoTUnref<GrTexture> fTexture; |
| 218 | SkIPoint fOffset; |
| 219 | }; |
| 220 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | #endif |