blob: 467fd34998f32d1afb75e28f78ff7988600b9141 [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) {
brianosman9cf31f42016-03-25 12:55:52 -070023 int width = 1 + rand.nextU() % 1000;
24 int height = 1 + rand.nextU() % 1000;
cblumee2412d52016-02-17 14:53:23 -080025 make_bitmap(&bm, width, height);
halcanary96fcdcc2015-08-27 07:41:13 -070026 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000027
cblumee2412d52016-02-17 14:53:23 -080028 REPORTER_ASSERT(reporter, mm->countLevels() == SkMipMap::ComputeLevelCount(width, height));
fmalita33ed3ad2016-02-09 08:20:18 -080029 REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_Scalar1),
30 nullptr));
31 REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2, SK_Scalar1 * 2),
32 nullptr));
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000033
reed@google.com2c38fed2013-07-24 14:55:00 +000034 SkMipMap::Level prevLevel;
35 sk_bzero(&prevLevel, sizeof(prevLevel));
reed@google.comeed6f1b2013-07-18 19:53:31 +000036
37 SkScalar scale = SK_Scalar1;
38 for (int j = 0; j < 30; ++j) {
39 scale = scale * 2 / 3;
40
41 SkMipMap::Level level;
fmalita33ed3ad2016-02-09 08:20:18 -080042 if (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
reed67b09bf2016-01-16 18:50:35 -080043 REPORTER_ASSERT(reporter, level.fPixmap.addr());
44 REPORTER_ASSERT(reporter, level.fPixmap.width() > 0);
45 REPORTER_ASSERT(reporter, level.fPixmap.height() > 0);
46 REPORTER_ASSERT(reporter, (int)level.fPixmap.rowBytes() >= level.fPixmap.width() * 4);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000047
reed67b09bf2016-01-16 18:50:35 -080048 if (prevLevel.fPixmap.addr()) {
49 REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel.fPixmap.width());
50 REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLevel.fPixmap.height());
reed@google.comeed6f1b2013-07-18 19:53:31 +000051 }
52 prevLevel = level;
53 }
54 }
55 }
56}
cblumee2412d52016-02-17 14:53:23 -080057
58static void test_mipmap_generation(int width, int height, int expectedMipLevelCount,
59 skiatest::Reporter* reporter) {
60 SkBitmap bm;
61 bm.allocN32Pixels(width, height);
62 bm.eraseColor(SK_ColorWHITE);
63 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
64
65 const int mipLevelCount = mm->countLevels();
66 REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount);
67 for (int i = 0; i < mipLevelCount; ++i) {
68 SkMipMap::Level level;
69 REPORTER_ASSERT(reporter, mm->getLevel(i, &level));
70 // Make sure the mipmaps contain valid data and that the sizes are correct
71 REPORTER_ASSERT(reporter, level.fPixmap.addr());
72
73 // + 1 because SkMipMap does not include the base mipmap level.
74 int twoToTheMipLevel = 1 << (i + 1);
75 int currentWidth = width / twoToTheMipLevel;
76 int currentHeight = height / twoToTheMipLevel;
77 REPORTER_ASSERT(reporter, level.fPixmap.width() == currentWidth);
78 REPORTER_ASSERT(reporter, level.fPixmap.height() == currentHeight);
79 }
80}
81
82DEF_TEST(MipMap_DirectLevelAccess, reporter) {
83 // create mipmap with invalid size
84 {
85 // SkMipMap current requires the dimensions be greater than 2x2
86 SkBitmap bm;
87 bm.allocN32Pixels(1, 1);
88 bm.eraseColor(SK_ColorWHITE);
89 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
90
91 REPORTER_ASSERT(reporter, mm == nullptr);
92 }
93
94 // check small mipmap's count and levels
95 // There should be 5 mipmap levels generated:
96 // 16x16, 8x8, 4x4, 2x2, 1x1
97 test_mipmap_generation(32, 32, 5, reporter);
98
99 // check large mipmap's count and levels
100 // There should be 9 mipmap levels generated:
101 // 500x500, 250x250, 125x125, 62x62, 31x31, 15x15, 7x7, 3x3, 1x1
102 test_mipmap_generation(1000, 1000, 9, reporter);
103}
104
105struct LevelCountScenario {
106 int fWidth;
107 int fHeight;
108 int fExpectedLevelCount;
109};
110
111DEF_TEST(MipMap_ComputeLevelCount, reporter) {
112 const LevelCountScenario tests[] = {
113 // Test mipmaps with negative sizes
114 {-100, 100, 0},
115 {100, -100, 0},
116 {-100, -100, 0},
117
118 // Test mipmaps with 0, 1, 2 as dimensions
brianosman9cf31f42016-03-25 12:55:52 -0700119 // (SkMipMap::Build requires a min size of 1)
cblumee2412d52016-02-17 14:53:23 -0800120 //
121 // 0
122 {0, 100, 0},
123 {100, 0, 0},
124 {0, 0, 0},
125 // 1
cblume5b9ad762016-03-01 13:54:30 -0800126 {1, 100, 6},
127 {100, 1, 6},
cblumee2412d52016-02-17 14:53:23 -0800128 {1, 1, 0},
129 // 2
cblume5b9ad762016-03-01 13:54:30 -0800130 {2, 100, 6},
131 {100, 2, 6},
cblumee2412d52016-02-17 14:53:23 -0800132 {2, 2, 1},
133
134 // Test a handful of boundaries such as 63x63 and 64x64
135 {63, 63, 5},
136 {64, 64, 6},
137 {127, 127, 6},
138 {128, 128, 7},
139 {255, 255, 7},
140 {256, 256, 8},
141
142 // Test different dimensions, such as 256x64
cblume5b9ad762016-03-01 13:54:30 -0800143 {64, 129, 7},
144 {255, 32, 7},
145 {500, 1000, 9}
cblumee2412d52016-02-17 14:53:23 -0800146 };
147
148 for (auto& currentTest : tests) {
149 int levelCount = SkMipMap::ComputeLevelCount(currentTest.fWidth, currentTest.fHeight);
150 REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount);
151 }
152}