OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMipMap.h" | 8 #include "SkMipMap.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkMath.h" |
11 #include "SkNx.h" | 12 #include "SkNx.h" |
| 13 #include "SkTypes.h" |
12 | 14 |
13 // | 15 // |
14 // ColorTypeFilter is the "Type" we pass to some downsample template functions. | 16 // ColorTypeFilter is the "Type" we pass to some downsample template functions. |
15 // It controls how we expand a pixel into a large type, with space between each
component, | 17 // It controls how we expand a pixel into a large type, with space between each
component, |
16 // so we can then perform our simple filter (either box or triangle) and store t
he intermediates | 18 // so we can then perform our simple filter (either box or triangle) and store t
he intermediates |
17 // in the expanded type. | 19 // in the expanded type. |
18 // | 20 // |
19 | 21 |
20 struct ColorTypeFilter_8888 { | 22 struct ColorTypeFilter_8888 { |
21 typedef uint32_t Type; | 23 typedef uint32_t Type; |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 break; | 244 break; |
243 } | 245 } |
244 size += SkColorTypeMinRowBytes(ct, width) * height; | 246 size += SkColorTypeMinRowBytes(ct, width) * height; |
245 countLevels += 1; | 247 countLevels += 1; |
246 } | 248 } |
247 } | 249 } |
248 if (0 == countLevels) { | 250 if (0 == countLevels) { |
249 return nullptr; | 251 return nullptr; |
250 } | 252 } |
251 | 253 |
| 254 SkASSERT(countLevels == SkMipMap::ComputeLevelCount(src.width(), src.height(
))); |
| 255 |
252 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); | 256 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); |
253 if (0 == storageSize) { | 257 if (0 == storageSize) { |
254 return nullptr; | 258 return nullptr; |
255 } | 259 } |
256 | 260 |
257 SkMipMap* mipmap; | 261 SkMipMap* mipmap; |
258 if (fact) { | 262 if (fact) { |
259 SkDiscardableMemory* dm = fact(storageSize); | 263 SkDiscardableMemory* dm = fact(storageSize); |
260 if (nullptr == dm) { | 264 if (nullptr == dm) { |
261 return nullptr; | 265 return nullptr; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes(); | 315 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes(); |
312 } | 316 } |
313 srcPM = dstPM; | 317 srcPM = dstPM; |
314 addr += height * rowBytes; | 318 addr += height * rowBytes; |
315 } | 319 } |
316 SkASSERT(addr == baseAddr + size); | 320 SkASSERT(addr == baseAddr + size); |
317 | 321 |
318 return mipmap; | 322 return mipmap; |
319 } | 323 } |
320 | 324 |
| 325 int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) { |
| 326 // OpenGL's spec requires that each mipmap level have height/width equal to |
| 327 // max(1, floor(original_height / 2^i) |
| 328 // (or original_width) where i is the mipmap level. |
| 329 // Continue scaling down until both axes are size 1. |
| 330 // |
| 331 // This means it maintains isotropic space (both axes scaling down |
| 332 // at the same rate) until one axis hits size 1. |
| 333 // At that point, OpenGL continues to scale down into anisotropic space |
| 334 // (where the scales are not the same between axes). |
| 335 // |
| 336 // Skia currently does not go into anisotropic space. |
| 337 // Once an axis hits size 1 we stop. |
| 338 // All this means is rather than use the largest axis we will use the |
| 339 // smallest axis. |
| 340 |
| 341 const int smallestAxis = SkTMin(baseWidth, baseHeight); |
| 342 if (smallestAxis < 2) { |
| 343 // SkMipMap::Build requires a minimum size of 2. |
| 344 return 0; |
| 345 } |
| 346 const int leadingZeros = SkCLZ(static_cast<uint32_t>(smallestAxis)); |
| 347 // If the value 00011010 has 3 leading 0s then it has 5 significant bits |
| 348 // (the bits which are not leading zeros) |
| 349 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros; |
| 350 // This is making the assumption that the size of a byte is 8 bits |
| 351 // and that sizeof(uint32_t)'s implementation-defined behavior is 4. |
| 352 int mipLevelCount = significantBits; |
| 353 |
| 354 // SkMipMap does not include the base mip level. |
| 355 // For example, it contains levels 1-x instead of 0-x. |
| 356 // This is because the image used to create SkMipMap is the base level. |
| 357 // So subtract 1 from the mip level count. |
| 358 if (mipLevelCount > 0) { |
| 359 --mipLevelCount; |
| 360 } |
| 361 |
| 362 return mipLevelCount; |
| 363 } |
| 364 |
321 /////////////////////////////////////////////////////////////////////////////// | 365 /////////////////////////////////////////////////////////////////////////////// |
322 | 366 |
323 bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const { | 367 bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const { |
324 if (nullptr == fLevels) { | 368 if (nullptr == fLevels) { |
325 return false; | 369 return false; |
326 } | 370 } |
327 | 371 |
328 SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0); | 372 SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0); |
329 // Use the smallest scale to match the GPU impl. | 373 // Use the smallest scale to match the GPU impl. |
330 const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height()); | 374 const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 return nullptr; | 408 return nullptr; |
365 } | 409 } |
366 const SkPixmap& srcPixmap = srcUnlocker.pixmap(); | 410 const SkPixmap& srcPixmap = srcUnlocker.pixmap(); |
367 // Try to catch where we might have returned nullptr for src crbug.com/49281
8 | 411 // Try to catch where we might have returned nullptr for src crbug.com/49281
8 |
368 if (nullptr == srcPixmap.addr()) { | 412 if (nullptr == srcPixmap.addr()) { |
369 sk_throw(); | 413 sk_throw(); |
370 } | 414 } |
371 return Build(srcPixmap, fact); | 415 return Build(srcPixmap, fact); |
372 } | 416 } |
373 | 417 |
| 418 int SkMipMap::countLevels() const { |
| 419 return fCount; |
| 420 } |
| 421 |
| 422 bool SkMipMap::getLevel(int index, Level* levelPtr) const { |
| 423 if (NULL == fLevels) { |
| 424 return false; |
| 425 } |
| 426 if (index < 0) { |
| 427 return false; |
| 428 } |
| 429 if (index > fCount - 1) { |
| 430 return false; |
| 431 } |
| 432 if (levelPtr) { |
| 433 *levelPtr = fLevels[index]; |
| 434 } |
| 435 return true; |
| 436 } |
OLD | NEW |