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