aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/BitmapFactoryTest.cpp
blob: 9f1fa478fda036712d35784be0974c72cb816f5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifdef SK_DEBUG

#include "SkBitmap.h"
#include "SkBitmapFactory.h"
#include "SkCanvas.h"
#include "SkColor.h"
#include "SkData.h"
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkLazyPixelRef.h"
#include "SkLruImageCache.h"
#include "SkPaint.h"
#include "SkPurgeableImageCache.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "Test.h"

static SkBitmap* create_bitmap() {
    SkBitmap* bm = SkNEW(SkBitmap);
    // Use a large bitmap.
    const int W = 1000, H = 1000;
    bm->setConfig(SkBitmap::kARGB_8888_Config, W, H);
    bm->allocPixels();
    bm->eraseColor(SK_ColorBLACK);
    SkCanvas canvas(*bm);
    SkPaint paint;
    paint.setColor(SK_ColorBLUE);
    canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), SkIntToScalar(H/2), paint);
    return bm;
}

static SkData* create_data_from_bitmap(const SkBitmap& bm) {
    SkDynamicMemoryWStream stream;
    if (SkImageEncoder::EncodeStream(&stream, bm, SkImageEncoder::kPNG_Type, 100)) {
        return stream.copyToData();
    }
    return NULL;
}

static void assert_bounds_equal(skiatest::Reporter* reporter, const SkBitmap& bm1,
                                const SkBitmap& bm2) {
    REPORTER_ASSERT(reporter, bm1.width() == bm2.width());
    REPORTER_ASSERT(reporter, bm1.height() == bm2.height());
}

static void test_cache(skiatest::Reporter* reporter, SkImageCache* cache) {
    // Test the cache directly:
    cache->purgeAllUnpinnedCaches();
    intptr_t ID = SkImageCache::UNINITIALIZED_ID;
    const size_t size = 1000;
    char buffer[size];
    sk_bzero((void*) buffer, size);
    void* memory = cache->allocAndPinCache(size, &ID);
    if (memory != NULL) {
        memcpy(memory, (void*)buffer, size);
        REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID) == SkImageCache::kPinned_MemoryStatus);
        cache->releaseCache(ID);
        REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID) != SkImageCache::kPinned_MemoryStatus);
        SkImageCache::DataStatus dataStatus;
        memory = cache->pinCache(ID, &dataStatus);
        if (memory != NULL) {
            REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
                                      == SkImageCache::kPinned_MemoryStatus);
            if (SkImageCache::kRetained_DataStatus == dataStatus) {
                REPORTER_ASSERT(reporter, !memcmp(memory, (void*) buffer, size));
            }
            cache->releaseCache(ID);
            REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
                                      != SkImageCache::kPinned_MemoryStatus);
            cache->purgeAllUnpinnedCaches();
            REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
                                      != SkImageCache::kPinned_MemoryStatus);
            memory = cache->pinCache(ID, &dataStatus);
            if (memory != NULL) {
                // The memory block may or may not have survived the purging (at the
                // memory manager's whim) so we cannot check dataStatus here.
                cache->releaseCache(ID);
                REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
                                          != SkImageCache::kPinned_MemoryStatus);
                cache->throwAwayCache(ID);
                REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
                                          == SkImageCache::kFreed_MemoryStatus);
            } else {
                REPORTER_ASSERT(reporter, cache->getMemoryStatus(ID)
                                          == SkImageCache::kFreed_MemoryStatus);
            }
        }
    }
}

static void test_factory(skiatest::Reporter* reporter, SkImageCache* cache, SkData* encodedData,
                       const SkBitmap& origBitmap) {
    SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
    factory.setImageCache(cache);
    SkAutoTDelete<SkBitmap> bitmapFromFactory(SkNEW(SkBitmap));
    bool success = factory.installPixelRef(encodedData, bitmapFromFactory.get());
    // This assumes that if the encoder worked, the decoder should also work, so the above call
    // should not fail.
    REPORTER_ASSERT(reporter, success);
    assert_bounds_equal(reporter, origBitmap, *bitmapFromFactory.get());

    SkPixelRef* pixelRef = bitmapFromFactory->pixelRef();
    REPORTER_ASSERT(reporter, pixelRef != NULL);
    if (NULL == cache) {
        // This assumes that installPixelRef called lockPixels.
        REPORTER_ASSERT(reporter, bitmapFromFactory->readyToDraw());
    } else {
        // Lazy decoding
        REPORTER_ASSERT(reporter, !bitmapFromFactory->readyToDraw());
        SkLazyPixelRef* lazyRef = static_cast<SkLazyPixelRef*>(pixelRef);
        intptr_t cacheID = lazyRef->getCacheId();
        REPORTER_ASSERT(reporter, cache->getMemoryStatus(cacheID)
                                  != SkImageCache::kPinned_MemoryStatus);
        {
            SkAutoLockPixels alp(*bitmapFromFactory.get());
            REPORTER_ASSERT(reporter, bitmapFromFactory->readyToDraw());
            cacheID = lazyRef->getCacheId();
            REPORTER_ASSERT(reporter, cache->getMemoryStatus(cacheID)
                                      == SkImageCache::kPinned_MemoryStatus);
        }
        REPORTER_ASSERT(reporter, !bitmapFromFactory->readyToDraw());
        REPORTER_ASSERT(reporter, cache->getMemoryStatus(cacheID)
                                  != SkImageCache::kPinned_MemoryStatus);
        bitmapFromFactory.free();
        REPORTER_ASSERT(reporter, cache->getMemoryStatus(cacheID)
                                  == SkImageCache::kFreed_MemoryStatus);
    }
}

class ImageCacheHolder : public SkNoncopyable {

public:
    ~ImageCacheHolder() {
        fCaches.safeUnrefAll();
    }

    void addImageCache(SkImageCache* cache) {
        SkSafeRef(cache);
        *fCaches.append() = cache;
    }

    int count() const { return fCaches.count(); }

    SkImageCache* getAt(int i) {
        if (i < 0 || i > fCaches.count()) {
            return NULL;
        }
        return fCaches.getAt(i);
    }

private:
    SkTDArray<SkImageCache*> fCaches;
};

static void TestBitmapFactory(skiatest::Reporter* reporter) {
    SkAutoTDelete<SkBitmap> bitmap(create_bitmap());
    SkASSERT(bitmap.get() != NULL);

    SkAutoDataUnref encodedBitmap(create_data_from_bitmap(*bitmap.get()));
    bool encodeSucceeded = encodedBitmap.get() != NULL;
    SkASSERT(encodeSucceeded);

    ImageCacheHolder cacheHolder;

    SkAutoTUnref<SkLruImageCache> lruCache(SkNEW_ARGS(SkLruImageCache, (1024 * 1024)));
    cacheHolder.addImageCache(lruCache);

    cacheHolder.addImageCache(NULL);

    SkImageCache* purgeableCache = SkPurgeableImageCache::Create();
    if (purgeableCache != NULL) {
        cacheHolder.addImageCache(purgeableCache);
        purgeableCache->unref();
    }

    for (int i = 0; i < cacheHolder.count(); i++) {
        SkImageCache* cache = cacheHolder.getAt(i);
        if (cache != NULL) {
            test_cache(reporter, cache);
        }
        if (encodeSucceeded) {
            test_factory(reporter, cache, encodedBitmap, *bitmap.get());
        }
    }
}

#include "TestClassDef.h"
DEFINE_TESTCLASS("BitmapFactory", TestBitmapFactoryClass, TestBitmapFactory)

#endif // SK_DEBUG