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; |
18 | 19 |
19 class GrTexture : public GrSurface { | 20 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 | |
53 /** | 22 /** |
54 * Approximate number of bytes used by the texture | 23 * Approximate number of bytes used by the texture |
55 */ | 24 */ |
56 virtual size_t gpuMemorySize() const SK_OVERRIDE; | 25 virtual size_t gpuMemorySize() const SK_OVERRIDE; |
57 | 26 |
58 // GrSurface overrides | 27 // GrSurface overrides |
59 virtual bool readPixels(int left, int top, int width, int height, | 28 virtual bool readPixels(int left, int top, int width, int height, |
60 GrPixelConfig config, | 29 GrPixelConfig config, |
61 void* buffer, | 30 void* buffer, |
62 size_t rowBytes = 0, | 31 size_t rowBytes = 0, |
63 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; | 32 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
64 | 33 |
65 virtual void writePixels(int left, int top, int width, int height, | 34 virtual void writePixels(int left, int top, int width, int height, |
66 GrPixelConfig config, | 35 GrPixelConfig config, |
67 const void* buffer, | 36 const void* buffer, |
68 size_t rowBytes = 0, | 37 size_t rowBytes = 0, |
69 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; | 38 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
70 | 39 |
71 /** | |
72 * @return this texture | |
73 */ | |
74 virtual GrTexture* asTexture() SK_OVERRIDE { return this; } | 40 virtual GrTexture* asTexture() SK_OVERRIDE { return this; } |
75 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } | 41 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(); } |
76 | 44 |
77 /** | 45 /** |
78 * Retrieves the render target underlying this texture that can be passed to | 46 * Convert from texels to normalized texture coords for POT textures only. P
lease don't add |
79 * GrGpu::setRenderTarget(). | 47 * new callsites for these functions. They are slated for removal. |
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. | |
95 */ | 48 */ |
96 SkFixed normalizeFixedX(SkFixed x) const { | 49 SkFixed normalizeFixedX(SkFixed x) const { |
97 SkASSERT(GrIsPow2(fDesc.fWidth)); | 50 SkASSERT(GrIsPow2(fDesc.fWidth)); |
98 return x >> fShiftFixedX; | 51 return x >> fShiftFixedX; |
99 } | 52 } |
100 SkFixed normalizeFixedY(SkFixed y) const { | 53 SkFixed normalizeFixedY(SkFixed y) const { |
101 SkASSERT(GrIsPow2(fDesc.fHeight)); | 54 SkASSERT(GrIsPow2(fDesc.fHeight)); |
102 return y >> fShiftFixedY; | 55 return y >> fShiftFixedY; |
103 } | 56 } |
104 | 57 |
105 /** | 58 /** |
106 * Return the native ID or handle to the texture, depending on the | 59 * Return the native ID or handle to the texture, depending on the |
107 * platform. e.g. on OpenGL, return the texture ID. | 60 * platform. e.g. on OpenGL, return the texture ID. |
108 */ | 61 */ |
109 virtual GrBackendObject getTextureHandle() const = 0; | 62 virtual GrBackendObject getTextureHandle() const = 0; |
110 | 63 |
111 /** | 64 /** |
112 * Call this when the state of the native API texture object is | 65 * This function indicates that the texture parameters (wrap mode, filtering
, ...) have been |
113 * altered directly, without being tracked by skia. | 66 * changed externally to Skia. |
114 */ | 67 */ |
115 virtual void invalidateCachedState() = 0; | 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 GrTextureI
mpl 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 } |
116 | 88 |
117 #ifdef SK_DEBUG | 89 #ifdef SK_DEBUG |
118 void validate() const { | 90 void validate() const { |
119 this->INHERITED::validate(); | 91 this->INHERITED::validate(); |
120 | 92 |
121 this->validateDesc(); | 93 this->validateDesc(); |
122 } | 94 } |
123 #endif | 95 #endif |
124 | 96 |
125 static GrResourceKey ComputeKey(const GrGpu* gpu, | 97 GrTextureImpl* impl() { return reinterpret_cast<GrTextureImpl*>(this); } |
126 const GrTextureParams* params, | 98 const GrTextureImpl* impl() const { return reinterpret_cast<const GrTextureI
mpl*>(this); } |
127 const GrTextureDesc& desc, | |
128 const GrCacheID& cacheID); | |
129 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc); | |
130 static bool NeedsResizing(const GrResourceKey& key); | |
131 static bool NeedsBilerp(const GrResourceKey& key); | |
132 | 99 |
133 protected: | 100 protected: |
134 // A texture refs its rt representation but not vice-versa. It is up to | 101 // A texture refs its rt representation but not vice-versa. It is up to |
135 // the subclass constructor to initialize this pointer. | 102 // the subclass constructor to initialize this pointer. |
136 SkAutoTUnref<GrRenderTarget> fRenderTarget; | 103 SkAutoTUnref<GrRenderTarget> fRenderTarget; |
137 | 104 |
138 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) | 105 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) |
139 : INHERITED(gpu, isWrapped, desc) | 106 : INHERITED(gpu, isWrapped, desc) |
140 , fRenderTarget(NULL) | 107 , fRenderTarget(NULL) { |
141 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { | |
142 | |
143 // only make sense if alloc size is pow2 | 108 // only make sense if alloc size is pow2 |
144 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); | 109 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); |
145 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); | 110 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); |
146 } | 111 } |
| 112 |
147 virtual ~GrTexture(); | 113 virtual ~GrTexture(); |
148 | 114 |
149 // GrResource overrides | 115 // GrResource overrides |
150 virtual void onRelease() SK_OVERRIDE; | 116 virtual void onRelease() SK_OVERRIDE; |
151 virtual void onAbandon() SK_OVERRIDE; | 117 virtual void onAbandon() SK_OVERRIDE; |
152 | 118 |
153 void validateDesc() const; | 119 void validateDesc() const; |
154 | 120 |
155 private: | 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 |
| 156 static GrResourceKey ComputeKey(const GrGpu* gpu, |
| 157 const GrTextureParams* params, |
| 158 const GrTextureDesc& desc, |
| 159 const GrCacheID& cacheID); |
| 160 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc); |
| 161 static bool NeedsResizing(const GrResourceKey& key); |
| 162 static bool NeedsBilerp(const GrResourceKey& key); |
| 163 |
| 164 protected: |
| 165 GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) |
| 166 : INHERITED(gpu, isWrapped, desc) |
| 167 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { |
| 168 } |
| 169 |
| 170 private: |
156 enum MipMapsStatus { | 171 enum MipMapsStatus { |
157 kNotAllocated_MipMapsStatus, | 172 kNotAllocated_MipMapsStatus, |
158 kAllocated_MipMapsStatus, | 173 kAllocated_MipMapsStatus, |
159 kValid_MipMapsStatus | 174 kValid_MipMapsStatus |
160 }; | 175 }; |
161 | 176 |
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 | |
167 MipMapsStatus fMipMapsStatus; | 177 MipMapsStatus fMipMapsStatus; |
168 | 178 |
169 virtual void internal_dispose() const SK_OVERRIDE; | 179 typedef GrTexture INHERITED; |
170 | |
171 typedef GrSurface INHERITED; | |
172 }; | 180 }; |
173 | 181 |
174 /** | 182 /** |
175 * Represents a texture that is intended to be accessed in device coords with an
offset. | 183 * Represents a texture that is intended to be accessed in device coords with an
offset. |
176 */ | 184 */ |
177 class GrDeviceCoordTexture { | 185 class GrDeviceCoordTexture { |
178 public: | 186 public: |
179 GrDeviceCoordTexture() { fOffset.set(0, 0); } | 187 GrDeviceCoordTexture() { fOffset.set(0, 0); } |
180 | 188 |
181 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) { | 189 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) { |
(...skipping 15 matching lines...) Expand all Loading... |
197 | 205 |
198 void setOffset(const SkIPoint& offset) { fOffset = offset; } | 206 void setOffset(const SkIPoint& offset) { fOffset = offset; } |
199 void setOffset(int ox, int oy) { fOffset.set(ox, oy); } | 207 void setOffset(int ox, int oy) { fOffset.set(ox, oy); } |
200 | 208 |
201 GrTexture* texture() const { return fTexture.get(); } | 209 GrTexture* texture() const { return fTexture.get(); } |
202 | 210 |
203 GrTexture* setTexture(GrTexture* texture) { | 211 GrTexture* setTexture(GrTexture* texture) { |
204 fTexture.reset(SkSafeRef(texture)); | 212 fTexture.reset(SkSafeRef(texture)); |
205 return texture; | 213 return texture; |
206 } | 214 } |
| 215 |
207 private: | 216 private: |
208 SkAutoTUnref<GrTexture> fTexture; | 217 SkAutoTUnref<GrTexture> fTexture; |
209 SkIPoint fOffset; | 218 SkIPoint fOffset; |
210 }; | 219 }; |
211 | 220 |
212 #endif | 221 #endif |
OLD | NEW |