blob: 9f7814f21fa02bc8d25b2d1101c4024f91719b07 [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
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMSrcSink.h"
Brian Salomonf865b052018-03-09 09:01:53 -05009#include <cmath>
10#include <functional>
11#include "../src/jumper/SkJumper.h"
msarett9876ac52016-06-01 14:47:18 -070012#include "Resources.h"
msarett3d9d7a72015-10-21 10:27:10 -070013#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050014#include "SkAutoMalloc.h"
Brian Osmane5756ec2017-09-06 17:08:30 -040015#include "SkBase64.h"
scroggof24f2242015-03-03 08:59:20 -080016#include "SkCodec.h"
msarettb714fb02016-01-22 14:46:42 -080017#include "SkCodecImageGenerator.h"
msarett888dc162016-05-23 10:21:17 -070018#include "SkColorSpace.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000019#include "SkColorSpaceXform.h"
Mike Klein841101d2017-03-10 09:55:51 -050020#include "SkColorSpaceXformCanvas.h"
Hal Canary95e3c052017-01-11 12:44:43 -050021#include "SkColorSpace_XYZ.h"
mtkleina16e69e2015-05-05 11:38:45 -070022#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070023#include "SkCommonFlagsGpu.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070024#include "SkData.h"
Hal Canary85c7fe82016-10-25 10:33:27 -040025#include "SkDebugCanvas.h"
Robert Phillipsad8a43f2017-08-30 12:06:35 -040026#include "SkDeferredDisplayListRecorder.h"
mtklein748ca3b2015-01-15 10:56:12 -080027#include "SkDocument.h"
Brian Osmanf9810662017-08-30 10:02:10 -040028#include "SkExecutor.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070029#include "SkImageGenerator.h"
msarett18976312016-03-09 14:20:58 -080030#include "SkImageGeneratorCG.h"
msarettfc0b6d12016-03-17 13:50:17 -070031#include "SkImageGeneratorWIC.h"
Mike Reed7fcfb622018-02-09 13:26:46 -050032#include "SkImageInfoPriv.h"
mtklein8bbbb692016-08-15 12:56:00 -070033#include "SkLiteDL.h"
34#include "SkLiteRecorder.h"
mtkleinc8be09a2016-01-04 18:56:57 -080035#include "SkMallocPixelRef.h"
Hal Canary45cde312017-04-03 16:06:42 -040036#include "SkMultiPictureDocumentPriv.h"
mtklein748ca3b2015-01-15 10:56:12 -080037#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080038#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080039#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050040#include "SkOSPath.h"
msarett9e9444c2016-02-03 12:39:10 -080041#include "SkOpts.h"
Mike Reede7a58322017-12-20 14:09:20 -050042#include "SkPictureCommon.h"
mtkleinffa901a2015-03-16 10:38:07 -070043#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080044#include "SkPictureRecorder.h"
reed54dc4872016-09-13 08:09:45 -070045#include "SkPipe.h"
Brian Osmane5756ec2017-09-06 17:08:30 -040046#include "SkPngEncoder.h"
mtklein748ca3b2015-01-15 10:56:12 -080047#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070048#include "SkRecordDraw.h"
49#include "SkRecorder.h"
fmalita2aafe6f2015-02-06 12:51:10 -080050#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080051#include "SkStream.h"
Brian Salomonf865b052018-03-09 09:01:53 -050052#include "SkSurfaceCharacterization.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000053#include "SkSwizzler.h"
Brian Salomonf865b052018-03-09 09:01:53 -050054#include "SkTLogic.h"
Robert Phillipsad8a43f2017-08-30 12:06:35 -040055#include "SkTaskGroup.h"
Yuqian Lib8b62532018-02-23 14:13:36 +080056#include "SkThreadedBMPDevice.h"
msarettfc0b6d12016-03-17 13:50:17 -070057#if defined(SK_BUILD_FOR_WIN)
58 #include "SkAutoCoInitialize.h"
Hal Canary5e221e72017-02-06 09:51:42 -050059 #include "SkHRESULT.h"
60 #include "SkTScopedComPtr.h"
61 #include <XpsObjectModel.h>
msarettfc0b6d12016-03-17 13:50:17 -070062#endif
63
Florin Malita124d5af2017-12-31 17:02:26 -050064#if !defined(SK_BUILD_FOR_GOOGLE3)
Florin Malita54f65c42018-01-16 17:04:30 -050065 #include "Skottie.h"
Florin Malita124d5af2017-12-31 17:02:26 -050066#endif
67
fmalita718df0a2016-07-15 10:33:29 -070068#if defined(SK_XML)
fmalitaa48f0e32016-08-04 06:26:05 -070069 #include "SkSVGDOM.h"
fmalita718df0a2016-07-15 10:33:29 -070070 #include "SkXMLWriter.h"
71#endif
72
Robert Phillips0c4b7b12018-03-06 08:20:37 -050073#if SK_SUPPORT_GPU
Brian Salomonf865b052018-03-09 09:01:53 -050074#include "GrBackendSurface.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050075#include "GrContextPriv.h"
Brian Salomonf865b052018-03-09 09:01:53 -050076#include "GrGpu.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050077#endif
78
halcanary7e798182015-04-14 14:06:18 -070079DEFINE_bool(multiPage, false, "For document-type backends, render the source"
80 " into multiple pages");
scroggo3ac66e92016-02-08 15:09:48 -080081DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070082
bsalomon3724e572016-03-30 18:56:19 -070083using sk_gpu_test::GrContextFactory;
84
mtklein748ca3b2015-01-15 10:56:12 -080085namespace DM {
86
mtklein748ca3b2015-01-15 10:56:12 -080087GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
88
89Error GMSrc::draw(SkCanvas* canvas) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040090 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080091 gm->draw(canvas);
92 return "";
93}
94
95SkISize GMSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040096 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080097 return gm->getISize();
98}
99
100Name GMSrc::name() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400101 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -0800102 return gm->getName();
103}
104
bsalomon4ee6bd82015-05-27 13:23:23 -0700105void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400106 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -0700107 gm->modifyGrContextOptions(options);
108}
109
mtklein748ca3b2015-01-15 10:56:12 -0800110/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
111
msarettd1227a72016-05-18 06:23:57 -0700112BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
msaretta5783ae2015-09-08 15:35:32 -0700113 : fPath(path)
msaretta5783ae2015-09-08 15:35:32 -0700114 , fMode(mode)
115 , fDstColorType(dstColorType)
116 , fSampleSize(sampleSize)
117{}
118
119bool BRDSrc::veto(SinkFlags flags) const {
120 // No need to test to non-raster or indirect backends.
121 return flags.type != SinkFlags::kRaster
122 || flags.approach != SinkFlags::kDirect;
123}
124
msarettd1227a72016-05-18 06:23:57 -0700125static SkBitmapRegionDecoder* create_brd(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700126 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msaretta5783ae2015-09-08 15:35:32 -0700127 if (!encoded) {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400128 return nullptr;
msaretta5783ae2015-09-08 15:35:32 -0700129 }
reed42943c82016-09-12 12:01:44 -0700130 return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy);
msaretta5783ae2015-09-08 15:35:32 -0700131}
132
Matt Sarett334df3a2016-12-15 18:17:33 -0500133static inline void alpha8_to_gray8(SkBitmap* bitmap) {
134 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
135 // them back to kGray8 so our test framework can draw them correctly.
136 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
137 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
138 .makeAlphaType(kOpaque_SkAlphaType);
139 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
140 }
141}
142
msaretta5783ae2015-09-08 15:35:32 -0700143Error BRDSrc::draw(SkCanvas* canvas) const {
Matt Saretta40d9c82017-05-19 15:21:05 -0400144 if (canvas->imageInfo().colorSpace() &&
145 kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) {
146 // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only
147 // run these tests on legacy canvases.
148 // We allow an exception for F16, since Android uses F16.
149 return Error::Nonfatal("Skip testing to color correct canvas.");
150 }
151
msaretta5783ae2015-09-08 15:35:32 -0700152 SkColorType colorType = canvas->imageInfo().colorType();
153 if (kRGB_565_SkColorType == colorType &&
154 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
155 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
156 }
157 switch (fDstColorType) {
158 case CodecSrc::kGetFromCanvas_DstColorType:
159 break;
msaretta5783ae2015-09-08 15:35:32 -0700160 case CodecSrc::kGrayscale_Always_DstColorType:
161 colorType = kGray_8_SkColorType;
162 break;
msarett34e0ec42016-04-22 16:27:24 -0700163 default:
164 SkASSERT(false);
165 break;
msaretta5783ae2015-09-08 15:35:32 -0700166 }
167
Ben Wagner145dbcd2016-11-03 14:40:50 -0400168 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700169 if (nullptr == brd.get()) {
170 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
171 }
172
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400173 if (kRGB_565_SkColorType == colorType) {
174 auto recommendedCT = brd->computeOutputColorType(colorType);
175 if (recommendedCT != colorType) {
176 return Error::Nonfatal("Skip decoding non-opaque to 565.");
177 }
178 }
179
msaretta5783ae2015-09-08 15:35:32 -0700180 const uint32_t width = brd->width();
181 const uint32_t height = brd->height();
182 // Visually inspecting very small output images is not necessary.
183 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
184 return Error::Nonfatal("Scaling very small images is uninteresting.");
185 }
186 switch (fMode) {
187 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700188 SkBitmap bitmap;
189 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
Matt Sarett68feef42017-04-11 09:51:32 -0400190 fSampleSize, colorType, false, SkColorSpace::MakeSRGB())) {
mtklein9b439152015-12-09 13:02:26 -0800191 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700192 }
Matt Sarett334df3a2016-12-15 18:17:33 -0500193 alpha8_to_gray8(&bitmap);
Matt Sarett8db74f12017-06-14 13:02:05 +0000194
msarett35e5d1b2015-10-27 12:50:25 -0700195 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700196 return "";
197 }
198 case kDivisor_Mode: {
199 const uint32_t divisor = 2;
200 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800201 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700202 }
203
204 // Use a border to test subsets that extend outside the image.
205 // We will not allow the border to be larger than the image dimensions. Allowing
206 // these large borders causes off by one errors that indicate a problem with the
207 // test suite, not a problem with the implementation.
208 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
209 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
210 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
211
212 // We may need to clear the canvas to avoid uninitialized memory.
213 // Assume we are scaling a 780x780 image with sampleSize = 8.
214 // The output image should be 97x97.
215 // Each subset will be 390x390.
216 // Each scaled subset be 48x48.
217 // Four scaled subsets will only fill a 96x96 image.
218 // The bottom row and last column will not be touched.
219 // This is an unfortunate result of our rounding rules when scaling.
220 // Maybe we need to consider testing scaled subsets without trying to
221 // combine them to match the full scaled image? Or maybe this is the
222 // best we can do?
223 canvas->clear(0);
224
225 for (uint32_t x = 0; x < divisor; x++) {
226 for (uint32_t y = 0; y < divisor; y++) {
227 // Calculate the subset dimensions
228 uint32_t subsetWidth = width / divisor;
229 uint32_t subsetHeight = height / divisor;
230 const int left = x * subsetWidth;
231 const int top = y * subsetHeight;
232
233 // Increase the size of the last subset in each row or column, when the
234 // divisor does not divide evenly into the image dimensions
235 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
236 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
237
238 // Increase the size of the subset in order to have a border on each side
239 const int decodeLeft = left - unscaledBorder;
240 const int decodeTop = top - unscaledBorder;
241 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
242 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700243 SkBitmap bitmap;
244 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
Matt Sarett68feef42017-04-11 09:51:32 -0400245 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false,
246 SkColorSpace::MakeSRGB())) {
mtklein9b439152015-12-09 13:02:26 -0800247 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700248 }
msaretta5783ae2015-09-08 15:35:32 -0700249
Matt Sarett334df3a2016-12-15 18:17:33 -0500250 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700251 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700252 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
253 (SkScalar) (subsetWidth / fSampleSize),
254 (SkScalar) (subsetHeight / fSampleSize)),
255 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
256 (SkScalar) (top / fSampleSize),
257 (SkScalar) (subsetWidth / fSampleSize),
258 (SkScalar) (subsetHeight / fSampleSize)),
259 nullptr);
260 }
261 }
262 return "";
263 }
264 default:
265 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800266 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700267 }
268}
269
270SkISize BRDSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400271 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700272 if (brd) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400273 return {SkTMax(1, brd->width() / (int)fSampleSize),
274 SkTMax(1, brd->height() / (int)fSampleSize)};
msaretta5783ae2015-09-08 15:35:32 -0700275 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400276 return {0, 0};
msaretta5783ae2015-09-08 15:35:32 -0700277}
278
279static SkString get_scaled_name(const Path& path, float scale) {
280 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
281}
282
283Name BRDSrc::name() const {
284 // We will replicate the names used by CodecSrc so that images can
285 // be compared in Gold.
286 if (1 == fSampleSize) {
287 return SkOSPath::Basename(fPath.c_str());
288 }
msarett4b0778e2015-11-13 09:59:11 -0800289 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700290}
291
292/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
293
scroggo3ac66e92016-02-08 15:09:48 -0800294static bool serial_from_path_name(const SkString& path) {
295 if (!FLAGS_RAW_threading) {
296 static const char* const exts[] = {
297 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
298 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
299 };
300 const char* actualExt = strrchr(path.c_str(), '.');
301 if (actualExt) {
302 actualExt++;
303 for (auto* ext : exts) {
304 if (0 == strcmp(ext, actualExt)) {
305 return true;
306 }
307 }
308 }
309 }
310 return false;
311}
312
scroggoc5560be2016-02-03 09:42:42 -0800313CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
314 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700315 : fPath(path)
316 , fMode(mode)
317 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800318 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700319 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800320 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700321{}
mtklein748ca3b2015-01-15 10:56:12 -0800322
mtklein99cab4e2015-07-31 06:43:04 -0700323bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800324 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800325 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700326}
scroggo9b77ddd2015-03-19 06:03:39 -0700327
msarett34e0ec42016-04-22 16:27:24 -0700328// Allows us to test decodes to non-native 8888.
msarettb1be46b2016-05-17 08:52:11 -0700329static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700330 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
331 return;
332 }
333
334 for (int y = 0; y < bitmap.height(); y++) {
335 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
336 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
337 }
338}
339
msarett9e9444c2016-02-03 12:39:10 -0800340// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
341// This allows us to still test unpremultiplied decodes.
msarettb1be46b2016-05-17 08:52:11 -0700342static void premultiply_if_necessary(SkBitmap& bitmap) {
msarett9e9444c2016-02-03 12:39:10 -0800343 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
344 return;
345 }
346
347 switch (bitmap.colorType()) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400348 case kRGBA_F16_SkColorType: {
349 SkJumper_MemoryCtx ctx = { bitmap.getAddr(0,0), bitmap.rowBytesAsPixels() };
350 SkRasterPipeline_<256> p;
351 p.append(SkRasterPipeline::load_f16, &ctx);
352 p.append(SkRasterPipeline::premul);
353 p.append(SkRasterPipeline::store_f16, &ctx);
354 p.run(0,0, bitmap.width(), bitmap.height());
355 }
Leon Scroggins IIIeceee042017-05-23 16:58:09 -0400356 break;
msarett9e9444c2016-02-03 12:39:10 -0800357 case kN32_SkColorType:
358 for (int y = 0; y < bitmap.height(); y++) {
359 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
360 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
361 }
362 break;
msarett9e9444c2016-02-03 12:39:10 -0800363 default:
364 // No need to premultiply kGray or k565 outputs.
365 break;
366 }
msarette1daa482016-02-03 15:31:18 -0800367
368 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
369 // bitmap as kPremul.
370 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800371}
372
msarettb1be46b2016-05-17 08:52:11 -0700373static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
scroggoba584892016-05-20 13:56:13 -0700374 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
msarett3d9d7a72015-10-21 10:27:10 -0700375 switch (dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700376 case CodecSrc::kGrayscale_Always_DstColorType:
scroggo1a361922016-05-20 14:27:16 -0700377 if (kRGB_565_SkColorType == canvasColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700378 return false;
379 }
scroggoc5560be2016-02-03 09:42:42 -0800380 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700381 break;
msarett34e0ec42016-04-22 16:27:24 -0700382 case CodecSrc::kNonNative8888_Always_DstColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400383 if (kRGB_565_SkColorType == canvasColorType
384 || kRGBA_F16_SkColorType == canvasColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700385 return false;
386 }
387#ifdef SK_PMCOLOR_IS_RGBA
388 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
389#else
390 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
391#endif
392 break;
msarett3d9d7a72015-10-21 10:27:10 -0700393 default:
msarett55f7bdd2016-02-16 13:24:54 -0800394 if (kRGB_565_SkColorType == canvasColorType &&
395 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
396 return false;
397 }
Matt Sarett09a1c082017-02-01 15:34:22 -0800398
399 if (kRGBA_F16_SkColorType == canvasColorType) {
Brian Osman36703d92017-12-12 14:09:31 -0500400 sk_sp<SkColorSpace> linearSpace = decodeInfo->colorSpace()->makeLinearGamma();
Matt Sarett09a1c082017-02-01 15:34:22 -0800401 *decodeInfo = decodeInfo->makeColorSpace(std::move(linearSpace));
402 }
403
scroggoc5560be2016-02-03 09:42:42 -0800404 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700405 break;
406 }
407
scroggoba584892016-05-20 13:56:13 -0700408 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700409 return true;
410}
411
msarettb1be46b2016-05-17 08:52:11 -0700412static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000413 CodecSrc::DstColorType dstColorType,
msarettb1be46b2016-05-17 08:52:11 -0700414 SkScalar left = 0, SkScalar top = 0) {
msarettb1be46b2016-05-17 08:52:11 -0700415 SkBitmap bitmap;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000416 bitmap.installPixels(info, pixels, rowBytes);
msarettb1be46b2016-05-17 08:52:11 -0700417 premultiply_if_necessary(bitmap);
418 swap_rb_if_necessary(bitmap, dstColorType);
419 canvas->drawBitmap(bitmap, left, top);
420}
421
Matt Sarettf485cf92017-05-26 08:58:03 -0400422// For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or
423// color format conversions should be performed by the codec. Sometimes the output of the
424// decode will be in an interesting color space. On our srgb and f16 backends, we need to
425// "pretend" that the color space is standard sRGB to avoid triggering color conversion
426// at draw time.
427static void set_bitmap_color_space(SkImageInfo* info) {
428 if (kRGBA_F16_SkColorType == info->colorType()) {
429 *info = info->makeColorSpace(SkColorSpace::MakeSRGBLinear());
430 } else {
431 *info = info->makeColorSpace(SkColorSpace::MakeSRGB());
432 }
433}
434
mtkleine0effd62015-07-29 06:37:28 -0700435Error CodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700436 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800437 if (!encoded) {
438 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
439 }
msarettb714fb02016-01-22 14:46:42 -0800440
Mike Reedede7bac2017-07-23 15:30:02 -0400441 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700442 if (nullptr == codec.get()) {
443 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700444 }
445
scroggoba584892016-05-20 13:56:13 -0700446 SkImageInfo decodeInfo = codec->getInfo();
447 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
448 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800449 return Error::Nonfatal("Skipping uninteresting test.");
msarett438b2ad2015-04-09 12:43:10 -0700450 }
451
msarett0a242972015-06-11 14:27:27 -0700452 // Try to scale the image if it is desired
453 SkISize size = codec->getScaledDimensions(fScale);
454 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
455 return Error::Nonfatal("Test without scaling is uninteresting.");
456 }
msarettb32758a2015-08-18 13:22:46 -0700457
458 // Visually inspecting very small output images is not necessary. We will
459 // cover these cases in unit testing.
460 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
461 return Error::Nonfatal("Scaling very small images is uninteresting.");
462 }
msarett0a242972015-06-11 14:27:27 -0700463 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
464
Mike Reed7fcfb622018-02-09 13:26:46 -0500465 const int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700466 const size_t rowBytes = size.width() * bpp;
Mike Reedf0ffb892017-10-03 14:47:21 -0400467 const size_t safeSize = decodeInfo.computeByteSize(rowBytes);
scroggo19b91532016-10-24 09:03:26 -0700468 SkAutoMalloc pixels(safeSize);
msarett438b2ad2015-04-09 12:43:10 -0700469
msarettbb25b532016-01-13 09:31:39 -0800470 SkCodec::Options options;
Matt Saretta40d9c82017-05-19 15:21:05 -0400471 options.fPremulBehavior = canvas->imageInfo().colorSpace() ?
472 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
msarettbb25b532016-01-13 09:31:39 -0800473 if (kCodecZeroInit_Mode == fMode) {
msarettb1be46b2016-05-17 08:52:11 -0700474 memset(pixels.get(), 0, size.height() * rowBytes);
msarettbb25b532016-01-13 09:31:39 -0800475 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
476 }
msarett34e0ec42016-04-22 16:27:24 -0700477
478 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400479 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700480 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
481 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
482 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
483 }
scroggo9b77ddd2015-03-19 06:03:39 -0700484
scroggo9c59ebc2015-03-25 13:48:49 -0700485 switch (fMode) {
scroggo19b91532016-10-24 09:03:26 -0700486 case kAnimated_Mode: {
487 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
488 if (frameInfos.size() <= 1) {
489 return SkStringPrintf("%s is not an animated image.", fPath.c_str());
490 }
491
scroggo90e971a2016-10-25 06:11:01 -0700492 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
493 // into. "factor" is the number of frames to draw on one row. There will be
494 // up to "factor" rows as well.
495 const float root = sqrt((float) frameInfos.size());
496 const int factor = sk_float_ceil2int(root);
497
scroggo19b91532016-10-24 09:03:26 -0700498 // Used to cache a frame that future frames will depend on.
499 SkAutoMalloc priorFramePixels;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400500 int cachedFrame = SkCodec::kNone;
501 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
scroggo19b91532016-10-24 09:03:26 -0700502 options.fFrameIndex = i;
503 // Check for a prior frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400504 const int reqFrame = frameInfos[i].fRequiredFrame;
scroggo19b91532016-10-24 09:03:26 -0700505 if (reqFrame != SkCodec::kNone && reqFrame == cachedFrame
506 && priorFramePixels.get()) {
507 // Copy into pixels
508 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400509 options.fPriorFrame = reqFrame;
scroggo19b91532016-10-24 09:03:26 -0700510 } else {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400511 options.fPriorFrame = SkCodec::kNone;
scroggo19b91532016-10-24 09:03:26 -0700512 }
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500513 SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000514 rowBytes, &options);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500515 if (SkCodec::kInvalidInput == result && i > 0) {
516 // Some of our test images have truncated later frames. Treat that
517 // the same as incomplete.
518 result = SkCodec::kIncompleteInput;
519 }
scroggo19b91532016-10-24 09:03:26 -0700520 switch (result) {
521 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400522 case SkCodec::kErrorInInput:
scroggo90e971a2016-10-25 06:11:01 -0700523 case SkCodec::kIncompleteInput: {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400524 // If the next frame depends on this one, store it in priorFrame.
525 // It is possible that we may discard a frame that future frames depend on,
526 // but the codec will simply redecode the discarded frame.
527 // Do this before calling draw_to_canvas, which premultiplies in place. If
528 // we're decoding to unpremul, we want to pass the unmodified frame to the
529 // codec for decoding the next frame.
530 if (static_cast<size_t>(i+1) < frameInfos.size()
531 && frameInfos[i+1].fRequiredFrame == i) {
532 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
533 cachedFrame = i;
534 }
535
scroggo90e971a2016-10-25 06:11:01 -0700536 SkAutoCanvasRestore acr(canvas, true);
537 const int xTranslate = (i % factor) * decodeInfo.width();
538 const int yTranslate = (i / factor) * decodeInfo.height();
539 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
Leon Scroggins571b30f2017-07-11 17:35:31 +0000540 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
Leon Scroggins III674a1842017-07-06 12:26:09 -0400541 if (result != SkCodec::kSuccess) {
scroggo19b91532016-10-24 09:03:26 -0700542 return "";
543 }
544 break;
scroggo90e971a2016-10-25 06:11:01 -0700545 }
scroggo53f63b62016-10-27 08:29:13 -0700546 case SkCodec::kInvalidConversion:
Mike Reed304a07c2017-07-12 15:10:28 -0400547 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) {
scroggo53f63b62016-10-27 08:29:13 -0700548 return Error::Nonfatal(SkStringPrintf(
Mike Reed304a07c2017-07-12 15:10:28 -0400549 "Cannot decode frame %i to 565 (%s).", i, fPath.c_str()));
scroggo53f63b62016-10-27 08:29:13 -0700550 }
551 // Fall through.
scroggo19b91532016-10-24 09:03:26 -0700552 default:
553 return SkStringPrintf("Couldn't getPixels for frame %i in %s.",
554 i, fPath.c_str());
555 }
scroggo19b91532016-10-24 09:03:26 -0700556 }
557 break;
558 }
msarettbb25b532016-01-13 09:31:39 -0800559 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700560 case kCodec_Mode: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000561 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggoeb602a52015-07-09 08:16:03 -0700562 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400563 // We consider these to be valid, since we should still decode what is
scroggo9c59ebc2015-03-25 13:48:49 -0700564 // available.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400565 case SkCodec::kErrorInInput:
scroggoeb602a52015-07-09 08:16:03 -0700566 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700567 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700568 default:
569 // Everything else is considered a failure.
570 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
571 }
msarettb1be46b2016-05-17 08:52:11 -0700572
Leon Scroggins571b30f2017-07-11 17:35:31 +0000573 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggo9c59ebc2015-03-25 13:48:49 -0700574 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700575 }
scroggo9c59ebc2015-03-25 13:48:49 -0700576 case kScanline_Mode: {
scroggod8d68552016-06-06 11:26:17 -0700577 void* dst = pixels.get();
578 uint32_t height = decodeInfo.height();
scroggo19b91532016-10-24 09:03:26 -0700579 const bool useIncremental = [this]() {
580 auto exts = { "png", "PNG", "gif", "GIF" };
581 for (auto ext : exts) {
582 if (fPath.endsWith(ext)) {
583 return true;
584 }
585 }
586 return false;
587 }();
588 // ico may use the old scanline method or the new one, depending on whether it
589 // internally holds a bmp or a png.
scroggo8e6c7ad2016-09-16 08:20:38 -0700590 const bool ico = fPath.endsWith("ico");
scroggo19b91532016-10-24 09:03:26 -0700591 bool useOldScanlineMethod = !useIncremental && !ico;
592 if (useIncremental || ico) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700593 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000594 rowBytes, &options)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700595 int rowsDecoded;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400596 auto result = codec->incrementalDecode(&rowsDecoded);
597 if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700598 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
599 SkCodec::kNo_ZeroInitialized, height,
600 rowsDecoded);
scroggo6fb23912016-06-02 14:16:43 -0700601 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700602 } else {
scroggo19b91532016-10-24 09:03:26 -0700603 if (useIncremental) {
604 // Error: These should support incremental decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700605 return "Could not start incremental decode";
606 }
607 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
608 // which should work via startScanlineDecode
609 useOldScanlineMethod = true;
610 }
611 }
612
613 if (useOldScanlineMethod) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000614 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700615 return "Could not start scanline decoder";
616 }
617
618 switch (codec->getScanlineOrder()) {
619 case SkCodec::kTopDown_SkScanlineOrder:
620 case SkCodec::kBottomUp_SkScanlineOrder:
621 // We do not need to check the return value. On an incomplete
622 // image, memory will be filled with a default value.
623 codec->getScanlines(dst, height, rowBytes);
624 break;
msarett10522ff2015-09-07 08:54:01 -0700625 }
626 }
627
Leon Scroggins571b30f2017-07-11 17:35:31 +0000628 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700629 break;
630 }
msarett0a242972015-06-11 14:27:27 -0700631 case kStripe_Mode: {
632 const int height = decodeInfo.height();
633 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
634 // does not align with image blocks.
635 const int stripeHeight = 37;
636 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
msarettb1be46b2016-05-17 08:52:11 -0700637 void* dst = pixels.get();
msarett0a242972015-06-11 14:27:27 -0700638
639 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000640 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarettb65e6042016-02-23 05:37:25 -0800641 return "Could not start scanline decoder";
642 }
643
644 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
645 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
646 // to run this test for image types that do not have this scanline ordering.
scroggo12e2f502016-05-16 09:04:18 -0700647 // We only run this on Jpeg, which is always kTopDown.
648 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
msarette6dd0042015-10-09 11:07:34 -0700649
msarett0a242972015-06-11 14:27:27 -0700650 for (int i = 0; i < numStripes; i += 2) {
651 // Skip a stripe
652 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700653 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700654
655 // Read a stripe
656 const int startY = (i + 1) * stripeHeight;
657 const int linesToRead = SkTMin(stripeHeight, height - startY);
658 if (linesToRead > 0) {
msarettb1be46b2016-05-17 08:52:11 -0700659 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
660 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700661 }
662 }
663
664 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000665 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
scroggo1c005e42015-08-04 09:24:45 -0700666 if (SkCodec::kSuccess != startResult) {
667 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700668 }
669 for (int i = 0; i < numStripes; i += 2) {
670 // Read a stripe
671 const int startY = i * stripeHeight;
672 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarettb1be46b2016-05-17 08:52:11 -0700673 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
674 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700675
676 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700677 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
678 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700679 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700680 }
681 }
msarettb1be46b2016-05-17 08:52:11 -0700682
Leon Scroggins571b30f2017-07-11 17:35:31 +0000683 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700684 break;
msarett0a242972015-06-11 14:27:27 -0700685 }
msarett91c22b22016-02-22 12:27:46 -0800686 case kCroppedScanline_Mode: {
687 const int width = decodeInfo.width();
688 const int height = decodeInfo.height();
689 // This value is chosen because, as we move across the image, it will sometimes
690 // align with the jpeg block sizes and it will sometimes not. This allows us
691 // to test interestingly different code paths in the implementation.
692 const int tileSize = 36;
msarett91c22b22016-02-22 12:27:46 -0800693 SkIRect subset;
694 for (int x = 0; x < width; x += tileSize) {
695 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
Matt Saretta40d9c82017-05-19 15:21:05 -0400696 options.fSubset = &subset;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000697 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarett91c22b22016-02-22 12:27:46 -0800698 return "Could not start scanline decoder.";
699 }
700
msarettb1be46b2016-05-17 08:52:11 -0700701 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
msarett91c22b22016-02-22 12:27:46 -0800702 }
703
Leon Scroggins571b30f2017-07-11 17:35:31 +0000704 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800705 break;
706 }
scroggob636b452015-07-22 07:16:20 -0700707 case kSubset_Mode: {
708 // Arbitrarily choose a divisor.
709 int divisor = 2;
710 // Total width/height of the image.
711 const int W = codec->getInfo().width();
712 const int H = codec->getInfo().height();
713 if (divisor > W || divisor > H) {
714 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
715 "for %s with dimensions (%d x %d)", divisor,
716 fPath.c_str(), W, H));
717 }
718 // subset dimensions
719 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
720 const int w = SkAlign2(W / divisor);
721 const int h = SkAlign2(H / divisor);
722 SkIRect subset;
Matt Saretta40d9c82017-05-19 15:21:05 -0400723 options.fSubset = &subset;
scroggob636b452015-07-22 07:16:20 -0700724 SkBitmap subsetBm;
725 // We will reuse pixel memory from bitmap.
msarettb1be46b2016-05-17 08:52:11 -0700726 void* dst = pixels.get();
scroggob636b452015-07-22 07:16:20 -0700727 // Keep track of left and top (for drawing subsetBm into canvas). We could use
728 // fScale * x and fScale * y, but we want integers such that the next subset will start
729 // where the last one ended. So we'll add decodeInfo.width() and height().
730 int left = 0;
731 for (int x = 0; x < W; x += w) {
732 int top = 0;
733 for (int y = 0; y < H; y+= h) {
734 // Do not make the subset go off the edge of the image.
735 const int preScaleW = SkTMin(w, W - x);
736 const int preScaleH = SkTMin(h, H - y);
737 subset.setXYWH(x, y, preScaleW, preScaleH);
738 // And scale
739 // FIXME: Should we have a version of getScaledDimensions that takes a subset
740 // into account?
msarettc7eb4902016-04-25 07:04:58 -0700741 const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale));
742 const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale));
743 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
744 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
msarettb1be46b2016-05-17 08:52:11 -0700745 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
746 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000747 &options);
scroggob636b452015-07-22 07:16:20 -0700748 switch (result) {
749 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400750 case SkCodec::kErrorInInput:
scroggob636b452015-07-22 07:16:20 -0700751 case SkCodec::kIncompleteInput:
752 break;
scroggob636b452015-07-22 07:16:20 -0700753 default:
754 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
755 "from %s with dimensions (%d x %d)\t error %d",
756 x, y, decodeInfo.width(), decodeInfo.height(),
757 fPath.c_str(), W, H, result);
758 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000759 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType,
760 SkIntToScalar(left), SkIntToScalar(top));
msarettb1be46b2016-05-17 08:52:11 -0700761
scroggob636b452015-07-22 07:16:20 -0700762 // translate by the scaled height.
763 top += decodeInfo.height();
764 }
765 // translate by the scaled width.
766 left += decodeInfo.width();
767 }
768 return "";
769 }
msarettb714fb02016-01-22 14:46:42 -0800770 default:
771 SkASSERT(false);
772 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700773 }
scroggo9c59ebc2015-03-25 13:48:49 -0700774 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700775}
776
777SkISize CodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700778 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400779 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700780 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400781 return {0, 0};
scroggo7fac5af2015-09-30 11:33:12 -0700782 }
scroggo19b91532016-10-24 09:03:26 -0700783
784 auto imageSize = codec->getScaledDimensions(fScale);
785 if (fMode == kAnimated_Mode) {
scroggo90e971a2016-10-25 06:11:01 -0700786 // We'll draw one of each frame, so make it big enough to hold them all
787 // in a grid. The grid will be roughly square, with "factor" frames per
788 // row and up to "factor" rows.
scroggo19b91532016-10-24 09:03:26 -0700789 const size_t count = codec->getFrameInfo().size();
scroggo90e971a2016-10-25 06:11:01 -0700790 const float root = sqrt((float) count);
791 const int factor = sk_float_ceil2int(root);
792 imageSize.fWidth = imageSize.fWidth * factor;
793 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
scroggo19b91532016-10-24 09:03:26 -0700794 }
795 return imageSize;
scroggo9b77ddd2015-03-19 06:03:39 -0700796}
797
798Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700799 if (1.0f == fScale) {
scroggo6e8c68e2016-10-24 13:48:49 -0700800 Name name = SkOSPath::Basename(fPath.c_str());
801 if (fMode == kAnimated_Mode) {
802 name.append("_animated");
803 }
804 return name;
msarett0a242972015-06-11 14:27:27 -0700805 }
scroggo6e8c68e2016-10-24 13:48:49 -0700806 SkASSERT(fMode != kAnimated_Mode);
msaretta5783ae2015-09-08 15:35:32 -0700807 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700808}
809
810/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
811
scroggof8dc9df2016-05-16 09:04:13 -0700812AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800813 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700814 : fPath(path)
msarett3d9d7a72015-10-21 10:27:10 -0700815 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800816 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700817 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800818 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700819{}
820
821bool AndroidCodecSrc::veto(SinkFlags flags) const {
822 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700823 return flags.type != SinkFlags::kRaster
824 || flags.approach != SinkFlags::kDirect;
825}
826
827Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
Matt Saretta40d9c82017-05-19 15:21:05 -0400828 if (canvas->imageInfo().colorSpace() &&
829 kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) {
830 // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only
831 // run these tests on legacy canvases.
832 // We allow an exception for F16, since Android uses F16.
833 return Error::Nonfatal("Skip testing to color correct canvas.");
834 }
835
bungeman38d909e2016-08-02 14:40:46 -0700836 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700837 if (!encoded) {
838 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
839 }
Mike Reedede7bac2017-07-23 15:30:02 -0400840 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
841 if (nullptr == codec) {
msarett3d9d7a72015-10-21 10:27:10 -0700842 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
843 }
844
scroggoba584892016-05-20 13:56:13 -0700845 SkImageInfo decodeInfo = codec->getInfo();
846 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
847 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800848 return Error::Nonfatal("Skipping uninteresting test.");
msarett3d9d7a72015-10-21 10:27:10 -0700849 }
850
851 // Scale the image if it is desired.
852 SkISize size = codec->getSampledDimensions(fSampleSize);
853
854 // Visually inspecting very small output images is not necessary. We will
855 // cover these cases in unit testing.
856 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
857 return Error::Nonfatal("Scaling very small images is uninteresting.");
858 }
859 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
860
Mike Reed7fcfb622018-02-09 13:26:46 -0500861 int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700862 size_t rowBytes = size.width() * bpp;
863 SkAutoMalloc pixels(size.height() * rowBytes);
msarett3d9d7a72015-10-21 10:27:10 -0700864
865 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700866 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400867 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700868 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
869 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
870 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
871 }
msarett3d9d7a72015-10-21 10:27:10 -0700872
873 // Create options for the codec.
874 SkAndroidCodec::AndroidOptions options;
msarett3d9d7a72015-10-21 10:27:10 -0700875 options.fSampleSize = fSampleSize;
876
msarettb1be46b2016-05-17 08:52:11 -0700877 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggof8dc9df2016-05-16 09:04:13 -0700878 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400879 case SkCodec::kErrorInInput:
scroggof8dc9df2016-05-16 09:04:13 -0700880 case SkCodec::kIncompleteInput:
881 break;
msarett3d9d7a72015-10-21 10:27:10 -0700882 default:
scroggof8dc9df2016-05-16 09:04:13 -0700883 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700884 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000885 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggof8dc9df2016-05-16 09:04:13 -0700886 return "";
msarett3d9d7a72015-10-21 10:27:10 -0700887}
888
889SkISize AndroidCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700890 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400891 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700892 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400893 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700894 }
895 return codec->getSampledDimensions(fSampleSize);
896}
897
898Name AndroidCodecSrc::name() const {
899 // We will replicate the names used by CodecSrc so that images can
900 // be compared in Gold.
901 if (1 == fSampleSize) {
902 return SkOSPath::Basename(fPath.c_str());
903 }
msarett4b0778e2015-11-13 09:59:11 -0800904 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700905}
906
907/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
908
msarett18976312016-03-09 14:20:58 -0800909ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
910 : fPath(path)
911 , fMode(mode)
912 , fDstAlphaType(alphaType)
913 , fIsGpu(isGpu)
914 , fRunSerially(serial_from_path_name(path))
915{}
916
917bool ImageGenSrc::veto(SinkFlags flags) const {
918 if (fIsGpu) {
Brian Salomonbd7c5512017-03-07 09:08:36 -0500919 // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs.
920 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect ||
921 flags.multisampled == SinkFlags::kMultisampled;
msarett18976312016-03-09 14:20:58 -0800922 }
923
924 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
925}
926
927Error ImageGenSrc::draw(SkCanvas* canvas) const {
928 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
929 return Error::Nonfatal("Uninteresting to test image generator to 565.");
930 }
931
bungeman38d909e2016-08-02 14:40:46 -0700932 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett18976312016-03-09 14:20:58 -0800933 if (!encoded) {
934 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
935 }
936
msarettfc0b6d12016-03-17 13:50:17 -0700937#if defined(SK_BUILD_FOR_WIN)
938 // Initialize COM in order to test with WIC.
939 SkAutoCoInitialize com;
940 if (!com.succeeded()) {
941 return "Could not initialize COM.";
942 }
943#endif
944
Ben Wagner145dbcd2016-11-03 14:40:50 -0400945 std::unique_ptr<SkImageGenerator> gen(nullptr);
msarett18976312016-03-09 14:20:58 -0800946 switch (fMode) {
947 case kCodec_Mode:
Mike Reed185130c2017-02-15 15:14:16 -0500948 gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded);
msarett18976312016-03-09 14:20:58 -0800949 if (!gen) {
950 return "Could not create codec image generator.";
951 }
952 break;
msarettfc0b6d12016-03-17 13:50:17 -0700953 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800954#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -0400955 gen = SkImageGeneratorCG::MakeFromEncodedCG(encoded);
msarettfc0b6d12016-03-17 13:50:17 -0700956#elif defined(SK_BUILD_FOR_WIN)
bungeman38d909e2016-08-02 14:40:46 -0700957 gen.reset(SkImageGeneratorWIC::NewFromEncodedWIC(encoded.get()));
msarettfc0b6d12016-03-17 13:50:17 -0700958#endif
959
msarett18976312016-03-09 14:20:58 -0800960 if (!gen) {
msarettfc0b6d12016-03-17 13:50:17 -0700961 return "Could not create platform image generator.";
msarett18976312016-03-09 14:20:58 -0800962 }
963 break;
msarettfc0b6d12016-03-17 13:50:17 -0700964 }
msarett18976312016-03-09 14:20:58 -0800965 default:
966 SkASSERT(false);
967 return "Invalid image generator mode";
968 }
969
970 // Test deferred decoding path on GPU
971 if (fIsGpu) {
Mike Reed185130c2017-02-15 15:14:16 -0500972 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr));
msarett18976312016-03-09 14:20:58 -0800973 if (!image) {
974 return "Could not create image from codec image generator.";
975 }
976 canvas->drawImage(image, 0, 0);
977 return "";
978 }
mtklein343a63d2016-03-22 11:46:53 -0700979
msarett18976312016-03-09 14:20:58 -0800980 // Test various color and alpha types on CPU
981 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700982
Matt Saretta40d9c82017-05-19 15:21:05 -0400983 SkImageGenerator::Options options;
984 options.fBehavior = canvas->imageInfo().colorSpace() ?
985 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
986
Mike Reed7fcfb622018-02-09 13:26:46 -0500987 int bpp = decodeInfo.bytesPerPixel();
msarettb1be46b2016-05-17 08:52:11 -0700988 size_t rowBytes = decodeInfo.width() * bpp;
989 SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
Matt Saretta40d9c82017-05-19 15:21:05 -0400990 if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
Matt Sarett05cb4c32017-03-02 12:07:46 -0500991 SkString err =
992 SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str());
993
994#if defined(SK_BUILD_FOR_WIN)
995 if (kPlatform_Mode == fMode) {
996 // Do not issue a fatal error for WIC flakiness.
997 return Error::Nonfatal(err);
998 }
999#endif
1000
1001 return err;
msarett18976312016-03-09 14:20:58 -08001002 }
1003
Matt Sarettf485cf92017-05-26 08:58:03 -04001004 set_bitmap_color_space(&decodeInfo);
Leon Scroggins571b30f2017-07-11 17:35:31 +00001005 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes,
msarettb1be46b2016-05-17 08:52:11 -07001006 CodecSrc::kGetFromCanvas_DstColorType);
msarett18976312016-03-09 14:20:58 -08001007 return "";
1008}
1009
1010SkISize ImageGenSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001011 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001012 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett18976312016-03-09 14:20:58 -08001013 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001014 return {0, 0};
msarett18976312016-03-09 14:20:58 -08001015 }
1016 return codec->getInfo().dimensions();
1017}
1018
1019Name ImageGenSrc::name() const {
1020 return SkOSPath::Basename(fPath.c_str());
1021}
1022
1023/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1024
msarett9ce3a542016-07-15 13:54:38 -07001025ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, SkColorType colorType)
msarett69deca82016-04-29 09:38:40 -07001026 : fPath(path)
1027 , fMode(mode)
msarett9ce3a542016-07-15 13:54:38 -07001028 , fColorType(colorType)
msarett69deca82016-04-29 09:38:40 -07001029{}
1030
1031bool ColorCodecSrc::veto(SinkFlags flags) const {
1032 // Test to direct raster backends (8888 and 565).
1033 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
1034}
1035
Matt Sarett6a0feba2017-06-09 10:45:44 -04001036void clamp_if_necessary(const SkBitmap& bitmap, SkColorType dstCT) {
1037 if (kRGBA_F16_SkColorType != bitmap.colorType() || kRGBA_F16_SkColorType == dstCT) {
1038 // No need to clamp if the dst is F16. We will clamp when we encode to PNG.
1039 return;
1040 }
1041
Mike Klein45c16fa2017-07-18 18:15:13 -04001042 SkJumper_MemoryCtx ptr = { bitmap.getAddr(0,0), bitmap.rowBytesAsPixels() };
1043
Matt Sarett6a0feba2017-06-09 10:45:44 -04001044 SkRasterPipeline_<256> p;
1045 p.append(SkRasterPipeline::load_f16, &ptr);
1046 p.append(SkRasterPipeline::clamp_0);
1047 if (kPremul_SkAlphaType == bitmap.alphaType()) {
1048 p.append(SkRasterPipeline::clamp_a);
1049 } else {
1050 p.append(SkRasterPipeline::clamp_1);
1051 }
1052 p.append(SkRasterPipeline::store_f16, &ptr);
1053
Mike Klein45c16fa2017-07-18 18:15:13 -04001054 p.run(0,0, bitmap.width(), bitmap.height());
Matt Sarett6a0feba2017-06-09 10:45:44 -04001055}
1056
msarett69deca82016-04-29 09:38:40 -07001057Error ColorCodecSrc::draw(SkCanvas* canvas) const {
1058 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
1059 return Error::Nonfatal("No need to test color correction to 565 backend.");
1060 }
1061
msarettd1ec89b2016-08-03 12:59:27 -07001062 bool runInLegacyMode = kBaseline_Mode == fMode;
msarettd1ec89b2016-08-03 12:59:27 -07001063 if (runInLegacyMode && canvas->imageInfo().colorSpace()) {
1064 return Error::Nonfatal("Skipping tests that are only interesting in legacy mode.");
1065 } else if (!runInLegacyMode && !canvas->imageInfo().colorSpace()) {
1066 return Error::Nonfatal("Skipping tests that are only interesting in srgb mode.");
msarett9ce3a542016-07-15 13:54:38 -07001067 }
1068
bungeman38d909e2016-08-02 14:40:46 -07001069 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett69deca82016-04-29 09:38:40 -07001070 if (!encoded) {
1071 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1072 }
1073
Mike Reedede7bac2017-07-23 15:30:02 -04001074 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
1075 if (nullptr == codec) {
msarett69deca82016-04-29 09:38:40 -07001076 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
1077 }
1078
msarett9876ac52016-06-01 14:47:18 -07001079 // Load the dst ICC profile. This particular dst is fairly similar to Adobe RGB.
Mike Reed0933bc92017-12-09 01:27:41 +00001080 sk_sp<SkData> dstData = GetResourceAsData("icc_profiles/HP_ZR30w.icc");
msarett9876ac52016-06-01 14:47:18 -07001081 if (!dstData) {
1082 return "Cannot read monitor profile. Is the resource path set correctly?";
1083 }
1084
msarett50ce1f22016-07-29 06:23:33 -07001085 sk_sp<SkColorSpace> dstSpace = nullptr;
1086 if (kDst_sRGB_Mode == fMode) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -05001087 dstSpace = SkColorSpace::MakeSRGB();
msarett50ce1f22016-07-29 06:23:33 -07001088 } else if (kDst_HPZR30w_Mode == fMode) {
Brian Osman526972e2016-10-24 09:24:02 -04001089 dstSpace = SkColorSpace::MakeICC(dstData->data(), dstData->size());
msarett50ce1f22016-07-29 06:23:33 -07001090 }
1091
1092 SkImageInfo decodeInfo = codec->getInfo().makeColorType(fColorType).makeColorSpace(dstSpace);
msarettd1ec89b2016-08-03 12:59:27 -07001093 if (kUnpremul_SkAlphaType == decodeInfo.alphaType()) {
1094 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
1095 }
msarett2ecc35f2016-09-08 11:55:16 -07001096 if (kRGBA_F16_SkColorType == fColorType) {
Brian Osman36703d92017-12-12 14:09:31 -05001097 decodeInfo = decodeInfo.makeColorSpace(decodeInfo.colorSpace()->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001098 }
msarettd1ec89b2016-08-03 12:59:27 -07001099
msarett50ce1f22016-07-29 06:23:33 -07001100 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -04001101 set_bitmap_color_space(&bitmapInfo);
msarett50ce1f22016-07-29 06:23:33 -07001102 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
1103 kBGRA_8888_SkColorType == decodeInfo.colorType())
1104 {
1105 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
1106 }
1107
1108 SkBitmap bitmap;
1109 if (!bitmap.tryAllocPixels(bitmapInfo)) {
1110 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
1111 bitmapInfo.width(), bitmapInfo.height());
1112 }
1113
1114 size_t rowBytes = bitmap.rowBytes();
1115 SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), rowBytes);
Leon Scroggins III674a1842017-07-06 12:26:09 -04001116 switch (r) {
1117 case SkCodec::kSuccess:
1118 case SkCodec::kErrorInInput:
1119 case SkCodec::kIncompleteInput:
1120 break;
1121 default:
1122 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
msarett50ce1f22016-07-29 06:23:33 -07001123 }
1124
msarett69deca82016-04-29 09:38:40 -07001125 switch (fMode) {
1126 case kBaseline_Mode:
msarett39979d82016-07-28 17:11:18 -07001127 case kDst_sRGB_Mode:
msarett50ce1f22016-07-29 06:23:33 -07001128 case kDst_HPZR30w_Mode:
Matt Sarett6a0feba2017-06-09 10:45:44 -04001129 // We do not support drawing unclamped F16.
1130 clamp_if_necessary(bitmap, canvas->imageInfo().colorType());
msarett39979d82016-07-28 17:11:18 -07001131 canvas->drawBitmap(bitmap, 0, 0);
1132 break;
msarett69deca82016-04-29 09:38:40 -07001133 default:
1134 SkASSERT(false);
1135 return "Invalid fMode";
1136 }
1137 return "";
1138}
1139
1140SkISize ColorCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001141 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001142 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001143 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001144 return {0, 0};
msarett69deca82016-04-29 09:38:40 -07001145 }
Hal Canaryfafe1352017-04-11 12:12:02 -04001146 return {codec->getInfo().width(), codec->getInfo().height()};
msarett69deca82016-04-29 09:38:40 -07001147}
1148
1149Name ColorCodecSrc::name() const {
1150 return SkOSPath::Basename(fPath.c_str());
1151}
1152
1153/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1154
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001155static const SkRect kSKPViewport = {0, 0, 1000, 1000};
mtkleinf4ba3212015-01-28 15:32:24 -08001156
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001157SKPSrc::SKPSrc(Path path) : fPath(path) { }
mtklein748ca3b2015-01-15 10:56:12 -08001158
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001159static sk_sp<SkPicture> read_skp(const char* path) {
1160 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtklein75d98fd2015-01-18 07:05:01 -08001161 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001162 return nullptr;
mtklein748ca3b2015-01-15 10:56:12 -08001163 }
bungemanf93d7112016-09-16 06:24:20 -07001164 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
mtklein75d98fd2015-01-18 07:05:01 -08001165 if (!pic) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001166 return nullptr;
mtklein75d98fd2015-01-18 07:05:01 -08001167 }
bungemanf93d7112016-09-16 06:24:20 -07001168 stream = nullptr; // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -07001169
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001170 return pic;
1171}
1172
1173Error SKPSrc::draw(SkCanvas* canvas) const {
1174 sk_sp<SkPicture> pic = read_skp(fPath.c_str());
1175 if (!pic) {
1176 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1177 }
1178
mtkleinf4ba3212015-01-28 15:32:24 -08001179 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -08001180 canvas->drawPicture(pic);
1181 return "";
1182}
1183
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001184static SkRect get_cull_rect_for_skp(const char* path) {
1185 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtkleinffa901a2015-03-16 10:38:07 -07001186 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001187 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001188 }
1189 SkPictInfo info;
Mike Reede7a58322017-12-20 14:09:20 -05001190 if (!SkPicture_StreamIsSKP(stream.get(), &info)) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001191 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001192 }
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001193
1194 return info.fCullRect;
1195}
1196
1197SkISize SKPSrc::size() const {
1198 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
1199 if (!viewport.intersect(kSKPViewport)) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001200 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001201 }
1202 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001203}
1204
1205Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1206
1207/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001208
1209static const int kNumDDLXTiles = 4;
1210static const int kNumDDLYTiles = 4;
1211static const int kDDLTileSize = 1024;
1212static const SkRect kDDLSKPViewport = { 0, 0,
1213 kNumDDLXTiles * kDDLTileSize,
1214 kNumDDLYTiles * kDDLTileSize };
1215
1216DDLSKPSrc::DDLSKPSrc(Path path) : fPath(path) { }
1217
1218Error DDLSKPSrc::draw(SkCanvas* canvas) const {
1219 class TileData {
1220 public:
1221 // Note: we could just pass in surface characterization
1222 TileData(sk_sp<SkSurface> surf, const SkIRect& clip)
1223 : fSurface(std::move(surf))
1224 , fClip(clip) {
1225 SkAssertResult(fSurface->characterize(&fCharacterization));
1226 }
1227
1228 // This method operates in parallel
1229 void preprocess(SkPicture* pic) {
1230 SkDeferredDisplayListRecorder recorder(fCharacterization);
1231
1232 SkCanvas* subCanvas = recorder.getCanvas();
1233
1234 subCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
1235 subCanvas->translate(-fClip.fLeft, -fClip.fTop);
1236
1237 // Note: in this use case we only render a picture to the deferred canvas
1238 // but, more generally, clients will use arbitrary draw calls.
1239 subCanvas->drawPicture(pic);
1240
1241 fDisplayList = recorder.detach();
1242 }
1243
1244 // This method operates serially
1245 void draw() {
1246 fSurface->draw(fDisplayList.get());
1247 }
1248
1249 // This method also operates serially
1250 void compose(SkCanvas* dst) {
1251 sk_sp<SkImage> img = fSurface->makeImageSnapshot();
1252 dst->save();
1253 dst->clipRect(SkRect::Make(fClip));
1254 dst->drawImage(std::move(img), fClip.fLeft, fClip.fTop);
1255 dst->restore();
1256 }
1257
1258 private:
1259 sk_sp<SkSurface> fSurface;
1260 SkIRect fClip; // in the device space of the destination canvas
1261 std::unique_ptr<SkDeferredDisplayList> fDisplayList;
1262 SkSurfaceCharacterization fCharacterization;
1263 };
1264
1265 SkTArray<TileData> tileData;
1266 tileData.reserve(16);
1267
1268 sk_sp<SkPicture> pic = read_skp(fPath.c_str());
1269 if (!pic) {
1270 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1271 }
1272
1273 const SkRect cullRect = pic->cullRect();
1274
1275 // All the destination tiles are the same size
1276 const SkImageInfo tileII = SkImageInfo::MakeN32Premul(kDDLTileSize, kDDLTileSize);
1277
1278 // First, create the destination tiles
1279 for (int y = 0; y < kNumDDLYTiles; ++y) {
1280 for (int x = 0; x < kNumDDLXTiles; ++x) {
1281 SkRect clip = SkRect::MakeXYWH(x * kDDLTileSize, y * kDDLTileSize,
1282 kDDLTileSize, kDDLTileSize);
1283
1284 if (!clip.intersect(cullRect)) {
1285 continue;
1286 }
1287
1288 tileData.push_back(TileData(canvas->makeSurface(tileII), clip.roundOut()));
1289 }
1290 }
1291
1292 // Second, run the cpu pre-processing in threads
1293 SkTaskGroup().batch(tileData.count(), [&](int i) {
1294 tileData[i].preprocess(pic.get());
1295 });
1296
1297 // Third, synchronously render the display lists into the dest tiles
1298 // TODO: it would be cool to not wait until all the tiles are drawn to begin
1299 // drawing to the GPU
1300 for (int i = 0; i < tileData.count(); ++i) {
1301 tileData[i].draw();
1302 }
1303
1304 // Finally, compose the drawn tiles into the result
1305 // Note: the separation between the tiles and the final composition better
1306 // matches Chrome but costs us a copy
1307 for (int i = 0; i < tileData.count(); ++i) {
1308 tileData[i].compose(canvas);
1309 }
1310
1311 return "";
1312}
1313
1314SkISize DDLSKPSrc::size() const {
1315 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
1316 if (!viewport.intersect(kDDLSKPViewport)) {
1317 return {0, 0};
1318 }
1319 return viewport.roundOut().size();
1320}
1321
1322Name DDLSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1323
Florin Malitafc043dc2017-12-31 11:08:42 -05001324/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1325
Florin Malita124d5af2017-12-31 17:02:26 -05001326#if !defined(SK_BUILD_FOR_GOOGLE3)
Florin Malita54f65c42018-01-16 17:04:30 -05001327SkottieSrc::SkottieSrc(Path path)
Florin Malitafc043dc2017-12-31 11:08:42 -05001328 : fName(SkOSPath::Basename(path.c_str())) {
1329
Florin Malita54f65c42018-01-16 17:04:30 -05001330 fAnimation = skottie::Animation::MakeFromFile(path.c_str());
Florin Malitafc043dc2017-12-31 11:08:42 -05001331 if (!fAnimation) {
1332 return;
1333 }
1334
1335 // Fit kTileCount x kTileCount frames to a 1000x1000 film strip.
1336 static constexpr SkScalar kTargetSize = 1000;
1337 const auto scale = kTargetSize / (kTileCount * std::max(fAnimation->size().width(),
1338 fAnimation->size().height()));
1339 fTileSize = SkSize::Make(scale * fAnimation->size().width(),
1340 scale * fAnimation->size().height()).toCeil();
1341
1342}
1343
Florin Malita54f65c42018-01-16 17:04:30 -05001344Error SkottieSrc::draw(SkCanvas* canvas) const {
Florin Malitafc043dc2017-12-31 11:08:42 -05001345 if (!fAnimation) {
1346 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
1347 }
1348
1349 canvas->drawColor(SK_ColorWHITE);
1350
Florin Malitadf2713c2018-01-09 15:51:21 -05001351 SkPaint paint, clockPaint;
Florin Malitafc043dc2017-12-31 11:08:42 -05001352 paint.setColor(0xffa0a0a0);
1353 paint.setStyle(SkPaint::kStroke_Style);
Florin Malitad3c1b842018-01-27 12:43:24 -05001354 paint.setStrokeWidth(1);
1355 paint.setAntiAlias(true);
Florin Malitafc043dc2017-12-31 11:08:42 -05001356
Florin Malitadf2713c2018-01-09 15:51:21 -05001357 clockPaint.setTextSize(12);
Florin Malitad3c1b842018-01-27 12:43:24 -05001358 clockPaint.setAntiAlias(true);
Florin Malitadf2713c2018-01-09 15:51:21 -05001359
Florin Malitafc043dc2017-12-31 11:08:42 -05001360 const auto ip = fAnimation->inPoint() * 1000 / fAnimation->frameRate(),
1361 op = fAnimation->outPoint() * 1000 / fAnimation->frameRate(),
1362 fr = (op - ip) / (kTileCount * kTileCount - 1);
1363
Florin Malitad3c1b842018-01-27 12:43:24 -05001364 // Shuffled order to exercise non-linear frame progression.
1365 static constexpr int frames[] = { 4, 0, 3, 1, 2 };
1366 static_assert(SK_ARRAY_COUNT(frames) == kTileCount, "");
1367
Florin Malitafc043dc2017-12-31 11:08:42 -05001368 const auto canvas_size = this->size();
1369 for (int i = 0; i < kTileCount; ++i) {
Florin Malitad3c1b842018-01-27 12:43:24 -05001370 const SkScalar y = frames[i] * (fTileSize.height() + 1);
Florin Malitafc043dc2017-12-31 11:08:42 -05001371
1372 for (int j = 0; j < kTileCount; ++j) {
Florin Malitad3c1b842018-01-27 12:43:24 -05001373 const SkScalar x = frames[j] * (fTileSize.width() + 1);
Florin Malitafc043dc2017-12-31 11:08:42 -05001374 SkRect dest = SkRect::MakeXYWH(x, y, fTileSize.width(), fTileSize.height());
1375
Florin Malitad3c1b842018-01-27 12:43:24 -05001376 const auto t = fr * (frames[i] * kTileCount + frames[j]);
Florin Malitadf2713c2018-01-09 15:51:21 -05001377 {
1378 SkAutoCanvasRestore acr(canvas, true);
Florin Malitad3c1b842018-01-27 12:43:24 -05001379 canvas->clipRect(dest, true);
Florin Malitadf2713c2018-01-09 15:51:21 -05001380 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(fAnimation->size()),
1381 dest,
1382 SkMatrix::kFill_ScaleToFit));
1383
1384 fAnimation->animationTick(t);
1385 fAnimation->render(canvas);
1386 }
1387
Florin Malitad3c1b842018-01-27 12:43:24 -05001388 canvas->drawLine(x + fTileSize.width() + .5f, 0,
1389 x + fTileSize.width() + .5f, canvas_size.height(), paint);
Florin Malitadf2713c2018-01-09 15:51:21 -05001390 const auto label = SkStringPrintf("%.3f", t);
1391 canvas->drawText(label.c_str(), label.size(), dest.x(),
1392 dest.bottom(), clockPaint);
Florin Malitafc043dc2017-12-31 11:08:42 -05001393 }
Florin Malitad3c1b842018-01-27 12:43:24 -05001394
1395 canvas->drawLine(0 , y + fTileSize.height() + .5f,
1396 canvas_size.width(), y + fTileSize.height() + .5f, paint);
Florin Malitafc043dc2017-12-31 11:08:42 -05001397 }
1398
1399 return "";
1400}
1401
Florin Malita54f65c42018-01-16 17:04:30 -05001402SkISize SkottieSrc::size() const {
Florin Malitafc043dc2017-12-31 11:08:42 -05001403 // Padding for grid.
1404 return SkISize::Make(kTileCount * (fTileSize.width() + 1),
1405 kTileCount * (fTileSize.height() + 1));
1406}
1407
Florin Malita54f65c42018-01-16 17:04:30 -05001408Name SkottieSrc::name() const { return fName; }
Florin Malitafc043dc2017-12-31 11:08:42 -05001409
Florin Malita54f65c42018-01-16 17:04:30 -05001410bool SkottieSrc::veto(SinkFlags flags) const {
Florin Malitafc043dc2017-12-31 11:08:42 -05001411 // No need to test to non-(raster||gpu||vector) or indirect backends.
1412 bool type_ok = flags.type == SinkFlags::kRaster
1413 || flags.type == SinkFlags::kGPU
1414 || flags.type == SinkFlags::kVector;
1415
1416 return !type_ok || flags.approach != SinkFlags::kDirect;
1417}
Florin Malita124d5af2017-12-31 17:02:26 -05001418#endif
Florin Malitafc043dc2017-12-31 11:08:42 -05001419
1420/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fmalitaa2b9fdf2016-08-03 19:53:36 -07001421#if defined(SK_XML)
fmalitabdf3e5c2016-09-17 07:26:26 -07001422// Used when the image doesn't have an intrinsic size.
Hal Canaryfafe1352017-04-11 12:12:02 -04001423static const SkSize kDefaultSVGSize = {1000, 1000};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001424
fmalitabdf3e5c2016-09-17 07:26:26 -07001425// Used to force-scale tiny fixed-size images.
Hal Canaryfafe1352017-04-11 12:12:02 -04001426static const SkSize kMinimumSVGSize = {128, 128};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001427
fmalitaacd2f5c2016-11-08 07:13:45 -08001428SVGSrc::SVGSrc(Path path)
1429 : fName(SkOSPath::Basename(path.c_str()))
1430 , fScale(1) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001431
fmalitaacd2f5c2016-11-08 07:13:45 -08001432 SkFILEStream stream(path.c_str());
1433 if (!stream.isValid()) {
1434 return;
1435 }
1436 fDom = SkSVGDOM::MakeFromStream(stream);
1437 if (!fDom) {
1438 return;
1439 }
1440
1441 const SkSize& sz = fDom->containerSize();
1442 if (sz.isEmpty()) {
1443 // no intrinsic size
1444 fDom->setContainerSize(kDefaultSVGSize);
1445 } else {
1446 fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(),
1447 kMinimumSVGSize.height() / sz.height()));
1448 }
1449}
1450
1451Error SVGSrc::draw(SkCanvas* canvas) const {
fmalitabdf3e5c2016-09-17 07:26:26 -07001452 if (!fDom) {
fmalitaacd2f5c2016-11-08 07:13:45 -08001453 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
fmalitaa2b9fdf2016-08-03 19:53:36 -07001454 }
1455
fmalitaacd2f5c2016-11-08 07:13:45 -08001456 SkAutoCanvasRestore acr(canvas, true);
1457 canvas->scale(fScale, fScale);
1458 fDom->render(canvas);
1459
fmalitaa2b9fdf2016-08-03 19:53:36 -07001460 return "";
1461}
1462
1463SkISize SVGSrc::size() const {
fmalitaacd2f5c2016-11-08 07:13:45 -08001464 if (!fDom) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001465 return {0, 0};
fmalitabdf3e5c2016-09-17 07:26:26 -07001466 }
1467
Hal Canaryfafe1352017-04-11 12:12:02 -04001468 return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale}
1469 .toRound();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001470}
1471
fmalitaacd2f5c2016-11-08 07:13:45 -08001472Name SVGSrc::name() const { return fName; }
fmalitaa2b9fdf2016-08-03 19:53:36 -07001473
fmalita179d8852016-08-16 14:23:29 -07001474bool SVGSrc::veto(SinkFlags flags) const {
Florin Malita93323eb2017-06-16 15:31:17 -04001475 // No need to test to non-(raster||gpu||vector) or indirect backends.
fmalita179d8852016-08-16 14:23:29 -07001476 bool type_ok = flags.type == SinkFlags::kRaster
Florin Malita93323eb2017-06-16 15:31:17 -04001477 || flags.type == SinkFlags::kGPU
1478 || flags.type == SinkFlags::kVector;
fmalita179d8852016-08-16 14:23:29 -07001479
1480 return !type_ok || flags.approach != SinkFlags::kDirect;
1481}
1482
fmalitaa2b9fdf2016-08-03 19:53:36 -07001483#endif // defined(SK_XML)
1484/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein748ca3b2015-01-15 10:56:12 -08001485
halcanary45420a92016-06-02 12:41:14 -07001486MSKPSrc::MSKPSrc(Path path) : fPath(path) {
bungemanf93d7112016-09-16 06:24:20 -07001487 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001488 int count = SkMultiPictureDocumentReadPageCount(stream.get());
1489 if (count > 0) {
1490 fPages.reset(count);
1491 (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count());
1492 }
halcanary45420a92016-06-02 12:41:14 -07001493}
1494
Hal Canary45cde312017-04-03 16:06:42 -04001495int MSKPSrc::pageCount() const { return fPages.count(); }
halcanary45420a92016-06-02 12:41:14 -07001496
1497SkISize MSKPSrc::size() const { return this->size(0); }
Hal Canary45cde312017-04-03 16:06:42 -04001498SkISize MSKPSrc::size(int i) const {
Hal Canaryfafe1352017-04-11 12:12:02 -04001499 return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0};
Hal Canary45cde312017-04-03 16:06:42 -04001500}
halcanary45420a92016-06-02 12:41:14 -07001501
1502Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); }
1503Error MSKPSrc::draw(int i, SkCanvas* canvas) const {
Hal Canary45cde312017-04-03 16:06:42 -04001504 if (this->pageCount() == 0) {
halcanary45420a92016-06-02 12:41:14 -07001505 return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
1506 }
Hal Canary45cde312017-04-03 16:06:42 -04001507 if (i >= fPages.count() || i < 0) {
halcanary45420a92016-06-02 12:41:14 -07001508 return SkStringPrintf("MultiPictureDocument page number out of range: %d", i);
1509 }
Hal Canary45cde312017-04-03 16:06:42 -04001510 SkPicture* page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001511 if (!page) {
Hal Canary45cde312017-04-03 16:06:42 -04001512 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1513 if (!stream) {
1514 return SkStringPrintf("Unable to open file: %s", fPath.c_str());
1515 }
1516 if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) {
1517 return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i,
1518 fPath.c_str());
1519 }
1520 page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001521 }
1522 canvas->drawPicture(page);
1523 return "";
1524}
1525
1526Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1527
1528/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1529
mtkleinad66f9b2015-02-13 15:11:10 -08001530Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
Mike Reed5df49342016-11-12 08:06:55 -06001531 return src.draw(SkMakeNullCanvas().get());
mtkleinad66f9b2015-02-13 15:11:10 -08001532}
1533
1534/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1535
Brian Osmane5756ec2017-09-06 17:08:30 -04001536static bool encode_png_base64(const SkBitmap& bitmap, SkString* dst) {
1537 SkPixmap pm;
1538 if (!bitmap.peekPixels(&pm)) {
1539 dst->set("peekPixels failed");
1540 return false;
1541 }
1542
1543 // We're going to embed this PNG in a data URI, so make it as small as possible
1544 SkPngEncoder::Options options;
1545 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
1546 options.fZLibLevel = 9;
1547 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1548 : SkTransferFunctionBehavior::kIgnore;
1549
1550 SkDynamicMemoryWStream wStream;
1551 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
1552 dst->set("SkPngEncoder::Encode failed");
1553 return false;
1554 }
1555
1556 sk_sp<SkData> pngData = wStream.detachAsData();
1557 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
1558
1559 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
1560 // Infra says these can be pretty big, as long as we're only outputting them on failure.
1561 static const size_t kMaxBase64Length = 1024 * 1024;
1562 if (len > kMaxBase64Length) {
1563 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
1564 return false;
1565 }
1566
1567 dst->resize(len);
1568 SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
1569 return true;
1570}
1571
1572static Error compare_bitmaps(const SkBitmap& reference, const SkBitmap& bitmap) {
1573 // The dimensions are a property of the Src only, and so should be identical.
Mike Reedf0ffb892017-10-03 14:47:21 -04001574 SkASSERT(reference.computeByteSize() == bitmap.computeByteSize());
1575 if (reference.computeByteSize() != bitmap.computeByteSize()) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001576 return "Dimensions don't match reference";
1577 }
1578 // All SkBitmaps in DM are tight, so this comparison is easy.
Mike Reedf0ffb892017-10-03 14:47:21 -04001579 if (0 != memcmp(reference.getPixels(), bitmap.getPixels(), reference.computeByteSize())) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001580 SkString encoded;
1581 SkString errString("Pixels don't match reference");
1582 if (encode_png_base64(reference, &encoded)) {
1583 errString.append("\nExpected: data:image/png;base64,");
1584 errString.append(encoded);
1585 } else {
1586 errString.append("\nExpected image failed to encode: ");
1587 errString.append(encoded);
1588 }
1589 if (encode_png_base64(bitmap, &encoded)) {
1590 errString.append("\nActual: data:image/png;base64,");
1591 errString.append(encoded);
1592 } else {
1593 errString.append("\nActual image failed to encode: ");
1594 errString.append(encoded);
1595 }
1596 return errString;
1597 }
1598 return "";
1599}
1600
1601/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1602
mtkleinb9eb4ac2015-02-02 18:26:03 -08001603DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
1604
bsalomon85b4b532016-04-05 11:06:27 -07001605GPUSink::GPUSink(GrContextFactory::ContextType ct,
csmartdaltone812d492017-02-21 12:36:05 -07001606 GrContextFactory::ContextOverrides overrides,
Brian Salomonf865b052018-03-09 09:01:53 -05001607 SkCommandLineConfigGpu::SurfType surfType,
mtklein82d28432015-01-15 12:46:02 -08001608 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -07001609 bool diText,
brianosmand93c1202016-03-10 07:49:08 -08001610 SkColorType colorType,
Brian Salomonce5ee602017-07-17 11:31:31 -04001611 SkAlphaType alphaType,
brianosmanb109b8c2016-06-16 13:03:24 -07001612 sk_sp<SkColorSpace> colorSpace,
Brian Osmanf21aa042017-08-21 16:48:46 -04001613 bool threaded,
1614 const GrContextOptions& grCtxOptions)
Brian Salomonce5ee602017-07-17 11:31:31 -04001615 : fContextType(ct)
1616 , fContextOverrides(overrides)
Brian Salomonf865b052018-03-09 09:01:53 -05001617 , fSurfType(surfType)
Brian Salomonce5ee602017-07-17 11:31:31 -04001618 , fSampleCount(samples)
1619 , fUseDIText(diText)
1620 , fColorType(colorType)
1621 , fAlphaType(alphaType)
1622 , fColorSpace(std::move(colorSpace))
Brian Osmanf21aa042017-08-21 16:48:46 -04001623 , fThreaded(threaded)
1624 , fBaseContextOptions(grCtxOptions) {}
mtklein748ca3b2015-01-15 10:56:12 -08001625
Brian Salomon09d994e2016-12-21 11:14:46 -05001626DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
bsalomon648c6962015-10-23 09:06:59 -07001627
Brian Osmanf9810662017-08-30 10:02:10 -04001628Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const {
1629 return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
1630}
1631
1632Error GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
1633 const GrContextOptions& baseOptions) const {
1634 GrContextOptions grOptions = baseOptions;
kkinnunen64492c42015-12-08 01:24:40 -08001635
kkinnunen5219fd92015-12-10 06:28:13 -08001636 src.modifyGrContextOptions(&grOptions);
1637
1638 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -08001639 const SkISize size = src.size();
Brian Salomonce5ee602017-07-17 11:31:31 -04001640 SkImageInfo info =
1641 SkImageInfo::Make(size.width(), size.height(), fColorType, fAlphaType, fColorSpace);
Brian Salomonf865b052018-03-09 09:01:53 -05001642 sk_sp<SkSurface> surface;
msarett13a036b2016-02-08 09:10:47 -08001643#if SK_SUPPORT_GPU
csmartdaltone812d492017-02-21 12:36:05 -07001644 GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext();
bsalomon8b7451a2016-05-11 06:33:06 -07001645 const int maxDimension = context->caps()->maxTextureSize();
msarett13a036b2016-02-08 09:10:47 -08001646 if (maxDimension < SkTMax(size.width(), size.height())) {
1647 return Error::Nonfatal("Src too large to create a texture.\n");
1648 }
Brian Salomonf865b052018-03-09 09:01:53 -05001649 uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
1650 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1651 GrBackendTexture backendTexture;
1652 GrBackendRenderTarget backendRT;
1653 switch (fSurfType) {
1654 case SkCommandLineConfigGpu::SurfType::kDefault:
1655 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
1656 &props);
1657 break;
1658 case SkCommandLineConfigGpu::SurfType::kBackendTexture:
1659 backendTexture = context->contextPriv().getGpu()->createTestingOnlyBackendTexture(
1660 nullptr, info.width(), info.height(), info.colorType(), true, GrMipMapped::kNo);
1661 surface = SkSurface::MakeFromBackendTexture(context, backendTexture,
1662 kTopLeft_GrSurfaceOrigin, fSampleCount,
1663 info.refColorSpace(), &props);
1664 break;
1665 case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
1666 if (1 == fSampleCount) {
1667 auto srgbEncoded = info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()
1668 ? GrSRGBEncoded::kYes
1669 : GrSRGBEncoded::kNo;
1670 auto colorType = SkColorTypeToGrColorType(info.colorType());
1671 backendRT = context->contextPriv().getGpu()->createTestingOnlyBackendRenderTarget(
1672 info.width(), info.height(), colorType, srgbEncoded);
1673 surface = SkSurface::MakeFromBackendRenderTarget(context, backendRT,
1674 kBottomLeft_GrSurfaceOrigin,
1675 info.refColorSpace(), &props);
1676 }
1677 break;
1678 }
msarett13a036b2016-02-08 09:10:47 -08001679#endif
1680
mtklein748ca3b2015-01-15 10:56:12 -08001681 if (!surface) {
1682 return "Could not create a surface.";
1683 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001684 if (FLAGS_preAbandonGpuContext) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001685 factory.abandonContexts();
1686 }
mtklein748ca3b2015-01-15 10:56:12 -08001687 SkCanvas* canvas = surface->getCanvas();
1688 Error err = src.draw(canvas);
1689 if (!err.isEmpty()) {
1690 return err;
1691 }
1692 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001693 if (FLAGS_gpuStats) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001694#if SK_SUPPORT_GPU
1695 canvas->getGrContext()->contextPriv().dumpCacheStats(log);
1696 canvas->getGrContext()->contextPriv().dumpGpuStats(log);
1697#endif
mtkleinb9eb4ac2015-02-02 18:26:03 -08001698 }
Brian Salomonce5ee602017-07-17 11:31:31 -04001699 if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType) {
1700 // We don't currently support readbacks into these formats on the GPU backend. Convert to
1701 // 32 bit.
1702 info = SkImageInfo::Make(size.width(), size.height(), kRGBA_8888_SkColorType,
1703 kPremul_SkAlphaType, fColorSpace);
1704 }
mtklein748ca3b2015-01-15 10:56:12 -08001705 dst->allocPixels(info);
Mike Reed12e946b2017-04-17 10:53:29 -04001706 canvas->readPixels(*dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001707 if (FLAGS_abandonGpuContext) {
1708 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001709 } else if (FLAGS_releaseAndAbandonGpuContext) {
1710 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001711 }
Brian Salomonf865b052018-03-09 09:01:53 -05001712#if SK_SUPPORT_GPU
1713 if (!context->contextPriv().abandoned()) {
1714 surface.reset();
1715 if (backendTexture.isValid()) {
Brian Salomon26102cb2018-03-09 09:33:19 -05001716 context->contextPriv().getGpu()->deleteTestingOnlyBackendTexture(backendTexture);
Brian Salomonf865b052018-03-09 09:01:53 -05001717 }
1718 if (backendRT.isValid()) {
1719 context->contextPriv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
1720 }
1721 }
1722#endif
mtklein748ca3b2015-01-15 10:56:12 -08001723 return "";
1724}
1725
1726/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1727
Brian Osmanf9810662017-08-30 10:02:10 -04001728GPUThreadTestingSink::GPUThreadTestingSink(GrContextFactory::ContextType ct,
1729 GrContextFactory::ContextOverrides overrides,
Brian Salomonf865b052018-03-09 09:01:53 -05001730 SkCommandLineConfigGpu::SurfType surfType,
Brian Osmanf9810662017-08-30 10:02:10 -04001731 int samples,
1732 bool diText,
1733 SkColorType colorType,
1734 SkAlphaType alphaType,
1735 sk_sp<SkColorSpace> colorSpace,
1736 bool threaded,
1737 const GrContextOptions& grCtxOptions)
Brian Salomonf865b052018-03-09 09:01:53 -05001738 : INHERITED(ct, overrides, surfType, samples, diText, colorType, alphaType,
1739 std::move(colorSpace), threaded, grCtxOptions)
Chris Dalton040238b2017-12-18 14:22:34 -07001740#if SK_SUPPORT_GPU
Mike Klein022cfa22017-09-01 11:53:16 -04001741 , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)) {
Chris Dalton040238b2017-12-18 14:22:34 -07001742#else
1743 , fExecutor(nullptr) {
1744#endif
Brian Osmanf9810662017-08-30 10:02:10 -04001745 SkASSERT(fExecutor);
1746}
1747
1748Error GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1749 SkString* log) const {
1750 // Draw twice, once with worker threads, and once without. Verify that we get the same result.
1751 // Also, force us to only use the software path renderer, so we really stress-test the threaded
1752 // version of that code.
1753 GrContextOptions contextOptions = this->baseContextOptions();
Brian Osman195c05b2017-08-30 15:14:04 -04001754 contextOptions.fGpuPathRenderers = GpuPathRenderers::kNone;
Brian Osmanf9810662017-08-30 10:02:10 -04001755
1756 contextOptions.fExecutor = fExecutor.get();
1757 Error err = this->onDraw(src, dst, wStream, log, contextOptions);
1758 if (!err.isEmpty() || !dst) {
1759 return err;
1760 }
1761
1762 SkBitmap reference;
1763 SkString refLog;
1764 SkDynamicMemoryWStream refStream;
1765 contextOptions.fExecutor = nullptr;
1766 Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1767 if (!refErr.isEmpty()) {
1768 return refErr;
1769 }
1770
Brian Osmane5756ec2017-09-06 17:08:30 -04001771 return compare_bitmaps(reference, *dst);
Brian Osmanf9810662017-08-30 10:02:10 -04001772}
1773
1774/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1775
halcanary47ef4d52015-03-03 09:13:09 -08001776static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001777 if (src.size().isEmpty()) {
1778 return "Source has empty dimensions";
1779 }
halcanary47ef4d52015-03-03 09:13:09 -08001780 SkASSERT(doc);
halcanary45420a92016-06-02 12:41:14 -07001781 int pageCount = src.pageCount();
1782 for (int i = 0; i < pageCount; ++i) {
1783 int width = src.size(i).width(), height = src.size(i).height();
halcanary7e798182015-04-14 14:06:18 -07001784 SkCanvas* canvas =
1785 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1786 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001787 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001788 }
halcanary45420a92016-06-02 12:41:14 -07001789 Error err = src.draw(i, canvas);
halcanary7e798182015-04-14 14:06:18 -07001790 if (!err.isEmpty()) {
1791 return err;
1792 }
1793 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001794 }
reedd14df7c2016-09-22 14:12:46 -07001795 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -08001796 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001797 return "";
1798}
1799
halcanary47ef4d52015-03-03 09:13:09 -08001800Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary4b656662016-04-27 07:45:18 -07001801 SkDocument::PDFMetadata metadata;
1802 metadata.fTitle = src.name();
1803 metadata.fSubject = "rendering correctness test";
1804 metadata.fCreator = "Skia/DM";
Mike Reeda4daf192017-12-14 13:25:04 -05001805 metadata.fRasterDPI = fRasterDpi;
1806 metadata.fPDFA = fPDFA;
1807 sk_sp<SkDocument> doc = SkDocument::MakePDF(dst, metadata);
halcanary47ef4d52015-03-03 09:13:09 -08001808 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001809 return "SkDocument::MakePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001810 }
1811 return draw_skdocument(src, doc.get(), dst);
1812}
1813
1814/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1815
1816XPSSink::XPSSink() {}
1817
Hal Canaryabc88d22017-02-06 09:26:49 -05001818#ifdef SK_BUILD_FOR_WIN
Hal Canary5e221e72017-02-06 09:51:42 -05001819static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() {
1820 IXpsOMObjectFactory* factory;
1821 HRN(CoCreateInstance(CLSID_XpsOMObjectFactory,
1822 nullptr,
1823 CLSCTX_INPROC_SERVER,
1824 IID_PPV_ARGS(&factory)));
1825 return SkTScopedComPtr<IXpsOMObjectFactory>(factory);
1826}
1827
1828Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canaryabc88d22017-02-06 09:26:49 -05001829 SkAutoCoInitialize com;
1830 if (!com.succeeded()) {
1831 return "Could not initialize COM.";
1832 }
Hal Canary5e221e72017-02-06 09:51:42 -05001833 SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory();
1834 if (!factory) {
1835 return "Failed to create XPS Factory.";
1836 }
1837 sk_sp<SkDocument> doc(SkDocument::MakeXPS(dst, factory.get()));
halcanary47ef4d52015-03-03 09:13:09 -08001838 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001839 return "SkDocument::MakeXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001840 }
1841 return draw_skdocument(src, doc.get(), dst);
1842}
Hal Canary5e221e72017-02-06 09:51:42 -05001843#else
1844Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1845 return "XPS not supported on this platform.";
1846}
1847#endif
reed54dc4872016-09-13 08:09:45 -07001848
1849/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1850
1851PipeSink::PipeSink() {}
1852
1853Error PipeSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1854 return src.draw(SkPipeSerializer().beginWrite(SkRect::Make(src.size()), dst));
1855}
1856
mtklein748ca3b2015-01-15 10:56:12 -08001857/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1858
mtklein9c3f17d2015-01-28 11:35:18 -08001859SKPSink::SKPSink() {}
1860
mtkleinb9eb4ac2015-02-02 18:26:03 -08001861Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001862 SkSize size;
1863 size = src.size();
1864 SkPictureRecorder recorder;
1865 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1866 if (!err.isEmpty()) {
1867 return err;
1868 }
reedca2622b2016-03-18 07:25:55 -07001869 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001870 return "";
1871}
1872
1873/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1874
Hal Canary85c7fe82016-10-25 10:33:27 -04001875Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1876 SkDebugCanvas debugCanvas(src.size().width(), src.size().height());
1877 Error err = src.draw(&debugCanvas);
1878 if (!err.isEmpty()) {
1879 return err;
1880 }
Mike Reed5df49342016-11-12 08:06:55 -06001881 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
Hal Canary85c7fe82016-10-25 10:33:27 -04001882 UrlDataManager dataManager(SkString("data"));
1883 Json::Value json = debugCanvas.toJSON(
1884 dataManager, debugCanvas.getSize(), nullCanvas.get());
1885 std::string value = Json::StyledWriter().write(json);
1886 return dst->write(value.c_str(), value.size()) ? "" : "SkWStream Error";
1887}
1888
1889/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1890
Bryce Thomas95a7b762018-03-02 13:54:21 -08001891SVGSink::SVGSink(int pageIndex) : fPageIndex(pageIndex) {}
mtklein8a4527e2015-01-31 20:00:58 -08001892
mtkleinb9eb4ac2015-02-02 18:26:03 -08001893Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita718df0a2016-07-15 10:33:29 -07001894#if defined(SK_XML)
Bryce Thomas95a7b762018-03-02 13:54:21 -08001895 if (src.pageCount() > 1) {
1896 int pageCount = src.pageCount();
1897 if (fPageIndex > pageCount - 1) {
1898 return Error(SkStringPrintf("Page index %d too high for document with only %d pages.",
1899 fPageIndex, pageCount));
1900 }
1901 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001902 std::unique_ptr<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
Bryce Thomas95a7b762018-03-02 13:54:21 -08001903 return src.draw(fPageIndex,
1904 SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
Mike Reed5df49342016-11-12 08:06:55 -06001905 SkIntToScalar(src.size().height())),
Bryce Thomas95a7b762018-03-02 13:54:21 -08001906 xmlWriter.get())
1907 .get());
fmalita718df0a2016-07-15 10:33:29 -07001908#else
1909 return Error("SVG sink is disabled.");
1910#endif // SK_XML
mtklein8a4527e2015-01-31 20:00:58 -08001911}
1912
1913/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1914
brianosmanb109b8c2016-06-16 13:03:24 -07001915RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
mtklein27c3fdd2016-02-26 14:43:21 -08001916 : fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001917 , fColorSpace(std::move(colorSpace)) {}
mtklein748ca3b2015-01-15 10:56:12 -08001918
Yuqian Lib8b62532018-02-23 14:13:36 +08001919void RasterSink::allocPixels(const Src& src, SkBitmap* dst) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001920 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001921 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1922 SkAlphaType alphaType = kPremul_SkAlphaType;
1923 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1924
Mike Reed086a4272017-07-18 10:53:11 -04001925 dst->allocPixelsFlags(SkImageInfo::Make(size.width(), size.height(),
1926 fColorType, alphaType, fColorSpace),
1927 SkBitmap::kZeroPixels_AllocFlag);
Yuqian Lib8b62532018-02-23 14:13:36 +08001928}
1929
1930Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
1931 this->allocPixels(src, dst);
mtklein748ca3b2015-01-15 10:56:12 -08001932 SkCanvas canvas(*dst);
1933 return src.draw(&canvas);
1934}
1935
1936/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1937
Yuqian Lib8b62532018-02-23 14:13:36 +08001938ThreadedSink::ThreadedSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
1939 : RasterSink(colorType, colorSpace)
1940 , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_backendThreads)) {
1941}
1942
1943Error ThreadedSink::draw(const Src& src, SkBitmap* dst, SkWStream* stream, SkString* str) const {
1944 this->allocPixels(src, dst);
1945
1946 std::unique_ptr<SkThreadedBMPDevice> device(new SkThreadedBMPDevice(
1947 *dst, FLAGS_backendTiles, FLAGS_backendThreads, fExecutor.get()));
1948 std::unique_ptr<SkCanvas> canvas(new SkCanvas(device.get()));
1949 Error result = src.draw(canvas.get());
1950 canvas->flush();
1951 return result;
1952
1953 // ??? yuqian: why does the following give me segmentation fault while the above one works?
1954 // The seg fault occurs right in the beginning of ThreadedSink::draw with invalid
1955 // memory address (it would crash without even calling this->allocPixels).
1956
1957 // SkThreadedBMPDevice device(*dst, tileCnt, FLAGS_cpuThreads, fExecutor.get());
1958 // SkCanvas canvas(&device);
1959 // Error result = src.draw(&canvas);
1960 // canvas.flush();
1961 // return result;
1962}
1963
1964/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1965
mtkleina16e69e2015-05-05 11:38:45 -07001966// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001967// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001968// Several examples below.
1969
mtkleincbf89782016-02-19 14:27:14 -08001970template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001971static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001972 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001973 class ProxySrc : public Src {
1974 public:
mtkleincbf89782016-02-19 14:27:14 -08001975 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001976 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001977 Name name() const override { return "ProxySrc"; }
1978 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001979 private:
mtkleincbf89782016-02-19 14:27:14 -08001980 SkISize fSize;
1981 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001982 };
msarett62d3b102015-12-10 15:14:27 -08001983 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001984}
1985
1986/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1987
mtklein4a34ecb2016-01-08 10:19:35 -08001988DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1989
1990// Is *bitmap identical to what you get drawing src into sink?
1991static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1992 // We can only check raster outputs.
1993 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1994 if (FLAGS_check && bitmap) {
1995 SkBitmap reference;
1996 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001997 SkDynamicMemoryWStream wStream;
1998 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001999 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
2000 SkASSERT(err.isEmpty());
2001 if (!err.isEmpty()) {
2002 return err;
2003 }
Brian Osmane5756ec2017-09-06 17:08:30 -04002004 return compare_bitmaps(reference, *bitmap);
mtklein4a34ecb2016-01-08 10:19:35 -08002005 }
2006 return "";
2007}
2008
2009/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2010
mtkleind603b222015-02-17 11:13:33 -08002011static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
2012 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
2013 matrix->mapRect(&bounds);
2014 matrix->postTranslate(-bounds.x(), -bounds.y());
Hal Canaryfafe1352017-04-11 12:12:02 -04002015 return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())};
mtkleind603b222015-02-17 11:13:33 -08002016}
2017
msarett62d3b102015-12-10 15:14:27 -08002018ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08002019
mtkleinb9eb4ac2015-02-02 18:26:03 -08002020Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07002021 SkMatrix matrix = fMatrix;
2022 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
Ben Wagner145dbcd2016-11-03 14:40:50 -04002023 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07002024 canvas->concat(matrix);
2025 return src.draw(canvas);
2026 });
mtklein748ca3b2015-01-15 10:56:12 -08002027}
2028
mtkleind603b222015-02-17 11:13:33 -08002029// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
2030// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08002031ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08002032
2033Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2034 Error err = fSink->draw(src, bitmap, stream, log);
2035 if (!err.isEmpty()) {
2036 return err;
2037 }
2038
2039 SkMatrix inverse;
2040 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
2041 return "Cannot upright --matrix.";
2042 }
2043 SkMatrix upright = SkMatrix::I();
2044 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
2045 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
2046 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
2047 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
2048
2049 SkBitmap uprighted;
2050 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
2051 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
2052
2053 SkCanvas canvas(uprighted);
2054 canvas.concat(upright);
2055 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07002056 paint.setBlendMode(SkBlendMode::kSrc);
mtkleind603b222015-02-17 11:13:33 -08002057 canvas.drawBitmap(*bitmap, 0, 0, &paint);
2058
2059 *bitmap = uprighted;
mtkleind603b222015-02-17 11:13:33 -08002060 return "";
2061}
2062
mtklein748ca3b2015-01-15 10:56:12 -08002063/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2064
mtkleina16e69e2015-05-05 11:38:45 -07002065Error ViaSerialization::draw(
2066 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08002067 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07002068 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08002069 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07002070 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
2071 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08002072 if (!err.isEmpty()) {
2073 return err;
2074 }
reedca2622b2016-03-18 07:25:55 -07002075 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08002076
2077 // Serialize it and then deserialize it.
reed39eaf5f2016-09-15 07:19:35 -07002078 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
mtklein748ca3b2015-01-15 10:56:12 -08002079
Ben Wagner145dbcd2016-11-03 14:40:50 -04002080 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07002081 canvas->drawPicture(deserialized);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002082 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07002083 });
mtklein748ca3b2015-01-15 10:56:12 -08002084}
2085
2086/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2087
msarett62d3b102015-12-10 15:14:27 -08002088ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
2089 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07002090 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08002091 , fH(h)
mtklein78829242015-05-06 07:54:07 -07002092 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08002093
mtkleinb9eb4ac2015-02-02 18:26:03 -08002094Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07002095 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08002096 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07002097 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
2098 SkIntToScalar(size.height()),
2099 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08002100 if (!err.isEmpty()) {
2101 return err;
2102 }
reedca2622b2016-03-18 07:25:55 -07002103 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08002104
Ben Wagner145dbcd2016-11-03 14:40:50 -04002105 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07002106 const int xTiles = (size.width() + fW - 1) / fW,
2107 yTiles = (size.height() + fH - 1) / fH;
2108 SkMultiPictureDraw mpd(xTiles*yTiles);
reede8f30622016-03-23 18:59:25 -07002109 SkTArray<sk_sp<SkSurface>> surfaces;
2110// surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08002111
mtkleina16e69e2015-05-05 11:38:45 -07002112 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
2113 for (int j = 0; j < yTiles; j++) {
2114 for (int i = 0; i < xTiles; i++) {
2115 // This lets our ultimate Sink determine the best kind of surface.
2116 // E.g., if it's a GpuSink, the surfaces and images are textures.
reede8f30622016-03-23 18:59:25 -07002117 auto s = canvas->makeSurface(info);
mtkleina16e69e2015-05-05 11:38:45 -07002118 if (!s) {
reede8f30622016-03-23 18:59:25 -07002119 s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08002120 }
reede8f30622016-03-23 18:59:25 -07002121 surfaces.push_back(s);
mtkleina16e69e2015-05-05 11:38:45 -07002122 SkCanvas* c = s->getCanvas();
2123 c->translate(SkIntToScalar(-i * fW),
2124 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
reedca2622b2016-03-18 07:25:55 -07002125 mpd.add(c, pic.get());
mtklein748ca3b2015-01-15 10:56:12 -08002126 }
mtklein748ca3b2015-01-15 10:56:12 -08002127 }
mtkleina16e69e2015-05-05 11:38:45 -07002128 mpd.draw();
2129 for (int j = 0; j < yTiles; j++) {
2130 for (int i = 0; i < xTiles; i++) {
reed9ce9d672016-03-17 10:51:11 -07002131 sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot());
mtkleina16e69e2015-05-05 11:38:45 -07002132 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
2133 }
2134 }
mtkleina16e69e2015-05-05 11:38:45 -07002135 return "";
2136 });
mtklein748ca3b2015-01-15 10:56:12 -08002137}
2138
mtkleinb7e8d692015-04-07 08:30:32 -07002139/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2140
mtklein4a34ecb2016-01-08 10:19:35 -08002141Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2142 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04002143 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtklein4a34ecb2016-01-08 10:19:35 -08002144 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07002145 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08002146 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
2147 SkIntToScalar(size.height())));
2148 if (!err.isEmpty()) {
2149 return err;
2150 }
reedca2622b2016-03-18 07:25:55 -07002151 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08002152 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002153 return check_against_reference(bitmap, src, fSink.get());
mtklein4a34ecb2016-01-08 10:19:35 -08002154 });
2155}
2156
2157/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2158
reed54dc4872016-09-13 08:09:45 -07002159Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2160 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04002161 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
reed54dc4872016-09-13 08:09:45 -07002162 SkDynamicMemoryWStream tmpStream;
2163 Error err = src.draw(SkPipeSerializer().beginWrite(SkRect::Make(size), &tmpStream));
2164 if (!err.isEmpty()) {
2165 return err;
2166 }
2167 sk_sp<SkData> data = tmpStream.detachAsData();
2168 SkPipeDeserializer().playback(data->data(), data->size(), canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002169 return check_against_reference(bitmap, src, fSink.get());
reed54dc4872016-09-13 08:09:45 -07002170 });
2171}
2172
2173/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2174
Mike Reedbae888e2017-02-18 16:50:45 -05002175#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -05002176#include "SkXMLWriter.h"
2177#include "SkSVGCanvas.h"
2178#include "SkSVGDOM.h"
2179
2180Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2181 auto size = src.size();
2182 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
2183 SkDynamicMemoryWStream wstream;
2184 SkXMLStreamWriter writer(&wstream);
2185 Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get());
2186 if (!err.isEmpty()) {
2187 return err;
2188 }
2189 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
2190 auto dom = SkSVGDOM::MakeFromStream(*rstream);
2191 if (dom) {
2192 dom->setContainerSize(SkSize::Make(size));
2193 dom->render(canvas);
2194 }
2195 return "";
2196 });
2197}
Mike Reedbae888e2017-02-18 16:50:45 -05002198#endif
Mike Reedf67c4592017-02-17 17:06:11 -05002199
2200/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2201
mtklein9c5052f2016-08-06 12:51:51 -07002202Error ViaLite::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2203 auto size = src.size();
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002204 SkIRect bounds = {0,0, size.width(), size.height()};
Ben Wagner145dbcd2016-11-03 14:40:50 -04002205 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002206 SkLiteDL dl;
mtklein8bbbb692016-08-15 12:56:00 -07002207 SkLiteRecorder rec;
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002208 rec.reset(&dl, bounds);
mtklein8bbbb692016-08-15 12:56:00 -07002209
2210 Error err = src.draw(&rec);
mtklein9c5052f2016-08-06 12:51:51 -07002211 if (!err.isEmpty()) {
2212 return err;
2213 }
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002214 dl.draw(canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002215 return check_against_reference(bitmap, src, fSink.get());
mtklein9c5052f2016-08-06 12:51:51 -07002216 });
2217}
2218
Mike Klein841101d2017-03-10 09:55:51 -05002219/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2220
Mike Klein919cc452017-03-18 15:36:52 +00002221ViaCSXform::ViaCSXform(Sink* sink, sk_sp<SkColorSpace> cs, bool colorSpin)
2222 : Via(sink)
2223 , fCS(std::move(cs))
2224 , fColorSpin(colorSpin) {}
Mike Klein841101d2017-03-10 09:55:51 -05002225
2226Error ViaCSXform::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2227 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(),
2228 [&](SkCanvas* canvas) -> Error {
Brian Osman5b7e4702017-10-31 16:41:29 -04002229 {
2230 SkAutoCanvasRestore acr(canvas, true);
2231 auto proxy = SkCreateColorSpaceXformCanvas(canvas, fCS);
2232 Error err = src.draw(proxy.get());
2233 if (!err.isEmpty()) {
2234 return err;
2235 }
Mike Klein919cc452017-03-18 15:36:52 +00002236 }
2237
2238 // Undo the color spin, so we can look at the pixels in Gold.
2239 if (fColorSpin) {
2240 SkBitmap pixels;
2241 pixels.allocPixels(canvas->imageInfo());
Mike Reed12e946b2017-04-17 10:53:29 -04002242 canvas->readPixels(pixels, 0, 0);
Mike Klein919cc452017-03-18 15:36:52 +00002243
Brian Osman5b7e4702017-10-31 16:41:29 -04002244 SkPaint rotateColors;
2245 SkScalar matrix[20] = { 0, 0, 1, 0, 0, // B -> R
2246 1, 0, 0, 0, 0, // R -> G
2247 0, 1, 0, 0, 0, // G -> B
2248 0, 0, 0, 1, 0 };
2249 rotateColors.setBlendMode(SkBlendMode::kSrc);
2250 rotateColors.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
2251 canvas->drawBitmap(pixels, 0, 0, &rotateColors);
Mike Klein919cc452017-03-18 15:36:52 +00002252 }
2253
2254 return "";
Mike Klein841101d2017-03-10 09:55:51 -05002255 });
2256}
2257
mtklein748ca3b2015-01-15 10:56:12 -08002258} // namespace DM