Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / tests / MipMapTest.cpp
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 "SkBitmap.h"
9 #include "SkMipMap.h"
10 #include "SkRandom.h"
11 #include "Test.h"
12
13 static void make_bitmap(SkBitmap* bm, SkRandom& rand) {
14     // 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;
18     bm->allocN32Pixels(w, h);
19     bm->eraseColor(SK_ColorWHITE);
20 }
21
22 DEF_TEST(MipMap, reporter) {
23     SkBitmap bm;
24     SkRandom rand;
25
26     for (int i = 0; i < 500; ++i) {
27         make_bitmap(&bm, rand);
28         SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, NULL));
29
30         REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1, NULL));
31         REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1 * 2, NULL));
32
33         SkMipMap::Level prevLevel;
34         sk_bzero(&prevLevel, sizeof(prevLevel));
35
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);
46
47                 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 }