1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "GrContext.h"
9 #include "GrCaps.h"
10 #include "GrGpu.h"
11 #include "GrResourceKey.h"
12 #include "GrRenderTarget.h"
13 #include "GrRenderTargetPriv.h"
14 #include "GrTexture.h"
15 #include "GrTexturePriv.h"
16 #include "GrTypes.h"
17 #include "SkMath.h"
18 #include "SkMipMap.h"
19 #include "SkTypes.h"
20 
dirtyMipMaps(bool mipMapsDirty)21 void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
22     if (mipMapsDirty) {
23         if (kValid_MipMapsStatus == fMipMapsStatus) {
24             fMipMapsStatus = kAllocated_MipMapsStatus;
25         }
26     } else {
27         const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
28         fMipMapsStatus = kValid_MipMapsStatus;
29         if (sizeChanged) {
30             // This must not be called until after changing fMipMapsStatus.
31             this->didChangeGpuMemorySize();
32             // TODO(http://skbug.com/4548) - The desc and scratch key should be
33             // updated to reflect the newly-allocated mipmaps.
34         }
35     }
36 }
37 
onGpuMemorySize() const38 size_t GrTexture::onGpuMemorySize() const {
39     size_t textureSize;
40 
41     if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
42         textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
43     } else {
44         textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
45     }
46 
47     if (this->texturePriv().hasMipMaps()) {
48         // We don't have to worry about the mipmaps being a different size than
49         // we'd expect because we never change fDesc.fWidth/fHeight.
50         textureSize += textureSize/3;
51     }
52 
53     SkASSERT(!SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
54     SkASSERT(textureSize <= WorstCaseSize(fDesc));
55 
56     return textureSize;
57 }
58 
validateDesc() const59 void GrTexture::validateDesc() const {
60     if (this->asRenderTarget()) {
61         // This texture has a render target
62         SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
63         SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples());
64     } else {
65         SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
66         SkASSERT(0 == fDesc.fSampleCnt);
67     }
68 }
69 
70 //////////////////////////////////////////////////////////////////////////////
71 
72 namespace {
73 
74 // FIXME:  This should be refactored with the code in gl/GrGLGpu.cpp.
resolve_origin(const GrSurfaceDesc & desc)75 GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
76     // By default, GrRenderTargets are GL's normal orientation so that they
77     // can be drawn to by the outside world without the client having
78     // to render upside down.
79     bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
80     if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
81         return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
82     } else {
83         return desc.fOrigin;
84     }
85 }
86 }
87 
88 //////////////////////////////////////////////////////////////////////////////
GrTexture(GrGpu * gpu,const GrSurfaceDesc & desc,GrSLType samplerType,bool wasMipMapDataProvided)89 GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
90                      bool wasMipMapDataProvided)
91     : INHERITED(gpu, desc)
92     , fSamplerType(samplerType)
93     // Gamma treatment is explicitly set after creation via GrTexturePriv
94     , fGammaTreatment(SkSourceGammaTreatment::kIgnore) {
95     if (wasMipMapDataProvided) {
96         fMipMapsStatus = kValid_MipMapsStatus;
97         fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight);
98     } else {
99         fMipMapsStatus = kNotAllocated_MipMapsStatus;
100         fMaxMipMapLevel = 0;
101     }
102 }
103 
computeScratchKey(GrScratchKey * key) const104 void GrTexture::computeScratchKey(GrScratchKey* key) const {
105     if (!GrPixelConfigIsCompressed(fDesc.fConfig)) {
106         GrTexturePriv::ComputeScratchKey(fDesc, key);
107     }
108 }
109 
ComputeScratchKey(const GrSurfaceDesc & desc,GrScratchKey * key)110 void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
111     static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
112 
113     GrSurfaceOrigin origin = resolve_origin(desc);
114     uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
115 
116     // make sure desc.fConfig fits in 5 bits
117     SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
118     SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5));
119     SkASSERT(desc.fSampleCnt < (1 << 8));
120     SkASSERT(flags < (1 << 10));
121     SkASSERT(static_cast<int>(origin) < (1 << 8));
122 
123     GrScratchKey::Builder builder(key, kType, 3);
124     builder[0] = desc.fWidth;
125     builder[1] = desc.fHeight;
126     builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14)
127                  | (origin << 24);
128 }
129