aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/CachedDecodingPixelRefTest.cpp
blob: 2d8a0e70737a66c376987caf9c2adb055026f054 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkData.h"
#include "SkDecodingImageGenerator.h"
#include "SkForceLinking.h"
#include "SkImageDecoder.h"
#include "SkImagePriv.h"
#include "SkLazyCachingPixelRef.h"
#include "SkLazyPixelRef.h"
#include "SkScaledImageCache.h"
#include "SkStream.h"

#include "Test.h"
#include "TestClassDef.h"

__SK_FORCE_IMAGE_DECODER_LINKING;

/**
 * Fill this bitmap with some color.
 */
static void make_test_image(SkBitmap* bm) {
    static const int W = 50, H = 50;
    static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config;
    bm->setConfig(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);
    paint.setColor(SK_ColorWHITE);
    canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
                          SkIntToScalar(W), SkIntToScalar(H), paint);
}

/**
 * encode this bitmap into some data via SkImageEncoder
 */
static SkData* create_data_from_bitmap(const SkBitmap& bm,
                                       SkImageEncoder::Type type) {
    SkDynamicMemoryWStream stream;
    if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
        return stream.copyToData();
    }
    return NULL;
}

/**
 *  A simplified version of SkBitmapFactory
 */
static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc,
                                  SkData* data,
                                  SkBitmap* dst) {
    SkImageInfo info;
    if (!proc(data->data(), data->size(), &info, NULL)) {
        return false;
    }
    dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
                    info.fHeight, 0, info.fAlphaType);
    SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef,
                                                (data, proc, NULL)));
    dst->setPixelRef(ref);
    return true;
}

static void compare_bitmaps(skiatest::Reporter* reporter,
                            const SkBitmap& b1, const SkBitmap& b2,
                            bool pixelPerfect = true) {
    REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
    REPORTER_ASSERT(reporter, b1.width() == b2.width());
    REPORTER_ASSERT(reporter, b1.height() == b2.height());
    REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
    SkAutoLockPixels autoLockPixels1(b1);
    SkAutoLockPixels autoLockPixels2(b2);
    REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
    if (b1.isNull() || b1.empty()) {
        return;
    }
    REPORTER_ASSERT(reporter, NULL != b1.getPixels());
    REPORTER_ASSERT(reporter, NULL != b2.getPixels());
    if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
        return;
    }
    if ((b1.width() != b2.width()) ||
        (b1.height() != b2.height())) {
        return;
    }
    if (!pixelPerfect) {
        return;
    }

    int pixelErrors = 0;
    for (int y = 0; y < b2.height(); ++y) {
        for (int x = 0; x < b2.width(); ++x) {
            if (b1.getColor(x, y) != b2.getColor(x, y)) {
                ++pixelErrors;
            }
        }
    }
    REPORTER_ASSERT(reporter, 0 == pixelErrors);
}


typedef void(*CompareEncodedToOriginal)(skiatest::Reporter* reporter,
                                        SkData* encoded,
                                        const SkBitmap& original,
                                        bool pixelPerfect);
/**
   this function tests three differently encoded images against the
   original bitmap  */
static void test_three_encodings(skiatest::Reporter* reporter,
                                 CompareEncodedToOriginal comp) {
    SkBitmap original;
    make_test_image(&original);
    REPORTER_ASSERT(reporter, !original.empty());
    REPORTER_ASSERT(reporter, !original.isNull());
    if (original.empty() || original.isNull()) {
        return;
    }
    static const SkImageEncoder::Type types[] = {
        SkImageEncoder::kPNG_Type,
        SkImageEncoder::kJPEG_Type,
        SkImageEncoder::kWEBP_Type
    };
    for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
        SkImageEncoder::Type type = types[i];
        SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
        REPORTER_ASSERT(reporter, encoded.get() != NULL);
        if (NULL != encoded.get()) {
            bool comparePixels = (SkImageEncoder::kPNG_Type == type);
            comp(reporter, encoded, original, comparePixels);
        }
    }
}

/**
 *  This checks to see that a SkLazyPixelRef works as advertised.
 */
static void compare_with_skLazyPixelRef(skiatest::Reporter* reporter,
                                        SkData* encoded,
                                        const SkBitmap& original,
                                        bool comparePixels) {
    SkBitmap lazy;
    static const SkBitmapFactory::DecodeProc decoder =
        &(SkImageDecoder::DecodeMemoryToTarget);
    bool success = simple_bitmap_factory(decoder, encoded, &lazy);
    REPORTER_ASSERT(reporter, success);

    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
    {
        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
    }
    // pixels should be gone!
    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
    {
        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
    }
    compare_bitmaps(reporter, original, lazy, comparePixels);
}
DEF_TEST(LazyPixelRef, reporter) {
    test_three_encodings(reporter, compare_with_skLazyPixelRef);
}



/**
 *  This checks to see that a SkLazyCachedPixelRef works as advertised.
 */

static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter,
                                              SkData* encoded,
                                              const SkBitmap& original,
                                              bool comparePixels) {
    SkBitmap lazy;
    static const SkBitmapFactory::DecodeProc decoder =
        &(SkImageDecoder::DecodeMemoryToTarget);
    bool success = SkLazyCachingPixelRef::Install(decoder, encoded, &lazy);
    REPORTER_ASSERT(reporter, success);

    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
    {
        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
    }
    // pixels should be gone!
    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
    {
        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
    }
    compare_bitmaps(reporter, original, lazy, comparePixels);
}
DEF_TEST(LazyCachedPixelRef, reporter) {
    test_three_encodings(reporter, compare_with_skLazyCachedPixelRef);
}

class TestPixelRef : public SkCachingPixelRef {
public:
    TestPixelRef(int x) : fX(x) { }
    virtual ~TestPixelRef() { }
    static bool Install(SkBitmap* destination, int x) {
        SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x)));
        return ref->configure(destination) && destination->setPixelRef(ref);
    }
    SK_DECLARE_UNFLATTENABLE_OBJECT()
protected:
    virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE {
        if (fX == 0) {
            return false;
        }
        SkASSERT(info);
        info->fWidth = 10;
        info->fHeight = 10;
        info->fColorType = kRGBA_8888_SkColorType;
        info->fAlphaType = kOpaque_SkAlphaType;
        return true;
    }
    virtual bool onDecodePixels(const SkImageInfo& info,
                                void* pixels,
                                size_t rowBytes) SK_OVERRIDE {
        return false;
    }
private:
    int fX;  // controls where the failure happens
    typedef SkCachingPixelRef INHERITED;
};

DEF_TEST(CachingPixelRef, reporter) {
    SkBitmap lazy;
    // test the error handling
    REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0));
    // onDecodeInfo should succeed, allowing installation
    REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1));
    SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
    // onDecodePixels should fail, so getting pixels will fail.
    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
}

static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter,
                                                  SkData* encoded,
                                                  const SkBitmap& original,
                                                  bool comparePixels) {

    SkBitmap lazy;
    bool success = SkDecodingImageGenerator::Install(encoded, &lazy);
    REPORTER_ASSERT(reporter, success);
    if (!success) {
        return;
    }

    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
    {
        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
        if (NULL == lazy.getPixels()) {
            return;
        }
    }
    // pixels should be gone!
    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
    {
        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
    }
    compare_bitmaps(reporter, original, lazy, comparePixels);
}
DEF_TEST(DecodingImageGenerator, reporter) {
    test_three_encodings(reporter, compare_with_SkDecodingImageGenerator);
}