blob: 9f1fa478fda036712d35784be0974c72cb816f5d [file] [log] [blame]
scroggo@google.com9f686f32012-11-29 21:05:37 +00001/*
2 * Copyright 2012 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
scroggo@google.comf8d7d272013-02-22 21:38:35 +00008#ifdef SK_DEBUG
9
scroggo@google.com9f686f32012-11-29 21:05:37 +000010#include "SkBitmap.h"
11#include "SkBitmapFactory.h"
12#include "SkCanvas.h"
13#include "SkColor.h"
14#include "SkData.h"
scroggo@google.comf8d7d272013-02-22 21:38:35 +000015#include "SkImageDecoder.h"
scroggo@google.com9f686f32012-11-29 21:05:37 +000016#include "SkImageEncoder.h"
scroggo@google.comf8d7d272013-02-22 21:38:35 +000017#include "SkLazyPixelRef.h"
18#include "SkLruImageCache.h"
scroggo@google.com9f686f32012-11-29 21:05:37 +000019#include "SkPaint.h"
scroggo@google.combb281f72013-03-18 21:37:39 +000020#include "SkPurgeableImageCache.h"
scroggo@google.com9f686f32012-11-29 21:05:37 +000021#include "SkStream.h"
22#include "SkTemplates.h"
23#include "Test.h"
24
25static SkBitmap* create_bitmap() {
26 SkBitmap* bm = SkNEW(SkBitmap);
scroggo@google.combb281f72013-03-18 21:37:39 +000027 // Use a large bitmap.
28 const int W = 1000, H = 1000;
scroggo@google.com9f686f32012-11-29 21:05:37 +000029 bm->setConfig(SkBitmap::kARGB_8888_Config, W, H);
30 bm->allocPixels();
31 bm->eraseColor(SK_ColorBLACK);
32 SkCanvas canvas(*bm);
33 SkPaint paint;
34 paint.setColor(SK_ColorBLUE);
35 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), SkIntToScalar(H/2), paint);
36 return bm;
37}
38
39static SkData* create_data_from_bitmap(const SkBitmap& bm) {
40 SkDynamicMemoryWStream stream;
41 if (SkImageEncoder::EncodeStream(&stream, bm, SkImageEncoder::kPNG_Type, 100)) {
42 return stream.copyToData();
43 }
44 return NULL;
45}
46
47static void assert_bounds_equal(skiatest::Reporter* reporter, const SkBitmap& bm1,
48 const SkBitmap& bm2) {
49 REPORTER_ASSERT(reporter, bm1.width() == bm2.width());
50 REPORTER_ASSERT(reporter, bm1.height() == bm2.height());
51}
52
scroggo@google.combb281f72013-03-18 21:37:39 +000053static void test_cache(skiatest::Reporter* reporter, SkImageCache* cache) {
54 // Test the cache directly:
55 cache->purgeAllUnpinnedCaches();
56 intptr_t ID = SkImageCache::UNINITIALIZED_ID;
57 const size_t size = 1000;
58 char buffer[size];
59 sk_bzero((void*) buffer, size);
60 void* memory = cache->allocAndPinCache(size, &ID);
61 if (memory != NULL) {
62 memcpy(memory, (void*)buffer, size);
63 REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID) == SkImageCache::kPinned_MemoryStatus);
64 cache->releaseCache(ID);
65 REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID) != SkImageCache::kPinned_MemoryStatus);
66 SkImageCache::DataStatus dataStatus;
67 memory = cache->pinCache(ID, &dataStatus);
68 if (memory != NULL) {
69 REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
70 == SkImageCache::kPinned_MemoryStatus);
71 if (SkImageCache::kRetained_DataStatus == dataStatus) {
72 REPORTER_ASSERT(reporter, !memcmp(memory, (void*) buffer, size));
73 }
74 cache->releaseCache(ID);
75 REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
76 != SkImageCache::kPinned_MemoryStatus);
77 cache->purgeAllUnpinnedCaches();
78 REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
79 != SkImageCache::kPinned_MemoryStatus);
80 memory = cache->pinCache(ID, &dataStatus);
81 if (memory != NULL) {
robertphillips@google.comae3f2ce2013-03-21 20:54:42 +000082 // The memory block may or may not have survived the purging (at the
83 // memory manager's whim) so we cannot check dataStatus here.
scroggo@google.combb281f72013-03-18 21:37:39 +000084 cache->releaseCache(ID);
85 REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
86 != SkImageCache::kPinned_MemoryStatus);
87 cache->throwAwayCache(ID);
88 REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
89 == SkImageCache::kFreed_MemoryStatus);
90 } else {
91 REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
92 == SkImageCache::kFreed_MemoryStatus);
93 }
94 }
95 }
96}
97
98static void test_factory(skiatest::Reporter* reporter, SkImageCache* cache, SkData* encodedData,
scroggo@google.comf8d7d272013-02-22 21:38:35 +000099 const SkBitmap& origBitmap) {
100 SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
101 factory.setImageCache(cache);
102 SkAutoTDelete<SkBitmap> bitmapFromFactory(SkNEW(SkBitmap));
103 bool success = factory.installPixelRef(encodedData, bitmapFromFactory.get());
104 // This assumes that if the encoder worked, the decoder should also work, so the above call
105 // should not fail.
106 REPORTER_ASSERT(reporter, success);
107 assert_bounds_equal(reporter, origBitmap, *bitmapFromFactory.get());
108
109 SkPixelRef* pixelRef = bitmapFromFactory->pixelRef();
110 REPORTER_ASSERT(reporter, pixelRef != NULL);
111 if (NULL == cache) {
112 // This assumes that installPixelRef called lockPixels.
113 REPORTER_ASSERT(reporter, bitmapFromFactory->readyToDraw());
114 } else {
115 // Lazy decoding
116 REPORTER_ASSERT(reporter, !bitmapFromFactory->readyToDraw());
117 SkLazyPixelRef* lazyRef = static_cast<SkLazyPixelRef*>(pixelRef);
scroggo@google.combb281f72013-03-18 21:37:39 +0000118 intptr_t cacheID = lazyRef->getCacheId();
119 REPORTER_ASSERT(reporter, cache->getMemoryStatus(cacheID)
120 != SkImageCache::kPinned_MemoryStatus);
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000121 {
122 SkAutoLockPixels alp(*bitmapFromFactory.get());
123 REPORTER_ASSERT(reporter, bitmapFromFactory->readyToDraw());
124 cacheID = lazyRef->getCacheId();
scroggo@google.combb281f72013-03-18 21:37:39 +0000125 REPORTER_ASSERT(reporter, cache->getMemoryStatus(cacheID)
126 == SkImageCache::kPinned_MemoryStatus);
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000127 }
128 REPORTER_ASSERT(reporter, !bitmapFromFactory->readyToDraw());
scroggo@google.combb281f72013-03-18 21:37:39 +0000129 REPORTER_ASSERT(reporter, cache->getMemoryStatus(cacheID)
130 != SkImageCache::kPinned_MemoryStatus);
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000131 bitmapFromFactory.free();
scroggo@google.combb281f72013-03-18 21:37:39 +0000132 REPORTER_ASSERT(reporter, cache->getMemoryStatus(cacheID)
133 == SkImageCache::kFreed_MemoryStatus);
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000134 }
135}
136
scroggo@google.combb281f72013-03-18 21:37:39 +0000137class ImageCacheHolder : public SkNoncopyable {
138
139public:
140 ~ImageCacheHolder() {
141 fCaches.safeUnrefAll();
142 }
143
144 void addImageCache(SkImageCache* cache) {
145 SkSafeRef(cache);
146 *fCaches.append() = cache;
147 }
148
149 int count() const { return fCaches.count(); }
150
151 SkImageCache* getAt(int i) {
152 if (i < 0 || i > fCaches.count()) {
153 return NULL;
154 }
155 return fCaches.getAt(i);
156 }
157
158private:
159 SkTDArray<SkImageCache*> fCaches;
160};
161
scroggo@google.com9f686f32012-11-29 21:05:37 +0000162static void TestBitmapFactory(skiatest::Reporter* reporter) {
163 SkAutoTDelete<SkBitmap> bitmap(create_bitmap());
164 SkASSERT(bitmap.get() != NULL);
165
166 SkAutoDataUnref encodedBitmap(create_data_from_bitmap(*bitmap.get()));
scroggo@google.combb281f72013-03-18 21:37:39 +0000167 bool encodeSucceeded = encodedBitmap.get() != NULL;
168 SkASSERT(encodeSucceeded);
169
170 ImageCacheHolder cacheHolder;
scroggo@google.com9f686f32012-11-29 21:05:37 +0000171
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000172 SkAutoTUnref<SkLruImageCache> lruCache(SkNEW_ARGS(SkLruImageCache, (1024 * 1024)));
scroggo@google.combb281f72013-03-18 21:37:39 +0000173 cacheHolder.addImageCache(lruCache);
174
175 cacheHolder.addImageCache(NULL);
176
177 SkImageCache* purgeableCache = SkPurgeableImageCache::Create();
178 if (purgeableCache != NULL) {
179 cacheHolder.addImageCache(purgeableCache);
180 purgeableCache->unref();
181 }
182
183 for (int i = 0; i < cacheHolder.count(); i++) {
184 SkImageCache* cache = cacheHolder.getAt(i);
185 if (cache != NULL) {
186 test_cache(reporter, cache);
187 }
188 if (encodeSucceeded) {
189 test_factory(reporter, cache, encodedBitmap, *bitmap.get());
190 }
191 }
scroggo@google.com9f686f32012-11-29 21:05:37 +0000192}
193
194#include "TestClassDef.h"
195DEFINE_TESTCLASS("BitmapFactory", TestBitmapFactoryClass, TestBitmapFactory)
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000196
197#endif // SK_DEBUG