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 #include "GrContext.h" 10 #include "GrCaps.h" 11 #include "GrGpu.h" 12 #include "GrResourceKey.h" 13 #include "GrRenderTarget.h" 14 #include "GrRenderTargetPriv.h" 15 #include "GrTexture.h" 16 #include "GrTexturePriv.h" 17 18 void GrTexture::dirtyMipMaps(bool mipMapsDirty) { 19 if (mipMapsDirty) { 20 if (kValid_MipMapsStatus == fMipMapsStatus) { 21 fMipMapsStatus = kAllocated_MipMapsStatus; 22 } 23 } else { 24 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; 25 fMipMapsStatus = kValid_MipMapsStatus; 26 if (sizeChanged) { 27 // This must not be called until after changing fMipMapsStatus. 28 this->didChangeGpuMemorySize(); 29 } 30 } 31 } 32 33 size_t GrTexture::onGpuMemorySize() const { 34 size_t textureSize; 35 36 if (GrPixelConfigIsCompressed(fDesc.fConfig)) { 37 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight); 38 } else { 39 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig); 40 } 41 42 if (this->texturePriv().hasMipMaps()) { 43 // We don't have to worry about the mipmaps being a different size than 44 // we'd expect because we never change fDesc.fWidth/fHeight. 45 textureSize += textureSize/3; 46 } 47 48 SkASSERT(!SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); 49 SkASSERT(textureSize <= WorseCaseSize(fDesc)); 50 51 return textureSize; 52 } 53 54 void GrTexture::validateDesc() const { 55 if (this->asRenderTarget()) { 56 // This texture has a render target 57 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); 58 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples()); 59 } else { 60 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); 61 SkASSERT(0 == fDesc.fSampleCnt); 62 } 63 } 64 65 ////////////////////////////////////////////////////////////////////////////// 66 67 namespace { 68 69 // FIXME: This should be refactored with the code in gl/GrGLGpu.cpp. 70 GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) { 71 // By default, GrRenderTargets are GL's normal orientation so that they 72 // can be drawn to by the outside world without the client having 73 // to render upside down. 74 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag); 75 if (kDefault_GrSurfaceOrigin == desc.fOrigin) { 76 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; 77 } else { 78 return desc.fOrigin; 79 } 80 } 81 } 82 83 ////////////////////////////////////////////////////////////////////////////// 84 GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc) 85 : INHERITED(gpu, lifeCycle, desc) 86 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { 87 88 if (!this->isExternal() && !GrPixelConfigIsCompressed(desc.fConfig) && 89 !desc.fTextureStorageAllocator.fAllocateTextureStorage) { 90 GrScratchKey key; 91 GrTexturePriv::ComputeScratchKey(desc, &key); 92 this->setScratchKey(key); 93 } 94 } 95 96 void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) { 97 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); 98 99 GrSurfaceOrigin origin = resolve_origin(desc); 100 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag; 101 102 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 6)); 103 SkASSERT(desc.fSampleCnt < (1 << 8)); 104 SkASSERT(flags < (1 << 10)); 105 SkASSERT(static_cast<int>(origin) < (1 << 8)); 106 107 GrScratchKey::Builder builder(key, kType, 3); 108 builder[0] = desc.fWidth; 109 builder[1] = desc.fHeight; 110 builder[2] = desc.fConfig | (desc.fSampleCnt << 6) | (flags << 14) | (origin << 24); 111 } 112