blob: c8396effa6ea133721fc9397c8d56d46927bb652 [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
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000013static void make_bitmap(SkBitmap* bm, SkRandom& rand) {
reed@google.comeed6f1b2013-07-18 19:53:31 +000014 // for now, Build needs a min size of 2, otherwise it will return NULL.
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.orgdeee4962014-02-13 14:41:43 +000018 bm->allocN32Pixels(w, h);
reed@google.comeed6f1b2013-07-18 19:53:31 +000019 bm->eraseColor(SK_ColorWHITE);
20}
21
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000022DEF_TEST(MipMap, reporter) {
reed@google.comeed6f1b2013-07-18 19:53:31 +000023 SkBitmap bm;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000024 SkRandom rand;
reed@google.comeed6f1b2013-07-18 19:53:31 +000025
26 for (int i = 0; i < 500; ++i) {
27 make_bitmap(&bm, rand);
reed9d93c2e2014-10-08 05:17:12 -070028 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, NULL));
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000029
reed@google.comeed6f1b2013-07-18 19:53:31 +000030 REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1, NULL));
31 REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1 * 2, NULL));
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000032
reed@google.com2c38fed2013-07-24 14:55:00 +000033 SkMipMap::Level prevLevel;
34 sk_bzero(&prevLevel, sizeof(prevLevel));
reed@google.comeed6f1b2013-07-18 19:53:31 +000035
36 SkScalar scale = SK_Scalar1;
37 for (int j = 0; j < 30; ++j) {
38 scale = scale * 2 / 3;
39
40 SkMipMap::Level level;
41 if (mm->extractLevel(scale, &level)) {
42 REPORTER_ASSERT(reporter, level.fPixels);
43 REPORTER_ASSERT(reporter, level.fWidth > 0);
44 REPORTER_ASSERT(reporter, level.fHeight > 0);
45 REPORTER_ASSERT(reporter, level.fRowBytes >= level.fWidth * 4);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +000046
reed@google.comeed6f1b2013-07-18 19:53:31 +000047 if (prevLevel.fPixels) {
48 REPORTER_ASSERT(reporter, level.fWidth <= prevLevel.fWidth);
49 REPORTER_ASSERT(reporter, level.fHeight <= prevLevel.fHeight);
50 }
51 prevLevel = level;
52 }
53 }
54 }
55}