| 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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * encode this bitmap into some data via SkImageEncoder | 39 * encode this bitmap into some data via SkImageEncoder |
| 40 */ | 40 */ |
| 41 static SkData* create_data_from_bitmap(const SkBitmap& bm, | 41 static SkData* create_data_from_bitmap(const SkBitmap& bm, |
| 42 SkImageEncoder::Type type) { | 42 SkImageEncoder::Type type) { |
| 43 SkDynamicMemoryWStream stream; | 43 SkDynamicMemoryWStream stream; |
| 44 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { | 44 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { |
| 45 return stream.copyToData(); | 45 return stream.copyToData(); |
| 46 } | 46 } |
| 47 return NULL; | 47 return nullptr; |
| 48 } | 48 } |
| 49 | 49 |
| 50 //////////////////////////////////////////////////////////////////////////////// | 50 //////////////////////////////////////////////////////////////////////////////// |
| 51 | 51 |
| 52 static void compare_bitmaps(skiatest::Reporter* reporter, | 52 static void compare_bitmaps(skiatest::Reporter* reporter, |
| 53 const SkBitmap& b1, const SkBitmap& b2, | 53 const SkBitmap& b1, const SkBitmap& b2, |
| 54 bool pixelPerfect = true) { | 54 bool pixelPerfect = true) { |
| 55 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); | 55 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); |
| 56 REPORTER_ASSERT(reporter, b1.width() == b2.width()); | 56 REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 57 REPORTER_ASSERT(reporter, b1.height() == b2.height()); | 57 REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 return; | 101 return; |
| 102 } | 102 } |
| 103 static const SkImageEncoder::Type types[] = { | 103 static const SkImageEncoder::Type types[] = { |
| 104 SkImageEncoder::kPNG_Type, | 104 SkImageEncoder::kPNG_Type, |
| 105 SkImageEncoder::kJPEG_Type, | 105 SkImageEncoder::kJPEG_Type, |
| 106 SkImageEncoder::kWEBP_Type | 106 SkImageEncoder::kWEBP_Type |
| 107 }; | 107 }; |
| 108 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { | 108 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { |
| 109 SkImageEncoder::Type type = types[i]; | 109 SkImageEncoder::Type type = types[i]; |
| 110 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); | 110 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); |
| 111 REPORTER_ASSERT(reporter, encoded.get() != NULL); | 111 REPORTER_ASSERT(reporter, encoded.get() != nullptr); |
| 112 if (NULL == encoded.get()) { | 112 if (nullptr == encoded.get()) { |
| 113 continue; | 113 continue; |
| 114 } | 114 } |
| 115 SkBitmap lazy; | 115 SkBitmap lazy; |
| 116 bool installSuccess = install(encoded.get(), &lazy); | 116 bool installSuccess = install(encoded.get(), &lazy); |
| 117 REPORTER_ASSERT(reporter, installSuccess); | 117 REPORTER_ASSERT(reporter, installSuccess); |
| 118 if (!installSuccess) { | 118 if (!installSuccess) { |
| 119 continue; | 119 continue; |
| 120 } | 120 } |
| 121 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 121 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels()); |
| 122 { | 122 { |
| 123 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 123 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 124 REPORTER_ASSERT(reporter, lazy.getPixels()); | 124 REPORTER_ASSERT(reporter, lazy.getPixels()); |
| 125 if (NULL == lazy.getPixels()) { | 125 if (nullptr == lazy.getPixels()) { |
| 126 continue; | 126 continue; |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 // pixels should be gone! | 129 // pixels should be gone! |
| 130 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 130 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels()); |
| 131 { | 131 { |
| 132 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 132 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 133 REPORTER_ASSERT(reporter, lazy.getPixels()); | 133 REPORTER_ASSERT(reporter, lazy.getPixels()); |
| 134 if (NULL == lazy.getPixels()) { | 134 if (nullptr == lazy.getPixels()) { |
| 135 continue; | 135 continue; |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 bool comparePixels = (SkImageEncoder::kPNG_Type == type); | 138 bool comparePixels = (SkImageEncoder::kPNG_Type == type); |
| 139 compare_bitmaps(reporter, original, lazy, comparePixels); | 139 compare_bitmaps(reporter, original, lazy, comparePixels); |
| 140 } | 140 } |
| 141 } | 141 } |
| 142 | 142 |
| 143 //////////////////////////////////////////////////////////////////////////////// | 143 //////////////////////////////////////////////////////////////////////////////// |
| 144 static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { | 144 static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 virtual ~TestImageGenerator() { } | 177 virtual ~TestImageGenerator() { } |
| 178 | 178 |
| 179 protected: | 179 protected: |
| 180 static SkImageInfo GetMyInfo() { | 180 static SkImageInfo GetMyInfo() { |
| 181 return SkImageInfo::MakeN32(TestImageGenerator::Width(), TestImageGenera
tor::Height(), | 181 return SkImageInfo::MakeN32(TestImageGenerator::Width(), TestImageGenera
tor::Height(), |
| 182 kOpaque_SkAlphaType); | 182 kOpaque_SkAlphaType); |
| 183 } | 183 } |
| 184 | 184 |
| 185 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, | 185 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 186 SkPMColor ctable[], int* ctableCount) override { | 186 SkPMColor ctable[], int* ctableCount) override { |
| 187 REPORTER_ASSERT(fReporter, pixels != NULL); | 187 REPORTER_ASSERT(fReporter, pixels != nullptr); |
| 188 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); | 188 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); |
| 189 if (fType != kSucceedGetPixels_TestType) { | 189 if (fType != kSucceedGetPixels_TestType) { |
| 190 return false; | 190 return false; |
| 191 } | 191 } |
| 192 if (info.colorType() != kN32_SkColorType) { | 192 if (info.colorType() != kN32_SkColorType) { |
| 193 return false; | 193 return false; |
| 194 } | 194 } |
| 195 char* bytePtr = static_cast<char*>(pixels); | 195 char* bytePtr = static_cast<char*>(pixels); |
| 196 for (int y = 0; y < info.height(); ++y) { | 196 for (int y = 0; y < info.height(); ++y) { |
| 197 sk_memset32(reinterpret_cast<SkColor*>(bytePtr), | 197 sk_memset32(reinterpret_cast<SkColor*>(bytePtr), |
| 198 TestImageGenerator::Color(), info.width()); | 198 TestImageGenerator::Color(), info.width()); |
| 199 bytePtr += rowBytes; | 199 bytePtr += rowBytes; |
| 200 } | 200 } |
| 201 return true; | 201 return true; |
| 202 } | 202 } |
| 203 | 203 |
| 204 private: | 204 private: |
| 205 const TestType fType; | 205 const TestType fType; |
| 206 skiatest::Reporter* const fReporter; | 206 skiatest::Reporter* const fReporter; |
| 207 | 207 |
| 208 typedef SkImageGenerator INHERITED; | 208 typedef SkImageGenerator INHERITED; |
| 209 }; | 209 }; |
| 210 | 210 |
| 211 static void check_test_image_generator_bitmap(skiatest::Reporter* reporter, | 211 static void check_test_image_generator_bitmap(skiatest::Reporter* reporter, |
| 212 const SkBitmap& bm) { | 212 const SkBitmap& bm) { |
| 213 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); | 213 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); |
| 214 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); | 214 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); |
| 215 SkAutoLockPixels autoLockPixels(bm); | 215 SkAutoLockPixels autoLockPixels(bm); |
| 216 REPORTER_ASSERT(reporter, bm.getPixels()); | 216 REPORTER_ASSERT(reporter, bm.getPixels()); |
| 217 if (NULL == bm.getPixels()) { | 217 if (nullptr == bm.getPixels()) { |
| 218 return; | 218 return; |
| 219 } | 219 } |
| 220 int errors = 0; | 220 int errors = 0; |
| 221 for (int y = 0; y < bm.height(); ++y) { | 221 for (int y = 0; y < bm.height(); ++y) { |
| 222 for (int x = 0; x < bm.width(); ++x) { | 222 for (int x = 0; x < bm.width(); ++x) { |
| 223 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) { | 223 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) { |
| 224 ++errors; | 224 ++errors; |
| 225 } | 225 } |
| 226 } | 226 } |
| 227 } | 227 } |
| 228 REPORTER_ASSERT(reporter, 0 == errors); | 228 REPORTER_ASSERT(reporter, 0 == errors); |
| 229 } | 229 } |
| 230 | 230 |
| 231 enum PixelRefType { | 231 enum PixelRefType { |
| 232 kSkCaching_PixelRefType, | 232 kSkCaching_PixelRefType, |
| 233 kSkDiscardable_PixelRefType, | 233 kSkDiscardable_PixelRefType, |
| 234 kLast_PixelRefType = kSkDiscardable_PixelRefType | 234 kLast_PixelRefType = kSkDiscardable_PixelRefType |
| 235 }; | 235 }; |
| 236 | 236 |
| 237 static void check_pixelref(TestImageGenerator::TestType type, | 237 static void check_pixelref(TestImageGenerator::TestType type, |
| 238 skiatest::Reporter* reporter, | 238 skiatest::Reporter* reporter, |
| 239 PixelRefType pixelRefType, | 239 PixelRefType pixelRefType, |
| 240 SkDiscardableMemory::Factory* factory) { | 240 SkDiscardableMemory::Factory* factory) { |
| 241 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType)); | 241 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType)); |
| 242 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter)); | 242 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter)); |
| 243 REPORTER_ASSERT(reporter, gen.get() != NULL); | 243 REPORTER_ASSERT(reporter, gen.get() != nullptr); |
| 244 SkBitmap lazy; | 244 SkBitmap lazy; |
| 245 bool success; | 245 bool success; |
| 246 if (kSkCaching_PixelRefType == pixelRefType) { | 246 if (kSkCaching_PixelRefType == pixelRefType) { |
| 247 // Ignore factory; use global cache. | 247 // Ignore factory; use global cache. |
| 248 success = SkCachingPixelRef::Install(gen.detach(), &lazy); | 248 success = SkCachingPixelRef::Install(gen.detach(), &lazy); |
| 249 } else { | 249 } else { |
| 250 success = SkInstallDiscardablePixelRef(gen.detach(), NULL, &lazy, factor
y); | 250 success = SkInstallDiscardablePixelRef(gen.detach(), nullptr, &lazy, fac
tory); |
| 251 } | 251 } |
| 252 REPORTER_ASSERT(reporter, success); | 252 REPORTER_ASSERT(reporter, success); |
| 253 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { | 253 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { |
| 254 check_test_image_generator_bitmap(reporter, lazy); | 254 check_test_image_generator_bitmap(reporter, lazy); |
| 255 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { | 255 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { |
| 256 SkAutoLockPixels autoLockPixels(lazy); | 256 SkAutoLockPixels autoLockPixels(lazy); |
| 257 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 257 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels()); |
| 258 } | 258 } |
| 259 } | 259 } |
| 260 | 260 |
| 261 // new/lock/delete is an odd pattern for a pixelref, but it needs to not assert | 261 // new/lock/delete is an odd pattern for a pixelref, but it needs to not assert |
| 262 static void test_newlockdelete(skiatest::Reporter* reporter) { | 262 static void test_newlockdelete(skiatest::Reporter* reporter) { |
| 263 #ifdef SK_SUPPORT_LEGACY_UNBALANCED_PIXELREF_LOCKCOUNT | 263 #ifdef SK_SUPPORT_LEGACY_UNBALANCED_PIXELREF_LOCKCOUNT |
| 264 SkBitmap bm; | 264 SkBitmap bm; |
| 265 SkImageGenerator* ig = new TestImageGenerator( | 265 SkImageGenerator* ig = new TestImageGenerator( |
| 266 TestImageGenerator::kSucceedGetPixels_TestType, reporter); | 266 TestImageGenerator::kSucceedGetPixels_TestType, reporter); |
| 267 SkInstallDiscardablePixelRef(ig, &bm); | 267 SkInstallDiscardablePixelRef(ig, &bm); |
| 268 bm.pixelRef()->lockPixels(); | 268 bm.pixelRef()->lockPixels(); |
| 269 #endif | 269 #endif |
| 270 } | 270 } |
| 271 | 271 |
| 272 /** | 272 /** |
| 273 * This tests the basic functionality of SkDiscardablePixelRef with a | 273 * This tests the basic functionality of SkDiscardablePixelRef with a |
| 274 * basic SkImageGenerator implementation and several | 274 * basic SkImageGenerator implementation and several |
| 275 * SkDiscardableMemory::Factory choices. | 275 * SkDiscardableMemory::Factory choices. |
| 276 */ | 276 */ |
| 277 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { | 277 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { |
| 278 test_newlockdelete(reporter); | 278 test_newlockdelete(reporter); |
| 279 | 279 |
| 280 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 280 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 281 reporter, kSkCaching_PixelRefType, NULL); | 281 reporter, kSkCaching_PixelRefType, nullptr); |
| 282 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 282 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 283 reporter, kSkCaching_PixelRefType, NULL); | 283 reporter, kSkCaching_PixelRefType, nullptr); |
| 284 | 284 |
| 285 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 285 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 286 reporter, kSkDiscardable_PixelRefType, NULL); | 286 reporter, kSkDiscardable_PixelRefType, nullptr); |
| 287 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 287 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 288 reporter, kSkDiscardable_PixelRefType, NULL); | 288 reporter, kSkDiscardable_PixelRefType, nullptr); |
| 289 | 289 |
| 290 SkAutoTUnref<SkDiscardableMemoryPool> pool( | 290 SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 291 SkDiscardableMemoryPool::Create(1, NULL)); | 291 SkDiscardableMemoryPool::Create(1, nullptr)); |
| 292 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 292 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 293 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 293 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 294 reporter, kSkDiscardable_PixelRefType, pool); | 294 reporter, kSkDiscardable_PixelRefType, pool); |
| 295 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 295 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 296 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 296 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 297 reporter, kSkDiscardable_PixelRefType, pool); | 297 reporter, kSkDiscardable_PixelRefType, pool); |
| 298 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 298 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 299 | 299 |
| 300 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); | 300 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); |
| 301 // Only acts differently from NULL on a platform that has a | 301 // Only acts differently from nullptr on a platform that has a |
| 302 // default discardable memory implementation that differs from the | 302 // default discardable memory implementation that differs from the |
| 303 // global DM pool. | 303 // global DM pool. |
| 304 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 304 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 305 reporter, kSkDiscardable_PixelRefType, globalPool); | 305 reporter, kSkDiscardable_PixelRefType, globalPool); |
| 306 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 306 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 307 reporter, kSkDiscardable_PixelRefType, globalPool); | 307 reporter, kSkDiscardable_PixelRefType, globalPool); |
| 308 } | 308 } |
| 309 | 309 |
| 310 //////////////////////////////////////////////////////////////////////////////// | 310 //////////////////////////////////////////////////////////////////////////////// |
| 311 | 311 |
| 312 DEF_TEST(Image_NewFromGenerator, r) { | 312 DEF_TEST(Image_NewFromGenerator, r) { |
| 313 TestImageGenerator::TestType testTypes[] = { | 313 TestImageGenerator::TestType testTypes[] = { |
| 314 TestImageGenerator::kFailGetPixels_TestType, | 314 TestImageGenerator::kFailGetPixels_TestType, |
| 315 TestImageGenerator::kSucceedGetPixels_TestType, | 315 TestImageGenerator::kSucceedGetPixels_TestType, |
| 316 }; | 316 }; |
| 317 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { | 317 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { |
| 318 TestImageGenerator::TestType test = testTypes[i]; | 318 TestImageGenerator::TestType test = testTypes[i]; |
| 319 SkImageGenerator* gen = new TestImageGenerator(test, r); | 319 SkImageGenerator* gen = new TestImageGenerator(test, r); |
| 320 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen)); | 320 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen)); |
| 321 if (NULL == image.get()) { | 321 if (nullptr == image.get()) { |
| 322 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [" | 322 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [" |
| 323 SK_SIZE_T_SPECIFIER "]", i); | 323 SK_SIZE_T_SPECIFIER "]", i); |
| 324 continue; | 324 continue; |
| 325 } | 325 } |
| 326 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width()); | 326 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width()); |
| 327 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height()); | 327 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height()); |
| 328 REPORTER_ASSERT(r, image->isLazyGenerated()); | 328 REPORTER_ASSERT(r, image->isLazyGenerated()); |
| 329 | 329 |
| 330 SkBitmap bitmap; | 330 SkBitmap bitmap; |
| 331 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::H
eight()); | 331 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::H
eight()); |
| 332 SkCanvas canvas(bitmap); | 332 SkCanvas canvas(bitmap); |
| 333 const SkColor kDefaultColor = 0xffabcdef; | 333 const SkColor kDefaultColor = 0xffabcdef; |
| 334 canvas.clear(kDefaultColor); | 334 canvas.clear(kDefaultColor); |
| 335 canvas.drawImage(image, 0, 0, NULL); | 335 canvas.drawImage(image, 0, 0, nullptr); |
| 336 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { | 336 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { |
| 337 REPORTER_ASSERT( | 337 REPORTER_ASSERT( |
| 338 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); | 338 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); |
| 339 } else { | 339 } else { |
| 340 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); | 340 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); |
| 341 } | 341 } |
| 342 } | 342 } |
| 343 } | 343 } |
| OLD | NEW |