blob: af1df1093c3469dc082195a1cd60a5a1ffd54a3f [file] [log] [blame]
commit-bot@chromium.org75854792013-10-29 19:55:00 +00001/*
2 * 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 "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkData.h"
halcanary@google.comad04eb42013-11-21 15:32:08 +000011#include "SkDecodingImageGenerator.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000012#include "SkForceLinking.h"
13#include "SkImageDecoder.h"
14#include "SkImagePriv.h"
15#include "SkLazyPixelRef.h"
halcanary@google.com36d08c52013-12-05 14:00:03 +000016#include "SkCachingPixelRef.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000017#include "SkScaledImageCache.h"
18#include "SkStream.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000019
commit-bot@chromium.org75854792013-10-29 19:55:00 +000020#include "Test.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000021#include "TestClassDef.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000022
23__SK_FORCE_IMAGE_DECODER_LINKING;
24
25/**
26 * Fill this bitmap with some color.
27 */
28static void make_test_image(SkBitmap* bm) {
29 static const int W = 50, H = 50;
30 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config;
31 bm->setConfig(config, W, H);
32 bm->allocPixels();
33 bm->eraseColor(SK_ColorBLACK);
34 SkCanvas canvas(*bm);
35 SkPaint paint;
36 paint.setColor(SK_ColorBLUE);
37 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2),
38 SkIntToScalar(H/2), paint);
39 paint.setColor(SK_ColorWHITE);
40 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
41 SkIntToScalar(W), SkIntToScalar(H), paint);
42}
43
44/**
45 * encode this bitmap into some data via SkImageEncoder
46 */
47static SkData* create_data_from_bitmap(const SkBitmap& bm,
48 SkImageEncoder::Type type) {
49 SkDynamicMemoryWStream stream;
50 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
51 return stream.copyToData();
52 }
53 return NULL;
54}
55
56/**
57 * A simplified version of SkBitmapFactory
58 */
59static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc,
60 SkData* data,
61 SkBitmap* dst) {
reed@google.com2bd8b812013-11-01 13:46:54 +000062 SkImageInfo info;
commit-bot@chromium.org75854792013-10-29 19:55:00 +000063 if (!proc(data->data(), data->size(), &info, NULL)) {
64 return false;
65 }
66 dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
67 info.fHeight, 0, info.fAlphaType);
68 SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef,
69 (data, proc, NULL)));
70 dst->setPixelRef(ref);
71 return true;
72}
73
74static void compare_bitmaps(skiatest::Reporter* reporter,
75 const SkBitmap& b1, const SkBitmap& b2,
76 bool pixelPerfect = true) {
77 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
78 REPORTER_ASSERT(reporter, b1.width() == b2.width());
79 REPORTER_ASSERT(reporter, b1.height() == b2.height());
80 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
81 SkAutoLockPixels autoLockPixels1(b1);
82 SkAutoLockPixels autoLockPixels2(b2);
83 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
84 if (b1.isNull() || b1.empty()) {
85 return;
86 }
87 REPORTER_ASSERT(reporter, NULL != b1.getPixels());
88 REPORTER_ASSERT(reporter, NULL != b2.getPixels());
89 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
90 return;
91 }
92 if ((b1.width() != b2.width()) ||
93 (b1.height() != b2.height())) {
94 return;
95 }
96 if (!pixelPerfect) {
97 return;
98 }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000099
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000100 int pixelErrors = 0;
101 for (int y = 0; y < b2.height(); ++y) {
102 for (int x = 0; x < b2.width(); ++x) {
103 if (b1.getColor(x, y) != b2.getColor(x, y)) {
104 ++pixelErrors;
105 }
106 }
107 }
108 REPORTER_ASSERT(reporter, 0 == pixelErrors);
109}
110
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000111
halcanary@google.com36d08c52013-12-05 14:00:03 +0000112typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
113
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000114/**
halcanary@google.com36d08c52013-12-05 14:00:03 +0000115 This function tests three differently encoded images against the
116 original bitmap */
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000117static void test_three_encodings(skiatest::Reporter* reporter,
halcanary@google.com36d08c52013-12-05 14:00:03 +0000118 InstallEncoded install) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000119 SkBitmap original;
120 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000121 REPORTER_ASSERT(reporter, !original.empty());
122 REPORTER_ASSERT(reporter, !original.isNull());
123 if (original.empty() || original.isNull()) {
124 return;
125 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000126 static const SkImageEncoder::Type types[] = {
127 SkImageEncoder::kPNG_Type,
128 SkImageEncoder::kJPEG_Type,
129 SkImageEncoder::kWEBP_Type
130 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000131 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
132 SkImageEncoder::Type type = types[i];
133 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
134 REPORTER_ASSERT(reporter, encoded.get() != NULL);
halcanary@google.com36d08c52013-12-05 14:00:03 +0000135 if (NULL == encoded.get()) {
136 continue;
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000137 }
halcanary@google.com36d08c52013-12-05 14:00:03 +0000138 SkBitmap lazy;
139 bool installSuccess = install(encoded.get(), &lazy);
140 REPORTER_ASSERT(reporter, installSuccess);
141 if (!installSuccess) {
142 continue;
143 }
144 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
145 {
146 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
147 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
148 if (NULL == lazy.getPixels()) {
149 continue;
150 }
151 }
152 // pixels should be gone!
153 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
154 {
155 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
156 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
157 if (NULL == lazy.getPixels()) {
158 continue;
159 }
160 }
161 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
162 compare_bitmaps(reporter, original, lazy, comparePixels);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000163 }
164}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000165
halcanary@google.com36d08c52013-12-05 14:00:03 +0000166
167////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000168/**
169 * This checks to see that a SkLazyPixelRef works as advertised.
170 */
halcanary@google.com36d08c52013-12-05 14:00:03 +0000171bool install_skLazyPixelRef(SkData* encoded, SkBitmap* dst) {
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000172 static const SkBitmapFactory::DecodeProc decoder =
173 &(SkImageDecoder::DecodeMemoryToTarget);
halcanary@google.com36d08c52013-12-05 14:00:03 +0000174 return simple_bitmap_factory(decoder, encoded, dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000175}
176DEF_TEST(LazyPixelRef, reporter) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000177 test_three_encodings(reporter, install_skLazyPixelRef);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000178}
179
halcanary@google.com36d08c52013-12-05 14:00:03 +0000180////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000181/**
halcanary@google.com36d08c52013-12-05 14:00:03 +0000182 * This checks to see that a SkCachingPixelRef works as advertised.
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000183 */
halcanary@google.com36d08c52013-12-05 14:00:03 +0000184bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
185 return SkCachingPixelRef::Install(
186 SkNEW_ARGS(SkDecodingImageGenerator, (encoded)), dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000187}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000188DEF_TEST(CachingPixelRef, reporter) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000189 test_three_encodings(reporter, install_skCachingPixelRef);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000190}
halcanary@google.comad04eb42013-11-21 15:32:08 +0000191
halcanary@google.com36d08c52013-12-05 14:00:03 +0000192////////////////////////////////////////////////////////////////////////////////
193/**
194 * This checks to see that a SkDecodingImageGenerator works as advertised.
195 */
halcanary@google.comad04eb42013-11-21 15:32:08 +0000196DEF_TEST(DecodingImageGenerator, reporter) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000197 test_three_encodings(reporter, SkDecodingImageGenerator::Install);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000198}