Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: src/core/SkMipMap.cpp

Issue 1639693002: Adding direct access to SkMipMap levels. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Making negative test assert SkMipMap::Build returned null Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // We add 1 below because countLevels stores the extra, non-base mipmap
255 // levels. That is, SkMipMap stores levels 1-x not 0-x.
256 SkASSERT(countLevels + 1 == SkMipMap::ComputeLevelCount(src.width(), src.hei ght()));
257
252 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); 258 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size);
253 if (0 == storageSize) { 259 if (0 == storageSize) {
254 return nullptr; 260 return nullptr;
255 } 261 }
256 262
257 SkMipMap* mipmap; 263 SkMipMap* mipmap;
258 if (fact) { 264 if (fact) {
259 SkDiscardableMemory* dm = fact(storageSize); 265 SkDiscardableMemory* dm = fact(storageSize);
260 if (nullptr == dm) { 266 if (nullptr == dm) {
261 return nullptr; 267 return nullptr;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 return nullptr; 366 return nullptr;
361 } 367 }
362 const SkPixmap& srcPixmap = srcUnlocker.pixmap(); 368 const SkPixmap& srcPixmap = srcUnlocker.pixmap();
363 // Try to catch where we might have returned nullptr for src crbug.com/49281 8 369 // Try to catch where we might have returned nullptr for src crbug.com/49281 8
364 if (nullptr == srcPixmap.addr()) { 370 if (nullptr == srcPixmap.addr()) {
365 sk_throw(); 371 sk_throw();
366 } 372 }
367 return Build(srcPixmap, fact); 373 return Build(srcPixmap, fact);
368 } 374 }
369 375
376 int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) {
377 // OpenGL's spec requires that each mipmap level have height/width equal to
378 // max(1, floor(original_height / 2^i)
379 // (or original_width) where i is the mipmap level.
380 // Continue scaling down until both axes are size 1.
381 //
382 // This means when it maintains isotropic space (both axes scaling down
383 // at the same rate) until one axis hits size 1.
384 // At that point, OpenGL continues to scale down into anisotropic space
385 // (where the scales are not the same between axes).
386 //
387 // Skia currently does not go into anisotropic space.
388 // Once an axis hits size 1 we stop.
389 // All this means is rather than use the largest axis we will use the
390 // smallest axis.
391
392 const int smallestAxis = SkTMin(baseWidth, baseHeight);
393 if (smallestAxis < 2) {
394 // SkMipMap::Build requires a minimum size of 2.
395 return 0;
396 }
397 const int leadingZeros = SkCLZ(static_cast<uint32_t>(smallestAxis));
398 // If the value 00011010 has 3 leading 0s then it has 5 significant bits
399 // (the bits which are not leading zeros)
400 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
401 // This is making the assumption that the size of a byte is 8 bits
402 // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
403 const int mipLevelCount = significantBits;
404
405 return mipLevelCount;
406 }
407
408 int SkMipMap::countLevels() const {
409 return fCount;
410 }
411
412 void SkMipMap::getLevel(int index, Level* levelPtr) const {
413 if (NULL == fLevels) {
reed1 2016/02/05 22:37:11 I think we should either assert or return a bool (
cblume 2016/02/06 13:38:16 Done.
414 return;
415 }
416 if (index < 0) {
417 return;
418 }
419 if (index > fCount - 1) {
420 return;
421 }
422 if (levelPtr) {
423 *levelPtr = fLevels[index];
424 }
425 }
OLDNEW
« src/core/SkMipMap.h ('K') | « src/core/SkMipMap.h ('k') | tests/MipMapTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698