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
8#include "Test.h"
9#include "TestClassDef.h"
10#include "SkMipMap.h"
11#include "SkBitmap.h"
12#include "SkRandom.h"
13
14static void make_bitmap(SkBitmap* bm, SkRandom& rand) {
15    // for now, Build needs a min size of 2, otherwise it will return NULL.
16    // should fix that to support 1 X N, where N > 1 to return non-null.
17    int w = 2 + rand.nextU() % 1000;
18    int h = 2 + rand.nextU() % 1000;
19    bm->setConfig(SkBitmap::kARGB_8888_Config, w, h);
20    bm->allocPixels();
21    bm->eraseColor(SK_ColorWHITE);
22}
23
24DEF_TEST(MipMap, reporter) {
25    SkBitmap bm;
26    SkRandom rand;
27
28    for (int i = 0; i < 500; ++i) {
29        make_bitmap(&bm, rand);
30        SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm));
31
32        REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1, NULL));
33        REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1 * 2, NULL));
34
35        SkMipMap::Level prevLevel;
36        sk_bzero(&prevLevel, sizeof(prevLevel));
37
38        SkScalar scale = SK_Scalar1;
39        for (int j = 0; j < 30; ++j) {
40            scale = scale * 2 / 3;
41
42            SkMipMap::Level level;
43            if (mm->extractLevel(scale, &level)) {
44                REPORTER_ASSERT(reporter, level.fPixels);
45                REPORTER_ASSERT(reporter, level.fWidth > 0);
46                REPORTER_ASSERT(reporter, level.fHeight > 0);
47                REPORTER_ASSERT(reporter, level.fRowBytes >= level.fWidth * 4);
48
49                if (prevLevel.fPixels) {
50                    REPORTER_ASSERT(reporter, level.fWidth <= prevLevel.fWidth);
51                    REPORTER_ASSERT(reporter, level.fHeight <= prevLevel.fHeight);
52                }
53                prevLevel = level;
54            }
55        }
56    }
57}
58