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; |
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 | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 23 | // from GrResource |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | /** |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 43 | bool isSetFlag(GrTextureFlags flags) const { |
| 44 | return 0 != (fDesc.fFlags & flags); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 45 | } |
skia.committer@gmail.com | aeefb2a | 2013-07-27 07:01:06 +0000 | [diff] [blame] | 46 | |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 47 | void dirtyMipMaps(bool mipMapsDirty) { |
| 48 | fMipMapsDirty = mipMapsDirty; |
| 49 | } |
skia.committer@gmail.com | aeefb2a | 2013-07-27 07:01:06 +0000 | [diff] [blame] | 50 | |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 51 | bool mipMapsAreDirty() const { |
| 52 | return fMipMapsDirty; |
| 53 | } |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 56 | * Approximate number of bytes used by the texture |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | */ |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 58 | virtual size_t sizeInBytes() const SK_OVERRIDE { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 59 | return (size_t) fDesc.fWidth * |
| 60 | fDesc.fHeight * |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 61 | GrBytesPerPixel(fDesc.fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | } |
| 63 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 64 | // GrSurface overrides |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 65 | virtual bool readPixels(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 | void* buffer, |
| 68 | size_t rowBytes = 0, |
| 69 | uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 70 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 71 | virtual void writePixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 72 | GrPixelConfig config, |
| 73 | const void* buffer, |
| 74 | size_t rowBytes = 0, |
| 75 | uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * @return this texture |
| 79 | */ |
| 80 | virtual GrTexture* asTexture() SK_OVERRIDE { return this; } |
| 81 | virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | * Retrieves the render target underlying this texture that can be passed to |
| 85 | * GrGpu::setRenderTarget(). |
| 86 | * |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 87 | * @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] | 88 | * render target |
| 89 | */ |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 90 | virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 91 | return fRenderTarget.get(); |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 92 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 93 | virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 94 | return fRenderTarget.get(); |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // GrTexture |
| 98 | /** |
| 99 | * Convert from texels to normalized texture coords for POT textures |
| 100 | * only. |
| 101 | */ |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 102 | GrFixed normalizeFixedX(GrFixed x) const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 103 | SkASSERT(GrIsPow2(fDesc.fWidth)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 104 | return x >> fShiftFixedX; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 105 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 106 | GrFixed normalizeFixedY(GrFixed y) const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 107 | SkASSERT(GrIsPow2(fDesc.fHeight)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 108 | return y >> fShiftFixedY; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 109 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | |
| 111 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 112 | * Return the native ID or handle to the texture, depending on the |
bsalomon@google.com | 08afc84 | 2012-10-25 18:56:10 +0000 | [diff] [blame] | 113 | * platform. e.g. on OpenGL, return the texture ID. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | */ |
bsalomon@google.com | 08afc84 | 2012-10-25 18:56:10 +0000 | [diff] [blame] | 115 | virtual GrBackendObject getTextureHandle() const = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | |
junov@chromium.org | 957ebdd | 2012-06-12 13:58:36 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Call this when the state of the native API texture object is |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 119 | * altered directly, without being tracked by skia. |
junov@chromium.org | 957ebdd | 2012-06-12 13:58:36 +0000 | [diff] [blame] | 120 | */ |
| 121 | virtual void invalidateCachedState() = 0; |
| 122 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 123 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | void validate() const { |
| 125 | this->INHERITED::validate(); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 126 | |
| 127 | this->validateDesc(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 129 | #endif |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 130 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 131 | static GrResourceKey ComputeKey(const GrGpu* gpu, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 132 | const GrTextureParams* params, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 133 | const GrTextureDesc& desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 134 | const GrCacheID& cacheID); |
| 135 | static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 136 | static bool NeedsResizing(const GrResourceKey& key); |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 137 | static bool NeedsBilerp(const GrResourceKey& key); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 138 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 139 | protected: |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 140 | // A texture refs its rt representation but not vice-versa. It is up to |
| 141 | // the subclass constructor to initialize this pointer. |
| 142 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 143 | |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 144 | GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) |
| 145 | : INHERITED(gpu, isWrapped, desc) |
skia.committer@gmail.com | aeefb2a | 2013-07-27 07:01:06 +0000 | [diff] [blame] | 146 | , fRenderTarget(NULL) |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 147 | , fMipMapsDirty(true) { |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 148 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 149 | // only make sense if alloc size is pow2 |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 150 | fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); |
| 151 | fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 152 | } |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 153 | virtual ~GrTexture(); |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 154 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 155 | // GrResource overrides |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 156 | virtual void onRelease() SK_OVERRIDE; |
| 157 | virtual void onAbandon() SK_OVERRIDE; |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 158 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 159 | void validateDesc() const; |
| 160 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 161 | private: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | // these two shift a fixed-point value into normalized coordinates |
| 163 | // for this texture if the texture is power of two sized. |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 164 | int fShiftFixedX; |
| 165 | int fShiftFixedY; |
skia.committer@gmail.com | aeefb2a | 2013-07-27 07:01:06 +0000 | [diff] [blame] | 166 | |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 167 | bool fMipMapsDirty; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 168 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 169 | virtual void internal_dispose() const SK_OVERRIDE; |
| 170 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 171 | typedef GrSurface INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 174 | /** |
| 175 | * Represents a texture that is intended to be accessed in device coords with an offset. |
| 176 | */ |
| 177 | class GrDeviceCoordTexture { |
| 178 | public: |
| 179 | GrDeviceCoordTexture() { fOffset.set(0, 0); } |
| 180 | |
| 181 | GrDeviceCoordTexture(const GrDeviceCoordTexture& other) { |
| 182 | *this = other; |
| 183 | } |
| 184 | |
| 185 | GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset) |
| 186 | : fTexture(SkSafeRef(texture)) |
| 187 | , fOffset(offset) { |
| 188 | } |
| 189 | |
| 190 | GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) { |
| 191 | fTexture.reset(SkSafeRef(other.fTexture.get())); |
| 192 | fOffset = other.fOffset; |
| 193 | return *this; |
| 194 | } |
| 195 | |
| 196 | const SkIPoint& offset() const { return fOffset; } |
| 197 | |
| 198 | void setOffset(const SkIPoint& offset) { fOffset = offset; } |
| 199 | void setOffset(int ox, int oy) { fOffset.set(ox, oy); } |
| 200 | |
| 201 | GrTexture* texture() const { return fTexture.get(); } |
| 202 | |
| 203 | GrTexture* setTexture(GrTexture* texture) { |
| 204 | fTexture.reset(SkSafeRef(texture)); |
| 205 | return texture; |
| 206 | } |
| 207 | private: |
| 208 | SkAutoTUnref<GrTexture> fTexture; |
| 209 | SkIPoint fOffset; |
| 210 | }; |
| 211 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | #endif |