blob: 92d0b519d0dc3e367161f151e7f5c3508b7dbe09 [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
reed@google.come4eb1222013-12-09 22:29:30 +00008#include "SkDiscardableMemory.h"
reed@google.com602a1d72013-07-23 19:13:54 +00009#include "SkScaledImageCache.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
reed@google.com602a1d72013-07-23 19:13:54 +000011
12static void make_bm(SkBitmap* bm, int w, int h) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000013 bm->allocN32Pixels(w, h);
reed@google.com602a1d72013-07-23 19:13:54 +000014}
15
reed@google.come4eb1222013-12-09 22:29:30 +000016static const int COUNT = 10;
17static const int DIM = 256;
18
19static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache,
20 bool testPurge) {
reed@google.com602a1d72013-07-23 19:13:54 +000021 SkScaledImageCache::ID* id;
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000022
reed@google.com602a1d72013-07-23 19:13:54 +000023 SkBitmap bm[COUNT];
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000024
reed@google.com772443a2013-12-11 15:30:24 +000025 const SkScalar scale = 2;
26 for (int i = 0; i < COUNT; ++i) {
27 make_bm(&bm[i], DIM, DIM);
28 }
tfarina@chromium.org4f3c3052013-12-14 15:12:48 +000029
reed@google.com0e661622013-07-23 19:27:48 +000030 for (int i = 0; i < COUNT; ++i) {
reed@google.com602a1d72013-07-23 19:13:54 +000031 SkBitmap tmp;
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000032
reed@google.com772443a2013-12-11 15:30:24 +000033 SkScaledImageCache::ID* id = cache.findAndLock(bm[i], scale, scale, &tmp);
reed@google.com602a1d72013-07-23 19:13:54 +000034 REPORTER_ASSERT(reporter, NULL == id);
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000035
reed@google.com602a1d72013-07-23 19:13:54 +000036 make_bm(&tmp, DIM, DIM);
37 id = cache.addAndLock(bm[i], scale, scale, tmp);
38 REPORTER_ASSERT(reporter, NULL != id);
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000039
reed@google.com602a1d72013-07-23 19:13:54 +000040 SkBitmap tmp2;
41 SkScaledImageCache::ID* id2 = cache.findAndLock(bm[i], scale, scale,
42 &tmp2);
43 REPORTER_ASSERT(reporter, id == id2);
44 REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef());
45 REPORTER_ASSERT(reporter, tmp.width() == tmp2.width());
46 REPORTER_ASSERT(reporter, tmp.height() == tmp2.height());
47 cache.unlock(id2);
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000048
reed@google.com602a1d72013-07-23 19:13:54 +000049 cache.unlock(id);
50 }
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000051
reed@google.come4eb1222013-12-09 22:29:30 +000052 if (testPurge) {
53 // stress test, should trigger purges
reed@google.com772443a2013-12-11 15:30:24 +000054 float incScale = 2;
reed@google.come4eb1222013-12-09 22:29:30 +000055 for (size_t i = 0; i < COUNT * 100; ++i) {
reed@google.com772443a2013-12-11 15:30:24 +000056 incScale += 1;
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000057
reed@google.come4eb1222013-12-09 22:29:30 +000058 SkBitmap tmp;
reed@google.come4eb1222013-12-09 22:29:30 +000059 make_bm(&tmp, DIM, DIM);
reed@google.com772443a2013-12-11 15:30:24 +000060
61 SkScaledImageCache::ID* id = cache.addAndLock(bm[0], incScale,
62 incScale, tmp);
reed@google.come4eb1222013-12-09 22:29:30 +000063 REPORTER_ASSERT(reporter, NULL != id);
64 cache.unlock(id);
65 }
reed@google.com602a1d72013-07-23 19:13:54 +000066 }
reed@google.com772443a2013-12-11 15:30:24 +000067
68 // test the originals after all that purging
69 for (int i = 0; i < COUNT; ++i) {
70 SkBitmap tmp;
71 id = cache.findAndLock(bm[i], scale, scale, &tmp);
72 if (id) {
73 cache.unlock(id);
74 }
75 }
76
reed@google.com602a1d72013-07-23 19:13:54 +000077 cache.setByteLimit(0);
78}
79
reed@google.com772443a2013-12-11 15:30:24 +000080#include "SkDiscardableMemoryPool.h"
81
82static SkDiscardableMemoryPool* gPool;
83static SkDiscardableMemory* pool_factory(size_t bytes) {
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000084 SkASSERT(gPool);
reed@google.com772443a2013-12-11 15:30:24 +000085 return gPool->create(bytes);
86}
87
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000088DEF_TEST(ImageCache, 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 {
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000096 SkAutoTUnref<SkDiscardableMemoryPool> pool(
97 SkDiscardableMemoryPool::Create(defLimit, NULL));
98 gPool = pool.get();
reed@google.com772443a2013-12-11 15:30:24 +000099 SkScaledImageCache cache(pool_factory);
100 test_cache(reporter, cache, true);
101 }
102 {
reed@google.come4eb1222013-12-09 22:29:30 +0000103 SkScaledImageCache cache(SkDiscardableMemory::Create);
104 test_cache(reporter, cache, false);
105 }
106}
107
rmistry@google.comd6bab022013-12-02 13:50:38 +0000108DEF_TEST(ImageCache_doubleAdd, r) {
109 // Adding the same key twice should be safe.
commit-bot@chromium.org5e4112b2014-03-05 13:44:18 +0000110 SkScaledImageCache cache(4096);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000111
112 SkBitmap original;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000113 original.allocN32Pixels(40, 40);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000114
commit-bot@chromium.org5e4112b2014-03-05 13:44:18 +0000115 SkBitmap scaled1;
116 scaled1.allocN32Pixels(20, 20);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000117
commit-bot@chromium.org5e4112b2014-03-05 13:44:18 +0000118 SkBitmap scaled2;
119 scaled2.allocN32Pixels(20, 20);
120
121 SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled1);
122 SkScaledImageCache::ID* id2 = cache.addAndLock(original, 0.5f, 0.5f, scaled2);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000123 // We don't really care if id1 == id2 as long as unlocking both works.
124 cache.unlock(id1);
125 cache.unlock(id2);
commit-bot@chromium.org5e4112b2014-03-05 13:44:18 +0000126
127 SkBitmap tmp;
128 // Lookup should return the value that was added last.
129 SkScaledImageCache::ID* id = cache.findAndLock(original, 0.5f, 0.5f, &tmp);
130 REPORTER_ASSERT(r, NULL != id);
131 REPORTER_ASSERT(r, tmp.getGenerationID() == scaled2.getGenerationID());
132 cache.unlock(id);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000133}