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 | 9 |
10 #include "GrTexture.h" | |
11 | |
12 #include "GrContext.h" | 10 #include "GrContext.h" |
13 #include "GrDrawTargetCaps.h" | 11 #include "GrDrawTargetCaps.h" |
14 #include "GrGpu.h" | 12 #include "GrGpu.h" |
15 #include "GrRenderTarget.h" | 13 #include "GrRenderTarget.h" |
16 #include "GrResourceCache.h" | 14 #include "GrResourceCache.h" |
| 15 #include "GrTexture.h" |
| 16 #include "GrTexturePriv.h" |
17 | 17 |
18 GrTexture::~GrTexture() { | 18 GrTexture::~GrTexture() { |
19 if (fRenderTarget.get()) { | 19 if (fRenderTarget.get()) { |
20 fRenderTarget.get()->owningTextureDestroyed(); | 20 fRenderTarget.get()->owningTextureDestroyed(); |
21 } | 21 } |
22 } | 22 } |
23 | 23 |
24 /** | 24 /** |
25 * This method allows us to interrupt the normal deletion process and place | 25 * This method allows us to interrupt the normal deletion process and place |
26 * textures back in the texture cache when their ref count goes to zero. | 26 * textures back in the texture cache when their ref count goes to zero. |
27 */ | 27 */ |
28 void GrTexture::internal_dispose() const { | 28 void GrTexture::internal_dispose() const { |
29 if (this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_F
lagBit) && | 29 if (this->texturePriv().isSetFlag((GrTextureFlags) GrTexture::kReturnToCache
_FlagBit) && |
30 this->INHERITED::getContext()) { | 30 this->INHERITED::getContext()) { |
31 GrTexture* nonConstThis = const_cast<GrTexture *>(this); | 31 GrTexture* nonConstThis = const_cast<GrTexture *>(this); |
32 this->ref(); // restore ref count to initial setting | 32 this->ref(); // restore ref count to initial setting |
33 | 33 |
34 nonConstThis->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnT
oCache_FlagBit); | 34 nonConstThis->texturePriv().resetFlag((GrTextureFlags) kReturnToCache_Fl
agBit); |
35 nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonCons
tThis); | 35 nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonCons
tThis); |
36 | 36 |
37 // Note: "this" texture might be freed inside addExistingTextureToCache | 37 // Note: "this" texture might be freed inside addExistingTextureToCache |
38 // if it is purged. | 38 // if it is purged. |
39 return; | 39 return; |
40 } | 40 } |
41 | 41 |
42 this->INHERITED::internal_dispose(); | 42 this->INHERITED::internal_dispose(); |
43 } | 43 } |
44 | 44 |
45 void GrTextureImpl::dirtyMipMaps(bool mipMapsDirty) { | 45 void GrTexture::dirtyMipMaps(bool mipMapsDirty) { |
46 if (mipMapsDirty) { | 46 if (mipMapsDirty) { |
47 if (kValid_MipMapsStatus == fMipMapsStatus) { | 47 if (kValid_MipMapsStatus == fMipMapsStatus) { |
48 fMipMapsStatus = kAllocated_MipMapsStatus; | 48 fMipMapsStatus = kAllocated_MipMapsStatus; |
49 } | 49 } |
50 } else { | 50 } else { |
51 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; | 51 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; |
52 fMipMapsStatus = kValid_MipMapsStatus; | 52 fMipMapsStatus = kValid_MipMapsStatus; |
53 if (sizeChanged) { | 53 if (sizeChanged) { |
54 // This must not be called until after changing fMipMapsStatus. | 54 // This must not be called until after changing fMipMapsStatus. |
55 this->didChangeGpuMemorySize(); | 55 this->didChangeGpuMemorySize(); |
56 } | 56 } |
57 } | 57 } |
58 } | 58 } |
59 | 59 |
60 size_t GrTexture::gpuMemorySize() const { | 60 size_t GrTexture::gpuMemorySize() const { |
61 size_t textureSize; | 61 size_t textureSize; |
62 | 62 |
63 if (GrPixelConfigIsCompressed(fDesc.fConfig)) { | 63 if (GrPixelConfigIsCompressed(fDesc.fConfig)) { |
64 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fD
esc.fHeight); | 64 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fD
esc.fHeight); |
65 } else { | 65 } else { |
66 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fD
esc.fConfig); | 66 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fD
esc.fConfig); |
67 } | 67 } |
68 | 68 |
69 if (this->impl()->hasMipMaps()) { | 69 if (this->texturePriv().hasMipMaps()) { |
70 // We don't have to worry about the mipmaps being a different size than | 70 // We don't have to worry about the mipmaps being a different size than |
71 // we'd expect because we never change fDesc.fWidth/fHeight. | 71 // we'd expect because we never change fDesc.fWidth/fHeight. |
72 textureSize *= 2; | 72 textureSize *= 2; |
73 } | 73 } |
74 return textureSize; | 74 return textureSize; |
75 } | 75 } |
76 | 76 |
77 bool GrTexture::readPixels(int left, int top, int width, int height, | 77 bool GrTexture::readPixels(int left, int top, int width, int height, |
78 GrPixelConfig config, void* buffer, | 78 GrPixelConfig config, void* buffer, |
79 size_t rowBytes, uint32_t pixelOpsFlags) { | 79 size_t rowBytes, uint32_t pixelOpsFlags) { |
(...skipping 20 matching lines...) Expand all Loading... |
100 left, top, width, height, | 100 left, top, width, height, |
101 config, buffer, rowBytes, | 101 config, buffer, rowBytes, |
102 pixelOpsFlags); | 102 pixelOpsFlags); |
103 } | 103 } |
104 | 104 |
105 void GrTexture::abandonReleaseCommon() { | 105 void GrTexture::abandonReleaseCommon() { |
106 // In debug builds the resource cache tracks removed/exclusive textures and
has an unref'ed ptr. | 106 // In debug builds the resource cache tracks removed/exclusive textures and
has an unref'ed ptr. |
107 // After abandon() or release() the resource cache will be unreachable (getC
ontext() == NULL). | 107 // After abandon() or release() the resource cache will be unreachable (getC
ontext() == NULL). |
108 // So we readd the texture to the cache here so that it is removed from the
exclusive list and | 108 // So we readd the texture to the cache here so that it is removed from the
exclusive list and |
109 // there is no longer an unref'ed ptr to the texture in the cache. | 109 // there is no longer an unref'ed ptr to the texture in the cache. |
110 if (this->impl()->isSetFlag((GrTextureFlags)GrTextureImpl::kReturnToCache_Fl
agBit)) { | 110 if (this->texturePriv().isSetFlag((GrTextureFlags)kReturnToCache_FlagBit)) { |
111 SkASSERT(!this->wasDestroyed()); | 111 SkASSERT(!this->wasDestroyed()); |
112 this->ref(); // restores the ref the resource cache gave up when it mar
ked this exclusive. | 112 this->ref(); // restores the ref the resource cache gave up when it mar
ked this exclusive. |
113 this->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_F
lagBit); | 113 this->texturePriv().resetFlag((GrTextureFlags) kReturnToCache_FlagBit); |
114 this->getContext()->addExistingTextureToCache(this); | 114 this->getContext()->addExistingTextureToCache(this); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 void GrTexture::onRelease() { | 118 void GrTexture::onRelease() { |
119 this->abandonReleaseCommon(); | 119 this->abandonReleaseCommon(); |
120 SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToC
ache_FlagBit)); | 120 SkASSERT(!this->texturePriv().isSetFlag((GrTextureFlags) kReturnToCache_Flag
Bit)); |
121 INHERITED::onRelease(); | 121 INHERITED::onRelease(); |
122 } | 122 } |
123 | 123 |
124 void GrTexture::onAbandon() { | 124 void GrTexture::onAbandon() { |
125 this->abandonReleaseCommon(); | 125 this->abandonReleaseCommon(); |
126 if (fRenderTarget.get()) { | 126 if (fRenderTarget.get()) { |
127 fRenderTarget->abandon(); | 127 fRenderTarget->abandon(); |
128 } | 128 } |
129 INHERITED::onAbandon(); | 129 INHERITED::onAbandon(); |
130 } | 130 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); | 200 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); |
201 if (kDefault_GrSurfaceOrigin == desc.fOrigin) { | 201 if (kDefault_GrSurfaceOrigin == desc.fOrigin) { |
202 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; | 202 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; |
203 } else { | 203 } else { |
204 return desc.fOrigin; | 204 return desc.fOrigin; |
205 } | 205 } |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 ////////////////////////////////////////////////////////////////////////////// | 209 ////////////////////////////////////////////////////////////////////////////// |
210 GrTextureImpl::GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& de
sc) | 210 GrTexture::GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) |
211 : INHERITED(gpu, isWrapped, desc) | 211 : INHERITED(gpu, isWrapped, desc) |
| 212 , fRenderTarget(NULL) |
212 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { | 213 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { |
213 this->setScratchKey(ComputeScratchKey(desc)); | 214 this->setScratchKey(GrTexturePriv::ComputeScratchKey(desc)); |
| 215 // only make sense if alloc size is pow2 |
| 216 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); |
| 217 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); |
214 } | 218 } |
215 | 219 |
216 GrResourceKey GrTextureImpl::ComputeKey(const GrGpu* gpu, | 220 GrResourceKey GrTexturePriv::ComputeKey(const GrGpu* gpu, |
217 const GrTextureParams* params, | 221 const GrTextureParams* params, |
218 const GrTextureDesc& desc, | 222 const GrTextureDesc& desc, |
219 const GrCacheID& cacheID) { | 223 const GrCacheID& cacheID) { |
220 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc); | 224 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc); |
221 return GrResourceKey(cacheID, texture_resource_type(), flags); | 225 return GrResourceKey(cacheID, texture_resource_type(), flags); |
222 } | 226 } |
223 | 227 |
224 GrResourceKey GrTextureImpl::ComputeScratchKey(const GrTextureDesc& desc) { | 228 GrResourceKey GrTexturePriv::ComputeScratchKey(const GrTextureDesc& desc) { |
225 GrCacheID::Key idKey; | 229 GrCacheID::Key idKey; |
226 // Instead of a client-provided key of the texture contents we create a key
from the | 230 // Instead of a client-provided key of the texture contents we create a key
from the |
227 // descriptor. | 231 // descriptor. |
228 GR_STATIC_ASSERT(sizeof(idKey) >= 16); | 232 GR_STATIC_ASSERT(sizeof(idKey) >= 16); |
229 SkASSERT(desc.fHeight < (1 << 16)); | 233 SkASSERT(desc.fHeight < (1 << 16)); |
230 SkASSERT(desc.fWidth < (1 << 16)); | 234 SkASSERT(desc.fWidth < (1 << 16)); |
231 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); | 235 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); |
232 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; | 236 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; |
233 idKey.fData32[2] = desc.fFlags; | 237 idKey.fData32[2] = desc.fFlags; |
234 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually | 238 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually |
235 static const int kPadSize = sizeof(idKey) - 16; | 239 static const int kPadSize = sizeof(idKey) - 16; |
236 GR_STATIC_ASSERT(kPadSize >= 0); | 240 GR_STATIC_ASSERT(kPadSize >= 0); |
237 memset(idKey.fData8 + 16, 0, kPadSize); | 241 memset(idKey.fData8 + 16, 0, kPadSize); |
238 | 242 |
239 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey); | 243 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey); |
240 return GrResourceKey(cacheID, texture_resource_type(), 0); | 244 return GrResourceKey(cacheID, texture_resource_type(), 0); |
241 } | 245 } |
242 | 246 |
243 bool GrTextureImpl::NeedsResizing(const GrResourceKey& key) { | 247 bool GrTexturePriv::NeedsResizing(const GrResourceKey& key) { |
244 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); | 248 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); |
245 } | 249 } |
246 | 250 |
247 bool GrTextureImpl::NeedsBilerp(const GrResourceKey& key) { | 251 bool GrTexturePriv::NeedsBilerp(const GrResourceKey& key) { |
248 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag); | 252 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag); |
249 } | 253 } |
OLD | NEW |