| 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 "SkCommonFlags.h" | 12 #include "SkCommonFlags.h" |
| 12 #include "SkData.h" | 13 #include "SkData.h" |
| 13 #include "SkDocument.h" | 14 #include "SkDocument.h" |
| 14 #include "SkError.h" | 15 #include "SkError.h" |
| 15 #include "SkImageGenerator.h" | 16 #include "SkImageGenerator.h" |
| 16 #include "SkMallocPixelRef.h" | 17 #include "SkMallocPixelRef.h" |
| 17 #include "SkMultiPictureDraw.h" | 18 #include "SkMultiPictureDraw.h" |
| 18 #include "SkNullCanvas.h" | 19 #include "SkNullCanvas.h" |
| 19 #include "SkOSFile.h" | 20 #include "SkOSFile.h" |
| 20 #include "SkPictureData.h" | 21 #include "SkPictureData.h" |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 234 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
| 234 | 235 |
| 235 CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale) | 236 CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale) |
| 236 : fPath(path) | 237 : fPath(path) |
| 237 , fMode(mode) | 238 , fMode(mode) |
| 238 , fDstColorType(dstColorType) | 239 , fDstColorType(dstColorType) |
| 239 , fScale(scale) | 240 , fScale(scale) |
| 240 {} | 241 {} |
| 241 | 242 |
| 242 bool CodecSrc::veto(SinkFlags flags) const { | 243 bool CodecSrc::veto(SinkFlags flags) const { |
| 243 // No need to test decoding to non-raster or indirect backend. | 244 // Test CodecImageGenerator on 8888, 565, and gpu |
| 244 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferr
ed decode to | 245 if (kGen_Mode == fMode) { |
| 245 // let the GPU handle it. | 246 return (flags.type != SinkFlags::kRaster || flags.approach != SinkFlags:
:kDirect) && |
| 246 return flags.type != SinkFlags::kRaster | 247 flags.type != SinkFlags::kGPU; |
| 247 || flags.approach != SinkFlags::kDirect; | 248 } |
| 249 |
| 250 // Test all other modes to direct raster backends (8888 and 565). |
| 251 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir
ect; |
| 248 } | 252 } |
| 249 | 253 |
| 250 bool get_decode_info(SkImageInfo* decodeInfo, const SkImageInfo& defaultInfo, | 254 bool get_decode_info(SkImageInfo* decodeInfo, const SkImageInfo& defaultInfo, |
| 251 SkColorType canvasColorType, CodecSrc::DstColorType dstColorType) { | 255 SkColorType canvasColorType, CodecSrc::DstColorType dstColorType) { |
| 252 switch (dstColorType) { | 256 switch (dstColorType) { |
| 253 case CodecSrc::kIndex8_Always_DstColorType: | 257 case CodecSrc::kIndex8_Always_DstColorType: |
| 254 if (kRGB_565_SkColorType == canvasColorType) { | 258 if (kRGB_565_SkColorType == canvasColorType) { |
| 255 return false; | 259 return false; |
| 256 } | 260 } |
| 257 *decodeInfo = defaultInfo.makeColorType(kIndex_8_SkColorType); | 261 *decodeInfo = defaultInfo.makeColorType(kIndex_8_SkColorType); |
| 258 break; | 262 break; |
| 259 case CodecSrc::kGrayscale_Always_DstColorType: | 263 case CodecSrc::kGrayscale_Always_DstColorType: |
| 260 if (kRGB_565_SkColorType == canvasColorType) { | 264 if (kRGB_565_SkColorType == canvasColorType) { |
| 261 return false; | 265 return false; |
| 262 } | 266 } |
| 263 *decodeInfo = defaultInfo.makeColorType(kGray_8_SkColorType); | 267 *decodeInfo = defaultInfo.makeColorType(kGray_8_SkColorType); |
| 264 break; | 268 break; |
| 265 default: | 269 default: |
| 266 *decodeInfo = defaultInfo.makeColorType(canvasColorType); | 270 *decodeInfo = defaultInfo.makeColorType(canvasColorType); |
| 267 break; | 271 break; |
| 268 } | 272 } |
| 269 | 273 |
| 270 // FIXME: Currently we cannot draw unpremultiplied sources. | 274 // FIXME: Currently we cannot draw unpremultiplied sources. |
| 271 if (decodeInfo->alphaType() == kUnpremul_SkAlphaType) { | 275 if (decodeInfo->alphaType() == kUnpremul_SkAlphaType) { |
| 272 *decodeInfo = decodeInfo->makeAlphaType(kPremul_SkAlphaType); | 276 *decodeInfo = decodeInfo->makeAlphaType(kPremul_SkAlphaType); |
| 273 } | 277 } |
| 274 return true; | 278 return true; |
| 275 } | 279 } |
| 276 | 280 |
| 281 Error test_gen(SkCanvas* canvas, SkData* data) { |
| 282 SkImageGenerator* gen = SkCodecImageGenerator::NewFromEncodedCodec(data); |
| 283 if (!gen) { |
| 284 return "Could not create image generator."; |
| 285 } |
| 286 |
| 287 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/
4822) |
| 288 // Currently, we will avoid creating a CodecSrc for this case (see DM.cpp). |
| 289 SkASSERT(kGray_8_SkColorType != gen->getInfo().colorType()); |
| 290 |
| 291 SkAutoTDelete<SkImage> image(SkImage::NewFromGenerator(gen, nullptr)); |
| 292 if (!image) { |
| 293 return "Could not create image from codec image generator."; |
| 294 } |
| 295 |
| 296 canvas->drawImage(image, 0, 0); |
| 297 return ""; |
| 298 } |
| 299 |
| 277 Error CodecSrc::draw(SkCanvas* canvas) const { | 300 Error CodecSrc::draw(SkCanvas* canvas) const { |
| 278 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 301 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 279 if (!encoded) { | 302 if (!encoded) { |
| 280 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 303 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 281 } | 304 } |
| 305 |
| 306 // The CodecImageGenerator test does not share much code with the other test
s, |
| 307 // so we will handle it in its own function. |
| 308 if (kGen_Mode == fMode) { |
| 309 return test_gen(canvas, encoded); |
| 310 } |
| 311 |
| 282 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 312 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 283 if (nullptr == codec.get()) { | 313 if (nullptr == codec.get()) { |
| 284 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); | 314 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| 285 } | 315 } |
| 286 | 316 |
| 287 SkImageInfo decodeInfo; | 317 SkImageInfo decodeInfo; |
| 288 if (!get_decode_info(&decodeInfo, codec->getInfo(), canvas->imageInfo().colo
rType(), | 318 if (!get_decode_info(&decodeInfo, codec->getInfo(), canvas->imageInfo().colo
rType(), |
| 289 fDstColorType)) { | 319 fDstColorType)) { |
| 290 return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); | 320 return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 291 } | 321 } |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 } | 532 } |
| 503 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToSca
lar(top)); | 533 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToSca
lar(top)); |
| 504 // translate by the scaled height. | 534 // translate by the scaled height. |
| 505 top += decodeInfo.height(); | 535 top += decodeInfo.height(); |
| 506 } | 536 } |
| 507 // translate by the scaled width. | 537 // translate by the scaled width. |
| 508 left += decodeInfo.width(); | 538 left += decodeInfo.width(); |
| 509 } | 539 } |
| 510 return ""; | 540 return ""; |
| 511 } | 541 } |
| 542 default: |
| 543 SkASSERT(false); |
| 544 return "Invalid fMode"; |
| 512 } | 545 } |
| 513 return ""; | 546 return ""; |
| 514 } | 547 } |
| 515 | 548 |
| 516 SkISize CodecSrc::size() const { | 549 SkISize CodecSrc::size() const { |
| 517 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 550 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 518 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 551 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 519 if (nullptr == codec) { | 552 if (nullptr == codec) { |
| 520 return SkISize::Make(0, 0); | 553 return SkISize::Make(0, 0); |
| 521 } | 554 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 534 AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType ds
tColorType, | 567 AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType ds
tColorType, |
| 535 int sampleSize) | 568 int sampleSize) |
| 536 : fPath(path) | 569 : fPath(path) |
| 537 , fMode(mode) | 570 , fMode(mode) |
| 538 , fDstColorType(dstColorType) | 571 , fDstColorType(dstColorType) |
| 539 , fSampleSize(sampleSize) | 572 , fSampleSize(sampleSize) |
| 540 {} | 573 {} |
| 541 | 574 |
| 542 bool AndroidCodecSrc::veto(SinkFlags flags) const { | 575 bool AndroidCodecSrc::veto(SinkFlags flags) const { |
| 543 // No need to test decoding to non-raster or indirect backend. | 576 // No need to test decoding to non-raster or indirect backend. |
| 544 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferr
ed decode to | |
| 545 // let the GPU handle it. | |
| 546 return flags.type != SinkFlags::kRaster | 577 return flags.type != SinkFlags::kRaster |
| 547 || flags.approach != SinkFlags::kDirect; | 578 || flags.approach != SinkFlags::kDirect; |
| 548 } | 579 } |
| 549 | 580 |
| 550 Error AndroidCodecSrc::draw(SkCanvas* canvas) const { | 581 Error AndroidCodecSrc::draw(SkCanvas* canvas) const { |
| 551 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 582 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 552 if (!encoded) { | 583 if (!encoded) { |
| 553 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 584 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 554 } | 585 } |
| 555 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); | 586 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); |
| (...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1309 skr.visit<void>(i, drawsAsSingletonPictures); | 1340 skr.visit<void>(i, drawsAsSingletonPictures); |
| 1310 } | 1341 } |
| 1311 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); | 1342 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
| 1312 | 1343 |
| 1313 canvas->drawPicture(macroPic); | 1344 canvas->drawPicture(macroPic); |
| 1314 return check_against_reference(bitmap, src, fSink); | 1345 return check_against_reference(bitmap, src, fSink); |
| 1315 }); | 1346 }); |
| 1316 } | 1347 } |
| 1317 | 1348 |
| 1318 } // namespace DM | 1349 } // namespace DM |
| OLD | NEW |