scroggo | 478652e | 2015-03-25 07:11:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 8 | #include "DMSrcSink.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 9 | #include "DDLPromiseImageHelper.h" |
| 10 | #include "DDLTileHelper.h" |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 11 | #include "GrBackendSurface.h" |
| 12 | #include "GrContextPriv.h" |
| 13 | #include "GrGpu.h" |
| 14 | #include "MemoryCache.h" |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 15 | #include "Resources.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 16 | #include "SkAndroidCodec.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 17 | #include "SkAutoMalloc.h" |
Brian Osman | a76b7a9 | 2018-07-18 14:36:43 -0400 | [diff] [blame] | 18 | #include "SkAutoPixmapStorage.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 19 | #include "SkCodec.h" |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 20 | #include "SkCodecImageGenerator.h" |
msarett | 888dc16 | 2016-05-23 10:21:17 -0700 | [diff] [blame] | 21 | #include "SkColorSpace.h" |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 22 | #include "SkColorSpaceXformCanvas.h" |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 23 | #include "SkCommonFlags.h" |
Chris Dalton | 040238b | 2017-12-18 14:22:34 -0700 | [diff] [blame] | 24 | #include "SkCommonFlagsGpu.h" |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 25 | #include "SkData.h" |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 26 | #include "SkDebugCanvas.h" |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 27 | #include "SkDeferredDisplayListRecorder.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 28 | #include "SkDocument.h" |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 29 | #include "SkExecutor.h" |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 30 | #include "SkImageGenerator.h" |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 31 | #include "SkImageGeneratorCG.h" |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 32 | #include "SkImageGeneratorWIC.h" |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 33 | #include "SkImageInfoPriv.h" |
mtklein | 8bbbb69 | 2016-08-15 12:56:00 -0700 | [diff] [blame] | 34 | #include "SkLiteDL.h" |
| 35 | #include "SkLiteRecorder.h" |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 36 | #include "SkMakeUnique.h" |
mtklein | c8be09a | 2016-01-04 18:56:57 -0800 | [diff] [blame] | 37 | #include "SkMallocPixelRef.h" |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 38 | #include "SkMultiPictureDocumentPriv.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 39 | #include "SkMultiPictureDraw.h" |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 40 | #include "SkNullCanvas.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 41 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 42 | #include "SkOSPath.h" |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 43 | #include "SkOpts.h" |
Mike Reed | e7a5832 | 2017-12-20 14:09:20 -0500 | [diff] [blame] | 44 | #include "SkPictureCommon.h" |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 45 | #include "SkPictureData.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 46 | #include "SkPictureRecorder.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 47 | #include "SkPDFDocument.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 48 | #include "SkRandom.h" |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 49 | #include "SkRecordDraw.h" |
| 50 | #include "SkRecorder.h" |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 51 | #include "SkStream.h" |
Mike Klein | 48b6490 | 2018-07-25 13:28:44 -0400 | [diff] [blame] | 52 | #include "SkSurface.h" |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 53 | #include "SkSurfaceCharacterization.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 54 | #include "SkSwizzler.h" |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 55 | #include "SkTLogic.h" |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 56 | #include "SkTaskGroup.h" |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 57 | #if defined(SK_BUILD_FOR_WIN) |
| 58 | #include "SkAutoCoInitialize.h" |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 59 | #include "SkHRESULT.h" |
| 60 | #include "SkTScopedComPtr.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 61 | #include "SkXPSDocument.h" |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 62 | #include <XpsObjectModel.h> |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 63 | #endif |
| 64 | |
Florin Malita | 87ccf33 | 2018-05-04 12:23:24 -0400 | [diff] [blame] | 65 | #if defined(SK_ENABLE_SKOTTIE) |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 66 | #include "Skottie.h" |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 67 | #include "SkottieUtils.h" |
Florin Malita | 124d5af | 2017-12-31 17:02:26 -0500 | [diff] [blame] | 68 | #endif |
| 69 | |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 70 | #if defined(SK_XML) |
Ben Wagner | 6ce482a | 2018-03-27 10:57:43 -0400 | [diff] [blame] | 71 | #include "SkSVGCanvas.h" |
fmalita | a48f0e3 | 2016-08-04 06:26:05 -0700 | [diff] [blame] | 72 | #include "SkSVGDOM.h" |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 73 | #include "SkXMLWriter.h" |
| 74 | #endif |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 75 | #include "TestUtils.h" |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 76 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 77 | #include <cmath> |
| 78 | #include <functional> |
| 79 | |
Brian Osman | 8dc68c6 | 2018-05-30 12:57:45 -0400 | [diff] [blame] | 80 | #include "../third_party/skcms/skcms.h" |
Brian Osman | c494a70 | 2018-05-23 11:52:00 -0400 | [diff] [blame] | 81 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 82 | DEFINE_bool(multiPage, false, "For document-type backends, render the source" |
| 83 | " into multiple pages"); |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 84 | DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?"); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 85 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 86 | using sk_gpu_test::GrContextFactory; |
| 87 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 88 | namespace DM { |
| 89 | |
Hal Canary | 972eba3 | 2018-07-30 17:07:07 -0400 | [diff] [blame] | 90 | GMSrc::GMSrc(skiagm::GMFactory factory) : fFactory(factory) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 91 | |
| 92 | Error GMSrc::draw(SkCanvas* canvas) const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 93 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 94 | gm->draw(canvas); |
| 95 | return ""; |
| 96 | } |
| 97 | |
| 98 | SkISize GMSrc::size() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 99 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 100 | return gm->getISize(); |
| 101 | } |
| 102 | |
| 103 | Name GMSrc::name() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 104 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 105 | return gm->getName(); |
| 106 | } |
| 107 | |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 108 | void GMSrc::modifyGrContextOptions(GrContextOptions* options) const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 109 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 110 | gm->modifyGrContextOptions(options); |
| 111 | } |
| 112 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 113 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 114 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 115 | BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 116 | : fPath(path) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 117 | , fMode(mode) |
| 118 | , fDstColorType(dstColorType) |
| 119 | , fSampleSize(sampleSize) |
| 120 | {} |
| 121 | |
| 122 | bool BRDSrc::veto(SinkFlags flags) const { |
| 123 | // No need to test to non-raster or indirect backends. |
| 124 | return flags.type != SinkFlags::kRaster |
| 125 | || flags.approach != SinkFlags::kDirect; |
| 126 | } |
| 127 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 128 | static SkBitmapRegionDecoder* create_brd(Path path) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 129 | sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str())); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 130 | if (!encoded) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 131 | return nullptr; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 132 | } |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 133 | return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 136 | static inline void alpha8_to_gray8(SkBitmap* bitmap) { |
| 137 | // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert |
| 138 | // them back to kGray8 so our test framework can draw them correctly. |
| 139 | if (kAlpha_8_SkColorType == bitmap->info().colorType()) { |
| 140 | SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType) |
| 141 | .makeAlphaType(kOpaque_SkAlphaType); |
| 142 | *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo; |
| 143 | } |
| 144 | } |
| 145 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 146 | Error BRDSrc::draw(SkCanvas* canvas) const { |
| 147 | SkColorType colorType = canvas->imageInfo().colorType(); |
| 148 | if (kRGB_565_SkColorType == colorType && |
| 149 | CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) { |
| 150 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 151 | } |
| 152 | switch (fDstColorType) { |
| 153 | case CodecSrc::kGetFromCanvas_DstColorType: |
| 154 | break; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 155 | case CodecSrc::kGrayscale_Always_DstColorType: |
| 156 | colorType = kGray_8_SkColorType; |
| 157 | break; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 158 | default: |
| 159 | SkASSERT(false); |
| 160 | break; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 163 | std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 164 | if (nullptr == brd.get()) { |
| 165 | return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str())); |
| 166 | } |
| 167 | |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 168 | auto recommendedCT = brd->computeOutputColorType(colorType); |
| 169 | if (kRGB_565_SkColorType == colorType && recommendedCT != colorType) { |
| 170 | return Error::Nonfatal("Skip decoding non-opaque to 565."); |
Leon Scroggins III | 1dc8ecb | 2017-08-17 13:42:48 -0400 | [diff] [blame] | 171 | } |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 172 | colorType = recommendedCT; |
| 173 | |
| 174 | auto colorSpace = brd->computeOutputColorSpace(colorType, nullptr); |
Leon Scroggins III | 1dc8ecb | 2017-08-17 13:42:48 -0400 | [diff] [blame] | 175 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 176 | const uint32_t width = brd->width(); |
| 177 | const uint32_t height = brd->height(); |
| 178 | // Visually inspecting very small output images is not necessary. |
| 179 | if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) { |
| 180 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 181 | } |
| 182 | switch (fMode) { |
| 183 | case kFullImage_Mode: { |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 184 | SkBitmap bitmap; |
| 185 | if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height), |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 186 | fSampleSize, colorType, false, colorSpace)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 187 | return "Cannot decode (full) region."; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 188 | } |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 189 | alpha8_to_gray8(&bitmap); |
Matt Sarett | 8db74f1 | 2017-06-14 13:02:05 +0000 | [diff] [blame] | 190 | |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 191 | canvas->drawBitmap(bitmap, 0, 0); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 192 | return ""; |
| 193 | } |
| 194 | case kDivisor_Mode: { |
| 195 | const uint32_t divisor = 2; |
| 196 | if (width < divisor || height < divisor) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 197 | return Error::Nonfatal("Divisor is larger than image dimension."); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Use a border to test subsets that extend outside the image. |
| 201 | // We will not allow the border to be larger than the image dimensions. Allowing |
| 202 | // these large borders causes off by one errors that indicate a problem with the |
| 203 | // test suite, not a problem with the implementation. |
| 204 | const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor); |
| 205 | const uint32_t scaledBorder = SkTMin(5u, maxBorder); |
| 206 | const uint32_t unscaledBorder = scaledBorder * fSampleSize; |
| 207 | |
| 208 | // We may need to clear the canvas to avoid uninitialized memory. |
| 209 | // Assume we are scaling a 780x780 image with sampleSize = 8. |
| 210 | // The output image should be 97x97. |
| 211 | // Each subset will be 390x390. |
| 212 | // Each scaled subset be 48x48. |
| 213 | // Four scaled subsets will only fill a 96x96 image. |
| 214 | // The bottom row and last column will not be touched. |
| 215 | // This is an unfortunate result of our rounding rules when scaling. |
| 216 | // Maybe we need to consider testing scaled subsets without trying to |
| 217 | // combine them to match the full scaled image? Or maybe this is the |
| 218 | // best we can do? |
| 219 | canvas->clear(0); |
| 220 | |
| 221 | for (uint32_t x = 0; x < divisor; x++) { |
| 222 | for (uint32_t y = 0; y < divisor; y++) { |
| 223 | // Calculate the subset dimensions |
| 224 | uint32_t subsetWidth = width / divisor; |
| 225 | uint32_t subsetHeight = height / divisor; |
| 226 | const int left = x * subsetWidth; |
| 227 | const int top = y * subsetHeight; |
| 228 | |
| 229 | // Increase the size of the last subset in each row or column, when the |
| 230 | // divisor does not divide evenly into the image dimensions |
| 231 | subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0; |
| 232 | subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0; |
| 233 | |
| 234 | // Increase the size of the subset in order to have a border on each side |
| 235 | const int decodeLeft = left - unscaledBorder; |
| 236 | const int decodeTop = top - unscaledBorder; |
| 237 | const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2; |
| 238 | const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 239 | SkBitmap bitmap; |
| 240 | if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft, |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 241 | decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false, |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 242 | colorSpace)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 243 | return "Cannot decode region."; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 244 | } |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 245 | |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 246 | alpha8_to_gray8(&bitmap); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 247 | canvas->drawBitmapRect(bitmap, |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 248 | SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder, |
| 249 | (SkScalar) (subsetWidth / fSampleSize), |
| 250 | (SkScalar) (subsetHeight / fSampleSize)), |
| 251 | SkRect::MakeXYWH((SkScalar) (left / fSampleSize), |
| 252 | (SkScalar) (top / fSampleSize), |
| 253 | (SkScalar) (subsetWidth / fSampleSize), |
| 254 | (SkScalar) (subsetHeight / fSampleSize)), |
| 255 | nullptr); |
| 256 | } |
| 257 | } |
| 258 | return ""; |
| 259 | } |
| 260 | default: |
| 261 | SkASSERT(false); |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 262 | return "Error: Should not be reached."; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | SkISize BRDSrc::size() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 267 | std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 268 | if (brd) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 269 | return {SkTMax(1, brd->width() / (int)fSampleSize), |
| 270 | SkTMax(1, brd->height() / (int)fSampleSize)}; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 271 | } |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 272 | return {0, 0}; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static SkString get_scaled_name(const Path& path, float scale) { |
| 276 | return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale); |
| 277 | } |
| 278 | |
| 279 | Name BRDSrc::name() const { |
| 280 | // We will replicate the names used by CodecSrc so that images can |
| 281 | // be compared in Gold. |
| 282 | if (1 == fSampleSize) { |
| 283 | return SkOSPath::Basename(fPath.c_str()); |
| 284 | } |
msarett | 4b0778e | 2015-11-13 09:59:11 -0800 | [diff] [blame] | 285 | return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 289 | |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 290 | static bool serial_from_path_name(const SkString& path) { |
| 291 | if (!FLAGS_RAW_threading) { |
| 292 | static const char* const exts[] = { |
| 293 | "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw", |
| 294 | "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW", |
| 295 | }; |
| 296 | const char* actualExt = strrchr(path.c_str(), '.'); |
| 297 | if (actualExt) { |
| 298 | actualExt++; |
| 299 | for (auto* ext : exts) { |
| 300 | if (0 == strcmp(ext, actualExt)) { |
| 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 309 | CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType, |
| 310 | float scale) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 311 | : fPath(path) |
| 312 | , fMode(mode) |
| 313 | , fDstColorType(dstColorType) |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 314 | , fDstAlphaType(dstAlphaType) |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 315 | , fScale(scale) |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 316 | , fRunSerially(serial_from_path_name(path)) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 317 | {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 318 | |
mtklein | 99cab4e | 2015-07-31 06:43:04 -0700 | [diff] [blame] | 319 | bool CodecSrc::veto(SinkFlags flags) const { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 320 | // Test to direct raster backends (8888 and 565). |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 321 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 322 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 323 | |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 324 | // Allows us to test decodes to non-native 8888. |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 325 | static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 326 | if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | for (int y = 0; y < bitmap.height(); y++) { |
| 331 | uint32_t* row = (uint32_t*) bitmap.getAddr(0, y); |
| 332 | SkOpts::RGBA_to_BGRA(row, row, bitmap.width()); |
| 333 | } |
| 334 | } |
| 335 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 336 | static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType, |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 337 | CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 338 | switch (dstColorType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 339 | case CodecSrc::kGrayscale_Always_DstColorType: |
scroggo | 1a36192 | 2016-05-20 14:27:16 -0700 | [diff] [blame] | 340 | if (kRGB_565_SkColorType == canvasColorType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 341 | return false; |
| 342 | } |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 343 | *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 344 | break; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 345 | case CodecSrc::kNonNative8888_Always_DstColorType: |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 346 | if (kRGB_565_SkColorType == canvasColorType |
| 347 | || kRGBA_F16_SkColorType == canvasColorType) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | #ifdef SK_PMCOLOR_IS_RGBA |
| 351 | *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType); |
| 352 | #else |
| 353 | *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType); |
| 354 | #endif |
| 355 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 356 | default: |
msarett | 55f7bdd | 2016-02-16 13:24:54 -0800 | [diff] [blame] | 357 | if (kRGB_565_SkColorType == canvasColorType && |
| 358 | kOpaque_SkAlphaType != decodeInfo->alphaType()) { |
| 359 | return false; |
| 360 | } |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 361 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 362 | *decodeInfo = decodeInfo->makeColorType(canvasColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 366 | *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 370 | static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 371 | CodecSrc::DstColorType dstColorType, |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 372 | SkScalar left = 0, SkScalar top = 0) { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 373 | SkBitmap bitmap; |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 374 | bitmap.installPixels(info, pixels, rowBytes); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 375 | swap_rb_if_necessary(bitmap, dstColorType); |
| 376 | canvas->drawBitmap(bitmap, left, top); |
Yuqian Li | cb31248 | 2018-04-27 15:52:40 -0400 | [diff] [blame] | 377 | canvas->flush(); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 380 | // For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or |
| 381 | // color format conversions should be performed by the codec. Sometimes the output of the |
| 382 | // decode will be in an interesting color space. On our srgb and f16 backends, we need to |
| 383 | // "pretend" that the color space is standard sRGB to avoid triggering color conversion |
| 384 | // at draw time. |
| 385 | static void set_bitmap_color_space(SkImageInfo* info) { |
Brian Osman | 6b62296 | 2018-08-27 19:16:02 +0000 | [diff] [blame] | 386 | *info = info->makeColorSpace(SkColorSpace::MakeSRGB()); |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 387 | } |
| 388 | |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 389 | Error CodecSrc::draw(SkCanvas* canvas) const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 390 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 391 | if (!encoded) { |
| 392 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 393 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 394 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 395 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
msarett | 9e707a0 | 2015-09-01 14:57:57 -0700 | [diff] [blame] | 396 | if (nullptr == codec.get()) { |
| 397 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 398 | } |
| 399 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 400 | SkImageInfo decodeInfo = codec->getInfo(); |
| 401 | if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType, |
| 402 | fDstAlphaType)) { |
Matt Sarett | ea518f0 | 2017-02-03 11:58:59 -0800 | [diff] [blame] | 403 | return Error::Nonfatal("Skipping uninteresting test."); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 404 | } |
| 405 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 406 | // Try to scale the image if it is desired |
| 407 | SkISize size = codec->getScaledDimensions(fScale); |
| 408 | if (size == decodeInfo.dimensions() && 1.0f != fScale) { |
| 409 | return Error::Nonfatal("Test without scaling is uninteresting."); |
| 410 | } |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 411 | |
| 412 | // Visually inspecting very small output images is not necessary. We will |
| 413 | // cover these cases in unit testing. |
| 414 | if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) { |
| 415 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 416 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 417 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 418 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 419 | const int bpp = decodeInfo.bytesPerPixel(); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 420 | const size_t rowBytes = size.width() * bpp; |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 421 | const size_t safeSize = decodeInfo.computeByteSize(rowBytes); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 422 | SkAutoMalloc pixels(safeSize); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 423 | |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 424 | SkCodec::Options options; |
| 425 | if (kCodecZeroInit_Mode == fMode) { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 426 | memset(pixels.get(), 0, size.height() * rowBytes); |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 427 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 428 | } |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 429 | |
| 430 | SkImageInfo bitmapInfo = decodeInfo; |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 431 | set_bitmap_color_space(&bitmapInfo); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 432 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 433 | kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
| 434 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 435 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 436 | |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 437 | switch (fMode) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 438 | case kAnimated_Mode: { |
| 439 | std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo(); |
| 440 | if (frameInfos.size() <= 1) { |
| 441 | return SkStringPrintf("%s is not an animated image.", fPath.c_str()); |
| 442 | } |
| 443 | |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 444 | // As in CodecSrc::size(), compute a roughly square grid to draw the frames |
| 445 | // into. "factor" is the number of frames to draw on one row. There will be |
| 446 | // up to "factor" rows as well. |
| 447 | const float root = sqrt((float) frameInfos.size()); |
| 448 | const int factor = sk_float_ceil2int(root); |
| 449 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 450 | // Used to cache a frame that future frames will depend on. |
| 451 | SkAutoMalloc priorFramePixels; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 452 | int cachedFrame = SkCodec::kNoFrame; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 453 | for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 454 | options.fFrameIndex = i; |
| 455 | // Check for a prior frame |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 456 | const int reqFrame = frameInfos[i].fRequiredFrame; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 457 | if (reqFrame != SkCodec::kNoFrame && reqFrame == cachedFrame |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 458 | && priorFramePixels.get()) { |
| 459 | // Copy into pixels |
| 460 | memcpy(pixels.get(), priorFramePixels.get(), safeSize); |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 461 | options.fPriorFrame = reqFrame; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 462 | } else { |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 463 | options.fPriorFrame = SkCodec::kNoFrame; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 464 | } |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 465 | SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 466 | rowBytes, &options); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 467 | if (SkCodec::kInvalidInput == result && i > 0) { |
| 468 | // Some of our test images have truncated later frames. Treat that |
| 469 | // the same as incomplete. |
| 470 | result = SkCodec::kIncompleteInput; |
| 471 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 472 | switch (result) { |
| 473 | case SkCodec::kSuccess: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 474 | case SkCodec::kErrorInInput: |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 475 | case SkCodec::kIncompleteInput: { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 476 | // If the next frame depends on this one, store it in priorFrame. |
| 477 | // It is possible that we may discard a frame that future frames depend on, |
| 478 | // but the codec will simply redecode the discarded frame. |
| 479 | // Do this before calling draw_to_canvas, which premultiplies in place. If |
| 480 | // we're decoding to unpremul, we want to pass the unmodified frame to the |
| 481 | // codec for decoding the next frame. |
| 482 | if (static_cast<size_t>(i+1) < frameInfos.size() |
| 483 | && frameInfos[i+1].fRequiredFrame == i) { |
| 484 | memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize); |
| 485 | cachedFrame = i; |
| 486 | } |
| 487 | |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 488 | SkAutoCanvasRestore acr(canvas, true); |
| 489 | const int xTranslate = (i % factor) * decodeInfo.width(); |
| 490 | const int yTranslate = (i / factor) * decodeInfo.height(); |
| 491 | canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate)); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 492 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType); |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 493 | if (result != SkCodec::kSuccess) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 494 | return ""; |
| 495 | } |
| 496 | break; |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 497 | } |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 498 | case SkCodec::kInvalidConversion: |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 499 | if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) { |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 500 | return Error::Nonfatal(SkStringPrintf( |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 501 | "Cannot decode frame %i to 565 (%s).", i, fPath.c_str())); |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 502 | } |
| 503 | // Fall through. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 504 | default: |
| 505 | return SkStringPrintf("Couldn't getPixels for frame %i in %s.", |
| 506 | i, fPath.c_str()); |
| 507 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 508 | } |
| 509 | break; |
| 510 | } |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 511 | case kCodecZeroInit_Mode: |
msarett | 9e707a0 | 2015-09-01 14:57:57 -0700 | [diff] [blame] | 512 | case kCodec_Mode: { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 513 | switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 514 | case SkCodec::kSuccess: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 515 | // We consider these to be valid, since we should still decode what is |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 516 | // available. |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 517 | case SkCodec::kErrorInInput: |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 518 | case SkCodec::kIncompleteInput: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 519 | break; |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 520 | default: |
| 521 | // Everything else is considered a failure. |
| 522 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 523 | } |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 524 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 525 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType); |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 526 | break; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 527 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 528 | case kScanline_Mode: { |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 529 | void* dst = pixels.get(); |
| 530 | uint32_t height = decodeInfo.height(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 531 | const bool useIncremental = [this]() { |
| 532 | auto exts = { "png", "PNG", "gif", "GIF" }; |
| 533 | for (auto ext : exts) { |
| 534 | if (fPath.endsWith(ext)) { |
| 535 | return true; |
| 536 | } |
| 537 | } |
| 538 | return false; |
| 539 | }(); |
| 540 | // ico may use the old scanline method or the new one, depending on whether it |
| 541 | // internally holds a bmp or a png. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 542 | const bool ico = fPath.endsWith("ico"); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 543 | bool useOldScanlineMethod = !useIncremental && !ico; |
| 544 | if (useIncremental || ico) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 545 | if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 546 | rowBytes, &options)) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 547 | int rowsDecoded; |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 548 | auto result = codec->incrementalDecode(&rowsDecoded); |
| 549 | if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 550 | codec->fillIncompleteImage(decodeInfo, dst, rowBytes, |
| 551 | SkCodec::kNo_ZeroInitialized, height, |
| 552 | rowsDecoded); |
scroggo | 6fb2391 | 2016-06-02 14:16:43 -0700 | [diff] [blame] | 553 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 554 | } else { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 555 | if (useIncremental) { |
| 556 | // Error: These should support incremental decode. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 557 | return "Could not start incremental decode"; |
| 558 | } |
| 559 | // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP, |
| 560 | // which should work via startScanlineDecode |
| 561 | useOldScanlineMethod = true; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | if (useOldScanlineMethod) { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 566 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 567 | return "Could not start scanline decoder"; |
| 568 | } |
| 569 | |
Brian Osman | ea176c6 | 2018-04-06 15:28:23 -0400 | [diff] [blame] | 570 | // We do not need to check the return value. On an incomplete |
| 571 | // image, memory will be filled with a default value. |
| 572 | codec->getScanlines(dst, height, rowBytes); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 575 | draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 576 | break; |
| 577 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 578 | case kStripe_Mode: { |
| 579 | const int height = decodeInfo.height(); |
| 580 | // This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 581 | // does not align with image blocks. |
| 582 | const int stripeHeight = 37; |
| 583 | const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 584 | void* dst = pixels.get(); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 585 | |
| 586 | // Decode odd stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 587 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) { |
msarett | b65e604 | 2016-02-23 05:37:25 -0800 | [diff] [blame] | 588 | return "Could not start scanline decoder"; |
| 589 | } |
| 590 | |
| 591 | // This mode was designed to test the new skip scanlines API in libjpeg-turbo. |
| 592 | // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting |
| 593 | // to run this test for image types that do not have this scanline ordering. |
scroggo | 12e2f50 | 2016-05-16 09:04:18 -0700 | [diff] [blame] | 594 | // We only run this on Jpeg, which is always kTopDown. |
| 595 | SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 596 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 597 | for (int i = 0; i < numStripes; i += 2) { |
| 598 | // Skip a stripe |
| 599 | const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 600 | codec->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 601 | |
| 602 | // Read a stripe |
| 603 | const int startY = (i + 1) * stripeHeight; |
| 604 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 605 | if (linesToRead > 0) { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 606 | codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead, |
| 607 | rowBytes); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | // Decode even stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 612 | const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 613 | if (SkCodec::kSuccess != startResult) { |
| 614 | return "Failed to restart scanline decoder with same parameters."; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 615 | } |
| 616 | for (int i = 0; i < numStripes; i += 2) { |
| 617 | // Read a stripe |
| 618 | const int startY = i * stripeHeight; |
| 619 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 620 | codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead, |
| 621 | rowBytes); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 622 | |
| 623 | // Skip a stripe |
msarett | f6db27e | 2015-06-12 09:34:04 -0700 | [diff] [blame] | 624 | const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); |
| 625 | if (linesToSkip > 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 626 | codec->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 629 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 630 | draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 631 | break; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 632 | } |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 633 | case kCroppedScanline_Mode: { |
| 634 | const int width = decodeInfo.width(); |
| 635 | const int height = decodeInfo.height(); |
| 636 | // This value is chosen because, as we move across the image, it will sometimes |
| 637 | // align with the jpeg block sizes and it will sometimes not. This allows us |
| 638 | // to test interestingly different code paths in the implementation. |
| 639 | const int tileSize = 36; |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 640 | SkIRect subset; |
| 641 | for (int x = 0; x < width; x += tileSize) { |
| 642 | subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height); |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 643 | options.fSubset = ⊂ |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 644 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) { |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 645 | return "Could not start scanline decoder."; |
| 646 | } |
| 647 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 648 | codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes); |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 651 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType); |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 652 | break; |
| 653 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 654 | case kSubset_Mode: { |
| 655 | // Arbitrarily choose a divisor. |
| 656 | int divisor = 2; |
| 657 | // Total width/height of the image. |
| 658 | const int W = codec->getInfo().width(); |
| 659 | const int H = codec->getInfo().height(); |
| 660 | if (divisor > W || divisor > H) { |
| 661 | return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big " |
| 662 | "for %s with dimensions (%d x %d)", divisor, |
| 663 | fPath.c_str(), W, H)); |
| 664 | } |
| 665 | // subset dimensions |
| 666 | // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 667 | const int w = SkAlign2(W / divisor); |
| 668 | const int h = SkAlign2(H / divisor); |
| 669 | SkIRect subset; |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 670 | options.fSubset = ⊂ |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 671 | SkBitmap subsetBm; |
| 672 | // We will reuse pixel memory from bitmap. |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 673 | void* dst = pixels.get(); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 674 | // Keep track of left and top (for drawing subsetBm into canvas). We could use |
| 675 | // fScale * x and fScale * y, but we want integers such that the next subset will start |
| 676 | // where the last one ended. So we'll add decodeInfo.width() and height(). |
| 677 | int left = 0; |
| 678 | for (int x = 0; x < W; x += w) { |
| 679 | int top = 0; |
| 680 | for (int y = 0; y < H; y+= h) { |
| 681 | // Do not make the subset go off the edge of the image. |
| 682 | const int preScaleW = SkTMin(w, W - x); |
| 683 | const int preScaleH = SkTMin(h, H - y); |
| 684 | subset.setXYWH(x, y, preScaleW, preScaleH); |
| 685 | // And scale |
| 686 | // FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 687 | // into account? |
msarett | c7eb490 | 2016-04-25 07:04:58 -0700 | [diff] [blame] | 688 | const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)); |
| 689 | const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)); |
| 690 | decodeInfo = decodeInfo.makeWH(scaledW, scaledH); |
| 691 | SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 692 | size_t subsetRowBytes = subsetBitmapInfo.minRowBytes(); |
| 693 | const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 694 | &options); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 695 | switch (result) { |
| 696 | case SkCodec::kSuccess: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 697 | case SkCodec::kErrorInInput: |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 698 | case SkCodec::kIncompleteInput: |
| 699 | break; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 700 | default: |
| 701 | return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) " |
| 702 | "from %s with dimensions (%d x %d)\t error %d", |
| 703 | x, y, decodeInfo.width(), decodeInfo.height(), |
| 704 | fPath.c_str(), W, H, result); |
| 705 | } |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 706 | draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType, |
| 707 | SkIntToScalar(left), SkIntToScalar(top)); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 708 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 709 | // translate by the scaled height. |
| 710 | top += decodeInfo.height(); |
| 711 | } |
| 712 | // translate by the scaled width. |
| 713 | left += decodeInfo.width(); |
| 714 | } |
| 715 | return ""; |
| 716 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 717 | default: |
| 718 | SkASSERT(false); |
| 719 | return "Invalid fMode"; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 720 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 721 | return ""; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | SkISize CodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 725 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 726 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
scroggo | 7fac5af | 2015-09-30 11:33:12 -0700 | [diff] [blame] | 727 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 728 | return {0, 0}; |
scroggo | 7fac5af | 2015-09-30 11:33:12 -0700 | [diff] [blame] | 729 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 730 | |
| 731 | auto imageSize = codec->getScaledDimensions(fScale); |
| 732 | if (fMode == kAnimated_Mode) { |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 733 | // We'll draw one of each frame, so make it big enough to hold them all |
| 734 | // in a grid. The grid will be roughly square, with "factor" frames per |
| 735 | // row and up to "factor" rows. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 736 | const size_t count = codec->getFrameInfo().size(); |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 737 | const float root = sqrt((float) count); |
| 738 | const int factor = sk_float_ceil2int(root); |
| 739 | imageSize.fWidth = imageSize.fWidth * factor; |
| 740 | imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 741 | } |
| 742 | return imageSize; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | Name CodecSrc::name() const { |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 746 | if (1.0f == fScale) { |
scroggo | 6e8c68e | 2016-10-24 13:48:49 -0700 | [diff] [blame] | 747 | Name name = SkOSPath::Basename(fPath.c_str()); |
| 748 | if (fMode == kAnimated_Mode) { |
| 749 | name.append("_animated"); |
| 750 | } |
| 751 | return name; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 752 | } |
scroggo | 6e8c68e | 2016-10-24 13:48:49 -0700 | [diff] [blame] | 753 | SkASSERT(fMode != kAnimated_Mode); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 754 | return get_scaled_name(fPath, fScale); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 758 | |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 759 | AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType, |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 760 | SkAlphaType dstAlphaType, int sampleSize) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 761 | : fPath(path) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 762 | , fDstColorType(dstColorType) |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 763 | , fDstAlphaType(dstAlphaType) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 764 | , fSampleSize(sampleSize) |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 765 | , fRunSerially(serial_from_path_name(path)) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 766 | {} |
| 767 | |
| 768 | bool AndroidCodecSrc::veto(SinkFlags flags) const { |
| 769 | // No need to test decoding to non-raster or indirect backend. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 770 | return flags.type != SinkFlags::kRaster |
| 771 | || flags.approach != SinkFlags::kDirect; |
| 772 | } |
| 773 | |
| 774 | Error AndroidCodecSrc::draw(SkCanvas* canvas) const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 775 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 776 | if (!encoded) { |
| 777 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 778 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 779 | std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded)); |
| 780 | if (nullptr == codec) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 781 | return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str()); |
| 782 | } |
| 783 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 784 | SkImageInfo decodeInfo = codec->getInfo(); |
| 785 | if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType, |
| 786 | fDstAlphaType)) { |
Matt Sarett | ea518f0 | 2017-02-03 11:58:59 -0800 | [diff] [blame] | 787 | return Error::Nonfatal("Skipping uninteresting test."); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // Scale the image if it is desired. |
| 791 | SkISize size = codec->getSampledDimensions(fSampleSize); |
| 792 | |
| 793 | // Visually inspecting very small output images is not necessary. We will |
| 794 | // cover these cases in unit testing. |
| 795 | if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) { |
| 796 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 797 | } |
| 798 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 799 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 800 | int bpp = decodeInfo.bytesPerPixel(); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 801 | size_t rowBytes = size.width() * bpp; |
| 802 | SkAutoMalloc pixels(size.height() * rowBytes); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 803 | |
| 804 | SkBitmap bitmap; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 805 | SkImageInfo bitmapInfo = decodeInfo; |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 806 | set_bitmap_color_space(&bitmapInfo); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 807 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 808 | kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
| 809 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 810 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 811 | |
| 812 | // Create options for the codec. |
| 813 | SkAndroidCodec::AndroidOptions options; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 814 | options.fSampleSize = fSampleSize; |
| 815 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 816 | switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) { |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 817 | case SkCodec::kSuccess: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 818 | case SkCodec::kErrorInInput: |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 819 | case SkCodec::kIncompleteInput: |
| 820 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 821 | default: |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 822 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 823 | } |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 824 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType); |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 825 | return ""; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | SkISize AndroidCodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 829 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 830 | std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded)); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 831 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 832 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 833 | } |
| 834 | return codec->getSampledDimensions(fSampleSize); |
| 835 | } |
| 836 | |
| 837 | Name AndroidCodecSrc::name() const { |
| 838 | // We will replicate the names used by CodecSrc so that images can |
| 839 | // be compared in Gold. |
| 840 | if (1 == fSampleSize) { |
| 841 | return SkOSPath::Basename(fPath.c_str()); |
| 842 | } |
msarett | 4b0778e | 2015-11-13 09:59:11 -0800 | [diff] [blame] | 843 | return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 847 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 848 | ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu) |
| 849 | : fPath(path) |
| 850 | , fMode(mode) |
| 851 | , fDstAlphaType(alphaType) |
| 852 | , fIsGpu(isGpu) |
| 853 | , fRunSerially(serial_from_path_name(path)) |
| 854 | {} |
| 855 | |
| 856 | bool ImageGenSrc::veto(SinkFlags flags) const { |
| 857 | if (fIsGpu) { |
Brian Salomon | bd7c551 | 2017-03-07 09:08:36 -0500 | [diff] [blame] | 858 | // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs. |
| 859 | return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect || |
| 860 | flags.multisampled == SinkFlags::kMultisampled; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
| 864 | } |
| 865 | |
| 866 | Error ImageGenSrc::draw(SkCanvas* canvas) const { |
| 867 | if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 868 | return Error::Nonfatal("Uninteresting to test image generator to 565."); |
| 869 | } |
| 870 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 871 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 872 | if (!encoded) { |
| 873 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 874 | } |
| 875 | |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 876 | #if defined(SK_BUILD_FOR_WIN) |
| 877 | // Initialize COM in order to test with WIC. |
| 878 | SkAutoCoInitialize com; |
| 879 | if (!com.succeeded()) { |
| 880 | return "Could not initialize COM."; |
| 881 | } |
| 882 | #endif |
| 883 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 884 | std::unique_ptr<SkImageGenerator> gen(nullptr); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 885 | switch (fMode) { |
| 886 | case kCodec_Mode: |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 887 | gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 888 | if (!gen) { |
| 889 | return "Could not create codec image generator."; |
| 890 | } |
| 891 | break; |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 892 | case kPlatform_Mode: { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 893 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame] | 894 | gen = SkImageGeneratorCG::MakeFromEncodedCG(encoded); |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 895 | #elif defined(SK_BUILD_FOR_WIN) |
Leon Scroggins III | a57488a | 2018-06-25 14:01:29 -0400 | [diff] [blame] | 896 | gen = SkImageGeneratorWIC::MakeFromEncodedWIC(encoded); |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 897 | #endif |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 898 | if (!gen) { |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 899 | return "Could not create platform image generator."; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 900 | } |
| 901 | break; |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 902 | } |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 903 | default: |
| 904 | SkASSERT(false); |
| 905 | return "Invalid image generator mode"; |
| 906 | } |
| 907 | |
| 908 | // Test deferred decoding path on GPU |
| 909 | if (fIsGpu) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 910 | sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 911 | if (!image) { |
| 912 | return "Could not create image from codec image generator."; |
| 913 | } |
| 914 | canvas->drawImage(image, 0, 0); |
| 915 | return ""; |
| 916 | } |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 917 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 918 | // Test various color and alpha types on CPU |
| 919 | SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType); |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 920 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 921 | int bpp = decodeInfo.bytesPerPixel(); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 922 | size_t rowBytes = decodeInfo.width() * bpp; |
| 923 | SkAutoMalloc pixels(decodeInfo.height() * rowBytes); |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 924 | if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes)) { |
Matt Sarett | 05cb4c3 | 2017-03-02 12:07:46 -0500 | [diff] [blame] | 925 | SkString err = |
| 926 | SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str()); |
| 927 | |
| 928 | #if defined(SK_BUILD_FOR_WIN) |
| 929 | if (kPlatform_Mode == fMode) { |
| 930 | // Do not issue a fatal error for WIC flakiness. |
| 931 | return Error::Nonfatal(err); |
| 932 | } |
| 933 | #endif |
| 934 | |
| 935 | return err; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 938 | set_bitmap_color_space(&decodeInfo); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 939 | draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes, |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 940 | CodecSrc::kGetFromCanvas_DstColorType); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 941 | return ""; |
| 942 | } |
| 943 | |
| 944 | SkISize ImageGenSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 945 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 946 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 947 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 948 | return {0, 0}; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 949 | } |
| 950 | return codec->getInfo().dimensions(); |
| 951 | } |
| 952 | |
| 953 | Name ImageGenSrc::name() const { |
| 954 | return SkOSPath::Basename(fPath.c_str()); |
| 955 | } |
| 956 | |
| 957 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 958 | |
msarett | 9ce3a54 | 2016-07-15 13:54:38 -0700 | [diff] [blame] | 959 | ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, SkColorType colorType) |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 960 | : fPath(path) |
| 961 | , fMode(mode) |
msarett | 9ce3a54 | 2016-07-15 13:54:38 -0700 | [diff] [blame] | 962 | , fColorType(colorType) |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 963 | {} |
| 964 | |
| 965 | bool ColorCodecSrc::veto(SinkFlags flags) const { |
| 966 | // Test to direct raster backends (8888 and 565). |
| 967 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
| 968 | } |
| 969 | |
Matt Sarett | 6a0feba | 2017-06-09 10:45:44 -0400 | [diff] [blame] | 970 | void clamp_if_necessary(const SkBitmap& bitmap, SkColorType dstCT) { |
| 971 | if (kRGBA_F16_SkColorType != bitmap.colorType() || kRGBA_F16_SkColorType == dstCT) { |
| 972 | // No need to clamp if the dst is F16. We will clamp when we encode to PNG. |
| 973 | return; |
| 974 | } |
| 975 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 976 | SkRasterPipeline_MemoryCtx ptr = { bitmap.getAddr(0,0), bitmap.rowBytesAsPixels() }; |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 977 | |
Matt Sarett | 6a0feba | 2017-06-09 10:45:44 -0400 | [diff] [blame] | 978 | SkRasterPipeline_<256> p; |
| 979 | p.append(SkRasterPipeline::load_f16, &ptr); |
| 980 | p.append(SkRasterPipeline::clamp_0); |
| 981 | if (kPremul_SkAlphaType == bitmap.alphaType()) { |
| 982 | p.append(SkRasterPipeline::clamp_a); |
| 983 | } else { |
| 984 | p.append(SkRasterPipeline::clamp_1); |
| 985 | } |
| 986 | p.append(SkRasterPipeline::store_f16, &ptr); |
| 987 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 988 | p.run(0,0, bitmap.width(), bitmap.height()); |
Matt Sarett | 6a0feba | 2017-06-09 10:45:44 -0400 | [diff] [blame] | 989 | } |
| 990 | |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 991 | Error ColorCodecSrc::draw(SkCanvas* canvas) const { |
| 992 | if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 993 | return Error::Nonfatal("No need to test color correction to 565 backend."); |
| 994 | } |
| 995 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 996 | bool runInLegacyMode = kBaseline_Mode == fMode; |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 997 | if (runInLegacyMode && canvas->imageInfo().colorSpace()) { |
| 998 | return Error::Nonfatal("Skipping tests that are only interesting in legacy mode."); |
| 999 | } else if (!runInLegacyMode && !canvas->imageInfo().colorSpace()) { |
| 1000 | return Error::Nonfatal("Skipping tests that are only interesting in srgb mode."); |
msarett | 9ce3a54 | 2016-07-15 13:54:38 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 1003 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1004 | if (!encoded) { |
| 1005 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 1006 | } |
| 1007 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 1008 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
| 1009 | if (nullptr == codec) { |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1010 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| 1011 | } |
| 1012 | |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 1013 | // Load the dst ICC profile. This particular dst is fairly similar to Adobe RGB. |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 1014 | sk_sp<SkData> dstData = GetResourceAsData("icc_profiles/HP_ZR30w.icc"); |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 1015 | if (!dstData) { |
| 1016 | return "Cannot read monitor profile. Is the resource path set correctly?"; |
| 1017 | } |
| 1018 | |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1019 | sk_sp<SkColorSpace> dstSpace = nullptr; |
| 1020 | if (kDst_sRGB_Mode == fMode) { |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 1021 | dstSpace = SkColorSpace::MakeSRGB(); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1022 | } else if (kDst_HPZR30w_Mode == fMode) { |
Brian Osman | c494a70 | 2018-05-23 11:52:00 -0400 | [diff] [blame] | 1023 | skcms_ICCProfile profile; |
| 1024 | SkAssertResult(skcms_Parse(dstData->data(), dstData->size(), &profile)); |
| 1025 | dstSpace = SkColorSpace::Make(profile); |
| 1026 | SkASSERT(dstSpace); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | SkImageInfo decodeInfo = codec->getInfo().makeColorType(fColorType).makeColorSpace(dstSpace); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1030 | if (kUnpremul_SkAlphaType == decodeInfo.alphaType()) { |
| 1031 | decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType); |
| 1032 | } |
| 1033 | |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1034 | SkImageInfo bitmapInfo = decodeInfo; |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 1035 | set_bitmap_color_space(&bitmapInfo); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1036 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 1037 | kBGRA_8888_SkColorType == decodeInfo.colorType()) |
| 1038 | { |
| 1039 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 1040 | } |
| 1041 | |
| 1042 | SkBitmap bitmap; |
| 1043 | if (!bitmap.tryAllocPixels(bitmapInfo)) { |
| 1044 | return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
| 1045 | bitmapInfo.width(), bitmapInfo.height()); |
| 1046 | } |
| 1047 | |
| 1048 | size_t rowBytes = bitmap.rowBytes(); |
| 1049 | SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), rowBytes); |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 1050 | switch (r) { |
| 1051 | case SkCodec::kSuccess: |
| 1052 | case SkCodec::kErrorInInput: |
| 1053 | case SkCodec::kIncompleteInput: |
| 1054 | break; |
| 1055 | default: |
| 1056 | return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1059 | switch (fMode) { |
| 1060 | case kBaseline_Mode: |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 1061 | case kDst_sRGB_Mode: |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1062 | case kDst_HPZR30w_Mode: |
Matt Sarett | 6a0feba | 2017-06-09 10:45:44 -0400 | [diff] [blame] | 1063 | // We do not support drawing unclamped F16. |
| 1064 | clamp_if_necessary(bitmap, canvas->imageInfo().colorType()); |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 1065 | canvas->drawBitmap(bitmap, 0, 0); |
| 1066 | break; |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1067 | default: |
| 1068 | SkASSERT(false); |
| 1069 | return "Invalid fMode"; |
| 1070 | } |
| 1071 | return ""; |
| 1072 | } |
| 1073 | |
| 1074 | SkISize ColorCodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 1075 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 1076 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1077 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1078 | return {0, 0}; |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1079 | } |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1080 | return {codec->getInfo().width(), codec->getInfo().height()}; |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | Name ColorCodecSrc::name() const { |
| 1084 | return SkOSPath::Basename(fPath.c_str()); |
| 1085 | } |
| 1086 | |
| 1087 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1088 | |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1089 | SKPSrc::SKPSrc(Path path) : fPath(path) { } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1090 | |
Robert Phillips | e4aae34 | 2018-03-14 10:26:57 -0400 | [diff] [blame] | 1091 | static sk_sp<SkPicture> read_skp(const char* path, const SkDeserialProcs* procs = nullptr) { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1092 | std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 1093 | if (!stream) { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1094 | return nullptr; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1095 | } |
Robert Phillips | e4aae34 | 2018-03-14 10:26:57 -0400 | [diff] [blame] | 1096 | sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get(), procs)); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 1097 | if (!pic) { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1098 | return nullptr; |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 1099 | } |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1100 | stream = nullptr; // Might as well drop this when we're done with it. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 1101 | |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1102 | return pic; |
| 1103 | } |
| 1104 | |
| 1105 | Error SKPSrc::draw(SkCanvas* canvas) const { |
| 1106 | sk_sp<SkPicture> pic = read_skp(fPath.c_str()); |
| 1107 | if (!pic) { |
| 1108 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 1109 | } |
| 1110 | |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1111 | canvas->clipRect(SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1112 | canvas->drawPicture(pic); |
| 1113 | return ""; |
| 1114 | } |
| 1115 | |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1116 | static SkRect get_cull_rect_for_skp(const char* path) { |
| 1117 | std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path); |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1118 | if (!stream) { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1119 | return SkRect::MakeEmpty(); |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1120 | } |
| 1121 | SkPictInfo info; |
Mike Reed | e7a5832 | 2017-12-20 14:09:20 -0500 | [diff] [blame] | 1122 | if (!SkPicture_StreamIsSKP(stream.get(), &info)) { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1123 | return SkRect::MakeEmpty(); |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1124 | } |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1125 | |
| 1126 | return info.fCullRect; |
| 1127 | } |
| 1128 | |
| 1129 | SkISize SKPSrc::size() const { |
| 1130 | SkRect viewport = get_cull_rect_for_skp(fPath.c_str()); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1131 | if (!viewport.intersect((SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize)))) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1132 | return {0, 0}; |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1133 | } |
| 1134 | return viewport.roundOut().size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 1138 | |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1139 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1140 | |
Chris Dalton | 184c37e | 2018-09-28 11:27:39 -0600 | [diff] [blame] | 1141 | BisectSrc::BisectSrc(Path path, const char* trail) : INHERITED(path), fTrail(trail) {} |
| 1142 | |
| 1143 | Error BisectSrc::draw(SkCanvas* canvas) const { |
| 1144 | struct FoundPath { |
| 1145 | SkPath fPath; |
| 1146 | SkPaint fPaint; |
| 1147 | SkMatrix fViewMatrix; |
| 1148 | }; |
| 1149 | |
| 1150 | // This subclass of SkCanvas just extracts all the SkPaths (drawn via drawPath) from an SKP. |
| 1151 | class PathFindingCanvas : public SkCanvas { |
| 1152 | public: |
| 1153 | PathFindingCanvas(int width, int height) : SkCanvas(width, height, nullptr) {} |
| 1154 | const SkTArray<FoundPath>& foundPaths() const { return fFoundPaths; } |
| 1155 | |
| 1156 | private: |
| 1157 | void onDrawPath(const SkPath& path, const SkPaint& paint) override { |
| 1158 | fFoundPaths.push_back() = {path, paint, this->getTotalMatrix()}; |
| 1159 | } |
| 1160 | |
| 1161 | SkTArray<FoundPath> fFoundPaths; |
| 1162 | }; |
| 1163 | |
| 1164 | PathFindingCanvas pathFinder(canvas->getBaseLayerSize().width(), |
| 1165 | canvas->getBaseLayerSize().height()); |
| 1166 | Error err = this->INHERITED::draw(&pathFinder); |
| 1167 | if (!err.isEmpty()) { |
| 1168 | return err; |
| 1169 | } |
| 1170 | |
| 1171 | int start = 0, end = pathFinder.foundPaths().count(); |
| 1172 | for (const char* ch = fTrail.c_str(); *ch; ++ch) { |
| 1173 | int midpt = (start + end) / 2; |
| 1174 | if ('l' == *ch) { |
| 1175 | start = midpt; |
| 1176 | } else if ('r' == *ch) { |
| 1177 | end = midpt; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | for (int i = start; i < end; ++i) { |
| 1182 | const FoundPath& path = pathFinder.foundPaths()[i]; |
| 1183 | SkAutoCanvasRestore acr(canvas, true); |
| 1184 | canvas->concat(path.fViewMatrix); |
| 1185 | canvas->drawPath(path.fPath, path.fPaint); |
| 1186 | } |
| 1187 | |
| 1188 | return ""; |
| 1189 | } |
| 1190 | |
| 1191 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1192 | |
Florin Malita | 87ccf33 | 2018-05-04 12:23:24 -0400 | [diff] [blame] | 1193 | #if defined(SK_ENABLE_SKOTTIE) |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1194 | SkottieSrc::SkottieSrc(Path path) : fPath(std::move(path)) {} |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1195 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 1196 | Error SkottieSrc::draw(SkCanvas* canvas) const { |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 1197 | auto animation = skottie::Animation::Builder() |
| 1198 | .setResourceProvider( |
| 1199 | skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()))) |
| 1200 | .makeFromFile(fPath.c_str()); |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1201 | if (!animation) { |
| 1202 | return SkStringPrintf("Unable to parse file: %s", fPath.c_str()); |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | canvas->drawColor(SK_ColorWHITE); |
| 1206 | |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 1207 | const auto t_rate = 1.0f / (kTileCount * kTileCount - 1); |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1208 | |
Kevin Lubick | c5fe15d | 2018-08-29 09:52:52 -0400 | [diff] [blame] | 1209 | // Draw the frames in a shuffled order to exercise non-linear |
| 1210 | // frame progression. The film strip will still be in order left-to-right, |
| 1211 | // top-down, just not drawn in that order. |
| 1212 | static constexpr int frameOrder[] = { 4, 0, 3, 1, 2 }; |
| 1213 | static_assert(SK_ARRAY_COUNT(frameOrder) == kTileCount, ""); |
Florin Malita | d3c1b84 | 2018-01-27 12:43:24 -0500 | [diff] [blame] | 1214 | |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1215 | for (int i = 0; i < kTileCount; ++i) { |
Kevin Lubick | c5fe15d | 2018-08-29 09:52:52 -0400 | [diff] [blame] | 1216 | const SkScalar y = frameOrder[i] * kTileSize; |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1217 | |
| 1218 | for (int j = 0; j < kTileCount; ++j) { |
Kevin Lubick | c5fe15d | 2018-08-29 09:52:52 -0400 | [diff] [blame] | 1219 | const SkScalar x = frameOrder[j] * kTileSize; |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1220 | SkRect dest = SkRect::MakeXYWH(x, y, kTileSize, kTileSize); |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1221 | |
Kevin Lubick | c5fe15d | 2018-08-29 09:52:52 -0400 | [diff] [blame] | 1222 | const auto t = t_rate * (frameOrder[i] * kTileCount + frameOrder[j]); |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 1223 | { |
| 1224 | SkAutoCanvasRestore acr(canvas, true); |
Florin Malita | d3c1b84 | 2018-01-27 12:43:24 -0500 | [diff] [blame] | 1225 | canvas->clipRect(dest, true); |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1226 | canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()), |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 1227 | dest, |
Florin Malita | 3fae0f3 | 2018-03-15 21:52:54 -0400 | [diff] [blame] | 1228 | SkMatrix::kCenter_ScaleToFit)); |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1229 | animation->seek(t); |
| 1230 | animation->render(canvas); |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 1231 | } |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | return ""; |
| 1236 | } |
| 1237 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 1238 | SkISize SkottieSrc::size() const { |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1239 | return SkISize::Make(kTargetSize, kTargetSize); |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1240 | } |
| 1241 | |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1242 | Name SkottieSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1243 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 1244 | bool SkottieSrc::veto(SinkFlags flags) const { |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1245 | // No need to test to non-(raster||gpu||vector) or indirect backends. |
| 1246 | bool type_ok = flags.type == SinkFlags::kRaster |
| 1247 | || flags.type == SinkFlags::kGPU |
| 1248 | || flags.type == SinkFlags::kVector; |
| 1249 | |
| 1250 | return !type_ok || flags.approach != SinkFlags::kDirect; |
| 1251 | } |
Florin Malita | 124d5af | 2017-12-31 17:02:26 -0500 | [diff] [blame] | 1252 | #endif |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1253 | |
| 1254 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1255 | #if defined(SK_XML) |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1256 | // Used when the image doesn't have an intrinsic size. |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1257 | static const SkSize kDefaultSVGSize = {1000, 1000}; |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1258 | |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1259 | // Used to force-scale tiny fixed-size images. |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1260 | static const SkSize kMinimumSVGSize = {128, 128}; |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1261 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1262 | SVGSrc::SVGSrc(Path path) |
| 1263 | : fName(SkOSPath::Basename(path.c_str())) |
| 1264 | , fScale(1) { |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1265 | |
Brian Osman | 133823d | 2018-09-19 14:14:15 -0400 | [diff] [blame] | 1266 | sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str())); |
| 1267 | if (!data) { |
| 1268 | return; |
| 1269 | } |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1270 | |
Brian Osman | 133823d | 2018-09-19 14:14:15 -0400 | [diff] [blame] | 1271 | SkMemoryStream stream(std::move(data)); |
| 1272 | fDom = SkSVGDOM::MakeFromStream(stream); |
| 1273 | if (!fDom) { |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | const SkSize& sz = fDom->containerSize(); |
| 1278 | if (sz.isEmpty()) { |
| 1279 | // no intrinsic size |
| 1280 | fDom->setContainerSize(kDefaultSVGSize); |
| 1281 | } else { |
| 1282 | fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(), |
| 1283 | kMinimumSVGSize.height() / sz.height())); |
| 1284 | } |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | Error SVGSrc::draw(SkCanvas* canvas) const { |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1288 | if (!fDom) { |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1289 | return SkStringPrintf("Unable to parse file: %s", fName.c_str()); |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1292 | SkAutoCanvasRestore acr(canvas, true); |
| 1293 | canvas->scale(fScale, fScale); |
| 1294 | fDom->render(canvas); |
| 1295 | |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1296 | return ""; |
| 1297 | } |
| 1298 | |
| 1299 | SkISize SVGSrc::size() const { |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1300 | if (!fDom) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1301 | return {0, 0}; |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1304 | return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale} |
| 1305 | .toRound(); |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1308 | Name SVGSrc::name() const { return fName; } |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1309 | |
fmalita | 179d885 | 2016-08-16 14:23:29 -0700 | [diff] [blame] | 1310 | bool SVGSrc::veto(SinkFlags flags) const { |
Florin Malita | 93323eb | 2017-06-16 15:31:17 -0400 | [diff] [blame] | 1311 | // No need to test to non-(raster||gpu||vector) or indirect backends. |
fmalita | 179d885 | 2016-08-16 14:23:29 -0700 | [diff] [blame] | 1312 | bool type_ok = flags.type == SinkFlags::kRaster |
Florin Malita | 93323eb | 2017-06-16 15:31:17 -0400 | [diff] [blame] | 1313 | || flags.type == SinkFlags::kGPU |
| 1314 | || flags.type == SinkFlags::kVector; |
fmalita | 179d885 | 2016-08-16 14:23:29 -0700 | [diff] [blame] | 1315 | |
| 1316 | return !type_ok || flags.approach != SinkFlags::kDirect; |
| 1317 | } |
| 1318 | |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1319 | #endif // defined(SK_XML) |
| 1320 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1321 | |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1322 | MSKPSrc::MSKPSrc(Path path) : fPath(path) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1323 | std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str()); |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1324 | int count = SkMultiPictureDocumentReadPageCount(stream.get()); |
| 1325 | if (count > 0) { |
| 1326 | fPages.reset(count); |
| 1327 | (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count()); |
| 1328 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1331 | int MSKPSrc::pageCount() const { return fPages.count(); } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1332 | |
| 1333 | SkISize MSKPSrc::size() const { return this->size(0); } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1334 | SkISize MSKPSrc::size(int i) const { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1335 | return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0}; |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1336 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1337 | |
| 1338 | Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); } |
| 1339 | Error MSKPSrc::draw(int i, SkCanvas* canvas) const { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1340 | if (this->pageCount() == 0) { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1341 | return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str()); |
| 1342 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1343 | if (i >= fPages.count() || i < 0) { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1344 | return SkStringPrintf("MultiPictureDocument page number out of range: %d", i); |
| 1345 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1346 | SkPicture* page = fPages[i].fPicture.get(); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1347 | if (!page) { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1348 | std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str()); |
| 1349 | if (!stream) { |
| 1350 | return SkStringPrintf("Unable to open file: %s", fPath.c_str()); |
| 1351 | } |
| 1352 | if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) { |
| 1353 | return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i, |
| 1354 | fPath.c_str()); |
| 1355 | } |
| 1356 | page = fPages[i].fPicture.get(); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1357 | } |
| 1358 | canvas->drawPicture(page); |
| 1359 | return ""; |
| 1360 | } |
| 1361 | |
| 1362 | Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 1363 | |
| 1364 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1365 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 1366 | Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1367 | return src.draw(SkMakeNullCanvas().get()); |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1371 | |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1372 | static Error compare_bitmaps(const SkBitmap& reference, const SkBitmap& bitmap) { |
| 1373 | // The dimensions are a property of the Src only, and so should be identical. |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 1374 | SkASSERT(reference.computeByteSize() == bitmap.computeByteSize()); |
| 1375 | if (reference.computeByteSize() != bitmap.computeByteSize()) { |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1376 | return "Dimensions don't match reference"; |
| 1377 | } |
| 1378 | // All SkBitmaps in DM are tight, so this comparison is easy. |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 1379 | if (0 != memcmp(reference.getPixels(), bitmap.getPixels(), reference.computeByteSize())) { |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1380 | SkString encoded; |
| 1381 | SkString errString("Pixels don't match reference"); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 1382 | if (bitmap_to_base64_data_uri(reference, &encoded)) { |
| 1383 | errString.append("\nExpected: "); |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1384 | errString.append(encoded); |
| 1385 | } else { |
| 1386 | errString.append("\nExpected image failed to encode: "); |
| 1387 | errString.append(encoded); |
| 1388 | } |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 1389 | if (bitmap_to_base64_data_uri(bitmap, &encoded)) { |
| 1390 | errString.append("\nActual: "); |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1391 | errString.append(encoded); |
| 1392 | } else { |
| 1393 | errString.append("\nActual image failed to encode: "); |
| 1394 | errString.append(encoded); |
| 1395 | } |
| 1396 | return errString; |
| 1397 | } |
| 1398 | return ""; |
| 1399 | } |
| 1400 | |
| 1401 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1402 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1403 | DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?"); |
| 1404 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 1405 | GPUSink::GPUSink(GrContextFactory::ContextType ct, |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 1406 | GrContextFactory::ContextOverrides overrides, |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1407 | SkCommandLineConfigGpu::SurfType surfType, |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 1408 | int samples, |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 1409 | bool diText, |
brianosman | d93c120 | 2016-03-10 07:49:08 -0800 | [diff] [blame] | 1410 | SkColorType colorType, |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1411 | SkAlphaType alphaType, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1412 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | f21aa04 | 2017-08-21 16:48:46 -0400 | [diff] [blame] | 1413 | bool threaded, |
| 1414 | const GrContextOptions& grCtxOptions) |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1415 | : fContextType(ct) |
| 1416 | , fContextOverrides(overrides) |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1417 | , fSurfType(surfType) |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1418 | , fSampleCount(samples) |
| 1419 | , fUseDIText(diText) |
| 1420 | , fColorType(colorType) |
| 1421 | , fAlphaType(alphaType) |
| 1422 | , fColorSpace(std::move(colorSpace)) |
Brian Osman | f21aa04 | 2017-08-21 16:48:46 -0400 | [diff] [blame] | 1423 | , fThreaded(threaded) |
| 1424 | , fBaseContextOptions(grCtxOptions) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1425 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 1426 | DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing."); |
bsalomon | 648c696 | 2015-10-23 09:06:59 -0700 | [diff] [blame] | 1427 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1428 | Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const { |
| 1429 | return this->onDraw(src, dst, dstStream, log, fBaseContextOptions); |
| 1430 | } |
| 1431 | |
| 1432 | Error GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log, |
| 1433 | const GrContextOptions& baseOptions) const { |
| 1434 | GrContextOptions grOptions = baseOptions; |
kkinnunen | 64492c4 | 2015-12-08 01:24:40 -0800 | [diff] [blame] | 1435 | |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1436 | // We don't expect the src to mess with the persistent cache or the executor. |
| 1437 | SkDEBUGCODE(auto cache = grOptions.fPersistentCache); |
| 1438 | SkDEBUGCODE(auto exec = grOptions.fExecutor); |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1439 | src.modifyGrContextOptions(&grOptions); |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1440 | SkASSERT(cache == grOptions.fPersistentCache); |
| 1441 | SkASSERT(exec == grOptions.fExecutor); |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1442 | |
| 1443 | GrContextFactory factory(grOptions); |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1444 | const SkISize size = src.size(); |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1445 | SkImageInfo info = |
| 1446 | SkImageInfo::Make(size.width(), size.height(), fColorType, fAlphaType, fColorSpace); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1447 | sk_sp<SkSurface> surface; |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 1448 | GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext(); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 1449 | const int maxDimension = context->contextPriv().caps()->maxTextureSize(); |
msarett | 13a036b | 2016-02-08 09:10:47 -0800 | [diff] [blame] | 1450 | if (maxDimension < SkTMax(size.width(), size.height())) { |
| 1451 | return Error::Nonfatal("Src too large to create a texture.\n"); |
| 1452 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1453 | uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0; |
| 1454 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 1455 | GrBackendTexture backendTexture; |
| 1456 | GrBackendRenderTarget backendRT; |
| 1457 | switch (fSurfType) { |
| 1458 | case SkCommandLineConfigGpu::SurfType::kDefault: |
| 1459 | surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount, |
| 1460 | &props); |
| 1461 | break; |
| 1462 | case SkCommandLineConfigGpu::SurfType::kBackendTexture: |
| 1463 | backendTexture = context->contextPriv().getGpu()->createTestingOnlyBackendTexture( |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 1464 | nullptr, info.width(), info.height(), info.colorType(), true, GrMipMapped::kNo); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1465 | surface = SkSurface::MakeFromBackendTexture(context, backendTexture, |
| 1466 | kTopLeft_GrSurfaceOrigin, fSampleCount, |
Brian Salomon | 5370677 | 2018-03-19 14:18:08 -0400 | [diff] [blame] | 1467 | fColorType, info.refColorSpace(), &props); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1468 | break; |
| 1469 | case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget: |
| 1470 | if (1 == fSampleCount) { |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1471 | auto colorType = SkColorTypeToGrColorType(info.colorType()); |
| 1472 | backendRT = context->contextPriv().getGpu()->createTestingOnlyBackendRenderTarget( |
Brian Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame] | 1473 | info.width(), info.height(), colorType); |
Brian Salomon | 49edccd | 2018-03-23 15:31:32 -0400 | [diff] [blame] | 1474 | surface = SkSurface::MakeFromBackendRenderTarget( |
| 1475 | context, backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(), |
| 1476 | info.refColorSpace(), &props); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1477 | } |
| 1478 | break; |
| 1479 | } |
msarett | 13a036b | 2016-02-08 09:10:47 -0800 | [diff] [blame] | 1480 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1481 | if (!surface) { |
| 1482 | return "Could not create a surface."; |
| 1483 | } |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1484 | if (FLAGS_preAbandonGpuContext) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1485 | factory.abandonContexts(); |
| 1486 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1487 | SkCanvas* canvas = surface->getCanvas(); |
| 1488 | Error err = src.draw(canvas); |
| 1489 | if (!err.isEmpty()) { |
| 1490 | return err; |
| 1491 | } |
| 1492 | canvas->flush(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1493 | if (FLAGS_gpuStats) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1494 | canvas->getGrContext()->contextPriv().dumpCacheStats(log); |
| 1495 | canvas->getGrContext()->contextPriv().dumpGpuStats(log); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1496 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 1497 | if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType || |
| 1498 | info.colorType() == kRGB_888x_SkColorType) { |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1499 | // We don't currently support readbacks into these formats on the GPU backend. Convert to |
| 1500 | // 32 bit. |
| 1501 | info = SkImageInfo::Make(size.width(), size.height(), kRGBA_8888_SkColorType, |
| 1502 | kPremul_SkAlphaType, fColorSpace); |
| 1503 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1504 | dst->allocPixels(info); |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 1505 | canvas->readPixels(*dst, 0, 0); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 1506 | if (FLAGS_abandonGpuContext) { |
| 1507 | factory.abandonContexts(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 1508 | } else if (FLAGS_releaseAndAbandonGpuContext) { |
| 1509 | factory.releaseResourcesAndAbandonContexts(); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 1510 | } |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 1511 | if (!context->abandoned()) { |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1512 | surface.reset(); |
| 1513 | if (backendTexture.isValid()) { |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 1514 | context->contextPriv().getGpu()->deleteTestingOnlyBackendTexture(backendTexture); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1515 | } |
| 1516 | if (backendRT.isValid()) { |
| 1517 | context->contextPriv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT); |
| 1518 | } |
| 1519 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1520 | return ""; |
| 1521 | } |
| 1522 | |
| 1523 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1524 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1525 | GPUThreadTestingSink::GPUThreadTestingSink(GrContextFactory::ContextType ct, |
| 1526 | GrContextFactory::ContextOverrides overrides, |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1527 | SkCommandLineConfigGpu::SurfType surfType, |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1528 | int samples, |
| 1529 | bool diText, |
| 1530 | SkColorType colorType, |
| 1531 | SkAlphaType alphaType, |
| 1532 | sk_sp<SkColorSpace> colorSpace, |
| 1533 | bool threaded, |
| 1534 | const GrContextOptions& grCtxOptions) |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1535 | : INHERITED(ct, overrides, surfType, samples, diText, colorType, alphaType, |
| 1536 | std::move(colorSpace), threaded, grCtxOptions) |
Mike Klein | 022cfa2 | 2017-09-01 11:53:16 -0400 | [diff] [blame] | 1537 | , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)) { |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1538 | SkASSERT(fExecutor); |
| 1539 | } |
| 1540 | |
| 1541 | Error GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream, |
| 1542 | SkString* log) const { |
| 1543 | // Draw twice, once with worker threads, and once without. Verify that we get the same result. |
| 1544 | // Also, force us to only use the software path renderer, so we really stress-test the threaded |
| 1545 | // version of that code. |
| 1546 | GrContextOptions contextOptions = this->baseContextOptions(); |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 1547 | contextOptions.fGpuPathRenderers = GpuPathRenderers::kNone; |
Greg Daniel | 024615e | 2018-05-10 17:25:52 +0000 | [diff] [blame] | 1548 | contextOptions.fExecutor = fExecutor.get(); |
Adrienne Walker | ab7181d | 2018-05-14 14:02:03 -0700 | [diff] [blame] | 1549 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1550 | Error err = this->onDraw(src, dst, wStream, log, contextOptions); |
| 1551 | if (!err.isEmpty() || !dst) { |
| 1552 | return err; |
| 1553 | } |
| 1554 | |
| 1555 | SkBitmap reference; |
| 1556 | SkString refLog; |
| 1557 | SkDynamicMemoryWStream refStream; |
| 1558 | contextOptions.fExecutor = nullptr; |
| 1559 | Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions); |
| 1560 | if (!refErr.isEmpty()) { |
| 1561 | return refErr; |
| 1562 | } |
| 1563 | |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1564 | return compare_bitmaps(reference, *dst); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1568 | |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1569 | GPUPersistentCacheTestingSink::GPUPersistentCacheTestingSink( |
| 1570 | GrContextFactory::ContextType ct, |
| 1571 | GrContextFactory::ContextOverrides overrides, |
| 1572 | SkCommandLineConfigGpu::SurfType surfType, |
| 1573 | int samples, |
| 1574 | bool diText, |
| 1575 | SkColorType colorType, |
| 1576 | SkAlphaType alphaType, |
| 1577 | sk_sp<SkColorSpace> colorSpace, |
| 1578 | bool threaded, |
| 1579 | const GrContextOptions& grCtxOptions) |
| 1580 | : INHERITED(ct, overrides, surfType, samples, diText, colorType, alphaType, |
| 1581 | std::move(colorSpace), threaded, grCtxOptions) {} |
| 1582 | |
| 1583 | Error GPUPersistentCacheTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream, |
| 1584 | SkString* log) const { |
| 1585 | // Draw twice, once with a cold cache, and again with a warm cache. Verify that we get the same |
| 1586 | // result. |
| 1587 | sk_gpu_test::MemoryCache memoryCache; |
| 1588 | GrContextOptions contextOptions = this->baseContextOptions(); |
| 1589 | contextOptions.fPersistentCache = &memoryCache; |
| 1590 | |
| 1591 | Error err = this->onDraw(src, dst, wStream, log, contextOptions); |
| 1592 | if (!err.isEmpty() || !dst) { |
| 1593 | return err; |
| 1594 | } |
| 1595 | |
| 1596 | SkBitmap reference; |
| 1597 | SkString refLog; |
| 1598 | SkDynamicMemoryWStream refStream; |
| 1599 | memoryCache.resetNumCacheMisses(); |
| 1600 | Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions); |
| 1601 | if (!refErr.isEmpty()) { |
| 1602 | return refErr; |
| 1603 | } |
Brian Salomon | 7fc5299 | 2018-07-12 16:20:23 -0400 | [diff] [blame] | 1604 | SkASSERT(!memoryCache.numCacheMisses()); |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1605 | |
| 1606 | return compare_bitmaps(reference, *dst); |
| 1607 | } |
| 1608 | |
| 1609 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1610 | static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) { |
halcanary | 4ba051c | 2016-03-10 10:31:53 -0800 | [diff] [blame] | 1611 | if (src.size().isEmpty()) { |
| 1612 | return "Source has empty dimensions"; |
| 1613 | } |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1614 | SkASSERT(doc); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1615 | int pageCount = src.pageCount(); |
| 1616 | for (int i = 0; i < pageCount; ++i) { |
| 1617 | int width = src.size(i).width(), height = src.size(i).height(); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1618 | SkCanvas* canvas = |
| 1619 | doc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); |
| 1620 | if (!canvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1621 | return "SkDocument::beginPage(w,h) returned nullptr"; |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1622 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1623 | Error err = src.draw(i, canvas); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1624 | if (!err.isEmpty()) { |
| 1625 | return err; |
| 1626 | } |
| 1627 | doc->endPage(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1628 | } |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 1629 | doc->close(); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 1630 | dst->flush(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1631 | return ""; |
| 1632 | } |
| 1633 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1634 | Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 1635 | SkPDF::Metadata metadata; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1636 | metadata.fTitle = src.name(); |
| 1637 | metadata.fSubject = "rendering correctness test"; |
| 1638 | metadata.fCreator = "Skia/DM"; |
Mike Reed | a4daf19 | 2017-12-14 13:25:04 -0500 | [diff] [blame] | 1639 | metadata.fRasterDPI = fRasterDpi; |
| 1640 | metadata.fPDFA = fPDFA; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 1641 | sk_sp<SkDocument> doc = SkPDF::MakeDocument(dst, metadata); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1642 | if (!doc) { |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 1643 | return "SkPDF::MakeDocument() returned nullptr"; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1644 | } |
| 1645 | return draw_skdocument(src, doc.get(), dst); |
| 1646 | } |
| 1647 | |
| 1648 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1649 | |
| 1650 | XPSSink::XPSSink() {} |
| 1651 | |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 1652 | #ifdef SK_BUILD_FOR_WIN |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1653 | static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() { |
| 1654 | IXpsOMObjectFactory* factory; |
| 1655 | HRN(CoCreateInstance(CLSID_XpsOMObjectFactory, |
| 1656 | nullptr, |
| 1657 | CLSCTX_INPROC_SERVER, |
| 1658 | IID_PPV_ARGS(&factory))); |
| 1659 | return SkTScopedComPtr<IXpsOMObjectFactory>(factory); |
| 1660 | } |
| 1661 | |
| 1662 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 1663 | SkAutoCoInitialize com; |
| 1664 | if (!com.succeeded()) { |
| 1665 | return "Could not initialize COM."; |
| 1666 | } |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1667 | SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory(); |
| 1668 | if (!factory) { |
| 1669 | return "Failed to create XPS Factory."; |
| 1670 | } |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 1671 | sk_sp<SkDocument> doc(SkXPS::MakeDocument(dst, factory.get())); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1672 | if (!doc) { |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 1673 | return "SkXPS::MAkeDocument() returned nullptr"; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1674 | } |
| 1675 | return draw_skdocument(src, doc.get(), dst); |
| 1676 | } |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1677 | #else |
| 1678 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 1679 | return "XPS not supported on this platform."; |
| 1680 | } |
| 1681 | #endif |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 1682 | |
| 1683 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1684 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1685 | SKPSink::SKPSink() {} |
| 1686 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1687 | Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1688 | SkSize size; |
| 1689 | size = src.size(); |
| 1690 | SkPictureRecorder recorder; |
| 1691 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 1692 | if (!err.isEmpty()) { |
| 1693 | return err; |
| 1694 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1695 | recorder.finishRecordingAsPicture()->serialize(dst); |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1696 | return ""; |
| 1697 | } |
| 1698 | |
| 1699 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1700 | |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 1701 | Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 1702 | SkDebugCanvas debugCanvas(src.size().width(), src.size().height()); |
| 1703 | Error err = src.draw(&debugCanvas); |
| 1704 | if (!err.isEmpty()) { |
| 1705 | return err; |
| 1706 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1707 | std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas(); |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 1708 | UrlDataManager dataManager(SkString("data")); |
| 1709 | Json::Value json = debugCanvas.toJSON( |
| 1710 | dataManager, debugCanvas.getSize(), nullCanvas.get()); |
| 1711 | std::string value = Json::StyledWriter().write(json); |
| 1712 | return dst->write(value.c_str(), value.size()) ? "" : "SkWStream Error"; |
| 1713 | } |
| 1714 | |
| 1715 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1716 | |
Bryce Thomas | 95a7b76 | 2018-03-02 13:54:21 -0800 | [diff] [blame] | 1717 | SVGSink::SVGSink(int pageIndex) : fPageIndex(pageIndex) {} |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 1718 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1719 | Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 1720 | #if defined(SK_XML) |
Bryce Thomas | 95a7b76 | 2018-03-02 13:54:21 -0800 | [diff] [blame] | 1721 | if (src.pageCount() > 1) { |
| 1722 | int pageCount = src.pageCount(); |
| 1723 | if (fPageIndex > pageCount - 1) { |
| 1724 | return Error(SkStringPrintf("Page index %d too high for document with only %d pages.", |
| 1725 | fPageIndex, pageCount)); |
| 1726 | } |
| 1727 | } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1728 | std::unique_ptr<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst)); |
Bryce Thomas | 95a7b76 | 2018-03-02 13:54:21 -0800 | [diff] [blame] | 1729 | return src.draw(fPageIndex, |
| 1730 | SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()), |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1731 | SkIntToScalar(src.size().height())), |
Bryce Thomas | 95a7b76 | 2018-03-02 13:54:21 -0800 | [diff] [blame] | 1732 | xmlWriter.get()) |
| 1733 | .get()); |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 1734 | #else |
Hal Canary | 327ef03 | 2018-03-22 13:10:51 -0400 | [diff] [blame] | 1735 | (void)fPageIndex; |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 1736 | return Error("SVG sink is disabled."); |
| 1737 | #endif // SK_XML |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1741 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1742 | RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace) |
mtklein | 27c3fdd | 2016-02-26 14:43:21 -0800 | [diff] [blame] | 1743 | : fColorType(colorType) |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1744 | , fColorSpace(std::move(colorSpace)) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1745 | |
Mike Klein | 48b6490 | 2018-07-25 13:28:44 -0400 | [diff] [blame] | 1746 | Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1747 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1748 | // If there's an appropriate alpha type for this color type, use it, otherwise use premul. |
| 1749 | SkAlphaType alphaType = kPremul_SkAlphaType; |
| 1750 | (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType); |
| 1751 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 1752 | dst->allocPixelsFlags(SkImageInfo::Make(size.width(), size.height(), |
| 1753 | fColorType, alphaType, fColorSpace), |
| 1754 | SkBitmap::kZeroPixels_AllocFlag); |
Yuqian Li | b8b6253 | 2018-02-23 14:13:36 +0800 | [diff] [blame] | 1755 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1756 | SkCanvas canvas(*dst); |
| 1757 | return src.draw(&canvas); |
| 1758 | } |
| 1759 | |
| 1760 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1761 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1762 | // Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(), |
mtklein | e44b508 | 2015-05-07 10:53:34 -0700 | [diff] [blame] | 1763 | // passing the Sink draw() arguments, a size, and a function draws into an SkCanvas. |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1764 | // Several examples below. |
| 1765 | |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1766 | template <typename Fn> |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1767 | static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log, |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1768 | SkISize size, const Fn& draw) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1769 | class ProxySrc : public Src { |
| 1770 | public: |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1771 | ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {} |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1772 | Error draw(SkCanvas* canvas) const override { return fDraw(canvas); } |
halcanary | b4a7f14 | 2016-03-30 08:31:27 -0700 | [diff] [blame] | 1773 | Name name() const override { return "ProxySrc"; } |
| 1774 | SkISize size() const override { return fSize; } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1775 | private: |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1776 | SkISize fSize; |
| 1777 | const Fn& fDraw; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1778 | }; |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1779 | return sink->draw(ProxySrc(size, draw), bitmap, stream, log); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1783 | |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1784 | DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output."); |
| 1785 | |
| 1786 | // Is *bitmap identical to what you get drawing src into sink? |
| 1787 | static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) { |
| 1788 | // We can only check raster outputs. |
| 1789 | // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.) |
| 1790 | if (FLAGS_check && bitmap) { |
| 1791 | SkBitmap reference; |
| 1792 | SkString log; |
halcanary | b4a7f14 | 2016-03-30 08:31:27 -0700 | [diff] [blame] | 1793 | SkDynamicMemoryWStream wStream; |
| 1794 | Error err = sink->draw(src, &reference, &wStream, &log); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1795 | // If we can draw into this Sink via some pipeline, we should be able to draw directly. |
| 1796 | SkASSERT(err.isEmpty()); |
| 1797 | if (!err.isEmpty()) { |
| 1798 | return err; |
| 1799 | } |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1800 | return compare_bitmaps(reference, *bitmap); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1801 | } |
| 1802 | return ""; |
| 1803 | } |
| 1804 | |
| 1805 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1806 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1807 | static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) { |
| 1808 | SkRect bounds = SkRect::MakeIWH(srcW, srcH); |
| 1809 | matrix->mapRect(&bounds); |
| 1810 | matrix->postTranslate(-bounds.x(), -bounds.y()); |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1811 | return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())}; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1812 | } |
| 1813 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1814 | ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1815 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1816 | Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1817 | SkMatrix matrix = fMatrix; |
| 1818 | SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height()); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1819 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1820 | canvas->concat(matrix); |
| 1821 | return src.draw(canvas); |
| 1822 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1823 | } |
| 1824 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1825 | // Undoes any flip or 90 degree rotate without changing the scale of the bitmap. |
| 1826 | // This should be pixel-preserving. |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1827 | ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1828 | |
| 1829 | Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1830 | Error err = fSink->draw(src, bitmap, stream, log); |
| 1831 | if (!err.isEmpty()) { |
| 1832 | return err; |
| 1833 | } |
| 1834 | |
| 1835 | SkMatrix inverse; |
| 1836 | if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) { |
| 1837 | return "Cannot upright --matrix."; |
| 1838 | } |
| 1839 | SkMatrix upright = SkMatrix::I(); |
| 1840 | upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX())); |
| 1841 | upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY())); |
| 1842 | upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX())); |
| 1843 | upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY())); |
| 1844 | |
| 1845 | SkBitmap uprighted; |
| 1846 | SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height()); |
| 1847 | uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height())); |
| 1848 | |
| 1849 | SkCanvas canvas(uprighted); |
| 1850 | canvas.concat(upright); |
| 1851 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1852 | paint.setBlendMode(SkBlendMode::kSrc); |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1853 | canvas.drawBitmap(*bitmap, 0, 0, &paint); |
| 1854 | |
| 1855 | *bitmap = uprighted; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1856 | return ""; |
| 1857 | } |
| 1858 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1859 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1860 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1861 | Error ViaSerialization::draw( |
| 1862 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1863 | // Record our Src into a picture. |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1864 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1865 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1866 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1867 | SkIntToScalar(size.height()))); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1868 | if (!err.isEmpty()) { |
| 1869 | return err; |
| 1870 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1871 | sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1872 | |
| 1873 | // Serialize it and then deserialize it. |
reed | 39eaf5f | 2016-09-15 07:19:35 -0700 | [diff] [blame] | 1874 | sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1875 | |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1876 | err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1877 | canvas->drawPicture(deserialized); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1878 | return ""; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1879 | }); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1880 | if (!err.isEmpty()) { |
| 1881 | return err; |
| 1882 | } |
| 1883 | |
| 1884 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1888 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1889 | ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink) |
| 1890 | : Via(sink) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 1891 | , fW(w) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1892 | , fH(h) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 1893 | , fFactory(factory) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1894 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1895 | Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1896 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1897 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1898 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1899 | SkIntToScalar(size.height()), |
| 1900 | fFactory.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1901 | if (!err.isEmpty()) { |
| 1902 | return err; |
| 1903 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1904 | sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1905 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1906 | return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1907 | const int xTiles = (size.width() + fW - 1) / fW, |
| 1908 | yTiles = (size.height() + fH - 1) / fH; |
| 1909 | SkMultiPictureDraw mpd(xTiles*yTiles); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1910 | SkTArray<sk_sp<SkSurface>> surfaces; |
| 1911 | // surfaces.setReserve(xTiles*yTiles); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1912 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1913 | SkImageInfo info = canvas->imageInfo().makeWH(fW, fH); |
| 1914 | for (int j = 0; j < yTiles; j++) { |
| 1915 | for (int i = 0; i < xTiles; i++) { |
| 1916 | // This lets our ultimate Sink determine the best kind of surface. |
| 1917 | // E.g., if it's a GpuSink, the surfaces and images are textures. |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1918 | auto s = canvas->makeSurface(info); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 1919 | if (!s) { |
| 1920 | s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces. |
| 1921 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1922 | surfaces.push_back(s); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1923 | SkCanvas* c = s->getCanvas(); |
| 1924 | c->translate(SkIntToScalar(-i * fW), |
| 1925 | SkIntToScalar(-j * fH)); // Line up the canvas with this tile. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1926 | mpd.add(c, pic.get()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1927 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1928 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1929 | mpd.draw(); |
| 1930 | for (int j = 0; j < yTiles; j++) { |
| 1931 | for (int i = 0; i < xTiles; i++) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1932 | sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot()); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1933 | canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH)); |
| 1934 | } |
| 1935 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1936 | return ""; |
| 1937 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1938 | } |
| 1939 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1940 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1941 | |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1942 | ViaDDL::ViaDDL(int numDivisions, Sink* sink) |
| 1943 | : Via(sink) |
| 1944 | , fNumDivisions(numDivisions) { |
| 1945 | } |
| 1946 | |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1947 | Error ViaDDL::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1948 | auto size = src.size(); |
| 1949 | SkPictureRecorder recorder; |
| 1950 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1951 | SkIntToScalar(size.height()))); |
| 1952 | if (!err.isEmpty()) { |
| 1953 | return err; |
| 1954 | } |
| 1955 | sk_sp<SkPicture> inputPicture(recorder.finishRecordingAsPicture()); |
| 1956 | |
| 1957 | // this is our ultimate final drawing area/rect |
| 1958 | SkIRect viewport = SkIRect::MakeWH(size.fWidth, size.fHeight); |
| 1959 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1960 | DDLPromiseImageHelper promiseImageHelper; |
| 1961 | sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture.get()); |
| 1962 | if (!compressedPictureData) { |
| 1963 | return SkStringPrintf("ViaDDL: Couldn't deflate SkPicture"); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1964 | } |
| 1965 | |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1966 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, |
| 1967 | [&](SkCanvas* canvas) -> Error { |
| 1968 | GrContext* context = canvas->getGrContext(); |
| 1969 | if (!context || !context->contextPriv().getGpu()) { |
| 1970 | return SkStringPrintf("DDLs are GPU only"); |
| 1971 | } |
| 1972 | |
Robert Phillips | a479f96 | 2018-04-10 11:45:40 -0400 | [diff] [blame] | 1973 | // This is here bc this is the first point where we have access to the context |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1974 | promiseImageHelper.uploadAllToGPU(context); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1975 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1976 | // First, create all the tiles (including their individual dest surfaces) |
| 1977 | DDLTileHelper tiles(canvas, viewport, fNumDivisions); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1978 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1979 | // Second, reinflate the compressed picture individually for each thread |
| 1980 | tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1981 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1982 | // Third, create the DDLs in parallel |
| 1983 | tiles.createDDLsInParallel(); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1984 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1985 | // This drops the promiseImageHelper's refs on all the promise images |
| 1986 | promiseImageHelper.reset(); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1987 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1988 | // Fourth, synchronously render the display lists into the dest tiles |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1989 | // TODO: it would be cool to not wait until all the tiles are drawn to begin |
| 1990 | // drawing to the GPU and composing to the final surface |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1991 | tiles.drawAllTilesAndFlush(context, false); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1992 | |
| 1993 | // Finally, compose the drawn tiles into the result |
| 1994 | // Note: the separation between the tiles and the final composition better |
| 1995 | // matches Chrome but costs us a copy |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1996 | tiles.composeAllTiles(canvas); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1997 | |
Robert Phillips | cc47335 | 2018-03-27 10:46:00 -0400 | [diff] [blame] | 1998 | context->flush(); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1999 | return ""; |
| 2000 | }); |
| 2001 | } |
| 2002 | |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 2003 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 2004 | |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 2005 | Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 2006 | auto size = src.size(); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 2007 | Error err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 2008 | SkPictureRecorder recorder; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 2009 | sk_sp<SkPicture> pic; |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 2010 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 2011 | SkIntToScalar(size.height()))); |
| 2012 | if (!err.isEmpty()) { |
| 2013 | return err; |
| 2014 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 2015 | pic = recorder.finishRecordingAsPicture(); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 2016 | canvas->drawPicture(pic); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 2017 | return err; |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 2018 | }); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 2019 | if (!err.isEmpty()) { |
| 2020 | return err; |
| 2021 | } |
| 2022 | |
| 2023 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 2027 | |
Mike Reed | bae888e | 2017-02-18 16:50:45 -0500 | [diff] [blame] | 2028 | #ifdef TEST_VIA_SVG |
Mike Reed | f67c459 | 2017-02-17 17:06:11 -0500 | [diff] [blame] | 2029 | #include "SkXMLWriter.h" |
| 2030 | #include "SkSVGCanvas.h" |
| 2031 | #include "SkSVGDOM.h" |
| 2032 | |
| 2033 | Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 2034 | auto size = src.size(); |
| 2035 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
| 2036 | SkDynamicMemoryWStream wstream; |
| 2037 | SkXMLStreamWriter writer(&wstream); |
| 2038 | Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get()); |
| 2039 | if (!err.isEmpty()) { |
| 2040 | return err; |
| 2041 | } |
| 2042 | std::unique_ptr<SkStream> rstream(wstream.detachAsStream()); |
| 2043 | auto dom = SkSVGDOM::MakeFromStream(*rstream); |
| 2044 | if (dom) { |
| 2045 | dom->setContainerSize(SkSize::Make(size)); |
| 2046 | dom->render(canvas); |
| 2047 | } |
| 2048 | return ""; |
| 2049 | }); |
| 2050 | } |
Mike Reed | bae888e | 2017-02-18 16:50:45 -0500 | [diff] [blame] | 2051 | #endif |
Mike Reed | f67c459 | 2017-02-17 17:06:11 -0500 | [diff] [blame] | 2052 | |
| 2053 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 2054 | |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 2055 | Error ViaLite::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 2056 | auto size = src.size(); |
Derek Sollenberger | d7875f5 | 2017-03-01 15:33:23 -0500 | [diff] [blame] | 2057 | SkIRect bounds = {0,0, size.width(), size.height()}; |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 2058 | Error err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
Derek Sollenberger | d7875f5 | 2017-03-01 15:33:23 -0500 | [diff] [blame] | 2059 | SkLiteDL dl; |
mtklein | 8bbbb69 | 2016-08-15 12:56:00 -0700 | [diff] [blame] | 2060 | SkLiteRecorder rec; |
Derek Sollenberger | d7875f5 | 2017-03-01 15:33:23 -0500 | [diff] [blame] | 2061 | rec.reset(&dl, bounds); |
mtklein | 8bbbb69 | 2016-08-15 12:56:00 -0700 | [diff] [blame] | 2062 | |
| 2063 | Error err = src.draw(&rec); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 2064 | if (!err.isEmpty()) { |
| 2065 | return err; |
| 2066 | } |
Derek Sollenberger | d7875f5 | 2017-03-01 15:33:23 -0500 | [diff] [blame] | 2067 | dl.draw(canvas); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 2068 | return err; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 2069 | }); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 2070 | if (!err.isEmpty()) { |
| 2071 | return err; |
| 2072 | } |
| 2073 | |
| 2074 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 2077 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 2078 | |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 2079 | ViaCSXform::ViaCSXform(Sink* sink, sk_sp<SkColorSpace> cs, bool colorSpin) |
| 2080 | : Via(sink) |
| 2081 | , fCS(std::move(cs)) |
| 2082 | , fColorSpin(colorSpin) {} |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 2083 | |
| 2084 | Error ViaCSXform::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
Brian Osman | a76b7a9 | 2018-07-18 14:36:43 -0400 | [diff] [blame] | 2085 | Error err = draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) { |
Brian Osman | 5b7e470 | 2017-10-31 16:41:29 -0400 | [diff] [blame] | 2086 | { |
| 2087 | SkAutoCanvasRestore acr(canvas, true); |
| 2088 | auto proxy = SkCreateColorSpaceXformCanvas(canvas, fCS); |
| 2089 | Error err = src.draw(proxy.get()); |
| 2090 | if (!err.isEmpty()) { |
| 2091 | return err; |
| 2092 | } |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | // Undo the color spin, so we can look at the pixels in Gold. |
| 2096 | if (fColorSpin) { |
| 2097 | SkBitmap pixels; |
| 2098 | pixels.allocPixels(canvas->imageInfo()); |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 2099 | canvas->readPixels(pixels, 0, 0); |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 2100 | |
Brian Osman | 5b7e470 | 2017-10-31 16:41:29 -0400 | [diff] [blame] | 2101 | SkPaint rotateColors; |
| 2102 | SkScalar matrix[20] = { 0, 0, 1, 0, 0, // B -> R |
| 2103 | 1, 0, 0, 0, 0, // R -> G |
| 2104 | 0, 1, 0, 0, 0, // G -> B |
| 2105 | 0, 0, 0, 1, 0 }; |
| 2106 | rotateColors.setBlendMode(SkBlendMode::kSrc); |
| 2107 | rotateColors.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); |
| 2108 | canvas->drawBitmap(pixels, 0, 0, &rotateColors); |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Brian Osman | a76b7a9 | 2018-07-18 14:36:43 -0400 | [diff] [blame] | 2111 | return Error(""); |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 2112 | }); |
Brian Osman | a76b7a9 | 2018-07-18 14:36:43 -0400 | [diff] [blame] | 2113 | |
| 2114 | if (!err.isEmpty()) { |
| 2115 | return err; |
| 2116 | } |
| 2117 | |
| 2118 | if (bitmap && !fColorSpin) { |
| 2119 | // It should be possible to do this without all the copies, but that (I think) requires |
| 2120 | // adding API to SkBitmap. |
| 2121 | SkAutoPixmapStorage pmap; |
| 2122 | pmap.alloc(bitmap->info()); |
| 2123 | bitmap->readPixels(pmap); |
| 2124 | pmap.setColorSpace(fCS); |
| 2125 | bitmap->allocPixels(pmap.info()); |
| 2126 | bitmap->writePixels(pmap); |
| 2127 | } |
| 2128 | |
| 2129 | return ""; |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 2130 | } |
| 2131 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 2132 | } // namespace DM |