reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 1 | /* |
| 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.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "SkMipMap.h" |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 10 | #include "SkRandom.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "Test.h" |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 12 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 13 | static void make_bitmap(SkBitmap* bm, SkRandom& rand) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 14 | // for now, Build needs a min size of 2, otherwise it will return nullptr. |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 15 | // should fix that to support 1 X N, where N > 1 to return non-null. |
| 16 | int w = 2 + rand.nextU() % 1000; |
| 17 | int h = 2 + rand.nextU() % 1000; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 18 | bm->allocN32Pixels(w, h); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 19 | bm->eraseColor(SK_ColorWHITE); |
| 20 | } |
| 21 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 22 | DEF_TEST(MipMap, reporter) { |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 23 | SkBitmap bm; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 24 | SkRandom rand; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 25 | |
| 26 | for (int i = 0; i < 500; ++i) { |
| 27 | make_bitmap(&bm, rand); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr)); |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 29 | |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame^] | 30 | REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_Scalar1), |
| 31 | nullptr)); |
| 32 | REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2, SK_Scalar1 * 2), |
| 33 | nullptr)); |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 34 | |
reed@google.com | 2c38fed | 2013-07-24 14:55:00 +0000 | [diff] [blame] | 35 | SkMipMap::Level prevLevel; |
| 36 | sk_bzero(&prevLevel, sizeof(prevLevel)); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 37 | |
| 38 | SkScalar scale = SK_Scalar1; |
| 39 | for (int j = 0; j < 30; ++j) { |
| 40 | scale = scale * 2 / 3; |
| 41 | |
| 42 | SkMipMap::Level level; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame^] | 43 | if (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, level.fPixmap.addr()); |
| 45 | REPORTER_ASSERT(reporter, level.fPixmap.width() > 0); |
| 46 | REPORTER_ASSERT(reporter, level.fPixmap.height() > 0); |
| 47 | REPORTER_ASSERT(reporter, (int)level.fPixmap.rowBytes() >= level.fPixmap.width() * 4); |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 48 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 49 | if (prevLevel.fPixmap.addr()) { |
| 50 | REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel.fPixmap.width()); |
| 51 | REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLevel.fPixmap.height()); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 52 | } |
| 53 | prevLevel = level; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |