blob: 4d4574698eac2bc85b6b2838e36f9d63cb86d5c6 [file] [log] [blame]
reed@google.comeed6f1b2013-07-18 19:53:31 +00001/*
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
reed@google.comeed6f1b2013-07-18 19:53:31 +00008#include "SkBitmap.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "SkMipMap.h"
reed@google.comeed6f1b2013-07-18 19:53:31 +000010#include "SkRandom.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
reed@google.comeed6f1b2013-07-18 19:53:31 +000012
cblumee2412d52016-02-17 14:53:23 -080013static void make_bitmap(SkBitmap* bm, int width, int height) {
14 bm->allocN32Pixels(width, height);
reed@google.comeed6f1b2013-07-18 19:53:31 +000015 bm->eraseColor(SK_ColorWHITE);
16}
17
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000018DEF_TEST(MipMap, reporter) {
reed@google.comeed6f1b2013-07-18 19:53:31 +000019 SkBitmap bm;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000020 SkRandom rand;
reed@google.comeed6f1b2013-07-18 19:53:31 +000021
22 for (int i = 0; i < 500; ++i) {
cblumee2412d52016-02-17 14:53:23 -080023 // for now, Build needs a min size of 2, otherwise it will return nullptr.
24 // should fix that to support 1 X N, where N > 1 to return non-null.
25 int width = 2 + rand.nextU() % 1000;
26 int height = 2 + rand.nextU() % 1000;
27 make_bitmap(&bm, width, height);
halcanary96fcdcc2015-08-27 07:41:13 -070028 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000029
cblumee2412d52016-02-17 14:53:23 -080030 REPORTER_ASSERT(reporter, mm->countLevels() == SkMipMap::ComputeLevelCount(width, height));
fmalita33ed3ad2016-02-09 08:20:18 -080031 REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_Scalar1),
32 nullptr));
33 REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2, SK_Scalar1 * 2),
34 nullptr));
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000035
reed@google.com2c38fed2013-07-24 14:55:00 +000036 SkMipMap::Level prevLevel;
37 sk_bzero(&prevLevel, sizeof(prevLevel));
reed@google.comeed6f1b2013-07-18 19:53:31 +000038
39 SkScalar scale = SK_Scalar1;
40 for (int j = 0; j < 30; ++j) {
41 scale = scale * 2 / 3;
42
43 SkMipMap::Level level;
fmalita33ed3ad2016-02-09 08:20:18 -080044 if (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
reed67b09bf2016-01-16 18:50:35 -080045 REPORTER_ASSERT(reporter, level.fPixmap.addr());
46 REPORTER_ASSERT(reporter, level.fPixmap.width() > 0);
47 REPORTER_ASSERT(reporter, level.fPixmap.height() > 0);
48 REPORTER_ASSERT(reporter, (int)level.fPixmap.rowBytes() >= level.fPixmap.width() * 4);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000049
reed67b09bf2016-01-16 18:50:35 -080050 if (prevLevel.fPixmap.addr()) {
51 REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel.fPixmap.width());
52 REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLevel.fPixmap.height());
reed@google.comeed6f1b2013-07-18 19:53:31 +000053 }
54 prevLevel = level;
55 }
56 }
57 }
58}
cblumee2412d52016-02-17 14:53:23 -080059
60static void test_mipmap_generation(int width, int height, int expectedMipLevelCount,
61 skiatest::Reporter* reporter) {
62 SkBitmap bm;
63 bm.allocN32Pixels(width, height);
64 bm.eraseColor(SK_ColorWHITE);
65 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
66
67 const int mipLevelCount = mm->countLevels();
68 REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount);
69 for (int i = 0; i < mipLevelCount; ++i) {
70 SkMipMap::Level level;
71 REPORTER_ASSERT(reporter, mm->getLevel(i, &level));
72 // Make sure the mipmaps contain valid data and that the sizes are correct
73 REPORTER_ASSERT(reporter, level.fPixmap.addr());
74
75 // + 1 because SkMipMap does not include the base mipmap level.
76 int twoToTheMipLevel = 1 << (i + 1);
77 int currentWidth = width / twoToTheMipLevel;
78 int currentHeight = height / twoToTheMipLevel;
79 REPORTER_ASSERT(reporter, level.fPixmap.width() == currentWidth);
80 REPORTER_ASSERT(reporter, level.fPixmap.height() == currentHeight);
81 }
82}
83
84DEF_TEST(MipMap_DirectLevelAccess, reporter) {
85 // create mipmap with invalid size
86 {
87 // SkMipMap current requires the dimensions be greater than 2x2
88 SkBitmap bm;
89 bm.allocN32Pixels(1, 1);
90 bm.eraseColor(SK_ColorWHITE);
91 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
92
93 REPORTER_ASSERT(reporter, mm == nullptr);
94 }
95
96 // check small mipmap's count and levels
97 // There should be 5 mipmap levels generated:
98 // 16x16, 8x8, 4x4, 2x2, 1x1
99 test_mipmap_generation(32, 32, 5, reporter);
100
101 // check large mipmap's count and levels
102 // There should be 9 mipmap levels generated:
103 // 500x500, 250x250, 125x125, 62x62, 31x31, 15x15, 7x7, 3x3, 1x1
104 test_mipmap_generation(1000, 1000, 9, reporter);
105}
106
107struct LevelCountScenario {
108 int fWidth;
109 int fHeight;
110 int fExpectedLevelCount;
111};
112
113DEF_TEST(MipMap_ComputeLevelCount, reporter) {
114 const LevelCountScenario tests[] = {
115 // Test mipmaps with negative sizes
116 {-100, 100, 0},
117 {100, -100, 0},
118 {-100, -100, 0},
119
120 // Test mipmaps with 0, 1, 2 as dimensions
121 // (SkMipMap::Build requires a min size of 2)
122 //
123 // 0
124 {0, 100, 0},
125 {100, 0, 0},
126 {0, 0, 0},
127 // 1
cblume5b9ad762016-03-01 13:54:30 -0800128 {1, 100, 6},
129 {100, 1, 6},
cblumee2412d52016-02-17 14:53:23 -0800130 {1, 1, 0},
131 // 2
cblume5b9ad762016-03-01 13:54:30 -0800132 {2, 100, 6},
133 {100, 2, 6},
cblumee2412d52016-02-17 14:53:23 -0800134 {2, 2, 1},
135
136 // Test a handful of boundaries such as 63x63 and 64x64
137 {63, 63, 5},
138 {64, 64, 6},
139 {127, 127, 6},
140 {128, 128, 7},
141 {255, 255, 7},
142 {256, 256, 8},
143
144 // Test different dimensions, such as 256x64
cblume5b9ad762016-03-01 13:54:30 -0800145 {64, 129, 7},
146 {255, 32, 7},
147 {500, 1000, 9}
cblumee2412d52016-02-17 14:53:23 -0800148 };
149
150 for (auto& currentTest : tests) {
151 int levelCount = SkMipMap::ComputeLevelCount(currentTest.fWidth, currentTest.fHeight);
152 REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount);
153 }
154}