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 return GrSurface::ComputeSize(fDesc, 1, this->texturePriv().hasMipMaps());
40 }
41
validateDesc() const42 void GrTexture::validateDesc() const {
43 if (this->asRenderTarget()) {
44 // This texture has a render target
45 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
46 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples());
47 } else {
48 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
49 SkASSERT(0 == fDesc.fSampleCnt);
50 }
51 }
52
53 //////////////////////////////////////////////////////////////////////////////
54
55 namespace {
56
57 // FIXME: This should be refactored with the code in gl/GrGLGpu.cpp.
resolve_origin(const GrSurfaceDesc & desc)58 GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
59 // By default, GrRenderTargets are GL's normal orientation so that they
60 // can be drawn to by the outside world without the client having
61 // to render upside down.
62 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
63 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
64 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
65 } else {
66 return desc.fOrigin;
67 }
68 }
69 }
70
71 //////////////////////////////////////////////////////////////////////////////
GrTexture(GrGpu * gpu,const GrSurfaceDesc & desc,GrSLType samplerType,GrSamplerParams::FilterMode highestFilterMode,bool wasMipMapDataProvided)72 GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
73 GrSamplerParams::FilterMode highestFilterMode, bool wasMipMapDataProvided)
74 : INHERITED(gpu, desc)
75 , fSamplerType(samplerType)
76 , fHighestFilterMode(highestFilterMode)
77 // Mip color mode is explicitly set after creation via GrTexturePriv
78 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) {
79 if (wasMipMapDataProvided) {
80 fMipMapsStatus = kValid_MipMapsStatus;
81 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight);
82 } else {
83 fMipMapsStatus = kNotAllocated_MipMapsStatus;
84 fMaxMipMapLevel = 0;
85 }
86 }
87
computeScratchKey(GrScratchKey * key) const88 void GrTexture::computeScratchKey(GrScratchKey* key) const {
89 if (!GrPixelConfigIsCompressed(fDesc.fConfig)) {
90 GrTexturePriv::ComputeScratchKey(fDesc, key);
91 }
92 }
93
ComputeScratchKey(const GrSurfaceDesc & desc,GrScratchKey * key)94 void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
95 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
96
97 GrSurfaceOrigin origin = resolve_origin(desc);
98 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
99
100 // make sure desc.fConfig fits in 5 bits
101 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
102 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5));
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.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14)
111 | (origin << 24);
112 }
113