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" |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 10 | #include "SkAndroidCodec.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 11 | #include "SkAutoMalloc.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 12 | #include "SkCodec.h" |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 13 | #include "SkCodecImageGenerator.h" |
msarett | 888dc16 | 2016-05-23 10:21:17 -0700 | [diff] [blame] | 14 | #include "SkColorSpace.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 15 | #include "SkColorSpaceXform.h" |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 16 | #include "SkColorSpaceXformCanvas.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 17 | #include "SkColorSpace_XYZ.h" |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 18 | #include "SkCommonFlags.h" |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 19 | #include "SkData.h" |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 20 | #include "SkDebugCanvas.h" |
reed | babc3de | 2016-07-08 08:43:27 -0700 | [diff] [blame] | 21 | #include "SkDeferredCanvas.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 22 | #include "SkDocument.h" |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 23 | #include "SkImageGenerator.h" |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 24 | #include "SkImageGeneratorCG.h" |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 25 | #include "SkImageGeneratorWIC.h" |
mtklein | 8bbbb69 | 2016-08-15 12:56:00 -0700 | [diff] [blame] | 26 | #include "SkLiteDL.h" |
| 27 | #include "SkLiteRecorder.h" |
mtklein | c8be09a | 2016-01-04 18:56:57 -0800 | [diff] [blame] | 28 | #include "SkMallocPixelRef.h" |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 29 | #include "SkMultiPictureDocumentPriv.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 30 | #include "SkMultiPictureDraw.h" |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 31 | #include "SkNullCanvas.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 32 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 33 | #include "SkOSPath.h" |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 34 | #include "SkOpts.h" |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 35 | #include "SkPictureData.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 36 | #include "SkPictureRecorder.h" |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 37 | #include "SkPipe.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 38 | #include "SkRandom.h" |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 39 | #include "SkRecordDraw.h" |
| 40 | #include "SkRecorder.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 41 | #include "SkSVGCanvas.h" |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 42 | #include "SkStream.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 43 | #include "SkSwizzler.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 44 | #include "SkTLogic.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 45 | #include <cmath> |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 46 | #include <functional> |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 47 | |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 48 | #if defined(SK_BUILD_FOR_WIN) |
| 49 | #include "SkAutoCoInitialize.h" |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 50 | #include "SkHRESULT.h" |
| 51 | #include "SkTScopedComPtr.h" |
| 52 | #include <XpsObjectModel.h> |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 53 | #endif |
| 54 | |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 55 | #if defined(SK_XML) |
fmalita | a48f0e3 | 2016-08-04 06:26:05 -0700 | [diff] [blame] | 56 | #include "SkSVGDOM.h" |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 57 | #include "SkXMLWriter.h" |
| 58 | #endif |
| 59 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 60 | DEFINE_bool(multiPage, false, "For document-type backends, render the source" |
| 61 | " into multiple pages"); |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 62 | DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?"); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 63 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 64 | using sk_gpu_test::GrContextFactory; |
| 65 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 66 | namespace DM { |
| 67 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 68 | GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
| 69 | |
| 70 | Error GMSrc::draw(SkCanvas* canvas) const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 71 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 72 | canvas->concat(gm->getInitialTransform()); |
| 73 | gm->draw(canvas); |
| 74 | return ""; |
| 75 | } |
| 76 | |
| 77 | SkISize GMSrc::size() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 78 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 79 | return gm->getISize(); |
| 80 | } |
| 81 | |
| 82 | Name GMSrc::name() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 83 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 84 | return gm->getName(); |
| 85 | } |
| 86 | |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 87 | void GMSrc::modifyGrContextOptions(GrContextOptions* options) const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 88 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 89 | gm->modifyGrContextOptions(options); |
| 90 | } |
| 91 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 92 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 93 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 94 | BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 95 | : fPath(path) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 96 | , fMode(mode) |
| 97 | , fDstColorType(dstColorType) |
| 98 | , fSampleSize(sampleSize) |
| 99 | {} |
| 100 | |
| 101 | bool BRDSrc::veto(SinkFlags flags) const { |
| 102 | // No need to test to non-raster or indirect backends. |
| 103 | return flags.type != SinkFlags::kRaster |
| 104 | || flags.approach != SinkFlags::kDirect; |
| 105 | } |
| 106 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 107 | static SkBitmapRegionDecoder* create_brd(Path path) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 108 | sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str())); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 109 | if (!encoded) { |
| 110 | return NULL; |
| 111 | } |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 112 | return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 115 | static inline void alpha8_to_gray8(SkBitmap* bitmap) { |
| 116 | // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert |
| 117 | // them back to kGray8 so our test framework can draw them correctly. |
| 118 | if (kAlpha_8_SkColorType == bitmap->info().colorType()) { |
| 119 | SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType) |
| 120 | .makeAlphaType(kOpaque_SkAlphaType); |
| 121 | *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo; |
| 122 | } |
| 123 | } |
| 124 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 125 | Error BRDSrc::draw(SkCanvas* canvas) const { |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 126 | if (canvas->imageInfo().colorSpace() && |
| 127 | kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) { |
| 128 | // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only |
| 129 | // run these tests on legacy canvases. |
| 130 | // We allow an exception for F16, since Android uses F16. |
| 131 | return Error::Nonfatal("Skip testing to color correct canvas."); |
| 132 | } |
| 133 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 134 | SkColorType colorType = canvas->imageInfo().colorType(); |
| 135 | if (kRGB_565_SkColorType == colorType && |
| 136 | CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) { |
| 137 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 138 | } |
| 139 | switch (fDstColorType) { |
| 140 | case CodecSrc::kGetFromCanvas_DstColorType: |
| 141 | break; |
| 142 | case CodecSrc::kIndex8_Always_DstColorType: |
| 143 | colorType = kIndex_8_SkColorType; |
| 144 | break; |
| 145 | case CodecSrc::kGrayscale_Always_DstColorType: |
| 146 | colorType = kGray_8_SkColorType; |
| 147 | break; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 148 | default: |
| 149 | SkASSERT(false); |
| 150 | break; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 153 | std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 154 | if (nullptr == brd.get()) { |
| 155 | return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str())); |
| 156 | } |
| 157 | |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 158 | if (!brd->conversionSupported(colorType)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 159 | return Error::Nonfatal("Cannot convert to color type."); |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 160 | } |
| 161 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 162 | const uint32_t width = brd->width(); |
| 163 | const uint32_t height = brd->height(); |
| 164 | // Visually inspecting very small output images is not necessary. |
| 165 | if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) { |
| 166 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 167 | } |
| 168 | switch (fMode) { |
| 169 | case kFullImage_Mode: { |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 170 | SkBitmap bitmap; |
| 171 | if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height), |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 172 | fSampleSize, colorType, false, SkColorSpace::MakeSRGB())) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 173 | return "Cannot decode (full) region."; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 174 | } |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 175 | alpha8_to_gray8(&bitmap); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 176 | canvas->drawBitmap(bitmap, 0, 0); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 177 | return ""; |
| 178 | } |
| 179 | case kDivisor_Mode: { |
| 180 | const uint32_t divisor = 2; |
| 181 | if (width < divisor || height < divisor) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 182 | return Error::Nonfatal("Divisor is larger than image dimension."); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Use a border to test subsets that extend outside the image. |
| 186 | // We will not allow the border to be larger than the image dimensions. Allowing |
| 187 | // these large borders causes off by one errors that indicate a problem with the |
| 188 | // test suite, not a problem with the implementation. |
| 189 | const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor); |
| 190 | const uint32_t scaledBorder = SkTMin(5u, maxBorder); |
| 191 | const uint32_t unscaledBorder = scaledBorder * fSampleSize; |
| 192 | |
| 193 | // We may need to clear the canvas to avoid uninitialized memory. |
| 194 | // Assume we are scaling a 780x780 image with sampleSize = 8. |
| 195 | // The output image should be 97x97. |
| 196 | // Each subset will be 390x390. |
| 197 | // Each scaled subset be 48x48. |
| 198 | // Four scaled subsets will only fill a 96x96 image. |
| 199 | // The bottom row and last column will not be touched. |
| 200 | // This is an unfortunate result of our rounding rules when scaling. |
| 201 | // Maybe we need to consider testing scaled subsets without trying to |
| 202 | // combine them to match the full scaled image? Or maybe this is the |
| 203 | // best we can do? |
| 204 | canvas->clear(0); |
| 205 | |
| 206 | for (uint32_t x = 0; x < divisor; x++) { |
| 207 | for (uint32_t y = 0; y < divisor; y++) { |
| 208 | // Calculate the subset dimensions |
| 209 | uint32_t subsetWidth = width / divisor; |
| 210 | uint32_t subsetHeight = height / divisor; |
| 211 | const int left = x * subsetWidth; |
| 212 | const int top = y * subsetHeight; |
| 213 | |
| 214 | // Increase the size of the last subset in each row or column, when the |
| 215 | // divisor does not divide evenly into the image dimensions |
| 216 | subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0; |
| 217 | subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0; |
| 218 | |
| 219 | // Increase the size of the subset in order to have a border on each side |
| 220 | const int decodeLeft = left - unscaledBorder; |
| 221 | const int decodeTop = top - unscaledBorder; |
| 222 | const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2; |
| 223 | const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 224 | SkBitmap bitmap; |
| 225 | if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft, |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 226 | decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false, |
| 227 | SkColorSpace::MakeSRGB())) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 228 | return "Cannot decode region."; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 229 | } |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 230 | |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 231 | alpha8_to_gray8(&bitmap); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 232 | canvas->drawBitmapRect(bitmap, |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 233 | SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder, |
| 234 | (SkScalar) (subsetWidth / fSampleSize), |
| 235 | (SkScalar) (subsetHeight / fSampleSize)), |
| 236 | SkRect::MakeXYWH((SkScalar) (left / fSampleSize), |
| 237 | (SkScalar) (top / fSampleSize), |
| 238 | (SkScalar) (subsetWidth / fSampleSize), |
| 239 | (SkScalar) (subsetHeight / fSampleSize)), |
| 240 | nullptr); |
| 241 | } |
| 242 | } |
| 243 | return ""; |
| 244 | } |
| 245 | default: |
| 246 | SkASSERT(false); |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 247 | return "Error: Should not be reached."; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | SkISize BRDSrc::size() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 252 | std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 253 | if (brd) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 254 | return {SkTMax(1, brd->width() / (int)fSampleSize), |
| 255 | SkTMax(1, brd->height() / (int)fSampleSize)}; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 256 | } |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 257 | return {0, 0}; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static SkString get_scaled_name(const Path& path, float scale) { |
| 261 | return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale); |
| 262 | } |
| 263 | |
| 264 | Name BRDSrc::name() const { |
| 265 | // We will replicate the names used by CodecSrc so that images can |
| 266 | // be compared in Gold. |
| 267 | if (1 == fSampleSize) { |
| 268 | return SkOSPath::Basename(fPath.c_str()); |
| 269 | } |
msarett | 4b0778e | 2015-11-13 09:59:11 -0800 | [diff] [blame] | 270 | return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 274 | |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 275 | static bool serial_from_path_name(const SkString& path) { |
| 276 | if (!FLAGS_RAW_threading) { |
| 277 | static const char* const exts[] = { |
| 278 | "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw", |
| 279 | "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW", |
| 280 | }; |
| 281 | const char* actualExt = strrchr(path.c_str(), '.'); |
| 282 | if (actualExt) { |
| 283 | actualExt++; |
| 284 | for (auto* ext : exts) { |
| 285 | if (0 == strcmp(ext, actualExt)) { |
| 286 | return true; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 294 | CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType, |
| 295 | float scale) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 296 | : fPath(path) |
| 297 | , fMode(mode) |
| 298 | , fDstColorType(dstColorType) |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 299 | , fDstAlphaType(dstAlphaType) |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 300 | , fScale(scale) |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 301 | , fRunSerially(serial_from_path_name(path)) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 302 | {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 303 | |
mtklein | 99cab4e | 2015-07-31 06:43:04 -0700 | [diff] [blame] | 304 | bool CodecSrc::veto(SinkFlags flags) const { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 305 | // Test to direct raster backends (8888 and 565). |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 306 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 307 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 308 | |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 309 | // Allows us to test decodes to non-native 8888. |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 310 | static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 311 | if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) { |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | for (int y = 0; y < bitmap.height(); y++) { |
| 316 | uint32_t* row = (uint32_t*) bitmap.getAddr(0, y); |
| 317 | SkOpts::RGBA_to_BGRA(row, row, bitmap.width()); |
| 318 | } |
| 319 | } |
| 320 | |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 321 | // FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339. |
| 322 | // This allows us to still test unpremultiplied decodes. |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 323 | static void premultiply_if_necessary(SkBitmap& bitmap) { |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 324 | if (kUnpremul_SkAlphaType != bitmap.alphaType()) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | switch (bitmap.colorType()) { |
Leon Scroggins III | eceee04 | 2017-05-23 16:58:09 -0400 | [diff] [blame^] | 329 | case kRGBA_F16_SkColorType: |
| 330 | for (int y = 0; y < bitmap.height(); y++) { |
| 331 | void* row = bitmap.getAddr(0, y); |
| 332 | SkRasterPipeline p; |
| 333 | p.append(SkRasterPipeline::load_f16, &row); |
| 334 | p.append(SkRasterPipeline::premul); |
| 335 | p.append(SkRasterPipeline::store_f16, &row); |
| 336 | p.run(0, bitmap.width()); |
| 337 | } |
| 338 | break; |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 339 | case kN32_SkColorType: |
| 340 | for (int y = 0; y < bitmap.height(); y++) { |
| 341 | uint32_t* row = (uint32_t*) bitmap.getAddr(0, y); |
| 342 | SkOpts::RGBA_to_rgbA(row, row, bitmap.width()); |
| 343 | } |
| 344 | break; |
| 345 | case kIndex_8_SkColorType: { |
| 346 | SkColorTable* colorTable = bitmap.getColorTable(); |
| 347 | SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 348 | SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count()); |
| 349 | break; |
| 350 | } |
| 351 | default: |
| 352 | // No need to premultiply kGray or k565 outputs. |
| 353 | break; |
| 354 | } |
msarett | e1daa48 | 2016-02-03 15:31:18 -0800 | [diff] [blame] | 355 | |
| 356 | // In the kIndex_8 case, the canvas won't even try to draw unless we mark the |
| 357 | // bitmap as kPremul. |
| 358 | bitmap.setAlphaType(kPremul_SkAlphaType); |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 359 | } |
| 360 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 361 | static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType, |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 362 | CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 363 | switch (dstColorType) { |
| 364 | case CodecSrc::kIndex8_Always_DstColorType: |
| 365 | if (kRGB_565_SkColorType == canvasColorType) { |
| 366 | return false; |
| 367 | } |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 368 | *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 369 | break; |
| 370 | case CodecSrc::kGrayscale_Always_DstColorType: |
scroggo | 1a36192 | 2016-05-20 14:27:16 -0700 | [diff] [blame] | 371 | if (kRGB_565_SkColorType == canvasColorType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 372 | return false; |
| 373 | } |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 374 | *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 375 | break; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 376 | case CodecSrc::kNonNative8888_Always_DstColorType: |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 377 | if (kRGB_565_SkColorType == canvasColorType |
| 378 | || kRGBA_F16_SkColorType == canvasColorType) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 379 | return false; |
| 380 | } |
| 381 | #ifdef SK_PMCOLOR_IS_RGBA |
| 382 | *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType); |
| 383 | #else |
| 384 | *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType); |
| 385 | #endif |
| 386 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 387 | default: |
msarett | 55f7bdd | 2016-02-16 13:24:54 -0800 | [diff] [blame] | 388 | if (kRGB_565_SkColorType == canvasColorType && |
| 389 | kOpaque_SkAlphaType != decodeInfo->alphaType()) { |
| 390 | return false; |
| 391 | } |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 392 | |
| 393 | if (kRGBA_F16_SkColorType == canvasColorType) { |
| 394 | sk_sp<SkColorSpace> linearSpace = |
| 395 | as_CSB(decodeInfo->colorSpace())->makeLinearGamma(); |
| 396 | *decodeInfo = decodeInfo->makeColorSpace(std::move(linearSpace)); |
| 397 | } |
| 398 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 399 | *decodeInfo = decodeInfo->makeColorType(canvasColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 400 | break; |
| 401 | } |
| 402 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 403 | *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 404 | return true; |
| 405 | } |
| 406 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 407 | static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 408 | SkPMColor* colorPtr, int colorCount, CodecSrc::DstColorType dstColorType, |
| 409 | SkScalar left = 0, SkScalar top = 0) { |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 410 | sk_sp<SkColorTable> colorTable(new SkColorTable(colorPtr, colorCount)); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 411 | SkBitmap bitmap; |
| 412 | bitmap.installPixels(info, pixels, rowBytes, colorTable.get(), nullptr, nullptr); |
| 413 | premultiply_if_necessary(bitmap); |
| 414 | swap_rb_if_necessary(bitmap, dstColorType); |
| 415 | canvas->drawBitmap(bitmap, left, top); |
| 416 | } |
| 417 | |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 418 | Error CodecSrc::draw(SkCanvas* canvas) const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 419 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 420 | if (!encoded) { |
| 421 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 422 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 423 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 424 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); |
msarett | 9e707a0 | 2015-09-01 14:57:57 -0700 | [diff] [blame] | 425 | if (nullptr == codec.get()) { |
| 426 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 427 | } |
| 428 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 429 | SkImageInfo decodeInfo = codec->getInfo(); |
| 430 | if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType, |
| 431 | fDstAlphaType)) { |
Matt Sarett | ea518f0 | 2017-02-03 11:58:59 -0800 | [diff] [blame] | 432 | return Error::Nonfatal("Skipping uninteresting test."); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 433 | } |
| 434 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 435 | // Try to scale the image if it is desired |
| 436 | SkISize size = codec->getScaledDimensions(fScale); |
| 437 | if (size == decodeInfo.dimensions() && 1.0f != fScale) { |
| 438 | return Error::Nonfatal("Test without scaling is uninteresting."); |
| 439 | } |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 440 | |
| 441 | // Visually inspecting very small output images is not necessary. We will |
| 442 | // cover these cases in unit testing. |
| 443 | if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) { |
| 444 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 445 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 446 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 447 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 448 | const int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType()); |
| 449 | const size_t rowBytes = size.width() * bpp; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 450 | const size_t safeSize = decodeInfo.getSafeSize(rowBytes); |
| 451 | SkAutoMalloc pixels(safeSize); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 452 | SkPMColor colorPtr[256]; |
| 453 | int colorCount = 256; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 454 | |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 455 | SkCodec::Options options; |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 456 | options.fPremulBehavior = canvas->imageInfo().colorSpace() ? |
| 457 | SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore; |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 458 | if (kCodecZeroInit_Mode == fMode) { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 459 | memset(pixels.get(), 0, size.height() * rowBytes); |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 460 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 461 | } |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 462 | |
| 463 | SkImageInfo bitmapInfo = decodeInfo; |
| 464 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 465 | kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
| 466 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 467 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 468 | |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 469 | switch (fMode) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 470 | case kAnimated_Mode: { |
| 471 | std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo(); |
| 472 | if (frameInfos.size() <= 1) { |
| 473 | return SkStringPrintf("%s is not an animated image.", fPath.c_str()); |
| 474 | } |
| 475 | |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 476 | // As in CodecSrc::size(), compute a roughly square grid to draw the frames |
| 477 | // into. "factor" is the number of frames to draw on one row. There will be |
| 478 | // up to "factor" rows as well. |
| 479 | const float root = sqrt((float) frameInfos.size()); |
| 480 | const int factor = sk_float_ceil2int(root); |
| 481 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 482 | // Used to cache a frame that future frames will depend on. |
| 483 | SkAutoMalloc priorFramePixels; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 484 | int cachedFrame = SkCodec::kNone; |
| 485 | for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 486 | options.fFrameIndex = i; |
| 487 | // Check for a prior frame |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 488 | const int reqFrame = frameInfos[i].fRequiredFrame; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 489 | if (reqFrame != SkCodec::kNone && reqFrame == cachedFrame |
| 490 | && priorFramePixels.get()) { |
| 491 | // Copy into pixels |
| 492 | memcpy(pixels.get(), priorFramePixels.get(), safeSize); |
| 493 | options.fHasPriorFrame = true; |
| 494 | } else { |
| 495 | options.fHasPriorFrame = false; |
| 496 | } |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 497 | SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(), |
| 498 | rowBytes, &options, |
| 499 | colorPtr, &colorCount); |
| 500 | if (SkCodec::kInvalidInput == result && i > 0) { |
| 501 | // Some of our test images have truncated later frames. Treat that |
| 502 | // the same as incomplete. |
| 503 | result = SkCodec::kIncompleteInput; |
| 504 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 505 | switch (result) { |
| 506 | case SkCodec::kSuccess: |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 507 | case SkCodec::kIncompleteInput: { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 508 | // If the next frame depends on this one, store it in priorFrame. |
| 509 | // It is possible that we may discard a frame that future frames depend on, |
| 510 | // but the codec will simply redecode the discarded frame. |
| 511 | // Do this before calling draw_to_canvas, which premultiplies in place. If |
| 512 | // we're decoding to unpremul, we want to pass the unmodified frame to the |
| 513 | // codec for decoding the next frame. |
| 514 | if (static_cast<size_t>(i+1) < frameInfos.size() |
| 515 | && frameInfos[i+1].fRequiredFrame == i) { |
| 516 | memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize); |
| 517 | cachedFrame = i; |
| 518 | } |
| 519 | |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 520 | SkAutoCanvasRestore acr(canvas, true); |
| 521 | const int xTranslate = (i % factor) * decodeInfo.width(); |
| 522 | const int yTranslate = (i / factor) * decodeInfo.height(); |
| 523 | canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 524 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, |
| 525 | colorPtr, colorCount, fDstColorType); |
| 526 | if (result == SkCodec::kIncompleteInput) { |
| 527 | return ""; |
| 528 | } |
| 529 | break; |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 530 | } |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 531 | case SkCodec::kInvalidConversion: |
| 532 | if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType |
| 533 | || decodeInfo.colorType() == kIndex_8_SkColorType)) { |
| 534 | return Error::Nonfatal(SkStringPrintf( |
| 535 | "Cannot decode frame %i to 565/Index8 (%s).", i, fPath.c_str())); |
| 536 | } |
| 537 | // Fall through. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 538 | default: |
| 539 | return SkStringPrintf("Couldn't getPixels for frame %i in %s.", |
| 540 | i, fPath.c_str()); |
| 541 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 542 | } |
| 543 | break; |
| 544 | } |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 545 | case kCodecZeroInit_Mode: |
msarett | 9e707a0 | 2015-09-01 14:57:57 -0700 | [diff] [blame] | 546 | case kCodec_Mode: { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 547 | switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options, |
| 548 | colorPtr, &colorCount)) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 549 | case SkCodec::kSuccess: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 550 | // We consider incomplete to be valid, since we should still decode what is |
| 551 | // available. |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 552 | case SkCodec::kIncompleteInput: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 553 | break; |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 554 | default: |
| 555 | // Everything else is considered a failure. |
| 556 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 557 | } |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 558 | |
| 559 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount, |
| 560 | fDstColorType); |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 561 | break; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 562 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 563 | case kScanline_Mode: { |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 564 | void* dst = pixels.get(); |
| 565 | uint32_t height = decodeInfo.height(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 566 | const bool useIncremental = [this]() { |
| 567 | auto exts = { "png", "PNG", "gif", "GIF" }; |
| 568 | for (auto ext : exts) { |
| 569 | if (fPath.endsWith(ext)) { |
| 570 | return true; |
| 571 | } |
| 572 | } |
| 573 | return false; |
| 574 | }(); |
| 575 | // ico may use the old scanline method or the new one, depending on whether it |
| 576 | // internally holds a bmp or a png. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 577 | const bool ico = fPath.endsWith("ico"); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 578 | bool useOldScanlineMethod = !useIncremental && !ico; |
| 579 | if (useIncremental || ico) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 580 | if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst, |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 581 | rowBytes, &options, colorPtr, &colorCount)) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 582 | int rowsDecoded; |
| 583 | if (SkCodec::kIncompleteInput == codec->incrementalDecode(&rowsDecoded)) { |
| 584 | codec->fillIncompleteImage(decodeInfo, dst, rowBytes, |
| 585 | SkCodec::kNo_ZeroInitialized, height, |
| 586 | rowsDecoded); |
scroggo | 6fb2391 | 2016-06-02 14:16:43 -0700 | [diff] [blame] | 587 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 588 | } else { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 589 | if (useIncremental) { |
| 590 | // Error: These should support incremental decode. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 591 | return "Could not start incremental decode"; |
| 592 | } |
| 593 | // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP, |
| 594 | // which should work via startScanlineDecode |
| 595 | useOldScanlineMethod = true; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (useOldScanlineMethod) { |
| 600 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr, |
| 601 | &colorCount)) { |
| 602 | return "Could not start scanline decoder"; |
| 603 | } |
| 604 | |
| 605 | switch (codec->getScanlineOrder()) { |
| 606 | case SkCodec::kTopDown_SkScanlineOrder: |
| 607 | case SkCodec::kBottomUp_SkScanlineOrder: |
| 608 | // We do not need to check the return value. On an incomplete |
| 609 | // image, memory will be filled with a default value. |
| 610 | codec->getScanlines(dst, height, rowBytes); |
| 611 | break; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 615 | draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCount, fDstColorType); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 616 | break; |
| 617 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 618 | case kStripe_Mode: { |
| 619 | const int height = decodeInfo.height(); |
| 620 | // This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 621 | // does not align with image blocks. |
| 622 | const int stripeHeight = 37; |
| 623 | const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 624 | void* dst = pixels.get(); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 625 | |
| 626 | // Decode odd stripes |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 627 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options, colorPtr, |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 628 | &colorCount)) { |
msarett | b65e604 | 2016-02-23 05:37:25 -0800 | [diff] [blame] | 629 | return "Could not start scanline decoder"; |
| 630 | } |
| 631 | |
| 632 | // This mode was designed to test the new skip scanlines API in libjpeg-turbo. |
| 633 | // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting |
| 634 | // 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] | 635 | // We only run this on Jpeg, which is always kTopDown. |
| 636 | SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 637 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 638 | for (int i = 0; i < numStripes; i += 2) { |
| 639 | // Skip a stripe |
| 640 | const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 641 | codec->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 642 | |
| 643 | // Read a stripe |
| 644 | const int startY = (i + 1) * stripeHeight; |
| 645 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 646 | if (linesToRead > 0) { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 647 | codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead, |
| 648 | rowBytes); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
| 652 | // Decode even stripes |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 653 | const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr, |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 654 | colorPtr, &colorCount); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 655 | if (SkCodec::kSuccess != startResult) { |
| 656 | return "Failed to restart scanline decoder with same parameters."; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 657 | } |
| 658 | for (int i = 0; i < numStripes; i += 2) { |
| 659 | // Read a stripe |
| 660 | const int startY = i * stripeHeight; |
| 661 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 662 | codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead, |
| 663 | rowBytes); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 664 | |
| 665 | // Skip a stripe |
msarett | f6db27e | 2015-06-12 09:34:04 -0700 | [diff] [blame] | 666 | const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); |
| 667 | if (linesToSkip > 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 668 | codec->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 669 | } |
| 670 | } |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 671 | |
| 672 | draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCount, fDstColorType); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 673 | break; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 674 | } |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 675 | case kCroppedScanline_Mode: { |
| 676 | const int width = decodeInfo.width(); |
| 677 | const int height = decodeInfo.height(); |
| 678 | // This value is chosen because, as we move across the image, it will sometimes |
| 679 | // align with the jpeg block sizes and it will sometimes not. This allows us |
| 680 | // to test interestingly different code paths in the implementation. |
| 681 | const int tileSize = 36; |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 682 | SkIRect subset; |
| 683 | for (int x = 0; x < width; x += tileSize) { |
| 684 | subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height); |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 685 | options.fSubset = ⊂ |
| 686 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options, |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 687 | colorPtr, &colorCount)) { |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 688 | return "Could not start scanline decoder."; |
| 689 | } |
| 690 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 691 | codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes); |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 692 | } |
| 693 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 694 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount, |
| 695 | fDstColorType); |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 696 | break; |
| 697 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 698 | case kSubset_Mode: { |
| 699 | // Arbitrarily choose a divisor. |
| 700 | int divisor = 2; |
| 701 | // Total width/height of the image. |
| 702 | const int W = codec->getInfo().width(); |
| 703 | const int H = codec->getInfo().height(); |
| 704 | if (divisor > W || divisor > H) { |
| 705 | return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big " |
| 706 | "for %s with dimensions (%d x %d)", divisor, |
| 707 | fPath.c_str(), W, H)); |
| 708 | } |
| 709 | // subset dimensions |
| 710 | // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 711 | const int w = SkAlign2(W / divisor); |
| 712 | const int h = SkAlign2(H / divisor); |
| 713 | SkIRect subset; |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 714 | options.fSubset = ⊂ |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 715 | SkBitmap subsetBm; |
| 716 | // We will reuse pixel memory from bitmap. |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 717 | void* dst = pixels.get(); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 718 | // Keep track of left and top (for drawing subsetBm into canvas). We could use |
| 719 | // fScale * x and fScale * y, but we want integers such that the next subset will start |
| 720 | // where the last one ended. So we'll add decodeInfo.width() and height(). |
| 721 | int left = 0; |
| 722 | for (int x = 0; x < W; x += w) { |
| 723 | int top = 0; |
| 724 | for (int y = 0; y < H; y+= h) { |
| 725 | // Do not make the subset go off the edge of the image. |
| 726 | const int preScaleW = SkTMin(w, W - x); |
| 727 | const int preScaleH = SkTMin(h, H - y); |
| 728 | subset.setXYWH(x, y, preScaleW, preScaleH); |
| 729 | // And scale |
| 730 | // FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 731 | // into account? |
msarett | c7eb490 | 2016-04-25 07:04:58 -0700 | [diff] [blame] | 732 | const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)); |
| 733 | const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)); |
| 734 | decodeInfo = decodeInfo.makeWH(scaledW, scaledH); |
| 735 | SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 736 | size_t subsetRowBytes = subsetBitmapInfo.minRowBytes(); |
| 737 | const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes, |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 738 | &options, colorPtr, &colorCount); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 739 | switch (result) { |
| 740 | case SkCodec::kSuccess: |
| 741 | case SkCodec::kIncompleteInput: |
| 742 | break; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 743 | default: |
| 744 | return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) " |
| 745 | "from %s with dimensions (%d x %d)\t error %d", |
| 746 | x, y, decodeInfo.width(), decodeInfo.height(), |
| 747 | fPath.c_str(), W, H, result); |
| 748 | } |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 749 | draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, colorPtr, |
| 750 | colorCount, fDstColorType, SkIntToScalar(left), |
| 751 | SkIntToScalar(top)); |
| 752 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 753 | // translate by the scaled height. |
| 754 | top += decodeInfo.height(); |
| 755 | } |
| 756 | // translate by the scaled width. |
| 757 | left += decodeInfo.width(); |
| 758 | } |
| 759 | return ""; |
| 760 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 761 | default: |
| 762 | SkASSERT(false); |
| 763 | return "Invalid fMode"; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 764 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 765 | return ""; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | SkISize CodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 769 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 770 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); |
scroggo | 7fac5af | 2015-09-30 11:33:12 -0700 | [diff] [blame] | 771 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 772 | return {0, 0}; |
scroggo | 7fac5af | 2015-09-30 11:33:12 -0700 | [diff] [blame] | 773 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 774 | |
| 775 | auto imageSize = codec->getScaledDimensions(fScale); |
| 776 | if (fMode == kAnimated_Mode) { |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 777 | // We'll draw one of each frame, so make it big enough to hold them all |
| 778 | // in a grid. The grid will be roughly square, with "factor" frames per |
| 779 | // row and up to "factor" rows. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 780 | const size_t count = codec->getFrameInfo().size(); |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 781 | const float root = sqrt((float) count); |
| 782 | const int factor = sk_float_ceil2int(root); |
| 783 | imageSize.fWidth = imageSize.fWidth * factor; |
| 784 | imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 785 | } |
| 786 | return imageSize; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | Name CodecSrc::name() const { |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 790 | if (1.0f == fScale) { |
scroggo | 6e8c68e | 2016-10-24 13:48:49 -0700 | [diff] [blame] | 791 | Name name = SkOSPath::Basename(fPath.c_str()); |
| 792 | if (fMode == kAnimated_Mode) { |
| 793 | name.append("_animated"); |
| 794 | } |
| 795 | return name; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 796 | } |
scroggo | 6e8c68e | 2016-10-24 13:48:49 -0700 | [diff] [blame] | 797 | SkASSERT(fMode != kAnimated_Mode); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 798 | return get_scaled_name(fPath, fScale); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 802 | |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 803 | AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType, |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 804 | SkAlphaType dstAlphaType, int sampleSize) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 805 | : fPath(path) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 806 | , fDstColorType(dstColorType) |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 807 | , fDstAlphaType(dstAlphaType) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 808 | , fSampleSize(sampleSize) |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 809 | , fRunSerially(serial_from_path_name(path)) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 810 | {} |
| 811 | |
| 812 | bool AndroidCodecSrc::veto(SinkFlags flags) const { |
| 813 | // No need to test decoding to non-raster or indirect backend. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 814 | return flags.type != SinkFlags::kRaster |
| 815 | || flags.approach != SinkFlags::kDirect; |
| 816 | } |
| 817 | |
| 818 | Error AndroidCodecSrc::draw(SkCanvas* canvas) const { |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 819 | if (canvas->imageInfo().colorSpace() && |
| 820 | kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) { |
| 821 | // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only |
| 822 | // run these tests on legacy canvases. |
| 823 | // We allow an exception for F16, since Android uses F16. |
| 824 | return Error::Nonfatal("Skip testing to color correct canvas."); |
| 825 | } |
| 826 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 827 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 828 | if (!encoded) { |
| 829 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 830 | } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 831 | std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 832 | if (nullptr == codec.get()) { |
| 833 | return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str()); |
| 834 | } |
| 835 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 836 | SkImageInfo decodeInfo = codec->getInfo(); |
| 837 | if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType, |
| 838 | fDstAlphaType)) { |
Matt Sarett | ea518f0 | 2017-02-03 11:58:59 -0800 | [diff] [blame] | 839 | return Error::Nonfatal("Skipping uninteresting test."); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | // Scale the image if it is desired. |
| 843 | SkISize size = codec->getSampledDimensions(fSampleSize); |
| 844 | |
| 845 | // Visually inspecting very small output images is not necessary. We will |
| 846 | // cover these cases in unit testing. |
| 847 | if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) { |
| 848 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 849 | } |
| 850 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 851 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 852 | int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType()); |
| 853 | size_t rowBytes = size.width() * bpp; |
| 854 | SkAutoMalloc pixels(size.height() * rowBytes); |
| 855 | SkPMColor colorPtr[256]; |
| 856 | int colorCount = 256; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 857 | |
| 858 | SkBitmap bitmap; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 859 | SkImageInfo bitmapInfo = decodeInfo; |
| 860 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 861 | kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
| 862 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 863 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 864 | |
| 865 | // Create options for the codec. |
| 866 | SkAndroidCodec::AndroidOptions options; |
| 867 | options.fColorPtr = colorPtr; |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 868 | options.fColorCount = &colorCount; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 869 | options.fSampleSize = fSampleSize; |
| 870 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 871 | switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) { |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 872 | case SkCodec::kSuccess: |
| 873 | case SkCodec::kIncompleteInput: |
| 874 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 875 | default: |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 876 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 877 | } |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 878 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount, fDstColorType); |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 879 | return ""; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | SkISize AndroidCodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 883 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 884 | std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 885 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 886 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 887 | } |
| 888 | return codec->getSampledDimensions(fSampleSize); |
| 889 | } |
| 890 | |
| 891 | Name AndroidCodecSrc::name() const { |
| 892 | // We will replicate the names used by CodecSrc so that images can |
| 893 | // be compared in Gold. |
| 894 | if (1 == fSampleSize) { |
| 895 | return SkOSPath::Basename(fPath.c_str()); |
| 896 | } |
msarett | 4b0778e | 2015-11-13 09:59:11 -0800 | [diff] [blame] | 897 | return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 901 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 902 | ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu) |
| 903 | : fPath(path) |
| 904 | , fMode(mode) |
| 905 | , fDstAlphaType(alphaType) |
| 906 | , fIsGpu(isGpu) |
| 907 | , fRunSerially(serial_from_path_name(path)) |
| 908 | {} |
| 909 | |
| 910 | bool ImageGenSrc::veto(SinkFlags flags) const { |
| 911 | if (fIsGpu) { |
Brian Salomon | bd7c551 | 2017-03-07 09:08:36 -0500 | [diff] [blame] | 912 | // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs. |
| 913 | return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect || |
| 914 | flags.multisampled == SinkFlags::kMultisampled; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
| 918 | } |
| 919 | |
| 920 | Error ImageGenSrc::draw(SkCanvas* canvas) const { |
| 921 | if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 922 | return Error::Nonfatal("Uninteresting to test image generator to 565."); |
| 923 | } |
| 924 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 925 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 926 | if (!encoded) { |
| 927 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 928 | } |
| 929 | |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 930 | #if defined(SK_BUILD_FOR_WIN) |
| 931 | // Initialize COM in order to test with WIC. |
| 932 | SkAutoCoInitialize com; |
| 933 | if (!com.succeeded()) { |
| 934 | return "Could not initialize COM."; |
| 935 | } |
| 936 | #endif |
| 937 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 938 | std::unique_ptr<SkImageGenerator> gen(nullptr); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 939 | switch (fMode) { |
| 940 | case kCodec_Mode: |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 941 | gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 942 | if (!gen) { |
| 943 | return "Could not create codec image generator."; |
| 944 | } |
| 945 | break; |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 946 | case kPlatform_Mode: { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 947 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 948 | gen.reset(SkImageGeneratorCG::NewFromEncodedCG(encoded.get())); |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 949 | #elif defined(SK_BUILD_FOR_WIN) |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 950 | gen.reset(SkImageGeneratorWIC::NewFromEncodedWIC(encoded.get())); |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 951 | #endif |
| 952 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 953 | if (!gen) { |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 954 | return "Could not create platform image generator."; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 955 | } |
| 956 | break; |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 957 | } |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 958 | default: |
| 959 | SkASSERT(false); |
| 960 | return "Invalid image generator mode"; |
| 961 | } |
| 962 | |
| 963 | // Test deferred decoding path on GPU |
| 964 | if (fIsGpu) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 965 | sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 966 | if (!image) { |
| 967 | return "Could not create image from codec image generator."; |
| 968 | } |
| 969 | canvas->drawImage(image, 0, 0); |
| 970 | return ""; |
| 971 | } |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 972 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 973 | // Test various color and alpha types on CPU |
| 974 | SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType); |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 975 | |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 976 | SkImageGenerator::Options options; |
| 977 | options.fBehavior = canvas->imageInfo().colorSpace() ? |
| 978 | SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore; |
| 979 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 980 | int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType()); |
| 981 | size_t rowBytes = decodeInfo.width() * bpp; |
| 982 | SkAutoMalloc pixels(decodeInfo.height() * rowBytes); |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 983 | if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) { |
Matt Sarett | 05cb4c3 | 2017-03-02 12:07:46 -0500 | [diff] [blame] | 984 | SkString err = |
| 985 | SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str()); |
| 986 | |
| 987 | #if defined(SK_BUILD_FOR_WIN) |
| 988 | if (kPlatform_Mode == fMode) { |
| 989 | // Do not issue a fatal error for WIC flakiness. |
| 990 | return Error::Nonfatal(err); |
| 991 | } |
| 992 | #endif |
| 993 | |
| 994 | return err; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 997 | SkPMColor colorPtr[256]; |
| 998 | int colorCount = 256; |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 999 | draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes, colorPtr, colorCount, |
| 1000 | CodecSrc::kGetFromCanvas_DstColorType); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 1001 | return ""; |
| 1002 | } |
| 1003 | |
| 1004 | SkISize ImageGenSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 1005 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1006 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 1007 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1008 | return {0, 0}; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 1009 | } |
| 1010 | return codec->getInfo().dimensions(); |
| 1011 | } |
| 1012 | |
| 1013 | Name ImageGenSrc::name() const { |
| 1014 | return SkOSPath::Basename(fPath.c_str()); |
| 1015 | } |
| 1016 | |
| 1017 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1018 | |
msarett | 9ce3a54 | 2016-07-15 13:54:38 -0700 | [diff] [blame] | 1019 | ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, SkColorType colorType) |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1020 | : fPath(path) |
| 1021 | , fMode(mode) |
msarett | 9ce3a54 | 2016-07-15 13:54:38 -0700 | [diff] [blame] | 1022 | , fColorType(colorType) |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1023 | {} |
| 1024 | |
| 1025 | bool ColorCodecSrc::veto(SinkFlags flags) const { |
| 1026 | // Test to direct raster backends (8888 and 565). |
| 1027 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
| 1028 | } |
| 1029 | |
| 1030 | Error ColorCodecSrc::draw(SkCanvas* canvas) const { |
| 1031 | if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 1032 | return Error::Nonfatal("No need to test color correction to 565 backend."); |
| 1033 | } |
| 1034 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1035 | bool runInLegacyMode = kBaseline_Mode == fMode; |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1036 | if (runInLegacyMode && canvas->imageInfo().colorSpace()) { |
| 1037 | return Error::Nonfatal("Skipping tests that are only interesting in legacy mode."); |
| 1038 | } else if (!runInLegacyMode && !canvas->imageInfo().colorSpace()) { |
| 1039 | return Error::Nonfatal("Skipping tests that are only interesting in srgb mode."); |
msarett | 9ce3a54 | 2016-07-15 13:54:38 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 1042 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1043 | if (!encoded) { |
| 1044 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 1045 | } |
| 1046 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1047 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1048 | if (nullptr == codec.get()) { |
| 1049 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| 1050 | } |
| 1051 | |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 1052 | // Load the dst ICC profile. This particular dst is fairly similar to Adobe RGB. |
| 1053 | sk_sp<SkData> dstData = SkData::MakeFromFileName( |
msarett | a714bc3 | 2016-07-29 08:58:33 -0700 | [diff] [blame] | 1054 | GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 1055 | if (!dstData) { |
| 1056 | return "Cannot read monitor profile. Is the resource path set correctly?"; |
| 1057 | } |
| 1058 | |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1059 | sk_sp<SkColorSpace> dstSpace = nullptr; |
| 1060 | if (kDst_sRGB_Mode == fMode) { |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 1061 | dstSpace = SkColorSpace::MakeSRGB(); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1062 | } else if (kDst_HPZR30w_Mode == fMode) { |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 1063 | dstSpace = SkColorSpace::MakeICC(dstData->data(), dstData->size()); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | SkImageInfo decodeInfo = codec->getInfo().makeColorType(fColorType).makeColorSpace(dstSpace); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1067 | if (kUnpremul_SkAlphaType == decodeInfo.alphaType()) { |
| 1068 | decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType); |
| 1069 | } |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1070 | if (kRGBA_F16_SkColorType == fColorType) { |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 1071 | SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(decodeInfo.colorSpace())->type()); |
| 1072 | SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(decodeInfo.colorSpace()); |
| 1073 | decodeInfo = decodeInfo.makeColorSpace(csXYZ->makeLinearGamma()); |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1074 | } |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1075 | |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1076 | SkImageInfo bitmapInfo = decodeInfo; |
| 1077 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 1078 | kBGRA_8888_SkColorType == decodeInfo.colorType()) |
| 1079 | { |
| 1080 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 1081 | } |
| 1082 | |
| 1083 | SkBitmap bitmap; |
| 1084 | if (!bitmap.tryAllocPixels(bitmapInfo)) { |
| 1085 | return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
| 1086 | bitmapInfo.width(), bitmapInfo.height()); |
| 1087 | } |
| 1088 | |
| 1089 | size_t rowBytes = bitmap.rowBytes(); |
| 1090 | SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), rowBytes); |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 1091 | if (SkCodec::kSuccess != r && SkCodec::kIncompleteInput != r) { |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 1092 | return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1095 | switch (fMode) { |
| 1096 | case kBaseline_Mode: |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 1097 | case kDst_sRGB_Mode: |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1098 | case kDst_HPZR30w_Mode: |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 1099 | canvas->drawBitmap(bitmap, 0, 0); |
| 1100 | break; |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1101 | default: |
| 1102 | SkASSERT(false); |
| 1103 | return "Invalid fMode"; |
| 1104 | } |
| 1105 | return ""; |
| 1106 | } |
| 1107 | |
| 1108 | SkISize ColorCodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 1109 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1110 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1111 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1112 | return {0, 0}; |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1113 | } |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1114 | return {codec->getInfo().width(), codec->getInfo().height()}; |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | Name ColorCodecSrc::name() const { |
| 1118 | return SkOSPath::Basename(fPath.c_str()); |
| 1119 | } |
| 1120 | |
| 1121 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1122 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1123 | static const SkRect kSKPViewport = {0,0, 1000,1000}; |
| 1124 | |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 1125 | SKPSrc::SKPSrc(Path path) : fPath(path) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1126 | |
| 1127 | Error SKPSrc::draw(SkCanvas* canvas) const { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1128 | std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str()); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 1129 | if (!stream) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1130 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 1131 | } |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1132 | sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get())); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 1133 | if (!pic) { |
| 1134 | return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str()); |
| 1135 | } |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1136 | stream = nullptr; // Might as well drop this when we're done with it. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 1137 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1138 | canvas->clipRect(kSKPViewport); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1139 | canvas->drawPicture(pic); |
| 1140 | return ""; |
| 1141 | } |
| 1142 | |
| 1143 | SkISize SKPSrc::size() const { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1144 | std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str()); |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1145 | if (!stream) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1146 | return {0, 0}; |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1147 | } |
| 1148 | SkPictInfo info; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1149 | if (!SkPicture::InternalOnly_StreamIsSKP(stream.get(), &info)) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1150 | return {0, 0}; |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1151 | } |
| 1152 | SkRect viewport = kSKPViewport; |
| 1153 | if (!viewport.intersect(info.fCullRect)) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1154 | return {0, 0}; |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1155 | } |
| 1156 | return viewport.roundOut().size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 1160 | |
| 1161 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1162 | #if defined(SK_XML) |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1163 | // Used when the image doesn't have an intrinsic size. |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1164 | static const SkSize kDefaultSVGSize = {1000, 1000}; |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1165 | |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1166 | // Used to force-scale tiny fixed-size images. |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1167 | static const SkSize kMinimumSVGSize = {128, 128}; |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1168 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1169 | SVGSrc::SVGSrc(Path path) |
| 1170 | : fName(SkOSPath::Basename(path.c_str())) |
| 1171 | , fScale(1) { |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1172 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1173 | SkFILEStream stream(path.c_str()); |
| 1174 | if (!stream.isValid()) { |
| 1175 | return; |
| 1176 | } |
| 1177 | fDom = SkSVGDOM::MakeFromStream(stream); |
| 1178 | if (!fDom) { |
| 1179 | return; |
| 1180 | } |
| 1181 | |
| 1182 | const SkSize& sz = fDom->containerSize(); |
| 1183 | if (sz.isEmpty()) { |
| 1184 | // no intrinsic size |
| 1185 | fDom->setContainerSize(kDefaultSVGSize); |
| 1186 | } else { |
| 1187 | fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(), |
| 1188 | kMinimumSVGSize.height() / sz.height())); |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | Error SVGSrc::draw(SkCanvas* canvas) const { |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1193 | if (!fDom) { |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1194 | return SkStringPrintf("Unable to parse file: %s", fName.c_str()); |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1197 | SkAutoCanvasRestore acr(canvas, true); |
| 1198 | canvas->scale(fScale, fScale); |
| 1199 | fDom->render(canvas); |
| 1200 | |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1201 | return ""; |
| 1202 | } |
| 1203 | |
| 1204 | SkISize SVGSrc::size() const { |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1205 | if (!fDom) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1206 | return {0, 0}; |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1209 | return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale} |
| 1210 | .toRound(); |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1213 | Name SVGSrc::name() const { return fName; } |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1214 | |
fmalita | 179d885 | 2016-08-16 14:23:29 -0700 | [diff] [blame] | 1215 | bool SVGSrc::veto(SinkFlags flags) const { |
| 1216 | // No need to test to non-(raster||gpu) or indirect backends. |
| 1217 | bool type_ok = flags.type == SinkFlags::kRaster |
| 1218 | || flags.type == SinkFlags::kGPU; |
| 1219 | |
| 1220 | return !type_ok || flags.approach != SinkFlags::kDirect; |
| 1221 | } |
| 1222 | |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1223 | #endif // defined(SK_XML) |
| 1224 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1225 | |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1226 | MSKPSrc::MSKPSrc(Path path) : fPath(path) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1227 | std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str()); |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1228 | int count = SkMultiPictureDocumentReadPageCount(stream.get()); |
| 1229 | if (count > 0) { |
| 1230 | fPages.reset(count); |
| 1231 | (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count()); |
| 1232 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1235 | int MSKPSrc::pageCount() const { return fPages.count(); } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1236 | |
| 1237 | SkISize MSKPSrc::size() const { return this->size(0); } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1238 | SkISize MSKPSrc::size(int i) const { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1239 | 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] | 1240 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1241 | |
| 1242 | Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); } |
| 1243 | Error MSKPSrc::draw(int i, SkCanvas* canvas) const { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1244 | if (this->pageCount() == 0) { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1245 | return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str()); |
| 1246 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1247 | if (i >= fPages.count() || i < 0) { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1248 | return SkStringPrintf("MultiPictureDocument page number out of range: %d", i); |
| 1249 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1250 | SkPicture* page = fPages[i].fPicture.get(); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1251 | if (!page) { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1252 | std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str()); |
| 1253 | if (!stream) { |
| 1254 | return SkStringPrintf("Unable to open file: %s", fPath.c_str()); |
| 1255 | } |
| 1256 | if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) { |
| 1257 | return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i, |
| 1258 | fPath.c_str()); |
| 1259 | } |
| 1260 | page = fPages[i].fPicture.get(); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1261 | } |
| 1262 | canvas->drawPicture(page); |
| 1263 | return ""; |
| 1264 | } |
| 1265 | |
| 1266 | Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 1267 | |
| 1268 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1269 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 1270 | Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1271 | return src.draw(SkMakeNullCanvas().get()); |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1275 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1276 | DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?"); |
| 1277 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 1278 | GPUSink::GPUSink(GrContextFactory::ContextType ct, |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 1279 | GrContextFactory::ContextOverrides overrides, |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 1280 | int samples, |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 1281 | bool diText, |
brianosman | d93c120 | 2016-03-10 07:49:08 -0800 | [diff] [blame] | 1282 | SkColorType colorType, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1283 | sk_sp<SkColorSpace> colorSpace, |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 1284 | bool threaded) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1285 | : fContextType(ct) |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 1286 | , fContextOverrides(overrides) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1287 | , fSampleCount(samples) |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 1288 | , fUseDIText(diText) |
brianosman | d93c120 | 2016-03-10 07:49:08 -0800 | [diff] [blame] | 1289 | , fColorType(colorType) |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1290 | , fColorSpace(std::move(colorSpace)) |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 1291 | , fThreaded(threaded) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1292 | |
bsalomon | 648c696 | 2015-10-23 09:06:59 -0700 | [diff] [blame] | 1293 | DEFINE_bool(imm, false, "Run gpu configs in immediate mode."); |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 1294 | DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing."); |
bsalomon | 648c696 | 2015-10-23 09:06:59 -0700 | [diff] [blame] | 1295 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1296 | Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const { |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1297 | GrContextOptions grOptions; |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 1298 | grOptions.fImmediateMode = FLAGS_imm; |
kkinnunen | 64492c4 | 2015-12-08 01:24:40 -0800 | [diff] [blame] | 1299 | |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1300 | src.modifyGrContextOptions(&grOptions); |
| 1301 | |
| 1302 | GrContextFactory factory(grOptions); |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1303 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1304 | const SkImageInfo info = |
brianosman | d93c120 | 2016-03-10 07:49:08 -0800 | [diff] [blame] | 1305 | SkImageInfo::Make(size.width(), size.height(), fColorType, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1306 | kPremul_SkAlphaType, fColorSpace); |
msarett | 13a036b | 2016-02-08 09:10:47 -0800 | [diff] [blame] | 1307 | #if SK_SUPPORT_GPU |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 1308 | GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext(); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1309 | const int maxDimension = context->caps()->maxTextureSize(); |
msarett | 13a036b | 2016-02-08 09:10:47 -0800 | [diff] [blame] | 1310 | if (maxDimension < SkTMax(size.width(), size.height())) { |
| 1311 | return Error::Nonfatal("Src too large to create a texture.\n"); |
| 1312 | } |
| 1313 | #endif |
| 1314 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1315 | auto surface( |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 1316 | NewGpuSurface(&factory, fContextType, fContextOverrides, info, fSampleCount, fUseDIText)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1317 | if (!surface) { |
| 1318 | return "Could not create a surface."; |
| 1319 | } |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1320 | if (FLAGS_preAbandonGpuContext) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1321 | factory.abandonContexts(); |
| 1322 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1323 | SkCanvas* canvas = surface->getCanvas(); |
| 1324 | Error err = src.draw(canvas); |
| 1325 | if (!err.isEmpty()) { |
| 1326 | return err; |
| 1327 | } |
| 1328 | canvas->flush(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1329 | if (FLAGS_gpuStats) { |
| 1330 | canvas->getGrContext()->dumpCacheStats(log); |
| 1331 | canvas->getGrContext()->dumpGpuStats(log); |
| 1332 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1333 | dst->allocPixels(info); |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 1334 | canvas->readPixels(*dst, 0, 0); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 1335 | if (FLAGS_abandonGpuContext) { |
| 1336 | factory.abandonContexts(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 1337 | } else if (FLAGS_releaseAndAbandonGpuContext) { |
| 1338 | factory.releaseResourcesAndAbandonContexts(); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 1339 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1340 | return ""; |
| 1341 | } |
| 1342 | |
| 1343 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1344 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1345 | static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) { |
halcanary | 4ba051c | 2016-03-10 10:31:53 -0800 | [diff] [blame] | 1346 | if (src.size().isEmpty()) { |
| 1347 | return "Source has empty dimensions"; |
| 1348 | } |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1349 | SkASSERT(doc); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1350 | int pageCount = src.pageCount(); |
| 1351 | for (int i = 0; i < pageCount; ++i) { |
| 1352 | int width = src.size(i).width(), height = src.size(i).height(); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1353 | SkCanvas* canvas = |
| 1354 | doc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); |
| 1355 | if (!canvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1356 | return "SkDocument::beginPage(w,h) returned nullptr"; |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1357 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1358 | Error err = src.draw(i, canvas); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1359 | if (!err.isEmpty()) { |
| 1360 | return err; |
| 1361 | } |
| 1362 | doc->endPage(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1363 | } |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 1364 | doc->close(); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 1365 | dst->flush(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1366 | return ""; |
| 1367 | } |
| 1368 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1369 | Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1370 | SkDocument::PDFMetadata metadata; |
| 1371 | metadata.fTitle = src.name(); |
| 1372 | metadata.fSubject = "rendering correctness test"; |
| 1373 | metadata.fCreator = "Skia/DM"; |
| 1374 | sk_sp<SkDocument> doc = SkDocument::MakePDF(dst, SK_ScalarDefaultRasterDPI, |
| 1375 | metadata, nullptr, fPDFA); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1376 | if (!doc) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1377 | return "SkDocument::MakePDF() returned nullptr"; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1378 | } |
| 1379 | return draw_skdocument(src, doc.get(), dst); |
| 1380 | } |
| 1381 | |
| 1382 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1383 | |
| 1384 | XPSSink::XPSSink() {} |
| 1385 | |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 1386 | #ifdef SK_BUILD_FOR_WIN |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1387 | static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() { |
| 1388 | IXpsOMObjectFactory* factory; |
| 1389 | HRN(CoCreateInstance(CLSID_XpsOMObjectFactory, |
| 1390 | nullptr, |
| 1391 | CLSCTX_INPROC_SERVER, |
| 1392 | IID_PPV_ARGS(&factory))); |
| 1393 | return SkTScopedComPtr<IXpsOMObjectFactory>(factory); |
| 1394 | } |
| 1395 | |
| 1396 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 1397 | SkAutoCoInitialize com; |
| 1398 | if (!com.succeeded()) { |
| 1399 | return "Could not initialize COM."; |
| 1400 | } |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1401 | SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory(); |
| 1402 | if (!factory) { |
| 1403 | return "Failed to create XPS Factory."; |
| 1404 | } |
| 1405 | sk_sp<SkDocument> doc(SkDocument::MakeXPS(dst, factory.get())); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1406 | if (!doc) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1407 | return "SkDocument::MakeXPS() returned nullptr"; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1408 | } |
| 1409 | return draw_skdocument(src, doc.get(), dst); |
| 1410 | } |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1411 | #else |
| 1412 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 1413 | return "XPS not supported on this platform."; |
| 1414 | } |
| 1415 | #endif |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 1416 | |
| 1417 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1418 | |
| 1419 | PipeSink::PipeSink() {} |
| 1420 | |
| 1421 | Error PipeSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 1422 | return src.draw(SkPipeSerializer().beginWrite(SkRect::Make(src.size()), dst)); |
| 1423 | } |
| 1424 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1425 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1426 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1427 | SKPSink::SKPSink() {} |
| 1428 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1429 | Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1430 | SkSize size; |
| 1431 | size = src.size(); |
| 1432 | SkPictureRecorder recorder; |
| 1433 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 1434 | if (!err.isEmpty()) { |
| 1435 | return err; |
| 1436 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1437 | recorder.finishRecordingAsPicture()->serialize(dst); |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1438 | return ""; |
| 1439 | } |
| 1440 | |
| 1441 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1442 | |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 1443 | Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 1444 | SkDebugCanvas debugCanvas(src.size().width(), src.size().height()); |
| 1445 | Error err = src.draw(&debugCanvas); |
| 1446 | if (!err.isEmpty()) { |
| 1447 | return err; |
| 1448 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1449 | std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas(); |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 1450 | UrlDataManager dataManager(SkString("data")); |
| 1451 | Json::Value json = debugCanvas.toJSON( |
| 1452 | dataManager, debugCanvas.getSize(), nullCanvas.get()); |
| 1453 | std::string value = Json::StyledWriter().write(json); |
| 1454 | return dst->write(value.c_str(), value.size()) ? "" : "SkWStream Error"; |
| 1455 | } |
| 1456 | |
| 1457 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1458 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 1459 | SVGSink::SVGSink() {} |
| 1460 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1461 | Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 1462 | #if defined(SK_XML) |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1463 | std::unique_ptr<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst)); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1464 | return src.draw(SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()), |
| 1465 | SkIntToScalar(src.size().height())), |
| 1466 | xmlWriter.get()).get()); |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 1467 | #else |
| 1468 | return Error("SVG sink is disabled."); |
| 1469 | #endif // SK_XML |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1473 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1474 | RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace) |
mtklein | 27c3fdd | 2016-02-26 14:43:21 -0800 | [diff] [blame] | 1475 | : fColorType(colorType) |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1476 | , fColorSpace(std::move(colorSpace)) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1477 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1478 | Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1479 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1480 | // If there's an appropriate alpha type for this color type, use it, otherwise use premul. |
| 1481 | SkAlphaType alphaType = kPremul_SkAlphaType; |
| 1482 | (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType); |
| 1483 | |
mtklein | 27c3fdd | 2016-02-26 14:43:21 -0800 | [diff] [blame] | 1484 | dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1485 | fColorType, alphaType, fColorSpace), |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 1486 | nullptr/*colortable*/, SkBitmap::kZeroPixels_AllocFlag); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1487 | SkCanvas canvas(*dst); |
| 1488 | return src.draw(&canvas); |
| 1489 | } |
| 1490 | |
| 1491 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1492 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1493 | // 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] | 1494 | // 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] | 1495 | // Several examples below. |
| 1496 | |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1497 | template <typename Fn> |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1498 | static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log, |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1499 | SkISize size, const Fn& draw) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1500 | class ProxySrc : public Src { |
| 1501 | public: |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1502 | ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {} |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1503 | Error draw(SkCanvas* canvas) const override { return fDraw(canvas); } |
halcanary | b4a7f14 | 2016-03-30 08:31:27 -0700 | [diff] [blame] | 1504 | Name name() const override { return "ProxySrc"; } |
| 1505 | SkISize size() const override { return fSize; } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1506 | private: |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1507 | SkISize fSize; |
| 1508 | const Fn& fDraw; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1509 | }; |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1510 | return sink->draw(ProxySrc(size, draw), bitmap, stream, log); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1514 | |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1515 | DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output."); |
| 1516 | |
| 1517 | // Is *bitmap identical to what you get drawing src into sink? |
| 1518 | static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) { |
| 1519 | // We can only check raster outputs. |
| 1520 | // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.) |
| 1521 | if (FLAGS_check && bitmap) { |
| 1522 | SkBitmap reference; |
| 1523 | SkString log; |
halcanary | b4a7f14 | 2016-03-30 08:31:27 -0700 | [diff] [blame] | 1524 | SkDynamicMemoryWStream wStream; |
| 1525 | Error err = sink->draw(src, &reference, &wStream, &log); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1526 | // If we can draw into this Sink via some pipeline, we should be able to draw directly. |
| 1527 | SkASSERT(err.isEmpty()); |
| 1528 | if (!err.isEmpty()) { |
| 1529 | return err; |
| 1530 | } |
| 1531 | // The dimensions are a property of the Src only, and so should be identical. |
| 1532 | SkASSERT(reference.getSize() == bitmap->getSize()); |
| 1533 | if (reference.getSize() != bitmap->getSize()) { |
| 1534 | return "Dimensions don't match reference"; |
| 1535 | } |
| 1536 | // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy. |
| 1537 | if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) { |
| 1538 | return "Pixels don't match reference"; |
| 1539 | } |
| 1540 | } |
| 1541 | return ""; |
| 1542 | } |
| 1543 | |
| 1544 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1545 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1546 | static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) { |
| 1547 | SkRect bounds = SkRect::MakeIWH(srcW, srcH); |
| 1548 | matrix->mapRect(&bounds); |
| 1549 | matrix->postTranslate(-bounds.x(), -bounds.y()); |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1550 | return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())}; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1553 | ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1554 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1555 | Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1556 | SkMatrix matrix = fMatrix; |
| 1557 | SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height()); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1558 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1559 | canvas->concat(matrix); |
| 1560 | return src.draw(canvas); |
| 1561 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1564 | // Undoes any flip or 90 degree rotate without changing the scale of the bitmap. |
| 1565 | // This should be pixel-preserving. |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1566 | ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1567 | |
| 1568 | Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1569 | Error err = fSink->draw(src, bitmap, stream, log); |
| 1570 | if (!err.isEmpty()) { |
| 1571 | return err; |
| 1572 | } |
| 1573 | |
| 1574 | SkMatrix inverse; |
| 1575 | if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) { |
| 1576 | return "Cannot upright --matrix."; |
| 1577 | } |
| 1578 | SkMatrix upright = SkMatrix::I(); |
| 1579 | upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX())); |
| 1580 | upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY())); |
| 1581 | upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX())); |
| 1582 | upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY())); |
| 1583 | |
| 1584 | SkBitmap uprighted; |
| 1585 | SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height()); |
| 1586 | uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height())); |
| 1587 | |
| 1588 | SkCanvas canvas(uprighted); |
| 1589 | canvas.concat(upright); |
| 1590 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1591 | paint.setBlendMode(SkBlendMode::kSrc); |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1592 | canvas.drawBitmap(*bitmap, 0, 0, &paint); |
| 1593 | |
| 1594 | *bitmap = uprighted; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1595 | return ""; |
| 1596 | } |
| 1597 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1598 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1599 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1600 | Error ViaSerialization::draw( |
| 1601 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1602 | // Record our Src into a picture. |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1603 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1604 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1605 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1606 | SkIntToScalar(size.height()))); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1607 | if (!err.isEmpty()) { |
| 1608 | return err; |
| 1609 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1610 | sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1611 | |
| 1612 | // Serialize it and then deserialize it. |
reed | 39eaf5f | 2016-09-15 07:19:35 -0700 | [diff] [blame] | 1613 | sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1614 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1615 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1616 | canvas->drawPicture(deserialized); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1617 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1618 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1622 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1623 | ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink) |
| 1624 | : Via(sink) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 1625 | , fW(w) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1626 | , fH(h) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 1627 | , fFactory(factory) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1628 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1629 | Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1630 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1631 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1632 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1633 | SkIntToScalar(size.height()), |
| 1634 | fFactory.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1635 | if (!err.isEmpty()) { |
| 1636 | return err; |
| 1637 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1638 | sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1639 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1640 | return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1641 | const int xTiles = (size.width() + fW - 1) / fW, |
| 1642 | yTiles = (size.height() + fH - 1) / fH; |
| 1643 | SkMultiPictureDraw mpd(xTiles*yTiles); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1644 | SkTArray<sk_sp<SkSurface>> surfaces; |
| 1645 | // surfaces.setReserve(xTiles*yTiles); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1646 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1647 | SkImageInfo info = canvas->imageInfo().makeWH(fW, fH); |
| 1648 | for (int j = 0; j < yTiles; j++) { |
| 1649 | for (int i = 0; i < xTiles; i++) { |
| 1650 | // This lets our ultimate Sink determine the best kind of surface. |
| 1651 | // E.g., if it's a GpuSink, the surfaces and images are textures. |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1652 | auto s = canvas->makeSurface(info); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1653 | if (!s) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1654 | s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1655 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1656 | surfaces.push_back(s); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1657 | SkCanvas* c = s->getCanvas(); |
| 1658 | c->translate(SkIntToScalar(-i * fW), |
| 1659 | SkIntToScalar(-j * fH)); // Line up the canvas with this tile. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1660 | mpd.add(c, pic.get()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1661 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1662 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1663 | mpd.draw(); |
| 1664 | for (int j = 0; j < yTiles; j++) { |
| 1665 | for (int i = 0; i < xTiles; i++) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1666 | sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot()); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1667 | canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH)); |
| 1668 | } |
| 1669 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1670 | return ""; |
| 1671 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1672 | } |
| 1673 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1674 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1675 | |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1676 | Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1677 | auto size = src.size(); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1678 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1679 | SkPictureRecorder recorder; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1680 | sk_sp<SkPicture> pic; |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1681 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1682 | SkIntToScalar(size.height()))); |
| 1683 | if (!err.isEmpty()) { |
| 1684 | return err; |
| 1685 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1686 | pic = recorder.finishRecordingAsPicture(); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1687 | canvas->drawPicture(pic); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1688 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1689 | }); |
| 1690 | } |
| 1691 | |
| 1692 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1693 | |
reed | babc3de | 2016-07-08 08:43:27 -0700 | [diff] [blame] | 1694 | Error ViaDefer::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1695 | auto size = src.size(); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1696 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
Herb Derby | 1a3c4b8 | 2017-03-17 15:51:53 -0400 | [diff] [blame] | 1697 | SkDeferredCanvas deferred(canvas, SkDeferredCanvas::kEager); |
reed | babc3de | 2016-07-08 08:43:27 -0700 | [diff] [blame] | 1698 | return src.draw(&deferred); |
| 1699 | }); |
| 1700 | } |
| 1701 | |
| 1702 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1703 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 1704 | Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1705 | auto size = src.size(); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1706 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 1707 | SkDynamicMemoryWStream tmpStream; |
| 1708 | Error err = src.draw(SkPipeSerializer().beginWrite(SkRect::Make(size), &tmpStream)); |
| 1709 | if (!err.isEmpty()) { |
| 1710 | return err; |
| 1711 | } |
| 1712 | sk_sp<SkData> data = tmpStream.detachAsData(); |
| 1713 | SkPipeDeserializer().playback(data->data(), data->size(), canvas); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1714 | return check_against_reference(bitmap, src, fSink.get()); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 1715 | }); |
| 1716 | } |
| 1717 | |
| 1718 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1719 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1720 | // Draw the Src into two pictures, then draw the second picture into the wrapped Sink. |
| 1721 | // This tests that any shortcuts we may take while recording that second picture are legal. |
| 1722 | Error ViaSecondPicture::draw( |
| 1723 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1724 | auto size = src.size(); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1725 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1726 | SkPictureRecorder recorder; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1727 | sk_sp<SkPicture> pic; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1728 | for (int i = 0; i < 2; i++) { |
| 1729 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1730 | SkIntToScalar(size.height()))); |
| 1731 | if (!err.isEmpty()) { |
| 1732 | return err; |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1733 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1734 | pic = recorder.finishRecordingAsPicture(); |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1735 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1736 | canvas->drawPicture(pic); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1737 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1738 | }); |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1741 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1742 | |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 1743 | // Draw the Src twice. This can help exercise caching. |
| 1744 | Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1745 | return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error { |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 1746 | for (int i = 0; i < 2; i++) { |
| 1747 | SkAutoCanvasRestore acr(canvas, true/*save now*/); |
| 1748 | canvas->clear(SK_ColorTRANSPARENT); |
| 1749 | Error err = src.draw(canvas); |
| 1750 | if (err.isEmpty()) { |
| 1751 | return err; |
| 1752 | } |
| 1753 | } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1754 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 1755 | }); |
| 1756 | } |
| 1757 | |
| 1758 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1759 | |
Mike Reed | bae888e | 2017-02-18 16:50:45 -0500 | [diff] [blame] | 1760 | #ifdef TEST_VIA_SVG |
Mike Reed | f67c459 | 2017-02-17 17:06:11 -0500 | [diff] [blame] | 1761 | #include "SkXMLWriter.h" |
| 1762 | #include "SkSVGCanvas.h" |
| 1763 | #include "SkSVGDOM.h" |
| 1764 | |
| 1765 | Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1766 | auto size = src.size(); |
| 1767 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
| 1768 | SkDynamicMemoryWStream wstream; |
| 1769 | SkXMLStreamWriter writer(&wstream); |
| 1770 | Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get()); |
| 1771 | if (!err.isEmpty()) { |
| 1772 | return err; |
| 1773 | } |
| 1774 | std::unique_ptr<SkStream> rstream(wstream.detachAsStream()); |
| 1775 | auto dom = SkSVGDOM::MakeFromStream(*rstream); |
| 1776 | if (dom) { |
| 1777 | dom->setContainerSize(SkSize::Make(size)); |
| 1778 | dom->render(canvas); |
| 1779 | } |
| 1780 | return ""; |
| 1781 | }); |
| 1782 | } |
Mike Reed | bae888e | 2017-02-18 16:50:45 -0500 | [diff] [blame] | 1783 | #endif |
Mike Reed | f67c459 | 2017-02-17 17:06:11 -0500 | [diff] [blame] | 1784 | |
| 1785 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1786 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1787 | // This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas. |
| 1788 | // Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op. |
| 1789 | // This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures. |
| 1790 | struct DrawsAsSingletonPictures { |
| 1791 | SkCanvas* fCanvas; |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1792 | const SkDrawableList& fDrawables; |
mtklein | 11064df | 2016-04-06 15:01:57 -0700 | [diff] [blame] | 1793 | SkRect fBounds; |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1794 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1795 | template <typename T> |
| 1796 | void draw(const T& op, SkCanvas* canvas) { |
| 1797 | // We must pass SkMatrix::I() as our initial matrix. |
| 1798 | // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix, |
| 1799 | // which would have the funky effect of applying transforms over and over. |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1800 | SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I()); |
| 1801 | d(op); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 1804 | // Draws get their own picture. |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1805 | template <typename T> |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 1806 | SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) { |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1807 | SkPictureRecorder rec; |
mtklein | 11064df | 2016-04-06 15:01:57 -0700 | [diff] [blame] | 1808 | this->draw(op, rec.beginRecording(fBounds)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1809 | sk_sp<SkPicture> pic(rec.finishRecordingAsPicture()); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1810 | fCanvas->drawPicture(pic); |
| 1811 | } |
| 1812 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 1813 | // We'll just issue non-draws directly. |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1814 | template <typename T> |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 1815 | skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) { |
| 1816 | this->draw(op, fCanvas); |
| 1817 | } |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1818 | }; |
| 1819 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1820 | // Record Src into a picture, then record it into a macro picture with a sub-picture for each draw. |
| 1821 | // Then play back that macro picture into our wrapped sink. |
| 1822 | Error ViaSingletonPictures::draw( |
| 1823 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1824 | auto size = src.size(); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1825 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1826 | // Use low-level (Skia-private) recording APIs so we can read the SkRecord. |
| 1827 | SkRecord skr; |
| 1828 | SkRecorder recorder(&skr, size.width(), size.height()); |
| 1829 | Error err = src.draw(&recorder); |
| 1830 | if (!err.isEmpty()) { |
| 1831 | return err; |
| 1832 | } |
| 1833 | |
| 1834 | // Record our macro-picture, with each draw op as its own sub-picture. |
| 1835 | SkPictureRecorder macroRec; |
| 1836 | SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()), |
| 1837 | SkIntToScalar(size.height())); |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1838 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 1839 | std::unique_ptr<SkDrawableList> drawables(recorder.detachDrawableList()); |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1840 | const SkDrawableList empty; |
| 1841 | |
| 1842 | DrawsAsSingletonPictures drawsAsSingletonPictures = { |
| 1843 | macroCanvas, |
| 1844 | drawables ? *drawables : empty, |
mtklein | 11064df | 2016-04-06 15:01:57 -0700 | [diff] [blame] | 1845 | SkRect::MakeWH((SkScalar)size.width(), (SkScalar)size.height()), |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1846 | }; |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 1847 | for (int i = 0; i < skr.count(); i++) { |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 1848 | skr.visit(i, drawsAsSingletonPictures); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1849 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1850 | sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture()); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1851 | |
| 1852 | canvas->drawPicture(macroPic); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1853 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1854 | }); |
| 1855 | } |
| 1856 | |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 1857 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1858 | |
| 1859 | Error ViaLite::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1860 | auto size = src.size(); |
Derek Sollenberger | d7875f5 | 2017-03-01 15:33:23 -0500 | [diff] [blame] | 1861 | SkIRect bounds = {0,0, size.width(), size.height()}; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1862 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
Derek Sollenberger | d7875f5 | 2017-03-01 15:33:23 -0500 | [diff] [blame] | 1863 | SkLiteDL dl; |
mtklein | 8bbbb69 | 2016-08-15 12:56:00 -0700 | [diff] [blame] | 1864 | SkLiteRecorder rec; |
Derek Sollenberger | d7875f5 | 2017-03-01 15:33:23 -0500 | [diff] [blame] | 1865 | rec.reset(&dl, bounds); |
mtklein | 8bbbb69 | 2016-08-15 12:56:00 -0700 | [diff] [blame] | 1866 | |
| 1867 | Error err = src.draw(&rec); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 1868 | if (!err.isEmpty()) { |
| 1869 | return err; |
| 1870 | } |
Derek Sollenberger | d7875f5 | 2017-03-01 15:33:23 -0500 | [diff] [blame] | 1871 | dl.draw(canvas); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1872 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 1873 | }); |
| 1874 | } |
| 1875 | |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 1876 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1877 | |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 1878 | ViaCSXform::ViaCSXform(Sink* sink, sk_sp<SkColorSpace> cs, bool colorSpin) |
| 1879 | : Via(sink) |
| 1880 | , fCS(std::move(cs)) |
| 1881 | , fColorSpin(colorSpin) {} |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 1882 | |
| 1883 | Error ViaCSXform::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1884 | return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), |
| 1885 | [&](SkCanvas* canvas) -> Error { |
| 1886 | auto proxy = SkCreateColorSpaceXformCanvas(canvas, fCS); |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 1887 | Error err = src.draw(proxy.get()); |
| 1888 | if (!err.isEmpty()) { |
| 1889 | return err; |
| 1890 | } |
| 1891 | |
| 1892 | // Undo the color spin, so we can look at the pixels in Gold. |
| 1893 | if (fColorSpin) { |
| 1894 | SkBitmap pixels; |
| 1895 | pixels.allocPixels(canvas->imageInfo()); |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 1896 | canvas->readPixels(pixels, 0, 0); |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 1897 | for (int y = 0; y < pixels.height(); y++) { |
| 1898 | for (int x = 0; x < pixels.width(); x++) { |
| 1899 | uint32_t pixel = *pixels.getAddr32(x, y); |
| 1900 | uint8_t r = SkGetPackedR32(pixel); |
| 1901 | uint8_t g = SkGetPackedG32(pixel); |
| 1902 | uint8_t b = SkGetPackedB32(pixel); |
| 1903 | uint8_t a = SkGetPackedA32(pixel); |
| 1904 | *pixels.getAddr32(x, y) = |
| 1905 | SkSwizzle_RGBA_to_PMColor(b << 0 | r << 8 | g << 16 | a << 24); |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | canvas->writePixels(pixels, 0, 0); |
| 1910 | } |
| 1911 | |
| 1912 | return ""; |
Mike Klein | 841101d | 2017-03-10 09:55:51 -0500 | [diff] [blame] | 1913 | }); |
| 1914 | } |
| 1915 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1916 | } // namespace DM |