Debian Codegraph @

src/core/SkMipmap.cpp

raw
1 /*
2 * Copyright 2013 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 #include "src/core/SkMipmap.h"
8
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/base/SkTo.h"
14 #include "src/base/SkMathPriv.h"
15 #include "src/core/SkImageInfoPriv.h"
16 #include "src/core/SkMipmapBuilder.h"
17
18 #include <new>
19
20 //
21 // ColorTypeFilter is the "Type" we pass to some downsample template functions.
22 // It controls how we expand a pixel into a large type, with space between each component,
23 // so we can then perform our simple filter (either box or triangle) and store the intermediates
24 // in the expanded type.
25 //
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////
28
29 SkMipmap::SkMipmap(void* malloc, size_t size) : SkCachedData(malloc, size) {}
30 SkMipmap::SkMipmap(size_t size, SkDiscardableMemory* dm) : SkCachedData(size, dm) {}
31
32 SkMipmap::~SkMipmap() = default;
33
34 size_t SkMipmap::AllocLevelsSize(int levelCount, size_t pixelSize) {
35 if (levelCount < 0) {
36 return 0;
37 }
38 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize;
39 if (!SkTFitsIn<int32_t>(size)) {
40 return 0;
41 }
42 return SkTo<int32_t>(size);
43 }
44
45 SkMipmap* SkMipmap::Build(const SkPixmap& src, SkDiscardableFactoryProc fact,
46 bool computeContents) {
47 if (src.width() <= 1 && src.height() <= 1) {
48 return nullptr;
49 }
50
51 const SkColorType ct = src.colorType();
52 const SkAlphaType at = src.alphaType();
53
54 // whip through our loop to compute the exact size needed
55 size_t size = 0;
56 int countLevels = ComputeLevelCount(src.width(), src.height());
57 for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) {
58 SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel);
59 size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight;
60 }
61
62 size_t storageSize = SkMipmap::AllocLevelsSize(countLevels, size);
63 if (0 == storageSize) {
64 return nullptr;
65 }
66
67 SkMipmap* mipmap;
68 if (fact) {
69 SkDiscardableMemory* dm = fact(storageSize);
70 if (nullptr == dm) {
71 return nullptr;
72 }
73 mipmap = new SkMipmap(storageSize, dm);
74 } else {
75 void* tmp = sk_malloc_canfail(storageSize);
76 if (!tmp) {
77 return nullptr;
78 }
79
80 mipmap = new SkMipmap(tmp, storageSize);
81 }
82
83 // init
84 mipmap->fCS = sk_ref_sp(src.info().colorSpace());
85 mipmap->fCount = countLevels;
86 mipmap->fLevels = (Level*)mipmap->writable_data();
87 SkASSERT(mipmap->fLevels);
88
89 Level* levels = mipmap->fLevels;
90 uint8_t* baseAddr = (uint8_t*)&levels[countLevels];
91 uint8_t* addr = baseAddr;
92 int width = src.width();
93 int height = src.height();
94 uint32_t rowBytes;
95 SkPixmap srcPM(src);
96
97 // Depending on architecture and other factors, the pixel data alignment may need to be as
98 // large as 8 (for F16 pixels). See the comment on SkMipmap::Level.
99 SkASSERT(SkIsAlign8((uintptr_t)addr));
100
101 std::unique_ptr<SkMipmapDownSampler> downsampler;
102 if (computeContents) {
103 downsampler = MakeDownSampler(src);
104 if (!downsampler) {
105 delete mipmap;
106 return nullptr;
107 }
108 }
109
110 for (int i = 0; i < countLevels; ++i) {
111 width = std::max(1, width >> 1);
112 height = std::max(1, height >> 1);
113 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
114
115 // We make the Info w/o any colorspace, since that storage is not under our control, and
116 // will not be deleted in a controlled fashion. When the caller is given the pixmap for
117 // a given level, we augment this pixmap with fCS (which we do manage).
118 new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
119 levels[i].fScale = SkSize::Make(SkIntToScalar(width) / src.width(),
120 SkIntToScalar(height) / src.height());
121
122 const SkPixmap& dstPM = levels[i].fPixmap;
123 if (downsampler) {
124 downsampler->buildLevel(dstPM, srcPM);
125 }
126 srcPM = dstPM;
127 addr += height * rowBytes;
128 }
129 SkASSERT(addr == baseAddr + size);
130
131 SkASSERT(mipmap->fLevels);
132 return mipmap;
133 }
134
135 int SkMipmap::ComputeLevelCount(int baseWidth, int baseHeight) {
136 if (baseWidth < 1 || baseHeight < 1) {
137 return 0;
138 }
139
140 // OpenGL's spec requires that each mipmap level have height/width equal to
141 // max(1, floor(original_height / 2^i)
142 // (or original_width) where i is the mipmap level.
143 // Continue scaling down until both axes are size 1.
144
145 const int largestAxis = std::max(baseWidth, baseHeight);
146 if (largestAxis < 2) {
147 // SkMipmap::Build requires a minimum size of 2.
148 return 0;
149 }
150 const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis));
151 // If the value 00011010 has 3 leading 0s then it has 5 significant bits
152 // (the bits which are not leading zeros)
153 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
154 // This is making the assumption that the size of a byte is 8 bits
155 // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
156 int mipLevelCount = significantBits;
157
158 // SkMipmap does not include the base mip level.
159 // For example, it contains levels 1-x instead of 0-x.
160 // This is because the image used to create SkMipmap is the base level.
161 // So subtract 1 from the mip level count.
162 if (mipLevelCount > 0) {
163 --mipLevelCount;
164 }
165
166 return mipLevelCount;
167 }
168
169 SkISize SkMipmap::ComputeLevelSize(int baseWidth, int baseHeight, int level) {
170 if (baseWidth < 1 || baseHeight < 1) {
171 return SkISize::Make(0, 0);
172 }
173
174 int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight);
175 if (level >= maxLevelCount || level < 0) {
176 return SkISize::Make(0, 0);
177 }
178 // OpenGL's spec requires that each mipmap level have height/width equal to
179 // max(1, floor(original_height / 2^i)
180 // (or original_width) where i is the mipmap level.
181
182 // SkMipmap does not include the base mip level.
183 // For example, it contains levels 1-x instead of 0-x.
184 // This is because the image used to create SkMipmap is the base level.
185 // So subtract 1 from the mip level to get the index stored by SkMipmap.
186 int width = std::max(1, baseWidth >> (level + 1));
187 int height = std::max(1, baseHeight >> (level + 1));
188
189 return SkISize::Make(width, height);
190 }
191
192 ///////////////////////////////////////////////////////////////////////////////
193
194 // Returns fractional level value. floor(level) is the index of the larger level.
195 // < 0 means failure.
196 float SkMipmap::ComputeLevel(SkSize scaleSize) {
197 SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0);
198
199 #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE
200 // Use the smallest scale to match the GPU impl.
201 const float scale = std::min(scaleSize.width(), scaleSize.height());
202 #else
203 // Ideally we'd pick the smaller scale, to match Ganesh. But ignoring one of the
204 // scales can produce some atrocious results, so for now we use the geometric mean.
205 // (https://bugs.chromium.org/p/skia/issues/detail?id=4863)
206 const float scale = std::sqrt(scaleSize.width() * scaleSize.height());
207 #endif
208
209 if (scale >= SK_Scalar1 || scale <= 0 || !SkIsFinite(scale)) {
210 return -1;
211 }
212
213 // The -0.5 bias here is to emulate GPU's sharpen mipmap option.
214 float L = std::max(-SkScalarLog2(scale) - 0.5f, 0.f);
215 if (!SkIsFinite(L)) {
216 return -1;
217 }
218 return L;
219 }
220
221 bool SkMipmap::extractLevel(SkSize scaleSize, Level* levelPtr) const {
222 if (nullptr == fLevels) {
223 return false;
224 }
225
226 float L = ComputeLevel(scaleSize);
227 int level = sk_float_round2int(L);
228 if (level <= 0) {
229 return false;
230 }
231
232 if (level > fCount) {
233 level = fCount;
234 }
235 if (levelPtr) {
236 *levelPtr = fLevels[level - 1];
237 // need to augment with our colorspace
238 levelPtr->fPixmap.setColorSpace(fCS);
239 }
240 return true;
241 }
242
243 bool SkMipmap::validForRootLevel(const SkImageInfo& root) const {
244 if (nullptr == fLevels) {
245 return false;
246 }
247
248 const SkISize dimension = root.dimensions();
249 if (dimension.width() <= 1 && dimension.height() <= 1) {
250 return false;
251 }
252
253 if (fLevels[0].fPixmap. width() != std::max(1, dimension. width() >> 1) ||
254 fLevels[0].fPixmap.height() != std::max(1, dimension.height() >> 1)) {
255 return false;
256 }
257
258 for (int i = 0; i < this->countLevels(); ++i) {
259 if (fLevels[i].fPixmap.colorType() != root.colorType() ||
260 fLevels[i].fPixmap.alphaType() != root.alphaType()) {
261 return false;
262 }
263 }
264 return true;
265 }
266
267 // Helper which extracts a pixmap from the src bitmap
268 //
269 SkMipmap* SkMipmap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
270 SkPixmap srcPixmap;
271 if (!src.peekPixels(&srcPixmap)) {
272 return nullptr;
273 }
274 return Build(srcPixmap, fact);
275 }
276
277 int SkMipmap::countLevels() const {
278 return fCount;
279 }
280
281 bool SkMipmap::getLevel(int index, Level* levelPtr) const {
282 if (nullptr == fLevels) {
283 return false;
284 }
285 if (index < 0) {
286 return false;
287 }
288 if (index > fCount - 1) {
289 return false;
290 }
291 if (levelPtr) {
292 *levelPtr = fLevels[index];
293 // need to augment with our colorspace
294 levelPtr->fPixmap.setColorSpace(fCS);
295 }
296 return true;
297 }