OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCachingPixelRef.h" | 9 #include "SkCachingPixelRef.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkDiscardableMemoryPool.h" | 12 #include "SkDiscardableMemoryPool.h" |
| 13 #include "SkEncodedFormat.h" |
13 #include "SkImageDecoder.h" | 14 #include "SkImageDecoder.h" |
14 #include "SkImageGeneratorPriv.h" | 15 #include "SkImageGeneratorPriv.h" |
15 #include "SkResourceCache.h" | 16 #include "SkResourceCache.h" |
16 #include "SkStream.h" | 17 #include "SkStream.h" |
17 #include "SkUtils.h" | 18 #include "SkUtils.h" |
18 | 19 |
19 #include "Test.h" | 20 #include "Test.h" |
20 | 21 |
21 /** | 22 /** |
22 * Fill this bitmap with some color. | 23 * Fill this bitmap with some color. |
23 */ | 24 */ |
24 static void make_test_image(SkBitmap* bm) { | 25 static void make_test_image(SkBitmap* bm) { |
25 const int W = 50, H = 50; | 26 const int W = 50, H = 50; |
26 bm->allocN32Pixels(W, H); | 27 bm->allocN32Pixels(W, H); |
27 bm->eraseColor(SK_ColorBLACK); | 28 bm->eraseColor(SK_ColorBLACK); |
28 SkCanvas canvas(*bm); | 29 SkCanvas canvas(*bm); |
29 SkPaint paint; | 30 SkPaint paint; |
30 paint.setColor(SK_ColorBLUE); | 31 paint.setColor(SK_ColorBLUE); |
31 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), | 32 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), |
32 SkIntToScalar(H/2), paint); | 33 SkIntToScalar(H/2), paint); |
33 paint.setColor(SK_ColorWHITE); | 34 paint.setColor(SK_ColorWHITE); |
34 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2), | 35 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2), |
35 SkIntToScalar(W), SkIntToScalar(H), paint); | 36 SkIntToScalar(W), SkIntToScalar(H), paint); |
36 } | 37 } |
37 | 38 |
38 /** | 39 /** |
39 * encode this bitmap into some data via SkImageEncoder | 40 * encode this bitmap into some data via SkImageEncoder |
40 */ | 41 */ |
41 static SkData* create_data_from_bitmap(const SkBitmap& bm, | 42 static SkData* create_data_from_bitmap(const SkBitmap& bm, |
42 SkImageEncoder::Type type) { | 43 SkEncodedFormat type) { |
43 SkDynamicMemoryWStream stream; | 44 SkDynamicMemoryWStream stream; |
44 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { | 45 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { |
45 return stream.copyToData(); | 46 return stream.copyToData(); |
46 } | 47 } |
47 return NULL; | 48 return NULL; |
48 } | 49 } |
49 | 50 |
50 //////////////////////////////////////////////////////////////////////////////// | 51 //////////////////////////////////////////////////////////////////////////////// |
51 | 52 |
52 static void compare_bitmaps(skiatest::Reporter* reporter, | 53 static void compare_bitmaps(skiatest::Reporter* reporter, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 original bitmap */ | 94 original bitmap */ |
94 static void test_three_encodings(skiatest::Reporter* reporter, | 95 static void test_three_encodings(skiatest::Reporter* reporter, |
95 InstallEncoded install) { | 96 InstallEncoded install) { |
96 SkBitmap original; | 97 SkBitmap original; |
97 make_test_image(&original); | 98 make_test_image(&original); |
98 REPORTER_ASSERT(reporter, !original.empty()); | 99 REPORTER_ASSERT(reporter, !original.empty()); |
99 REPORTER_ASSERT(reporter, !original.isNull()); | 100 REPORTER_ASSERT(reporter, !original.isNull()); |
100 if (original.empty() || original.isNull()) { | 101 if (original.empty() || original.isNull()) { |
101 return; | 102 return; |
102 } | 103 } |
103 static const SkImageEncoder::Type types[] = { | 104 static const SkEncodedFormat types[] = { |
104 SkImageEncoder::kPNG_Type, | 105 kJPEG_SkEncodedFormat, |
105 SkImageEncoder::kJPEG_Type, | 106 kPNG_SkEncodedFormat, |
106 SkImageEncoder::kWEBP_Type | 107 kWEBP_SkEncodedFormat, |
107 }; | 108 }; |
108 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { | 109 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { |
109 SkImageEncoder::Type type = types[i]; | 110 SkEncodedFormat type = types[i]; |
110 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); | 111 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); |
111 REPORTER_ASSERT(reporter, encoded.get() != NULL); | 112 REPORTER_ASSERT(reporter, encoded.get() != NULL); |
112 if (NULL == encoded.get()) { | 113 if (NULL == encoded.get()) { |
113 continue; | 114 continue; |
114 } | 115 } |
115 SkBitmap lazy; | 116 SkBitmap lazy; |
116 bool installSuccess = install(encoded.get(), &lazy); | 117 bool installSuccess = install(encoded.get(), &lazy); |
117 REPORTER_ASSERT(reporter, installSuccess); | 118 REPORTER_ASSERT(reporter, installSuccess); |
118 if (!installSuccess) { | 119 if (!installSuccess) { |
119 continue; | 120 continue; |
120 } | 121 } |
121 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 122 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
122 { | 123 { |
123 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 124 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
124 REPORTER_ASSERT(reporter, lazy.getPixels()); | 125 REPORTER_ASSERT(reporter, lazy.getPixels()); |
125 if (NULL == lazy.getPixels()) { | 126 if (NULL == lazy.getPixels()) { |
126 continue; | 127 continue; |
127 } | 128 } |
128 } | 129 } |
129 // pixels should be gone! | 130 // pixels should be gone! |
130 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 131 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
131 { | 132 { |
132 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 133 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
133 REPORTER_ASSERT(reporter, lazy.getPixels()); | 134 REPORTER_ASSERT(reporter, lazy.getPixels()); |
134 if (NULL == lazy.getPixels()) { | 135 if (NULL == lazy.getPixels()) { |
135 continue; | 136 continue; |
136 } | 137 } |
137 } | 138 } |
138 bool comparePixels = (SkImageEncoder::kPNG_Type == type); | 139 bool comparePixels = (kPNG_SkEncodedFormat == type); |
139 compare_bitmaps(reporter, original, lazy, comparePixels); | 140 compare_bitmaps(reporter, original, lazy, comparePixels); |
140 } | 141 } |
141 } | 142 } |
142 | 143 |
143 //////////////////////////////////////////////////////////////////////////////// | 144 //////////////////////////////////////////////////////////////////////////////// |
144 static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { | 145 static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { |
145 return SkCachingPixelRef::Install(SkImageGenerator::NewFromData(encoded), ds
t); | 146 return SkCachingPixelRef::Install(SkImageGenerator::NewFromData(encoded), ds
t); |
146 } | 147 } |
147 static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { | 148 static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { |
148 // Use system-default discardable memory. | 149 // Use system-default discardable memory. |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 canvas.clear(kDefaultColor); | 349 canvas.clear(kDefaultColor); |
349 canvas.drawImage(image, 0, 0, NULL); | 350 canvas.drawImage(image, 0, 0, NULL); |
350 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { | 351 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { |
351 REPORTER_ASSERT( | 352 REPORTER_ASSERT( |
352 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); | 353 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); |
353 } else { | 354 } else { |
354 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); | 355 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); |
355 } | 356 } |
356 } | 357 } |
357 } | 358 } |
OLD | NEW |