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