commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 11 | #include "SkDecodingImageGenerator.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 12 | #include "SkForceLinking.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkImagePriv.h" |
| 15 | #include "SkLazyPixelRef.h" |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 16 | #include "SkCachingPixelRef.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 17 | #include "SkScaledImageCache.h" |
| 18 | #include "SkStream.h" |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 19 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 20 | #include "Test.h" |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 21 | #include "TestClassDef.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 22 | |
| 23 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 24 | |
| 25 | /** |
| 26 | * Fill this bitmap with some color. |
| 27 | */ |
| 28 | static 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 | */ |
| 47 | static 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 | */ |
| 59 | static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc, |
| 60 | SkData* data, |
| 61 | SkBitmap* dst) { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 62 | SkImageInfo info; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 63 | 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 | |
| 74 | static 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.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 99 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 100 | 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.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 111 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 112 | typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst); |
| 113 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 114 | /** |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 115 | This function tests three differently encoded images against the |
| 116 | original bitmap */ |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 117 | static void test_three_encodings(skiatest::Reporter* reporter, |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 118 | InstallEncoded install) { |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 119 | SkBitmap original; |
| 120 | make_test_image(&original); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 121 | REPORTER_ASSERT(reporter, !original.empty()); |
| 122 | REPORTER_ASSERT(reporter, !original.isNull()); |
| 123 | if (original.empty() || original.isNull()) { |
| 124 | return; |
| 125 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 126 | static const SkImageEncoder::Type types[] = { |
| 127 | SkImageEncoder::kPNG_Type, |
| 128 | SkImageEncoder::kJPEG_Type, |
| 129 | SkImageEncoder::kWEBP_Type |
| 130 | }; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 131 | 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.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 135 | if (NULL == encoded.get()) { |
| 136 | continue; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 137 | } |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 138 | 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.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 165 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 166 | |
| 167 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 168 | /** |
| 169 | * This checks to see that a SkLazyPixelRef works as advertised. |
| 170 | */ |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 171 | bool install_skLazyPixelRef(SkData* encoded, SkBitmap* dst) { |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 172 | static const SkBitmapFactory::DecodeProc decoder = |
| 173 | &(SkImageDecoder::DecodeMemoryToTarget); |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 174 | return simple_bitmap_factory(decoder, encoded, dst); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 175 | } |
| 176 | DEF_TEST(LazyPixelRef, reporter) { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 177 | test_three_encodings(reporter, install_skLazyPixelRef); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 178 | } |
| 179 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 180 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 181 | /** |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 182 | * This checks to see that a SkCachingPixelRef works as advertised. |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 183 | */ |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 184 | bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { |
| 185 | return SkCachingPixelRef::Install( |
| 186 | SkNEW_ARGS(SkDecodingImageGenerator, (encoded)), dst); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 187 | } |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 188 | DEF_TEST(CachingPixelRef, reporter) { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 189 | test_three_encodings(reporter, install_skCachingPixelRef); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 190 | } |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 191 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 192 | //////////////////////////////////////////////////////////////////////////////// |
| 193 | /** |
| 194 | * This checks to see that a SkDecodingImageGenerator works as advertised. |
| 195 | */ |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 196 | DEF_TEST(DecodingImageGenerator, reporter) { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame^] | 197 | test_three_encodings(reporter, SkDecodingImageGenerator::Install); |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 198 | } |