| 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 "GrContext.h" | 10 #include "GrContext.h" | 
| 11 #include "GrDrawTargetCaps.h" | 11 #include "GrDrawTargetCaps.h" | 
| 12 #include "GrGpu.h" | 12 #include "GrGpu.h" | 
| 13 #include "GrRenderTarget.h" | 13 #include "GrRenderTarget.h" | 
| 14 #include "GrResourceCache.h" | 14 #include "GrResourceCache.h" | 
| 15 #include "GrTexture.h" | 15 #include "GrTexture.h" | 
| 16 #include "GrTexturePriv.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 /** |  | 
| 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. |  | 
| 27  */ |  | 
| 28 void GrTexture::internal_dispose() const { |  | 
| 29     if (this->texturePriv().isSetFlag((GrTextureFlags) GrTexture::kReturnToCache
     _FlagBit) && |  | 
| 30         this->INHERITED::getContext()) { |  | 
| 31         GrTexture* nonConstThis = const_cast<GrTexture *>(this); |  | 
| 32         this->ref(); // restore ref count to initial setting |  | 
| 33 |  | 
| 34         nonConstThis->texturePriv().resetFlag((GrTextureFlags) kReturnToCache_Fl
     agBit); |  | 
| 35         nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonCons
     tThis); |  | 
| 36 |  | 
| 37         // Note: "this" texture might be freed inside addExistingTextureToCache |  | 
| 38         // if it is purged. |  | 
| 39         return; |  | 
| 40     } |  | 
| 41 |  | 
| 42     this->INHERITED::internal_dispose(); |  | 
| 43 } |  | 
| 44 |  | 
| 45 void GrTexture::dirtyMipMaps(bool mipMapsDirty) { | 24 void GrTexture::dirtyMipMaps(bool mipMapsDirty) { | 
| 46     if (mipMapsDirty) { | 25     if (mipMapsDirty) { | 
| 47         if (kValid_MipMapsStatus == fMipMapsStatus) { | 26         if (kValid_MipMapsStatus == fMipMapsStatus) { | 
| 48            fMipMapsStatus = kAllocated_MipMapsStatus; | 27            fMipMapsStatus = kAllocated_MipMapsStatus; | 
| 49         } | 28         } | 
| 50     } else { | 29     } else { | 
| 51         const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; | 30         const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; | 
| 52         fMipMapsStatus = kValid_MipMapsStatus; | 31         fMipMapsStatus = kValid_MipMapsStatus; | 
| 53         if (sizeChanged) { | 32         if (sizeChanged) { | 
| 54             // This must not be called until after changing fMipMapsStatus. | 33             // This must not be called until after changing fMipMapsStatus. | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 95     GrContext* context = this->getContext(); | 74     GrContext* context = this->getContext(); | 
| 96     if (NULL == context) { | 75     if (NULL == context) { | 
| 97         return; | 76         return; | 
| 98     } | 77     } | 
| 99     context->writeTexturePixels(this, | 78     context->writeTexturePixels(this, | 
| 100                                 left, top, width, height, | 79                                 left, top, width, height, | 
| 101                                 config, buffer, rowBytes, | 80                                 config, buffer, rowBytes, | 
| 102                                 pixelOpsFlags); | 81                                 pixelOpsFlags); | 
| 103 } | 82 } | 
| 104 | 83 | 
| 105 void GrTexture::abandonReleaseCommon() { |  | 
| 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). |  | 
| 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. |  | 
| 110     if (this->texturePriv().isSetFlag((GrTextureFlags)kReturnToCache_FlagBit)) { |  | 
| 111         SkASSERT(!this->wasDestroyed()); |  | 
| 112         this->ref();  // restores the ref the resource cache gave up when it mar
     ked this exclusive. |  | 
| 113         this->texturePriv().resetFlag((GrTextureFlags) kReturnToCache_FlagBit); |  | 
| 114         this->getContext()->addExistingTextureToCache(this); |  | 
| 115     } |  | 
| 116 } |  | 
| 117 |  | 
| 118 void GrTexture::onRelease() { | 84 void GrTexture::onRelease() { | 
| 119     this->abandonReleaseCommon(); |  | 
| 120     SkASSERT(!this->texturePriv().isSetFlag((GrTextureFlags) kReturnToCache_Flag
     Bit)); | 85     SkASSERT(!this->texturePriv().isSetFlag((GrTextureFlags) kReturnToCache_Flag
     Bit)); | 
| 121     INHERITED::onRelease(); | 86     INHERITED::onRelease(); | 
| 122 } | 87 } | 
| 123 | 88 | 
| 124 void GrTexture::onAbandon() { | 89 void GrTexture::onAbandon() { | 
| 125     this->abandonReleaseCommon(); |  | 
| 126     if (fRenderTarget.get()) { | 90     if (fRenderTarget.get()) { | 
| 127         fRenderTarget->abandon(); | 91         fRenderTarget->abandon(); | 
| 128     } | 92     } | 
| 129     INHERITED::onAbandon(); | 93     INHERITED::onAbandon(); | 
| 130 } | 94 } | 
| 131 | 95 | 
| 132 void GrTexture::validateDesc() const { | 96 void GrTexture::validateDesc() const { | 
| 133     if (this->asRenderTarget()) { | 97     if (this->asRenderTarget()) { | 
| 134         // This texture has a render target | 98         // This texture has a render target | 
| 135         SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); | 99         SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); | 
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 180                 case GrTextureParams::kBilerp_FilterMode: | 144                 case GrTextureParams::kBilerp_FilterMode: | 
| 181                 case GrTextureParams::kMipMap_FilterMode: | 145                 case GrTextureParams::kMipMap_FilterMode: | 
| 182                     flags |= kBilerp_TextureFlag; | 146                     flags |= kBilerp_TextureFlag; | 
| 183                     break; | 147                     break; | 
| 184             } | 148             } | 
| 185         } | 149         } | 
| 186     } | 150     } | 
| 187     return flags; | 151     return flags; | 
| 188 } | 152 } | 
| 189 | 153 | 
| 190 GrResourceKey::ResourceType texture_resource_type() { |  | 
| 191     static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateReso
     urceType(); |  | 
| 192     return gType; |  | 
| 193 } |  | 
| 194 |  | 
| 195 // FIXME:  This should be refactored with the code in gl/GrGpuGL.cpp. | 154 // FIXME:  This should be refactored with the code in gl/GrGpuGL.cpp. | 
| 196 GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) { | 155 GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) { | 
| 197     // By default, GrRenderTargets are GL's normal orientation so that they | 156     // By default, GrRenderTargets are GL's normal orientation so that they | 
| 198     // can be drawn to by the outside world without the client having | 157     // can be drawn to by the outside world without the client having | 
| 199     // to render upside down. | 158     // to render upside down. | 
| 200     bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); | 159     bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); | 
| 201     if (kDefault_GrSurfaceOrigin == desc.fOrigin) { | 160     if (kDefault_GrSurfaceOrigin == desc.fOrigin) { | 
| 202         return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
     igin; | 161         return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
     igin; | 
| 203     } else { | 162     } else { | 
| 204         return desc.fOrigin; | 163         return desc.fOrigin; | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 215     // only make sense if alloc size is pow2 | 174     // only make sense if alloc size is pow2 | 
| 216     fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); | 175     fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); | 
| 217     fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); | 176     fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); | 
| 218 } | 177 } | 
| 219 | 178 | 
| 220 GrResourceKey GrTexturePriv::ComputeKey(const GrGpu* gpu, | 179 GrResourceKey GrTexturePriv::ComputeKey(const GrGpu* gpu, | 
| 221                                     const GrTextureParams* params, | 180                                     const GrTextureParams* params, | 
| 222                                     const GrTextureDesc& desc, | 181                                     const GrTextureDesc& desc, | 
| 223                                     const GrCacheID& cacheID) { | 182                                     const GrCacheID& cacheID) { | 
| 224     GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc); | 183     GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc); | 
| 225     return GrResourceKey(cacheID, texture_resource_type(), flags); | 184     return GrResourceKey(cacheID, ResourceType(), flags); | 
| 226 } | 185 } | 
| 227 | 186 | 
| 228 GrResourceKey GrTexturePriv::ComputeScratchKey(const GrTextureDesc& desc) { | 187 GrResourceKey GrTexturePriv::ComputeScratchKey(const GrTextureDesc& desc) { | 
| 229     GrCacheID::Key idKey; | 188     GrCacheID::Key idKey; | 
| 230     // Instead of a client-provided key of the texture contents we create a key 
     from the | 189     // Instead of a client-provided key of the texture contents we create a key 
     from the | 
| 231     // descriptor. | 190     // descriptor. | 
| 232     GR_STATIC_ASSERT(sizeof(idKey) >= 16); | 191     GR_STATIC_ASSERT(sizeof(idKey) >= 16); | 
| 233     SkASSERT(desc.fHeight < (1 << 16)); | 192     SkASSERT(desc.fHeight < (1 << 16)); | 
| 234     SkASSERT(desc.fWidth < (1 << 16)); | 193     SkASSERT(desc.fWidth < (1 << 16)); | 
| 235     idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); | 194     idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); | 
| 236     idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; | 195     idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; | 
| 237     idKey.fData32[2] = desc.fFlags; | 196     idKey.fData32[2] = desc.fFlags; | 
| 238     idKey.fData32[3] = resolve_origin(desc);    // Only needs 2 bits actually | 197     idKey.fData32[3] = resolve_origin(desc);    // Only needs 2 bits actually | 
| 239     static const int kPadSize = sizeof(idKey) - 16; | 198     static const int kPadSize = sizeof(idKey) - 16; | 
| 240     GR_STATIC_ASSERT(kPadSize >= 0); | 199     GR_STATIC_ASSERT(kPadSize >= 0); | 
| 241     memset(idKey.fData8 + 16, 0, kPadSize); | 200     memset(idKey.fData8 + 16, 0, kPadSize); | 
| 242 | 201 | 
| 243     GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey); | 202     GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey); | 
| 244     return GrResourceKey(cacheID, texture_resource_type(), 0); | 203     return GrResourceKey(cacheID, ResourceType(), 0); | 
| 245 } | 204 } | 
| 246 | 205 | 
| 247 bool GrTexturePriv::NeedsResizing(const GrResourceKey& key) { | 206 bool GrTexturePriv::NeedsResizing(const GrResourceKey& key) { | 
| 248     return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); | 207     return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); | 
| 249 } | 208 } | 
| 250 | 209 | 
| 251 bool GrTexturePriv::NeedsBilerp(const GrResourceKey& key) { | 210 bool GrTexturePriv::NeedsBilerp(const GrResourceKey& key) { | 
| 252     return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag); | 211     return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag); | 
| 253 } | 212 } | 
| OLD | NEW | 
|---|