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