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 #include "GrContext.h" | 9 #include "GrContext.h" |
10 #include "GrCaps.h" | 10 #include "GrCaps.h" |
11 #include "GrGpu.h" | 11 #include "GrGpu.h" |
12 #include "GrResourceKey.h" | 12 #include "GrResourceKey.h" |
13 #include "GrRenderTarget.h" | 13 #include "GrRenderTarget.h" |
14 #include "GrRenderTargetPriv.h" | 14 #include "GrRenderTargetPriv.h" |
15 #include "GrTexture.h" | 15 #include "GrTexture.h" |
16 #include "GrTexturePriv.h" | 16 #include "GrTexturePriv.h" |
| 17 #include "SkFloatingPoint.h" |
17 | 18 |
18 void GrTexture::dirtyMipMaps(bool mipMapsDirty) { | 19 void GrTexture::dirtyMipMaps(bool mipMapsDirty) { |
19 if (mipMapsDirty) { | 20 if (mipMapsDirty) { |
20 if (kValid_MipMapsStatus == fMipMapsStatus) { | 21 if (kValid_MipMapsStatus == fMipMapsStatus) { |
21 fMipMapsStatus = kAllocated_MipMapsStatus; | 22 fMipMapsStatus = kAllocated_MipMapsStatus; |
22 } | 23 } |
23 } else { | 24 } else { |
24 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; | 25 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; |
25 fMipMapsStatus = kValid_MipMapsStatus; | 26 fMipMapsStatus = kValid_MipMapsStatus; |
26 if (sizeChanged) { | 27 if (sizeChanged) { |
27 // This must not be called until after changing fMipMapsStatus. | 28 // This must not be called until after changing fMipMapsStatus. |
28 this->didChangeGpuMemorySize(); | 29 this->didChangeGpuMemorySize(); |
| 30 // TODO(http://skbug.com/4548) - The desc and scratch key should be |
| 31 // updated to reflect the newly-allocated mipmaps. |
29 } | 32 } |
30 } | 33 } |
31 } | 34 } |
32 | 35 |
33 size_t GrTexture::onGpuMemorySize() const { | 36 size_t GrTexture::onGpuMemorySize() const { |
34 size_t textureSize; | 37 size_t textureSize; |
35 | 38 |
36 if (GrPixelConfigIsCompressed(fDesc.fConfig)) { | 39 if (GrPixelConfigIsCompressed(fDesc.fConfig)) { |
37 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fD
esc.fHeight); | 40 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fD
esc.fHeight); |
38 } else { | 41 } else { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; | 79 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; |
77 } else { | 80 } else { |
78 return desc.fOrigin; | 81 return desc.fOrigin; |
79 } | 82 } |
80 } | 83 } |
81 } | 84 } |
82 | 85 |
83 ////////////////////////////////////////////////////////////////////////////// | 86 ////////////////////////////////////////////////////////////////////////////// |
84 GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc) | 87 GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc) |
85 : INHERITED(gpu, lifeCycle, desc) | 88 : INHERITED(gpu, lifeCycle, desc) |
86 , fMipMapsStatus(kNotAllocated_MipMapsStatus) { | 89 , fMipMapsStatus(kNotAllocated_MipMapsStatus) |
87 | 90 , fIsMaxMipMapLevelSpecified(false) |
| 91 , fMaxMipMapLevel(0) { |
88 if (!this->isExternal() && !GrPixelConfigIsCompressed(desc.fConfig)) { | 92 if (!this->isExternal() && !GrPixelConfigIsCompressed(desc.fConfig)) { |
89 GrScratchKey key; | 93 GrScratchKey key; |
90 GrTexturePriv::ComputeScratchKey(desc, &key); | 94 GrTexturePriv::ComputeScratchKey(desc, &key); |
| 95 this->setScratchKey(key); |
| 96 } |
| 97 // only make sense if alloc size is pow2 |
| 98 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); |
| 99 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); |
| 100 } |
| 101 |
| 102 GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc, |
| 103 bool wasMipMapDataProvided) |
| 104 : INHERITED(gpu, lifeCycle, desc) { |
| 105 if (!this->isExternal() && !GrPixelConfigIsCompressed(desc.fConfig)) { |
| 106 GrScratchKey key; |
| 107 GrTexturePriv::ComputeScratchKey(desc, &key); |
91 this->setScratchKey(key); | 108 this->setScratchKey(key); |
92 } | 109 } |
93 // only make sense if alloc size is pow2 | 110 // only make sense if alloc size is pow2 |
94 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); | 111 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); |
95 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); | 112 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); |
| 113 |
| 114 if (wasMipMapDataProvided) { |
| 115 fMipMapsStatus = kValid_MipMapsStatus; |
| 116 |
| 117 const uint32_t baseWidth = static_cast<uint32_t>(fDesc.fWidth); |
| 118 const uint32_t baseHeight = static_cast<uint32_t>(fDesc.fHeight); |
| 119 |
| 120 // OpenGL's spec requires that each mipmap level have height/width equal |
| 121 // to max(1, floor(original_height / 2^i) |
| 122 // (or original width) where i is the mipmap level. |
| 123 // Continue scaling down until both axes are size 1. |
| 124 const uint32_t largestAxis = SkTMax(baseWidth, baseHeight); |
| 125 const int leadingZeros = SkCLZ(largestAxis); |
| 126 // If the value 00011010 has 3 leading 0s then it has 5 significant bits |
| 127 // (the bits which are not leading zeros) |
| 128 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros; |
| 129 // This is making the assumption that the size of a byte is 8 bits |
| 130 // and that sizeof(uint32_t)'s implementation-defined behavior is 4. |
| 131 SkASSERT(significantBits >= 0); |
| 132 fMaxMipMapLevel = significantBits; |
| 133 fIsMaxMipMapLevelSpecified = true; |
| 134 } else { |
| 135 fMipMapsStatus = kNotAllocated_MipMapsStatus; |
| 136 fMaxMipMapLevel = 0; |
| 137 fIsMaxMipMapLevelSpecified = false; |
| 138 } |
96 } | 139 } |
97 | 140 |
98 void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* k
ey) { | 141 void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* k
ey) { |
99 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResour
ceType(); | 142 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResour
ceType(); |
100 | 143 |
101 GrSurfaceOrigin origin = resolve_origin(desc); | 144 GrSurfaceOrigin origin = resolve_origin(desc); |
102 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag; | 145 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag; |
103 | 146 |
104 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 6)); | 147 // make sure desc.fConfig fits in 5 bits |
| 148 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5); |
| 149 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5)); |
105 SkASSERT(desc.fSampleCnt < (1 << 8)); | 150 SkASSERT(desc.fSampleCnt < (1 << 8)); |
106 SkASSERT(flags < (1 << 10)); | 151 SkASSERT(flags < (1 << 10)); |
107 SkASSERT(static_cast<int>(origin) < (1 << 8)); | 152 SkASSERT(static_cast<int>(origin) < (1 << 8)); |
108 | 153 |
109 GrScratchKey::Builder builder(key, kType, 3); | 154 GrScratchKey::Builder builder(key, kType, 3); |
110 builder[0] = desc.fWidth; | 155 builder[0] = desc.fWidth; |
111 builder[1] = desc.fHeight; | 156 builder[1] = desc.fHeight; |
112 builder[2] = desc.fConfig | (desc.fSampleCnt << 6) | (flags << 14) | (origin
<< 24); | 157 builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6
) | (flags << 14) |
| 158 | (origin << 24); |
113 } | 159 } |
OLD | NEW |