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 "SkImageGeneratorCG.h" |
17 #include "SkMallocPixelRef.h" | 18 #include "SkMallocPixelRef.h" |
18 #include "SkMultiPictureDraw.h" | 19 #include "SkMultiPictureDraw.h" |
19 #include "SkNullCanvas.h" | 20 #include "SkNullCanvas.h" |
20 #include "SkOSFile.h" | 21 #include "SkOSFile.h" |
21 #include "SkOpts.h" | 22 #include "SkOpts.h" |
22 #include "SkPictureData.h" | 23 #include "SkPictureData.h" |
23 #include "SkPictureRecorder.h" | 24 #include "SkPictureRecorder.h" |
24 #include "SkRandom.h" | 25 #include "SkRandom.h" |
25 #include "SkRecordDraw.h" | 26 #include "SkRecordDraw.h" |
26 #include "SkRecorder.h" | 27 #include "SkRecorder.h" |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 float scale) | 258 float scale) |
258 : fPath(path) | 259 : fPath(path) |
259 , fMode(mode) | 260 , fMode(mode) |
260 , fDstColorType(dstColorType) | 261 , fDstColorType(dstColorType) |
261 , fDstAlphaType(dstAlphaType) | 262 , fDstAlphaType(dstAlphaType) |
262 , fScale(scale) | 263 , fScale(scale) |
263 , fRunSerially(serial_from_path_name(path)) | 264 , fRunSerially(serial_from_path_name(path)) |
264 {} | 265 {} |
265 | 266 |
266 bool CodecSrc::veto(SinkFlags flags) const { | 267 bool CodecSrc::veto(SinkFlags flags) const { |
267 // Test CodecImageGenerator on 8888, 565, and gpu | 268 // Test to direct raster backends (8888 and 565). |
268 if (kGen_Mode == fMode) { | |
269 // For image generator, we want to test kDirect approaches for kRaster a
nd kGPU, | |
270 // while skipping everything else. | |
271 return (flags.type != SinkFlags::kRaster && flags.type != SinkFlags::kGP
U) || | |
272 flags.approach != SinkFlags::kDirect; | |
273 } | |
274 | |
275 // Test all other modes to direct raster backends (8888 and 565). | |
276 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir
ect; | 269 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir
ect; |
277 } | 270 } |
278 | 271 |
279 // FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and s
kbug.com/3339. | 272 // FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and s
kbug.com/3339. |
280 // This allows us to still test unpremultiplied decodes. | 273 // This allows us to still test unpremultiplied decodes. |
281 void premultiply_if_necessary(SkBitmap& bitmap) { | 274 void premultiply_if_necessary(SkBitmap& bitmap) { |
282 if (kUnpremul_SkAlphaType != bitmap.alphaType()) { | 275 if (kUnpremul_SkAlphaType != bitmap.alphaType()) { |
283 return; | 276 return; |
284 } | 277 } |
285 | 278 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 kOpaque_SkAlphaType != decodeInfo->alphaType()) { | 320 kOpaque_SkAlphaType != decodeInfo->alphaType()) { |
328 return false; | 321 return false; |
329 } | 322 } |
330 *decodeInfo = decodeInfo->makeColorType(canvasColorType); | 323 *decodeInfo = decodeInfo->makeColorType(canvasColorType); |
331 break; | 324 break; |
332 } | 325 } |
333 | 326 |
334 return true; | 327 return true; |
335 } | 328 } |
336 | 329 |
337 Error test_gen(SkCanvas* canvas, SkData* data) { | |
338 SkAutoTDelete<SkImageGenerator> gen = SkCodecImageGenerator::NewFromEncodedC
odec(data); | |
339 if (!gen) { | |
340 return "Could not create image generator."; | |
341 } | |
342 | |
343 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/
4822) | |
344 // Currently, we will avoid creating a CodecSrc for this case (see DM.cpp). | |
345 SkASSERT(kGray_8_SkColorType != gen->getInfo().colorType()); | |
346 | |
347 if (kOpaque_SkAlphaType != gen->getInfo().alphaType() && | |
348 kRGB_565_SkColorType == canvas->imageInfo().colorType()) { | |
349 return Error::Nonfatal("Skip testing non-opaque images to 565."); | |
350 } | |
351 | |
352 SkAutoTDelete<SkImage> image(SkImage::NewFromGenerator(gen.detach(), nullptr
)); | |
353 if (!image) { | |
354 return "Could not create image from codec image generator."; | |
355 } | |
356 | |
357 canvas->drawImage(image, 0, 0); | |
358 return ""; | |
359 } | |
360 | |
361 Error CodecSrc::draw(SkCanvas* canvas) const { | 330 Error CodecSrc::draw(SkCanvas* canvas) const { |
362 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 331 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
363 if (!encoded) { | 332 if (!encoded) { |
364 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 333 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
365 } | 334 } |
366 | 335 |
367 // The CodecImageGenerator test does not share much code with the other test
s, | |
368 // so we will handle it in its own function. | |
369 if (kGen_Mode == fMode) { | |
370 return test_gen(canvas, encoded); | |
371 } | |
372 | |
373 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 336 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
374 if (nullptr == codec.get()) { | 337 if (nullptr == codec.get()) { |
375 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); | 338 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
376 } | 339 } |
377 | 340 |
378 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType); | 341 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType); |
379 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColor
Type)) { | 342 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColor
Type)) { |
380 return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); | 343 return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
381 } | 344 } |
382 | 345 |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 // We will replicate the names used by CodecSrc so that images can | 759 // We will replicate the names used by CodecSrc so that images can |
797 // be compared in Gold. | 760 // be compared in Gold. |
798 if (1 == fSampleSize) { | 761 if (1 == fSampleSize) { |
799 return SkOSPath::Basename(fPath.c_str()); | 762 return SkOSPath::Basename(fPath.c_str()); |
800 } | 763 } |
801 return get_scaled_name(fPath, 1.0f / (float) fSampleSize); | 764 return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
802 } | 765 } |
803 | 766 |
804 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 767 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
805 | 768 |
| 769 ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu
) |
| 770 : fPath(path) |
| 771 , fMode(mode) |
| 772 , fDstAlphaType(alphaType) |
| 773 , fIsGpu(isGpu) |
| 774 , fRunSerially(serial_from_path_name(path)) |
| 775 {} |
| 776 |
| 777 bool ImageGenSrc::veto(SinkFlags flags) const { |
| 778 if (fIsGpu) { |
| 779 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDi
rect; |
| 780 } |
| 781 |
| 782 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir
ect; |
| 783 } |
| 784 |
| 785 Error ImageGenSrc::draw(SkCanvas* canvas) const { |
| 786 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 787 return Error::Nonfatal("Uninteresting to test image generator to 565."); |
| 788 } |
| 789 |
| 790 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 791 if (!encoded) { |
| 792 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 793 } |
| 794 |
| 795 SkAutoTDelete<SkImageGenerator> gen(nullptr); |
| 796 switch (fMode) { |
| 797 case kCodec_Mode: |
| 798 gen.reset(SkCodecImageGenerator::NewFromEncodedCodec(encoded)); |
| 799 if (!gen) { |
| 800 return "Could not create codec image generator."; |
| 801 } |
| 802 break; |
| 803 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 804 case kCG_Mode: |
| 805 gen.reset(SkImageGeneratorCG::NewFromEncodedCG(encoded)); |
| 806 if (!gen) { |
| 807 return "Could not create CG image generator."; |
| 808 } |
| 809 break; |
| 810 #endif |
| 811 default: |
| 812 SkASSERT(false); |
| 813 return "Invalid image generator mode"; |
| 814 } |
| 815 |
| 816 // Test deferred decoding path on GPU |
| 817 if (fIsGpu) { |
| 818 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.
com/4822) |
| 819 // We have disabled these tests in DM.cpp. |
| 820 SkASSERT(kGray_8_SkColorType != gen->getInfo().colorType()); |
| 821 |
| 822 SkAutoTDelete<SkImage> image(SkImage::NewFromGenerator(gen.detach(), nul
lptr)); |
| 823 if (!image) { |
| 824 return "Could not create image from codec image generator."; |
| 825 } |
| 826 canvas->drawImage(image, 0, 0); |
| 827 return ""; |
| 828 } |
| 829 |
| 830 // Test various color and alpha types on CPU |
| 831 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType); |
| 832 |
| 833 if (kGray_8_SkColorType == decodeInfo.colorType() && |
| 834 kOpaque_SkAlphaType != decodeInfo.alphaType()) { |
| 835 return Error::Nonfatal("Avoid requesting non-opaque kGray8 decodes."); |
| 836 } |
| 837 |
| 838 SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 839 SkPMColor* colorPtr = nullptr; |
| 840 int* colorCountPtr = nullptr; |
| 841 int maxColors = 256; |
| 842 if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 843 SkPMColor colors[256]; |
| 844 colorTable.reset(new SkColorTable(colors, maxColors)); |
| 845 colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 846 colorCountPtr = &maxColors; |
| 847 } |
| 848 |
| 849 SkBitmap bitmap; |
| 850 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) { |
| 851 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
| 852 decodeInfo.width(), decodeInfo.height()); |
| 853 } |
| 854 |
| 855 if (!gen->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), color
Ptr, |
| 856 colorCountPtr)) |
| 857 { |
| 858 return SkStringPrintf("Image generator could not getPixels() for %s\n",
fPath.c_str()); |
| 859 } |
| 860 |
| 861 premultiply_if_necessary(bitmap); |
| 862 canvas->drawBitmap(bitmap, 0, 0); |
| 863 return ""; |
| 864 } |
| 865 |
| 866 SkISize ImageGenSrc::size() const { |
| 867 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 868 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 869 if (nullptr == codec) { |
| 870 return SkISize::Make(0, 0); |
| 871 } |
| 872 return codec->getInfo().dimensions(); |
| 873 } |
| 874 |
| 875 Name ImageGenSrc::name() const { |
| 876 return SkOSPath::Basename(fPath.c_str()); |
| 877 } |
| 878 |
| 879 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
| 880 |
806 static const SkRect kSKPViewport = {0,0, 1000,1000}; | 881 static const SkRect kSKPViewport = {0,0, 1000,1000}; |
807 | 882 |
808 SKPSrc::SKPSrc(Path path) : fPath(path) {} | 883 SKPSrc::SKPSrc(Path path) : fPath(path) {} |
809 | 884 |
810 Error SKPSrc::draw(SkCanvas* canvas) const { | 885 Error SKPSrc::draw(SkCanvas* canvas) const { |
811 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); | 886 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
812 if (!stream) { | 887 if (!stream) { |
813 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 888 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
814 } | 889 } |
815 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream)); | 890 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream)); |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1419 skr.visit<void>(i, drawsAsSingletonPictures); | 1494 skr.visit<void>(i, drawsAsSingletonPictures); |
1420 } | 1495 } |
1421 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); | 1496 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
1422 | 1497 |
1423 canvas->drawPicture(macroPic); | 1498 canvas->drawPicture(macroPic); |
1424 return check_against_reference(bitmap, src, fSink); | 1499 return check_against_reference(bitmap, src, fSink); |
1425 }); | 1500 }); |
1426 } | 1501 } |
1427 | 1502 |
1428 } // namespace DM | 1503 } // namespace DM |
OLD | NEW |