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

Unified Diff: tests/MipMapTest.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, 11 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 side-by-side diff with in-line comments
Download patch
« src/core/SkMipMap.cpp ('K') | « src/core/SkMipMap.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/MipMapTest.cpp
diff --git a/tests/MipMapTest.cpp b/tests/MipMapTest.cpp
index 11021e6ebf960ef674c6a9b4f35bfde0275d30e9..62655168aac6f398fc89a5b105f6d8c5b4b68255 100644
--- a/tests/MipMapTest.cpp
+++ b/tests/MipMapTest.cpp
@@ -10,12 +10,8 @@
#include "SkRandom.h"
#include "Test.h"
-static void make_bitmap(SkBitmap* bm, SkRandom& rand) {
- // for now, Build needs a min size of 2, otherwise it will return nullptr.
- // should fix that to support 1 X N, where N > 1 to return non-null.
- int w = 2 + rand.nextU() % 1000;
- int h = 2 + rand.nextU() % 1000;
- bm->allocN32Pixels(w, h);
+static void make_bitmap(SkBitmap* bm, int width, int height) {
+ bm->allocN32Pixels(width, height);
bm->eraseColor(SK_ColorWHITE);
}
@@ -24,9 +20,18 @@ DEF_TEST(MipMap, reporter) {
SkRandom rand;
for (int i = 0; i < 500; ++i) {
- make_bitmap(&bm, rand);
+ // for now, Build needs a min size of 2, otherwise it will return nullptr.
+ // should fix that to support 1 X N, where N > 1 to return non-null.
+ int width = 2 + rand.nextU() % 1000;
+ int height = 2 + rand.nextU() % 1000;
+ make_bitmap(&bm, width, height);
SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
+ // Add 1 because SkMipMap doesn't store the base mip level.
+ // It stores levels 1-x, not 0-x.
+ REPORTER_ASSERT(reporter, mm->countLevels() + 1 == SkMipMap::ComputeLevelCount(width,
+ height));
+
REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1, nullptr));
REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1 * 2, nullptr));
@@ -53,3 +58,162 @@ DEF_TEST(MipMap, reporter) {
}
}
}
+
+static void test_mipmap_generation(int width, int height, int expectedMipLevelCount,
+ skiatest::Reporter* reporter) {
+ SkBitmap bm;
+ bm.allocN32Pixels(width, height);
+ bm.eraseColor(SK_ColorWHITE);
+ SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
+
+ const int mipLevelCount = mm->countLevels();
+ REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount);
+ for (int i = 0; i < mipLevelCount; ++i) {
+ SkMipMap::Level level;
+ mm->getLevel(i, &level);
+ // Make sure the mipmaps contain valid data and that the sizes are
+ // correct
+ REPORTER_ASSERT(reporter, level.fPixmap.addr());
+
+ // + 1 because SkMipMap does not include the base mipmap level.
+ int twoToTheMipLevel = 1 << (i + 1);
+ int currentWidth = width / twoToTheMipLevel;
+ int currentHeight = height / twoToTheMipLevel;
+ REPORTER_ASSERT(reporter, level.fPixmap.width() == currentWidth);
+ REPORTER_ASSERT(reporter, level.fPixmap.height() == currentHeight);
+ }
+}
+
+DEF_TEST(MipMap_DirectLevelAccess, reporter) {
+ // create mipmap with invalid size
+ {
+ // SkMipMap current requires the dimensions be greater than 2x2
+ SkBitmap bm;
+ bm.allocN32Pixels(1, 1);
+ bm.eraseColor(SK_ColorWHITE);
+ SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
+
+ REPORTER_ASSERT(reporter, mm == nullptr);
+ }
+
+
+ // check small mipmap's count and levels
+ // There should be 5 mipmap levels generated:
+ // 16x16, 8x8, 4x4, 2x2, 1x1
+ test_mipmap_generation(32, 32, 5, reporter);
+
+ // check large mipmap's count and levels
+ // There should be 9 mipmap levels generated:
+ // 500x500, 250x250, 125x125, 62x62, 31x31, 15x15, 7x7, 3x3. 1x1
+ test_mipmap_generation(1000, 1000, 9, reporter);
+}
+
+DEF_TEST(MipMap_ComputeLevelCount, reporter) {
+ // Test mipmaps with negative sizes
+ int negativeWidthResult = SkMipMap::ComputeLevelCount(-100, 100);
+ REPORTER_ASSERT(reporter, negativeWidthResult == 0);
+
+ int negativeHeightResult = SkMipMap::ComputeLevelCount(100, -100);
+ REPORTER_ASSERT(reporter, negativeHeightResult == 0);
+
+ int negativeBothResult = SkMipMap::ComputeLevelCount(-100, -100);
+ REPORTER_ASSERT(reporter, negativeBothResult == 0);
+
+
+ // Test mipmaps with 0, 1, 2 as dimensions
+ // (SkMipMap::Build requires a min size of 2)
+ //
+ // 0
+ int zeroWidthResult = SkMipMap::ComputeLevelCount(0, 100);
+ REPORTER_ASSERT(reporter, zeroWidthResult == 0);
+
+ int zeroHeightResult = SkMipMap::ComputeLevelCount(100, 0);
+ REPORTER_ASSERT(reporter, zeroHeightResult == 0);
+
+ int zeroBothResult = SkMipMap::ComputeLevelCount(0, 0);
+ REPORTER_ASSERT(reporter, zeroBothResult == 0);
+
+ // 1
+ int oneWidthResult = SkMipMap::ComputeLevelCount(1, 100);
+ REPORTER_ASSERT(reporter, oneWidthResult == 0);
+
+ int oneHeightResult = SkMipMap::ComputeLevelCount(100, 1);
+ REPORTER_ASSERT(reporter, oneHeightResult == 0);
+
+ int oneBothResult = SkMipMap::ComputeLevelCount(1, 1);
+ REPORTER_ASSERT(reporter, oneBothResult == 0);
+
+ // 2
+ int twoWidthResult = SkMipMap::ComputeLevelCount(2, 100);
+ REPORTER_ASSERT(reporter, twoWidthResult == 2);
+
+ int twoHeightResult = SkMipMap::ComputeLevelCount(100, 2);
+ REPORTER_ASSERT(reporter, twoHeightResult == 2);
+
+ int twoBothResult = SkMipMap::ComputeLevelCount(2, 2);
+ REPORTER_ASSERT(reporter, twoBothResult == 2);
+
+
+ // Test a handful of boundaries such as 63x63 and 64x64
+ int sixtyThreeResult = SkMipMap::ComputeLevelCount(63, 63);
+ REPORTER_ASSERT(reporter, sixtyThreeResult == 6);
+
+ int sixtyFourResult = SkMipMap::ComputeLevelCount(64, 64);
+ REPORTER_ASSERT(reporter, sixtyFourResult == 7);
+
+ int oneHundredTwentySevenResult = SkMipMap::ComputeLevelCount(127, 127);
+ REPORTER_ASSERT(reporter, oneHundredTwentySevenResult == 7);
+
+ int oneHundredTwentyEightResult = SkMipMap::ComputeLevelCount(128, 128);
+ REPORTER_ASSERT(reporter, oneHundredTwentyEightResult == 8);
+
+ int twoHundredFiftyFiveResult = SkMipMap::ComputeLevelCount(255, 255);
+ REPORTER_ASSERT(reporter, twoHundredFiftyFiveResult == 8);
+
+ int twoHundredFiftySixResult = SkMipMap::ComputeLevelCount(256, 256);
+ REPORTER_ASSERT(reporter, twoHundredFiftySixResult == 9);
+
+
+ // Test different dimensions, such as 256x64
+ int smallestAxisIsSixtyFourResult = SkMipMap::ComputeLevelCount(64, 129);
+ REPORTER_ASSERT(reporter, smallestAxisIsSixtyFourResult == 7);
+
+ int smallestAxisIsThirtyTwoResult = SkMipMap::ComputeLevelCount(255, 32);
+ REPORTER_ASSERT(reporter, smallestAxisIsThirtyTwoResult == 6);
+
+ int smallestAxisIsFiveHundredResult = SkMipMap::ComputeLevelCount(500, 1000);
+ REPORTER_ASSERT(reporter, smallestAxisIsFiveHundredResult == 9);
+
+ // Test a few calls to SkMipMap::Build to make sure it matches
+ // SkMipMap::ComputeLevelCount
+ //
+ // Add 1 to mm->countLevels() because SkMipMap does not contain the base
+ // mipmap level.
+ // It contains mipmap levels 1-x, not 0-x.
+ {
+ SkBitmap bm;
+ bm.allocN32Pixels(255, 255);
+ bm.eraseColor(SK_ColorWHITE);
+ SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
+
+ REPORTER_ASSERT(reporter, mm->countLevels() + 1 == SkMipMap::ComputeLevelCount(255, 255));
+ }
+
+ {
+ SkBitmap bm;
+ bm.allocN32Pixels(32, 32);
+ bm.eraseColor(SK_ColorWHITE);
+ SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
+
+ REPORTER_ASSERT(reporter, mm->countLevels() + 1 == SkMipMap::ComputeLevelCount(32, 32));
+ }
+
+ {
+ SkBitmap bm;
+ bm.allocN32Pixels(1000, 1000);
+ bm.eraseColor(SK_ColorWHITE);
+ SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
+
+ REPORTER_ASSERT(reporter, mm->countLevels() + 1 == SkMipMap::ComputeLevelCount(1000, 1000));
+ }
+}
« src/core/SkMipMap.cpp ('K') | « src/core/SkMipMap.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698