blob: b8815a32170da030c332ff12ef16889bfc2ee2bb [file] [log] [blame]
reed@google.com602a1d72013-07-23 19:13:54 +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
8#include "Test.h"
9#include "SkScaledImageCache.h"
10
11static void make_bm(SkBitmap* bm, int w, int h) {
12 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h);
13 bm->allocPixels();
14}
15
16static void TestImageCache(skiatest::Reporter* reporter) {
17 static const int COUNT = 10;
18 static const int DIM = 256;
19 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop
20 SkScaledImageCache cache(defLimit);
21 SkScaledImageCache::ID* id;
22
23 SkBitmap bm[COUNT];
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000024
reed@google.com602a1d72013-07-23 19:13:54 +000025 SkScalar scale = 2;
reed@google.com0e661622013-07-23 19:27:48 +000026 for (int i = 0; i < COUNT; ++i) {
reed@google.com602a1d72013-07-23 19:13:54 +000027 SkBitmap tmp;
28
29 make_bm(&bm[i], DIM, DIM);
30 id = cache.findAndLock(bm[i], scale, scale, &tmp);
31 REPORTER_ASSERT(reporter, NULL == id);
32
33 make_bm(&tmp, DIM, DIM);
34 id = cache.addAndLock(bm[i], scale, scale, tmp);
35 REPORTER_ASSERT(reporter, NULL != id);
36
37 SkBitmap tmp2;
38 SkScaledImageCache::ID* id2 = cache.findAndLock(bm[i], scale, scale,
39 &tmp2);
40 REPORTER_ASSERT(reporter, id == id2);
41 REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef());
42 REPORTER_ASSERT(reporter, tmp.width() == tmp2.width());
43 REPORTER_ASSERT(reporter, tmp.height() == tmp2.height());
44 cache.unlock(id2);
45
46 cache.unlock(id);
47 }
48
49 // stress test, should trigger purges
50 for (size_t i = 0; i < COUNT * 100; ++i) {
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000051 scale += 1;
52
reed@google.com602a1d72013-07-23 19:13:54 +000053 SkBitmap tmp;
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000054
reed@google.com602a1d72013-07-23 19:13:54 +000055 make_bm(&tmp, DIM, DIM);
56 id = cache.addAndLock(bm[0], scale, scale, tmp);
57 REPORTER_ASSERT(reporter, NULL != id);
58 cache.unlock(id);
reed@google.com602a1d72013-07-23 19:13:54 +000059 }
60
61 cache.setByteLimit(0);
62}
63
64#include "TestClassDef.h"
65DEFINE_TESTCLASS("ImageCache", TestImageCacheClass, TestImageCache)
rmistry@google.comd6bab022013-12-02 13:50:38 +000066
67DEF_TEST(ImageCache_doubleAdd, r) {
68 // Adding the same key twice should be safe.
69 SkScaledImageCache cache(1024);
70
71 SkBitmap original;
72 original.setConfig(SkBitmap::kARGB_8888_Config, 40, 40);
73 original.allocPixels();
74
75 SkBitmap scaled;
76 scaled.setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
77 scaled.allocPixels();
78
79 SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled);
80 SkScaledImageCache::ID* id2 = cache.addAndLock(original, 0.5f, 0.5f, scaled);
81 // We don't really care if id1 == id2 as long as unlocking both works.
82 cache.unlock(id1);
83 cache.unlock(id2);
84}