| 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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 * SkDecodingImageGenerator. | 156 * SkDecodingImageGenerator. |
| 157 */ | 157 */ |
| 158 DEF_TEST(DecodingImageGenerator, reporter) { | 158 DEF_TEST(DecodingImageGenerator, reporter) { |
| 159 test_three_encodings(reporter, install_skCachingPixelRef); | 159 test_three_encodings(reporter, install_skCachingPixelRef); |
| 160 test_three_encodings(reporter, install_skDiscardablePixelRef); | 160 test_three_encodings(reporter, install_skDiscardablePixelRef); |
| 161 } | 161 } |
| 162 | 162 |
| 163 class TestImageGenerator : public SkImageGenerator { | 163 class TestImageGenerator : public SkImageGenerator { |
| 164 public: | 164 public: |
| 165 enum TestType { | 165 enum TestType { |
| 166 kFailGetInfo_TestType, | |
| 167 kFailGetPixels_TestType, | 166 kFailGetPixels_TestType, |
| 168 kSucceedGetPixels_TestType, | 167 kSucceedGetPixels_TestType, |
| 169 kLast_TestType = kSucceedGetPixels_TestType | 168 kLast_TestType = kSucceedGetPixels_TestType |
| 170 }; | 169 }; |
| 171 static int Width() { return 10; } | 170 static int Width() { return 10; } |
| 172 static int Height() { return 10; } | 171 static int Height() { return 10; } |
| 173 static uint32_t Color() { return 0xff123456; } | 172 static uint32_t Color() { return 0xff123456; } |
| 174 TestImageGenerator(TestType type, skiatest::Reporter* reporter) | 173 TestImageGenerator(TestType type, skiatest::Reporter* reporter) |
| 175 : fType(type), fReporter(reporter) { | 174 : INHERITED(GetMyInfo()), fType(type), fReporter(reporter) { |
| 176 SkASSERT((fType <= kLast_TestType) && (fType >= 0)); | 175 SkASSERT((fType <= kLast_TestType) && (fType >= 0)); |
| 177 } | 176 } |
| 178 virtual ~TestImageGenerator() { } | 177 virtual ~TestImageGenerator() { } |
| 179 | 178 |
| 180 protected: | 179 protected: |
| 180 static SkImageInfo GetMyInfo() { |
| 181 return SkImageInfo::MakeN32(TestImageGenerator::Width(), TestImageGenera
tor::Height(), |
| 182 kOpaque_SkAlphaType); |
| 183 } |
| 184 |
| 185 #ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO |
| 181 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE { | 186 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE { |
| 182 REPORTER_ASSERT(fReporter, info); | 187 REPORTER_ASSERT(fReporter, info); |
| 183 if ((NULL == info) || (kFailGetInfo_TestType == fType)) { | 188 *info = GetMyInfo(); |
| 184 return false; | |
| 185 } | |
| 186 *info = SkImageInfo::MakeN32(TestImageGenerator::Width(), | |
| 187 TestImageGenerator::Height(), | |
| 188 kOpaque_SkAlphaType); | |
| 189 return true; | 189 return true; |
| 190 } | 190 } |
| 191 #endif |
| 191 | 192 |
| 192 virtual Result onGetPixels(const SkImageInfo& info, void* pixels, size_t row
Bytes, | 193 virtual Result onGetPixels(const SkImageInfo& info, void* pixels, size_t row
Bytes, |
| 193 const Options&, | 194 const Options&, |
| 194 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE
{ | 195 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE
{ |
| 195 REPORTER_ASSERT(fReporter, pixels != NULL); | 196 REPORTER_ASSERT(fReporter, pixels != NULL); |
| 196 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); | 197 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); |
| 197 if (fType != kSucceedGetPixels_TestType) { | 198 if (fType != kSucceedGetPixels_TestType) { |
| 198 return kUnimplemented; | 199 return kUnimplemented; |
| 199 } | 200 } |
| 200 if (info.colorType() != kN32_SkColorType) { | 201 if (info.colorType() != kN32_SkColorType) { |
| 201 return kInvalidConversion; | 202 return kInvalidConversion; |
| 202 } | 203 } |
| 203 char* bytePtr = static_cast<char*>(pixels); | 204 char* bytePtr = static_cast<char*>(pixels); |
| 204 for (int y = 0; y < info.height(); ++y) { | 205 for (int y = 0; y < info.height(); ++y) { |
| 205 sk_memset32(reinterpret_cast<SkColor*>(bytePtr), | 206 sk_memset32(reinterpret_cast<SkColor*>(bytePtr), |
| 206 TestImageGenerator::Color(), info.width()); | 207 TestImageGenerator::Color(), info.width()); |
| 207 bytePtr += rowBytes; | 208 bytePtr += rowBytes; |
| 208 } | 209 } |
| 209 return kSuccess; | 210 return kSuccess; |
| 210 } | 211 } |
| 211 | 212 |
| 212 private: | 213 private: |
| 213 const TestType fType; | 214 const TestType fType; |
| 214 skiatest::Reporter* const fReporter; | 215 skiatest::Reporter* const fReporter; |
| 216 |
| 217 typedef SkImageGenerator INHERITED; |
| 215 }; | 218 }; |
| 216 | 219 |
| 217 static void check_test_image_generator_bitmap(skiatest::Reporter* reporter, | 220 static void check_test_image_generator_bitmap(skiatest::Reporter* reporter, |
| 218 const SkBitmap& bm) { | 221 const SkBitmap& bm) { |
| 219 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); | 222 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); |
| 220 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); | 223 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); |
| 221 SkAutoLockPixels autoLockPixels(bm); | 224 SkAutoLockPixels autoLockPixels(bm); |
| 222 REPORTER_ASSERT(reporter, bm.getPixels()); | 225 REPORTER_ASSERT(reporter, bm.getPixels()); |
| 223 if (NULL == bm.getPixels()) { | 226 if (NULL == bm.getPixels()) { |
| 224 return; | 227 return; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 249 (type, reporter))); | 252 (type, reporter))); |
| 250 REPORTER_ASSERT(reporter, gen.get() != NULL); | 253 REPORTER_ASSERT(reporter, gen.get() != NULL); |
| 251 SkBitmap lazy; | 254 SkBitmap lazy; |
| 252 bool success; | 255 bool success; |
| 253 if (kSkCaching_PixelRefType == pixelRefType) { | 256 if (kSkCaching_PixelRefType == pixelRefType) { |
| 254 // Ignore factory; use global cache. | 257 // Ignore factory; use global cache. |
| 255 success = SkCachingPixelRef::Install(gen.detach(), &lazy); | 258 success = SkCachingPixelRef::Install(gen.detach(), &lazy); |
| 256 } else { | 259 } else { |
| 257 success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory); | 260 success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory); |
| 258 } | 261 } |
| 259 REPORTER_ASSERT(reporter, success | 262 REPORTER_ASSERT(reporter, success); |
| 260 == (TestImageGenerator::kFailGetInfo_TestType != type)); | |
| 261 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { | 263 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { |
| 262 check_test_image_generator_bitmap(reporter, lazy); | 264 check_test_image_generator_bitmap(reporter, lazy); |
| 263 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { | 265 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { |
| 264 SkAutoLockPixels autoLockPixels(lazy); | 266 SkAutoLockPixels autoLockPixels(lazy); |
| 265 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 267 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 266 } | 268 } |
| 267 } | 269 } |
| 268 | 270 |
| 269 // new/lock/delete is an odd pattern for a pixelref, but it needs to not assert | 271 // new/lock/delete is an odd pattern for a pixelref, but it needs to not assert |
| 270 static void test_newlockdelete(skiatest::Reporter* reporter) { | 272 static void test_newlockdelete(skiatest::Reporter* reporter) { |
| 271 SkBitmap bm; | 273 SkBitmap bm; |
| 272 SkImageGenerator* ig = new TestImageGenerator( | 274 SkImageGenerator* ig = new TestImageGenerator( |
| 273 TestImageGenerator::kSucceedGetPixels_TestType, reporter); | 275 TestImageGenerator::kSucceedGetPixels_TestType, reporter); |
| 274 SkInstallDiscardablePixelRef(ig, &bm); | 276 SkInstallDiscardablePixelRef(ig, &bm); |
| 275 bm.pixelRef()->lockPixels(); | 277 bm.pixelRef()->lockPixels(); |
| 276 } | 278 } |
| 277 | 279 |
| 278 /** | 280 /** |
| 279 * This tests the basic functionality of SkDiscardablePixelRef with a | 281 * This tests the basic functionality of SkDiscardablePixelRef with a |
| 280 * basic SkImageGenerator implementation and several | 282 * basic SkImageGenerator implementation and several |
| 281 * SkDiscardableMemory::Factory choices. | 283 * SkDiscardableMemory::Factory choices. |
| 282 */ | 284 */ |
| 283 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { | 285 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { |
| 284 test_newlockdelete(reporter); | 286 test_newlockdelete(reporter); |
| 285 | 287 |
| 286 check_pixelref(TestImageGenerator::kFailGetInfo_TestType, | |
| 287 reporter, kSkCaching_PixelRefType, NULL); | |
| 288 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 288 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 289 reporter, kSkCaching_PixelRefType, NULL); | 289 reporter, kSkCaching_PixelRefType, NULL); |
| 290 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 290 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 291 reporter, kSkCaching_PixelRefType, NULL); | 291 reporter, kSkCaching_PixelRefType, NULL); |
| 292 | 292 |
| 293 check_pixelref(TestImageGenerator::kFailGetInfo_TestType, | |
| 294 reporter, kSkDiscardable_PixelRefType, NULL); | |
| 295 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 293 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 296 reporter, kSkDiscardable_PixelRefType, NULL); | 294 reporter, kSkDiscardable_PixelRefType, NULL); |
| 297 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 295 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 298 reporter, kSkDiscardable_PixelRefType, NULL); | 296 reporter, kSkDiscardable_PixelRefType, NULL); |
| 299 | 297 |
| 300 SkAutoTUnref<SkDiscardableMemoryPool> pool( | 298 SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 301 SkDiscardableMemoryPool::Create(1, NULL)); | 299 SkDiscardableMemoryPool::Create(1, NULL)); |
| 302 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 300 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 303 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 301 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 304 reporter, kSkDiscardable_PixelRefType, pool); | 302 reporter, kSkDiscardable_PixelRefType, pool); |
| 305 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 303 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 306 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 304 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 307 reporter, kSkDiscardable_PixelRefType, pool); | 305 reporter, kSkDiscardable_PixelRefType, pool); |
| 308 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 306 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 309 | 307 |
| 310 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); | 308 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); |
| 311 // Only acts differently from NULL on a platform that has a | 309 // Only acts differently from NULL on a platform that has a |
| 312 // default discardable memory implementation that differs from the | 310 // default discardable memory implementation that differs from the |
| 313 // global DM pool. | 311 // global DM pool. |
| 314 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 312 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 315 reporter, kSkDiscardable_PixelRefType, globalPool); | 313 reporter, kSkDiscardable_PixelRefType, globalPool); |
| 316 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 314 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 317 reporter, kSkDiscardable_PixelRefType, globalPool); | 315 reporter, kSkDiscardable_PixelRefType, globalPool); |
| 318 } | 316 } |
| 319 | 317 |
| 320 //////////////////////////////////////////////////////////////////////////////// | 318 //////////////////////////////////////////////////////////////////////////////// |
| 321 | 319 |
| 322 DEF_TEST(Image_NewFromGenerator, r) { | 320 DEF_TEST(Image_NewFromGenerator, r) { |
| 323 TestImageGenerator::TestType testTypes[] = { | 321 TestImageGenerator::TestType testTypes[] = { |
| 324 TestImageGenerator::kFailGetInfo_TestType, | |
| 325 TestImageGenerator::kFailGetPixels_TestType, | 322 TestImageGenerator::kFailGetPixels_TestType, |
| 326 TestImageGenerator::kSucceedGetPixels_TestType, | 323 TestImageGenerator::kSucceedGetPixels_TestType, |
| 327 }; | 324 }; |
| 328 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { | 325 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { |
| 329 TestImageGenerator::TestType test = testTypes[i]; | 326 TestImageGenerator::TestType test = testTypes[i]; |
| 330 SkImageGenerator* gen = SkNEW_ARGS(TestImageGenerator, (test, r)); | 327 SkImageGenerator* gen = SkNEW_ARGS(TestImageGenerator, (test, r)); |
| 331 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen)); | 328 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen)); |
| 332 if (TestImageGenerator::kFailGetInfo_TestType == test) { | |
| 333 REPORTER_ASSERT(r, NULL == image.get()); | |
| 334 continue; | |
| 335 } | |
| 336 if (NULL == image.get()) { | 329 if (NULL == image.get()) { |
| 337 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [" | 330 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [" |
| 338 SK_SIZE_T_SPECIFIER "]", i); | 331 SK_SIZE_T_SPECIFIER "]", i); |
| 339 continue; | 332 continue; |
| 340 } | 333 } |
| 341 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width()); | 334 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width()); |
| 342 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height()); | 335 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height()); |
| 343 | 336 |
| 344 SkBitmap bitmap; | 337 SkBitmap bitmap; |
| 345 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::H
eight()); | 338 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::H
eight()); |
| 346 SkCanvas canvas(bitmap); | 339 SkCanvas canvas(bitmap); |
| 347 const SkColor kDefaultColor = 0xffabcdef; | 340 const SkColor kDefaultColor = 0xffabcdef; |
| 348 canvas.clear(kDefaultColor); | 341 canvas.clear(kDefaultColor); |
| 349 canvas.drawImage(image, 0, 0, NULL); | 342 canvas.drawImage(image, 0, 0, NULL); |
| 350 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { | 343 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { |
| 351 REPORTER_ASSERT( | 344 REPORTER_ASSERT( |
| 352 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); | 345 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); |
| 353 } else { | 346 } else { |
| 354 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); | 347 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); |
| 355 } | 348 } |
| 356 } | 349 } |
| 357 } | 350 } |
| OLD | NEW |