1 2 /* 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. 7 */ 8 9 10 #include "GrTexture.h" 11 12 #include "GrContext.h" 13 #include "GrDrawTargetCaps.h" 14 #include "GrGpu.h" 15 #include "GrRenderTarget.h" 16 #include "GrResourceCache.h" 17 18 GrTexture::~GrTexture() { 19 if (fRenderTarget.get()) { 20 fRenderTarget.get()->owningTextureDestroyed(); 21 } 22 } 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->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::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->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit); 35 nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis); 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 GrTextureImpl::dirtyMipMaps(bool mipMapsDirty) { 46 if (mipMapsDirty) { 47 if (kValid_MipMapsStatus == fMipMapsStatus) { 48 fMipMapsStatus = kAllocated_MipMapsStatus; 49 } 50 } else { 51 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; 52 fMipMapsStatus = kValid_MipMapsStatus; 53 if (sizeChanged) { 54 // This must not be called until after changing fMipMapsStatus. 55 this->didChangeGpuMemorySize(); 56 } 57 } 58 } 59 60 size_t GrTexture::gpuMemorySize() const { 61 size_t textureSize; 62 63 if (GrPixelConfigIsCompressed(fDesc.fConfig)) { 64 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight); 65 } else { 66 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig); 67 } 68 69 if (this->impl()->hasMipMaps()) { 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. 72 textureSize *= 2; 73 } 74 return textureSize; 75 } 76 77 bool GrTexture::readPixels(int left, int top, int width, int height, 78 GrPixelConfig config, void* buffer, 79 size_t rowBytes, uint32_t pixelOpsFlags) { 80 // go through context so that all necessary flushing occurs 81 GrContext* context = this->getContext(); 82 if (NULL == context) { 83 return false; 84 } 85 return context->readTexturePixels(this, 86 left, top, width, height, 87 config, buffer, rowBytes, 88 pixelOpsFlags); 89 } 90 91 void GrTexture::writePixels(int left, int top, int width, int height, 92 GrPixelConfig config, const void* buffer, 93 size_t rowBytes, uint32_t pixelOpsFlags) { 94 // go through context so that all necessary flushing occurs 95 GrContext* context = this->getContext(); 96 if (NULL == context) { 97 return; 98 } 99 context->writeTexturePixels(this, 100 left, top, width, height, 101 config, buffer, rowBytes, 102 pixelOpsFlags); 103 } 104 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 (getContext() == 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->impl()->isSetFlag((GrTextureFlags)GrTextureImpl::kReturnToCache_FlagBit)) { 111 SkASSERT(!this->wasDestroyed()); 112 this->ref(); // restores the ref the resource cache gave up when it marked this exclusive. 113 this->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit); 114 this->getContext()->addExistingTextureToCache(this); 115 } 116 } 117 118 void GrTexture::onRelease() { 119 this->abandonReleaseCommon(); 120 SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit)); 121 INHERITED::onRelease(); 122 } 123 124 void GrTexture::onAbandon() { 125 this->abandonReleaseCommon(); 126 if (fRenderTarget.get()) { 127 fRenderTarget->abandon(); 128 } 129 INHERITED::onAbandon(); 130 } 131 132 void GrTexture::validateDesc() const { 133 if (this->asRenderTarget()) { 134 // This texture has a render target 135 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); 136 137 if (this->asRenderTarget()->getStencilBuffer()) { 138 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); 139 } else { 140 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); 141 } 142 143 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples()); 144 } else { 145 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); 146 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); 147 SkASSERT(0 == fDesc.fSampleCnt); 148 } 149 } 150 151 ////////////////////////////////////////////////////////////////////////////// 152 153 // These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture 154 // key 155 enum TextureFlags { 156 /** 157 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the 158 * hardware doesn't support that feature. 159 */ 160 kStretchToPOT_TextureFlag = 0x1, 161 /** 162 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the 163 * stretched texture should be bilerped. 164 */ 165 kBilerp_TextureFlag = 0x2, 166 }; 167 168 namespace { 169 GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu, 170 const GrTextureParams* params, 171 const GrTextureDesc& desc) { 172 GrResourceKey::ResourceFlags flags = 0; 173 bool tiled = params && params->isTiled(); 174 if (tiled && !gpu->caps()->npotTextureTileSupport()) { 175 if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) { 176 flags |= kStretchToPOT_TextureFlag; 177 switch(params->filterMode()) { 178 case GrTextureParams::kNone_FilterMode: 179 break; 180 case GrTextureParams::kBilerp_FilterMode: 181 case GrTextureParams::kMipMap_FilterMode: 182 flags |= kBilerp_TextureFlag; 183 break; 184 } 185 } 186 } 187 return flags; 188 } 189 190 GrResourceKey::ResourceType texture_resource_type() { 191 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType(); 192 return gType; 193 } 194 195 // FIXME: This should be refactored with the code in gl/GrGpuGL.cpp. 196 GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) { 197 // By default, GrRenderTargets are GL's normal orientation so that they 198 // can be drawn to by the outside world without the client having 199 // to render upside down. 200 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); 201 if (kDefault_GrSurfaceOrigin == desc.fOrigin) { 202 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; 203 } else { 204 return desc.fOrigin; 205 } 206 } 207 } 208 209 ////////////////////////////////////////////////////////////////////////////// 210 GrTextureImpl::GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) 211 : INHERITED(gpu, isWrapped, desc) 212 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { 213 this->setScratchKey(ComputeScratchKey(desc)); 214 } 215 216 GrResourceKey GrTextureImpl::ComputeKey(const GrGpu* gpu, 217 const GrTextureParams* params, 218 const GrTextureDesc& desc, 219 const GrCacheID& cacheID) { 220 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc); 221 return GrResourceKey(cacheID, texture_resource_type(), flags); 222 } 223 224 GrResourceKey GrTextureImpl::ComputeScratchKey(const GrTextureDesc& desc) { 225 GrCacheID::Key idKey; 226 // Instead of a client-provided key of the texture contents we create a key from the 227 // descriptor. 228 GR_STATIC_ASSERT(sizeof(idKey) >= 16); 229 SkASSERT(desc.fHeight < (1 << 16)); 230 SkASSERT(desc.fWidth < (1 << 16)); 231 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); 232 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; 233 idKey.fData32[2] = desc.fFlags; 234 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually 235 static const int kPadSize = sizeof(idKey) - 16; 236 GR_STATIC_ASSERT(kPadSize >= 0); 237 memset(idKey.fData8 + 16, 0, kPadSize); 238 239 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey); 240 return GrResourceKey(cacheID, texture_resource_type(), 0); 241 } 242 243 bool GrTextureImpl::NeedsResizing(const GrResourceKey& key) { 244 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); 245 } 246 247 bool GrTextureImpl::NeedsBilerp(const GrResourceKey& key) { 248 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag); 249 } 250