blob: 8f1591f70801312b9b1420e335b8bf69506e2cde [file] [log] [blame]
reed@google.com772443a2013-12-11 15:30:24 +00001 /*
reed@google.com602a1d72013-07-23 19:13:54 +00002 * 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"
reed@google.come4eb1222013-12-09 22:29:30 +00009#include "SkDiscardableMemory.h"
reed@google.com602a1d72013-07-23 19:13:54 +000010#include "SkScaledImageCache.h"
11
12static void make_bm(SkBitmap* bm, int w, int h) {
13 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h);
14 bm->allocPixels();
15}
16
reed@google.come4eb1222013-12-09 22:29:30 +000017static const int COUNT = 10;
18static const int DIM = 256;
19
20static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache,
21 bool testPurge) {
reed@google.com602a1d72013-07-23 19:13:54 +000022 SkScaledImageCache::ID* id;
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000023
reed@google.com602a1d72013-07-23 19:13:54 +000024 SkBitmap bm[COUNT];
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000025
reed@google.com772443a2013-12-11 15:30:24 +000026 const SkScalar scale = 2;
27 for (int i = 0; i < COUNT; ++i) {
28 make_bm(&bm[i], DIM, DIM);
29 }
30
reed@google.com0e661622013-07-23 19:27:48 +000031 for (int i = 0; i < COUNT; ++i) {
reed@google.com602a1d72013-07-23 19:13:54 +000032 SkBitmap tmp;
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000033
reed@google.com772443a2013-12-11 15:30:24 +000034 SkScaledImageCache::ID* id = cache.findAndLock(bm[i], scale, scale, &tmp);
reed@google.com602a1d72013-07-23 19:13:54 +000035 REPORTER_ASSERT(reporter, NULL == id);
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000036
reed@google.com602a1d72013-07-23 19:13:54 +000037 make_bm(&tmp, DIM, DIM);
38 id = cache.addAndLock(bm[i], scale, scale, tmp);
39 REPORTER_ASSERT(reporter, NULL != id);
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000040
reed@google.com602a1d72013-07-23 19:13:54 +000041 SkBitmap tmp2;
42 SkScaledImageCache::ID* id2 = cache.findAndLock(bm[i], scale, scale,
43 &tmp2);
44 REPORTER_ASSERT(reporter, id == id2);
45 REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef());
46 REPORTER_ASSERT(reporter, tmp.width() == tmp2.width());
47 REPORTER_ASSERT(reporter, tmp.height() == tmp2.height());
48 cache.unlock(id2);
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000049
reed@google.com602a1d72013-07-23 19:13:54 +000050 cache.unlock(id);
51 }
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000052
reed@google.come4eb1222013-12-09 22:29:30 +000053 if (testPurge) {
54 // stress test, should trigger purges
reed@google.com772443a2013-12-11 15:30:24 +000055 float incScale = 2;
reed@google.come4eb1222013-12-09 22:29:30 +000056 for (size_t i = 0; i < COUNT * 100; ++i) {
reed@google.com772443a2013-12-11 15:30:24 +000057 incScale += 1;
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000058
reed@google.come4eb1222013-12-09 22:29:30 +000059 SkBitmap tmp;
reed@google.come4eb1222013-12-09 22:29:30 +000060 make_bm(&tmp, DIM, DIM);
reed@google.com772443a2013-12-11 15:30:24 +000061
62 SkScaledImageCache::ID* id = cache.addAndLock(bm[0], incScale,
63 incScale, tmp);
reed@google.come4eb1222013-12-09 22:29:30 +000064 REPORTER_ASSERT(reporter, NULL != id);
65 cache.unlock(id);
66 }
reed@google.com602a1d72013-07-23 19:13:54 +000067 }
reed@google.com772443a2013-12-11 15:30:24 +000068
69 // test the originals after all that purging
70 for (int i = 0; i < COUNT; ++i) {
71 SkBitmap tmp;
72 id = cache.findAndLock(bm[i], scale, scale, &tmp);
73 if (id) {
74 cache.unlock(id);
75 }
76 }
77
reed@google.com602a1d72013-07-23 19:13:54 +000078 cache.setByteLimit(0);
79}
80
reed@google.com772443a2013-12-11 15:30:24 +000081#include "SkDiscardableMemoryPool.h"
82
83static SkDiscardableMemoryPool* gPool;
84static SkDiscardableMemory* pool_factory(size_t bytes) {
85 return gPool->create(bytes);
86}
87
reed@google.come4eb1222013-12-09 22:29:30 +000088static void TestImageCache(skiatest::Reporter* reporter) {
reed@google.com772443a2013-12-11 15:30:24 +000089 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop
90
reed@google.come4eb1222013-12-09 22:29:30 +000091 {
reed@google.come4eb1222013-12-09 22:29:30 +000092 SkScaledImageCache cache(defLimit);
93 test_cache(reporter, cache, true);
94 }
95 {
reed@google.com772443a2013-12-11 15:30:24 +000096 SkDiscardableMemoryPool pool(defLimit);
97 gPool = &pool;
98 SkScaledImageCache cache(pool_factory);
99 test_cache(reporter, cache, true);
100 }
101 {
reed@google.come4eb1222013-12-09 22:29:30 +0000102 SkScaledImageCache cache(SkDiscardableMemory::Create);
103 test_cache(reporter, cache, false);
104 }
105}
106
reed@google.com602a1d72013-07-23 19:13:54 +0000107#include "TestClassDef.h"
108DEFINE_TESTCLASS("ImageCache", TestImageCacheClass, TestImageCache)
rmistry@google.comd6bab022013-12-02 13:50:38 +0000109
110DEF_TEST(ImageCache_doubleAdd, r) {
111 // Adding the same key twice should be safe.
112 SkScaledImageCache cache(1024);
113
114 SkBitmap original;
115 original.setConfig(SkBitmap::kARGB_8888_Config, 40, 40);
116 original.allocPixels();
117
118 SkBitmap scaled;
119 scaled.setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
120 scaled.allocPixels();
121
122 SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled);
123 SkScaledImageCache::ID* id2 = cache.addAndLock(original, 0.5f, 0.5f, scaled);
124 // We don't really care if id1 == id2 as long as unlocking both works.
125 cache.unlock(id1);
126 cache.unlock(id2);
127}