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