blob: 30a27fe4acc6cbd77f81273d9bd3f0e23fe347a0 [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"
25#include "include/ports/SkImageGeneratorCG.h"
26#include "include/ports/SkImageGeneratorWIC.h"
27#include "include/private/SkImageInfoPriv.h"
28#include "include/private/SkTLogic.h"
Brian Osmanea236bf2019-04-29 10:28:22 -040029#include "include/third_party/skcms/skcms.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "include/utils/SkNullCanvas.h"
31#include "include/utils/SkRandom.h"
32#include "src/codec/SkCodecImageGenerator.h"
33#include "src/codec/SkSwizzler.h"
34#include "src/core/SkAutoMalloc.h"
35#include "src/core/SkAutoPixmapStorage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/core/SkOSFile.h"
37#include "src/core/SkOpts.h"
38#include "src/core/SkPictureCommon.h"
39#include "src/core/SkPictureData.h"
40#include "src/core/SkRecordDraw.h"
41#include "src/core/SkRecorder.h"
42#include "src/core/SkTaskGroup.h"
43#include "src/gpu/GrContextPriv.h"
44#include "src/gpu/GrGpu.h"
45#include "src/utils/SkMultiPictureDocumentPriv.h"
46#include "src/utils/SkOSPath.h"
47#include "tools/DDLPromiseImageHelper.h"
48#include "tools/DDLTileHelper.h"
49#include "tools/Resources.h"
50#include "tools/debugger/DebugCanvas.h"
51#include "tools/gpu/MemoryCache.h"
msarettfc0b6d12016-03-17 13:50:17 -070052#if defined(SK_BUILD_FOR_WIN)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053 #include "include/docs/SkXPSDocument.h"
54 #include "src/utils/win/SkAutoCoInitialize.h"
55 #include "src/utils/win/SkHRESULT.h"
56 #include "src/utils/win/SkTScopedComPtr.h"
Hal Canary5e221e72017-02-06 09:51:42 -050057 #include <XpsObjectModel.h>
msarettfc0b6d12016-03-17 13:50:17 -070058#endif
59
Florin Malita87ccf332018-05-04 12:23:24 -040060#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050061 #include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050062 #include "modules/skresources/include/SkResources.h"
Florin Malita124d5af2017-12-31 17:02:26 -050063#endif
64
fmalita718df0a2016-07-15 10:33:29 -070065#if defined(SK_XML)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050066 #include "experimental/svg/model/SkSVGDOM.h"
67 #include "include/svg/SkSVGCanvas.h"
68 #include "src/xml/SkXMLWriter.h"
fmalita718df0a2016-07-15 10:33:29 -070069#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050070#include "tests/TestUtils.h"
fmalita718df0a2016-07-15 10:33:29 -070071
Hal Canary8a001442018-09-19 11:31:27 -040072#include <cmath>
73#include <functional>
74
Mike Klein84836b72019-03-21 11:31:36 -050075static DEFINE_bool(multiPage, false,
76 "For document-type backends, render the source into multiple pages");
77static DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070078
Mike Klein4932a5e2019-03-22 11:08:02 -050079DECLARE_int(gpuThreads);
80
bsalomon3724e572016-03-30 18:56:19 -070081using sk_gpu_test::GrContextFactory;
82
mtklein748ca3b2015-01-15 10:56:12 -080083namespace DM {
84
Hal Canary972eba32018-07-30 17:07:07 -040085GMSrc::GMSrc(skiagm::GMFactory factory) : fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -080086
87Error GMSrc::draw(SkCanvas* canvas) const {
Hal Canaryedda5652019-08-05 10:28:09 -040088 std::unique_ptr<skiagm::GM> gm(fFactory());
Chris Dalton50e24d72019-02-07 16:20:09 -070089 SkString errorMsg;
90 skiagm::DrawResult drawResult = gm->draw(canvas, &errorMsg);
91 if (skiagm::DrawResult::kSkip == drawResult) {
92 return Error::Nonfatal(std::move(errorMsg)); // Cause this test to be skipped.
93 }
Mike Klein16b1efb2019-04-02 10:01:11 -040094 return errorMsg;
mtklein748ca3b2015-01-15 10:56:12 -080095}
96
97SkISize GMSrc::size() const {
Hal Canaryedda5652019-08-05 10:28:09 -040098 std::unique_ptr<skiagm::GM> gm(fFactory());
mtklein748ca3b2015-01-15 10:56:12 -080099 return gm->getISize();
100}
101
102Name GMSrc::name() const {
Hal Canaryedda5652019-08-05 10:28:09 -0400103 std::unique_ptr<skiagm::GM> gm(fFactory());
mtklein748ca3b2015-01-15 10:56:12 -0800104 return gm->getName();
105}
106
bsalomon4ee6bd82015-05-27 13:23:23 -0700107void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
Hal Canaryedda5652019-08-05 10:28:09 -0400108 std::unique_ptr<skiagm::GM> gm(fFactory());
bsalomon4ee6bd82015-05-27 13:23:23 -0700109 gm->modifyGrContextOptions(options);
110}
111
Tyler Denniston45f94f82020-02-04 16:09:08 -0500112std::unique_ptr<skiagm::verifiers::VerifierList> GMSrc::getVerifiers() const {
113 std::unique_ptr<skiagm::GM> gm(fFactory());
114 return gm->getVerifiers();
115}
116
mtklein748ca3b2015-01-15 10:56:12 -0800117/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
118
msarettd1227a72016-05-18 06:23:57 -0700119BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
msaretta5783ae2015-09-08 15:35:32 -0700120 : fPath(path)
msaretta5783ae2015-09-08 15:35:32 -0700121 , fMode(mode)
122 , fDstColorType(dstColorType)
123 , fSampleSize(sampleSize)
124{}
125
126bool BRDSrc::veto(SinkFlags flags) const {
127 // No need to test to non-raster or indirect backends.
128 return flags.type != SinkFlags::kRaster
129 || flags.approach != SinkFlags::kDirect;
130}
131
msarettd1227a72016-05-18 06:23:57 -0700132static SkBitmapRegionDecoder* create_brd(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700133 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msaretta5783ae2015-09-08 15:35:32 -0700134 if (!encoded) {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400135 return nullptr;
msaretta5783ae2015-09-08 15:35:32 -0700136 }
reed42943c82016-09-12 12:01:44 -0700137 return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy);
msaretta5783ae2015-09-08 15:35:32 -0700138}
139
Matt Sarett334df3a2016-12-15 18:17:33 -0500140static inline void alpha8_to_gray8(SkBitmap* bitmap) {
141 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
142 // them back to kGray8 so our test framework can draw them correctly.
143 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
144 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
145 .makeAlphaType(kOpaque_SkAlphaType);
146 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
147 }
148}
149
msaretta5783ae2015-09-08 15:35:32 -0700150Error BRDSrc::draw(SkCanvas* canvas) const {
151 SkColorType colorType = canvas->imageInfo().colorType();
152 if (kRGB_565_SkColorType == colorType &&
153 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
154 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
155 }
156 switch (fDstColorType) {
157 case CodecSrc::kGetFromCanvas_DstColorType:
158 break;
msaretta5783ae2015-09-08 15:35:32 -0700159 case CodecSrc::kGrayscale_Always_DstColorType:
160 colorType = kGray_8_SkColorType;
161 break;
msarett34e0ec42016-04-22 16:27:24 -0700162 default:
163 SkASSERT(false);
164 break;
msaretta5783ae2015-09-08 15:35:32 -0700165 }
166
Ben Wagner145dbcd2016-11-03 14:40:50 -0400167 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700168 if (nullptr == brd.get()) {
169 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
170 }
171
Leon Scroggins III0118e972018-03-13 11:14:33 -0400172 auto recommendedCT = brd->computeOutputColorType(colorType);
173 if (kRGB_565_SkColorType == colorType && recommendedCT != colorType) {
174 return Error::Nonfatal("Skip decoding non-opaque to 565.");
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400175 }
Leon Scroggins III0118e972018-03-13 11:14:33 -0400176 colorType = recommendedCT;
177
178 auto colorSpace = brd->computeOutputColorSpace(colorType, nullptr);
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400179
msaretta5783ae2015-09-08 15:35:32 -0700180 const uint32_t width = brd->width();
181 const uint32_t height = brd->height();
182 // Visually inspecting very small output images is not necessary.
183 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
184 return Error::Nonfatal("Scaling very small images is uninteresting.");
185 }
186 switch (fMode) {
187 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700188 SkBitmap bitmap;
189 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
Leon Scroggins III0118e972018-03-13 11:14:33 -0400190 fSampleSize, colorType, false, colorSpace)) {
mtklein9b439152015-12-09 13:02:26 -0800191 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700192 }
Matt Sarett334df3a2016-12-15 18:17:33 -0500193 alpha8_to_gray8(&bitmap);
Matt Sarett8db74f12017-06-14 13:02:05 +0000194
msarett35e5d1b2015-10-27 12:50:25 -0700195 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700196 return "";
197 }
198 case kDivisor_Mode: {
199 const uint32_t divisor = 2;
200 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800201 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700202 }
203
204 // Use a border to test subsets that extend outside the image.
205 // We will not allow the border to be larger than the image dimensions. Allowing
206 // these large borders causes off by one errors that indicate a problem with the
207 // test suite, not a problem with the implementation.
Brian Osman788b9162020-02-07 10:36:46 -0500208 const uint32_t maxBorder = std::min(width, height) / (fSampleSize * divisor);
209 const uint32_t scaledBorder = std::min(5u, maxBorder);
msaretta5783ae2015-09-08 15:35:32 -0700210 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
211
212 // We may need to clear the canvas to avoid uninitialized memory.
213 // Assume we are scaling a 780x780 image with sampleSize = 8.
214 // The output image should be 97x97.
215 // Each subset will be 390x390.
216 // Each scaled subset be 48x48.
217 // Four scaled subsets will only fill a 96x96 image.
218 // The bottom row and last column will not be touched.
219 // This is an unfortunate result of our rounding rules when scaling.
220 // Maybe we need to consider testing scaled subsets without trying to
221 // combine them to match the full scaled image? Or maybe this is the
222 // best we can do?
223 canvas->clear(0);
224
225 for (uint32_t x = 0; x < divisor; x++) {
226 for (uint32_t y = 0; y < divisor; y++) {
227 // Calculate the subset dimensions
228 uint32_t subsetWidth = width / divisor;
229 uint32_t subsetHeight = height / divisor;
230 const int left = x * subsetWidth;
231 const int top = y * subsetHeight;
232
233 // Increase the size of the last subset in each row or column, when the
234 // divisor does not divide evenly into the image dimensions
235 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
236 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
237
238 // Increase the size of the subset in order to have a border on each side
239 const int decodeLeft = left - unscaledBorder;
240 const int decodeTop = top - unscaledBorder;
241 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
242 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700243 SkBitmap bitmap;
244 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
Matt Sarett68feef42017-04-11 09:51:32 -0400245 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false,
Leon Scroggins III0118e972018-03-13 11:14:33 -0400246 colorSpace)) {
mtklein9b439152015-12-09 13:02:26 -0800247 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700248 }
msaretta5783ae2015-09-08 15:35:32 -0700249
Matt Sarett334df3a2016-12-15 18:17:33 -0500250 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700251 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700252 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
253 (SkScalar) (subsetWidth / fSampleSize),
254 (SkScalar) (subsetHeight / fSampleSize)),
255 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
256 (SkScalar) (top / fSampleSize),
257 (SkScalar) (subsetWidth / fSampleSize),
258 (SkScalar) (subsetHeight / fSampleSize)),
259 nullptr);
260 }
261 }
262 return "";
263 }
264 default:
265 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800266 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700267 }
268}
269
270SkISize BRDSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400271 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700272 if (brd) {
Brian Osman788b9162020-02-07 10:36:46 -0500273 return {std::max(1, brd->width() / (int)fSampleSize),
274 std::max(1, brd->height() / (int)fSampleSize)};
msaretta5783ae2015-09-08 15:35:32 -0700275 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400276 return {0, 0};
msaretta5783ae2015-09-08 15:35:32 -0700277}
278
279static SkString get_scaled_name(const Path& path, float scale) {
280 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
281}
282
283Name BRDSrc::name() const {
284 // We will replicate the names used by CodecSrc so that images can
285 // be compared in Gold.
286 if (1 == fSampleSize) {
287 return SkOSPath::Basename(fPath.c_str());
288 }
msarett4b0778e2015-11-13 09:59:11 -0800289 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700290}
291
292/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
293
scroggo3ac66e92016-02-08 15:09:48 -0800294static bool serial_from_path_name(const SkString& path) {
295 if (!FLAGS_RAW_threading) {
296 static const char* const exts[] = {
297 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
298 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
299 };
300 const char* actualExt = strrchr(path.c_str(), '.');
301 if (actualExt) {
302 actualExt++;
303 for (auto* ext : exts) {
304 if (0 == strcmp(ext, actualExt)) {
305 return true;
306 }
307 }
308 }
309 }
310 return false;
311}
312
scroggoc5560be2016-02-03 09:42:42 -0800313CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
314 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700315 : fPath(path)
316 , fMode(mode)
317 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800318 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700319 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800320 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700321{}
mtklein748ca3b2015-01-15 10:56:12 -0800322
mtklein99cab4e2015-07-31 06:43:04 -0700323bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800324 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800325 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700326}
scroggo9b77ddd2015-03-19 06:03:39 -0700327
msarett34e0ec42016-04-22 16:27:24 -0700328// Allows us to test decodes to non-native 8888.
msarettb1be46b2016-05-17 08:52:11 -0700329static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700330 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
331 return;
332 }
333
334 for (int y = 0; y < bitmap.height(); y++) {
335 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
336 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
337 }
338}
339
msarettb1be46b2016-05-17 08:52:11 -0700340static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
scroggoba584892016-05-20 13:56:13 -0700341 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
msarett3d9d7a72015-10-21 10:27:10 -0700342 switch (dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700343 case CodecSrc::kGrayscale_Always_DstColorType:
scroggo1a361922016-05-20 14:27:16 -0700344 if (kRGB_565_SkColorType == canvasColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700345 return false;
346 }
scroggoc5560be2016-02-03 09:42:42 -0800347 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700348 break;
msarett34e0ec42016-04-22 16:27:24 -0700349 case CodecSrc::kNonNative8888_Always_DstColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400350 if (kRGB_565_SkColorType == canvasColorType
351 || kRGBA_F16_SkColorType == canvasColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700352 return false;
353 }
354#ifdef SK_PMCOLOR_IS_RGBA
355 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
356#else
357 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
358#endif
359 break;
msarett3d9d7a72015-10-21 10:27:10 -0700360 default:
msarett55f7bdd2016-02-16 13:24:54 -0800361 if (kRGB_565_SkColorType == canvasColorType &&
362 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
363 return false;
364 }
Matt Sarett09a1c082017-02-01 15:34:22 -0800365
scroggoc5560be2016-02-03 09:42:42 -0800366 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700367 break;
368 }
369
scroggoba584892016-05-20 13:56:13 -0700370 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700371 return true;
372}
373
msarettb1be46b2016-05-17 08:52:11 -0700374static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000375 CodecSrc::DstColorType dstColorType,
msarettb1be46b2016-05-17 08:52:11 -0700376 SkScalar left = 0, SkScalar top = 0) {
msarettb1be46b2016-05-17 08:52:11 -0700377 SkBitmap bitmap;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000378 bitmap.installPixels(info, pixels, rowBytes);
msarettb1be46b2016-05-17 08:52:11 -0700379 swap_rb_if_necessary(bitmap, dstColorType);
380 canvas->drawBitmap(bitmap, left, top);
381}
382
Matt Sarettf485cf92017-05-26 08:58:03 -0400383// For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or
384// color format conversions should be performed by the codec. Sometimes the output of the
385// decode will be in an interesting color space. On our srgb and f16 backends, we need to
386// "pretend" that the color space is standard sRGB to avoid triggering color conversion
387// at draw time.
388static void set_bitmap_color_space(SkImageInfo* info) {
Brian Osman6b622962018-08-27 19:16:02 +0000389 *info = info->makeColorSpace(SkColorSpace::MakeSRGB());
Matt Sarettf485cf92017-05-26 08:58:03 -0400390}
391
mtkleine0effd62015-07-29 06:37:28 -0700392Error CodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700393 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800394 if (!encoded) {
395 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
396 }
msarettb714fb02016-01-22 14:46:42 -0800397
Mike Reedede7bac2017-07-23 15:30:02 -0400398 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700399 if (nullptr == codec.get()) {
400 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700401 }
402
scroggoba584892016-05-20 13:56:13 -0700403 SkImageInfo decodeInfo = codec->getInfo();
404 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
405 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800406 return Error::Nonfatal("Skipping uninteresting test.");
msarett438b2ad2015-04-09 12:43:10 -0700407 }
408
msarett0a242972015-06-11 14:27:27 -0700409 // Try to scale the image if it is desired
410 SkISize size = codec->getScaledDimensions(fScale);
411 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
412 return Error::Nonfatal("Test without scaling is uninteresting.");
413 }
msarettb32758a2015-08-18 13:22:46 -0700414
415 // Visually inspecting very small output images is not necessary. We will
416 // cover these cases in unit testing.
417 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
418 return Error::Nonfatal("Scaling very small images is uninteresting.");
419 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400420 decodeInfo = decodeInfo.makeDimensions(size);
msarett0a242972015-06-11 14:27:27 -0700421
Mike Reed7fcfb622018-02-09 13:26:46 -0500422 const int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700423 const size_t rowBytes = size.width() * bpp;
Mike Reedf0ffb892017-10-03 14:47:21 -0400424 const size_t safeSize = decodeInfo.computeByteSize(rowBytes);
scroggo19b91532016-10-24 09:03:26 -0700425 SkAutoMalloc pixels(safeSize);
msarett438b2ad2015-04-09 12:43:10 -0700426
msarettbb25b532016-01-13 09:31:39 -0800427 SkCodec::Options options;
428 if (kCodecZeroInit_Mode == fMode) {
msarettb1be46b2016-05-17 08:52:11 -0700429 memset(pixels.get(), 0, size.height() * rowBytes);
msarettbb25b532016-01-13 09:31:39 -0800430 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
431 }
msarett34e0ec42016-04-22 16:27:24 -0700432
433 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400434 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700435 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
436 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
437 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
438 }
scroggo9b77ddd2015-03-19 06:03:39 -0700439
scroggo9c59ebc2015-03-25 13:48:49 -0700440 switch (fMode) {
scroggo19b91532016-10-24 09:03:26 -0700441 case kAnimated_Mode: {
442 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
443 if (frameInfos.size() <= 1) {
444 return SkStringPrintf("%s is not an animated image.", fPath.c_str());
445 }
446
scroggo90e971a2016-10-25 06:11:01 -0700447 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
448 // into. "factor" is the number of frames to draw on one row. There will be
449 // up to "factor" rows as well.
450 const float root = sqrt((float) frameInfos.size());
451 const int factor = sk_float_ceil2int(root);
452
scroggo19b91532016-10-24 09:03:26 -0700453 // Used to cache a frame that future frames will depend on.
454 SkAutoMalloc priorFramePixels;
Nigel Tao66bc5242018-08-22 10:56:03 +1000455 int cachedFrame = SkCodec::kNoFrame;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400456 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
scroggo19b91532016-10-24 09:03:26 -0700457 options.fFrameIndex = i;
458 // Check for a prior frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400459 const int reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +1000460 if (reqFrame != SkCodec::kNoFrame && reqFrame == cachedFrame
scroggo19b91532016-10-24 09:03:26 -0700461 && priorFramePixels.get()) {
462 // Copy into pixels
463 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400464 options.fPriorFrame = reqFrame;
scroggo19b91532016-10-24 09:03:26 -0700465 } else {
Nigel Tao66bc5242018-08-22 10:56:03 +1000466 options.fPriorFrame = SkCodec::kNoFrame;
scroggo19b91532016-10-24 09:03:26 -0700467 }
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500468 SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000469 rowBytes, &options);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500470 if (SkCodec::kInvalidInput == result && i > 0) {
471 // Some of our test images have truncated later frames. Treat that
472 // the same as incomplete.
473 result = SkCodec::kIncompleteInput;
474 }
scroggo19b91532016-10-24 09:03:26 -0700475 switch (result) {
476 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400477 case SkCodec::kErrorInInput:
scroggo90e971a2016-10-25 06:11:01 -0700478 case SkCodec::kIncompleteInput: {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400479 // If the next frame depends on this one, store it in priorFrame.
480 // It is possible that we may discard a frame that future frames depend on,
481 // but the codec will simply redecode the discarded frame.
482 // Do this before calling draw_to_canvas, which premultiplies in place. If
483 // we're decoding to unpremul, we want to pass the unmodified frame to the
484 // codec for decoding the next frame.
485 if (static_cast<size_t>(i+1) < frameInfos.size()
486 && frameInfos[i+1].fRequiredFrame == i) {
487 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
488 cachedFrame = i;
489 }
490
scroggo90e971a2016-10-25 06:11:01 -0700491 SkAutoCanvasRestore acr(canvas, true);
492 const int xTranslate = (i % factor) * decodeInfo.width();
493 const int yTranslate = (i / factor) * decodeInfo.height();
494 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
Leon Scroggins571b30f2017-07-11 17:35:31 +0000495 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
Leon Scroggins III674a1842017-07-06 12:26:09 -0400496 if (result != SkCodec::kSuccess) {
scroggo19b91532016-10-24 09:03:26 -0700497 return "";
498 }
499 break;
scroggo90e971a2016-10-25 06:11:01 -0700500 }
scroggo53f63b62016-10-27 08:29:13 -0700501 case SkCodec::kInvalidConversion:
Mike Reed304a07c2017-07-12 15:10:28 -0400502 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) {
scroggo53f63b62016-10-27 08:29:13 -0700503 return Error::Nonfatal(SkStringPrintf(
Mike Reed304a07c2017-07-12 15:10:28 -0400504 "Cannot decode frame %i to 565 (%s).", i, fPath.c_str()));
scroggo53f63b62016-10-27 08:29:13 -0700505 }
506 // Fall through.
scroggo19b91532016-10-24 09:03:26 -0700507 default:
508 return SkStringPrintf("Couldn't getPixels for frame %i in %s.",
509 i, fPath.c_str());
510 }
scroggo19b91532016-10-24 09:03:26 -0700511 }
512 break;
513 }
msarettbb25b532016-01-13 09:31:39 -0800514 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700515 case kCodec_Mode: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000516 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggoeb602a52015-07-09 08:16:03 -0700517 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400518 // We consider these to be valid, since we should still decode what is
scroggo9c59ebc2015-03-25 13:48:49 -0700519 // available.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400520 case SkCodec::kErrorInInput:
scroggoeb602a52015-07-09 08:16:03 -0700521 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700522 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700523 default:
524 // Everything else is considered a failure.
525 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
526 }
msarettb1be46b2016-05-17 08:52:11 -0700527
Leon Scroggins571b30f2017-07-11 17:35:31 +0000528 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggo9c59ebc2015-03-25 13:48:49 -0700529 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700530 }
scroggo9c59ebc2015-03-25 13:48:49 -0700531 case kScanline_Mode: {
scroggod8d68552016-06-06 11:26:17 -0700532 void* dst = pixels.get();
533 uint32_t height = decodeInfo.height();
scroggo19b91532016-10-24 09:03:26 -0700534 const bool useIncremental = [this]() {
535 auto exts = { "png", "PNG", "gif", "GIF" };
536 for (auto ext : exts) {
537 if (fPath.endsWith(ext)) {
538 return true;
539 }
540 }
541 return false;
542 }();
543 // ico may use the old scanline method or the new one, depending on whether it
544 // internally holds a bmp or a png.
scroggo8e6c7ad2016-09-16 08:20:38 -0700545 const bool ico = fPath.endsWith("ico");
scroggo19b91532016-10-24 09:03:26 -0700546 bool useOldScanlineMethod = !useIncremental && !ico;
547 if (useIncremental || ico) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700548 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000549 rowBytes, &options)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700550 int rowsDecoded;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400551 auto result = codec->incrementalDecode(&rowsDecoded);
552 if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700553 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
554 SkCodec::kNo_ZeroInitialized, height,
555 rowsDecoded);
scroggo6fb23912016-06-02 14:16:43 -0700556 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700557 } else {
scroggo19b91532016-10-24 09:03:26 -0700558 if (useIncremental) {
559 // Error: These should support incremental decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700560 return "Could not start incremental decode";
561 }
562 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
563 // which should work via startScanlineDecode
564 useOldScanlineMethod = true;
565 }
566 }
567
568 if (useOldScanlineMethod) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000569 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700570 return "Could not start scanline decoder";
571 }
572
Brian Osmanea176c62018-04-06 15:28:23 -0400573 // We do not need to check the return value. On an incomplete
574 // image, memory will be filled with a default value.
575 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700576 }
577
Leon Scroggins571b30f2017-07-11 17:35:31 +0000578 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700579 break;
580 }
msarett0a242972015-06-11 14:27:27 -0700581 case kStripe_Mode: {
582 const int height = decodeInfo.height();
583 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
584 // does not align with image blocks.
585 const int stripeHeight = 37;
586 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
msarettb1be46b2016-05-17 08:52:11 -0700587 void* dst = pixels.get();
msarett0a242972015-06-11 14:27:27 -0700588
589 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000590 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarettb65e6042016-02-23 05:37:25 -0800591 return "Could not start scanline decoder";
592 }
593
594 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
595 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
596 // to run this test for image types that do not have this scanline ordering.
scroggo12e2f502016-05-16 09:04:18 -0700597 // We only run this on Jpeg, which is always kTopDown.
598 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
msarette6dd0042015-10-09 11:07:34 -0700599
msarett0a242972015-06-11 14:27:27 -0700600 for (int i = 0; i < numStripes; i += 2) {
601 // Skip a stripe
Brian Osman788b9162020-02-07 10:36:46 -0500602 const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700603 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700604
605 // Read a stripe
606 const int startY = (i + 1) * stripeHeight;
Brian Osman788b9162020-02-07 10:36:46 -0500607 const int linesToRead = std::min(stripeHeight, height - startY);
msarett0a242972015-06-11 14:27:27 -0700608 if (linesToRead > 0) {
msarettb1be46b2016-05-17 08:52:11 -0700609 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
610 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700611 }
612 }
613
614 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000615 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
scroggo1c005e42015-08-04 09:24:45 -0700616 if (SkCodec::kSuccess != startResult) {
617 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700618 }
619 for (int i = 0; i < numStripes; i += 2) {
620 // Read a stripe
621 const int startY = i * stripeHeight;
Brian Osman788b9162020-02-07 10:36:46 -0500622 const int linesToRead = std::min(stripeHeight, height - startY);
msarettb1be46b2016-05-17 08:52:11 -0700623 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
624 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700625
626 // Skip a stripe
Brian Osman788b9162020-02-07 10:36:46 -0500627 const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight);
msarettf6db27e2015-06-12 09:34:04 -0700628 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700629 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700630 }
631 }
msarettb1be46b2016-05-17 08:52:11 -0700632
Leon Scroggins571b30f2017-07-11 17:35:31 +0000633 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700634 break;
msarett0a242972015-06-11 14:27:27 -0700635 }
msarett91c22b22016-02-22 12:27:46 -0800636 case kCroppedScanline_Mode: {
637 const int width = decodeInfo.width();
638 const int height = decodeInfo.height();
639 // This value is chosen because, as we move across the image, it will sometimes
640 // align with the jpeg block sizes and it will sometimes not. This allows us
641 // to test interestingly different code paths in the implementation.
642 const int tileSize = 36;
msarett91c22b22016-02-22 12:27:46 -0800643 SkIRect subset;
644 for (int x = 0; x < width; x += tileSize) {
Brian Osman788b9162020-02-07 10:36:46 -0500645 subset = SkIRect::MakeXYWH(x, 0, std::min(tileSize, width - x), height);
Matt Saretta40d9c82017-05-19 15:21:05 -0400646 options.fSubset = &subset;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000647 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarett91c22b22016-02-22 12:27:46 -0800648 return "Could not start scanline decoder.";
649 }
650
msarettb1be46b2016-05-17 08:52:11 -0700651 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
msarett91c22b22016-02-22 12:27:46 -0800652 }
653
Leon Scroggins571b30f2017-07-11 17:35:31 +0000654 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800655 break;
656 }
scroggob636b452015-07-22 07:16:20 -0700657 case kSubset_Mode: {
658 // Arbitrarily choose a divisor.
659 int divisor = 2;
660 // Total width/height of the image.
661 const int W = codec->getInfo().width();
662 const int H = codec->getInfo().height();
663 if (divisor > W || divisor > H) {
664 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
665 "for %s with dimensions (%d x %d)", divisor,
666 fPath.c_str(), W, H));
667 }
668 // subset dimensions
669 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
670 const int w = SkAlign2(W / divisor);
671 const int h = SkAlign2(H / divisor);
672 SkIRect subset;
Matt Saretta40d9c82017-05-19 15:21:05 -0400673 options.fSubset = &subset;
scroggob636b452015-07-22 07:16:20 -0700674 SkBitmap subsetBm;
675 // We will reuse pixel memory from bitmap.
msarettb1be46b2016-05-17 08:52:11 -0700676 void* dst = pixels.get();
scroggob636b452015-07-22 07:16:20 -0700677 // Keep track of left and top (for drawing subsetBm into canvas). We could use
678 // fScale * x and fScale * y, but we want integers such that the next subset will start
679 // where the last one ended. So we'll add decodeInfo.width() and height().
680 int left = 0;
681 for (int x = 0; x < W; x += w) {
682 int top = 0;
683 for (int y = 0; y < H; y+= h) {
684 // Do not make the subset go off the edge of the image.
Brian Osman788b9162020-02-07 10:36:46 -0500685 const int preScaleW = std::min(w, W - x);
686 const int preScaleH = std::min(h, H - y);
scroggob636b452015-07-22 07:16:20 -0700687 subset.setXYWH(x, y, preScaleW, preScaleH);
688 // And scale
689 // FIXME: Should we have a version of getScaledDimensions that takes a subset
690 // into account?
Brian Osman788b9162020-02-07 10:36:46 -0500691 const int scaledW = std::max(1, SkScalarRoundToInt(preScaleW * fScale));
692 const int scaledH = std::max(1, SkScalarRoundToInt(preScaleH * fScale));
msarettc7eb4902016-04-25 07:04:58 -0700693 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
694 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
msarettb1be46b2016-05-17 08:52:11 -0700695 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
696 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000697 &options);
scroggob636b452015-07-22 07:16:20 -0700698 switch (result) {
699 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400700 case SkCodec::kErrorInInput:
scroggob636b452015-07-22 07:16:20 -0700701 case SkCodec::kIncompleteInput:
702 break;
scroggob636b452015-07-22 07:16:20 -0700703 default:
704 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
705 "from %s with dimensions (%d x %d)\t error %d",
706 x, y, decodeInfo.width(), decodeInfo.height(),
707 fPath.c_str(), W, H, result);
708 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000709 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType,
710 SkIntToScalar(left), SkIntToScalar(top));
msarettb1be46b2016-05-17 08:52:11 -0700711
scroggob636b452015-07-22 07:16:20 -0700712 // translate by the scaled height.
713 top += decodeInfo.height();
714 }
715 // translate by the scaled width.
716 left += decodeInfo.width();
717 }
718 return "";
719 }
msarettb714fb02016-01-22 14:46:42 -0800720 default:
721 SkASSERT(false);
722 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700723 }
scroggo9c59ebc2015-03-25 13:48:49 -0700724 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700725}
726
727SkISize CodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700728 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400729 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700730 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400731 return {0, 0};
scroggo7fac5af2015-09-30 11:33:12 -0700732 }
scroggo19b91532016-10-24 09:03:26 -0700733
734 auto imageSize = codec->getScaledDimensions(fScale);
735 if (fMode == kAnimated_Mode) {
scroggo90e971a2016-10-25 06:11:01 -0700736 // We'll draw one of each frame, so make it big enough to hold them all
737 // in a grid. The grid will be roughly square, with "factor" frames per
738 // row and up to "factor" rows.
scroggo19b91532016-10-24 09:03:26 -0700739 const size_t count = codec->getFrameInfo().size();
scroggo90e971a2016-10-25 06:11:01 -0700740 const float root = sqrt((float) count);
741 const int factor = sk_float_ceil2int(root);
742 imageSize.fWidth = imageSize.fWidth * factor;
743 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
scroggo19b91532016-10-24 09:03:26 -0700744 }
745 return imageSize;
scroggo9b77ddd2015-03-19 06:03:39 -0700746}
747
748Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700749 if (1.0f == fScale) {
scroggo6e8c68e2016-10-24 13:48:49 -0700750 Name name = SkOSPath::Basename(fPath.c_str());
751 if (fMode == kAnimated_Mode) {
752 name.append("_animated");
753 }
754 return name;
msarett0a242972015-06-11 14:27:27 -0700755 }
scroggo6e8c68e2016-10-24 13:48:49 -0700756 SkASSERT(fMode != kAnimated_Mode);
msaretta5783ae2015-09-08 15:35:32 -0700757 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700758}
759
760/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
761
scroggof8dc9df2016-05-16 09:04:13 -0700762AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800763 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700764 : fPath(path)
msarett3d9d7a72015-10-21 10:27:10 -0700765 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800766 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700767 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800768 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700769{}
770
771bool AndroidCodecSrc::veto(SinkFlags flags) const {
772 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700773 return flags.type != SinkFlags::kRaster
774 || flags.approach != SinkFlags::kDirect;
775}
776
777Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700778 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700779 if (!encoded) {
780 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
781 }
Mike Reedede7bac2017-07-23 15:30:02 -0400782 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
783 if (nullptr == codec) {
msarett3d9d7a72015-10-21 10:27:10 -0700784 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
785 }
786
scroggoba584892016-05-20 13:56:13 -0700787 SkImageInfo decodeInfo = codec->getInfo();
788 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
789 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800790 return Error::Nonfatal("Skipping uninteresting test.");
msarett3d9d7a72015-10-21 10:27:10 -0700791 }
792
793 // Scale the image if it is desired.
794 SkISize size = codec->getSampledDimensions(fSampleSize);
795
796 // Visually inspecting very small output images is not necessary. We will
797 // cover these cases in unit testing.
798 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
799 return Error::Nonfatal("Scaling very small images is uninteresting.");
800 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400801 decodeInfo = decodeInfo.makeDimensions(size);
msarett3d9d7a72015-10-21 10:27:10 -0700802
Mike Reed7fcfb622018-02-09 13:26:46 -0500803 int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700804 size_t rowBytes = size.width() * bpp;
805 SkAutoMalloc pixels(size.height() * rowBytes);
msarett3d9d7a72015-10-21 10:27:10 -0700806
807 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700808 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400809 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700810 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
811 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
812 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
813 }
msarett3d9d7a72015-10-21 10:27:10 -0700814
815 // Create options for the codec.
816 SkAndroidCodec::AndroidOptions options;
msarett3d9d7a72015-10-21 10:27:10 -0700817 options.fSampleSize = fSampleSize;
818
msarettb1be46b2016-05-17 08:52:11 -0700819 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggof8dc9df2016-05-16 09:04:13 -0700820 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400821 case SkCodec::kErrorInInput:
scroggof8dc9df2016-05-16 09:04:13 -0700822 case SkCodec::kIncompleteInput:
823 break;
msarett3d9d7a72015-10-21 10:27:10 -0700824 default:
scroggof8dc9df2016-05-16 09:04:13 -0700825 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700826 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000827 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggof8dc9df2016-05-16 09:04:13 -0700828 return "";
msarett3d9d7a72015-10-21 10:27:10 -0700829}
830
831SkISize AndroidCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700832 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400833 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700834 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400835 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700836 }
837 return codec->getSampledDimensions(fSampleSize);
838}
839
840Name AndroidCodecSrc::name() const {
841 // We will replicate the names used by CodecSrc so that images can
842 // be compared in Gold.
843 if (1 == fSampleSize) {
844 return SkOSPath::Basename(fPath.c_str());
845 }
msarett4b0778e2015-11-13 09:59:11 -0800846 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700847}
848
849/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
850
msarett18976312016-03-09 14:20:58 -0800851ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
852 : fPath(path)
853 , fMode(mode)
854 , fDstAlphaType(alphaType)
855 , fIsGpu(isGpu)
856 , fRunSerially(serial_from_path_name(path))
857{}
858
859bool ImageGenSrc::veto(SinkFlags flags) const {
860 if (fIsGpu) {
Brian Salomonbd7c5512017-03-07 09:08:36 -0500861 // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs.
862 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect ||
863 flags.multisampled == SinkFlags::kMultisampled;
msarett18976312016-03-09 14:20:58 -0800864 }
865
866 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
867}
868
869Error ImageGenSrc::draw(SkCanvas* canvas) const {
870 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
871 return Error::Nonfatal("Uninteresting to test image generator to 565.");
872 }
873
bungeman38d909e2016-08-02 14:40:46 -0700874 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett18976312016-03-09 14:20:58 -0800875 if (!encoded) {
876 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
877 }
878
msarettfc0b6d12016-03-17 13:50:17 -0700879#if defined(SK_BUILD_FOR_WIN)
880 // Initialize COM in order to test with WIC.
881 SkAutoCoInitialize com;
882 if (!com.succeeded()) {
883 return "Could not initialize COM.";
884 }
885#endif
886
Ben Wagner145dbcd2016-11-03 14:40:50 -0400887 std::unique_ptr<SkImageGenerator> gen(nullptr);
msarett18976312016-03-09 14:20:58 -0800888 switch (fMode) {
889 case kCodec_Mode:
Mike Reed185130c2017-02-15 15:14:16 -0500890 gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded);
msarett18976312016-03-09 14:20:58 -0800891 if (!gen) {
892 return "Could not create codec image generator.";
893 }
894 break;
msarettfc0b6d12016-03-17 13:50:17 -0700895 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800896#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -0400897 gen = SkImageGeneratorCG::MakeFromEncodedCG(encoded);
msarettfc0b6d12016-03-17 13:50:17 -0700898#elif defined(SK_BUILD_FOR_WIN)
Leon Scroggins IIIa57488a2018-06-25 14:01:29 -0400899 gen = SkImageGeneratorWIC::MakeFromEncodedWIC(encoded);
msarettfc0b6d12016-03-17 13:50:17 -0700900#endif
msarett18976312016-03-09 14:20:58 -0800901 if (!gen) {
msarettfc0b6d12016-03-17 13:50:17 -0700902 return "Could not create platform image generator.";
msarett18976312016-03-09 14:20:58 -0800903 }
904 break;
msarettfc0b6d12016-03-17 13:50:17 -0700905 }
msarett18976312016-03-09 14:20:58 -0800906 default:
907 SkASSERT(false);
908 return "Invalid image generator mode";
909 }
910
911 // Test deferred decoding path on GPU
912 if (fIsGpu) {
Mike Reed185130c2017-02-15 15:14:16 -0500913 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr));
msarett18976312016-03-09 14:20:58 -0800914 if (!image) {
915 return "Could not create image from codec image generator.";
916 }
917 canvas->drawImage(image, 0, 0);
918 return "";
919 }
mtklein343a63d2016-03-22 11:46:53 -0700920
msarett18976312016-03-09 14:20:58 -0800921 // Test various color and alpha types on CPU
922 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700923
Mike Reed7fcfb622018-02-09 13:26:46 -0500924 int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700925 size_t rowBytes = decodeInfo.width() * bpp;
926 SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
Brian Osmanc87cfb62018-07-11 09:08:46 -0400927 if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes)) {
Matt Sarett05cb4c32017-03-02 12:07:46 -0500928 SkString err =
929 SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str());
930
931#if defined(SK_BUILD_FOR_WIN)
932 if (kPlatform_Mode == fMode) {
933 // Do not issue a fatal error for WIC flakiness.
934 return Error::Nonfatal(err);
935 }
936#endif
937
938 return err;
msarett18976312016-03-09 14:20:58 -0800939 }
940
Matt Sarettf485cf92017-05-26 08:58:03 -0400941 set_bitmap_color_space(&decodeInfo);
Leon Scroggins571b30f2017-07-11 17:35:31 +0000942 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes,
msarettb1be46b2016-05-17 08:52:11 -0700943 CodecSrc::kGetFromCanvas_DstColorType);
msarett18976312016-03-09 14:20:58 -0800944 return "";
945}
946
947SkISize ImageGenSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700948 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400949 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett18976312016-03-09 14:20:58 -0800950 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400951 return {0, 0};
msarett18976312016-03-09 14:20:58 -0800952 }
953 return codec->getInfo().dimensions();
954}
955
956Name ImageGenSrc::name() const {
957 return SkOSPath::Basename(fPath.c_str());
958}
959
960/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
961
Mike Klein0d5d1422019-03-06 10:56:04 -0600962ColorCodecSrc::ColorCodecSrc(Path path, bool decode_to_dst) : fPath(path)
963 , fDecodeToDst(decode_to_dst) {}
msarett69deca82016-04-29 09:38:40 -0700964
965bool ColorCodecSrc::veto(SinkFlags flags) const {
966 // Test to direct raster backends (8888 and 565).
967 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
968}
969
970Error ColorCodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700971 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett69deca82016-04-29 09:38:40 -0700972 if (!encoded) {
973 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
974 }
975
Mike Reedede7bac2017-07-23 15:30:02 -0400976 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
977 if (nullptr == codec) {
msarett69deca82016-04-29 09:38:40 -0700978 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
979 }
980
Mike Klein0d5d1422019-03-06 10:56:04 -0600981 SkImageInfo info = codec->getInfo();
982 if (fDecodeToDst) {
Leon Scroggins III42a604f2020-02-06 15:47:58 -0500983 SkImageInfo canvasInfo = canvas->imageInfo();
984 if (!canvasInfo.colorSpace()) {
985 // This will skip color conversion, and the resulting images will
986 // look different from images they are compared against in Gold, but
987 // that doesn't mean they are wrong. We have a test verifying that
988 // passing a null SkColorSpace skips conversion, so skip this
989 // misleading test.
990 return Error::Nonfatal("Skipping decoding without color transform.");
991 }
992 info = canvasInfo.makeDimensions(info.dimensions());
msarett50ce1f22016-07-29 06:23:33 -0700993 }
994
995 SkBitmap bitmap;
Mike Klein0d5d1422019-03-06 10:56:04 -0600996 if (!bitmap.tryAllocPixels(info)) {
997 return SkStringPrintf("Image(%s) is too large (%d x %d)",
998 fPath.c_str(), info.width(), info.height());
msarett50ce1f22016-07-29 06:23:33 -0700999 }
1000
Mike Klein0d5d1422019-03-06 10:56:04 -06001001 switch (auto r = codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes())) {
Leon Scroggins III674a1842017-07-06 12:26:09 -04001002 case SkCodec::kSuccess:
1003 case SkCodec::kErrorInInput:
1004 case SkCodec::kIncompleteInput:
Mike Klein0d5d1422019-03-06 10:56:04 -06001005 canvas->drawBitmap(bitmap, 0,0);
1006 return "";
1007 case SkCodec::kInvalidConversion:
1008 // TODO(mtklein): why are there formats we can't decode to?
1009 return Error::Nonfatal("SkCodec can't decode to this format.");
Leon Scroggins III674a1842017-07-06 12:26:09 -04001010 default:
1011 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
msarett50ce1f22016-07-29 06:23:33 -07001012 }
msarett69deca82016-04-29 09:38:40 -07001013}
1014
1015SkISize ColorCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001016 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001017 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001018 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001019 return {0, 0};
msarett69deca82016-04-29 09:38:40 -07001020 }
Hal Canaryfafe1352017-04-11 12:12:02 -04001021 return {codec->getInfo().width(), codec->getInfo().height()};
msarett69deca82016-04-29 09:38:40 -07001022}
1023
1024Name ColorCodecSrc::name() const {
1025 return SkOSPath::Basename(fPath.c_str());
1026}
1027
1028/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1029
Mike Klein19fb3972019-03-21 13:08:08 -05001030static DEFINE_int(skpViewportSize, 1000,
1031 "Width & height of the viewport used to crop skp rendering.");
1032
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001033SKPSrc::SKPSrc(Path path) : fPath(path) { }
mtklein748ca3b2015-01-15 10:56:12 -08001034
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001035Error SKPSrc::draw(SkCanvas* canvas) const {
Hal Canary72b91cb2019-01-14 12:47:59 -05001036 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
1037 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001038 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1039 }
Hal Canary72b91cb2019-01-14 12:47:59 -05001040 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
1041 if (!pic) {
1042 return SkStringPrintf("Couldn't parse file %s.", fPath.c_str());
1043 }
1044 stream = nullptr; // Might as well drop this when we're done with it.
Robert Phillips33f02ed2018-03-27 08:06:57 -04001045 canvas->clipRect(SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize));
mtklein748ca3b2015-01-15 10:56:12 -08001046 canvas->drawPicture(pic);
1047 return "";
1048}
1049
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001050static SkRect get_cull_rect_for_skp(const char* path) {
1051 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtkleinffa901a2015-03-16 10:38:07 -07001052 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001053 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001054 }
1055 SkPictInfo info;
Mike Reede7a58322017-12-20 14:09:20 -05001056 if (!SkPicture_StreamIsSKP(stream.get(), &info)) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001057 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001058 }
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001059
1060 return info.fCullRect;
1061}
1062
1063SkISize SKPSrc::size() const {
1064 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
Robert Phillips33f02ed2018-03-27 08:06:57 -04001065 if (!viewport.intersect((SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize)))) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001066 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001067 }
1068 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001069}
1070
1071Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1072
Florin Malitafc043dc2017-12-31 11:08:42 -05001073/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1074
Chris Dalton184c37e2018-09-28 11:27:39 -06001075BisectSrc::BisectSrc(Path path, const char* trail) : INHERITED(path), fTrail(trail) {}
1076
1077Error BisectSrc::draw(SkCanvas* canvas) const {
1078 struct FoundPath {
1079 SkPath fPath;
1080 SkPaint fPaint;
1081 SkMatrix fViewMatrix;
1082 };
1083
1084 // This subclass of SkCanvas just extracts all the SkPaths (drawn via drawPath) from an SKP.
1085 class PathFindingCanvas : public SkCanvas {
1086 public:
1087 PathFindingCanvas(int width, int height) : SkCanvas(width, height, nullptr) {}
1088 const SkTArray<FoundPath>& foundPaths() const { return fFoundPaths; }
1089
1090 private:
1091 void onDrawPath(const SkPath& path, const SkPaint& paint) override {
1092 fFoundPaths.push_back() = {path, paint, this->getTotalMatrix()};
1093 }
1094
1095 SkTArray<FoundPath> fFoundPaths;
1096 };
1097
1098 PathFindingCanvas pathFinder(canvas->getBaseLayerSize().width(),
1099 canvas->getBaseLayerSize().height());
1100 Error err = this->INHERITED::draw(&pathFinder);
1101 if (!err.isEmpty()) {
1102 return err;
1103 }
1104
1105 int start = 0, end = pathFinder.foundPaths().count();
1106 for (const char* ch = fTrail.c_str(); *ch; ++ch) {
1107 int midpt = (start + end) / 2;
1108 if ('l' == *ch) {
1109 start = midpt;
1110 } else if ('r' == *ch) {
1111 end = midpt;
1112 }
1113 }
1114
1115 for (int i = start; i < end; ++i) {
1116 const FoundPath& path = pathFinder.foundPaths()[i];
1117 SkAutoCanvasRestore acr(canvas, true);
1118 canvas->concat(path.fViewMatrix);
1119 canvas->drawPath(path.fPath, path.fPaint);
1120 }
1121
1122 return "";
1123}
1124
1125/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1126
Florin Malita87ccf332018-05-04 12:23:24 -04001127#if defined(SK_ENABLE_SKOTTIE)
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001128SkottieSrc::SkottieSrc(Path path) : fPath(std::move(path)) {}
Florin Malitafc043dc2017-12-31 11:08:42 -05001129
Florin Malita54f65c42018-01-16 17:04:30 -05001130Error SkottieSrc::draw(SkCanvas* canvas) const {
Florin Malitaa8316552018-11-09 16:19:44 -05001131 auto animation = skottie::Animation::Builder()
1132 .setResourceProvider(
Brian Osman849f4d62019-11-26 08:58:26 -05001133 skresources::DataURIResourceProviderProxy::Make(
1134 skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
Florin Malitacfbc4d42019-10-18 13:13:40 -04001135 /*predecode=*/true),
1136 /*predecode=*/true))
Florin Malitaa8316552018-11-09 16:19:44 -05001137 .makeFromFile(fPath.c_str());
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001138 if (!animation) {
1139 return SkStringPrintf("Unable to parse file: %s", fPath.c_str());
Florin Malitafc043dc2017-12-31 11:08:42 -05001140 }
1141
1142 canvas->drawColor(SK_ColorWHITE);
1143
Florin Malitaa33447d2018-05-29 13:46:54 -04001144 const auto t_rate = 1.0f / (kTileCount * kTileCount - 1);
Florin Malitafc043dc2017-12-31 11:08:42 -05001145
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001146 // Draw the frames in a shuffled order to exercise non-linear
1147 // frame progression. The film strip will still be in order left-to-right,
1148 // top-down, just not drawn in that order.
1149 static constexpr int frameOrder[] = { 4, 0, 3, 1, 2 };
1150 static_assert(SK_ARRAY_COUNT(frameOrder) == kTileCount, "");
Florin Malitad3c1b842018-01-27 12:43:24 -05001151
Florin Malitafc043dc2017-12-31 11:08:42 -05001152 for (int i = 0; i < kTileCount; ++i) {
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001153 const SkScalar y = frameOrder[i] * kTileSize;
Florin Malitafc043dc2017-12-31 11:08:42 -05001154
1155 for (int j = 0; j < kTileCount; ++j) {
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001156 const SkScalar x = frameOrder[j] * kTileSize;
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001157 SkRect dest = SkRect::MakeXYWH(x, y, kTileSize, kTileSize);
Florin Malitafc043dc2017-12-31 11:08:42 -05001158
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001159 const auto t = t_rate * (frameOrder[i] * kTileCount + frameOrder[j]);
Florin Malitadf2713c2018-01-09 15:51:21 -05001160 {
1161 SkAutoCanvasRestore acr(canvas, true);
Florin Malitad3c1b842018-01-27 12:43:24 -05001162 canvas->clipRect(dest, true);
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001163 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
Florin Malitadf2713c2018-01-09 15:51:21 -05001164 dest,
Florin Malita3fae0f32018-03-15 21:52:54 -04001165 SkMatrix::kCenter_ScaleToFit));
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001166 animation->seek(t);
1167 animation->render(canvas);
Florin Malitadf2713c2018-01-09 15:51:21 -05001168 }
Florin Malitafc043dc2017-12-31 11:08:42 -05001169 }
1170 }
1171
1172 return "";
1173}
1174
Florin Malita54f65c42018-01-16 17:04:30 -05001175SkISize SkottieSrc::size() const {
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001176 return SkISize::Make(kTargetSize, kTargetSize);
Florin Malitafc043dc2017-12-31 11:08:42 -05001177}
1178
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001179Name SkottieSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
Florin Malitafc043dc2017-12-31 11:08:42 -05001180
Florin Malita54f65c42018-01-16 17:04:30 -05001181bool SkottieSrc::veto(SinkFlags flags) const {
Florin Malitafc043dc2017-12-31 11:08:42 -05001182 // No need to test to non-(raster||gpu||vector) or indirect backends.
1183 bool type_ok = flags.type == SinkFlags::kRaster
1184 || flags.type == SinkFlags::kGPU
1185 || flags.type == SinkFlags::kVector;
1186
1187 return !type_ok || flags.approach != SinkFlags::kDirect;
1188}
Florin Malita124d5af2017-12-31 17:02:26 -05001189#endif
Florin Malitafc043dc2017-12-31 11:08:42 -05001190
1191/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fmalitaa2b9fdf2016-08-03 19:53:36 -07001192#if defined(SK_XML)
fmalitabdf3e5c2016-09-17 07:26:26 -07001193// Used when the image doesn't have an intrinsic size.
Hal Canaryfafe1352017-04-11 12:12:02 -04001194static const SkSize kDefaultSVGSize = {1000, 1000};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001195
fmalitabdf3e5c2016-09-17 07:26:26 -07001196// Used to force-scale tiny fixed-size images.
Hal Canaryfafe1352017-04-11 12:12:02 -04001197static const SkSize kMinimumSVGSize = {128, 128};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001198
fmalitaacd2f5c2016-11-08 07:13:45 -08001199SVGSrc::SVGSrc(Path path)
1200 : fName(SkOSPath::Basename(path.c_str()))
1201 , fScale(1) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001202
Brian Osman133823d2018-09-19 14:14:15 -04001203 sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
1204 if (!data) {
1205 return;
1206 }
fmalitaacd2f5c2016-11-08 07:13:45 -08001207
Brian Osman133823d2018-09-19 14:14:15 -04001208 SkMemoryStream stream(std::move(data));
1209 fDom = SkSVGDOM::MakeFromStream(stream);
1210 if (!fDom) {
1211 return;
1212 }
1213
1214 const SkSize& sz = fDom->containerSize();
1215 if (sz.isEmpty()) {
1216 // no intrinsic size
1217 fDom->setContainerSize(kDefaultSVGSize);
1218 } else {
Brian Osman788b9162020-02-07 10:36:46 -05001219 fScale = std::max(1.f, std::max(kMinimumSVGSize.width() / sz.width(),
Brian Osman133823d2018-09-19 14:14:15 -04001220 kMinimumSVGSize.height() / sz.height()));
1221 }
fmalitaacd2f5c2016-11-08 07:13:45 -08001222}
1223
1224Error SVGSrc::draw(SkCanvas* canvas) const {
fmalitabdf3e5c2016-09-17 07:26:26 -07001225 if (!fDom) {
fmalitaacd2f5c2016-11-08 07:13:45 -08001226 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
fmalitaa2b9fdf2016-08-03 19:53:36 -07001227 }
1228
fmalitaacd2f5c2016-11-08 07:13:45 -08001229 SkAutoCanvasRestore acr(canvas, true);
1230 canvas->scale(fScale, fScale);
1231 fDom->render(canvas);
1232
fmalitaa2b9fdf2016-08-03 19:53:36 -07001233 return "";
1234}
1235
1236SkISize SVGSrc::size() const {
fmalitaacd2f5c2016-11-08 07:13:45 -08001237 if (!fDom) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001238 return {0, 0};
fmalitabdf3e5c2016-09-17 07:26:26 -07001239 }
1240
Hal Canaryfafe1352017-04-11 12:12:02 -04001241 return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale}
1242 .toRound();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001243}
1244
fmalitaacd2f5c2016-11-08 07:13:45 -08001245Name SVGSrc::name() const { return fName; }
fmalitaa2b9fdf2016-08-03 19:53:36 -07001246
fmalita179d8852016-08-16 14:23:29 -07001247bool SVGSrc::veto(SinkFlags flags) const {
Florin Malita93323eb2017-06-16 15:31:17 -04001248 // No need to test to non-(raster||gpu||vector) or indirect backends.
fmalita179d8852016-08-16 14:23:29 -07001249 bool type_ok = flags.type == SinkFlags::kRaster
Florin Malita93323eb2017-06-16 15:31:17 -04001250 || flags.type == SinkFlags::kGPU
1251 || flags.type == SinkFlags::kVector;
fmalita179d8852016-08-16 14:23:29 -07001252
1253 return !type_ok || flags.approach != SinkFlags::kDirect;
1254}
1255
fmalitaa2b9fdf2016-08-03 19:53:36 -07001256#endif // defined(SK_XML)
1257/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein748ca3b2015-01-15 10:56:12 -08001258
halcanary45420a92016-06-02 12:41:14 -07001259MSKPSrc::MSKPSrc(Path path) : fPath(path) {
bungemanf93d7112016-09-16 06:24:20 -07001260 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001261 int count = SkMultiPictureDocumentReadPageCount(stream.get());
1262 if (count > 0) {
1263 fPages.reset(count);
1264 (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count());
1265 }
halcanary45420a92016-06-02 12:41:14 -07001266}
1267
Hal Canary45cde312017-04-03 16:06:42 -04001268int MSKPSrc::pageCount() const { return fPages.count(); }
halcanary45420a92016-06-02 12:41:14 -07001269
1270SkISize MSKPSrc::size() const { return this->size(0); }
Hal Canary45cde312017-04-03 16:06:42 -04001271SkISize MSKPSrc::size(int i) const {
Hal Canaryfafe1352017-04-11 12:12:02 -04001272 return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0};
Hal Canary45cde312017-04-03 16:06:42 -04001273}
halcanary45420a92016-06-02 12:41:14 -07001274
1275Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); }
1276Error MSKPSrc::draw(int i, SkCanvas* canvas) const {
Hal Canary45cde312017-04-03 16:06:42 -04001277 if (this->pageCount() == 0) {
halcanary45420a92016-06-02 12:41:14 -07001278 return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
1279 }
Hal Canary45cde312017-04-03 16:06:42 -04001280 if (i >= fPages.count() || i < 0) {
halcanary45420a92016-06-02 12:41:14 -07001281 return SkStringPrintf("MultiPictureDocument page number out of range: %d", i);
1282 }
Hal Canary45cde312017-04-03 16:06:42 -04001283 SkPicture* page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001284 if (!page) {
Hal Canary45cde312017-04-03 16:06:42 -04001285 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1286 if (!stream) {
1287 return SkStringPrintf("Unable to open file: %s", fPath.c_str());
1288 }
1289 if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) {
1290 return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i,
1291 fPath.c_str());
1292 }
1293 page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001294 }
1295 canvas->drawPicture(page);
1296 return "";
1297}
1298
1299Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1300
1301/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1302
mtkleinad66f9b2015-02-13 15:11:10 -08001303Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
Mike Reed5df49342016-11-12 08:06:55 -06001304 return src.draw(SkMakeNullCanvas().get());
mtkleinad66f9b2015-02-13 15:11:10 -08001305}
1306
1307/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1308
Brian Osmane5756ec2017-09-06 17:08:30 -04001309static Error compare_bitmaps(const SkBitmap& reference, const SkBitmap& bitmap) {
1310 // The dimensions are a property of the Src only, and so should be identical.
Mike Reedf0ffb892017-10-03 14:47:21 -04001311 SkASSERT(reference.computeByteSize() == bitmap.computeByteSize());
1312 if (reference.computeByteSize() != bitmap.computeByteSize()) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001313 return "Dimensions don't match reference";
1314 }
1315 // All SkBitmaps in DM are tight, so this comparison is easy.
Mike Reedf0ffb892017-10-03 14:47:21 -04001316 if (0 != memcmp(reference.getPixels(), bitmap.getPixels(), reference.computeByteSize())) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001317 SkString encoded;
1318 SkString errString("Pixels don't match reference");
Brian Salomon28a8f282019-10-24 20:07:39 -04001319 if (BipmapToBase64DataURI(reference, &encoded)) {
Michael Ludwige8e10752018-10-01 12:42:53 -04001320 errString.append("\nExpected: ");
Brian Osmane5756ec2017-09-06 17:08:30 -04001321 errString.append(encoded);
1322 } else {
1323 errString.append("\nExpected image failed to encode: ");
1324 errString.append(encoded);
1325 }
Brian Salomon28a8f282019-10-24 20:07:39 -04001326 if (BipmapToBase64DataURI(bitmap, &encoded)) {
Michael Ludwige8e10752018-10-01 12:42:53 -04001327 errString.append("\nActual: ");
Brian Osmane5756ec2017-09-06 17:08:30 -04001328 errString.append(encoded);
1329 } else {
1330 errString.append("\nActual image failed to encode: ");
1331 errString.append(encoded);
1332 }
1333 return errString;
1334 }
1335 return "";
1336}
1337
1338/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1339
Mike Klein84836b72019-03-21 11:31:36 -05001340static DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
Mike Klein19fb3972019-03-21 13:08:08 -05001341static DEFINE_bool(preAbandonGpuContext, false,
1342 "Test abandoning the GrContext before running the test.");
1343static DEFINE_bool(abandonGpuContext, false,
1344 "Test abandoning the GrContext after running each test.");
1345static DEFINE_bool(releaseAndAbandonGpuContext, false,
1346 "Test releasing all gpu resources and abandoning the GrContext "
1347 "after running each test");
1348static DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
Brian Osman9c310472019-06-27 16:43:27 -04001349static DEFINE_bool(programBinaryCache, true, "Use in-memory program binary cache");
mtkleinb9eb4ac2015-02-02 18:26:03 -08001350
Brian Osman3fdfe282019-09-09 13:46:52 -04001351GPUSink::GPUSink(const SkCommandLineConfigGpu* config,
Brian Osmanf21aa042017-08-21 16:48:46 -04001352 const GrContextOptions& grCtxOptions)
Brian Osman3fdfe282019-09-09 13:46:52 -04001353 : fContextType(config->getContextType())
1354 , fContextOverrides(config->getContextOverrides())
1355 , fSurfType(config->getSurfType())
1356 , fSampleCount(config->getSamples())
1357 , fUseDIText(config->getUseDIText())
1358 , fColorType(config->getColorType())
1359 , fAlphaType(config->getAlphaType())
1360 , fColorSpace(sk_ref_sp(config->getColorSpace()))
Brian Osman9c310472019-06-27 16:43:27 -04001361 , fBaseContextOptions(grCtxOptions) {
1362 if (FLAGS_programBinaryCache) {
1363 fBaseContextOptions.fPersistentCache = &fMemoryCache;
1364 }
1365}
mtklein748ca3b2015-01-15 10:56:12 -08001366
Brian Osmanf9810662017-08-30 10:02:10 -04001367Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const {
1368 return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
1369}
1370
1371Error GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
Brian Osmaned58e002019-09-06 14:42:43 -04001372 const GrContextOptions& baseOptions,
1373 std::function<void(GrContext*)> initContext) const {
Brian Osmanf9810662017-08-30 10:02:10 -04001374 GrContextOptions grOptions = baseOptions;
kkinnunen64492c42015-12-08 01:24:40 -08001375
Brian Salomon00a5eb82018-07-11 15:32:05 -04001376 // We don't expect the src to mess with the persistent cache or the executor.
1377 SkDEBUGCODE(auto cache = grOptions.fPersistentCache);
1378 SkDEBUGCODE(auto exec = grOptions.fExecutor);
kkinnunen5219fd92015-12-10 06:28:13 -08001379 src.modifyGrContextOptions(&grOptions);
Brian Salomon00a5eb82018-07-11 15:32:05 -04001380 SkASSERT(cache == grOptions.fPersistentCache);
1381 SkASSERT(exec == grOptions.fExecutor);
kkinnunen5219fd92015-12-10 06:28:13 -08001382
1383 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -08001384 const SkISize size = src.size();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001385 SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
Brian Salomonf865b052018-03-09 09:01:53 -05001386 sk_sp<SkSurface> surface;
csmartdaltone812d492017-02-21 12:36:05 -07001387 GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext();
Brian Osmaned58e002019-09-06 14:42:43 -04001388 if (initContext) {
1389 initContext(context);
1390 }
Robert Phillips9da87e02019-02-04 13:26:26 -05001391 const int maxDimension = context->priv().caps()->maxTextureSize();
Brian Osman788b9162020-02-07 10:36:46 -05001392 if (maxDimension < std::max(size.width(), size.height())) {
msarett13a036b2016-02-08 09:10:47 -08001393 return Error::Nonfatal("Src too large to create a texture.\n");
1394 }
Brian Salomonf865b052018-03-09 09:01:53 -05001395 uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
1396 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1397 GrBackendTexture backendTexture;
1398 GrBackendRenderTarget backendRT;
1399 switch (fSurfType) {
1400 case SkCommandLineConfigGpu::SurfType::kDefault:
1401 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
1402 &props);
1403 break;
1404 case SkCommandLineConfigGpu::SurfType::kBackendTexture:
Robert Phillips4bdd36f2019-06-04 11:03:06 -04001405 backendTexture = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -04001406 info.width(), info.height(), info.colorType(), SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -04001407 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
Brian Salomonf865b052018-03-09 09:01:53 -05001408 surface = SkSurface::MakeFromBackendTexture(context, backendTexture,
1409 kTopLeft_GrSurfaceOrigin, fSampleCount,
Brian Salomon53706772018-03-19 14:18:08 -04001410 fColorType, info.refColorSpace(), &props);
Brian Salomonf865b052018-03-09 09:01:53 -05001411 break;
1412 case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
1413 if (1 == fSampleCount) {
Brian Salomonf865b052018-03-09 09:01:53 -05001414 auto colorType = SkColorTypeToGrColorType(info.colorType());
Robert Phillips9da87e02019-02-04 13:26:26 -05001415 backendRT = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
Brian Osman2d010b62018-08-09 10:55:09 -04001416 info.width(), info.height(), colorType);
Brian Salomon49edccd2018-03-23 15:31:32 -04001417 surface = SkSurface::MakeFromBackendRenderTarget(
1418 context, backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(),
1419 info.refColorSpace(), &props);
Brian Salomonf865b052018-03-09 09:01:53 -05001420 }
1421 break;
1422 }
msarett13a036b2016-02-08 09:10:47 -08001423
mtklein748ca3b2015-01-15 10:56:12 -08001424 if (!surface) {
1425 return "Could not create a surface.";
1426 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001427 if (FLAGS_preAbandonGpuContext) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001428 factory.abandonContexts();
1429 }
mtklein748ca3b2015-01-15 10:56:12 -08001430 SkCanvas* canvas = surface->getCanvas();
1431 Error err = src.draw(canvas);
1432 if (!err.isEmpty()) {
1433 return err;
1434 }
Robert Phillips9882dae2019-03-04 11:00:10 -05001435 surface->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001436 if (FLAGS_gpuStats) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001437 canvas->getGrContext()->priv().dumpCacheStats(log);
1438 canvas->getGrContext()->priv().dumpGpuStats(log);
mtkleinb9eb4ac2015-02-02 18:26:03 -08001439 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -04001440 if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType ||
1441 info.colorType() == kRGB_888x_SkColorType) {
Brian Salomonce5ee602017-07-17 11:31:31 -04001442 // We don't currently support readbacks into these formats on the GPU backend. Convert to
1443 // 32 bit.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001444 info = SkImageInfo::Make(size, kRGBA_8888_SkColorType, kPremul_SkAlphaType, fColorSpace);
Brian Salomonce5ee602017-07-17 11:31:31 -04001445 }
mtklein748ca3b2015-01-15 10:56:12 -08001446 dst->allocPixels(info);
Mike Reed12e946b2017-04-17 10:53:29 -04001447 canvas->readPixels(*dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001448 if (FLAGS_abandonGpuContext) {
1449 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001450 } else if (FLAGS_releaseAndAbandonGpuContext) {
1451 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001452 }
Khushalc421ca12018-06-26 14:38:34 -07001453 if (!context->abandoned()) {
Brian Salomonf865b052018-03-09 09:01:53 -05001454 surface.reset();
1455 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001456 context->deleteBackendTexture(backendTexture);
Brian Salomonf865b052018-03-09 09:01:53 -05001457 }
1458 if (backendRT.isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001459 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
Brian Salomonf865b052018-03-09 09:01:53 -05001460 }
1461 }
Greg Daniela870b462019-01-08 15:49:46 -05001462 if (grOptions.fPersistentCache) {
1463 context->storeVkPipelineCacheData();
1464 }
mtklein748ca3b2015-01-15 10:56:12 -08001465 return "";
1466}
1467
1468/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1469
Brian Osman3fdfe282019-09-09 13:46:52 -04001470GPUThreadTestingSink::GPUThreadTestingSink(const SkCommandLineConfigGpu* config,
Brian Osmanf9810662017-08-30 10:02:10 -04001471 const GrContextOptions& grCtxOptions)
Brian Osman3fdfe282019-09-09 13:46:52 -04001472 : INHERITED(config, grCtxOptions)
Mike Klein022cfa22017-09-01 11:53:16 -04001473 , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)) {
Brian Osmanf9810662017-08-30 10:02:10 -04001474 SkASSERT(fExecutor);
1475}
1476
1477Error GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1478 SkString* log) const {
1479 // Draw twice, once with worker threads, and once without. Verify that we get the same result.
1480 // Also, force us to only use the software path renderer, so we really stress-test the threaded
1481 // version of that code.
1482 GrContextOptions contextOptions = this->baseContextOptions();
Brian Osman195c05b2017-08-30 15:14:04 -04001483 contextOptions.fGpuPathRenderers = GpuPathRenderers::kNone;
Greg Daniel024615e2018-05-10 17:25:52 +00001484 contextOptions.fExecutor = fExecutor.get();
Adrienne Walkerab7181d2018-05-14 14:02:03 -07001485
Brian Osmanf9810662017-08-30 10:02:10 -04001486 Error err = this->onDraw(src, dst, wStream, log, contextOptions);
1487 if (!err.isEmpty() || !dst) {
1488 return err;
1489 }
1490
1491 SkBitmap reference;
1492 SkString refLog;
1493 SkDynamicMemoryWStream refStream;
1494 contextOptions.fExecutor = nullptr;
1495 Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1496 if (!refErr.isEmpty()) {
1497 return refErr;
1498 }
1499
Brian Osmane5756ec2017-09-06 17:08:30 -04001500 return compare_bitmaps(reference, *dst);
Brian Osmanf9810662017-08-30 10:02:10 -04001501}
1502
1503/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1504
Brian Osman3fdfe282019-09-09 13:46:52 -04001505GPUPersistentCacheTestingSink::GPUPersistentCacheTestingSink(const SkCommandLineConfigGpu* config,
1506 const GrContextOptions& grCtxOptions)
1507 : INHERITED(config, grCtxOptions)
1508 , fCacheType(config->getTestPersistentCache()) {}
Brian Salomon00a5eb82018-07-11 15:32:05 -04001509
1510Error GPUPersistentCacheTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1511 SkString* log) const {
1512 // Draw twice, once with a cold cache, and again with a warm cache. Verify that we get the same
1513 // result.
1514 sk_gpu_test::MemoryCache memoryCache;
1515 GrContextOptions contextOptions = this->baseContextOptions();
1516 contextOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -04001517 if (fCacheType == 2) {
1518 contextOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
1519 }
Brian Osmane4c88bb2019-06-27 16:15:11 -04001520 // anglebug.com/3619
1521 contextOptions.fGpuPathRenderers =
1522 contextOptions.fGpuPathRenderers & ~GpuPathRenderers::kStencilAndCover;
Brian Salomon00a5eb82018-07-11 15:32:05 -04001523
1524 Error err = this->onDraw(src, dst, wStream, log, contextOptions);
1525 if (!err.isEmpty() || !dst) {
1526 return err;
1527 }
1528
1529 SkBitmap reference;
1530 SkString refLog;
1531 SkDynamicMemoryWStream refStream;
1532 memoryCache.resetNumCacheMisses();
1533 Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1534 if (!refErr.isEmpty()) {
1535 return refErr;
1536 }
Brian Salomon7fc52992018-07-12 16:20:23 -04001537 SkASSERT(!memoryCache.numCacheMisses());
Brian Salomon00a5eb82018-07-11 15:32:05 -04001538
1539 return compare_bitmaps(reference, *dst);
1540}
1541
Brian Osmaned58e002019-09-06 14:42:43 -04001542
1543/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1544
Brian Osman3fdfe282019-09-09 13:46:52 -04001545GPUPrecompileTestingSink::GPUPrecompileTestingSink(const SkCommandLineConfigGpu* config,
1546 const GrContextOptions& grCtxOptions)
1547 : INHERITED(config, grCtxOptions) {}
Brian Osmaned58e002019-09-06 14:42:43 -04001548
1549Error GPUPrecompileTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1550 SkString* log) const {
1551 // Three step process:
1552 // 1) Draw once with an SkSL cache, and store off the shader blobs.
1553 // 2) For the second context, pre-compile the shaders to warm the cache.
1554 // 3) Draw with the second context, ensuring that we get the same result, and no cache misses.
1555 sk_gpu_test::MemoryCache memoryCache;
1556 GrContextOptions contextOptions = this->baseContextOptions();
1557 contextOptions.fPersistentCache = &memoryCache;
1558 contextOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL;
1559 // anglebug.com/3619 means that we don't cache shaders when we're using NVPR. That prevents
1560 // the precompile from working, so we'll trigger the assert at the end of this test.
1561 contextOptions.fGpuPathRenderers =
1562 contextOptions.fGpuPathRenderers & ~GpuPathRenderers::kStencilAndCover;
1563
1564 Error err = this->onDraw(src, dst, wStream, log, contextOptions);
1565 if (!err.isEmpty() || !dst) {
1566 return err;
1567 }
1568
1569 auto precompileShaders = [&memoryCache](GrContext* context) {
1570 memoryCache.foreach([context](sk_sp<const SkData> key, sk_sp<SkData> data, int /*count*/) {
1571 SkAssertResult(context->precompileShader(*key, *data));
1572 });
1573 };
1574
1575 sk_gpu_test::MemoryCache replayCache;
1576 GrContextOptions replayOptions = this->baseContextOptions();
1577 // Ensure that the runtime cache is large enough to hold all of the shaders we pre-compile
1578 replayOptions.fRuntimeProgramCacheSize = memoryCache.numCacheMisses();
1579 replayOptions.fPersistentCache = &replayCache;
1580 // anglebug.com/3619
1581 replayOptions.fGpuPathRenderers =
1582 replayOptions.fGpuPathRenderers & ~GpuPathRenderers::kStencilAndCover;
1583
1584 SkBitmap reference;
1585 SkString refLog;
1586 SkDynamicMemoryWStream refStream;
1587 Error refErr = this->onDraw(src, &reference, &refStream, &refLog, replayOptions,
1588 precompileShaders);
1589 if (!refErr.isEmpty()) {
1590 return refErr;
1591 }
1592 SkASSERT(!replayCache.numCacheMisses());
1593
1594 return compare_bitmaps(reference, *dst);
1595}
1596
Brian Salomon00a5eb82018-07-11 15:32:05 -04001597/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
halcanary47ef4d52015-03-03 09:13:09 -08001598static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001599 if (src.size().isEmpty()) {
1600 return "Source has empty dimensions";
1601 }
halcanary47ef4d52015-03-03 09:13:09 -08001602 SkASSERT(doc);
halcanary45420a92016-06-02 12:41:14 -07001603 int pageCount = src.pageCount();
1604 for (int i = 0; i < pageCount; ++i) {
1605 int width = src.size(i).width(), height = src.size(i).height();
halcanary7e798182015-04-14 14:06:18 -07001606 SkCanvas* canvas =
1607 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1608 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001609 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001610 }
halcanary45420a92016-06-02 12:41:14 -07001611 Error err = src.draw(i, canvas);
halcanary7e798182015-04-14 14:06:18 -07001612 if (!err.isEmpty()) {
1613 return err;
1614 }
1615 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001616 }
reedd14df7c2016-09-22 14:12:46 -07001617 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -08001618 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001619 return "";
1620}
1621
halcanary47ef4d52015-03-03 09:13:09 -08001622Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canary23564b92018-09-07 14:33:14 -04001623 SkPDF::Metadata metadata;
halcanary4b656662016-04-27 07:45:18 -07001624 metadata.fTitle = src.name();
1625 metadata.fSubject = "rendering correctness test";
1626 metadata.fCreator = "Skia/DM";
Mike Reeda4daf192017-12-14 13:25:04 -05001627 metadata.fRasterDPI = fRasterDpi;
1628 metadata.fPDFA = fPDFA;
Hal Canaryf34131a2018-12-18 15:11:03 -05001629#if SK_PDF_TEST_EXECUTOR
Hal Canary9a3f5542018-12-10 19:59:07 -05001630 std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool();
1631 metadata.fExecutor = executor.get();
1632#endif
Hal Canary3026d4b2019-01-07 10:00:48 -05001633 auto doc = SkPDF::MakeDocument(dst, metadata);
halcanary47ef4d52015-03-03 09:13:09 -08001634 if (!doc) {
Hal Canary23564b92018-09-07 14:33:14 -04001635 return "SkPDF::MakeDocument() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001636 }
1637 return draw_skdocument(src, doc.get(), dst);
1638}
1639
1640/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1641
1642XPSSink::XPSSink() {}
1643
Hal Canaryabc88d22017-02-06 09:26:49 -05001644#ifdef SK_BUILD_FOR_WIN
Hal Canary5e221e72017-02-06 09:51:42 -05001645static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() {
1646 IXpsOMObjectFactory* factory;
1647 HRN(CoCreateInstance(CLSID_XpsOMObjectFactory,
1648 nullptr,
1649 CLSCTX_INPROC_SERVER,
1650 IID_PPV_ARGS(&factory)));
1651 return SkTScopedComPtr<IXpsOMObjectFactory>(factory);
1652}
1653
1654Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canaryabc88d22017-02-06 09:26:49 -05001655 SkAutoCoInitialize com;
1656 if (!com.succeeded()) {
1657 return "Could not initialize COM.";
1658 }
Hal Canary5e221e72017-02-06 09:51:42 -05001659 SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory();
1660 if (!factory) {
1661 return "Failed to create XPS Factory.";
1662 }
Hal Canary3026d4b2019-01-07 10:00:48 -05001663 auto doc = SkXPS::MakeDocument(dst, factory.get());
halcanary47ef4d52015-03-03 09:13:09 -08001664 if (!doc) {
Hal Canary3026d4b2019-01-07 10:00:48 -05001665 return "SkXPS::MakeDocument() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001666 }
1667 return draw_skdocument(src, doc.get(), dst);
1668}
Hal Canary5e221e72017-02-06 09:51:42 -05001669#else
1670Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1671 return "XPS not supported on this platform.";
1672}
1673#endif
reed54dc4872016-09-13 08:09:45 -07001674
1675/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1676
mtklein9c3f17d2015-01-28 11:35:18 -08001677SKPSink::SKPSink() {}
1678
mtkleinb9eb4ac2015-02-02 18:26:03 -08001679Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Brian Salomon4e1066f2019-12-04 10:33:52 -05001680 auto size = SkSize::Make(src.size());
mtklein9c3f17d2015-01-28 11:35:18 -08001681 SkPictureRecorder recorder;
1682 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1683 if (!err.isEmpty()) {
1684 return err;
1685 }
reedca2622b2016-03-18 07:25:55 -07001686 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001687 return "";
1688}
1689
1690/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1691
Hal Canary85c7fe82016-10-25 10:33:27 -04001692Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Mike Klein8f4e2242019-03-20 11:59:00 -05001693 DebugCanvas debugCanvas(src.size().width(), src.size().height());
Hal Canary85c7fe82016-10-25 10:33:27 -04001694 Error err = src.draw(&debugCanvas);
1695 if (!err.isEmpty()) {
1696 return err;
1697 }
Mike Reed5df49342016-11-12 08:06:55 -06001698 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
Hal Canary85c7fe82016-10-25 10:33:27 -04001699 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05001700 SkJSONWriter writer(dst, SkJSONWriter::Mode::kPretty);
1701 writer.beginObject(); // root
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -05001702 debugCanvas.toJSON(writer, dataManager, nullCanvas.get());
Brian Osmand8a90f92019-01-28 13:41:19 -05001703 writer.endObject(); // root
1704 writer.flush();
1705 return "";
Hal Canary85c7fe82016-10-25 10:33:27 -04001706}
1707
1708/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1709
Bryce Thomas95a7b762018-03-02 13:54:21 -08001710SVGSink::SVGSink(int pageIndex) : fPageIndex(pageIndex) {}
mtklein8a4527e2015-01-31 20:00:58 -08001711
mtkleinb9eb4ac2015-02-02 18:26:03 -08001712Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita718df0a2016-07-15 10:33:29 -07001713#if defined(SK_XML)
Bryce Thomas95a7b762018-03-02 13:54:21 -08001714 if (src.pageCount() > 1) {
1715 int pageCount = src.pageCount();
1716 if (fPageIndex > pageCount - 1) {
1717 return Error(SkStringPrintf("Page index %d too high for document with only %d pages.",
1718 fPageIndex, pageCount));
1719 }
1720 }
Bryce Thomas95a7b762018-03-02 13:54:21 -08001721 return src.draw(fPageIndex,
1722 SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
Mike Reed5df49342016-11-12 08:06:55 -06001723 SkIntToScalar(src.size().height())),
Florin Malita562017b2019-02-14 13:42:15 -05001724 dst)
Bryce Thomas95a7b762018-03-02 13:54:21 -08001725 .get());
fmalita718df0a2016-07-15 10:33:29 -07001726#else
Hal Canary327ef032018-03-22 13:10:51 -04001727 (void)fPageIndex;
fmalita718df0a2016-07-15 10:33:29 -07001728 return Error("SVG sink is disabled.");
1729#endif // SK_XML
mtklein8a4527e2015-01-31 20:00:58 -08001730}
1731
1732/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1733
brianosmanb109b8c2016-06-16 13:03:24 -07001734RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
mtklein27c3fdd2016-02-26 14:43:21 -08001735 : fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001736 , fColorSpace(std::move(colorSpace)) {}
mtklein748ca3b2015-01-15 10:56:12 -08001737
Mike Klein48b64902018-07-25 13:28:44 -04001738Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001739 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001740 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1741 SkAlphaType alphaType = kPremul_SkAlphaType;
1742 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1743
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001744 dst->allocPixelsFlags(SkImageInfo::Make(size, fColorType, alphaType, fColorSpace),
Mike Reed086a4272017-07-18 10:53:11 -04001745 SkBitmap::kZeroPixels_AllocFlag);
Yuqian Lib8b62532018-02-23 14:13:36 +08001746
mtklein748ca3b2015-01-15 10:56:12 -08001747 SkCanvas canvas(*dst);
1748 return src.draw(&canvas);
1749}
1750
1751/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1752
mtkleina16e69e2015-05-05 11:38:45 -07001753// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001754// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001755// Several examples below.
1756
mtkleincbf89782016-02-19 14:27:14 -08001757template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001758static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001759 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001760 class ProxySrc : public Src {
1761 public:
mtkleincbf89782016-02-19 14:27:14 -08001762 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001763 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001764 Name name() const override { return "ProxySrc"; }
1765 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001766 private:
mtkleincbf89782016-02-19 14:27:14 -08001767 SkISize fSize;
1768 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001769 };
msarett62d3b102015-12-10 15:14:27 -08001770 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001771}
1772
1773/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1774
Mike Klein84836b72019-03-21 11:31:36 -05001775static DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
mtklein4a34ecb2016-01-08 10:19:35 -08001776
1777// Is *bitmap identical to what you get drawing src into sink?
1778static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1779 // We can only check raster outputs.
1780 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1781 if (FLAGS_check && bitmap) {
1782 SkBitmap reference;
1783 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001784 SkDynamicMemoryWStream wStream;
1785 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001786 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1787 SkASSERT(err.isEmpty());
1788 if (!err.isEmpty()) {
1789 return err;
1790 }
Brian Osmane5756ec2017-09-06 17:08:30 -04001791 return compare_bitmaps(reference, *bitmap);
mtklein4a34ecb2016-01-08 10:19:35 -08001792 }
1793 return "";
1794}
1795
1796/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1797
mtkleind603b222015-02-17 11:13:33 -08001798static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1799 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1800 matrix->mapRect(&bounds);
1801 matrix->postTranslate(-bounds.x(), -bounds.y());
Hal Canaryfafe1352017-04-11 12:12:02 -04001802 return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())};
mtkleind603b222015-02-17 11:13:33 -08001803}
1804
msarett62d3b102015-12-10 15:14:27 -08001805ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001806
mtkleinb9eb4ac2015-02-02 18:26:03 -08001807Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001808 SkMatrix matrix = fMatrix;
1809 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
Ben Wagner145dbcd2016-11-03 14:40:50 -04001810 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001811 canvas->concat(matrix);
1812 return src.draw(canvas);
1813 });
mtklein748ca3b2015-01-15 10:56:12 -08001814}
1815
mtkleind603b222015-02-17 11:13:33 -08001816// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1817// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001818ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001819
1820Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1821 Error err = fSink->draw(src, bitmap, stream, log);
1822 if (!err.isEmpty()) {
1823 return err;
1824 }
1825
1826 SkMatrix inverse;
1827 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1828 return "Cannot upright --matrix.";
1829 }
1830 SkMatrix upright = SkMatrix::I();
1831 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1832 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1833 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1834 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1835
1836 SkBitmap uprighted;
1837 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001838 uprighted.allocPixels(bitmap->info().makeDimensions(size));
mtkleind603b222015-02-17 11:13:33 -08001839
1840 SkCanvas canvas(uprighted);
1841 canvas.concat(upright);
1842 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07001843 paint.setBlendMode(SkBlendMode::kSrc);
mtkleind603b222015-02-17 11:13:33 -08001844 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1845
1846 *bitmap = uprighted;
mtkleind603b222015-02-17 11:13:33 -08001847 return "";
1848}
1849
mtklein748ca3b2015-01-15 10:56:12 -08001850/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1851
mtkleina16e69e2015-05-05 11:38:45 -07001852Error ViaSerialization::draw(
1853 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001854 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001855 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001856 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001857 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1858 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001859 if (!err.isEmpty()) {
1860 return err;
1861 }
reedca2622b2016-03-18 07:25:55 -07001862 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001863
1864 // Serialize it and then deserialize it.
reed39eaf5f2016-09-15 07:19:35 -07001865 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
mtklein748ca3b2015-01-15 10:56:12 -08001866
Ben Wagner1861e882018-04-04 17:40:46 -04001867 err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001868 canvas->drawPicture(deserialized);
Ben Wagner1861e882018-04-04 17:40:46 -04001869 return "";
mtkleina16e69e2015-05-05 11:38:45 -07001870 });
Ben Wagner1861e882018-04-04 17:40:46 -04001871 if (!err.isEmpty()) {
1872 return err;
1873 }
1874
1875 return check_against_reference(bitmap, src, fSink.get());
mtklein748ca3b2015-01-15 10:56:12 -08001876}
1877
1878/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1879
Brian Salomon57904202018-12-17 14:45:00 -05001880ViaDDL::ViaDDL(int numReplays, int numDivisions, Sink* sink)
1881 : Via(sink), fNumReplays(numReplays), fNumDivisions(numDivisions) {}
Robert Phillips33f02ed2018-03-27 08:06:57 -04001882
Robert Phillips33f02ed2018-03-27 08:06:57 -04001883Error ViaDDL::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1884 auto size = src.size();
1885 SkPictureRecorder recorder;
1886 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1887 SkIntToScalar(size.height())));
1888 if (!err.isEmpty()) {
1889 return err;
1890 }
1891 sk_sp<SkPicture> inputPicture(recorder.finishRecordingAsPicture());
1892
1893 // this is our ultimate final drawing area/rect
1894 SkIRect viewport = SkIRect::MakeWH(size.fWidth, size.fHeight);
1895
Brian Salomon7d88f312019-02-28 10:03:03 -05001896 DDLPromiseImageHelper promiseImageHelper;
Robert Phillips96601082018-05-29 16:13:26 -04001897 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture.get());
1898 if (!compressedPictureData) {
1899 return SkStringPrintf("ViaDDL: Couldn't deflate SkPicture");
Robert Phillips33f02ed2018-03-27 08:06:57 -04001900 }
Brian Salomon57904202018-12-17 14:45:00 -05001901 auto draw = [&](SkCanvas* canvas) -> Error {
1902 GrContext* context = canvas->getGrContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001903 if (!context || !context->priv().getGpu()) {
Brian Salomon57904202018-12-17 14:45:00 -05001904 return SkStringPrintf("DDLs are GPU only");
1905 }
Robert Phillips33f02ed2018-03-27 08:06:57 -04001906
Brian Salomon57904202018-12-17 14:45:00 -05001907 // This is here bc this is the first point where we have access to the context
1908 promiseImageHelper.uploadAllToGPU(context);
Brian Salomon7d88f312019-02-28 10:03:03 -05001909 // We draw N times, with a clear between.
Brian Salomon57904202018-12-17 14:45:00 -05001910 for (int replay = 0; replay < fNumReplays; ++replay) {
1911 if (replay > 0) {
1912 // Clear the drawing of the previous replay
Brian Salomon8d23a582018-12-19 11:35:20 -05001913 canvas->clear(SK_ColorTRANSPARENT);
Brian Salomon57904202018-12-17 14:45:00 -05001914 }
1915 // First, create all the tiles (including their individual dest surfaces)
1916 DDLTileHelper tiles(canvas, viewport, fNumDivisions);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001917
Brian Salomon57904202018-12-17 14:45:00 -05001918 // Second, reinflate the compressed picture individually for each thread
Brian Salomon1bf0ed82019-01-16 13:51:35 -05001919 // This recreates the promise SkImages on each replay iteration. We are currently
1920 // relying on this to test using a SkPromiseImageTexture to fulfill different
1921 // SkImages. On each replay the promise SkImages are recreated in createSKPPerTile.
Brian Salomon57904202018-12-17 14:45:00 -05001922 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001923
Brian Salomon57904202018-12-17 14:45:00 -05001924 // Third, create the DDLs in parallel
1925 tiles.createDDLsInParallel();
Robert Phillips33f02ed2018-03-27 08:06:57 -04001926
Brian Salomon57904202018-12-17 14:45:00 -05001927 if (replay == fNumReplays - 1) {
1928 // This drops the promiseImageHelper's refs on all the promise images if we're in
1929 // the last run.
1930 promiseImageHelper.reset();
1931 }
Robert Phillips33f02ed2018-03-27 08:06:57 -04001932
Brian Salomon57904202018-12-17 14:45:00 -05001933 // Fourth, synchronously render the display lists into the dest tiles
1934 // TODO: it would be cool to not wait until all the tiles are drawn to begin
1935 // drawing to the GPU and composing to the final surface
1936 tiles.drawAllTilesAndFlush(context, false);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001937
Brian Salomon57904202018-12-17 14:45:00 -05001938 // Finally, compose the drawn tiles into the result
1939 // Note: the separation between the tiles and the final composition better
1940 // matches Chrome but costs us a copy
1941 tiles.composeAllTiles(canvas);
1942 context->flush();
1943 }
1944 return "";
1945 };
1946 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, draw);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001947}
1948
Robert Phillips33f02ed2018-03-27 08:06:57 -04001949/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1950
mtklein4a34ecb2016-01-08 10:19:35 -08001951Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1952 auto size = src.size();
Ben Wagner1861e882018-04-04 17:40:46 -04001953 Error err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtklein4a34ecb2016-01-08 10:19:35 -08001954 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001955 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08001956 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1957 SkIntToScalar(size.height())));
1958 if (!err.isEmpty()) {
1959 return err;
1960 }
reedca2622b2016-03-18 07:25:55 -07001961 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08001962 canvas->drawPicture(pic);
Ben Wagner1861e882018-04-04 17:40:46 -04001963 return err;
mtklein4a34ecb2016-01-08 10:19:35 -08001964 });
Ben Wagner1861e882018-04-04 17:40:46 -04001965 if (!err.isEmpty()) {
1966 return err;
1967 }
1968
1969 return check_against_reference(bitmap, src, fSink.get());
mtklein4a34ecb2016-01-08 10:19:35 -08001970}
1971
1972/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1973
Mike Reedbae888e2017-02-18 16:50:45 -05001974#ifdef TEST_VIA_SVG
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001975#include "experimental/svg/model/SkSVGDOM.h"
1976#include "include/svg/SkSVGCanvas.h"
1977#include "src/xml/SkXMLWriter.h"
Mike Reedf67c4592017-02-17 17:06:11 -05001978
1979Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1980 auto size = src.size();
1981 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1982 SkDynamicMemoryWStream wstream;
1983 SkXMLStreamWriter writer(&wstream);
1984 Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get());
1985 if (!err.isEmpty()) {
1986 return err;
1987 }
1988 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
1989 auto dom = SkSVGDOM::MakeFromStream(*rstream);
1990 if (dom) {
1991 dom->setContainerSize(SkSize::Make(size));
1992 dom->render(canvas);
1993 }
1994 return "";
1995 });
1996}
Mike Reedbae888e2017-02-18 16:50:45 -05001997#endif
Mike Reedf67c4592017-02-17 17:06:11 -05001998
mtklein748ca3b2015-01-15 10:56:12 -08001999} // namespace DM