blob: 473be2b241400843c0c58ebae8ae2fba418a10bc [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"
9#include "include/codec/SkAndroidCodec.h"
10#include "include/codec/SkCodec.h"
11#include "include/core/SkColorSpace.h"
12#include "include/core/SkData.h"
13#include "include/core/SkDeferredDisplayListRecorder.h"
14#include "include/core/SkDocument.h"
15#include "include/core/SkExecutor.h"
16#include "include/core/SkImageGenerator.h"
17#include "include/core/SkMallocPixelRef.h"
18#include "include/core/SkMultiPictureDraw.h"
19#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/SkMakeUnique.h"
37#include "src/core/SkOSFile.h"
38#include "src/core/SkOpts.h"
39#include "src/core/SkPictureCommon.h"
40#include "src/core/SkPictureData.h"
41#include "src/core/SkRecordDraw.h"
42#include "src/core/SkRecorder.h"
43#include "src/core/SkTaskGroup.h"
44#include "src/gpu/GrContextPriv.h"
45#include "src/gpu/GrGpu.h"
46#include "src/utils/SkMultiPictureDocumentPriv.h"
47#include "src/utils/SkOSPath.h"
48#include "tools/DDLPromiseImageHelper.h"
49#include "tools/DDLTileHelper.h"
50#include "tools/Resources.h"
51#include "tools/debugger/DebugCanvas.h"
52#include "tools/gpu/MemoryCache.h"
msarettfc0b6d12016-03-17 13:50:17 -070053#if defined(SK_BUILD_FOR_WIN)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054 #include "include/docs/SkXPSDocument.h"
55 #include "src/utils/win/SkAutoCoInitialize.h"
56 #include "src/utils/win/SkHRESULT.h"
57 #include "src/utils/win/SkTScopedComPtr.h"
Hal Canary5e221e72017-02-06 09:51:42 -050058 #include <XpsObjectModel.h>
msarettfc0b6d12016-03-17 13:50:17 -070059#endif
60
Florin Malita87ccf332018-05-04 12:23:24 -040061#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050062 #include "modules/skottie/include/Skottie.h"
63 #include "modules/skottie/utils/SkottieUtils.h"
Florin Malita124d5af2017-12-31 17:02:26 -050064#endif
65
fmalita718df0a2016-07-15 10:33:29 -070066#if defined(SK_XML)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050067 #include "experimental/svg/model/SkSVGDOM.h"
68 #include "include/svg/SkSVGCanvas.h"
69 #include "src/xml/SkXMLWriter.h"
fmalita718df0a2016-07-15 10:33:29 -070070#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050071#include "tests/TestUtils.h"
fmalita718df0a2016-07-15 10:33:29 -070072
Hal Canary8a001442018-09-19 11:31:27 -040073#include <cmath>
74#include <functional>
75
Mike Klein84836b72019-03-21 11:31:36 -050076static DEFINE_bool(multiPage, false,
77 "For document-type backends, render the source into multiple pages");
78static DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070079
Mike Klein4932a5e2019-03-22 11:08:02 -050080DECLARE_int(gpuThreads);
81
bsalomon3724e572016-03-30 18:56:19 -070082using sk_gpu_test::GrContextFactory;
83
mtklein748ca3b2015-01-15 10:56:12 -080084namespace DM {
85
Hal Canary972eba32018-07-30 17:07:07 -040086GMSrc::GMSrc(skiagm::GMFactory factory) : fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -080087
88Error GMSrc::draw(SkCanvas* canvas) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040089 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
Chris Dalton50e24d72019-02-07 16:20:09 -070090 SkString errorMsg;
91 skiagm::DrawResult drawResult = gm->draw(canvas, &errorMsg);
92 if (skiagm::DrawResult::kSkip == drawResult) {
93 return Error::Nonfatal(std::move(errorMsg)); // Cause this test to be skipped.
94 }
Mike Klein16b1efb2019-04-02 10:01:11 -040095 return errorMsg;
mtklein748ca3b2015-01-15 10:56:12 -080096}
97
98SkISize GMSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040099 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -0800100 return gm->getISize();
101}
102
103Name GMSrc::name() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400104 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -0800105 return gm->getName();
106}
107
bsalomon4ee6bd82015-05-27 13:23:23 -0700108void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400109 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -0700110 gm->modifyGrContextOptions(options);
111}
112
mtklein748ca3b2015-01-15 10:56:12 -0800113/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
114
msarettd1227a72016-05-18 06:23:57 -0700115BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
msaretta5783ae2015-09-08 15:35:32 -0700116 : fPath(path)
msaretta5783ae2015-09-08 15:35:32 -0700117 , fMode(mode)
118 , fDstColorType(dstColorType)
119 , fSampleSize(sampleSize)
120{}
121
122bool BRDSrc::veto(SinkFlags flags) const {
123 // No need to test to non-raster or indirect backends.
124 return flags.type != SinkFlags::kRaster
125 || flags.approach != SinkFlags::kDirect;
126}
127
msarettd1227a72016-05-18 06:23:57 -0700128static SkBitmapRegionDecoder* create_brd(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700129 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msaretta5783ae2015-09-08 15:35:32 -0700130 if (!encoded) {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400131 return nullptr;
msaretta5783ae2015-09-08 15:35:32 -0700132 }
reed42943c82016-09-12 12:01:44 -0700133 return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy);
msaretta5783ae2015-09-08 15:35:32 -0700134}
135
Matt Sarett334df3a2016-12-15 18:17:33 -0500136static inline void alpha8_to_gray8(SkBitmap* bitmap) {
137 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
138 // them back to kGray8 so our test framework can draw them correctly.
139 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
140 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
141 .makeAlphaType(kOpaque_SkAlphaType);
142 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
143 }
144}
145
msaretta5783ae2015-09-08 15:35:32 -0700146Error BRDSrc::draw(SkCanvas* canvas) const {
147 SkColorType colorType = canvas->imageInfo().colorType();
148 if (kRGB_565_SkColorType == colorType &&
149 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
150 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
151 }
152 switch (fDstColorType) {
153 case CodecSrc::kGetFromCanvas_DstColorType:
154 break;
msaretta5783ae2015-09-08 15:35:32 -0700155 case CodecSrc::kGrayscale_Always_DstColorType:
156 colorType = kGray_8_SkColorType;
157 break;
msarett34e0ec42016-04-22 16:27:24 -0700158 default:
159 SkASSERT(false);
160 break;
msaretta5783ae2015-09-08 15:35:32 -0700161 }
162
Ben Wagner145dbcd2016-11-03 14:40:50 -0400163 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700164 if (nullptr == brd.get()) {
165 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
166 }
167
Leon Scroggins III0118e972018-03-13 11:14:33 -0400168 auto recommendedCT = brd->computeOutputColorType(colorType);
169 if (kRGB_565_SkColorType == colorType && recommendedCT != colorType) {
170 return Error::Nonfatal("Skip decoding non-opaque to 565.");
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400171 }
Leon Scroggins III0118e972018-03-13 11:14:33 -0400172 colorType = recommendedCT;
173
174 auto colorSpace = brd->computeOutputColorSpace(colorType, nullptr);
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400175
msaretta5783ae2015-09-08 15:35:32 -0700176 const uint32_t width = brd->width();
177 const uint32_t height = brd->height();
178 // Visually inspecting very small output images is not necessary.
179 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
180 return Error::Nonfatal("Scaling very small images is uninteresting.");
181 }
182 switch (fMode) {
183 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700184 SkBitmap bitmap;
185 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
Leon Scroggins III0118e972018-03-13 11:14:33 -0400186 fSampleSize, colorType, false, colorSpace)) {
mtklein9b439152015-12-09 13:02:26 -0800187 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700188 }
Matt Sarett334df3a2016-12-15 18:17:33 -0500189 alpha8_to_gray8(&bitmap);
Matt Sarett8db74f12017-06-14 13:02:05 +0000190
msarett35e5d1b2015-10-27 12:50:25 -0700191 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700192 return "";
193 }
194 case kDivisor_Mode: {
195 const uint32_t divisor = 2;
196 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800197 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700198 }
199
200 // Use a border to test subsets that extend outside the image.
201 // We will not allow the border to be larger than the image dimensions. Allowing
202 // these large borders causes off by one errors that indicate a problem with the
203 // test suite, not a problem with the implementation.
204 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
205 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
206 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
207
208 // We may need to clear the canvas to avoid uninitialized memory.
209 // Assume we are scaling a 780x780 image with sampleSize = 8.
210 // The output image should be 97x97.
211 // Each subset will be 390x390.
212 // Each scaled subset be 48x48.
213 // Four scaled subsets will only fill a 96x96 image.
214 // The bottom row and last column will not be touched.
215 // This is an unfortunate result of our rounding rules when scaling.
216 // Maybe we need to consider testing scaled subsets without trying to
217 // combine them to match the full scaled image? Or maybe this is the
218 // best we can do?
219 canvas->clear(0);
220
221 for (uint32_t x = 0; x < divisor; x++) {
222 for (uint32_t y = 0; y < divisor; y++) {
223 // Calculate the subset dimensions
224 uint32_t subsetWidth = width / divisor;
225 uint32_t subsetHeight = height / divisor;
226 const int left = x * subsetWidth;
227 const int top = y * subsetHeight;
228
229 // Increase the size of the last subset in each row or column, when the
230 // divisor does not divide evenly into the image dimensions
231 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
232 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
233
234 // Increase the size of the subset in order to have a border on each side
235 const int decodeLeft = left - unscaledBorder;
236 const int decodeTop = top - unscaledBorder;
237 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
238 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700239 SkBitmap bitmap;
240 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
Matt Sarett68feef42017-04-11 09:51:32 -0400241 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false,
Leon Scroggins III0118e972018-03-13 11:14:33 -0400242 colorSpace)) {
mtklein9b439152015-12-09 13:02:26 -0800243 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700244 }
msaretta5783ae2015-09-08 15:35:32 -0700245
Matt Sarett334df3a2016-12-15 18:17:33 -0500246 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700247 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700248 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
249 (SkScalar) (subsetWidth / fSampleSize),
250 (SkScalar) (subsetHeight / fSampleSize)),
251 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
252 (SkScalar) (top / fSampleSize),
253 (SkScalar) (subsetWidth / fSampleSize),
254 (SkScalar) (subsetHeight / fSampleSize)),
255 nullptr);
256 }
257 }
258 return "";
259 }
260 default:
261 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800262 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700263 }
264}
265
266SkISize BRDSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400267 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700268 if (brd) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400269 return {SkTMax(1, brd->width() / (int)fSampleSize),
270 SkTMax(1, brd->height() / (int)fSampleSize)};
msaretta5783ae2015-09-08 15:35:32 -0700271 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400272 return {0, 0};
msaretta5783ae2015-09-08 15:35:32 -0700273}
274
275static SkString get_scaled_name(const Path& path, float scale) {
276 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
277}
278
279Name BRDSrc::name() const {
280 // We will replicate the names used by CodecSrc so that images can
281 // be compared in Gold.
282 if (1 == fSampleSize) {
283 return SkOSPath::Basename(fPath.c_str());
284 }
msarett4b0778e2015-11-13 09:59:11 -0800285 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700286}
287
288/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
289
scroggo3ac66e92016-02-08 15:09:48 -0800290static bool serial_from_path_name(const SkString& path) {
291 if (!FLAGS_RAW_threading) {
292 static const char* const exts[] = {
293 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
294 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
295 };
296 const char* actualExt = strrchr(path.c_str(), '.');
297 if (actualExt) {
298 actualExt++;
299 for (auto* ext : exts) {
300 if (0 == strcmp(ext, actualExt)) {
301 return true;
302 }
303 }
304 }
305 }
306 return false;
307}
308
scroggoc5560be2016-02-03 09:42:42 -0800309CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
310 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700311 : fPath(path)
312 , fMode(mode)
313 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800314 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700315 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800316 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700317{}
mtklein748ca3b2015-01-15 10:56:12 -0800318
mtklein99cab4e2015-07-31 06:43:04 -0700319bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800320 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800321 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700322}
scroggo9b77ddd2015-03-19 06:03:39 -0700323
msarett34e0ec42016-04-22 16:27:24 -0700324// Allows us to test decodes to non-native 8888.
msarettb1be46b2016-05-17 08:52:11 -0700325static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700326 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
327 return;
328 }
329
330 for (int y = 0; y < bitmap.height(); y++) {
331 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
332 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
333 }
334}
335
msarettb1be46b2016-05-17 08:52:11 -0700336static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
scroggoba584892016-05-20 13:56:13 -0700337 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
msarett3d9d7a72015-10-21 10:27:10 -0700338 switch (dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700339 case CodecSrc::kGrayscale_Always_DstColorType:
scroggo1a361922016-05-20 14:27:16 -0700340 if (kRGB_565_SkColorType == canvasColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700341 return false;
342 }
scroggoc5560be2016-02-03 09:42:42 -0800343 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700344 break;
msarett34e0ec42016-04-22 16:27:24 -0700345 case CodecSrc::kNonNative8888_Always_DstColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400346 if (kRGB_565_SkColorType == canvasColorType
347 || kRGBA_F16_SkColorType == canvasColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700348 return false;
349 }
350#ifdef SK_PMCOLOR_IS_RGBA
351 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
352#else
353 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
354#endif
355 break;
msarett3d9d7a72015-10-21 10:27:10 -0700356 default:
msarett55f7bdd2016-02-16 13:24:54 -0800357 if (kRGB_565_SkColorType == canvasColorType &&
358 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
359 return false;
360 }
Matt Sarett09a1c082017-02-01 15:34:22 -0800361
scroggoc5560be2016-02-03 09:42:42 -0800362 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700363 break;
364 }
365
scroggoba584892016-05-20 13:56:13 -0700366 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700367 return true;
368}
369
msarettb1be46b2016-05-17 08:52:11 -0700370static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000371 CodecSrc::DstColorType dstColorType,
msarettb1be46b2016-05-17 08:52:11 -0700372 SkScalar left = 0, SkScalar top = 0) {
msarettb1be46b2016-05-17 08:52:11 -0700373 SkBitmap bitmap;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000374 bitmap.installPixels(info, pixels, rowBytes);
msarettb1be46b2016-05-17 08:52:11 -0700375 swap_rb_if_necessary(bitmap, dstColorType);
376 canvas->drawBitmap(bitmap, left, top);
377}
378
Matt Sarettf485cf92017-05-26 08:58:03 -0400379// For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or
380// color format conversions should be performed by the codec. Sometimes the output of the
381// decode will be in an interesting color space. On our srgb and f16 backends, we need to
382// "pretend" that the color space is standard sRGB to avoid triggering color conversion
383// at draw time.
384static void set_bitmap_color_space(SkImageInfo* info) {
Brian Osman6b622962018-08-27 19:16:02 +0000385 *info = info->makeColorSpace(SkColorSpace::MakeSRGB());
Matt Sarettf485cf92017-05-26 08:58:03 -0400386}
387
mtkleine0effd62015-07-29 06:37:28 -0700388Error CodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700389 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800390 if (!encoded) {
391 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
392 }
msarettb714fb02016-01-22 14:46:42 -0800393
Mike Reedede7bac2017-07-23 15:30:02 -0400394 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700395 if (nullptr == codec.get()) {
396 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700397 }
398
scroggoba584892016-05-20 13:56:13 -0700399 SkImageInfo decodeInfo = codec->getInfo();
400 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
401 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800402 return Error::Nonfatal("Skipping uninteresting test.");
msarett438b2ad2015-04-09 12:43:10 -0700403 }
404
msarett0a242972015-06-11 14:27:27 -0700405 // Try to scale the image if it is desired
406 SkISize size = codec->getScaledDimensions(fScale);
407 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
408 return Error::Nonfatal("Test without scaling is uninteresting.");
409 }
msarettb32758a2015-08-18 13:22:46 -0700410
411 // Visually inspecting very small output images is not necessary. We will
412 // cover these cases in unit testing.
413 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
414 return Error::Nonfatal("Scaling very small images is uninteresting.");
415 }
msarett0a242972015-06-11 14:27:27 -0700416 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
417
Mike Reed7fcfb622018-02-09 13:26:46 -0500418 const int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700419 const size_t rowBytes = size.width() * bpp;
Mike Reedf0ffb892017-10-03 14:47:21 -0400420 const size_t safeSize = decodeInfo.computeByteSize(rowBytes);
scroggo19b91532016-10-24 09:03:26 -0700421 SkAutoMalloc pixels(safeSize);
msarett438b2ad2015-04-09 12:43:10 -0700422
msarettbb25b532016-01-13 09:31:39 -0800423 SkCodec::Options options;
424 if (kCodecZeroInit_Mode == fMode) {
msarettb1be46b2016-05-17 08:52:11 -0700425 memset(pixels.get(), 0, size.height() * rowBytes);
msarettbb25b532016-01-13 09:31:39 -0800426 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
427 }
msarett34e0ec42016-04-22 16:27:24 -0700428
429 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400430 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700431 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
432 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
433 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
434 }
scroggo9b77ddd2015-03-19 06:03:39 -0700435
scroggo9c59ebc2015-03-25 13:48:49 -0700436 switch (fMode) {
scroggo19b91532016-10-24 09:03:26 -0700437 case kAnimated_Mode: {
438 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
439 if (frameInfos.size() <= 1) {
440 return SkStringPrintf("%s is not an animated image.", fPath.c_str());
441 }
442
scroggo90e971a2016-10-25 06:11:01 -0700443 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
444 // into. "factor" is the number of frames to draw on one row. There will be
445 // up to "factor" rows as well.
446 const float root = sqrt((float) frameInfos.size());
447 const int factor = sk_float_ceil2int(root);
448
scroggo19b91532016-10-24 09:03:26 -0700449 // Used to cache a frame that future frames will depend on.
450 SkAutoMalloc priorFramePixels;
Nigel Tao66bc5242018-08-22 10:56:03 +1000451 int cachedFrame = SkCodec::kNoFrame;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400452 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
scroggo19b91532016-10-24 09:03:26 -0700453 options.fFrameIndex = i;
454 // Check for a prior frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400455 const int reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +1000456 if (reqFrame != SkCodec::kNoFrame && reqFrame == cachedFrame
scroggo19b91532016-10-24 09:03:26 -0700457 && priorFramePixels.get()) {
458 // Copy into pixels
459 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400460 options.fPriorFrame = reqFrame;
scroggo19b91532016-10-24 09:03:26 -0700461 } else {
Nigel Tao66bc5242018-08-22 10:56:03 +1000462 options.fPriorFrame = SkCodec::kNoFrame;
scroggo19b91532016-10-24 09:03:26 -0700463 }
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500464 SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000465 rowBytes, &options);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500466 if (SkCodec::kInvalidInput == result && i > 0) {
467 // Some of our test images have truncated later frames. Treat that
468 // the same as incomplete.
469 result = SkCodec::kIncompleteInput;
470 }
scroggo19b91532016-10-24 09:03:26 -0700471 switch (result) {
472 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400473 case SkCodec::kErrorInInput:
scroggo90e971a2016-10-25 06:11:01 -0700474 case SkCodec::kIncompleteInput: {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400475 // If the next frame depends on this one, store it in priorFrame.
476 // It is possible that we may discard a frame that future frames depend on,
477 // but the codec will simply redecode the discarded frame.
478 // Do this before calling draw_to_canvas, which premultiplies in place. If
479 // we're decoding to unpremul, we want to pass the unmodified frame to the
480 // codec for decoding the next frame.
481 if (static_cast<size_t>(i+1) < frameInfos.size()
482 && frameInfos[i+1].fRequiredFrame == i) {
483 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
484 cachedFrame = i;
485 }
486
scroggo90e971a2016-10-25 06:11:01 -0700487 SkAutoCanvasRestore acr(canvas, true);
488 const int xTranslate = (i % factor) * decodeInfo.width();
489 const int yTranslate = (i / factor) * decodeInfo.height();
490 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
Leon Scroggins571b30f2017-07-11 17:35:31 +0000491 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
Leon Scroggins III674a1842017-07-06 12:26:09 -0400492 if (result != SkCodec::kSuccess) {
scroggo19b91532016-10-24 09:03:26 -0700493 return "";
494 }
495 break;
scroggo90e971a2016-10-25 06:11:01 -0700496 }
scroggo53f63b62016-10-27 08:29:13 -0700497 case SkCodec::kInvalidConversion:
Mike Reed304a07c2017-07-12 15:10:28 -0400498 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) {
scroggo53f63b62016-10-27 08:29:13 -0700499 return Error::Nonfatal(SkStringPrintf(
Mike Reed304a07c2017-07-12 15:10:28 -0400500 "Cannot decode frame %i to 565 (%s).", i, fPath.c_str()));
scroggo53f63b62016-10-27 08:29:13 -0700501 }
502 // Fall through.
scroggo19b91532016-10-24 09:03:26 -0700503 default:
504 return SkStringPrintf("Couldn't getPixels for frame %i in %s.",
505 i, fPath.c_str());
506 }
scroggo19b91532016-10-24 09:03:26 -0700507 }
508 break;
509 }
msarettbb25b532016-01-13 09:31:39 -0800510 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700511 case kCodec_Mode: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000512 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggoeb602a52015-07-09 08:16:03 -0700513 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400514 // We consider these to be valid, since we should still decode what is
scroggo9c59ebc2015-03-25 13:48:49 -0700515 // available.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400516 case SkCodec::kErrorInInput:
scroggoeb602a52015-07-09 08:16:03 -0700517 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700518 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700519 default:
520 // Everything else is considered a failure.
521 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
522 }
msarettb1be46b2016-05-17 08:52:11 -0700523
Leon Scroggins571b30f2017-07-11 17:35:31 +0000524 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggo9c59ebc2015-03-25 13:48:49 -0700525 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700526 }
scroggo9c59ebc2015-03-25 13:48:49 -0700527 case kScanline_Mode: {
scroggod8d68552016-06-06 11:26:17 -0700528 void* dst = pixels.get();
529 uint32_t height = decodeInfo.height();
scroggo19b91532016-10-24 09:03:26 -0700530 const bool useIncremental = [this]() {
531 auto exts = { "png", "PNG", "gif", "GIF" };
532 for (auto ext : exts) {
533 if (fPath.endsWith(ext)) {
534 return true;
535 }
536 }
537 return false;
538 }();
539 // ico may use the old scanline method or the new one, depending on whether it
540 // internally holds a bmp or a png.
scroggo8e6c7ad2016-09-16 08:20:38 -0700541 const bool ico = fPath.endsWith("ico");
scroggo19b91532016-10-24 09:03:26 -0700542 bool useOldScanlineMethod = !useIncremental && !ico;
543 if (useIncremental || ico) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700544 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000545 rowBytes, &options)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700546 int rowsDecoded;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400547 auto result = codec->incrementalDecode(&rowsDecoded);
548 if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700549 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
550 SkCodec::kNo_ZeroInitialized, height,
551 rowsDecoded);
scroggo6fb23912016-06-02 14:16:43 -0700552 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700553 } else {
scroggo19b91532016-10-24 09:03:26 -0700554 if (useIncremental) {
555 // Error: These should support incremental decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700556 return "Could not start incremental decode";
557 }
558 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
559 // which should work via startScanlineDecode
560 useOldScanlineMethod = true;
561 }
562 }
563
564 if (useOldScanlineMethod) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000565 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700566 return "Could not start scanline decoder";
567 }
568
Brian Osmanea176c62018-04-06 15:28:23 -0400569 // We do not need to check the return value. On an incomplete
570 // image, memory will be filled with a default value.
571 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700572 }
573
Leon Scroggins571b30f2017-07-11 17:35:31 +0000574 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700575 break;
576 }
msarett0a242972015-06-11 14:27:27 -0700577 case kStripe_Mode: {
578 const int height = decodeInfo.height();
579 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
580 // does not align with image blocks.
581 const int stripeHeight = 37;
582 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
msarettb1be46b2016-05-17 08:52:11 -0700583 void* dst = pixels.get();
msarett0a242972015-06-11 14:27:27 -0700584
585 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000586 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarettb65e6042016-02-23 05:37:25 -0800587 return "Could not start scanline decoder";
588 }
589
590 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
591 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
592 // to run this test for image types that do not have this scanline ordering.
scroggo12e2f502016-05-16 09:04:18 -0700593 // We only run this on Jpeg, which is always kTopDown.
594 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
msarette6dd0042015-10-09 11:07:34 -0700595
msarett0a242972015-06-11 14:27:27 -0700596 for (int i = 0; i < numStripes; i += 2) {
597 // Skip a stripe
598 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700599 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700600
601 // Read a stripe
602 const int startY = (i + 1) * stripeHeight;
603 const int linesToRead = SkTMin(stripeHeight, height - startY);
604 if (linesToRead > 0) {
msarettb1be46b2016-05-17 08:52:11 -0700605 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
606 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700607 }
608 }
609
610 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000611 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
scroggo1c005e42015-08-04 09:24:45 -0700612 if (SkCodec::kSuccess != startResult) {
613 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700614 }
615 for (int i = 0; i < numStripes; i += 2) {
616 // Read a stripe
617 const int startY = i * stripeHeight;
618 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarettb1be46b2016-05-17 08:52:11 -0700619 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
620 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700621
622 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700623 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
624 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700625 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700626 }
627 }
msarettb1be46b2016-05-17 08:52:11 -0700628
Leon Scroggins571b30f2017-07-11 17:35:31 +0000629 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700630 break;
msarett0a242972015-06-11 14:27:27 -0700631 }
msarett91c22b22016-02-22 12:27:46 -0800632 case kCroppedScanline_Mode: {
633 const int width = decodeInfo.width();
634 const int height = decodeInfo.height();
635 // This value is chosen because, as we move across the image, it will sometimes
636 // align with the jpeg block sizes and it will sometimes not. This allows us
637 // to test interestingly different code paths in the implementation.
638 const int tileSize = 36;
msarett91c22b22016-02-22 12:27:46 -0800639 SkIRect subset;
640 for (int x = 0; x < width; x += tileSize) {
641 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
Matt Saretta40d9c82017-05-19 15:21:05 -0400642 options.fSubset = &subset;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000643 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarett91c22b22016-02-22 12:27:46 -0800644 return "Could not start scanline decoder.";
645 }
646
msarettb1be46b2016-05-17 08:52:11 -0700647 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
msarett91c22b22016-02-22 12:27:46 -0800648 }
649
Leon Scroggins571b30f2017-07-11 17:35:31 +0000650 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800651 break;
652 }
scroggob636b452015-07-22 07:16:20 -0700653 case kSubset_Mode: {
654 // Arbitrarily choose a divisor.
655 int divisor = 2;
656 // Total width/height of the image.
657 const int W = codec->getInfo().width();
658 const int H = codec->getInfo().height();
659 if (divisor > W || divisor > H) {
660 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
661 "for %s with dimensions (%d x %d)", divisor,
662 fPath.c_str(), W, H));
663 }
664 // subset dimensions
665 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
666 const int w = SkAlign2(W / divisor);
667 const int h = SkAlign2(H / divisor);
668 SkIRect subset;
Matt Saretta40d9c82017-05-19 15:21:05 -0400669 options.fSubset = &subset;
scroggob636b452015-07-22 07:16:20 -0700670 SkBitmap subsetBm;
671 // We will reuse pixel memory from bitmap.
msarettb1be46b2016-05-17 08:52:11 -0700672 void* dst = pixels.get();
scroggob636b452015-07-22 07:16:20 -0700673 // Keep track of left and top (for drawing subsetBm into canvas). We could use
674 // fScale * x and fScale * y, but we want integers such that the next subset will start
675 // where the last one ended. So we'll add decodeInfo.width() and height().
676 int left = 0;
677 for (int x = 0; x < W; x += w) {
678 int top = 0;
679 for (int y = 0; y < H; y+= h) {
680 // Do not make the subset go off the edge of the image.
681 const int preScaleW = SkTMin(w, W - x);
682 const int preScaleH = SkTMin(h, H - y);
683 subset.setXYWH(x, y, preScaleW, preScaleH);
684 // And scale
685 // FIXME: Should we have a version of getScaledDimensions that takes a subset
686 // into account?
msarettc7eb4902016-04-25 07:04:58 -0700687 const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale));
688 const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale));
689 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
690 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
msarettb1be46b2016-05-17 08:52:11 -0700691 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
692 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000693 &options);
scroggob636b452015-07-22 07:16:20 -0700694 switch (result) {
695 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400696 case SkCodec::kErrorInInput:
scroggob636b452015-07-22 07:16:20 -0700697 case SkCodec::kIncompleteInput:
698 break;
scroggob636b452015-07-22 07:16:20 -0700699 default:
700 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
701 "from %s with dimensions (%d x %d)\t error %d",
702 x, y, decodeInfo.width(), decodeInfo.height(),
703 fPath.c_str(), W, H, result);
704 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000705 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType,
706 SkIntToScalar(left), SkIntToScalar(top));
msarettb1be46b2016-05-17 08:52:11 -0700707
scroggob636b452015-07-22 07:16:20 -0700708 // translate by the scaled height.
709 top += decodeInfo.height();
710 }
711 // translate by the scaled width.
712 left += decodeInfo.width();
713 }
714 return "";
715 }
msarettb714fb02016-01-22 14:46:42 -0800716 default:
717 SkASSERT(false);
718 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700719 }
scroggo9c59ebc2015-03-25 13:48:49 -0700720 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700721}
722
723SkISize CodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700724 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400725 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700726 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400727 return {0, 0};
scroggo7fac5af2015-09-30 11:33:12 -0700728 }
scroggo19b91532016-10-24 09:03:26 -0700729
730 auto imageSize = codec->getScaledDimensions(fScale);
731 if (fMode == kAnimated_Mode) {
scroggo90e971a2016-10-25 06:11:01 -0700732 // We'll draw one of each frame, so make it big enough to hold them all
733 // in a grid. The grid will be roughly square, with "factor" frames per
734 // row and up to "factor" rows.
scroggo19b91532016-10-24 09:03:26 -0700735 const size_t count = codec->getFrameInfo().size();
scroggo90e971a2016-10-25 06:11:01 -0700736 const float root = sqrt((float) count);
737 const int factor = sk_float_ceil2int(root);
738 imageSize.fWidth = imageSize.fWidth * factor;
739 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
scroggo19b91532016-10-24 09:03:26 -0700740 }
741 return imageSize;
scroggo9b77ddd2015-03-19 06:03:39 -0700742}
743
744Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700745 if (1.0f == fScale) {
scroggo6e8c68e2016-10-24 13:48:49 -0700746 Name name = SkOSPath::Basename(fPath.c_str());
747 if (fMode == kAnimated_Mode) {
748 name.append("_animated");
749 }
750 return name;
msarett0a242972015-06-11 14:27:27 -0700751 }
scroggo6e8c68e2016-10-24 13:48:49 -0700752 SkASSERT(fMode != kAnimated_Mode);
msaretta5783ae2015-09-08 15:35:32 -0700753 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700754}
755
756/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
757
scroggof8dc9df2016-05-16 09:04:13 -0700758AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800759 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700760 : fPath(path)
msarett3d9d7a72015-10-21 10:27:10 -0700761 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800762 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700763 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800764 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700765{}
766
767bool AndroidCodecSrc::veto(SinkFlags flags) const {
768 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700769 return flags.type != SinkFlags::kRaster
770 || flags.approach != SinkFlags::kDirect;
771}
772
773Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700774 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700775 if (!encoded) {
776 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
777 }
Mike Reedede7bac2017-07-23 15:30:02 -0400778 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
779 if (nullptr == codec) {
msarett3d9d7a72015-10-21 10:27:10 -0700780 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
781 }
782
scroggoba584892016-05-20 13:56:13 -0700783 SkImageInfo decodeInfo = codec->getInfo();
784 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
785 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800786 return Error::Nonfatal("Skipping uninteresting test.");
msarett3d9d7a72015-10-21 10:27:10 -0700787 }
788
789 // Scale the image if it is desired.
790 SkISize size = codec->getSampledDimensions(fSampleSize);
791
792 // Visually inspecting very small output images is not necessary. We will
793 // cover these cases in unit testing.
794 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
795 return Error::Nonfatal("Scaling very small images is uninteresting.");
796 }
797 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
798
Mike Reed7fcfb622018-02-09 13:26:46 -0500799 int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700800 size_t rowBytes = size.width() * bpp;
801 SkAutoMalloc pixels(size.height() * rowBytes);
msarett3d9d7a72015-10-21 10:27:10 -0700802
803 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700804 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400805 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700806 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
807 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
808 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
809 }
msarett3d9d7a72015-10-21 10:27:10 -0700810
811 // Create options for the codec.
812 SkAndroidCodec::AndroidOptions options;
msarett3d9d7a72015-10-21 10:27:10 -0700813 options.fSampleSize = fSampleSize;
814
msarettb1be46b2016-05-17 08:52:11 -0700815 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggof8dc9df2016-05-16 09:04:13 -0700816 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400817 case SkCodec::kErrorInInput:
scroggof8dc9df2016-05-16 09:04:13 -0700818 case SkCodec::kIncompleteInput:
819 break;
msarett3d9d7a72015-10-21 10:27:10 -0700820 default:
scroggof8dc9df2016-05-16 09:04:13 -0700821 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700822 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000823 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggof8dc9df2016-05-16 09:04:13 -0700824 return "";
msarett3d9d7a72015-10-21 10:27:10 -0700825}
826
827SkISize AndroidCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700828 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400829 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700830 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400831 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700832 }
833 return codec->getSampledDimensions(fSampleSize);
834}
835
836Name AndroidCodecSrc::name() const {
837 // We will replicate the names used by CodecSrc so that images can
838 // be compared in Gold.
839 if (1 == fSampleSize) {
840 return SkOSPath::Basename(fPath.c_str());
841 }
msarett4b0778e2015-11-13 09:59:11 -0800842 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700843}
844
845/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
846
msarett18976312016-03-09 14:20:58 -0800847ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
848 : fPath(path)
849 , fMode(mode)
850 , fDstAlphaType(alphaType)
851 , fIsGpu(isGpu)
852 , fRunSerially(serial_from_path_name(path))
853{}
854
855bool ImageGenSrc::veto(SinkFlags flags) const {
856 if (fIsGpu) {
Brian Salomonbd7c5512017-03-07 09:08:36 -0500857 // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs.
858 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect ||
859 flags.multisampled == SinkFlags::kMultisampled;
msarett18976312016-03-09 14:20:58 -0800860 }
861
862 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
863}
864
865Error ImageGenSrc::draw(SkCanvas* canvas) const {
866 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
867 return Error::Nonfatal("Uninteresting to test image generator to 565.");
868 }
869
bungeman38d909e2016-08-02 14:40:46 -0700870 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett18976312016-03-09 14:20:58 -0800871 if (!encoded) {
872 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
873 }
874
msarettfc0b6d12016-03-17 13:50:17 -0700875#if defined(SK_BUILD_FOR_WIN)
876 // Initialize COM in order to test with WIC.
877 SkAutoCoInitialize com;
878 if (!com.succeeded()) {
879 return "Could not initialize COM.";
880 }
881#endif
882
Ben Wagner145dbcd2016-11-03 14:40:50 -0400883 std::unique_ptr<SkImageGenerator> gen(nullptr);
msarett18976312016-03-09 14:20:58 -0800884 switch (fMode) {
885 case kCodec_Mode:
Mike Reed185130c2017-02-15 15:14:16 -0500886 gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded);
msarett18976312016-03-09 14:20:58 -0800887 if (!gen) {
888 return "Could not create codec image generator.";
889 }
890 break;
msarettfc0b6d12016-03-17 13:50:17 -0700891 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800892#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -0400893 gen = SkImageGeneratorCG::MakeFromEncodedCG(encoded);
msarettfc0b6d12016-03-17 13:50:17 -0700894#elif defined(SK_BUILD_FOR_WIN)
Leon Scroggins IIIa57488a2018-06-25 14:01:29 -0400895 gen = SkImageGeneratorWIC::MakeFromEncodedWIC(encoded);
msarettfc0b6d12016-03-17 13:50:17 -0700896#endif
msarett18976312016-03-09 14:20:58 -0800897 if (!gen) {
msarettfc0b6d12016-03-17 13:50:17 -0700898 return "Could not create platform image generator.";
msarett18976312016-03-09 14:20:58 -0800899 }
900 break;
msarettfc0b6d12016-03-17 13:50:17 -0700901 }
msarett18976312016-03-09 14:20:58 -0800902 default:
903 SkASSERT(false);
904 return "Invalid image generator mode";
905 }
906
907 // Test deferred decoding path on GPU
908 if (fIsGpu) {
Mike Reed185130c2017-02-15 15:14:16 -0500909 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr));
msarett18976312016-03-09 14:20:58 -0800910 if (!image) {
911 return "Could not create image from codec image generator.";
912 }
913 canvas->drawImage(image, 0, 0);
914 return "";
915 }
mtklein343a63d2016-03-22 11:46:53 -0700916
msarett18976312016-03-09 14:20:58 -0800917 // Test various color and alpha types on CPU
918 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700919
Mike Reed7fcfb622018-02-09 13:26:46 -0500920 int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700921 size_t rowBytes = decodeInfo.width() * bpp;
922 SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
Brian Osmanc87cfb62018-07-11 09:08:46 -0400923 if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes)) {
Matt Sarett05cb4c32017-03-02 12:07:46 -0500924 SkString err =
925 SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str());
926
927#if defined(SK_BUILD_FOR_WIN)
928 if (kPlatform_Mode == fMode) {
929 // Do not issue a fatal error for WIC flakiness.
930 return Error::Nonfatal(err);
931 }
932#endif
933
934 return err;
msarett18976312016-03-09 14:20:58 -0800935 }
936
Matt Sarettf485cf92017-05-26 08:58:03 -0400937 set_bitmap_color_space(&decodeInfo);
Leon Scroggins571b30f2017-07-11 17:35:31 +0000938 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes,
msarettb1be46b2016-05-17 08:52:11 -0700939 CodecSrc::kGetFromCanvas_DstColorType);
msarett18976312016-03-09 14:20:58 -0800940 return "";
941}
942
943SkISize ImageGenSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700944 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400945 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett18976312016-03-09 14:20:58 -0800946 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400947 return {0, 0};
msarett18976312016-03-09 14:20:58 -0800948 }
949 return codec->getInfo().dimensions();
950}
951
952Name ImageGenSrc::name() const {
953 return SkOSPath::Basename(fPath.c_str());
954}
955
956/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
957
Mike Klein0d5d1422019-03-06 10:56:04 -0600958ColorCodecSrc::ColorCodecSrc(Path path, bool decode_to_dst) : fPath(path)
959 , fDecodeToDst(decode_to_dst) {}
msarett69deca82016-04-29 09:38:40 -0700960
961bool ColorCodecSrc::veto(SinkFlags flags) const {
962 // Test to direct raster backends (8888 and 565).
963 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
964}
965
966Error ColorCodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700967 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett69deca82016-04-29 09:38:40 -0700968 if (!encoded) {
969 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
970 }
971
Mike Reedede7bac2017-07-23 15:30:02 -0400972 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
973 if (nullptr == codec) {
msarett69deca82016-04-29 09:38:40 -0700974 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
975 }
976
Mike Klein0d5d1422019-03-06 10:56:04 -0600977 SkImageInfo info = codec->getInfo();
978 if (fDecodeToDst) {
979 info = canvas->imageInfo().makeWH(info.width(),
980 info.height());
msarett50ce1f22016-07-29 06:23:33 -0700981 }
982
983 SkBitmap bitmap;
Mike Klein0d5d1422019-03-06 10:56:04 -0600984 if (!bitmap.tryAllocPixels(info)) {
985 return SkStringPrintf("Image(%s) is too large (%d x %d)",
986 fPath.c_str(), info.width(), info.height());
msarett50ce1f22016-07-29 06:23:33 -0700987 }
988
Mike Klein0d5d1422019-03-06 10:56:04 -0600989 switch (auto r = codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes())) {
Leon Scroggins III674a1842017-07-06 12:26:09 -0400990 case SkCodec::kSuccess:
991 case SkCodec::kErrorInInput:
992 case SkCodec::kIncompleteInput:
Mike Klein0d5d1422019-03-06 10:56:04 -0600993 canvas->drawBitmap(bitmap, 0,0);
994 return "";
995 case SkCodec::kInvalidConversion:
996 // TODO(mtklein): why are there formats we can't decode to?
997 return Error::Nonfatal("SkCodec can't decode to this format.");
Leon Scroggins III674a1842017-07-06 12:26:09 -0400998 default:
999 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
msarett50ce1f22016-07-29 06:23:33 -07001000 }
msarett69deca82016-04-29 09:38:40 -07001001}
1002
1003SkISize ColorCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001004 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001005 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001006 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001007 return {0, 0};
msarett69deca82016-04-29 09:38:40 -07001008 }
Hal Canaryfafe1352017-04-11 12:12:02 -04001009 return {codec->getInfo().width(), codec->getInfo().height()};
msarett69deca82016-04-29 09:38:40 -07001010}
1011
1012Name ColorCodecSrc::name() const {
1013 return SkOSPath::Basename(fPath.c_str());
1014}
1015
1016/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1017
Mike Klein19fb3972019-03-21 13:08:08 -05001018static DEFINE_int(skpViewportSize, 1000,
1019 "Width & height of the viewport used to crop skp rendering.");
1020
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001021SKPSrc::SKPSrc(Path path) : fPath(path) { }
mtklein748ca3b2015-01-15 10:56:12 -08001022
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001023Error SKPSrc::draw(SkCanvas* canvas) const {
Hal Canary72b91cb2019-01-14 12:47:59 -05001024 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
1025 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001026 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1027 }
Hal Canary72b91cb2019-01-14 12:47:59 -05001028 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
1029 if (!pic) {
1030 return SkStringPrintf("Couldn't parse file %s.", fPath.c_str());
1031 }
1032 stream = nullptr; // Might as well drop this when we're done with it.
Robert Phillips33f02ed2018-03-27 08:06:57 -04001033 canvas->clipRect(SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize));
mtklein748ca3b2015-01-15 10:56:12 -08001034 canvas->drawPicture(pic);
1035 return "";
1036}
1037
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001038static SkRect get_cull_rect_for_skp(const char* path) {
1039 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtkleinffa901a2015-03-16 10:38:07 -07001040 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001041 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001042 }
1043 SkPictInfo info;
Mike Reede7a58322017-12-20 14:09:20 -05001044 if (!SkPicture_StreamIsSKP(stream.get(), &info)) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001045 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001046 }
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001047
1048 return info.fCullRect;
1049}
1050
1051SkISize SKPSrc::size() const {
1052 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
Robert Phillips33f02ed2018-03-27 08:06:57 -04001053 if (!viewport.intersect((SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize)))) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001054 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001055 }
1056 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001057}
1058
1059Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1060
Florin Malitafc043dc2017-12-31 11:08:42 -05001061/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1062
Chris Dalton184c37e2018-09-28 11:27:39 -06001063BisectSrc::BisectSrc(Path path, const char* trail) : INHERITED(path), fTrail(trail) {}
1064
1065Error BisectSrc::draw(SkCanvas* canvas) const {
1066 struct FoundPath {
1067 SkPath fPath;
1068 SkPaint fPaint;
1069 SkMatrix fViewMatrix;
1070 };
1071
1072 // This subclass of SkCanvas just extracts all the SkPaths (drawn via drawPath) from an SKP.
1073 class PathFindingCanvas : public SkCanvas {
1074 public:
1075 PathFindingCanvas(int width, int height) : SkCanvas(width, height, nullptr) {}
1076 const SkTArray<FoundPath>& foundPaths() const { return fFoundPaths; }
1077
1078 private:
1079 void onDrawPath(const SkPath& path, const SkPaint& paint) override {
1080 fFoundPaths.push_back() = {path, paint, this->getTotalMatrix()};
1081 }
1082
1083 SkTArray<FoundPath> fFoundPaths;
1084 };
1085
1086 PathFindingCanvas pathFinder(canvas->getBaseLayerSize().width(),
1087 canvas->getBaseLayerSize().height());
1088 Error err = this->INHERITED::draw(&pathFinder);
1089 if (!err.isEmpty()) {
1090 return err;
1091 }
1092
1093 int start = 0, end = pathFinder.foundPaths().count();
1094 for (const char* ch = fTrail.c_str(); *ch; ++ch) {
1095 int midpt = (start + end) / 2;
1096 if ('l' == *ch) {
1097 start = midpt;
1098 } else if ('r' == *ch) {
1099 end = midpt;
1100 }
1101 }
1102
1103 for (int i = start; i < end; ++i) {
1104 const FoundPath& path = pathFinder.foundPaths()[i];
1105 SkAutoCanvasRestore acr(canvas, true);
1106 canvas->concat(path.fViewMatrix);
1107 canvas->drawPath(path.fPath, path.fPaint);
1108 }
1109
1110 return "";
1111}
1112
1113/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1114
Florin Malita87ccf332018-05-04 12:23:24 -04001115#if defined(SK_ENABLE_SKOTTIE)
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001116SkottieSrc::SkottieSrc(Path path) : fPath(std::move(path)) {}
Florin Malitafc043dc2017-12-31 11:08:42 -05001117
Florin Malita54f65c42018-01-16 17:04:30 -05001118Error SkottieSrc::draw(SkCanvas* canvas) const {
Florin Malitaa8316552018-11-09 16:19:44 -05001119 auto animation = skottie::Animation::Builder()
1120 .setResourceProvider(
1121 skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str())))
1122 .makeFromFile(fPath.c_str());
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001123 if (!animation) {
1124 return SkStringPrintf("Unable to parse file: %s", fPath.c_str());
Florin Malitafc043dc2017-12-31 11:08:42 -05001125 }
1126
1127 canvas->drawColor(SK_ColorWHITE);
1128
Florin Malitaa33447d2018-05-29 13:46:54 -04001129 const auto t_rate = 1.0f / (kTileCount * kTileCount - 1);
Florin Malitafc043dc2017-12-31 11:08:42 -05001130
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001131 // Draw the frames in a shuffled order to exercise non-linear
1132 // frame progression. The film strip will still be in order left-to-right,
1133 // top-down, just not drawn in that order.
1134 static constexpr int frameOrder[] = { 4, 0, 3, 1, 2 };
1135 static_assert(SK_ARRAY_COUNT(frameOrder) == kTileCount, "");
Florin Malitad3c1b842018-01-27 12:43:24 -05001136
Florin Malitafc043dc2017-12-31 11:08:42 -05001137 for (int i = 0; i < kTileCount; ++i) {
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001138 const SkScalar y = frameOrder[i] * kTileSize;
Florin Malitafc043dc2017-12-31 11:08:42 -05001139
1140 for (int j = 0; j < kTileCount; ++j) {
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001141 const SkScalar x = frameOrder[j] * kTileSize;
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001142 SkRect dest = SkRect::MakeXYWH(x, y, kTileSize, kTileSize);
Florin Malitafc043dc2017-12-31 11:08:42 -05001143
Kevin Lubickc5fe15d2018-08-29 09:52:52 -04001144 const auto t = t_rate * (frameOrder[i] * kTileCount + frameOrder[j]);
Florin Malitadf2713c2018-01-09 15:51:21 -05001145 {
1146 SkAutoCanvasRestore acr(canvas, true);
Florin Malitad3c1b842018-01-27 12:43:24 -05001147 canvas->clipRect(dest, true);
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001148 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
Florin Malitadf2713c2018-01-09 15:51:21 -05001149 dest,
Florin Malita3fae0f32018-03-15 21:52:54 -04001150 SkMatrix::kCenter_ScaleToFit));
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001151 animation->seek(t);
1152 animation->render(canvas);
Florin Malitadf2713c2018-01-09 15:51:21 -05001153 }
Florin Malitafc043dc2017-12-31 11:08:42 -05001154 }
1155 }
1156
1157 return "";
1158}
1159
Florin Malita54f65c42018-01-16 17:04:30 -05001160SkISize SkottieSrc::size() const {
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001161 return SkISize::Make(kTargetSize, kTargetSize);
Florin Malitafc043dc2017-12-31 11:08:42 -05001162}
1163
Florin Malita9f7d4cd2018-07-30 15:49:20 -04001164Name SkottieSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
Florin Malitafc043dc2017-12-31 11:08:42 -05001165
Florin Malita54f65c42018-01-16 17:04:30 -05001166bool SkottieSrc::veto(SinkFlags flags) const {
Florin Malitafc043dc2017-12-31 11:08:42 -05001167 // No need to test to non-(raster||gpu||vector) or indirect backends.
1168 bool type_ok = flags.type == SinkFlags::kRaster
1169 || flags.type == SinkFlags::kGPU
1170 || flags.type == SinkFlags::kVector;
1171
1172 return !type_ok || flags.approach != SinkFlags::kDirect;
1173}
Florin Malita124d5af2017-12-31 17:02:26 -05001174#endif
Florin Malitafc043dc2017-12-31 11:08:42 -05001175
1176/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fmalitaa2b9fdf2016-08-03 19:53:36 -07001177#if defined(SK_XML)
fmalitabdf3e5c2016-09-17 07:26:26 -07001178// Used when the image doesn't have an intrinsic size.
Hal Canaryfafe1352017-04-11 12:12:02 -04001179static const SkSize kDefaultSVGSize = {1000, 1000};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001180
fmalitabdf3e5c2016-09-17 07:26:26 -07001181// Used to force-scale tiny fixed-size images.
Hal Canaryfafe1352017-04-11 12:12:02 -04001182static const SkSize kMinimumSVGSize = {128, 128};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001183
fmalitaacd2f5c2016-11-08 07:13:45 -08001184SVGSrc::SVGSrc(Path path)
1185 : fName(SkOSPath::Basename(path.c_str()))
1186 , fScale(1) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001187
Brian Osman133823d2018-09-19 14:14:15 -04001188 sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
1189 if (!data) {
1190 return;
1191 }
fmalitaacd2f5c2016-11-08 07:13:45 -08001192
Brian Osman133823d2018-09-19 14:14:15 -04001193 SkMemoryStream stream(std::move(data));
1194 fDom = SkSVGDOM::MakeFromStream(stream);
1195 if (!fDom) {
1196 return;
1197 }
1198
1199 const SkSize& sz = fDom->containerSize();
1200 if (sz.isEmpty()) {
1201 // no intrinsic size
1202 fDom->setContainerSize(kDefaultSVGSize);
1203 } else {
1204 fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(),
1205 kMinimumSVGSize.height() / sz.height()));
1206 }
fmalitaacd2f5c2016-11-08 07:13:45 -08001207}
1208
1209Error SVGSrc::draw(SkCanvas* canvas) const {
fmalitabdf3e5c2016-09-17 07:26:26 -07001210 if (!fDom) {
fmalitaacd2f5c2016-11-08 07:13:45 -08001211 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
fmalitaa2b9fdf2016-08-03 19:53:36 -07001212 }
1213
fmalitaacd2f5c2016-11-08 07:13:45 -08001214 SkAutoCanvasRestore acr(canvas, true);
1215 canvas->scale(fScale, fScale);
1216 fDom->render(canvas);
1217
fmalitaa2b9fdf2016-08-03 19:53:36 -07001218 return "";
1219}
1220
1221SkISize SVGSrc::size() const {
fmalitaacd2f5c2016-11-08 07:13:45 -08001222 if (!fDom) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001223 return {0, 0};
fmalitabdf3e5c2016-09-17 07:26:26 -07001224 }
1225
Hal Canaryfafe1352017-04-11 12:12:02 -04001226 return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale}
1227 .toRound();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001228}
1229
fmalitaacd2f5c2016-11-08 07:13:45 -08001230Name SVGSrc::name() const { return fName; }
fmalitaa2b9fdf2016-08-03 19:53:36 -07001231
fmalita179d8852016-08-16 14:23:29 -07001232bool SVGSrc::veto(SinkFlags flags) const {
Florin Malita93323eb2017-06-16 15:31:17 -04001233 // No need to test to non-(raster||gpu||vector) or indirect backends.
fmalita179d8852016-08-16 14:23:29 -07001234 bool type_ok = flags.type == SinkFlags::kRaster
Florin Malita93323eb2017-06-16 15:31:17 -04001235 || flags.type == SinkFlags::kGPU
1236 || flags.type == SinkFlags::kVector;
fmalita179d8852016-08-16 14:23:29 -07001237
1238 return !type_ok || flags.approach != SinkFlags::kDirect;
1239}
1240
fmalitaa2b9fdf2016-08-03 19:53:36 -07001241#endif // defined(SK_XML)
1242/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein748ca3b2015-01-15 10:56:12 -08001243
halcanary45420a92016-06-02 12:41:14 -07001244MSKPSrc::MSKPSrc(Path path) : fPath(path) {
bungemanf93d7112016-09-16 06:24:20 -07001245 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001246 int count = SkMultiPictureDocumentReadPageCount(stream.get());
1247 if (count > 0) {
1248 fPages.reset(count);
1249 (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count());
1250 }
halcanary45420a92016-06-02 12:41:14 -07001251}
1252
Hal Canary45cde312017-04-03 16:06:42 -04001253int MSKPSrc::pageCount() const { return fPages.count(); }
halcanary45420a92016-06-02 12:41:14 -07001254
1255SkISize MSKPSrc::size() const { return this->size(0); }
Hal Canary45cde312017-04-03 16:06:42 -04001256SkISize MSKPSrc::size(int i) const {
Hal Canaryfafe1352017-04-11 12:12:02 -04001257 return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0};
Hal Canary45cde312017-04-03 16:06:42 -04001258}
halcanary45420a92016-06-02 12:41:14 -07001259
1260Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); }
1261Error MSKPSrc::draw(int i, SkCanvas* canvas) const {
Hal Canary45cde312017-04-03 16:06:42 -04001262 if (this->pageCount() == 0) {
halcanary45420a92016-06-02 12:41:14 -07001263 return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
1264 }
Hal Canary45cde312017-04-03 16:06:42 -04001265 if (i >= fPages.count() || i < 0) {
halcanary45420a92016-06-02 12:41:14 -07001266 return SkStringPrintf("MultiPictureDocument page number out of range: %d", i);
1267 }
Hal Canary45cde312017-04-03 16:06:42 -04001268 SkPicture* page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001269 if (!page) {
Hal Canary45cde312017-04-03 16:06:42 -04001270 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1271 if (!stream) {
1272 return SkStringPrintf("Unable to open file: %s", fPath.c_str());
1273 }
1274 if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) {
1275 return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i,
1276 fPath.c_str());
1277 }
1278 page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001279 }
1280 canvas->drawPicture(page);
1281 return "";
1282}
1283
1284Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1285
1286/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1287
mtkleinad66f9b2015-02-13 15:11:10 -08001288Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
Mike Reed5df49342016-11-12 08:06:55 -06001289 return src.draw(SkMakeNullCanvas().get());
mtkleinad66f9b2015-02-13 15:11:10 -08001290}
1291
1292/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1293
Brian Osmane5756ec2017-09-06 17:08:30 -04001294static Error compare_bitmaps(const SkBitmap& reference, const SkBitmap& bitmap) {
1295 // The dimensions are a property of the Src only, and so should be identical.
Mike Reedf0ffb892017-10-03 14:47:21 -04001296 SkASSERT(reference.computeByteSize() == bitmap.computeByteSize());
1297 if (reference.computeByteSize() != bitmap.computeByteSize()) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001298 return "Dimensions don't match reference";
1299 }
1300 // All SkBitmaps in DM are tight, so this comparison is easy.
Mike Reedf0ffb892017-10-03 14:47:21 -04001301 if (0 != memcmp(reference.getPixels(), bitmap.getPixels(), reference.computeByteSize())) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001302 SkString encoded;
1303 SkString errString("Pixels don't match reference");
Michael Ludwige8e10752018-10-01 12:42:53 -04001304 if (bitmap_to_base64_data_uri(reference, &encoded)) {
1305 errString.append("\nExpected: ");
Brian Osmane5756ec2017-09-06 17:08:30 -04001306 errString.append(encoded);
1307 } else {
1308 errString.append("\nExpected image failed to encode: ");
1309 errString.append(encoded);
1310 }
Michael Ludwige8e10752018-10-01 12:42:53 -04001311 if (bitmap_to_base64_data_uri(bitmap, &encoded)) {
1312 errString.append("\nActual: ");
Brian Osmane5756ec2017-09-06 17:08:30 -04001313 errString.append(encoded);
1314 } else {
1315 errString.append("\nActual image failed to encode: ");
1316 errString.append(encoded);
1317 }
1318 return errString;
1319 }
1320 return "";
1321}
1322
1323/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1324
Mike Klein84836b72019-03-21 11:31:36 -05001325static DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
Mike Klein19fb3972019-03-21 13:08:08 -05001326static DEFINE_bool(preAbandonGpuContext, false,
1327 "Test abandoning the GrContext before running the test.");
1328static DEFINE_bool(abandonGpuContext, false,
1329 "Test abandoning the GrContext after running each test.");
1330static DEFINE_bool(releaseAndAbandonGpuContext, false,
1331 "Test releasing all gpu resources and abandoning the GrContext "
1332 "after running each test");
1333static DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
mtkleinb9eb4ac2015-02-02 18:26:03 -08001334
bsalomon85b4b532016-04-05 11:06:27 -07001335GPUSink::GPUSink(GrContextFactory::ContextType ct,
csmartdaltone812d492017-02-21 12:36:05 -07001336 GrContextFactory::ContextOverrides overrides,
Brian Salomonf865b052018-03-09 09:01:53 -05001337 SkCommandLineConfigGpu::SurfType surfType,
mtklein82d28432015-01-15 12:46:02 -08001338 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -07001339 bool diText,
brianosmand93c1202016-03-10 07:49:08 -08001340 SkColorType colorType,
Brian Salomonce5ee602017-07-17 11:31:31 -04001341 SkAlphaType alphaType,
brianosmanb109b8c2016-06-16 13:03:24 -07001342 sk_sp<SkColorSpace> colorSpace,
Brian Osmanf21aa042017-08-21 16:48:46 -04001343 bool threaded,
1344 const GrContextOptions& grCtxOptions)
Brian Salomonce5ee602017-07-17 11:31:31 -04001345 : fContextType(ct)
1346 , fContextOverrides(overrides)
Brian Salomonf865b052018-03-09 09:01:53 -05001347 , fSurfType(surfType)
Brian Salomonce5ee602017-07-17 11:31:31 -04001348 , fSampleCount(samples)
1349 , fUseDIText(diText)
1350 , fColorType(colorType)
1351 , fAlphaType(alphaType)
1352 , fColorSpace(std::move(colorSpace))
Brian Osmanf21aa042017-08-21 16:48:46 -04001353 , fThreaded(threaded)
1354 , fBaseContextOptions(grCtxOptions) {}
mtklein748ca3b2015-01-15 10:56:12 -08001355
Brian Osmanf9810662017-08-30 10:02:10 -04001356Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const {
1357 return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
1358}
1359
1360Error GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
1361 const GrContextOptions& baseOptions) const {
1362 GrContextOptions grOptions = baseOptions;
kkinnunen64492c42015-12-08 01:24:40 -08001363
Brian Salomon00a5eb82018-07-11 15:32:05 -04001364 // We don't expect the src to mess with the persistent cache or the executor.
1365 SkDEBUGCODE(auto cache = grOptions.fPersistentCache);
1366 SkDEBUGCODE(auto exec = grOptions.fExecutor);
kkinnunen5219fd92015-12-10 06:28:13 -08001367 src.modifyGrContextOptions(&grOptions);
Brian Salomon00a5eb82018-07-11 15:32:05 -04001368 SkASSERT(cache == grOptions.fPersistentCache);
1369 SkASSERT(exec == grOptions.fExecutor);
kkinnunen5219fd92015-12-10 06:28:13 -08001370
1371 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -08001372 const SkISize size = src.size();
Brian Salomonce5ee602017-07-17 11:31:31 -04001373 SkImageInfo info =
1374 SkImageInfo::Make(size.width(), size.height(), fColorType, fAlphaType, fColorSpace);
Brian Salomonf865b052018-03-09 09:01:53 -05001375 sk_sp<SkSurface> surface;
csmartdaltone812d492017-02-21 12:36:05 -07001376 GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001377 const int maxDimension = context->priv().caps()->maxTextureSize();
msarett13a036b2016-02-08 09:10:47 -08001378 if (maxDimension < SkTMax(size.width(), size.height())) {
1379 return Error::Nonfatal("Src too large to create a texture.\n");
1380 }
Brian Salomonf865b052018-03-09 09:01:53 -05001381 uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
1382 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1383 GrBackendTexture backendTexture;
1384 GrBackendRenderTarget backendRT;
1385 switch (fSurfType) {
1386 case SkCommandLineConfigGpu::SurfType::kDefault:
1387 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
1388 &props);
1389 break;
1390 case SkCommandLineConfigGpu::SurfType::kBackendTexture:
Robert Phillips4bdd36f2019-06-04 11:03:06 -04001391 backendTexture = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -04001392 info.width(), info.height(), info.colorType(), SkColors::kTransparent,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001393 GrMipMapped::kNo, GrRenderable::kYes);
Brian Salomonf865b052018-03-09 09:01:53 -05001394 surface = SkSurface::MakeFromBackendTexture(context, backendTexture,
1395 kTopLeft_GrSurfaceOrigin, fSampleCount,
Brian Salomon53706772018-03-19 14:18:08 -04001396 fColorType, info.refColorSpace(), &props);
Brian Salomonf865b052018-03-09 09:01:53 -05001397 break;
1398 case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
1399 if (1 == fSampleCount) {
Brian Salomonf865b052018-03-09 09:01:53 -05001400 auto colorType = SkColorTypeToGrColorType(info.colorType());
Robert Phillips9da87e02019-02-04 13:26:26 -05001401 backendRT = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
Brian Osman2d010b62018-08-09 10:55:09 -04001402 info.width(), info.height(), colorType);
Brian Salomon49edccd2018-03-23 15:31:32 -04001403 surface = SkSurface::MakeFromBackendRenderTarget(
1404 context, backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(),
1405 info.refColorSpace(), &props);
Brian Salomonf865b052018-03-09 09:01:53 -05001406 }
1407 break;
1408 }
msarett13a036b2016-02-08 09:10:47 -08001409
mtklein748ca3b2015-01-15 10:56:12 -08001410 if (!surface) {
1411 return "Could not create a surface.";
1412 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001413 if (FLAGS_preAbandonGpuContext) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001414 factory.abandonContexts();
1415 }
mtklein748ca3b2015-01-15 10:56:12 -08001416 SkCanvas* canvas = surface->getCanvas();
1417 Error err = src.draw(canvas);
1418 if (!err.isEmpty()) {
1419 return err;
1420 }
Robert Phillips9882dae2019-03-04 11:00:10 -05001421 surface->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001422 if (FLAGS_gpuStats) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001423 canvas->getGrContext()->priv().dumpCacheStats(log);
1424 canvas->getGrContext()->priv().dumpGpuStats(log);
mtkleinb9eb4ac2015-02-02 18:26:03 -08001425 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -04001426 if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType ||
1427 info.colorType() == kRGB_888x_SkColorType) {
Brian Salomonce5ee602017-07-17 11:31:31 -04001428 // We don't currently support readbacks into these formats on the GPU backend. Convert to
1429 // 32 bit.
1430 info = SkImageInfo::Make(size.width(), size.height(), kRGBA_8888_SkColorType,
1431 kPremul_SkAlphaType, fColorSpace);
1432 }
mtklein748ca3b2015-01-15 10:56:12 -08001433 dst->allocPixels(info);
Mike Reed12e946b2017-04-17 10:53:29 -04001434 canvas->readPixels(*dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001435 if (FLAGS_abandonGpuContext) {
1436 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001437 } else if (FLAGS_releaseAndAbandonGpuContext) {
1438 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001439 }
Khushalc421ca12018-06-26 14:38:34 -07001440 if (!context->abandoned()) {
Brian Salomonf865b052018-03-09 09:01:53 -05001441 surface.reset();
1442 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001443 context->deleteBackendTexture(backendTexture);
Brian Salomonf865b052018-03-09 09:01:53 -05001444 }
1445 if (backendRT.isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001446 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
Brian Salomonf865b052018-03-09 09:01:53 -05001447 }
1448 }
Greg Daniela870b462019-01-08 15:49:46 -05001449 if (grOptions.fPersistentCache) {
1450 context->storeVkPipelineCacheData();
1451 }
mtklein748ca3b2015-01-15 10:56:12 -08001452 return "";
1453}
1454
1455/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1456
Brian Osmanf9810662017-08-30 10:02:10 -04001457GPUThreadTestingSink::GPUThreadTestingSink(GrContextFactory::ContextType ct,
1458 GrContextFactory::ContextOverrides overrides,
Brian Salomonf865b052018-03-09 09:01:53 -05001459 SkCommandLineConfigGpu::SurfType surfType,
Brian Osmanf9810662017-08-30 10:02:10 -04001460 int samples,
1461 bool diText,
1462 SkColorType colorType,
1463 SkAlphaType alphaType,
1464 sk_sp<SkColorSpace> colorSpace,
1465 bool threaded,
1466 const GrContextOptions& grCtxOptions)
Brian Salomonf865b052018-03-09 09:01:53 -05001467 : INHERITED(ct, overrides, surfType, samples, diText, colorType, alphaType,
1468 std::move(colorSpace), threaded, grCtxOptions)
Mike Klein022cfa22017-09-01 11:53:16 -04001469 , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)) {
Brian Osmanf9810662017-08-30 10:02:10 -04001470 SkASSERT(fExecutor);
1471}
1472
1473Error GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1474 SkString* log) const {
1475 // Draw twice, once with worker threads, and once without. Verify that we get the same result.
1476 // Also, force us to only use the software path renderer, so we really stress-test the threaded
1477 // version of that code.
1478 GrContextOptions contextOptions = this->baseContextOptions();
Brian Osman195c05b2017-08-30 15:14:04 -04001479 contextOptions.fGpuPathRenderers = GpuPathRenderers::kNone;
Greg Daniel024615e2018-05-10 17:25:52 +00001480 contextOptions.fExecutor = fExecutor.get();
Adrienne Walkerab7181d2018-05-14 14:02:03 -07001481
Brian Osmanf9810662017-08-30 10:02:10 -04001482 Error err = this->onDraw(src, dst, wStream, log, contextOptions);
1483 if (!err.isEmpty() || !dst) {
1484 return err;
1485 }
1486
1487 SkBitmap reference;
1488 SkString refLog;
1489 SkDynamicMemoryWStream refStream;
1490 contextOptions.fExecutor = nullptr;
1491 Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1492 if (!refErr.isEmpty()) {
1493 return refErr;
1494 }
1495
Brian Osmane5756ec2017-09-06 17:08:30 -04001496 return compare_bitmaps(reference, *dst);
Brian Osmanf9810662017-08-30 10:02:10 -04001497}
1498
1499/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1500
Brian Salomon00a5eb82018-07-11 15:32:05 -04001501GPUPersistentCacheTestingSink::GPUPersistentCacheTestingSink(
1502 GrContextFactory::ContextType ct,
1503 GrContextFactory::ContextOverrides overrides,
1504 SkCommandLineConfigGpu::SurfType surfType,
1505 int samples,
1506 bool diText,
1507 SkColorType colorType,
1508 SkAlphaType alphaType,
1509 sk_sp<SkColorSpace> colorSpace,
1510 bool threaded,
Brian Osmanf71b0702019-04-03 13:04:16 -04001511 const GrContextOptions& grCtxOptions,
1512 int cacheType)
Brian Salomon00a5eb82018-07-11 15:32:05 -04001513 : INHERITED(ct, overrides, surfType, samples, diText, colorType, alphaType,
Brian Osmanf71b0702019-04-03 13:04:16 -04001514 std::move(colorSpace), threaded, grCtxOptions)
1515 , fCacheType(cacheType) {}
Brian Salomon00a5eb82018-07-11 15:32:05 -04001516
1517Error GPUPersistentCacheTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1518 SkString* log) const {
1519 // Draw twice, once with a cold cache, and again with a warm cache. Verify that we get the same
1520 // result.
1521 sk_gpu_test::MemoryCache memoryCache;
1522 GrContextOptions contextOptions = this->baseContextOptions();
1523 contextOptions.fPersistentCache = &memoryCache;
Brian Osmanf71b0702019-04-03 13:04:16 -04001524 contextOptions.fDisallowGLSLBinaryCaching = (fCacheType == 2);
Brian Salomon00a5eb82018-07-11 15:32:05 -04001525
1526 Error err = this->onDraw(src, dst, wStream, log, contextOptions);
1527 if (!err.isEmpty() || !dst) {
1528 return err;
1529 }
1530
1531 SkBitmap reference;
1532 SkString refLog;
1533 SkDynamicMemoryWStream refStream;
1534 memoryCache.resetNumCacheMisses();
1535 Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1536 if (!refErr.isEmpty()) {
1537 return refErr;
1538 }
Brian Salomon7fc52992018-07-12 16:20:23 -04001539 SkASSERT(!memoryCache.numCacheMisses());
Brian Salomon00a5eb82018-07-11 15:32:05 -04001540
1541 return compare_bitmaps(reference, *dst);
1542}
1543
1544/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
halcanary47ef4d52015-03-03 09:13:09 -08001545static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001546 if (src.size().isEmpty()) {
1547 return "Source has empty dimensions";
1548 }
halcanary47ef4d52015-03-03 09:13:09 -08001549 SkASSERT(doc);
halcanary45420a92016-06-02 12:41:14 -07001550 int pageCount = src.pageCount();
1551 for (int i = 0; i < pageCount; ++i) {
1552 int width = src.size(i).width(), height = src.size(i).height();
halcanary7e798182015-04-14 14:06:18 -07001553 SkCanvas* canvas =
1554 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1555 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001556 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001557 }
halcanary45420a92016-06-02 12:41:14 -07001558 Error err = src.draw(i, canvas);
halcanary7e798182015-04-14 14:06:18 -07001559 if (!err.isEmpty()) {
1560 return err;
1561 }
1562 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001563 }
reedd14df7c2016-09-22 14:12:46 -07001564 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -08001565 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001566 return "";
1567}
1568
halcanary47ef4d52015-03-03 09:13:09 -08001569Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canary23564b92018-09-07 14:33:14 -04001570 SkPDF::Metadata metadata;
halcanary4b656662016-04-27 07:45:18 -07001571 metadata.fTitle = src.name();
1572 metadata.fSubject = "rendering correctness test";
1573 metadata.fCreator = "Skia/DM";
Mike Reeda4daf192017-12-14 13:25:04 -05001574 metadata.fRasterDPI = fRasterDpi;
1575 metadata.fPDFA = fPDFA;
Hal Canaryf34131a2018-12-18 15:11:03 -05001576#if SK_PDF_TEST_EXECUTOR
Hal Canary9a3f5542018-12-10 19:59:07 -05001577 std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool();
1578 metadata.fExecutor = executor.get();
1579#endif
Hal Canary3026d4b2019-01-07 10:00:48 -05001580 auto doc = SkPDF::MakeDocument(dst, metadata);
halcanary47ef4d52015-03-03 09:13:09 -08001581 if (!doc) {
Hal Canary23564b92018-09-07 14:33:14 -04001582 return "SkPDF::MakeDocument() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001583 }
1584 return draw_skdocument(src, doc.get(), dst);
1585}
1586
1587/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1588
1589XPSSink::XPSSink() {}
1590
Hal Canaryabc88d22017-02-06 09:26:49 -05001591#ifdef SK_BUILD_FOR_WIN
Hal Canary5e221e72017-02-06 09:51:42 -05001592static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() {
1593 IXpsOMObjectFactory* factory;
1594 HRN(CoCreateInstance(CLSID_XpsOMObjectFactory,
1595 nullptr,
1596 CLSCTX_INPROC_SERVER,
1597 IID_PPV_ARGS(&factory)));
1598 return SkTScopedComPtr<IXpsOMObjectFactory>(factory);
1599}
1600
1601Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canaryabc88d22017-02-06 09:26:49 -05001602 SkAutoCoInitialize com;
1603 if (!com.succeeded()) {
1604 return "Could not initialize COM.";
1605 }
Hal Canary5e221e72017-02-06 09:51:42 -05001606 SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory();
1607 if (!factory) {
1608 return "Failed to create XPS Factory.";
1609 }
Hal Canary3026d4b2019-01-07 10:00:48 -05001610 auto doc = SkXPS::MakeDocument(dst, factory.get());
halcanary47ef4d52015-03-03 09:13:09 -08001611 if (!doc) {
Hal Canary3026d4b2019-01-07 10:00:48 -05001612 return "SkXPS::MakeDocument() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001613 }
1614 return draw_skdocument(src, doc.get(), dst);
1615}
Hal Canary5e221e72017-02-06 09:51:42 -05001616#else
1617Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1618 return "XPS not supported on this platform.";
1619}
1620#endif
reed54dc4872016-09-13 08:09:45 -07001621
1622/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1623
mtklein9c3f17d2015-01-28 11:35:18 -08001624SKPSink::SKPSink() {}
1625
mtkleinb9eb4ac2015-02-02 18:26:03 -08001626Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001627 SkSize size;
1628 size = src.size();
1629 SkPictureRecorder recorder;
1630 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1631 if (!err.isEmpty()) {
1632 return err;
1633 }
reedca2622b2016-03-18 07:25:55 -07001634 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001635 return "";
1636}
1637
1638/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1639
Hal Canary85c7fe82016-10-25 10:33:27 -04001640Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Mike Klein8f4e2242019-03-20 11:59:00 -05001641 DebugCanvas debugCanvas(src.size().width(), src.size().height());
Hal Canary85c7fe82016-10-25 10:33:27 -04001642 Error err = src.draw(&debugCanvas);
1643 if (!err.isEmpty()) {
1644 return err;
1645 }
Mike Reed5df49342016-11-12 08:06:55 -06001646 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
Hal Canary85c7fe82016-10-25 10:33:27 -04001647 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05001648 SkJSONWriter writer(dst, SkJSONWriter::Mode::kPretty);
1649 writer.beginObject(); // root
1650 debugCanvas.toJSON(writer, dataManager, debugCanvas.getSize(), nullCanvas.get());
1651 writer.endObject(); // root
1652 writer.flush();
1653 return "";
Hal Canary85c7fe82016-10-25 10:33:27 -04001654}
1655
1656/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1657
Bryce Thomas95a7b762018-03-02 13:54:21 -08001658SVGSink::SVGSink(int pageIndex) : fPageIndex(pageIndex) {}
mtklein8a4527e2015-01-31 20:00:58 -08001659
mtkleinb9eb4ac2015-02-02 18:26:03 -08001660Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita718df0a2016-07-15 10:33:29 -07001661#if defined(SK_XML)
Bryce Thomas95a7b762018-03-02 13:54:21 -08001662 if (src.pageCount() > 1) {
1663 int pageCount = src.pageCount();
1664 if (fPageIndex > pageCount - 1) {
1665 return Error(SkStringPrintf("Page index %d too high for document with only %d pages.",
1666 fPageIndex, pageCount));
1667 }
1668 }
Bryce Thomas95a7b762018-03-02 13:54:21 -08001669 return src.draw(fPageIndex,
1670 SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
Mike Reed5df49342016-11-12 08:06:55 -06001671 SkIntToScalar(src.size().height())),
Florin Malita562017b2019-02-14 13:42:15 -05001672 dst)
Bryce Thomas95a7b762018-03-02 13:54:21 -08001673 .get());
fmalita718df0a2016-07-15 10:33:29 -07001674#else
Hal Canary327ef032018-03-22 13:10:51 -04001675 (void)fPageIndex;
fmalita718df0a2016-07-15 10:33:29 -07001676 return Error("SVG sink is disabled.");
1677#endif // SK_XML
mtklein8a4527e2015-01-31 20:00:58 -08001678}
1679
1680/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1681
brianosmanb109b8c2016-06-16 13:03:24 -07001682RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
mtklein27c3fdd2016-02-26 14:43:21 -08001683 : fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001684 , fColorSpace(std::move(colorSpace)) {}
mtklein748ca3b2015-01-15 10:56:12 -08001685
Mike Klein48b64902018-07-25 13:28:44 -04001686Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001687 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001688 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1689 SkAlphaType alphaType = kPremul_SkAlphaType;
1690 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1691
Mike Reed086a4272017-07-18 10:53:11 -04001692 dst->allocPixelsFlags(SkImageInfo::Make(size.width(), size.height(),
1693 fColorType, alphaType, fColorSpace),
1694 SkBitmap::kZeroPixels_AllocFlag);
Yuqian Lib8b62532018-02-23 14:13:36 +08001695
mtklein748ca3b2015-01-15 10:56:12 -08001696 SkCanvas canvas(*dst);
1697 return src.draw(&canvas);
1698}
1699
1700/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1701
mtkleina16e69e2015-05-05 11:38:45 -07001702// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001703// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001704// Several examples below.
1705
mtkleincbf89782016-02-19 14:27:14 -08001706template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001707static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001708 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001709 class ProxySrc : public Src {
1710 public:
mtkleincbf89782016-02-19 14:27:14 -08001711 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001712 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001713 Name name() const override { return "ProxySrc"; }
1714 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001715 private:
mtkleincbf89782016-02-19 14:27:14 -08001716 SkISize fSize;
1717 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001718 };
msarett62d3b102015-12-10 15:14:27 -08001719 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001720}
1721
1722/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1723
Mike Klein84836b72019-03-21 11:31:36 -05001724static DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
mtklein4a34ecb2016-01-08 10:19:35 -08001725
1726// Is *bitmap identical to what you get drawing src into sink?
1727static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1728 // We can only check raster outputs.
1729 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1730 if (FLAGS_check && bitmap) {
1731 SkBitmap reference;
1732 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001733 SkDynamicMemoryWStream wStream;
1734 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001735 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1736 SkASSERT(err.isEmpty());
1737 if (!err.isEmpty()) {
1738 return err;
1739 }
Brian Osmane5756ec2017-09-06 17:08:30 -04001740 return compare_bitmaps(reference, *bitmap);
mtklein4a34ecb2016-01-08 10:19:35 -08001741 }
1742 return "";
1743}
1744
1745/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1746
mtkleind603b222015-02-17 11:13:33 -08001747static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1748 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1749 matrix->mapRect(&bounds);
1750 matrix->postTranslate(-bounds.x(), -bounds.y());
Hal Canaryfafe1352017-04-11 12:12:02 -04001751 return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())};
mtkleind603b222015-02-17 11:13:33 -08001752}
1753
msarett62d3b102015-12-10 15:14:27 -08001754ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001755
mtkleinb9eb4ac2015-02-02 18:26:03 -08001756Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001757 SkMatrix matrix = fMatrix;
1758 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
Ben Wagner145dbcd2016-11-03 14:40:50 -04001759 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001760 canvas->concat(matrix);
1761 return src.draw(canvas);
1762 });
mtklein748ca3b2015-01-15 10:56:12 -08001763}
1764
mtkleind603b222015-02-17 11:13:33 -08001765// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1766// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001767ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001768
1769Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1770 Error err = fSink->draw(src, bitmap, stream, log);
1771 if (!err.isEmpty()) {
1772 return err;
1773 }
1774
1775 SkMatrix inverse;
1776 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1777 return "Cannot upright --matrix.";
1778 }
1779 SkMatrix upright = SkMatrix::I();
1780 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1781 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1782 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1783 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1784
1785 SkBitmap uprighted;
1786 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1787 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1788
1789 SkCanvas canvas(uprighted);
1790 canvas.concat(upright);
1791 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07001792 paint.setBlendMode(SkBlendMode::kSrc);
mtkleind603b222015-02-17 11:13:33 -08001793 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1794
1795 *bitmap = uprighted;
mtkleind603b222015-02-17 11:13:33 -08001796 return "";
1797}
1798
mtklein748ca3b2015-01-15 10:56:12 -08001799/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1800
mtkleina16e69e2015-05-05 11:38:45 -07001801Error ViaSerialization::draw(
1802 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001803 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001804 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001805 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001806 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1807 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001808 if (!err.isEmpty()) {
1809 return err;
1810 }
reedca2622b2016-03-18 07:25:55 -07001811 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001812
1813 // Serialize it and then deserialize it.
reed39eaf5f2016-09-15 07:19:35 -07001814 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
mtklein748ca3b2015-01-15 10:56:12 -08001815
Ben Wagner1861e882018-04-04 17:40:46 -04001816 err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001817 canvas->drawPicture(deserialized);
Ben Wagner1861e882018-04-04 17:40:46 -04001818 return "";
mtkleina16e69e2015-05-05 11:38:45 -07001819 });
Ben Wagner1861e882018-04-04 17:40:46 -04001820 if (!err.isEmpty()) {
1821 return err;
1822 }
1823
1824 return check_against_reference(bitmap, src, fSink.get());
mtklein748ca3b2015-01-15 10:56:12 -08001825}
1826
1827/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1828
msarett62d3b102015-12-10 15:14:27 -08001829ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1830 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001831 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001832 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001833 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001834
mtkleinb9eb4ac2015-02-02 18:26:03 -08001835Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001836 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001837 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001838 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1839 SkIntToScalar(size.height()),
1840 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001841 if (!err.isEmpty()) {
1842 return err;
1843 }
reedca2622b2016-03-18 07:25:55 -07001844 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001845
Ben Wagner145dbcd2016-11-03 14:40:50 -04001846 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001847 const int xTiles = (size.width() + fW - 1) / fW,
1848 yTiles = (size.height() + fH - 1) / fH;
1849 SkMultiPictureDraw mpd(xTiles*yTiles);
reede8f30622016-03-23 18:59:25 -07001850 SkTArray<sk_sp<SkSurface>> surfaces;
1851// surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001852
mtkleina16e69e2015-05-05 11:38:45 -07001853 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1854 for (int j = 0; j < yTiles; j++) {
1855 for (int i = 0; i < xTiles; i++) {
1856 // This lets our ultimate Sink determine the best kind of surface.
1857 // E.g., if it's a GpuSink, the surfaces and images are textures.
reede8f30622016-03-23 18:59:25 -07001858 auto s = canvas->makeSurface(info);
Cary Clarka24712e2018-09-05 18:41:40 +00001859 if (!s) {
1860 s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces.
1861 }
reede8f30622016-03-23 18:59:25 -07001862 surfaces.push_back(s);
mtkleina16e69e2015-05-05 11:38:45 -07001863 SkCanvas* c = s->getCanvas();
1864 c->translate(SkIntToScalar(-i * fW),
1865 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
reedca2622b2016-03-18 07:25:55 -07001866 mpd.add(c, pic.get());
mtklein748ca3b2015-01-15 10:56:12 -08001867 }
mtklein748ca3b2015-01-15 10:56:12 -08001868 }
mtkleina16e69e2015-05-05 11:38:45 -07001869 mpd.draw();
1870 for (int j = 0; j < yTiles; j++) {
1871 for (int i = 0; i < xTiles; i++) {
reed9ce9d672016-03-17 10:51:11 -07001872 sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot());
mtkleina16e69e2015-05-05 11:38:45 -07001873 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1874 }
1875 }
mtkleina16e69e2015-05-05 11:38:45 -07001876 return "";
1877 });
mtklein748ca3b2015-01-15 10:56:12 -08001878}
1879
mtkleinb7e8d692015-04-07 08:30:32 -07001880/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1881
Brian Salomon57904202018-12-17 14:45:00 -05001882ViaDDL::ViaDDL(int numReplays, int numDivisions, Sink* sink)
1883 : Via(sink), fNumReplays(numReplays), fNumDivisions(numDivisions) {}
Robert Phillips33f02ed2018-03-27 08:06:57 -04001884
Robert Phillips33f02ed2018-03-27 08:06:57 -04001885Error ViaDDL::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1886 auto size = src.size();
1887 SkPictureRecorder recorder;
1888 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1889 SkIntToScalar(size.height())));
1890 if (!err.isEmpty()) {
1891 return err;
1892 }
1893 sk_sp<SkPicture> inputPicture(recorder.finishRecordingAsPicture());
1894
1895 // this is our ultimate final drawing area/rect
1896 SkIRect viewport = SkIRect::MakeWH(size.fWidth, size.fHeight);
1897
Brian Salomon7d88f312019-02-28 10:03:03 -05001898 DDLPromiseImageHelper promiseImageHelper;
Robert Phillips96601082018-05-29 16:13:26 -04001899 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture.get());
1900 if (!compressedPictureData) {
1901 return SkStringPrintf("ViaDDL: Couldn't deflate SkPicture");
Robert Phillips33f02ed2018-03-27 08:06:57 -04001902 }
Brian Salomon57904202018-12-17 14:45:00 -05001903 auto draw = [&](SkCanvas* canvas) -> Error {
1904 GrContext* context = canvas->getGrContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001905 if (!context || !context->priv().getGpu()) {
Brian Salomon57904202018-12-17 14:45:00 -05001906 return SkStringPrintf("DDLs are GPU only");
1907 }
Robert Phillips33f02ed2018-03-27 08:06:57 -04001908
Brian Salomon57904202018-12-17 14:45:00 -05001909 // This is here bc this is the first point where we have access to the context
1910 promiseImageHelper.uploadAllToGPU(context);
Brian Salomon7d88f312019-02-28 10:03:03 -05001911 // We draw N times, with a clear between.
Brian Salomon57904202018-12-17 14:45:00 -05001912 for (int replay = 0; replay < fNumReplays; ++replay) {
1913 if (replay > 0) {
1914 // Clear the drawing of the previous replay
Brian Salomon8d23a582018-12-19 11:35:20 -05001915 canvas->clear(SK_ColorTRANSPARENT);
Brian Salomon57904202018-12-17 14:45:00 -05001916 }
1917 // First, create all the tiles (including their individual dest surfaces)
1918 DDLTileHelper tiles(canvas, viewport, fNumDivisions);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001919
Brian Salomon57904202018-12-17 14:45:00 -05001920 // Second, reinflate the compressed picture individually for each thread
Brian Salomon1bf0ed82019-01-16 13:51:35 -05001921 // This recreates the promise SkImages on each replay iteration. We are currently
1922 // relying on this to test using a SkPromiseImageTexture to fulfill different
1923 // SkImages. On each replay the promise SkImages are recreated in createSKPPerTile.
Brian Salomon57904202018-12-17 14:45:00 -05001924 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001925
Brian Salomon57904202018-12-17 14:45:00 -05001926 // Third, create the DDLs in parallel
1927 tiles.createDDLsInParallel();
Robert Phillips33f02ed2018-03-27 08:06:57 -04001928
Brian Salomon57904202018-12-17 14:45:00 -05001929 if (replay == fNumReplays - 1) {
1930 // This drops the promiseImageHelper's refs on all the promise images if we're in
1931 // the last run.
1932 promiseImageHelper.reset();
1933 }
Robert Phillips33f02ed2018-03-27 08:06:57 -04001934
Brian Salomon57904202018-12-17 14:45:00 -05001935 // Fourth, synchronously render the display lists into the dest tiles
1936 // TODO: it would be cool to not wait until all the tiles are drawn to begin
1937 // drawing to the GPU and composing to the final surface
1938 tiles.drawAllTilesAndFlush(context, false);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001939
Brian Salomon57904202018-12-17 14:45:00 -05001940 // Finally, compose the drawn tiles into the result
1941 // Note: the separation between the tiles and the final composition better
1942 // matches Chrome but costs us a copy
1943 tiles.composeAllTiles(canvas);
1944 context->flush();
1945 }
1946 return "";
1947 };
1948 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, draw);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001949}
1950
Robert Phillips33f02ed2018-03-27 08:06:57 -04001951/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1952
mtklein4a34ecb2016-01-08 10:19:35 -08001953Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1954 auto size = src.size();
Ben Wagner1861e882018-04-04 17:40:46 -04001955 Error err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtklein4a34ecb2016-01-08 10:19:35 -08001956 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001957 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08001958 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1959 SkIntToScalar(size.height())));
1960 if (!err.isEmpty()) {
1961 return err;
1962 }
reedca2622b2016-03-18 07:25:55 -07001963 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08001964 canvas->drawPicture(pic);
Ben Wagner1861e882018-04-04 17:40:46 -04001965 return err;
mtklein4a34ecb2016-01-08 10:19:35 -08001966 });
Ben Wagner1861e882018-04-04 17:40:46 -04001967 if (!err.isEmpty()) {
1968 return err;
1969 }
1970
1971 return check_against_reference(bitmap, src, fSink.get());
mtklein4a34ecb2016-01-08 10:19:35 -08001972}
1973
1974/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1975
Mike Reedbae888e2017-02-18 16:50:45 -05001976#ifdef TEST_VIA_SVG
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001977#include "experimental/svg/model/SkSVGDOM.h"
1978#include "include/svg/SkSVGCanvas.h"
1979#include "src/xml/SkXMLWriter.h"
Mike Reedf67c4592017-02-17 17:06:11 -05001980
1981Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1982 auto size = src.size();
1983 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1984 SkDynamicMemoryWStream wstream;
1985 SkXMLStreamWriter writer(&wstream);
1986 Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get());
1987 if (!err.isEmpty()) {
1988 return err;
1989 }
1990 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
1991 auto dom = SkSVGDOM::MakeFromStream(*rstream);
1992 if (dom) {
1993 dom->setContainerSize(SkSize::Make(size));
1994 dom->render(canvas);
1995 }
1996 return "";
1997 });
1998}
Mike Reedbae888e2017-02-18 16:50:45 -05001999#endif
Mike Reedf67c4592017-02-17 17:06:11 -05002000
mtklein748ca3b2015-01-15 10:56:12 -08002001} // namespace DM