Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "DMSrcSink.h" | 8 #include "DMSrcSink.h" |
| 9 #include "SkAndroidCodec.h" | 9 #include "SkAndroidCodec.h" |
| 10 #include "SkCodec.h" | 10 #include "SkCodec.h" |
| 11 #include "SkCodecImageGenerator.h" | 11 #include "SkCodecImageGenerator.h" |
| 12 #include "SkCommonFlags.h" | 12 #include "SkCommonFlags.h" |
| 13 #include "SkData.h" | 13 #include "SkData.h" |
| 14 #include "SkDocument.h" | 14 #include "SkDocument.h" |
| 15 #include "SkError.h" | 15 #include "SkError.h" |
| 16 #include "SkImageGenerator.h" | 16 #include "SkImageGenerator.h" |
| 17 #include "SkMallocPixelRef.h" | 17 #include "SkMallocPixelRef.h" |
| 18 #include "SkMultiPictureDraw.h" | 18 #include "SkMultiPictureDraw.h" |
| 19 #include "SkNullCanvas.h" | 19 #include "SkNullCanvas.h" |
| 20 #include "SkOSFile.h" | 20 #include "SkOSFile.h" |
| 21 #include "SkOpts.h" | |
| 21 #include "SkPictureData.h" | 22 #include "SkPictureData.h" |
| 22 #include "SkPictureRecorder.h" | 23 #include "SkPictureRecorder.h" |
| 23 #include "SkRandom.h" | 24 #include "SkRandom.h" |
| 24 #include "SkRecordDraw.h" | 25 #include "SkRecordDraw.h" |
| 25 #include "SkRecorder.h" | 26 #include "SkRecorder.h" |
| 26 #include "SkRemote.h" | 27 #include "SkRemote.h" |
| 27 #include "SkSVGCanvas.h" | 28 #include "SkSVGCanvas.h" |
| 28 #include "SkStream.h" | 29 #include "SkStream.h" |
| 29 #include "SkTLogic.h" | 30 #include "SkTLogic.h" |
| 30 #include "SkXMLWriter.h" | 31 #include "SkXMLWriter.h" |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 // Test CodecImageGenerator on 8888, 565, and gpu | 247 // Test CodecImageGenerator on 8888, 565, and gpu |
| 247 if (kGen_Mode == fMode) { | 248 if (kGen_Mode == fMode) { |
| 248 return (flags.type != SinkFlags::kRaster || flags.approach != SinkFlags: :kDirect) && | 249 return (flags.type != SinkFlags::kRaster || flags.approach != SinkFlags: :kDirect) && |
| 249 flags.type != SinkFlags::kGPU; | 250 flags.type != SinkFlags::kGPU; |
| 250 } | 251 } |
| 251 | 252 |
| 252 // Test all other modes to direct raster backends (8888 and 565). | 253 // Test all other modes to direct raster backends (8888 and 565). |
| 253 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir ect; | 254 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir ect; |
| 254 } | 255 } |
| 255 | 256 |
| 257 // FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and s kbug.com/3339. | |
| 258 // This allows us to still test unpremultiplied decodes. | |
| 259 void premultiply_if_necessary(SkBitmap& bitmap) { | |
| 260 if (kUnpremul_SkAlphaType != bitmap.alphaType()) { | |
| 261 return; | |
| 262 } | |
| 263 | |
| 264 switch (bitmap.colorType()) { | |
| 265 case kN32_SkColorType: | |
| 266 for (int y = 0; y < bitmap.height(); y++) { | |
| 267 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y); | |
| 268 SkOpts::RGBA_to_rgbA(row, row, bitmap.width()); | |
|
msarett
2016/02/03 20:06:36
I feel comfortable with this, but you could argue
| |
| 269 } | |
| 270 break; | |
| 271 case kIndex_8_SkColorType: { | |
| 272 SkColorTable* colorTable = bitmap.getColorTable(); | |
| 273 SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors( )); | |
| 274 SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count()); | |
| 275 break; | |
| 276 } | |
| 277 default: | |
| 278 // No need to premultiply kGray or k565 outputs. | |
| 279 break; | |
| 280 } | |
| 281 } | |
| 282 | |
| 256 bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType, | 283 bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType, |
| 257 CodecSrc::DstColorType dstColorType) { | 284 CodecSrc::DstColorType dstColorType) { |
| 258 switch (dstColorType) { | 285 switch (dstColorType) { |
| 259 case CodecSrc::kIndex8_Always_DstColorType: | 286 case CodecSrc::kIndex8_Always_DstColorType: |
| 260 if (kRGB_565_SkColorType == canvasColorType) { | 287 if (kRGB_565_SkColorType == canvasColorType) { |
| 261 return false; | 288 return false; |
| 262 } | 289 } |
| 263 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType); | 290 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType); |
| 264 break; | 291 break; |
| 265 case CodecSrc::kGrayscale_Always_DstColorType: | 292 case CodecSrc::kGrayscale_Always_DstColorType: |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 369 // We consider incomplete to be valid, since we should still decode what is | 396 // We consider incomplete to be valid, since we should still decode what is |
| 370 // available. | 397 // available. |
| 371 case SkCodec::kIncompleteInput: | 398 case SkCodec::kIncompleteInput: |
| 372 break; | 399 break; |
| 373 case SkCodec::kInvalidConversion: | 400 case SkCodec::kInvalidConversion: |
| 374 return Error::Nonfatal("Incompatible colortype conversion"); | 401 return Error::Nonfatal("Incompatible colortype conversion"); |
| 375 default: | 402 default: |
| 376 // Everything else is considered a failure. | 403 // Everything else is considered a failure. |
| 377 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( )); | 404 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( )); |
| 378 } | 405 } |
| 406 premultiply_if_necessary(bitmap); | |
| 379 canvas->drawBitmap(bitmap, 0, 0); | 407 canvas->drawBitmap(bitmap, 0, 0); |
| 380 break; | 408 break; |
| 381 } | 409 } |
| 382 case kScanline_Mode: { | 410 case kScanline_Mode: { |
| 383 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL , colorPtr, | 411 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL , colorPtr, |
| 384 colorCountPtr)) { | 412 colorCountPtr)) { |
| 385 return Error::Nonfatal("Could not start scanline decoder"); | 413 return Error::Nonfatal("Could not start scanline decoder"); |
| 386 } | 414 } |
| 387 | 415 |
| 388 void* dst = bitmap.getAddr(0, 0); | 416 void* dst = bitmap.getAddr(0, 0); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 402 void* dstPtr = bitmap.getAddr(0, dstY); | 430 void* dstPtr = bitmap.getAddr(0, dstY); |
| 403 // We complete the loop, even if this call begins to fai l | 431 // We complete the loop, even if this call begins to fai l |
| 404 // due to an incomplete image. This ensures any uniniti alized | 432 // due to an incomplete image. This ensures any uniniti alized |
| 405 // memory will be filled with the proper value. | 433 // memory will be filled with the proper value. |
| 406 codec->getScanlines(dstPtr, 1, bitmap.rowBytes()); | 434 codec->getScanlines(dstPtr, 1, bitmap.rowBytes()); |
| 407 } | 435 } |
| 408 break; | 436 break; |
| 409 } | 437 } |
| 410 } | 438 } |
| 411 | 439 |
| 440 premultiply_if_necessary(bitmap); | |
| 412 canvas->drawBitmap(bitmap, 0, 0); | 441 canvas->drawBitmap(bitmap, 0, 0); |
| 413 break; | 442 break; |
| 414 } | 443 } |
| 415 case kStripe_Mode: { | 444 case kStripe_Mode: { |
| 416 const int height = decodeInfo.height(); | 445 const int height = decodeInfo.height(); |
| 417 // This value is chosen arbitrarily. We exercise more cases by choo sing a value that | 446 // This value is chosen arbitrarily. We exercise more cases by choo sing a value that |
| 418 // does not align with image blocks. | 447 // does not align with image blocks. |
| 419 const int stripeHeight = 37; | 448 const int stripeHeight = 37; |
| 420 const int numStripes = (height + stripeHeight - 1) / stripeHeight; | 449 const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
| 421 | 450 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 453 const int startY = i * stripeHeight; | 482 const int startY = i * stripeHeight; |
| 454 const int linesToRead = SkTMin(stripeHeight, height - startY); | 483 const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 455 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitm ap.rowBytes()); | 484 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitm ap.rowBytes()); |
| 456 | 485 |
| 457 // Skip a stripe | 486 // Skip a stripe |
| 458 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); | 487 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); |
| 459 if (linesToSkip > 0) { | 488 if (linesToSkip > 0) { |
| 460 codec->skipScanlines(linesToSkip); | 489 codec->skipScanlines(linesToSkip); |
| 461 } | 490 } |
| 462 } | 491 } |
| 492 premultiply_if_necessary(bitmap); | |
| 463 canvas->drawBitmap(bitmap, 0, 0); | 493 canvas->drawBitmap(bitmap, 0, 0); |
| 464 break; | 494 break; |
| 465 } | 495 } |
| 466 case kSubset_Mode: { | 496 case kSubset_Mode: { |
| 467 // Arbitrarily choose a divisor. | 497 // Arbitrarily choose a divisor. |
| 468 int divisor = 2; | 498 int divisor = 2; |
| 469 // Total width/height of the image. | 499 // Total width/height of the image. |
| 470 const int W = codec->getInfo().width(); | 500 const int W = codec->getInfo().width(); |
| 471 const int H = codec->getInfo().height(); | 501 const int H = codec->getInfo().height(); |
| 472 if (divisor > W || divisor > H) { | 502 if (divisor > W || divisor > H) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 return Error::Nonfatal("subset codec not support ed"); | 555 return Error::Nonfatal("subset codec not support ed"); |
| 526 } | 556 } |
| 527 // If the first subset succeeded, why would a later one fail? | 557 // If the first subset succeeded, why would a later one fail? |
| 528 // fall through to failure | 558 // fall through to failure |
| 529 default: | 559 default: |
| 530 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) " | 560 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) " |
| 531 "from %s with dimensions (%d x %d)\t error %d", | 561 "from %s with dimensions (%d x %d)\t error %d", |
| 532 x, y, decodeInfo.width(), deco deInfo.height(), | 562 x, y, decodeInfo.width(), deco deInfo.height(), |
| 533 fPath.c_str(), W, H, result); | 563 fPath.c_str(), W, H, result); |
| 534 } | 564 } |
| 565 premultiply_if_necessary(subsetBm); | |
| 535 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToSca lar(top)); | 566 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToSca lar(top)); |
| 536 // translate by the scaled height. | 567 // translate by the scaled height. |
| 537 top += decodeInfo.height(); | 568 top += decodeInfo.height(); |
| 538 } | 569 } |
| 539 // translate by the scaled width. | 570 // translate by the scaled width. |
| 540 left += decodeInfo.width(); | 571 left += decodeInfo.width(); |
| 541 } | 572 } |
| 542 return ""; | 573 return ""; |
| 543 } | 574 } |
| 544 default: | 575 default: |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 635 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitm ap.rowBytes(), | 666 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitm ap.rowBytes(), |
| 636 &options)) { | 667 &options)) { |
| 637 case SkCodec::kSuccess: | 668 case SkCodec::kSuccess: |
| 638 case SkCodec::kIncompleteInput: | 669 case SkCodec::kIncompleteInput: |
| 639 break; | 670 break; |
| 640 case SkCodec::kInvalidConversion: | 671 case SkCodec::kInvalidConversion: |
| 641 return Error::Nonfatal("Cannot convert to requested color ty pe."); | 672 return Error::Nonfatal("Cannot convert to requested color ty pe."); |
| 642 default: | 673 default: |
| 643 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( )); | 674 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( )); |
| 644 } | 675 } |
| 676 premultiply_if_necessary(bitmap); | |
| 645 canvas->drawBitmap(bitmap, 0, 0); | 677 canvas->drawBitmap(bitmap, 0, 0); |
| 646 return ""; | 678 return ""; |
| 647 } | 679 } |
| 648 case kDivisor_Mode: { | 680 case kDivisor_Mode: { |
| 649 const int width = codec->getInfo().width(); | 681 const int width = codec->getInfo().width(); |
| 650 const int height = codec->getInfo().height(); | 682 const int height = codec->getInfo().height(); |
| 651 const int divisor = 2; | 683 const int divisor = 2; |
| 652 if (width < divisor || height < divisor) { | 684 if (width < divisor || height < divisor) { |
| 653 return Error::Nonfatal("Divisor is larger than image dimension." ); | 685 return Error::Nonfatal("Divisor is larger than image dimension." ); |
| 654 } | 686 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 694 case SkCodec::kInvalidConversion: | 726 case SkCodec::kInvalidConversion: |
| 695 return Error::Nonfatal("Cannot convert to requested color type."); | 727 return Error::Nonfatal("Cannot convert to requested color type."); |
| 696 default: | 728 default: |
| 697 return SkStringPrintf("Couldn't getPixels %s.", fPat h.c_str()); | 729 return SkStringPrintf("Couldn't getPixels %s.", fPat h.c_str()); |
| 698 } | 730 } |
| 699 } | 731 } |
| 700 } | 732 } |
| 701 | 733 |
| 702 SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth, | 734 SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth, |
| 703 (SkScalar) finalScaledHeight); | 735 (SkScalar) finalScaledHeight); |
| 736 premultiply_if_necessary(bitmap); | |
| 704 canvas->drawBitmapRect(bitmap, rect, rect, nullptr); | 737 canvas->drawBitmapRect(bitmap, rect, rect, nullptr); |
| 705 return ""; | 738 return ""; |
| 706 } | 739 } |
| 707 default: | 740 default: |
| 708 SkASSERT(false); | 741 SkASSERT(false); |
| 709 return "Error: Should not be reached."; | 742 return "Error: Should not be reached."; |
| 710 } | 743 } |
| 711 } | 744 } |
| 712 | 745 |
| 713 SkISize AndroidCodecSrc::size() const { | 746 SkISize AndroidCodecSrc::size() const { |
| (...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1292 skr.visit<void>(i, drawsAsSingletonPictures); | 1325 skr.visit<void>(i, drawsAsSingletonPictures); |
| 1293 } | 1326 } |
| 1294 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); | 1327 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
| 1295 | 1328 |
| 1296 canvas->drawPicture(macroPic); | 1329 canvas->drawPicture(macroPic); |
| 1297 return check_against_reference(bitmap, src, fSink); | 1330 return check_against_reference(bitmap, src, fSink); |
| 1298 }); | 1331 }); |
| 1299 } | 1332 } |
| 1300 | 1333 |
| 1301 } // namespace DM | 1334 } // namespace DM |
| OLD | NEW |