blob: 32716385c089dafe6b736879d87c06ed734455fb [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "dm/DMSrcSink.h"
Tyler Denniston45f94f82020-02-04 16:09:08 -05009#include "gm/verifiers/gmverifier.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/codec/SkAndroidCodec.h"
11#include "include/codec/SkCodec.h"
12#include "include/core/SkColorSpace.h"
13#include "include/core/SkData.h"
14#include "include/core/SkDeferredDisplayListRecorder.h"
15#include "include/core/SkDocument.h"
16#include "include/core/SkExecutor.h"
17#include "include/core/SkImageGenerator.h"
18#include "include/core/SkMallocPixelRef.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkPictureRecorder.h"
20#include "include/core/SkStream.h"
21#include "include/core/SkSurface.h"
22#include "include/core/SkSurfaceCharacterization.h"
23#include "include/docs/SkPDFDocument.h"
24#include "include/gpu/GrBackendSurface.h"
Robert Phillipsb87b39b2020-07-01 14:45:24 -040025#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/ports/SkImageGeneratorCG.h"
27#include "include/ports/SkImageGeneratorWIC.h"
28#include "include/private/SkImageInfoPriv.h"
29#include "include/private/SkTLogic.h"
Brian Osmanea236bf2019-04-29 10:28:22 -040030#include "include/third_party/skcms/skcms.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "include/utils/SkNullCanvas.h"
32#include "include/utils/SkRandom.h"
Florin Malitafbddfbb2020-05-06 15:55:18 -040033#include "modules/skottie/utils/SkottieUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/codec/SkCodecImageGenerator.h"
35#include "src/codec/SkSwizzler.h"
36#include "src/core/SkAutoMalloc.h"
37#include "src/core/SkAutoPixmapStorage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/core/SkOSFile.h"
39#include "src/core/SkOpts.h"
40#include "src/core/SkPictureCommon.h"
41#include "src/core/SkPictureData.h"
42#include "src/core/SkRecordDraw.h"
43#include "src/core/SkRecorder.h"
44#include "src/core/SkTaskGroup.h"
45#include "src/gpu/GrContextPriv.h"
46#include "src/gpu/GrGpu.h"
47#include "src/utils/SkMultiPictureDocumentPriv.h"
48#include "src/utils/SkOSPath.h"
49#include "tools/DDLPromiseImageHelper.h"
50#include "tools/DDLTileHelper.h"
51#include "tools/Resources.h"
52#include "tools/debugger/DebugCanvas.h"
53#include "tools/gpu/MemoryCache.h"
msarettfc0b6d12016-03-17 13:50:17 -070054#if defined(SK_BUILD_FOR_WIN)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050055 #include "include/docs/SkXPSDocument.h"
56 #include "src/utils/win/SkAutoCoInitialize.h"
57 #include "src/utils/win/SkHRESULT.h"
58 #include "src/utils/win/SkTScopedComPtr.h"
Hal Canary5e221e72017-02-06 09:51:42 -050059 #include <XpsObjectModel.h>
msarettfc0b6d12016-03-17 13:50:17 -070060#endif
61
Florin Malita87ccf332018-05-04 12:23:24 -040062#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050063 #include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050064 #include "modules/skresources/include/SkResources.h"
Florin Malita124d5af2017-12-31 17:02:26 -050065#endif
66
Florin Malita02616ea2020-06-25 13:33:17 -040067#if defined(SK_ENABLE_SKRIVE)
68 #include "experimental/skrive/include/SkRive.h"
69#endif
70
fmalita718df0a2016-07-15 10:33:29 -070071#if defined(SK_XML)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050072 #include "experimental/svg/model/SkSVGDOM.h"
73 #include "include/svg/SkSVGCanvas.h"
74 #include "src/xml/SkXMLWriter.h"
fmalita718df0a2016-07-15 10:33:29 -070075#endif
Leon Scroggins III87caae62020-05-04 10:02:45 -040076
77#if defined(SK_ENABLE_ANDROID_UTILS)
78 #include "client_utils/android/BitmapRegionDecoder.h"
79#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050080#include "tests/TestUtils.h"
fmalita718df0a2016-07-15 10:33:29 -070081
Hal Canary8a001442018-09-19 11:31:27 -040082#include <cmath>
83#include <functional>
84
Mike Klein84836b72019-03-21 11:31:36 -050085static DEFINE_bool(multiPage, false,
86 "For document-type backends, render the source into multiple pages");
87static DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070088
Mike Klein4932a5e2019-03-22 11:08:02 -050089DECLARE_int(gpuThreads);
90
bsalomon3724e572016-03-30 18:56:19 -070091using sk_gpu_test::GrContextFactory;
Robert Phillips291f3402020-02-19 14:14:47 -050092using sk_gpu_test::ContextInfo;
bsalomon3724e572016-03-30 18:56:19 -070093
mtklein748ca3b2015-01-15 10:56:12 -080094namespace DM {
95
Hal Canary972eba32018-07-30 17:07:07 -040096GMSrc::GMSrc(skiagm::GMFactory factory) : fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -080097
Robert Phillipsb87b39b2020-07-01 14:45:24 -040098Result GMSrc::draw(GrDirectContext* context, SkCanvas* canvas) const {
Hal Canaryedda5652019-08-05 10:28:09 -040099 std::unique_ptr<skiagm::GM> gm(fFactory());
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500100 SkString msg;
Robert Phillips762cb4e2020-06-15 13:12:32 -0400101
Robert Phillipsd26d25e2020-06-25 13:26:22 -0400102 skiagm::DrawResult gpuSetupResult = gm->gpuSetup(context, canvas, &msg);
Robert Phillips889d6132020-06-16 11:11:33 -0400103 switch (gpuSetupResult) {
104 case skiagm::DrawResult::kOk : break;
105 case skiagm::DrawResult::kFail: return Result(Result::Status::Fatal, msg);
106 case skiagm::DrawResult::kSkip: return Result(Result::Status::Skip, msg);
107 default: SK_ABORT("");
108 }
109
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500110 skiagm::DrawResult drawResult = gm->draw(canvas, &msg);
111 switch (drawResult) {
112 case skiagm::DrawResult::kOk : return Result(Result::Status::Ok, msg);
113 case skiagm::DrawResult::kFail: return Result(Result::Status::Fatal, msg);
114 case skiagm::DrawResult::kSkip: return Result(Result::Status::Skip, msg);
115 default: SK_ABORT("");
Chris Dalton50e24d72019-02-07 16:20:09 -0700116 }
Robert Phillipse9229532020-06-26 10:10:49 -0400117
118 // Note: we don't call "gpuTeardown" here because, when testing DDL recording, we want
119 // the gpu-backed images to live past the lifetime of the GM.
mtklein748ca3b2015-01-15 10:56:12 -0800120}
121
122SkISize GMSrc::size() const {
Hal Canaryedda5652019-08-05 10:28:09 -0400123 std::unique_ptr<skiagm::GM> gm(fFactory());
mtklein748ca3b2015-01-15 10:56:12 -0800124 return gm->getISize();
125}
126
127Name GMSrc::name() const {
Hal Canaryedda5652019-08-05 10:28:09 -0400128 std::unique_ptr<skiagm::GM> gm(fFactory());
mtklein748ca3b2015-01-15 10:56:12 -0800129 return gm->getName();
130}
131
bsalomon4ee6bd82015-05-27 13:23:23 -0700132void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
Hal Canaryedda5652019-08-05 10:28:09 -0400133 std::unique_ptr<skiagm::GM> gm(fFactory());
bsalomon4ee6bd82015-05-27 13:23:23 -0700134 gm->modifyGrContextOptions(options);
135}
136
Tyler Denniston45f94f82020-02-04 16:09:08 -0500137std::unique_ptr<skiagm::verifiers::VerifierList> GMSrc::getVerifiers() const {
138 std::unique_ptr<skiagm::GM> gm(fFactory());
139 return gm->getVerifiers();
140}
141
mtklein748ca3b2015-01-15 10:56:12 -0800142/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
143
Leon Scroggins III87caae62020-05-04 10:02:45 -0400144static SkString get_scaled_name(const Path& path, float scale) {
145 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
146}
147
148#ifdef SK_ENABLE_ANDROID_UTILS
msarettd1227a72016-05-18 06:23:57 -0700149BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
msaretta5783ae2015-09-08 15:35:32 -0700150 : fPath(path)
msaretta5783ae2015-09-08 15:35:32 -0700151 , fMode(mode)
152 , fDstColorType(dstColorType)
153 , fSampleSize(sampleSize)
154{}
155
156bool BRDSrc::veto(SinkFlags flags) const {
157 // No need to test to non-raster or indirect backends.
158 return flags.type != SinkFlags::kRaster
159 || flags.approach != SinkFlags::kDirect;
160}
161
Leon Scroggins III87caae62020-05-04 10:02:45 -0400162static std::unique_ptr<android::skia::BitmapRegionDecoder> create_brd(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700163 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Leon Scroggins III87caae62020-05-04 10:02:45 -0400164 return android::skia::BitmapRegionDecoder::Make(encoded);
msaretta5783ae2015-09-08 15:35:32 -0700165}
166
Matt Sarett334df3a2016-12-15 18:17:33 -0500167static inline void alpha8_to_gray8(SkBitmap* bitmap) {
168 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
169 // them back to kGray8 so our test framework can draw them correctly.
170 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
171 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
172 .makeAlphaType(kOpaque_SkAlphaType);
173 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
174 }
175}
176
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400177Result BRDSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
msaretta5783ae2015-09-08 15:35:32 -0700178 SkColorType colorType = canvas->imageInfo().colorType();
179 if (kRGB_565_SkColorType == colorType &&
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500180 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType)
181 {
182 return Result::Skip("Testing non-565 to 565 is uninteresting.");
msaretta5783ae2015-09-08 15:35:32 -0700183 }
184 switch (fDstColorType) {
185 case CodecSrc::kGetFromCanvas_DstColorType:
186 break;
msaretta5783ae2015-09-08 15:35:32 -0700187 case CodecSrc::kGrayscale_Always_DstColorType:
188 colorType = kGray_8_SkColorType;
189 break;
msarett34e0ec42016-04-22 16:27:24 -0700190 default:
191 SkASSERT(false);
192 break;
msaretta5783ae2015-09-08 15:35:32 -0700193 }
194
Leon Scroggins III87caae62020-05-04 10:02:45 -0400195 auto brd = create_brd(fPath);
msaretta5783ae2015-09-08 15:35:32 -0700196 if (nullptr == brd.get()) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500197 return Result::Skip("Could not create brd for %s.", fPath.c_str());
msaretta5783ae2015-09-08 15:35:32 -0700198 }
199
Leon Scroggins III0118e972018-03-13 11:14:33 -0400200 auto recommendedCT = brd->computeOutputColorType(colorType);
201 if (kRGB_565_SkColorType == colorType && recommendedCT != colorType) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500202 return Result::Skip("Skip decoding non-opaque to 565.");
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400203 }
Leon Scroggins III0118e972018-03-13 11:14:33 -0400204 colorType = recommendedCT;
205
206 auto colorSpace = brd->computeOutputColorSpace(colorType, nullptr);
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400207
msaretta5783ae2015-09-08 15:35:32 -0700208 const uint32_t width = brd->width();
209 const uint32_t height = brd->height();
210 // Visually inspecting very small output images is not necessary.
211 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500212 return Result::Skip("Scaling very small images is uninteresting.");
msaretta5783ae2015-09-08 15:35:32 -0700213 }
214 switch (fMode) {
215 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700216 SkBitmap bitmap;
217 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
Leon Scroggins III0118e972018-03-13 11:14:33 -0400218 fSampleSize, colorType, false, colorSpace)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500219 return Result::Fatal("Cannot decode (full) region.");
msarett35e5d1b2015-10-27 12:50:25 -0700220 }
Matt Sarett334df3a2016-12-15 18:17:33 -0500221 alpha8_to_gray8(&bitmap);
Matt Sarett8db74f12017-06-14 13:02:05 +0000222
msarett35e5d1b2015-10-27 12:50:25 -0700223 canvas->drawBitmap(bitmap, 0, 0);
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500224 return Result::Ok();
msaretta5783ae2015-09-08 15:35:32 -0700225 }
226 case kDivisor_Mode: {
227 const uint32_t divisor = 2;
228 if (width < divisor || height < divisor) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500229 return Result::Skip("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700230 }
231
232 // Use a border to test subsets that extend outside the image.
233 // We will not allow the border to be larger than the image dimensions. Allowing
234 // these large borders causes off by one errors that indicate a problem with the
235 // test suite, not a problem with the implementation.
Brian Osman788b9162020-02-07 10:36:46 -0500236 const uint32_t maxBorder = std::min(width, height) / (fSampleSize * divisor);
237 const uint32_t scaledBorder = std::min(5u, maxBorder);
msaretta5783ae2015-09-08 15:35:32 -0700238 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
239
240 // We may need to clear the canvas to avoid uninitialized memory.
241 // Assume we are scaling a 780x780 image with sampleSize = 8.
242 // The output image should be 97x97.
243 // Each subset will be 390x390.
244 // Each scaled subset be 48x48.
245 // Four scaled subsets will only fill a 96x96 image.
246 // The bottom row and last column will not be touched.
247 // This is an unfortunate result of our rounding rules when scaling.
248 // Maybe we need to consider testing scaled subsets without trying to
249 // combine them to match the full scaled image? Or maybe this is the
250 // best we can do?
251 canvas->clear(0);
252
253 for (uint32_t x = 0; x < divisor; x++) {
254 for (uint32_t y = 0; y < divisor; y++) {
255 // Calculate the subset dimensions
256 uint32_t subsetWidth = width / divisor;
257 uint32_t subsetHeight = height / divisor;
258 const int left = x * subsetWidth;
259 const int top = y * subsetHeight;
260
261 // Increase the size of the last subset in each row or column, when the
262 // divisor does not divide evenly into the image dimensions
263 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
264 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
265
266 // Increase the size of the subset in order to have a border on each side
267 const int decodeLeft = left - unscaledBorder;
268 const int decodeTop = top - unscaledBorder;
269 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
270 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700271 SkBitmap bitmap;
272 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
Matt Sarett68feef42017-04-11 09:51:32 -0400273 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false,
Leon Scroggins III0118e972018-03-13 11:14:33 -0400274 colorSpace)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500275 return Result::Fatal("Cannot decode region.");
msarett35e5d1b2015-10-27 12:50:25 -0700276 }
msaretta5783ae2015-09-08 15:35:32 -0700277
Matt Sarett334df3a2016-12-15 18:17:33 -0500278 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700279 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700280 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
281 (SkScalar) (subsetWidth / fSampleSize),
282 (SkScalar) (subsetHeight / fSampleSize)),
283 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
284 (SkScalar) (top / fSampleSize),
285 (SkScalar) (subsetWidth / fSampleSize),
286 (SkScalar) (subsetHeight / fSampleSize)),
287 nullptr);
288 }
289 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500290 return Result::Ok();
msaretta5783ae2015-09-08 15:35:32 -0700291 }
292 default:
293 SkASSERT(false);
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500294 return Result::Fatal("Error: Should not be reached.");
msaretta5783ae2015-09-08 15:35:32 -0700295 }
296}
297
298SkISize BRDSrc::size() const {
Leon Scroggins III87caae62020-05-04 10:02:45 -0400299 auto brd = create_brd(fPath);
msaretta5783ae2015-09-08 15:35:32 -0700300 if (brd) {
Brian Osman788b9162020-02-07 10:36:46 -0500301 return {std::max(1, brd->width() / (int)fSampleSize),
302 std::max(1, brd->height() / (int)fSampleSize)};
msaretta5783ae2015-09-08 15:35:32 -0700303 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400304 return {0, 0};
msaretta5783ae2015-09-08 15:35:32 -0700305}
306
msaretta5783ae2015-09-08 15:35:32 -0700307Name BRDSrc::name() const {
308 // We will replicate the names used by CodecSrc so that images can
309 // be compared in Gold.
310 if (1 == fSampleSize) {
311 return SkOSPath::Basename(fPath.c_str());
312 }
msarett4b0778e2015-11-13 09:59:11 -0800313 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700314}
315
Leon Scroggins III87caae62020-05-04 10:02:45 -0400316#endif // SK_ENABLE_ANDROID_UTILS
317
msaretta5783ae2015-09-08 15:35:32 -0700318/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
319
scroggo3ac66e92016-02-08 15:09:48 -0800320static bool serial_from_path_name(const SkString& path) {
321 if (!FLAGS_RAW_threading) {
322 static const char* const exts[] = {
323 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
324 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
325 };
326 const char* actualExt = strrchr(path.c_str(), '.');
327 if (actualExt) {
328 actualExt++;
329 for (auto* ext : exts) {
330 if (0 == strcmp(ext, actualExt)) {
331 return true;
332 }
333 }
334 }
335 }
336 return false;
337}
338
scroggoc5560be2016-02-03 09:42:42 -0800339CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
340 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700341 : fPath(path)
342 , fMode(mode)
343 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800344 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700345 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800346 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700347{}
mtklein748ca3b2015-01-15 10:56:12 -0800348
mtklein99cab4e2015-07-31 06:43:04 -0700349bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800350 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800351 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700352}
scroggo9b77ddd2015-03-19 06:03:39 -0700353
msarett34e0ec42016-04-22 16:27:24 -0700354// Allows us to test decodes to non-native 8888.
msarettb1be46b2016-05-17 08:52:11 -0700355static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700356 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
357 return;
358 }
359
360 for (int y = 0; y < bitmap.height(); y++) {
361 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
362 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
363 }
364}
365
msarettb1be46b2016-05-17 08:52:11 -0700366static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
scroggoba584892016-05-20 13:56:13 -0700367 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
msarett3d9d7a72015-10-21 10:27:10 -0700368 switch (dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700369 case CodecSrc::kGrayscale_Always_DstColorType:
scroggo1a361922016-05-20 14:27:16 -0700370 if (kRGB_565_SkColorType == canvasColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700371 return false;
372 }
scroggoc5560be2016-02-03 09:42:42 -0800373 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700374 break;
msarett34e0ec42016-04-22 16:27:24 -0700375 case CodecSrc::kNonNative8888_Always_DstColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400376 if (kRGB_565_SkColorType == canvasColorType
377 || kRGBA_F16_SkColorType == canvasColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700378 return false;
379 }
380#ifdef SK_PMCOLOR_IS_RGBA
381 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
382#else
383 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
384#endif
385 break;
msarett3d9d7a72015-10-21 10:27:10 -0700386 default:
msarett55f7bdd2016-02-16 13:24:54 -0800387 if (kRGB_565_SkColorType == canvasColorType &&
388 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
389 return false;
390 }
Matt Sarett09a1c082017-02-01 15:34:22 -0800391
scroggoc5560be2016-02-03 09:42:42 -0800392 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700393 break;
394 }
395
scroggoba584892016-05-20 13:56:13 -0700396 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700397 return true;
398}
399
msarettb1be46b2016-05-17 08:52:11 -0700400static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000401 CodecSrc::DstColorType dstColorType,
msarettb1be46b2016-05-17 08:52:11 -0700402 SkScalar left = 0, SkScalar top = 0) {
msarettb1be46b2016-05-17 08:52:11 -0700403 SkBitmap bitmap;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000404 bitmap.installPixels(info, pixels, rowBytes);
msarettb1be46b2016-05-17 08:52:11 -0700405 swap_rb_if_necessary(bitmap, dstColorType);
406 canvas->drawBitmap(bitmap, left, top);
407}
408
Matt Sarettf485cf92017-05-26 08:58:03 -0400409// For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or
410// color format conversions should be performed by the codec. Sometimes the output of the
411// decode will be in an interesting color space. On our srgb and f16 backends, we need to
412// "pretend" that the color space is standard sRGB to avoid triggering color conversion
413// at draw time.
414static void set_bitmap_color_space(SkImageInfo* info) {
Brian Osman6b622962018-08-27 19:16:02 +0000415 *info = info->makeColorSpace(SkColorSpace::MakeSRGB());
Matt Sarettf485cf92017-05-26 08:58:03 -0400416}
417
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400418Result CodecSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700419 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800420 if (!encoded) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500421 return Result::Fatal("Couldn't read %s.", fPath.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800422 }
msarettb714fb02016-01-22 14:46:42 -0800423
Mike Reedede7bac2017-07-23 15:30:02 -0400424 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700425 if (nullptr == codec.get()) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500426 return Result::Fatal("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700427 }
428
scroggoba584892016-05-20 13:56:13 -0700429 SkImageInfo decodeInfo = codec->getInfo();
430 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
431 fDstAlphaType)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500432 return Result::Skip("Skipping uninteresting test.");
msarett438b2ad2015-04-09 12:43:10 -0700433 }
434
msarett0a242972015-06-11 14:27:27 -0700435 // Try to scale the image if it is desired
436 SkISize size = codec->getScaledDimensions(fScale);
437 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500438 return Result::Skip("Test without scaling is uninteresting.");
msarett0a242972015-06-11 14:27:27 -0700439 }
msarettb32758a2015-08-18 13:22:46 -0700440
441 // Visually inspecting very small output images is not necessary. We will
442 // cover these cases in unit testing.
443 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500444 return Result::Skip("Scaling very small images is uninteresting.");
msarettb32758a2015-08-18 13:22:46 -0700445 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400446 decodeInfo = decodeInfo.makeDimensions(size);
msarett0a242972015-06-11 14:27:27 -0700447
Mike Reed7fcfb622018-02-09 13:26:46 -0500448 const int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700449 const size_t rowBytes = size.width() * bpp;
Mike Reedf0ffb892017-10-03 14:47:21 -0400450 const size_t safeSize = decodeInfo.computeByteSize(rowBytes);
scroggo19b91532016-10-24 09:03:26 -0700451 SkAutoMalloc pixels(safeSize);
msarett438b2ad2015-04-09 12:43:10 -0700452
msarettbb25b532016-01-13 09:31:39 -0800453 SkCodec::Options options;
454 if (kCodecZeroInit_Mode == fMode) {
msarettb1be46b2016-05-17 08:52:11 -0700455 memset(pixels.get(), 0, size.height() * rowBytes);
msarettbb25b532016-01-13 09:31:39 -0800456 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
457 }
msarett34e0ec42016-04-22 16:27:24 -0700458
459 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400460 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700461 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
462 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
463 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
464 }
scroggo9b77ddd2015-03-19 06:03:39 -0700465
scroggo9c59ebc2015-03-25 13:48:49 -0700466 switch (fMode) {
scroggo19b91532016-10-24 09:03:26 -0700467 case kAnimated_Mode: {
468 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
469 if (frameInfos.size() <= 1) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500470 return Result::Fatal("%s is not an animated image.", fPath.c_str());
scroggo19b91532016-10-24 09:03:26 -0700471 }
472
scroggo90e971a2016-10-25 06:11:01 -0700473 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
474 // into. "factor" is the number of frames to draw on one row. There will be
475 // up to "factor" rows as well.
476 const float root = sqrt((float) frameInfos.size());
477 const int factor = sk_float_ceil2int(root);
478
scroggo19b91532016-10-24 09:03:26 -0700479 // Used to cache a frame that future frames will depend on.
480 SkAutoMalloc priorFramePixels;
Nigel Tao66bc5242018-08-22 10:56:03 +1000481 int cachedFrame = SkCodec::kNoFrame;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400482 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
scroggo19b91532016-10-24 09:03:26 -0700483 options.fFrameIndex = i;
484 // Check for a prior frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400485 const int reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +1000486 if (reqFrame != SkCodec::kNoFrame && reqFrame == cachedFrame
scroggo19b91532016-10-24 09:03:26 -0700487 && priorFramePixels.get()) {
488 // Copy into pixels
489 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400490 options.fPriorFrame = reqFrame;
scroggo19b91532016-10-24 09:03:26 -0700491 } else {
Nigel Tao66bc5242018-08-22 10:56:03 +1000492 options.fPriorFrame = SkCodec::kNoFrame;
scroggo19b91532016-10-24 09:03:26 -0700493 }
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500494 SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000495 rowBytes, &options);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500496 if (SkCodec::kInvalidInput == result && i > 0) {
497 // Some of our test images have truncated later frames. Treat that
498 // the same as incomplete.
499 result = SkCodec::kIncompleteInput;
500 }
scroggo19b91532016-10-24 09:03:26 -0700501 switch (result) {
502 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400503 case SkCodec::kErrorInInput:
scroggo90e971a2016-10-25 06:11:01 -0700504 case SkCodec::kIncompleteInput: {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400505 // If the next frame depends on this one, store it in priorFrame.
506 // It is possible that we may discard a frame that future frames depend on,
507 // but the codec will simply redecode the discarded frame.
508 // Do this before calling draw_to_canvas, which premultiplies in place. If
509 // we're decoding to unpremul, we want to pass the unmodified frame to the
510 // codec for decoding the next frame.
511 if (static_cast<size_t>(i+1) < frameInfos.size()
512 && frameInfos[i+1].fRequiredFrame == i) {
513 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
514 cachedFrame = i;
515 }
516
scroggo90e971a2016-10-25 06:11:01 -0700517 SkAutoCanvasRestore acr(canvas, true);
518 const int xTranslate = (i % factor) * decodeInfo.width();
519 const int yTranslate = (i / factor) * decodeInfo.height();
520 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
Leon Scroggins571b30f2017-07-11 17:35:31 +0000521 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
Leon Scroggins III674a1842017-07-06 12:26:09 -0400522 if (result != SkCodec::kSuccess) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500523 return Result::Ok();
scroggo19b91532016-10-24 09:03:26 -0700524 }
525 break;
scroggo90e971a2016-10-25 06:11:01 -0700526 }
scroggo53f63b62016-10-27 08:29:13 -0700527 case SkCodec::kInvalidConversion:
Mike Reed304a07c2017-07-12 15:10:28 -0400528 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500529 return Result::Skip(
530 "Cannot decode frame %i to 565 (%s).", i, fPath.c_str());
scroggo53f63b62016-10-27 08:29:13 -0700531 }
John Stiles30212b72020-06-11 17:55:07 -0400532 [[fallthrough]];
scroggo19b91532016-10-24 09:03:26 -0700533 default:
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500534 return Result::Fatal(
535 "Couldn't getPixels for frame %i in %s.", i, fPath.c_str());
scroggo19b91532016-10-24 09:03:26 -0700536 }
scroggo19b91532016-10-24 09:03:26 -0700537 }
538 break;
539 }
msarettbb25b532016-01-13 09:31:39 -0800540 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700541 case kCodec_Mode: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000542 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggoeb602a52015-07-09 08:16:03 -0700543 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400544 // We consider these to be valid, since we should still decode what is
scroggo9c59ebc2015-03-25 13:48:49 -0700545 // available.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400546 case SkCodec::kErrorInInput:
scroggoeb602a52015-07-09 08:16:03 -0700547 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700548 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700549 default:
550 // Everything else is considered a failure.
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500551 return Result::Fatal("Couldn't getPixels %s.", fPath.c_str());
scroggo9c59ebc2015-03-25 13:48:49 -0700552 }
msarettb1be46b2016-05-17 08:52:11 -0700553
Leon Scroggins571b30f2017-07-11 17:35:31 +0000554 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggo9c59ebc2015-03-25 13:48:49 -0700555 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700556 }
scroggo9c59ebc2015-03-25 13:48:49 -0700557 case kScanline_Mode: {
scroggod8d68552016-06-06 11:26:17 -0700558 void* dst = pixels.get();
559 uint32_t height = decodeInfo.height();
scroggo19b91532016-10-24 09:03:26 -0700560 const bool useIncremental = [this]() {
561 auto exts = { "png", "PNG", "gif", "GIF" };
562 for (auto ext : exts) {
563 if (fPath.endsWith(ext)) {
564 return true;
565 }
566 }
567 return false;
568 }();
569 // ico may use the old scanline method or the new one, depending on whether it
570 // internally holds a bmp or a png.
scroggo8e6c7ad2016-09-16 08:20:38 -0700571 const bool ico = fPath.endsWith("ico");
scroggo19b91532016-10-24 09:03:26 -0700572 bool useOldScanlineMethod = !useIncremental && !ico;
573 if (useIncremental || ico) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700574 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000575 rowBytes, &options)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700576 int rowsDecoded;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400577 auto result = codec->incrementalDecode(&rowsDecoded);
578 if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700579 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
580 SkCodec::kNo_ZeroInitialized, height,
581 rowsDecoded);
scroggo6fb23912016-06-02 14:16:43 -0700582 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700583 } else {
scroggo19b91532016-10-24 09:03:26 -0700584 if (useIncremental) {
585 // Error: These should support incremental decode.
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500586 return Result::Fatal("Could not start incremental decode");
scroggo8e6c7ad2016-09-16 08:20:38 -0700587 }
588 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
589 // which should work via startScanlineDecode
590 useOldScanlineMethod = true;
591 }
592 }
593
594 if (useOldScanlineMethod) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000595 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500596 return Result::Fatal("Could not start scanline decoder");
scroggo8e6c7ad2016-09-16 08:20:38 -0700597 }
598
Brian Osmanea176c62018-04-06 15:28:23 -0400599 // We do not need to check the return value. On an incomplete
600 // image, memory will be filled with a default value.
601 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700602 }
603
Leon Scroggins571b30f2017-07-11 17:35:31 +0000604 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700605 break;
606 }
msarett0a242972015-06-11 14:27:27 -0700607 case kStripe_Mode: {
608 const int height = decodeInfo.height();
609 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
610 // does not align with image blocks.
611 const int stripeHeight = 37;
612 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
msarettb1be46b2016-05-17 08:52:11 -0700613 void* dst = pixels.get();
msarett0a242972015-06-11 14:27:27 -0700614
615 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000616 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500617 return Result::Fatal("Could not start scanline decoder");
msarettb65e6042016-02-23 05:37:25 -0800618 }
619
620 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
621 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
622 // to run this test for image types that do not have this scanline ordering.
scroggo12e2f502016-05-16 09:04:18 -0700623 // We only run this on Jpeg, which is always kTopDown.
624 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
msarette6dd0042015-10-09 11:07:34 -0700625
msarett0a242972015-06-11 14:27:27 -0700626 for (int i = 0; i < numStripes; i += 2) {
627 // Skip a stripe
Brian Osman788b9162020-02-07 10:36:46 -0500628 const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700629 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700630
631 // Read a stripe
632 const int startY = (i + 1) * stripeHeight;
Brian Osman788b9162020-02-07 10:36:46 -0500633 const int linesToRead = std::min(stripeHeight, height - startY);
msarett0a242972015-06-11 14:27:27 -0700634 if (linesToRead > 0) {
msarettb1be46b2016-05-17 08:52:11 -0700635 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
636 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700637 }
638 }
639
640 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000641 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
scroggo1c005e42015-08-04 09:24:45 -0700642 if (SkCodec::kSuccess != startResult) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500643 return Result::Fatal("Failed to restart scanline decoder with same parameters.");
msarett0a242972015-06-11 14:27:27 -0700644 }
645 for (int i = 0; i < numStripes; i += 2) {
646 // Read a stripe
647 const int startY = i * stripeHeight;
Brian Osman788b9162020-02-07 10:36:46 -0500648 const int linesToRead = std::min(stripeHeight, height - startY);
msarettb1be46b2016-05-17 08:52:11 -0700649 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
650 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700651
652 // Skip a stripe
Brian Osman788b9162020-02-07 10:36:46 -0500653 const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight);
msarettf6db27e2015-06-12 09:34:04 -0700654 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700655 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700656 }
657 }
msarettb1be46b2016-05-17 08:52:11 -0700658
Leon Scroggins571b30f2017-07-11 17:35:31 +0000659 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700660 break;
msarett0a242972015-06-11 14:27:27 -0700661 }
msarett91c22b22016-02-22 12:27:46 -0800662 case kCroppedScanline_Mode: {
663 const int width = decodeInfo.width();
664 const int height = decodeInfo.height();
665 // This value is chosen because, as we move across the image, it will sometimes
666 // align with the jpeg block sizes and it will sometimes not. This allows us
667 // to test interestingly different code paths in the implementation.
668 const int tileSize = 36;
msarett91c22b22016-02-22 12:27:46 -0800669 SkIRect subset;
670 for (int x = 0; x < width; x += tileSize) {
Brian Osman788b9162020-02-07 10:36:46 -0500671 subset = SkIRect::MakeXYWH(x, 0, std::min(tileSize, width - x), height);
Matt Saretta40d9c82017-05-19 15:21:05 -0400672 options.fSubset = &subset;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000673 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500674 return Result::Fatal("Could not start scanline decoder.");
msarett91c22b22016-02-22 12:27:46 -0800675 }
676
msarettb1be46b2016-05-17 08:52:11 -0700677 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
msarett91c22b22016-02-22 12:27:46 -0800678 }
679
Leon Scroggins571b30f2017-07-11 17:35:31 +0000680 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800681 break;
682 }
scroggob636b452015-07-22 07:16:20 -0700683 case kSubset_Mode: {
684 // Arbitrarily choose a divisor.
685 int divisor = 2;
686 // Total width/height of the image.
687 const int W = codec->getInfo().width();
688 const int H = codec->getInfo().height();
689 if (divisor > W || divisor > H) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500690 return Result::Skip("Cannot codec subset: divisor %d is too big "
691 "for %s with dimensions (%d x %d)", divisor,
692 fPath.c_str(), W, H);
scroggob636b452015-07-22 07:16:20 -0700693 }
694 // subset dimensions
695 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
696 const int w = SkAlign2(W / divisor);
697 const int h = SkAlign2(H / divisor);
698 SkIRect subset;
Matt Saretta40d9c82017-05-19 15:21:05 -0400699 options.fSubset = &subset;
scroggob636b452015-07-22 07:16:20 -0700700 SkBitmap subsetBm;
701 // We will reuse pixel memory from bitmap.
msarettb1be46b2016-05-17 08:52:11 -0700702 void* dst = pixels.get();
scroggob636b452015-07-22 07:16:20 -0700703 // Keep track of left and top (for drawing subsetBm into canvas). We could use
704 // fScale * x and fScale * y, but we want integers such that the next subset will start
705 // where the last one ended. So we'll add decodeInfo.width() and height().
706 int left = 0;
707 for (int x = 0; x < W; x += w) {
708 int top = 0;
709 for (int y = 0; y < H; y+= h) {
710 // Do not make the subset go off the edge of the image.
Brian Osman788b9162020-02-07 10:36:46 -0500711 const int preScaleW = std::min(w, W - x);
712 const int preScaleH = std::min(h, H - y);
scroggob636b452015-07-22 07:16:20 -0700713 subset.setXYWH(x, y, preScaleW, preScaleH);
714 // And scale
715 // FIXME: Should we have a version of getScaledDimensions that takes a subset
716 // into account?
Brian Osman788b9162020-02-07 10:36:46 -0500717 const int scaledW = std::max(1, SkScalarRoundToInt(preScaleW * fScale));
718 const int scaledH = std::max(1, SkScalarRoundToInt(preScaleH * fScale));
msarettc7eb4902016-04-25 07:04:58 -0700719 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
720 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
msarettb1be46b2016-05-17 08:52:11 -0700721 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
722 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000723 &options);
scroggob636b452015-07-22 07:16:20 -0700724 switch (result) {
725 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400726 case SkCodec::kErrorInInput:
scroggob636b452015-07-22 07:16:20 -0700727 case SkCodec::kIncompleteInput:
728 break;
scroggob636b452015-07-22 07:16:20 -0700729 default:
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500730 return Result::Fatal("subset codec failed to decode (%d, %d, %d, %d) "
731 "from %s with dimensions (%d x %d)\t error %d",
732 x, y, decodeInfo.width(), decodeInfo.height(),
733 fPath.c_str(), W, H, result);
scroggob636b452015-07-22 07:16:20 -0700734 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000735 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType,
736 SkIntToScalar(left), SkIntToScalar(top));
msarettb1be46b2016-05-17 08:52:11 -0700737
scroggob636b452015-07-22 07:16:20 -0700738 // translate by the scaled height.
739 top += decodeInfo.height();
740 }
741 // translate by the scaled width.
742 left += decodeInfo.width();
743 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500744 return Result::Ok();
scroggob636b452015-07-22 07:16:20 -0700745 }
msarettb714fb02016-01-22 14:46:42 -0800746 default:
747 SkASSERT(false);
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500748 return Result::Fatal("Invalid fMode");
scroggo9b77ddd2015-03-19 06:03:39 -0700749 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500750 return Result::Ok();
scroggo9b77ddd2015-03-19 06:03:39 -0700751}
752
753SkISize CodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700754 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400755 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700756 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400757 return {0, 0};
scroggo7fac5af2015-09-30 11:33:12 -0700758 }
scroggo19b91532016-10-24 09:03:26 -0700759
760 auto imageSize = codec->getScaledDimensions(fScale);
761 if (fMode == kAnimated_Mode) {
scroggo90e971a2016-10-25 06:11:01 -0700762 // We'll draw one of each frame, so make it big enough to hold them all
763 // in a grid. The grid will be roughly square, with "factor" frames per
764 // row and up to "factor" rows.
scroggo19b91532016-10-24 09:03:26 -0700765 const size_t count = codec->getFrameInfo().size();
scroggo90e971a2016-10-25 06:11:01 -0700766 const float root = sqrt((float) count);
767 const int factor = sk_float_ceil2int(root);
768 imageSize.fWidth = imageSize.fWidth * factor;
769 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
scroggo19b91532016-10-24 09:03:26 -0700770 }
771 return imageSize;
scroggo9b77ddd2015-03-19 06:03:39 -0700772}
773
774Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700775 if (1.0f == fScale) {
scroggo6e8c68e2016-10-24 13:48:49 -0700776 Name name = SkOSPath::Basename(fPath.c_str());
777 if (fMode == kAnimated_Mode) {
778 name.append("_animated");
779 }
780 return name;
msarett0a242972015-06-11 14:27:27 -0700781 }
scroggo6e8c68e2016-10-24 13:48:49 -0700782 SkASSERT(fMode != kAnimated_Mode);
msaretta5783ae2015-09-08 15:35:32 -0700783 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700784}
785
786/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
787
scroggof8dc9df2016-05-16 09:04:13 -0700788AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800789 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700790 : fPath(path)
msarett3d9d7a72015-10-21 10:27:10 -0700791 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800792 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700793 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800794 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700795{}
796
797bool AndroidCodecSrc::veto(SinkFlags flags) const {
798 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700799 return flags.type != SinkFlags::kRaster
800 || flags.approach != SinkFlags::kDirect;
801}
802
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400803Result AndroidCodecSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700804 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700805 if (!encoded) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500806 return Result::Fatal("Couldn't read %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700807 }
Mike Reedede7bac2017-07-23 15:30:02 -0400808 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
809 if (nullptr == codec) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500810 return Result::Fatal("Couldn't create android codec for %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700811 }
812
scroggoba584892016-05-20 13:56:13 -0700813 SkImageInfo decodeInfo = codec->getInfo();
814 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
815 fDstAlphaType)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500816 return Result::Skip("Skipping uninteresting test.");
msarett3d9d7a72015-10-21 10:27:10 -0700817 }
818
819 // Scale the image if it is desired.
820 SkISize size = codec->getSampledDimensions(fSampleSize);
821
822 // Visually inspecting very small output images is not necessary. We will
823 // cover these cases in unit testing.
824 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500825 return Result::Skip("Scaling very small images is uninteresting.");
msarett3d9d7a72015-10-21 10:27:10 -0700826 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400827 decodeInfo = decodeInfo.makeDimensions(size);
msarett3d9d7a72015-10-21 10:27:10 -0700828
Mike Reed7fcfb622018-02-09 13:26:46 -0500829 int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700830 size_t rowBytes = size.width() * bpp;
831 SkAutoMalloc pixels(size.height() * rowBytes);
msarett3d9d7a72015-10-21 10:27:10 -0700832
833 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700834 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400835 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700836 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
837 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
838 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
839 }
msarett3d9d7a72015-10-21 10:27:10 -0700840
841 // Create options for the codec.
842 SkAndroidCodec::AndroidOptions options;
msarett3d9d7a72015-10-21 10:27:10 -0700843 options.fSampleSize = fSampleSize;
844
msarettb1be46b2016-05-17 08:52:11 -0700845 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggof8dc9df2016-05-16 09:04:13 -0700846 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400847 case SkCodec::kErrorInInput:
scroggof8dc9df2016-05-16 09:04:13 -0700848 case SkCodec::kIncompleteInput:
849 break;
msarett3d9d7a72015-10-21 10:27:10 -0700850 default:
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500851 return Result::Fatal("Couldn't getPixels %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700852 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000853 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500854 return Result::Ok();
msarett3d9d7a72015-10-21 10:27:10 -0700855}
856
857SkISize AndroidCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700858 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400859 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700860 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400861 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700862 }
863 return codec->getSampledDimensions(fSampleSize);
864}
865
866Name AndroidCodecSrc::name() const {
867 // We will replicate the names used by CodecSrc so that images can
868 // be compared in Gold.
869 if (1 == fSampleSize) {
870 return SkOSPath::Basename(fPath.c_str());
871 }
msarett4b0778e2015-11-13 09:59:11 -0800872 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700873}
874
875/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
876
msarett18976312016-03-09 14:20:58 -0800877ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
878 : fPath(path)
879 , fMode(mode)
880 , fDstAlphaType(alphaType)
881 , fIsGpu(isGpu)
882 , fRunSerially(serial_from_path_name(path))
883{}
884
885bool ImageGenSrc::veto(SinkFlags flags) const {
886 if (fIsGpu) {
Brian Salomonbd7c5512017-03-07 09:08:36 -0500887 // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs.
888 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect ||
889 flags.multisampled == SinkFlags::kMultisampled;
msarett18976312016-03-09 14:20:58 -0800890 }
891
892 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
893}
894
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400895Result ImageGenSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
msarett18976312016-03-09 14:20:58 -0800896 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500897 return Result::Skip("Uninteresting to test image generator to 565.");
msarett18976312016-03-09 14:20:58 -0800898 }
899
bungeman38d909e2016-08-02 14:40:46 -0700900 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett18976312016-03-09 14:20:58 -0800901 if (!encoded) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500902 return Result::Fatal("Couldn't read %s.", fPath.c_str());
msarett18976312016-03-09 14:20:58 -0800903 }
904
msarettfc0b6d12016-03-17 13:50:17 -0700905#if defined(SK_BUILD_FOR_WIN)
906 // Initialize COM in order to test with WIC.
907 SkAutoCoInitialize com;
908 if (!com.succeeded()) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500909 return Result::Fatal("Could not initialize COM.");
msarettfc0b6d12016-03-17 13:50:17 -0700910 }
911#endif
912
Ben Wagner145dbcd2016-11-03 14:40:50 -0400913 std::unique_ptr<SkImageGenerator> gen(nullptr);
msarett18976312016-03-09 14:20:58 -0800914 switch (fMode) {
915 case kCodec_Mode:
Mike Reed185130c2017-02-15 15:14:16 -0500916 gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded);
msarett18976312016-03-09 14:20:58 -0800917 if (!gen) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500918 return Result::Fatal("Could not create codec image generator.");
msarett18976312016-03-09 14:20:58 -0800919 }
920 break;
msarettfc0b6d12016-03-17 13:50:17 -0700921 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800922#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -0400923 gen = SkImageGeneratorCG::MakeFromEncodedCG(encoded);
msarettfc0b6d12016-03-17 13:50:17 -0700924#elif defined(SK_BUILD_FOR_WIN)
Leon Scroggins IIIa57488a2018-06-25 14:01:29 -0400925 gen = SkImageGeneratorWIC::MakeFromEncodedWIC(encoded);
msarettfc0b6d12016-03-17 13:50:17 -0700926#endif
msarett18976312016-03-09 14:20:58 -0800927 if (!gen) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500928 return Result::Fatal("Could not create platform image generator.");
msarett18976312016-03-09 14:20:58 -0800929 }
930 break;
msarettfc0b6d12016-03-17 13:50:17 -0700931 }
msarett18976312016-03-09 14:20:58 -0800932 default:
933 SkASSERT(false);
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500934 return Result::Fatal("Invalid image generator mode");
msarett18976312016-03-09 14:20:58 -0800935 }
936
937 // Test deferred decoding path on GPU
938 if (fIsGpu) {
Mike Reed185130c2017-02-15 15:14:16 -0500939 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr));
msarett18976312016-03-09 14:20:58 -0800940 if (!image) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500941 return Result::Fatal("Could not create image from codec image generator.");
msarett18976312016-03-09 14:20:58 -0800942 }
943 canvas->drawImage(image, 0, 0);
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500944 return Result::Ok();
msarett18976312016-03-09 14:20:58 -0800945 }
mtklein343a63d2016-03-22 11:46:53 -0700946
msarett18976312016-03-09 14:20:58 -0800947 // Test various color and alpha types on CPU
948 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700949
Mike Reed7fcfb622018-02-09 13:26:46 -0500950 int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700951 size_t rowBytes = decodeInfo.width() * bpp;
952 SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
Brian Osmanc87cfb62018-07-11 09:08:46 -0400953 if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500954 Result::Status status = Result::Status::Fatal;
Matt Sarett05cb4c32017-03-02 12:07:46 -0500955#if defined(SK_BUILD_FOR_WIN)
956 if (kPlatform_Mode == fMode) {
957 // Do not issue a fatal error for WIC flakiness.
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500958 status = Result::Status::Skip;
Matt Sarett05cb4c32017-03-02 12:07:46 -0500959 }
960#endif
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500961 return Result(status, "Image generator could not getPixels() for %s\n", fPath.c_str());
msarett18976312016-03-09 14:20:58 -0800962 }
963
Matt Sarettf485cf92017-05-26 08:58:03 -0400964 set_bitmap_color_space(&decodeInfo);
Leon Scroggins571b30f2017-07-11 17:35:31 +0000965 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes,
msarettb1be46b2016-05-17 08:52:11 -0700966 CodecSrc::kGetFromCanvas_DstColorType);
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500967 return Result::Ok();
msarett18976312016-03-09 14:20:58 -0800968}
969
970SkISize ImageGenSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700971 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400972 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett18976312016-03-09 14:20:58 -0800973 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400974 return {0, 0};
msarett18976312016-03-09 14:20:58 -0800975 }
976 return codec->getInfo().dimensions();
977}
978
979Name ImageGenSrc::name() const {
980 return SkOSPath::Basename(fPath.c_str());
981}
982
983/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
984
Mike Klein0d5d1422019-03-06 10:56:04 -0600985ColorCodecSrc::ColorCodecSrc(Path path, bool decode_to_dst) : fPath(path)
986 , fDecodeToDst(decode_to_dst) {}
msarett69deca82016-04-29 09:38:40 -0700987
988bool ColorCodecSrc::veto(SinkFlags flags) const {
989 // Test to direct raster backends (8888 and 565).
990 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
991}
992
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400993Result ColorCodecSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700994 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett69deca82016-04-29 09:38:40 -0700995 if (!encoded) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500996 return Result::Fatal("Couldn't read %s.", fPath.c_str());
msarett69deca82016-04-29 09:38:40 -0700997 }
998
Mike Reedede7bac2017-07-23 15:30:02 -0400999 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
1000 if (nullptr == codec) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001001 return Result::Fatal("Couldn't create codec for %s.", fPath.c_str());
msarett69deca82016-04-29 09:38:40 -07001002 }
1003
Mike Klein0d5d1422019-03-06 10:56:04 -06001004 SkImageInfo info = codec->getInfo();
1005 if (fDecodeToDst) {
Leon Scroggins III42a604f2020-02-06 15:47:58 -05001006 SkImageInfo canvasInfo = canvas->imageInfo();
1007 if (!canvasInfo.colorSpace()) {
1008 // This will skip color conversion, and the resulting images will
1009 // look different from images they are compared against in Gold, but
1010 // that doesn't mean they are wrong. We have a test verifying that
1011 // passing a null SkColorSpace skips conversion, so skip this
1012 // misleading test.
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001013 return Result::Skip("Skipping decoding without color transform.");
Leon Scroggins III42a604f2020-02-06 15:47:58 -05001014 }
1015 info = canvasInfo.makeDimensions(info.dimensions());
msarett50ce1f22016-07-29 06:23:33 -07001016 }
1017
1018 SkBitmap bitmap;
Mike Klein0d5d1422019-03-06 10:56:04 -06001019 if (!bitmap.tryAllocPixels(info)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001020 return Result::Fatal("Image(%s) is too large (%d x %d)",
1021 fPath.c_str(), info.width(), info.height());
msarett50ce1f22016-07-29 06:23:33 -07001022 }
1023
Mike Klein0d5d1422019-03-06 10:56:04 -06001024 switch (auto r = codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes())) {
Leon Scroggins III674a1842017-07-06 12:26:09 -04001025 case SkCodec::kSuccess:
1026 case SkCodec::kErrorInInput:
1027 case SkCodec::kIncompleteInput:
Mike Klein0d5d1422019-03-06 10:56:04 -06001028 canvas->drawBitmap(bitmap, 0,0);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001029 return Result::Ok();
Mike Klein0d5d1422019-03-06 10:56:04 -06001030 case SkCodec::kInvalidConversion:
1031 // TODO(mtklein): why are there formats we can't decode to?
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001032 return Result::Skip("SkCodec can't decode to this format.");
Leon Scroggins III674a1842017-07-06 12:26:09 -04001033 default:
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001034 return Result::Fatal("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
msarett50ce1f22016-07-29 06:23:33 -07001035 }
msarett69deca82016-04-29 09:38:40 -07001036}
1037
1038SkISize ColorCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001039 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001040 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001041 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001042 return {0, 0};
msarett69deca82016-04-29 09:38:40 -07001043 }
Hal Canaryfafe1352017-04-11 12:12:02 -04001044 return {codec->getInfo().width(), codec->getInfo().height()};
msarett69deca82016-04-29 09:38:40 -07001045}
1046
1047Name ColorCodecSrc::name() const {
1048 return SkOSPath::Basename(fPath.c_str());
1049}
1050
1051/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1052
Mike Klein19fb3972019-03-21 13:08:08 -05001053static DEFINE_int(skpViewportSize, 1000,
1054 "Width & height of the viewport used to crop skp rendering.");
1055
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001056SKPSrc::SKPSrc(Path path) : fPath(path) { }
mtklein748ca3b2015-01-15 10:56:12 -08001057
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001058Result SKPSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
Hal Canary72b91cb2019-01-14 12:47:59 -05001059 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
1060 if (!stream) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001061 return Result::Fatal("Couldn't read %s.", fPath.c_str());
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001062 }
Hal Canary72b91cb2019-01-14 12:47:59 -05001063 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
1064 if (!pic) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001065 return Result::Fatal("Couldn't parse file %s.", fPath.c_str());
Hal Canary72b91cb2019-01-14 12:47:59 -05001066 }
1067 stream = nullptr; // Might as well drop this when we're done with it.
Robert Phillips33f02ed2018-03-27 08:06:57 -04001068 canvas->clipRect(SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize));
mtklein748ca3b2015-01-15 10:56:12 -08001069 canvas->drawPicture(pic);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001070 return Result::Ok();
mtklein748ca3b2015-01-15 10:56:12 -08001071}
1072
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001073static SkRect get_cull_rect_for_skp(const char* path) {
1074 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtkleinffa901a2015-03-16 10:38:07 -07001075 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001076 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001077 }
1078 SkPictInfo info;
Mike Reede7a58322017-12-20 14:09:20 -05001079 if (!SkPicture_StreamIsSKP(stream.get(), &info)) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001080 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001081 }
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001082
1083 return info.fCullRect;
1084}
1085
1086SkISize SKPSrc::size() const {
1087 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
Robert Phillips33f02ed2018-03-27 08:06:57 -04001088 if (!viewport.intersect((SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize)))) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001089 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001090 }
1091 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001092}
1093
1094Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1095
Florin Malitafc043dc2017-12-31 11:08:42 -05001096/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1097
Chris Dalton184c37e2018-09-28 11:27:39 -06001098BisectSrc::BisectSrc(Path path, const char* trail) : INHERITED(path), fTrail(trail) {}
1099
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001100Result BisectSrc::draw(GrDirectContext* context, SkCanvas* canvas) const {
Chris Dalton184c37e2018-09-28 11:27:39 -06001101 struct FoundPath {
1102 SkPath fPath;
1103 SkPaint fPaint;
1104 SkMatrix fViewMatrix;
1105 };
1106
1107 // This subclass of SkCanvas just extracts all the SkPaths (drawn via drawPath) from an SKP.
1108 class PathFindingCanvas : public SkCanvas {
1109 public:
1110 PathFindingCanvas(int width, int height) : SkCanvas(width, height, nullptr) {}
1111 const SkTArray<FoundPath>& foundPaths() const { return fFoundPaths; }
1112
1113 private:
1114 void onDrawPath(const SkPath& path, const SkPaint& paint) override {
1115 fFoundPaths.push_back() = {path, paint, this->getTotalMatrix()};
1116 }
1117
1118 SkTArray<FoundPath> fFoundPaths;
1119 };
1120
1121 PathFindingCanvas pathFinder(canvas->getBaseLayerSize().width(),
1122 canvas->getBaseLayerSize().height());
Robert Phillips889d6132020-06-16 11:11:33 -04001123 Result result = this->INHERITED::draw(context, &pathFinder);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001124 if (!result.isOk()) {
1125 return result;
Chris Dalton184c37e2018-09-28 11:27:39 -06001126 }
1127
1128 int start = 0, end = pathFinder.foundPaths().count();
1129 for (const char* ch = fTrail.c_str(); *ch; ++ch) {
1130 int midpt = (start + end) / 2;
1131 if ('l' == *ch) {
1132 start = midpt;
1133 } else if ('r' == *ch) {
1134 end = midpt;
1135 }
1136 }
1137
1138 for (int i = start; i < end; ++i) {
1139 const FoundPath& path = pathFinder.foundPaths()[i];
1140 SkAutoCanvasRestore acr(canvas, true);
1141 canvas->concat(path.fViewMatrix);
1142 canvas->drawPath(path.fPath, path.fPaint);
1143 }
1144
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001145 return Result::Ok();
Chris Dalton184c37e2018-09-28 11:27:39 -06001146}
1147
1148/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1149
Florin Malita87ccf332018-05-04 12:23:24 -04001150#if defined(SK_ENABLE_SKOTTIE)
Florin Malita67ff5412020-05-20 17:04:21 -04001151static DEFINE_bool(useLottieGlyphPaths, false,
1152 "Prioritize embedded glyph paths over native fonts.");
1153
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001154SkottieSrc::SkottieSrc(Path path) : fPath(std::move(path)) {}
Florin Malitafc043dc2017-12-31 11:08:42 -05001155
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001156Result SkottieSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
Florin Malitafbddfbb2020-05-06 15:55:18 -04001157 auto resource_provider =
1158 skresources::DataURIResourceProviderProxy::Make(
1159 skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
1160 /*predecode=*/true),
1161 /*predecode=*/true);
1162
1163 static constexpr char kInterceptPrefix[] = "__";
1164 auto precomp_interceptor =
1165 sk_make_sp<skottie_utils::ExternalAnimationPrecompInterceptor>(resource_provider,
1166 kInterceptPrefix);
Florin Malita67ff5412020-05-20 17:04:21 -04001167 uint32_t flags = 0;
1168 if (FLAGS_useLottieGlyphPaths) {
1169 flags |= skottie::Animation::Builder::kPreferEmbeddedFonts;
1170 }
1171
1172 auto animation = skottie::Animation::Builder(flags)
Florin Malitafbddfbb2020-05-06 15:55:18 -04001173 .setResourceProvider(std::move(resource_provider))
1174 .setPrecompInterceptor(std::move(precomp_interceptor))
Florin Malitaa8316552018-11-09 16:19:44 -05001175 .makeFromFile(fPath.c_str());
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001176 if (!animation) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001177 return Result::Fatal("Unable to parse file: %s", fPath.c_str());
Florin Malitafc043dc2017-12-31 11:08:42 -05001178 }
1179
1180 canvas->drawColor(SK_ColorWHITE);
1181
Florin Malitaa33447d2018-05-29 13:46:54 -04001182 const auto t_rate = 1.0f / (kTileCount * kTileCount - 1);
Florin Malitafc043dc2017-12-31 11:08:42 -05001183
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001184 // Draw the frames in a shuffled order to exercise non-linear
1185 // frame progression. The film strip will still be in order left-to-right,
1186 // top-down, just not drawn in that order.
1187 static constexpr int frameOrder[] = { 4, 0, 3, 1, 2 };
1188 static_assert(SK_ARRAY_COUNT(frameOrder) == kTileCount, "");
Florin Malitad3c1b842018-01-27 12:43:24 -05001189
Florin Malitafc043dc2017-12-31 11:08:42 -05001190 for (int i = 0; i < kTileCount; ++i) {
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001191 const SkScalar y = frameOrder[i] * kTileSize;
Florin Malitafc043dc2017-12-31 11:08:42 -05001192
1193 for (int j = 0; j < kTileCount; ++j) {
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001194 const SkScalar x = frameOrder[j] * kTileSize;
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001195 SkRect dest = SkRect::MakeXYWH(x, y, kTileSize, kTileSize);
Florin Malitafc043dc2017-12-31 11:08:42 -05001196
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001197 const auto t = t_rate * (frameOrder[i] * kTileCount + frameOrder[j]);
Florin Malitadf2713c2018-01-09 15:51:21 -05001198 {
1199 SkAutoCanvasRestore acr(canvas, true);
Florin Malitad3c1b842018-01-27 12:43:24 -05001200 canvas->clipRect(dest, true);
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001201 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
Florin Malitadf2713c2018-01-09 15:51:21 -05001202 dest,
Florin Malita3fae0f32018-03-15 21:52:54 -04001203 SkMatrix::kCenter_ScaleToFit));
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001204 animation->seek(t);
1205 animation->render(canvas);
Florin Malitadf2713c2018-01-09 15:51:21 -05001206 }
Florin Malitafc043dc2017-12-31 11:08:42 -05001207 }
1208 }
1209
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001210 return Result::Ok();
Florin Malitafc043dc2017-12-31 11:08:42 -05001211}
1212
Florin Malita54f65c42018-01-16 17:04:30 -05001213SkISize SkottieSrc::size() const {
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001214 return SkISize::Make(kTargetSize, kTargetSize);
Florin Malitafc043dc2017-12-31 11:08:42 -05001215}
1216
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001217Name SkottieSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
Florin Malitafc043dc2017-12-31 11:08:42 -05001218
Florin Malita54f65c42018-01-16 17:04:30 -05001219bool SkottieSrc::veto(SinkFlags flags) const {
Florin Malitafc043dc2017-12-31 11:08:42 -05001220 // No need to test to non-(raster||gpu||vector) or indirect backends.
1221 bool type_ok = flags.type == SinkFlags::kRaster
1222 || flags.type == SinkFlags::kGPU
1223 || flags.type == SinkFlags::kVector;
1224
1225 return !type_ok || flags.approach != SinkFlags::kDirect;
1226}
Florin Malita124d5af2017-12-31 17:02:26 -05001227#endif
Florin Malitafc043dc2017-12-31 11:08:42 -05001228
1229/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
Florin Malita02616ea2020-06-25 13:33:17 -04001230#if defined(SK_ENABLE_SKRIVE)
1231SkRiveSrc::SkRiveSrc(Path path) : fPath(std::move(path)) {}
1232
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001233Result SkRiveSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
John Stilesfb355542020-06-26 13:55:28 -04001234 auto fileStream = SkFILEStream::Make(fPath.c_str());
1235 if (!fileStream) {
1236 return Result::Fatal("Unable to open file: %s", fPath.c_str());
1237 }
1238
1239 const auto skrive = skrive::SkRive::Builder().make(std::move(fileStream));
Florin Malita02616ea2020-06-25 13:33:17 -04001240 if (!skrive) {
1241 return Result::Fatal("Unable to parse file: %s", fPath.c_str());
1242 }
1243
1244 auto bounds = SkRect::MakeEmpty();
1245
1246 for (const auto& ab : skrive->artboards()) {
1247 const auto& pos = ab->getTranslation();
1248 const auto& size = ab->getSize();
1249
1250 bounds.join(SkRect::MakeXYWH(pos.x, pos.y, size.x, size.y));
1251 }
1252
1253 canvas->drawColor(SK_ColorWHITE);
1254
1255 if (!bounds.isEmpty()) {
1256 // TODO: tiled frames when we add animation support
1257 SkAutoCanvasRestore acr(canvas, true);
1258 canvas->concat(SkMatrix::MakeRectToRect(bounds,
1259 SkRect::MakeWH(kTargetSize, kTargetSize),
1260 SkMatrix::kCenter_ScaleToFit ));
1261 for (const auto& ab : skrive->artboards()) {
1262 ab->render(canvas);
1263 }
1264 }
1265
1266 return Result::Ok();
1267}
1268
1269SkISize SkRiveSrc::size() const {
1270 return SkISize::Make(kTargetSize, kTargetSize);
1271}
1272
1273Name SkRiveSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1274
1275bool SkRiveSrc::veto(SinkFlags flags) const {
1276 // No need to test to non-(raster||gpu||vector) or indirect backends.
1277 bool type_ok = flags.type == SinkFlags::kRaster
1278 || flags.type == SinkFlags::kGPU
1279 || flags.type == SinkFlags::kVector;
1280
1281 return !type_ok || flags.approach != SinkFlags::kDirect;
1282}
1283#endif
1284
1285/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fmalitaa2b9fdf2016-08-03 19:53:36 -07001286#if defined(SK_XML)
fmalitabdf3e5c2016-09-17 07:26:26 -07001287// Used when the image doesn't have an intrinsic size.
Hal Canaryfafe1352017-04-11 12:12:02 -04001288static const SkSize kDefaultSVGSize = {1000, 1000};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001289
fmalitabdf3e5c2016-09-17 07:26:26 -07001290// Used to force-scale tiny fixed-size images.
Hal Canaryfafe1352017-04-11 12:12:02 -04001291static const SkSize kMinimumSVGSize = {128, 128};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001292
fmalitaacd2f5c2016-11-08 07:13:45 -08001293SVGSrc::SVGSrc(Path path)
1294 : fName(SkOSPath::Basename(path.c_str()))
1295 , fScale(1) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001296
Brian Osman133823d2018-09-19 14:14:15 -04001297 sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
1298 if (!data) {
1299 return;
1300 }
fmalitaacd2f5c2016-11-08 07:13:45 -08001301
Brian Osman133823d2018-09-19 14:14:15 -04001302 SkMemoryStream stream(std::move(data));
1303 fDom = SkSVGDOM::MakeFromStream(stream);
1304 if (!fDom) {
1305 return;
1306 }
1307
1308 const SkSize& sz = fDom->containerSize();
1309 if (sz.isEmpty()) {
1310 // no intrinsic size
1311 fDom->setContainerSize(kDefaultSVGSize);
1312 } else {
Brian Osman788b9162020-02-07 10:36:46 -05001313 fScale = std::max(1.f, std::max(kMinimumSVGSize.width() / sz.width(),
Brian Osman133823d2018-09-19 14:14:15 -04001314 kMinimumSVGSize.height() / sz.height()));
1315 }
fmalitaacd2f5c2016-11-08 07:13:45 -08001316}
1317
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001318Result SVGSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
fmalitabdf3e5c2016-09-17 07:26:26 -07001319 if (!fDom) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001320 return Result::Fatal("Unable to parse file: %s", fName.c_str());
fmalitaa2b9fdf2016-08-03 19:53:36 -07001321 }
1322
fmalitaacd2f5c2016-11-08 07:13:45 -08001323 SkAutoCanvasRestore acr(canvas, true);
1324 canvas->scale(fScale, fScale);
1325 fDom->render(canvas);
1326
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001327 return Result::Ok();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001328}
1329
1330SkISize SVGSrc::size() const {
fmalitaacd2f5c2016-11-08 07:13:45 -08001331 if (!fDom) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001332 return {0, 0};
fmalitabdf3e5c2016-09-17 07:26:26 -07001333 }
1334
Hal Canaryfafe1352017-04-11 12:12:02 -04001335 return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale}
1336 .toRound();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001337}
1338
fmalitaacd2f5c2016-11-08 07:13:45 -08001339Name SVGSrc::name() const { return fName; }
fmalitaa2b9fdf2016-08-03 19:53:36 -07001340
fmalita179d8852016-08-16 14:23:29 -07001341bool SVGSrc::veto(SinkFlags flags) const {
Florin Malita93323eb2017-06-16 15:31:17 -04001342 // No need to test to non-(raster||gpu||vector) or indirect backends.
fmalita179d8852016-08-16 14:23:29 -07001343 bool type_ok = flags.type == SinkFlags::kRaster
Florin Malita93323eb2017-06-16 15:31:17 -04001344 || flags.type == SinkFlags::kGPU
1345 || flags.type == SinkFlags::kVector;
fmalita179d8852016-08-16 14:23:29 -07001346
1347 return !type_ok || flags.approach != SinkFlags::kDirect;
1348}
1349
fmalitaa2b9fdf2016-08-03 19:53:36 -07001350#endif // defined(SK_XML)
1351/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein748ca3b2015-01-15 10:56:12 -08001352
halcanary45420a92016-06-02 12:41:14 -07001353MSKPSrc::MSKPSrc(Path path) : fPath(path) {
bungemanf93d7112016-09-16 06:24:20 -07001354 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001355 int count = SkMultiPictureDocumentReadPageCount(stream.get());
1356 if (count > 0) {
1357 fPages.reset(count);
1358 (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count());
1359 }
halcanary45420a92016-06-02 12:41:14 -07001360}
1361
Hal Canary45cde312017-04-03 16:06:42 -04001362int MSKPSrc::pageCount() const { return fPages.count(); }
halcanary45420a92016-06-02 12:41:14 -07001363
1364SkISize MSKPSrc::size() const { return this->size(0); }
Hal Canary45cde312017-04-03 16:06:42 -04001365SkISize MSKPSrc::size(int i) const {
Hal Canaryfafe1352017-04-11 12:12:02 -04001366 return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0};
Hal Canary45cde312017-04-03 16:06:42 -04001367}
halcanary45420a92016-06-02 12:41:14 -07001368
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001369Result MSKPSrc::draw(GrDirectContext* context, SkCanvas* c) const {
Robert Phillips889d6132020-06-16 11:11:33 -04001370 return this->draw(0, context, c);
Robert Phillips762cb4e2020-06-15 13:12:32 -04001371}
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001372Result MSKPSrc::draw(int i, GrDirectContext*, SkCanvas* canvas) const {
Hal Canary45cde312017-04-03 16:06:42 -04001373 if (this->pageCount() == 0) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001374 return Result::Fatal("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
halcanary45420a92016-06-02 12:41:14 -07001375 }
Hal Canary45cde312017-04-03 16:06:42 -04001376 if (i >= fPages.count() || i < 0) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001377 return Result::Fatal("MultiPictureDocument page number out of range: %d", i);
halcanary45420a92016-06-02 12:41:14 -07001378 }
Hal Canary45cde312017-04-03 16:06:42 -04001379 SkPicture* page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001380 if (!page) {
Hal Canary45cde312017-04-03 16:06:42 -04001381 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1382 if (!stream) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001383 return Result::Fatal("Unable to open file: %s", fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001384 }
1385 if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001386 return Result::Fatal("SkMultiPictureDocument reader failed on page %d: %s", i,
1387 fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001388 }
1389 page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001390 }
1391 canvas->drawPicture(page);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001392 return Result::Ok();
halcanary45420a92016-06-02 12:41:14 -07001393}
1394
1395Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1396
1397/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1398
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001399Result NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
Robert Phillips889d6132020-06-16 11:11:33 -04001400 return src.draw(nullptr, SkMakeNullCanvas().get());
mtkleinad66f9b2015-02-13 15:11:10 -08001401}
1402
1403/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1404
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001405static Result compare_bitmaps(const SkBitmap& reference, const SkBitmap& bitmap) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001406 // The dimensions are a property of the Src only, and so should be identical.
Mike Reedf0ffb892017-10-03 14:47:21 -04001407 SkASSERT(reference.computeByteSize() == bitmap.computeByteSize());
1408 if (reference.computeByteSize() != bitmap.computeByteSize()) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001409 return Result::Fatal("Dimensions don't match reference");
Brian Osmane5756ec2017-09-06 17:08:30 -04001410 }
1411 // All SkBitmaps in DM are tight, so this comparison is easy.
Mike Reedf0ffb892017-10-03 14:47:21 -04001412 if (0 != memcmp(reference.getPixels(), bitmap.getPixels(), reference.computeByteSize())) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001413 SkString encoded;
1414 SkString errString("Pixels don't match reference");
Brian Salomon28a8f282019-10-24 20:07:39 -04001415 if (BipmapToBase64DataURI(reference, &encoded)) {
Michael Ludwige8e10752018-10-01 12:42:53 -04001416 errString.append("\nExpected: ");
Brian Osmane5756ec2017-09-06 17:08:30 -04001417 errString.append(encoded);
1418 } else {
1419 errString.append("\nExpected image failed to encode: ");
1420 errString.append(encoded);
1421 }
Brian Salomon28a8f282019-10-24 20:07:39 -04001422 if (BipmapToBase64DataURI(bitmap, &encoded)) {
Michael Ludwige8e10752018-10-01 12:42:53 -04001423 errString.append("\nActual: ");
Brian Osmane5756ec2017-09-06 17:08:30 -04001424 errString.append(encoded);
1425 } else {
1426 errString.append("\nActual image failed to encode: ");
1427 errString.append(encoded);
1428 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001429 return Result::Fatal(errString);
Brian Osmane5756ec2017-09-06 17:08:30 -04001430 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001431 return Result::Ok();
Brian Osmane5756ec2017-09-06 17:08:30 -04001432}
1433
1434/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1435
Mike Klein84836b72019-03-21 11:31:36 -05001436static DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
Mike Klein19fb3972019-03-21 13:08:08 -05001437static DEFINE_bool(preAbandonGpuContext, false,
1438 "Test abandoning the GrContext before running the test.");
1439static DEFINE_bool(abandonGpuContext, false,
1440 "Test abandoning the GrContext after running each test.");
1441static DEFINE_bool(releaseAndAbandonGpuContext, false,
1442 "Test releasing all gpu resources and abandoning the GrContext "
1443 "after running each test");
1444static DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
Brian Osman9c310472019-06-27 16:43:27 -04001445static DEFINE_bool(programBinaryCache, true, "Use in-memory program binary cache");
mtkleinb9eb4ac2015-02-02 18:26:03 -08001446
Brian Osman3fdfe282019-09-09 13:46:52 -04001447GPUSink::GPUSink(const SkCommandLineConfigGpu* config,
Brian Osmanf21aa042017-08-21 16:48:46 -04001448 const GrContextOptions& grCtxOptions)
Brian Osman3fdfe282019-09-09 13:46:52 -04001449 : fContextType(config->getContextType())
1450 , fContextOverrides(config->getContextOverrides())
1451 , fSurfType(config->getSurfType())
1452 , fSampleCount(config->getSamples())
1453 , fUseDIText(config->getUseDIText())
1454 , fColorType(config->getColorType())
1455 , fAlphaType(config->getAlphaType())
1456 , fColorSpace(sk_ref_sp(config->getColorSpace()))
Brian Osman9c310472019-06-27 16:43:27 -04001457 , fBaseContextOptions(grCtxOptions) {
1458 if (FLAGS_programBinaryCache) {
1459 fBaseContextOptions.fPersistentCache = &fMemoryCache;
1460 }
1461}
mtklein748ca3b2015-01-15 10:56:12 -08001462
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001463Result GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const {
Brian Osmanf9810662017-08-30 10:02:10 -04001464 return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
1465}
1466
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001467sk_sp<SkSurface> GPUSink::createDstSurface(GrDirectContext* context, SkISize size,
Robert Phillips3c1efd42020-02-13 15:51:59 -05001468 GrBackendTexture* backendTexture,
1469 GrBackendRenderTarget* backendRT) const {
1470 sk_sp<SkSurface> surface;
1471
1472 SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
1473 uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
1474 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1475
1476 switch (fSurfType) {
1477 case SkCommandLineConfigGpu::SurfType::kDefault:
1478 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
1479 &props);
1480 break;
1481 case SkCommandLineConfigGpu::SurfType::kBackendTexture:
Greg Danielc1ad77c2020-05-06 11:40:03 -04001482 CreateBackendTexture(context, backendTexture, info.width(), info.height(),
Brian Salomon7e67dca2020-07-21 09:27:25 -04001483 info.colorType(), SkColors::kTransparent, GrMipmapped::kNo,
Greg Danielc1ad77c2020-05-06 11:40:03 -04001484 GrRenderable::kYes, GrProtected::kNo);
Robert Phillips3c1efd42020-02-13 15:51:59 -05001485 surface = SkSurface::MakeFromBackendTexture(context, *backendTexture,
1486 kTopLeft_GrSurfaceOrigin, fSampleCount,
1487 fColorType, info.refColorSpace(), &props);
1488 break;
1489 case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
1490 if (1 == fSampleCount) {
1491 auto colorType = SkColorTypeToGrColorType(info.colorType());
1492 *backendRT = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
1493 info.width(), info.height(), colorType);
1494 surface = SkSurface::MakeFromBackendRenderTarget(
1495 context, *backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(),
1496 info.refColorSpace(), &props);
1497 }
1498 break;
1499 }
1500
1501 return surface;
1502}
1503
1504bool GPUSink::readBack(SkSurface* surface, SkBitmap* dst) const {
1505 SkCanvas* canvas = surface->getCanvas();
1506 SkISize size = surface->imageInfo().dimensions();
1507
1508 SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
Robert Phillips3c1efd42020-02-13 15:51:59 -05001509 dst->allocPixels(info);
1510 return canvas->readPixels(*dst, 0, 0);
1511}
1512
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001513Result GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
1514 const GrContextOptions& baseOptions,
1515 std::function<void(GrContext*)> initContext) const {
Brian Osmanf9810662017-08-30 10:02:10 -04001516 GrContextOptions grOptions = baseOptions;
kkinnunen64492c42015-12-08 01:24:40 -08001517
Brian Salomon00a5eb82018-07-11 15:32:05 -04001518 // We don't expect the src to mess with the persistent cache or the executor.
1519 SkDEBUGCODE(auto cache = grOptions.fPersistentCache);
1520 SkDEBUGCODE(auto exec = grOptions.fExecutor);
kkinnunen5219fd92015-12-10 06:28:13 -08001521 src.modifyGrContextOptions(&grOptions);
Brian Salomon00a5eb82018-07-11 15:32:05 -04001522 SkASSERT(cache == grOptions.fPersistentCache);
1523 SkASSERT(exec == grOptions.fExecutor);
kkinnunen5219fd92015-12-10 06:28:13 -08001524
1525 GrContextFactory factory(grOptions);
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001526 auto direct = factory.getContextInfo(fContextType, fContextOverrides).directContext();
Brian Osmaned58e002019-09-06 14:42:43 -04001527 if (initContext) {
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001528 initContext(direct);
Brian Osmaned58e002019-09-06 14:42:43 -04001529 }
Robert Phillips3c1efd42020-02-13 15:51:59 -05001530
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001531 const int maxDimension = direct->priv().caps()->maxTextureSize();
Robert Phillips3c1efd42020-02-13 15:51:59 -05001532 if (maxDimension < std::max(src.size().width(), src.size().height())) {
Robert Phillips45f37c32020-02-13 20:26:58 +00001533 return Result::Skip("Src too large to create a texture.\n");
1534 }
Robert Phillips3c1efd42020-02-13 15:51:59 -05001535
Brian Salomonf865b052018-03-09 09:01:53 -05001536 GrBackendTexture backendTexture;
1537 GrBackendRenderTarget backendRT;
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001538 sk_sp<SkSurface> surface = this->createDstSurface(direct, src.size(),
Robert Phillips3c1efd42020-02-13 15:51:59 -05001539 &backendTexture, &backendRT);
mtklein748ca3b2015-01-15 10:56:12 -08001540 if (!surface) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001541 return Result::Fatal("Could not create a surface.");
mtklein748ca3b2015-01-15 10:56:12 -08001542 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001543 if (FLAGS_preAbandonGpuContext) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001544 factory.abandonContexts();
1545 }
mtklein748ca3b2015-01-15 10:56:12 -08001546 SkCanvas* canvas = surface->getCanvas();
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001547 Result result = src.draw(direct, canvas);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001548 if (!result.isOk()) {
1549 return result;
mtklein748ca3b2015-01-15 10:56:12 -08001550 }
Greg Daniel0a2464f2020-05-14 15:45:44 -04001551 surface->flushAndSubmit();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001552 if (FLAGS_gpuStats) {
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001553 direct->priv().dumpCacheStats(log);
1554 direct->priv().dumpGpuStats(log);
1555 direct->priv().dumpContextStats(log);
mtkleinb9eb4ac2015-02-02 18:26:03 -08001556 }
Robert Phillips3c1efd42020-02-13 15:51:59 -05001557
1558 this->readBack(surface.get(), dst);
1559
mtklein55e88b22015-01-21 15:50:13 -08001560 if (FLAGS_abandonGpuContext) {
1561 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001562 } else if (FLAGS_releaseAndAbandonGpuContext) {
1563 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001564 }
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001565 if (!direct->abandoned()) {
Brian Salomonf865b052018-03-09 09:01:53 -05001566 surface.reset();
1567 if (backendTexture.isValid()) {
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001568 direct->deleteBackendTexture(backendTexture);
Brian Salomonf865b052018-03-09 09:01:53 -05001569 }
1570 if (backendRT.isValid()) {
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001571 direct->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
Brian Salomonf865b052018-03-09 09:01:53 -05001572 }
1573 }
Greg Daniela870b462019-01-08 15:49:46 -05001574 if (grOptions.fPersistentCache) {
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001575 direct->storeVkPipelineCacheData();
Greg Daniela870b462019-01-08 15:49:46 -05001576 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001577 return Result::Ok();
mtklein748ca3b2015-01-15 10:56:12 -08001578}
1579
1580/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1581
Brian Osman3fdfe282019-09-09 13:46:52 -04001582GPUThreadTestingSink::GPUThreadTestingSink(const SkCommandLineConfigGpu* config,
Brian Osmanf9810662017-08-30 10:02:10 -04001583 const GrContextOptions& grCtxOptions)
Brian Osman3fdfe282019-09-09 13:46:52 -04001584 : INHERITED(config, grCtxOptions)
Mike Klein022cfa22017-09-01 11:53:16 -04001585 , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)) {
Brian Osmanf9810662017-08-30 10:02:10 -04001586 SkASSERT(fExecutor);
1587}
1588
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001589Result GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
Brian Osmanf9810662017-08-30 10:02:10 -04001590 SkString* log) const {
1591 // Draw twice, once with worker threads, and once without. Verify that we get the same result.
1592 // Also, force us to only use the software path renderer, so we really stress-test the threaded
1593 // version of that code.
1594 GrContextOptions contextOptions = this->baseContextOptions();
Brian Osman195c05b2017-08-30 15:14:04 -04001595 contextOptions.fGpuPathRenderers = GpuPathRenderers::kNone;
Greg Daniel024615e2018-05-10 17:25:52 +00001596 contextOptions.fExecutor = fExecutor.get();
Adrienne Walkerab7181d2018-05-14 14:02:03 -07001597
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001598 Result result = this->onDraw(src, dst, wStream, log, contextOptions);
1599 if (!result.isOk() || !dst) {
1600 return result;
Brian Osmanf9810662017-08-30 10:02:10 -04001601 }
1602
1603 SkBitmap reference;
1604 SkString refLog;
1605 SkDynamicMemoryWStream refStream;
1606 contextOptions.fExecutor = nullptr;
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001607 Result refResult = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1608 if (!refResult.isOk()) {
1609 return refResult;
Brian Osmanf9810662017-08-30 10:02:10 -04001610 }
1611
Brian Osmane5756ec2017-09-06 17:08:30 -04001612 return compare_bitmaps(reference, *dst);
Brian Osmanf9810662017-08-30 10:02:10 -04001613}
1614
1615/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1616
Brian Osman3fdfe282019-09-09 13:46:52 -04001617GPUPersistentCacheTestingSink::GPUPersistentCacheTestingSink(const SkCommandLineConfigGpu* config,
1618 const GrContextOptions& grCtxOptions)
1619 : INHERITED(config, grCtxOptions)
1620 , fCacheType(config->getTestPersistentCache()) {}
Brian Salomon00a5eb82018-07-11 15:32:05 -04001621
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001622Result GPUPersistentCacheTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1623 SkString* log) const {
Brian Salomon00a5eb82018-07-11 15:32:05 -04001624 // Draw twice, once with a cold cache, and again with a warm cache. Verify that we get the same
1625 // result.
1626 sk_gpu_test::MemoryCache memoryCache;
1627 GrContextOptions contextOptions = this->baseContextOptions();
1628 contextOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -04001629 if (fCacheType == 2) {
1630 contextOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
1631 }
Brian Salomon00a5eb82018-07-11 15:32:05 -04001632
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001633 Result result = this->onDraw(src, dst, wStream, log, contextOptions);
1634 if (!result.isOk() || !dst) {
1635 return result;
Brian Salomon00a5eb82018-07-11 15:32:05 -04001636 }
1637
1638 SkBitmap reference;
1639 SkString refLog;
1640 SkDynamicMemoryWStream refStream;
Brian Osman43f443f2020-06-05 11:11:36 -04001641 memoryCache.resetCacheStats();
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001642 Result refResult = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1643 if (!refResult.isOk()) {
1644 return refResult;
Brian Salomon00a5eb82018-07-11 15:32:05 -04001645 }
Brian Salomon7fc52992018-07-12 16:20:23 -04001646 SkASSERT(!memoryCache.numCacheMisses());
Brian Osman43f443f2020-06-05 11:11:36 -04001647 SkASSERT(!memoryCache.numCacheStores());
Brian Salomon00a5eb82018-07-11 15:32:05 -04001648
1649 return compare_bitmaps(reference, *dst);
1650}
1651
Brian Osmaned58e002019-09-06 14:42:43 -04001652
1653/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1654
Brian Osman3fdfe282019-09-09 13:46:52 -04001655GPUPrecompileTestingSink::GPUPrecompileTestingSink(const SkCommandLineConfigGpu* config,
1656 const GrContextOptions& grCtxOptions)
1657 : INHERITED(config, grCtxOptions) {}
Brian Osmaned58e002019-09-06 14:42:43 -04001658
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001659Result GPUPrecompileTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1660 SkString* log) const {
Brian Osmaned58e002019-09-06 14:42:43 -04001661 // Three step process:
1662 // 1) Draw once with an SkSL cache, and store off the shader blobs.
1663 // 2) For the second context, pre-compile the shaders to warm the cache.
1664 // 3) Draw with the second context, ensuring that we get the same result, and no cache misses.
1665 sk_gpu_test::MemoryCache memoryCache;
1666 GrContextOptions contextOptions = this->baseContextOptions();
1667 contextOptions.fPersistentCache = &memoryCache;
1668 contextOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmaned58e002019-09-06 14:42:43 -04001669
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001670 Result result = this->onDraw(src, dst, wStream, log, contextOptions);
1671 if (!result.isOk() || !dst) {
1672 return result;
Brian Osmaned58e002019-09-06 14:42:43 -04001673 }
1674
1675 auto precompileShaders = [&memoryCache](GrContext* context) {
1676 memoryCache.foreach([context](sk_sp<const SkData> key, sk_sp<SkData> data, int /*count*/) {
1677 SkAssertResult(context->precompileShader(*key, *data));
1678 });
1679 };
1680
1681 sk_gpu_test::MemoryCache replayCache;
1682 GrContextOptions replayOptions = this->baseContextOptions();
1683 // Ensure that the runtime cache is large enough to hold all of the shaders we pre-compile
1684 replayOptions.fRuntimeProgramCacheSize = memoryCache.numCacheMisses();
1685 replayOptions.fPersistentCache = &replayCache;
Brian Osmaned58e002019-09-06 14:42:43 -04001686
1687 SkBitmap reference;
1688 SkString refLog;
1689 SkDynamicMemoryWStream refStream;
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001690 Result refResult = this->onDraw(src, &reference, &refStream, &refLog, replayOptions,
1691 precompileShaders);
1692 if (!refResult.isOk()) {
1693 return refResult;
Brian Osmaned58e002019-09-06 14:42:43 -04001694 }
1695 SkASSERT(!replayCache.numCacheMisses());
1696
1697 return compare_bitmaps(reference, *dst);
1698}
1699
Robert Phillips62768192020-04-22 08:28:58 -04001700/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
Robert Phillips762cb4e2020-06-15 13:12:32 -04001701GPUOOPRSink::GPUOOPRSink(const SkCommandLineConfigGpu* config, const GrContextOptions& ctxOptions)
1702 : INHERITED(config, ctxOptions) {
1703}
1704
1705Result GPUOOPRSink::ooprDraw(const Src& src,
1706 sk_sp<SkSurface> dstSurface,
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001707 GrDirectContext* context) const {
Robert Phillips762cb4e2020-06-15 13:12:32 -04001708 SkSurfaceCharacterization dstCharacterization;
1709 SkAssertResult(dstSurface->characterize(&dstCharacterization));
1710
1711 SkDeferredDisplayListRecorder recorder(dstCharacterization);
1712
Robert Phillips889d6132020-06-16 11:11:33 -04001713 Result result = src.draw(context, recorder.getCanvas());
Robert Phillips762cb4e2020-06-15 13:12:32 -04001714 if (!result.isOk()) {
1715 return result;
1716 }
1717
Adlai Holler7580ad42020-06-24 13:45:25 -04001718 auto ddl = recorder.detach();
Robert Phillips762cb4e2020-06-15 13:12:32 -04001719
1720 SkDeferredDisplayList::ProgramIterator iter(context, ddl.get());
1721 for (; !iter.done(); iter.next()) {
1722 iter.compile();
1723 }
1724
Adlai Hollerf19bbb52020-06-29 10:00:08 -04001725 SkAssertResult(dstSurface->draw(std::move(ddl)));
Robert Phillips762cb4e2020-06-15 13:12:32 -04001726
1727 return Result::Ok();
1728}
1729
1730Result GPUOOPRSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
1731 GrContextOptions contextOptions = this->baseContextOptions();
1732 src.modifyGrContextOptions(&contextOptions);
1733 contextOptions.fPersistentCache = nullptr;
1734 contextOptions.fExecutor = nullptr;
1735
1736 GrContextFactory factory(contextOptions);
1737
1738 ContextInfo ctxInfo = factory.getContextInfo(this->contextType(), this->contextOverrides());
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001739 auto context = ctxInfo.directContext();
Robert Phillips762cb4e2020-06-15 13:12:32 -04001740 if (!context) {
1741 return Result::Fatal("Could not create context.");
1742 }
1743
1744 SkASSERT(context->priv().getGpu());
1745
1746 GrBackendTexture backendTexture;
1747 GrBackendRenderTarget backendRT;
1748 sk_sp<SkSurface> surface = this->createDstSurface(context, src.size(),
1749 &backendTexture, &backendRT);
1750 if (!surface) {
1751 return Result::Fatal("Could not create a surface.");
1752 }
1753
1754 Result result = this->ooprDraw(src, surface, context);
1755 if (!result.isOk()) {
1756 return result;
1757 }
1758
1759 if (FLAGS_gpuStats) {
1760 context->priv().dumpCacheStats(log);
1761 context->priv().dumpGpuStats(log);
1762 context->priv().dumpContextStats(log);
1763 }
1764
1765 if (!this->readBack(surface.get(), dst)) {
1766 return Result::Fatal("Could not readback from surface.");
1767 }
1768
1769 surface.reset();
1770 if (backendTexture.isValid()) {
1771 context->deleteBackendTexture(backendTexture);
1772 }
1773 if (backendRT.isValid()) {
1774 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
1775 }
1776
1777 return Result::Ok();
1778}
1779
1780/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1781GPUDDLSink::GPUDDLSink(const SkCommandLineConfigGpu* config, const GrContextOptions& ctxOptions)
1782 : INHERITED(config, ctxOptions)
1783 , fRecordingExecutor(SkExecutor::MakeLIFOThreadPool(1))
1784 , fGPUExecutor(SkExecutor::MakeFIFOThreadPool(1, false)) {
Robert Phillips291f3402020-02-19 14:14:47 -05001785}
1786
1787Result GPUDDLSink::ddlDraw(const Src& src,
1788 sk_sp<SkSurface> dstSurface,
1789 SkTaskGroup* recordingTaskGroup,
1790 SkTaskGroup* gpuTaskGroup,
Robert Phillips19f466d2020-02-26 10:27:07 -05001791 sk_gpu_test::TestContext* gpuTestCtx,
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001792 GrDirectContext* gpuThreadCtx) const {
Robert Phillips19f466d2020-02-26 10:27:07 -05001793
1794 // We have to do this here bc characterization can hit the SkGpuDevice's thread guard (i.e.,
1795 // leaving it until the DDLTileHelper ctor will result in multiple threads trying to use the
1796 // same context (this thread and the gpuThread - which will be uploading textures)).
1797 SkSurfaceCharacterization dstCharacterization;
1798 SkAssertResult(dstSurface->characterize(&dstCharacterization));
1799
Robert Phillips291f3402020-02-19 14:14:47 -05001800 auto size = src.size();
1801 SkPictureRecorder recorder;
Robert Phillips889d6132020-06-16 11:11:33 -04001802 Result result = src.draw(gpuThreadCtx, recorder.beginRecording(SkIntToScalar(size.width()),
1803 SkIntToScalar(size.height())));
Robert Phillips291f3402020-02-19 14:14:47 -05001804 if (!result.isOk()) {
1805 return result;
1806 }
1807 sk_sp<SkPicture> inputPicture(recorder.finishRecordingAsPicture());
1808
1809 // this is our ultimate final drawing area/rect
1810 SkIRect viewport = SkIRect::MakeWH(size.fWidth, size.fHeight);
1811
1812 DDLPromiseImageHelper promiseImageHelper;
1813 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture.get());
1814 if (!compressedPictureData) {
1815 return Result::Fatal("GPUDDLSink: Couldn't deflate SkPicture");
1816 }
1817
1818 promiseImageHelper.createCallbackContexts(gpuThreadCtx);
1819
Robert Phillipsc869ff72020-06-19 09:50:33 -04001820 // 'gpuTestCtx/gpuThreadCtx' is being shifted to the gpuThread. Leave the main (this)
1821 // thread w/o a context.
1822 gpuTestCtx->makeNotCurrent();
1823
1824 // Job one for the GPU thread is to make 'gpuTestCtx' current!
1825 gpuTaskGroup->add([gpuTestCtx] { gpuTestCtx->makeCurrent(); });
1826
Robert Phillips291f3402020-02-19 14:14:47 -05001827 // TODO: move the image upload to the utility thread
1828 promiseImageHelper.uploadAllToGPU(gpuTaskGroup, gpuThreadCtx);
1829
Robert Phillips11c67672020-04-23 15:10:03 -04001830 // Care must be taken when using 'gpuThreadCtx' bc it moves between the gpu-thread and this
1831 // one. About all it can be consistently used for is GrCaps access and 'defaultBackendFormat'
1832 // calls.
Robert Phillips291f3402020-02-19 14:14:47 -05001833 constexpr int kNumDivisions = 3;
Robert Phillips11c67672020-04-23 15:10:03 -04001834 DDLTileHelper tiles(gpuThreadCtx, dstCharacterization, viewport, kNumDivisions);
Robert Phillips291f3402020-02-19 14:14:47 -05001835
Robert Phillips8472a3d2020-04-16 16:27:45 -04001836 tiles.createBackendTextures(gpuTaskGroup, gpuThreadCtx);
1837
Robert Phillips291f3402020-02-19 14:14:47 -05001838 // Reinflate the compressed picture individually for each thread.
1839 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
1840
1841 tiles.kickOffThreadedWork(recordingTaskGroup, gpuTaskGroup, gpuThreadCtx);
Robert Phillips11c67672020-04-23 15:10:03 -04001842
1843 // We have to wait for the recording threads to schedule all their work on the gpu thread
1844 // before we can schedule the composition draw and the flush. Note that the gpu thread
1845 // is not blocked at this point and this thread is borrowing recording work.
Robert Phillips19f466d2020-02-26 10:27:07 -05001846 recordingTaskGroup->wait();
Robert Phillips291f3402020-02-19 14:14:47 -05001847
Robert Phillips11c67672020-04-23 15:10:03 -04001848 // Note: at this point the recording thread(s) are stalled out w/ nothing to do.
1849
1850 // The recording threads have already scheduled the drawing of each tile's DDL on the gpu
1851 // thread. The composition DDL must be scheduled last bc it relies on the result of all
1852 // the tiles' rendering. Additionally, bc we're aliasing the tiles' backend textures,
1853 // there is nothing in the DAG to automatically force the required order.
1854 gpuTaskGroup->add([dstSurface, ddl = tiles.composeDDL()]() {
1855 dstSurface->draw(ddl);
1856 });
1857
Robert Phillips5dbcca52020-05-29 10:41:33 -04001858 // This should be the only explicit flush for the entire DDL draw.
1859 // TODO: remove the flushes in do_gpu_stuff
Robert Phillips4e3ceb92020-04-16 15:42:50 -04001860 gpuTaskGroup->add([gpuThreadCtx]() {
1861 // We need to ensure all the GPU work is finished so
1862 // the following 'deleteAllFromGPU' call will work
1863 // on Vulkan.
1864 // TODO: switch over to using the promiseImage callbacks
1865 // to free the backendTextures. This is complicated a
1866 // bit by which thread possesses the direct context.
Greg Danielce9f0162020-06-30 13:42:46 -04001867 gpuThreadCtx->flush();
Greg Daniel0a2464f2020-05-14 15:45:44 -04001868 gpuThreadCtx->submit(true);
Robert Phillips4e3ceb92020-04-16 15:42:50 -04001869 });
Robert Phillips291f3402020-02-19 14:14:47 -05001870
Robert Phillips19f466d2020-02-26 10:27:07 -05001871 // The backend textures are created on the gpuThread by the 'uploadAllToGPU' call.
1872 // It is simpler to also delete them at this point on the gpuThread.
1873 promiseImageHelper.deleteAllFromGPU(gpuTaskGroup, gpuThreadCtx);
1874
Robert Phillips8472a3d2020-04-16 16:27:45 -04001875 tiles.deleteBackendTextures(gpuTaskGroup, gpuThreadCtx);
1876
Robert Phillips19f466d2020-02-26 10:27:07 -05001877 // A flush has already been scheduled on the gpu thread along with the clean up of the backend
Robert Phillips8472a3d2020-04-16 16:27:45 -04001878 // textures so it is safe to schedule making 'gpuTestCtx' not current on the gpuThread.
Robert Phillips19f466d2020-02-26 10:27:07 -05001879 gpuTaskGroup->add([gpuTestCtx] { gpuTestCtx->makeNotCurrent(); });
1880
1881 // All the work is scheduled on the gpu thread, we just need to wait
Robert Phillips291f3402020-02-19 14:14:47 -05001882 gpuTaskGroup->wait();
1883
1884 return Result::Ok();
1885}
1886
Robert Phillips762cb4e2020-06-15 13:12:32 -04001887Result GPUDDLSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
Robert Phillips291f3402020-02-19 14:14:47 -05001888 GrContextOptions contextOptions = this->baseContextOptions();
1889 src.modifyGrContextOptions(&contextOptions);
1890 contextOptions.fPersistentCache = nullptr;
1891 contextOptions.fExecutor = nullptr;
1892
1893 GrContextFactory factory(contextOptions);
1894
1895 // This captures the context destined to be the main gpu context
1896 ContextInfo mainCtxInfo = factory.getContextInfo(this->contextType(), this->contextOverrides());
1897 sk_gpu_test::TestContext* mainTestCtx = mainCtxInfo.testContext();
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001898 auto mainCtx = mainCtxInfo.directContext();
Robert Phillips291f3402020-02-19 14:14:47 -05001899 if (!mainCtx) {
1900 return Result::Fatal("Could not create context.");
1901 }
1902
1903 SkASSERT(mainCtx->priv().getGpu());
1904
1905 // TODO: make use of 'otherCtx' for uploads & compilation
1906#if 0
1907 // This captures the context destined to be the utility context. It is in a share group
1908 // with the main context
1909 ContextInfo otherCtxInfo = factory.getSharedContextInfo(mainCtx);
1910 sk_gpu_test::TestContext* otherTestCtx = otherCtxInfo.testContext();
1911 GrContext* otherCtx = otherCtxInfo.grContext();
1912 if (!otherCtx) {
1913 return Result::Fatal("Cound not create shared context.");
1914 }
1915
1916 SkASSERT(otherCtx->priv().getGpu());
1917#endif
1918
Robert Phillips762cb4e2020-06-15 13:12:32 -04001919 SkTaskGroup recordingTaskGroup(*fRecordingExecutor);
1920 SkTaskGroup gpuTaskGroup(*fGPUExecutor);
Robert Phillips291f3402020-02-19 14:14:47 -05001921
1922 // Make sure 'mainCtx' is current
1923 mainTestCtx->makeCurrent();
1924
1925 GrBackendTexture backendTexture;
1926 GrBackendRenderTarget backendRT;
1927 sk_sp<SkSurface> surface = this->createDstSurface(mainCtx, src.size(),
1928 &backendTexture, &backendRT);
1929 if (!surface) {
1930 return Result::Fatal("Could not create a surface.");
1931 }
1932
Robert Phillips19f466d2020-02-26 10:27:07 -05001933 Result result = this->ddlDraw(src, surface, &recordingTaskGroup, &gpuTaskGroup,
1934 mainTestCtx, mainCtx);
Robert Phillips291f3402020-02-19 14:14:47 -05001935 if (!result.isOk()) {
1936 return result;
1937 }
1938
Robert Phillips19f466d2020-02-26 10:27:07 -05001939 // 'ddlDraw' will have made 'mainCtx' not current on the gpuThread
Robert Phillips291f3402020-02-19 14:14:47 -05001940 mainTestCtx->makeCurrent();
1941
1942 if (FLAGS_gpuStats) {
1943 mainCtx->priv().dumpCacheStats(log);
1944 mainCtx->priv().dumpGpuStats(log);
Robert Phillips273f1072020-05-05 13:03:07 -04001945 mainCtx->priv().dumpContextStats(log);
Robert Phillips291f3402020-02-19 14:14:47 -05001946
1947#if 0
1948 otherCtx->priv().dumpCacheStats(log);
1949 otherCtx->priv().dumpGpuStats(log);
Robert Phillips273f1072020-05-05 13:03:07 -04001950 otherCtx->priv().dumpContextStats(log);
Robert Phillips291f3402020-02-19 14:14:47 -05001951#endif
1952 }
1953
1954 if (!this->readBack(surface.get(), dst)) {
1955 return Result::Fatal("Could not readback from surface.");
1956 }
1957
1958 surface.reset();
1959 if (backendTexture.isValid()) {
1960 mainCtx->deleteBackendTexture(backendTexture);
1961 }
1962 if (backendRT.isValid()) {
1963 mainCtx->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
1964 }
1965
1966 return Result::Ok();
1967}
1968
Brian Salomon00a5eb82018-07-11 15:32:05 -04001969/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001970static Result draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001971 if (src.size().isEmpty()) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001972 return Result::Fatal("Source has empty dimensions");
halcanary4ba051c2016-03-10 10:31:53 -08001973 }
halcanary47ef4d52015-03-03 09:13:09 -08001974 SkASSERT(doc);
halcanary45420a92016-06-02 12:41:14 -07001975 int pageCount = src.pageCount();
1976 for (int i = 0; i < pageCount; ++i) {
1977 int width = src.size(i).width(), height = src.size(i).height();
halcanary7e798182015-04-14 14:06:18 -07001978 SkCanvas* canvas =
1979 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1980 if (!canvas) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001981 return Result::Fatal("SkDocument::beginPage(w,h) returned nullptr");
halcanary7e798182015-04-14 14:06:18 -07001982 }
Robert Phillips889d6132020-06-16 11:11:33 -04001983 Result result = src.draw(i, nullptr, canvas);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001984 if (!result.isOk()) {
1985 return result;
halcanary7e798182015-04-14 14:06:18 -07001986 }
1987 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001988 }
reedd14df7c2016-09-22 14:12:46 -07001989 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -08001990 dst->flush();
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001991 return Result::Ok();
mtklein748ca3b2015-01-15 10:56:12 -08001992}
1993
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001994Result PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canary23564b92018-09-07 14:33:14 -04001995 SkPDF::Metadata metadata;
halcanary4b656662016-04-27 07:45:18 -07001996 metadata.fTitle = src.name();
1997 metadata.fSubject = "rendering correctness test";
1998 metadata.fCreator = "Skia/DM";
Mike Reeda4daf192017-12-14 13:25:04 -05001999 metadata.fRasterDPI = fRasterDpi;
2000 metadata.fPDFA = fPDFA;
Hal Canaryf34131a2018-12-18 15:11:03 -05002001#if SK_PDF_TEST_EXECUTOR
Hal Canary9a3f5542018-12-10 19:59:07 -05002002 std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool();
2003 metadata.fExecutor = executor.get();
2004#endif
Hal Canary3026d4b2019-01-07 10:00:48 -05002005 auto doc = SkPDF::MakeDocument(dst, metadata);
halcanary47ef4d52015-03-03 09:13:09 -08002006 if (!doc) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002007 return Result::Fatal("SkPDF::MakeDocument() returned nullptr");
halcanary47ef4d52015-03-03 09:13:09 -08002008 }
2009 return draw_skdocument(src, doc.get(), dst);
2010}
2011
2012/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2013
2014XPSSink::XPSSink() {}
2015
Robert Phillips9b00f1f2020-03-24 12:29:44 -04002016#if defined(SK_SUPPORT_XPS)
Hal Canary5e221e72017-02-06 09:51:42 -05002017static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() {
2018 IXpsOMObjectFactory* factory;
2019 HRN(CoCreateInstance(CLSID_XpsOMObjectFactory,
2020 nullptr,
2021 CLSCTX_INPROC_SERVER,
2022 IID_PPV_ARGS(&factory)));
2023 return SkTScopedComPtr<IXpsOMObjectFactory>(factory);
2024}
2025
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002026Result XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canaryabc88d22017-02-06 09:26:49 -05002027 SkAutoCoInitialize com;
2028 if (!com.succeeded()) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002029 return Result::Fatal("Could not initialize COM.");
Hal Canaryabc88d22017-02-06 09:26:49 -05002030 }
Hal Canary5e221e72017-02-06 09:51:42 -05002031 SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory();
2032 if (!factory) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002033 return Result::Fatal("Failed to create XPS Factory.");
Hal Canary5e221e72017-02-06 09:51:42 -05002034 }
Hal Canary3026d4b2019-01-07 10:00:48 -05002035 auto doc = SkXPS::MakeDocument(dst, factory.get());
halcanary47ef4d52015-03-03 09:13:09 -08002036 if (!doc) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002037 return Result::Fatal("SkXPS::MakeDocument() returned nullptr");
halcanary47ef4d52015-03-03 09:13:09 -08002038 }
2039 return draw_skdocument(src, doc.get(), dst);
2040}
Hal Canary5e221e72017-02-06 09:51:42 -05002041#else
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002042Result XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
2043 return Result::Fatal("XPS not supported on this platform.");
Hal Canary5e221e72017-02-06 09:51:42 -05002044}
2045#endif
reed54dc4872016-09-13 08:09:45 -07002046
2047/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2048
mtklein9c3f17d2015-01-28 11:35:18 -08002049SKPSink::SKPSink() {}
2050
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002051Result SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Brian Salomon4e1066f2019-12-04 10:33:52 -05002052 auto size = SkSize::Make(src.size());
mtklein9c3f17d2015-01-28 11:35:18 -08002053 SkPictureRecorder recorder;
Robert Phillips889d6132020-06-16 11:11:33 -04002054 Result result = src.draw(nullptr, recorder.beginRecording(size.width(), size.height()));
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002055 if (!result.isOk()) {
2056 return result;
mtklein9c3f17d2015-01-28 11:35:18 -08002057 }
reedca2622b2016-03-18 07:25:55 -07002058 recorder.finishRecordingAsPicture()->serialize(dst);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002059 return Result::Ok();
mtklein9c3f17d2015-01-28 11:35:18 -08002060}
2061
2062/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2063
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002064Result DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Mike Klein8f4e2242019-03-20 11:59:00 -05002065 DebugCanvas debugCanvas(src.size().width(), src.size().height());
Robert Phillips889d6132020-06-16 11:11:33 -04002066 Result result = src.draw(nullptr, &debugCanvas);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002067 if (!result.isOk()) {
2068 return result;
Hal Canary85c7fe82016-10-25 10:33:27 -04002069 }
Mike Reed5df49342016-11-12 08:06:55 -06002070 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
Hal Canary85c7fe82016-10-25 10:33:27 -04002071 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05002072 SkJSONWriter writer(dst, SkJSONWriter::Mode::kPretty);
2073 writer.beginObject(); // root
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -05002074 debugCanvas.toJSON(writer, dataManager, nullCanvas.get());
Brian Osmand8a90f92019-01-28 13:41:19 -05002075 writer.endObject(); // root
2076 writer.flush();
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002077 return Result::Ok();
Hal Canary85c7fe82016-10-25 10:33:27 -04002078}
2079
2080/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2081
Bryce Thomas95a7b762018-03-02 13:54:21 -08002082SVGSink::SVGSink(int pageIndex) : fPageIndex(pageIndex) {}
mtklein8a4527e2015-01-31 20:00:58 -08002083
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002084Result SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita718df0a2016-07-15 10:33:29 -07002085#if defined(SK_XML)
Bryce Thomas95a7b762018-03-02 13:54:21 -08002086 if (src.pageCount() > 1) {
2087 int pageCount = src.pageCount();
2088 if (fPageIndex > pageCount - 1) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002089 return Result::Fatal("Page index %d too high for document with only %d pages.",
2090 fPageIndex, pageCount);
Bryce Thomas95a7b762018-03-02 13:54:21 -08002091 }
2092 }
Robert Phillips889d6132020-06-16 11:11:33 -04002093 return src.draw(fPageIndex, nullptr,
Bryce Thomas95a7b762018-03-02 13:54:21 -08002094 SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
Mike Reed5df49342016-11-12 08:06:55 -06002095 SkIntToScalar(src.size().height())),
Florin Malita562017b2019-02-14 13:42:15 -05002096 dst)
Bryce Thomas95a7b762018-03-02 13:54:21 -08002097 .get());
fmalita718df0a2016-07-15 10:33:29 -07002098#else
Hal Canary327ef032018-03-22 13:10:51 -04002099 (void)fPageIndex;
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002100 return Result::Fatal("SVG sink is disabled.");
fmalita718df0a2016-07-15 10:33:29 -07002101#endif // SK_XML
mtklein8a4527e2015-01-31 20:00:58 -08002102}
2103
2104/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2105
brianosmanb109b8c2016-06-16 13:03:24 -07002106RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
mtklein27c3fdd2016-02-26 14:43:21 -08002107 : fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07002108 , fColorSpace(std::move(colorSpace)) {}
mtklein748ca3b2015-01-15 10:56:12 -08002109
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002110Result RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08002111 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08002112 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
2113 SkAlphaType alphaType = kPremul_SkAlphaType;
2114 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
2115
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002116 dst->allocPixelsFlags(SkImageInfo::Make(size, fColorType, alphaType, fColorSpace),
Mike Reed086a4272017-07-18 10:53:11 -04002117 SkBitmap::kZeroPixels_AllocFlag);
Yuqian Lib8b62532018-02-23 14:13:36 +08002118
mtklein748ca3b2015-01-15 10:56:12 -08002119 SkCanvas canvas(*dst);
Robert Phillips889d6132020-06-16 11:11:33 -04002120 return src.draw(nullptr, &canvas);
mtklein748ca3b2015-01-15 10:56:12 -08002121}
2122
2123/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2124
mtkleina16e69e2015-05-05 11:38:45 -07002125// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07002126// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07002127// Several examples below.
2128
mtkleincbf89782016-02-19 14:27:14 -08002129template <typename Fn>
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002130static Result draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
Robert Phillipsb87b39b2020-07-01 14:45:24 -04002131 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07002132 class ProxySrc : public Src {
2133 public:
mtkleincbf89782016-02-19 14:27:14 -08002134 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
Robert Phillipsb87b39b2020-07-01 14:45:24 -04002135 Result draw(GrDirectContext*, SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07002136 Name name() const override { return "ProxySrc"; }
2137 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07002138 private:
mtkleincbf89782016-02-19 14:27:14 -08002139 SkISize fSize;
2140 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07002141 };
msarett62d3b102015-12-10 15:14:27 -08002142 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07002143}
2144
2145/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2146
Mike Klein84836b72019-03-21 11:31:36 -05002147static DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
mtklein4a34ecb2016-01-08 10:19:35 -08002148
2149// Is *bitmap identical to what you get drawing src into sink?
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002150static Result check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
mtklein4a34ecb2016-01-08 10:19:35 -08002151 // We can only check raster outputs.
2152 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
2153 if (FLAGS_check && bitmap) {
2154 SkBitmap reference;
2155 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07002156 SkDynamicMemoryWStream wStream;
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002157 Result result = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08002158 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002159 SkASSERT(result.isOk());
2160 if (!result.isOk()) {
2161 return result;
mtklein4a34ecb2016-01-08 10:19:35 -08002162 }
Brian Osmane5756ec2017-09-06 17:08:30 -04002163 return compare_bitmaps(reference, *bitmap);
mtklein4a34ecb2016-01-08 10:19:35 -08002164 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002165 return Result::Ok();
mtklein4a34ecb2016-01-08 10:19:35 -08002166}
2167
2168/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2169
mtkleind603b222015-02-17 11:13:33 -08002170static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
2171 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
2172 matrix->mapRect(&bounds);
2173 matrix->postTranslate(-bounds.x(), -bounds.y());
Hal Canaryfafe1352017-04-11 12:12:02 -04002174 return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())};
mtkleind603b222015-02-17 11:13:33 -08002175}
2176
msarett62d3b102015-12-10 15:14:27 -08002177ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08002178
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002179Result ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07002180 SkMatrix matrix = fMatrix;
2181 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
Ben Wagner145dbcd2016-11-03 14:40:50 -04002182 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07002183 canvas->concat(matrix);
Robert Phillips889d6132020-06-16 11:11:33 -04002184 return src.draw(nullptr, canvas);
mtkleina16e69e2015-05-05 11:38:45 -07002185 });
mtklein748ca3b2015-01-15 10:56:12 -08002186}
2187
mtkleind603b222015-02-17 11:13:33 -08002188// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
2189// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08002190ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08002191
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002192Result ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2193 Result result = fSink->draw(src, bitmap, stream, log);
2194 if (!result.isOk()) {
2195 return result;
mtkleind603b222015-02-17 11:13:33 -08002196 }
2197
2198 SkMatrix inverse;
2199 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002200 return Result::Fatal("Cannot upright --matrix.");
mtkleind603b222015-02-17 11:13:33 -08002201 }
2202 SkMatrix upright = SkMatrix::I();
2203 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
2204 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
2205 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
2206 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
2207
2208 SkBitmap uprighted;
2209 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002210 uprighted.allocPixels(bitmap->info().makeDimensions(size));
mtkleind603b222015-02-17 11:13:33 -08002211
2212 SkCanvas canvas(uprighted);
2213 canvas.concat(upright);
2214 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07002215 paint.setBlendMode(SkBlendMode::kSrc);
mtkleind603b222015-02-17 11:13:33 -08002216 canvas.drawBitmap(*bitmap, 0, 0, &paint);
2217
2218 *bitmap = uprighted;
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002219 return Result::Ok();
mtkleind603b222015-02-17 11:13:33 -08002220}
2221
mtklein748ca3b2015-01-15 10:56:12 -08002222/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2223
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002224Result ViaSerialization::draw(
mtkleina16e69e2015-05-05 11:38:45 -07002225 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08002226 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07002227 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08002228 SkPictureRecorder recorder;
Robert Phillips889d6132020-06-16 11:11:33 -04002229 Result result = src.draw(nullptr, recorder.beginRecording(SkIntToScalar(size.width()),
2230 SkIntToScalar(size.height())));
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002231 if (!result.isOk()) {
2232 return result;
mtklein748ca3b2015-01-15 10:56:12 -08002233 }
reedca2622b2016-03-18 07:25:55 -07002234 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08002235
2236 // Serialize it and then deserialize it.
reed39eaf5f2016-09-15 07:19:35 -07002237 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
mtklein748ca3b2015-01-15 10:56:12 -08002238
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002239 result = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07002240 canvas->drawPicture(deserialized);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002241 return Result::Ok();
mtkleina16e69e2015-05-05 11:38:45 -07002242 });
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002243 if (!result.isOk()) {
2244 return result;
Ben Wagner1861e882018-04-04 17:40:46 -04002245 }
2246
2247 return check_against_reference(bitmap, src, fSink.get());
mtklein748ca3b2015-01-15 10:56:12 -08002248}
2249
2250/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2251
Brian Salomon57904202018-12-17 14:45:00 -05002252ViaDDL::ViaDDL(int numReplays, int numDivisions, Sink* sink)
2253 : Via(sink), fNumReplays(numReplays), fNumDivisions(numDivisions) {}
Robert Phillips33f02ed2018-03-27 08:06:57 -04002254
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002255Result ViaDDL::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
Robert Phillips33f02ed2018-03-27 08:06:57 -04002256 auto size = src.size();
2257 SkPictureRecorder recorder;
Robert Phillips889d6132020-06-16 11:11:33 -04002258 Result result = src.draw(nullptr, recorder.beginRecording(SkIntToScalar(size.width()),
2259 SkIntToScalar(size.height())));
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002260 if (!result.isOk()) {
2261 return result;
Robert Phillips33f02ed2018-03-27 08:06:57 -04002262 }
2263 sk_sp<SkPicture> inputPicture(recorder.finishRecordingAsPicture());
2264
2265 // this is our ultimate final drawing area/rect
2266 SkIRect viewport = SkIRect::MakeWH(size.fWidth, size.fHeight);
2267
Brian Salomon7d88f312019-02-28 10:03:03 -05002268 DDLPromiseImageHelper promiseImageHelper;
Robert Phillips96601082018-05-29 16:13:26 -04002269 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture.get());
2270 if (!compressedPictureData) {
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002271 return Result::Fatal("ViaDDL: Couldn't deflate SkPicture");
Robert Phillips33f02ed2018-03-27 08:06:57 -04002272 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002273 auto draw = [&](SkCanvas* canvas) -> Result {
Robert Phillipsd5f3c982020-07-07 13:18:47 -04002274 auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
2275 : nullptr;
2276 if (!direct) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -05002277 return Result::Fatal("ViaDDL: DDLs are GPU only");
Brian Salomon57904202018-12-17 14:45:00 -05002278 }
Robert Phillipsa865a3a2020-02-14 10:49:39 -05002279 SkSurface* tmp = canvas->getSurface();
2280 if (!tmp) {
2281 return Result::Fatal("ViaDDL: cannot get surface from canvas");
2282 }
Robert Phillips19f466d2020-02-26 10:27:07 -05002283 sk_sp<SkSurface> dstSurface = sk_ref_sp(tmp);
2284
2285 SkSurfaceCharacterization dstCharacterization;
2286 SkAssertResult(dstSurface->characterize(&dstCharacterization));
Robert Phillips33f02ed2018-03-27 08:06:57 -04002287
Robert Phillipsd5f3c982020-07-07 13:18:47 -04002288 promiseImageHelper.createCallbackContexts(direct);
Robert Phillips923181b2020-02-14 12:36:37 -05002289
Brian Salomon57904202018-12-17 14:45:00 -05002290 // This is here bc this is the first point where we have access to the context
Robert Phillipsd5f3c982020-07-07 13:18:47 -04002291 promiseImageHelper.uploadAllToGPU(nullptr, direct);
Brian Salomon7d88f312019-02-28 10:03:03 -05002292 // We draw N times, with a clear between.
Brian Salomon57904202018-12-17 14:45:00 -05002293 for (int replay = 0; replay < fNumReplays; ++replay) {
2294 if (replay > 0) {
2295 // Clear the drawing of the previous replay
Brian Salomon8d23a582018-12-19 11:35:20 -05002296 canvas->clear(SK_ColorTRANSPARENT);
Brian Salomon57904202018-12-17 14:45:00 -05002297 }
2298 // First, create all the tiles (including their individual dest surfaces)
Robert Phillipsd5f3c982020-07-07 13:18:47 -04002299 DDLTileHelper tiles(direct, dstCharacterization, viewport, fNumDivisions);
Robert Phillips33f02ed2018-03-27 08:06:57 -04002300
Robert Phillipsd5f3c982020-07-07 13:18:47 -04002301 tiles.createBackendTextures(nullptr, direct);
Robert Phillips8472a3d2020-04-16 16:27:45 -04002302
Brian Salomon57904202018-12-17 14:45:00 -05002303 // Second, reinflate the compressed picture individually for each thread
Brian Salomon1bf0ed82019-01-16 13:51:35 -05002304 // This recreates the promise SkImages on each replay iteration. We are currently
2305 // relying on this to test using a SkPromiseImageTexture to fulfill different
2306 // SkImages. On each replay the promise SkImages are recreated in createSKPPerTile.
Brian Salomon57904202018-12-17 14:45:00 -05002307 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
Robert Phillips33f02ed2018-03-27 08:06:57 -04002308
Brian Salomon57904202018-12-17 14:45:00 -05002309 // Third, create the DDLs in parallel
2310 tiles.createDDLsInParallel();
Robert Phillips33f02ed2018-03-27 08:06:57 -04002311
Brian Salomon57904202018-12-17 14:45:00 -05002312 if (replay == fNumReplays - 1) {
Robert Phillips11c67672020-04-23 15:10:03 -04002313 // All the DDLs are created and they ref any created promise images which,
2314 // in turn, ref the callback contexts. If it is the last run, drop the
2315 // promise image helper's refs on the callback contexts.
Brian Salomon57904202018-12-17 14:45:00 -05002316 promiseImageHelper.reset();
Robert Phillips11c67672020-04-23 15:10:03 -04002317 // Note: we cannot drop the tiles' callback contexts here bc they are needed
2318 // to create each tile's destination surface.
Brian Salomon57904202018-12-17 14:45:00 -05002319 }
Robert Phillips33f02ed2018-03-27 08:06:57 -04002320
Brian Salomon57904202018-12-17 14:45:00 -05002321 // Fourth, synchronously render the display lists into the dest tiles
2322 // TODO: it would be cool to not wait until all the tiles are drawn to begin
2323 // drawing to the GPU and composing to the final surface
Robert Phillipsd5f3c982020-07-07 13:18:47 -04002324 tiles.precompileAndDrawAllTiles(direct);
Robert Phillips33f02ed2018-03-27 08:06:57 -04002325
Robert Phillips11c67672020-04-23 15:10:03 -04002326 if (replay == fNumReplays - 1) {
2327 // At this point the compose DDL holds refs to the composition promise images
2328 // which, in turn, hold refs on the tile callback contexts. If it is the last run,
2329 // drop the refs on tile callback contexts.
2330 tiles.dropCallbackContexts();
2331 }
Robert Phillips8472a3d2020-04-16 16:27:45 -04002332
Robert Phillips11c67672020-04-23 15:10:03 -04002333 dstSurface->draw(tiles.composeDDL());
2334
2335 // We need to ensure all the GPU work is finished so the promise image callback
2336 // contexts will delete all the backend textures.
Robert Phillipsd5f3c982020-07-07 13:18:47 -04002337 direct->flush();
2338 direct->submit(true);
Brian Salomon57904202018-12-17 14:45:00 -05002339 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002340 return Result::Ok();
Brian Salomon57904202018-12-17 14:45:00 -05002341 };
2342 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, draw);
Robert Phillips33f02ed2018-03-27 08:06:57 -04002343}
2344
Robert Phillips33f02ed2018-03-27 08:06:57 -04002345/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2346
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002347Result ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein4a34ecb2016-01-08 10:19:35 -08002348 auto size = src.size();
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002349 Result result = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtklein4a34ecb2016-01-08 10:19:35 -08002350 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07002351 sk_sp<SkPicture> pic;
Robert Phillips889d6132020-06-16 11:11:33 -04002352 Result result = src.draw(nullptr, recorder.beginRecording(SkIntToScalar(size.width()),
2353 SkIntToScalar(size.height())));
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002354 if (!result.isOk()) {
2355 return result;
mtklein4a34ecb2016-01-08 10:19:35 -08002356 }
reedca2622b2016-03-18 07:25:55 -07002357 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08002358 canvas->drawPicture(pic);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002359 return result;
mtklein4a34ecb2016-01-08 10:19:35 -08002360 });
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002361 if (!result.isOk()) {
2362 return result;
Ben Wagner1861e882018-04-04 17:40:46 -04002363 }
2364
2365 return check_against_reference(bitmap, src, fSink.get());
mtklein4a34ecb2016-01-08 10:19:35 -08002366}
2367
2368/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2369
Mike Reedbae888e2017-02-18 16:50:45 -05002370#ifdef TEST_VIA_SVG
Mike Kleinc0bd9f92019-04-23 12:05:21 -05002371#include "experimental/svg/model/SkSVGDOM.h"
2372#include "include/svg/SkSVGCanvas.h"
2373#include "src/xml/SkXMLWriter.h"
Mike Reedf67c4592017-02-17 17:06:11 -05002374
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002375Result ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
Mike Reedf67c4592017-02-17 17:06:11 -05002376 auto size = src.size();
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002377 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Result {
Mike Reedf67c4592017-02-17 17:06:11 -05002378 SkDynamicMemoryWStream wstream;
2379 SkXMLStreamWriter writer(&wstream);
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002380 Result result = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get());
2381 if (!result.isOk()) {
2382 return result;
Mike Reedf67c4592017-02-17 17:06:11 -05002383 }
2384 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
2385 auto dom = SkSVGDOM::MakeFromStream(*rstream);
2386 if (dom) {
2387 dom->setContainerSize(SkSize::Make(size));
2388 dom->render(canvas);
2389 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -05002390 return Result::Ok();
Mike Reedf67c4592017-02-17 17:06:11 -05002391 });
2392}
Mike Reedbae888e2017-02-18 16:50:45 -05002393#endif
Mike Reedf67c4592017-02-17 17:06:11 -05002394
mtklein748ca3b2015-01-15 10:56:12 -08002395} // namespace DM