blob: f99afe48597247dd015f8c69d3aa1192a5ea39a4 [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"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
scroggof24f2242015-03-03 08:59:20 -080010#include "SkCodec.h"
msarettb714fb02016-01-22 14:46:42 -080011#include "SkCodecImageGenerator.h"
mtkleina16e69e2015-05-05 11:38:45 -070012#include "SkCommonFlags.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070013#include "SkData.h"
mtklein748ca3b2015-01-15 10:56:12 -080014#include "SkDocument.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080015#include "SkError.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070016#include "SkImageGenerator.h"
msarett18976312016-03-09 14:20:58 -080017#include "SkImageGeneratorCG.h"
msarettfc0b6d12016-03-17 13:50:17 -070018#include "SkImageGeneratorWIC.h"
mtkleinc8be09a2016-01-04 18:56:57 -080019#include "SkMallocPixelRef.h"
mtklein748ca3b2015-01-15 10:56:12 -080020#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080021#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080022#include "SkOSFile.h"
msarett9e9444c2016-02-03 12:39:10 -080023#include "SkOpts.h"
mtkleinffa901a2015-03-16 10:38:07 -070024#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080025#include "SkPictureRecorder.h"
26#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070027#include "SkRecordDraw.h"
28#include "SkRecorder.h"
fmalita2aafe6f2015-02-06 12:51:10 -080029#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080030#include "SkStream.h"
mtklein449d9b72015-09-28 10:33:02 -070031#include "SkTLogic.h"
fmalita2aafe6f2015-02-06 12:51:10 -080032#include "SkXMLWriter.h"
msarette6dd0042015-10-09 11:07:34 -070033#include "SkSwizzler.h"
mtklein64593522015-11-12 10:41:05 -080034#include <functional>
mtklein748ca3b2015-01-15 10:56:12 -080035
msarettfc0b6d12016-03-17 13:50:17 -070036#if defined(SK_BUILD_FOR_WIN)
37 #include "SkAutoCoInitialize.h"
38#endif
39
halcanary7e798182015-04-14 14:06:18 -070040DEFINE_bool(multiPage, false, "For document-type backends, render the source"
41 " into multiple pages");
scroggo3ac66e92016-02-08 15:09:48 -080042DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070043
bsalomon3724e572016-03-30 18:56:19 -070044using sk_gpu_test::GrContextFactory;
45
mtklein748ca3b2015-01-15 10:56:12 -080046namespace DM {
47
mtklein748ca3b2015-01-15 10:56:12 -080048GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
49
50Error GMSrc::draw(SkCanvas* canvas) const {
halcanary96fcdcc2015-08-27 07:41:13 -070051 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080052 canvas->concat(gm->getInitialTransform());
53 gm->draw(canvas);
54 return "";
55}
56
57SkISize GMSrc::size() const {
halcanary96fcdcc2015-08-27 07:41:13 -070058 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080059 return gm->getISize();
60}
61
62Name GMSrc::name() const {
halcanary96fcdcc2015-08-27 07:41:13 -070063 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080064 return gm->getName();
65}
66
bsalomon4ee6bd82015-05-27 13:23:23 -070067void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
halcanary96fcdcc2015-08-27 07:41:13 -070068 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070069 gm->modifyGrContextOptions(options);
70}
71
mtklein748ca3b2015-01-15 10:56:12 -080072/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
73
msarett5cb48852015-11-06 08:56:32 -080074BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode,
msaretta5783ae2015-09-08 15:35:32 -070075 CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
76 : fPath(path)
77 , fStrategy(strategy)
78 , fMode(mode)
79 , fDstColorType(dstColorType)
80 , fSampleSize(sampleSize)
81{}
82
83bool BRDSrc::veto(SinkFlags flags) const {
84 // No need to test to non-raster or indirect backends.
85 return flags.type != SinkFlags::kRaster
86 || flags.approach != SinkFlags::kDirect;
87}
88
msarett5cb48852015-11-06 08:56:32 -080089static SkBitmapRegionDecoder* create_brd(Path path,
90 SkBitmapRegionDecoder::Strategy strategy) {
msaretta5783ae2015-09-08 15:35:32 -070091 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
92 if (!encoded) {
93 return NULL;
94 }
msarett5cb48852015-11-06 08:56:32 -080095 return SkBitmapRegionDecoder::Create(encoded, strategy);
msaretta5783ae2015-09-08 15:35:32 -070096}
97
98Error BRDSrc::draw(SkCanvas* canvas) const {
99 SkColorType colorType = canvas->imageInfo().colorType();
100 if (kRGB_565_SkColorType == colorType &&
101 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
102 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
103 }
104 switch (fDstColorType) {
105 case CodecSrc::kGetFromCanvas_DstColorType:
106 break;
107 case CodecSrc::kIndex8_Always_DstColorType:
108 colorType = kIndex_8_SkColorType;
109 break;
110 case CodecSrc::kGrayscale_Always_DstColorType:
111 colorType = kGray_8_SkColorType;
112 break;
msarett34e0ec42016-04-22 16:27:24 -0700113 default:
114 SkASSERT(false);
115 break;
msaretta5783ae2015-09-08 15:35:32 -0700116 }
117
msarett5cb48852015-11-06 08:56:32 -0800118 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700119 if (nullptr == brd.get()) {
120 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
121 }
122
msarett04965c62015-10-12 10:24:38 -0700123 if (!brd->conversionSupported(colorType)) {
mtklein9b439152015-12-09 13:02:26 -0800124 return Error::Nonfatal("Cannot convert to color type.");
msarett04965c62015-10-12 10:24:38 -0700125 }
126
msaretta5783ae2015-09-08 15:35:32 -0700127 const uint32_t width = brd->width();
128 const uint32_t height = brd->height();
129 // Visually inspecting very small output images is not necessary.
130 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
131 return Error::Nonfatal("Scaling very small images is uninteresting.");
132 }
133 switch (fMode) {
134 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700135 SkBitmap bitmap;
136 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
137 fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800138 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700139 }
140 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800141 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700142 }
msarett35e5d1b2015-10-27 12:50:25 -0700143 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700144 return "";
145 }
146 case kDivisor_Mode: {
147 const uint32_t divisor = 2;
148 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800149 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700150 }
151
152 // Use a border to test subsets that extend outside the image.
153 // We will not allow the border to be larger than the image dimensions. Allowing
154 // these large borders causes off by one errors that indicate a problem with the
155 // test suite, not a problem with the implementation.
156 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
157 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
158 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
159
160 // We may need to clear the canvas to avoid uninitialized memory.
161 // Assume we are scaling a 780x780 image with sampleSize = 8.
162 // The output image should be 97x97.
163 // Each subset will be 390x390.
164 // Each scaled subset be 48x48.
165 // Four scaled subsets will only fill a 96x96 image.
166 // The bottom row and last column will not be touched.
167 // This is an unfortunate result of our rounding rules when scaling.
168 // Maybe we need to consider testing scaled subsets without trying to
169 // combine them to match the full scaled image? Or maybe this is the
170 // best we can do?
171 canvas->clear(0);
172
173 for (uint32_t x = 0; x < divisor; x++) {
174 for (uint32_t y = 0; y < divisor; y++) {
175 // Calculate the subset dimensions
176 uint32_t subsetWidth = width / divisor;
177 uint32_t subsetHeight = height / divisor;
178 const int left = x * subsetWidth;
179 const int top = y * subsetHeight;
180
181 // Increase the size of the last subset in each row or column, when the
182 // divisor does not divide evenly into the image dimensions
183 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
184 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
185
186 // Increase the size of the subset in order to have a border on each side
187 const int decodeLeft = left - unscaledBorder;
188 const int decodeTop = top - unscaledBorder;
189 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
190 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700191 SkBitmap bitmap;
192 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
193 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800194 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700195 }
196 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800197 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700198 }
199
msarett35e5d1b2015-10-27 12:50:25 -0700200 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700201 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
202 (SkScalar) (subsetWidth / fSampleSize),
203 (SkScalar) (subsetHeight / fSampleSize)),
204 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
205 (SkScalar) (top / fSampleSize),
206 (SkScalar) (subsetWidth / fSampleSize),
207 (SkScalar) (subsetHeight / fSampleSize)),
208 nullptr);
209 }
210 }
211 return "";
212 }
213 default:
214 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800215 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700216 }
217}
218
219SkISize BRDSrc::size() const {
msarett5cb48852015-11-06 08:56:32 -0800220 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700221 if (brd) {
222 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
223 SkTMax(1, brd->height() / (int) fSampleSize));
224 }
225 return SkISize::Make(0, 0);
226}
227
228static SkString get_scaled_name(const Path& path, float scale) {
229 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
230}
231
232Name BRDSrc::name() const {
233 // We will replicate the names used by CodecSrc so that images can
234 // be compared in Gold.
235 if (1 == fSampleSize) {
236 return SkOSPath::Basename(fPath.c_str());
237 }
msarett4b0778e2015-11-13 09:59:11 -0800238 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700239}
240
241/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
242
scroggo3ac66e92016-02-08 15:09:48 -0800243static bool serial_from_path_name(const SkString& path) {
244 if (!FLAGS_RAW_threading) {
245 static const char* const exts[] = {
246 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
247 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
248 };
249 const char* actualExt = strrchr(path.c_str(), '.');
250 if (actualExt) {
251 actualExt++;
252 for (auto* ext : exts) {
253 if (0 == strcmp(ext, actualExt)) {
254 return true;
255 }
256 }
257 }
258 }
259 return false;
260}
261
scroggoc5560be2016-02-03 09:42:42 -0800262CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
263 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700264 : fPath(path)
265 , fMode(mode)
266 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800267 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700268 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800269 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700270{}
mtklein748ca3b2015-01-15 10:56:12 -0800271
mtklein99cab4e2015-07-31 06:43:04 -0700272bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800273 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800274 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700275}
scroggo9b77ddd2015-03-19 06:03:39 -0700276
msarett34e0ec42016-04-22 16:27:24 -0700277// Allows us to test decodes to non-native 8888.
278void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
279 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
280 return;
281 }
282
283 for (int y = 0; y < bitmap.height(); y++) {
284 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
285 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
286 }
287}
288
msarett9e9444c2016-02-03 12:39:10 -0800289// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
290// This allows us to still test unpremultiplied decodes.
291void premultiply_if_necessary(SkBitmap& bitmap) {
292 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
293 return;
294 }
295
296 switch (bitmap.colorType()) {
297 case kN32_SkColorType:
298 for (int y = 0; y < bitmap.height(); y++) {
299 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
300 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
301 }
302 break;
303 case kIndex_8_SkColorType: {
304 SkColorTable* colorTable = bitmap.getColorTable();
305 SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
306 SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count());
307 break;
308 }
309 default:
310 // No need to premultiply kGray or k565 outputs.
311 break;
312 }
msarette1daa482016-02-03 15:31:18 -0800313
314 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
315 // bitmap as kPremul.
316 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800317}
318
scroggoc5560be2016-02-03 09:42:42 -0800319bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
320 CodecSrc::DstColorType dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700321 switch (dstColorType) {
322 case CodecSrc::kIndex8_Always_DstColorType:
323 if (kRGB_565_SkColorType == canvasColorType) {
324 return false;
325 }
scroggoc5560be2016-02-03 09:42:42 -0800326 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700327 break;
328 case CodecSrc::kGrayscale_Always_DstColorType:
msarett55f7bdd2016-02-16 13:24:54 -0800329 if (kRGB_565_SkColorType == canvasColorType ||
330 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
msarett3d9d7a72015-10-21 10:27:10 -0700331 return false;
332 }
scroggoc5560be2016-02-03 09:42:42 -0800333 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700334 break;
msarett34e0ec42016-04-22 16:27:24 -0700335 case CodecSrc::kNonNative8888_Always_DstColorType:
336 if (kRGB_565_SkColorType == canvasColorType) {
337 return false;
338 }
339#ifdef SK_PMCOLOR_IS_RGBA
340 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
341#else
342 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
343#endif
344 break;
msarett3d9d7a72015-10-21 10:27:10 -0700345 default:
msarett55f7bdd2016-02-16 13:24:54 -0800346 if (kRGB_565_SkColorType == canvasColorType &&
347 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
348 return false;
349 }
scroggoc5560be2016-02-03 09:42:42 -0800350 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700351 break;
352 }
353
msarett3d9d7a72015-10-21 10:27:10 -0700354 return true;
355}
356
mtkleine0effd62015-07-29 06:37:28 -0700357Error CodecSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -0800358 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800359 if (!encoded) {
360 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
361 }
msarettb714fb02016-01-22 14:46:42 -0800362
msarett3d9d7a72015-10-21 10:27:10 -0700363 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700364 if (nullptr == codec.get()) {
365 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700366 }
367
scroggoc5560be2016-02-03 09:42:42 -0800368 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
369 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700370 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
msarett438b2ad2015-04-09 12:43:10 -0700371 }
372
msarett0a242972015-06-11 14:27:27 -0700373 // Try to scale the image if it is desired
374 SkISize size = codec->getScaledDimensions(fScale);
375 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
376 return Error::Nonfatal("Test without scaling is uninteresting.");
377 }
msarettb32758a2015-08-18 13:22:46 -0700378
379 // Visually inspecting very small output images is not necessary. We will
380 // cover these cases in unit testing.
381 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
382 return Error::Nonfatal("Scaling very small images is uninteresting.");
383 }
msarett0a242972015-06-11 14:27:27 -0700384 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
385
msarett438b2ad2015-04-09 12:43:10 -0700386 // Construct a color table for the decode if necessary
halcanary96fcdcc2015-08-27 07:41:13 -0700387 SkAutoTUnref<SkColorTable> colorTable(nullptr);
388 SkPMColor* colorPtr = nullptr;
389 int* colorCountPtr = nullptr;
msarett438b2ad2015-04-09 12:43:10 -0700390 int maxColors = 256;
391 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
392 SkPMColor colors[256];
halcanary385fe4d2015-08-26 13:07:48 -0700393 colorTable.reset(new SkColorTable(colors, maxColors));
msarett438b2ad2015-04-09 12:43:10 -0700394 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
395 colorCountPtr = &maxColors;
396 }
397
scroggo9b77ddd2015-03-19 06:03:39 -0700398 SkBitmap bitmap;
msarettbb25b532016-01-13 09:31:39 -0800399 SkPixelRefFactory* factory = nullptr;
400 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
401 SkCodec::Options options;
402 if (kCodecZeroInit_Mode == fMode) {
403 factory = &zeroFactory;
404 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
405 }
msarett34e0ec42016-04-22 16:27:24 -0700406
407 SkImageInfo bitmapInfo = decodeInfo;
408 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
409 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
410 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
411 }
412 if (!bitmap.tryAllocPixels(bitmapInfo, factory, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800413 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
scroggo9b77ddd2015-03-19 06:03:39 -0700414 decodeInfo.width(), decodeInfo.height());
415 }
416
scroggo9c59ebc2015-03-25 13:48:49 -0700417 switch (fMode) {
msarettbb25b532016-01-13 09:31:39 -0800418 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700419 case kCodec_Mode: {
msarettbb25b532016-01-13 09:31:39 -0800420 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
msarett438b2ad2015-04-09 12:43:10 -0700421 colorPtr, colorCountPtr)) {
scroggoeb602a52015-07-09 08:16:03 -0700422 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700423 // We consider incomplete to be valid, since we should still decode what is
424 // available.
scroggoeb602a52015-07-09 08:16:03 -0700425 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700426 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700427 default:
428 // Everything else is considered a failure.
429 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
430 }
msarett9e9444c2016-02-03 12:39:10 -0800431 premultiply_if_necessary(bitmap);
msarett34e0ec42016-04-22 16:27:24 -0700432 swap_rb_if_necessary(bitmap, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700433 canvas->drawBitmap(bitmap, 0, 0);
scroggo9c59ebc2015-03-25 13:48:49 -0700434 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700435 }
scroggo9c59ebc2015-03-25 13:48:49 -0700436 case kScanline_Mode: {
scroggo46c57472015-09-30 08:57:13 -0700437 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
438 colorCountPtr)) {
msarettb65e6042016-02-23 05:37:25 -0800439 return "Could not start scanline decoder";
scroggo9c59ebc2015-03-25 13:48:49 -0700440 }
scroggo1c005e42015-08-04 09:24:45 -0700441
msarette6dd0042015-10-09 11:07:34 -0700442 void* dst = bitmap.getAddr(0, 0);
443 size_t rowBytes = bitmap.rowBytes();
444 uint32_t height = decodeInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700445 switch (codec->getScanlineOrder()) {
446 case SkCodec::kTopDown_SkScanlineOrder:
447 case SkCodec::kBottomUp_SkScanlineOrder:
448 case SkCodec::kNone_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700449 // We do not need to check the return value. On an incomplete
450 // image, memory will be filled with a default value.
451 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700452 break;
scroggo46c57472015-09-30 08:57:13 -0700453 case SkCodec::kOutOfOrder_SkScanlineOrder: {
msarett10522ff2015-09-07 08:54:01 -0700454 for (int y = 0; y < decodeInfo.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700455 int dstY = codec->outputScanline(y);
msarett10522ff2015-09-07 08:54:01 -0700456 void* dstPtr = bitmap.getAddr(0, dstY);
msarette6dd0042015-10-09 11:07:34 -0700457 // We complete the loop, even if this call begins to fail
458 // due to an incomplete image. This ensures any uninitialized
459 // memory will be filled with the proper value.
460 codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
msarett10522ff2015-09-07 08:54:01 -0700461 }
462 break;
463 }
464 }
465
msarett9e9444c2016-02-03 12:39:10 -0800466 premultiply_if_necessary(bitmap);
msarett34e0ec42016-04-22 16:27:24 -0700467 swap_rb_if_necessary(bitmap, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700468 canvas->drawBitmap(bitmap, 0, 0);
469 break;
470 }
msarett0a242972015-06-11 14:27:27 -0700471 case kStripe_Mode: {
472 const int height = decodeInfo.height();
473 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
474 // does not align with image blocks.
475 const int stripeHeight = 37;
476 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
477
478 // Decode odd stripes
scroggo46c57472015-09-30 08:57:13 -0700479 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
msarettb65e6042016-02-23 05:37:25 -0800480 colorCountPtr)) {
481 return "Could not start scanline decoder";
482 }
483
484 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
485 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
486 // to run this test for image types that do not have this scanline ordering.
487 if (SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
488 return Error::Nonfatal("kStripe test is only interesting for kTopDown codecs.");
msarett0a242972015-06-11 14:27:27 -0700489 }
msarette6dd0042015-10-09 11:07:34 -0700490
msarett0a242972015-06-11 14:27:27 -0700491 for (int i = 0; i < numStripes; i += 2) {
492 // Skip a stripe
493 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700494 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700495
496 // Read a stripe
497 const int startY = (i + 1) * stripeHeight;
498 const int linesToRead = SkTMin(stripeHeight, height - startY);
499 if (linesToRead > 0) {
msarette6dd0042015-10-09 11:07:34 -0700500 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700501 }
502 }
503
504 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700505 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
506 colorPtr, colorCountPtr);
scroggo1c005e42015-08-04 09:24:45 -0700507 if (SkCodec::kSuccess != startResult) {
508 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700509 }
510 for (int i = 0; i < numStripes; i += 2) {
511 // Read a stripe
512 const int startY = i * stripeHeight;
513 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarette6dd0042015-10-09 11:07:34 -0700514 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700515
516 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700517 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
518 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700519 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700520 }
521 }
msarett9e9444c2016-02-03 12:39:10 -0800522 premultiply_if_necessary(bitmap);
msarett34e0ec42016-04-22 16:27:24 -0700523 swap_rb_if_necessary(bitmap, fDstColorType);
msarett0a242972015-06-11 14:27:27 -0700524 canvas->drawBitmap(bitmap, 0, 0);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700525 break;
msarett0a242972015-06-11 14:27:27 -0700526 }
msarett91c22b22016-02-22 12:27:46 -0800527 case kCroppedScanline_Mode: {
528 const int width = decodeInfo.width();
529 const int height = decodeInfo.height();
530 // This value is chosen because, as we move across the image, it will sometimes
531 // align with the jpeg block sizes and it will sometimes not. This allows us
532 // to test interestingly different code paths in the implementation.
533 const int tileSize = 36;
534
535 SkCodec::Options opts;
536 SkIRect subset;
537 for (int x = 0; x < width; x += tileSize) {
538 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
539 opts.fSubset = &subset;
540 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &opts,
541 colorPtr, colorCountPtr)) {
542 return "Could not start scanline decoder.";
543 }
544
545 codec->getScanlines(bitmap.getAddr(x, 0), height, bitmap.rowBytes());
546 }
547
548 premultiply_if_necessary(bitmap);
msarett34e0ec42016-04-22 16:27:24 -0700549 swap_rb_if_necessary(bitmap, fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800550 canvas->drawBitmap(bitmap, 0, 0);
551 break;
552 }
scroggob636b452015-07-22 07:16:20 -0700553 case kSubset_Mode: {
554 // Arbitrarily choose a divisor.
555 int divisor = 2;
556 // Total width/height of the image.
557 const int W = codec->getInfo().width();
558 const int H = codec->getInfo().height();
559 if (divisor > W || divisor > H) {
560 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
561 "for %s with dimensions (%d x %d)", divisor,
562 fPath.c_str(), W, H));
563 }
564 // subset dimensions
565 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
566 const int w = SkAlign2(W / divisor);
567 const int h = SkAlign2(H / divisor);
568 SkIRect subset;
569 SkCodec::Options opts;
570 opts.fSubset = &subset;
571 SkBitmap subsetBm;
572 // We will reuse pixel memory from bitmap.
573 void* pixels = bitmap.getPixels();
574 // Keep track of left and top (for drawing subsetBm into canvas). We could use
575 // fScale * x and fScale * y, but we want integers such that the next subset will start
576 // where the last one ended. So we'll add decodeInfo.width() and height().
577 int left = 0;
578 for (int x = 0; x < W; x += w) {
579 int top = 0;
580 for (int y = 0; y < H; y+= h) {
581 // Do not make the subset go off the edge of the image.
582 const int preScaleW = SkTMin(w, W - x);
583 const int preScaleH = SkTMin(h, H - y);
584 subset.setXYWH(x, y, preScaleW, preScaleH);
585 // And scale
586 // FIXME: Should we have a version of getScaledDimensions that takes a subset
587 // into account?
msarettc7eb4902016-04-25 07:04:58 -0700588 const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale));
589 const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale));
590 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
591 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
592 size_t rowBytes = subsetBitmapInfo.minRowBytes();
593 if (!subsetBm.installPixels(subsetBitmapInfo, pixels, rowBytes, colorTable.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700594 nullptr, nullptr)) {
scroggob636b452015-07-22 07:16:20 -0700595 return SkStringPrintf("could not install pixels for %s.", fPath.c_str());
596 }
597 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
598 &opts, colorPtr, colorCountPtr);
599 switch (result) {
600 case SkCodec::kSuccess:
601 case SkCodec::kIncompleteInput:
602 break;
scroggob636b452015-07-22 07:16:20 -0700603 default:
604 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
605 "from %s with dimensions (%d x %d)\t error %d",
606 x, y, decodeInfo.width(), decodeInfo.height(),
607 fPath.c_str(), W, H, result);
608 }
msarett9e9444c2016-02-03 12:39:10 -0800609 premultiply_if_necessary(subsetBm);
msarettc7eb4902016-04-25 07:04:58 -0700610 swap_rb_if_necessary(subsetBm, fDstColorType);
scroggob636b452015-07-22 07:16:20 -0700611 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top));
612 // translate by the scaled height.
613 top += decodeInfo.height();
614 }
615 // translate by the scaled width.
616 left += decodeInfo.width();
617 }
618 return "";
619 }
msarettb714fb02016-01-22 14:46:42 -0800620 default:
621 SkASSERT(false);
622 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700623 }
scroggo9c59ebc2015-03-25 13:48:49 -0700624 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700625}
626
627SkISize CodecSrc::size() const {
628 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700629 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700630 if (nullptr == codec) {
631 return SkISize::Make(0, 0);
632 }
633 return codec->getScaledDimensions(fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700634}
635
636Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700637 if (1.0f == fScale) {
638 return SkOSPath::Basename(fPath.c_str());
msarett0a242972015-06-11 14:27:27 -0700639 }
msaretta5783ae2015-09-08 15:35:32 -0700640 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700641}
642
643/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
644
msarett3d9d7a72015-10-21 10:27:10 -0700645AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800646 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700647 : fPath(path)
648 , fMode(mode)
649 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800650 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700651 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800652 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700653{}
654
655bool AndroidCodecSrc::veto(SinkFlags flags) const {
656 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700657 return flags.type != SinkFlags::kRaster
658 || flags.approach != SinkFlags::kDirect;
659}
660
661Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
662 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
663 if (!encoded) {
664 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
665 }
666 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
667 if (nullptr == codec.get()) {
668 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
669 }
670
scroggoc5560be2016-02-03 09:42:42 -0800671 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
672 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700673 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
674 }
675
676 // Scale the image if it is desired.
677 SkISize size = codec->getSampledDimensions(fSampleSize);
678
679 // Visually inspecting very small output images is not necessary. We will
680 // cover these cases in unit testing.
681 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
682 return Error::Nonfatal("Scaling very small images is uninteresting.");
683 }
684 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
685
686 // Construct a color table for the decode if necessary
687 SkAutoTUnref<SkColorTable> colorTable(nullptr);
688 SkPMColor* colorPtr = nullptr;
689 int* colorCountPtr = nullptr;
690 int maxColors = 256;
691 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
692 SkPMColor colors[256];
693 colorTable.reset(new SkColorTable(colors, maxColors));
694 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
695 colorCountPtr = &maxColors;
696 }
697
698 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700699 SkImageInfo bitmapInfo = decodeInfo;
700 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
701 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
702 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
703 }
704 if (!bitmap.tryAllocPixels(bitmapInfo, nullptr, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800705 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
msarett3d9d7a72015-10-21 10:27:10 -0700706 decodeInfo.width(), decodeInfo.height());
707 }
708
709 // Create options for the codec.
710 SkAndroidCodec::AndroidOptions options;
711 options.fColorPtr = colorPtr;
712 options.fColorCount = colorCountPtr;
713 options.fSampleSize = fSampleSize;
714
715 switch (fMode) {
716 case kFullImage_Mode: {
717 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(),
718 &options)) {
719 case SkCodec::kSuccess:
720 case SkCodec::kIncompleteInput:
721 break;
msarett3d9d7a72015-10-21 10:27:10 -0700722 default:
723 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
724 }
msarett9e9444c2016-02-03 12:39:10 -0800725 premultiply_if_necessary(bitmap);
msarett34e0ec42016-04-22 16:27:24 -0700726 swap_rb_if_necessary(bitmap, fDstColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700727 canvas->drawBitmap(bitmap, 0, 0);
728 return "";
729 }
730 case kDivisor_Mode: {
731 const int width = codec->getInfo().width();
732 const int height = codec->getInfo().height();
733 const int divisor = 2;
734 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800735 return Error::Nonfatal("Divisor is larger than image dimension.");
msarett3d9d7a72015-10-21 10:27:10 -0700736 }
737
msarettfa23a9e2015-10-21 13:26:59 -0700738 // Keep track of the final decoded dimensions.
739 int finalScaledWidth = 0;
740 int finalScaledHeight = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700741 for (int x = 0; x < divisor; x++) {
742 for (int y = 0; y < divisor; y++) {
743 // Calculate the subset dimensions
744 int subsetWidth = width / divisor;
745 int subsetHeight = height / divisor;
746 const int left = x * subsetWidth;
747 const int top = y * subsetHeight;
748
749 // Increase the size of the last subset in each row or column, when the
750 // divisor does not divide evenly into the image dimensions
751 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
752 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
753 SkIRect subset = SkIRect::MakeXYWH(left, top, subsetWidth, subsetHeight);
754 if (!codec->getSupportedSubset(&subset)) {
mtklein9b439152015-12-09 13:02:26 -0800755 return "Could not get supported subset to decode.";
msarett3d9d7a72015-10-21 10:27:10 -0700756 }
757 options.fSubset = &subset;
msarettfa23a9e2015-10-21 13:26:59 -0700758 const int scaledWidthOffset = subset.left() / fSampleSize;
759 const int scaledHeightOffset = subset.top() / fSampleSize;
760 void* pixels = bitmap.getAddr(scaledWidthOffset, scaledHeightOffset);
msarett3d9d7a72015-10-21 10:27:10 -0700761 SkISize scaledSubsetSize = codec->getSampledSubsetDimensions(fSampleSize,
762 subset);
763 SkImageInfo subsetDecodeInfo = decodeInfo.makeWH(scaledSubsetSize.width(),
764 scaledSubsetSize.height());
765
msarettfa23a9e2015-10-21 13:26:59 -0700766 if (x + 1 == divisor && y + 1 == divisor) {
767 finalScaledWidth = scaledWidthOffset + scaledSubsetSize.width();
768 finalScaledHeight = scaledHeightOffset + scaledSubsetSize.height();
769 }
770
msarett3d9d7a72015-10-21 10:27:10 -0700771 switch (codec->getAndroidPixels(subsetDecodeInfo, pixels, bitmap.rowBytes(),
772 &options)) {
773 case SkCodec::kSuccess:
774 case SkCodec::kIncompleteInput:
775 break;
msarett3d9d7a72015-10-21 10:27:10 -0700776 default:
777 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
778 }
779 }
780 }
msarettfa23a9e2015-10-21 13:26:59 -0700781
782 SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth,
783 (SkScalar) finalScaledHeight);
msarett9e9444c2016-02-03 12:39:10 -0800784 premultiply_if_necessary(bitmap);
msarett34e0ec42016-04-22 16:27:24 -0700785 swap_rb_if_necessary(bitmap, fDstColorType);
msarettfa23a9e2015-10-21 13:26:59 -0700786 canvas->drawBitmapRect(bitmap, rect, rect, nullptr);
msarett3d9d7a72015-10-21 10:27:10 -0700787 return "";
788 }
789 default:
790 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800791 return "Error: Should not be reached.";
msarett3d9d7a72015-10-21 10:27:10 -0700792 }
793}
794
795SkISize AndroidCodecSrc::size() const {
796 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
797 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
798 if (nullptr == codec) {
799 return SkISize::Make(0, 0);
800 }
801 return codec->getSampledDimensions(fSampleSize);
802}
803
804Name AndroidCodecSrc::name() const {
805 // We will replicate the names used by CodecSrc so that images can
806 // be compared in Gold.
807 if (1 == fSampleSize) {
808 return SkOSPath::Basename(fPath.c_str());
809 }
msarett4b0778e2015-11-13 09:59:11 -0800810 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700811}
812
813/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
814
msarett18976312016-03-09 14:20:58 -0800815ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
816 : fPath(path)
817 , fMode(mode)
818 , fDstAlphaType(alphaType)
819 , fIsGpu(isGpu)
820 , fRunSerially(serial_from_path_name(path))
821{}
822
823bool ImageGenSrc::veto(SinkFlags flags) const {
824 if (fIsGpu) {
825 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect;
826 }
827
828 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
829}
830
831Error ImageGenSrc::draw(SkCanvas* canvas) const {
832 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
833 return Error::Nonfatal("Uninteresting to test image generator to 565.");
834 }
835
836 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
837 if (!encoded) {
838 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
839 }
840
msarettfc0b6d12016-03-17 13:50:17 -0700841#if defined(SK_BUILD_FOR_WIN)
842 // Initialize COM in order to test with WIC.
843 SkAutoCoInitialize com;
844 if (!com.succeeded()) {
845 return "Could not initialize COM.";
846 }
847#endif
848
msarett18976312016-03-09 14:20:58 -0800849 SkAutoTDelete<SkImageGenerator> gen(nullptr);
850 switch (fMode) {
851 case kCodec_Mode:
852 gen.reset(SkCodecImageGenerator::NewFromEncodedCodec(encoded));
853 if (!gen) {
854 return "Could not create codec image generator.";
855 }
856 break;
msarettfc0b6d12016-03-17 13:50:17 -0700857 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800858#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
msarett18976312016-03-09 14:20:58 -0800859 gen.reset(SkImageGeneratorCG::NewFromEncodedCG(encoded));
msarettfc0b6d12016-03-17 13:50:17 -0700860#elif defined(SK_BUILD_FOR_WIN)
861 gen.reset(SkImageGeneratorWIC::NewFromEncodedWIC(encoded));
862#endif
863
msarett18976312016-03-09 14:20:58 -0800864 if (!gen) {
msarettfc0b6d12016-03-17 13:50:17 -0700865 return "Could not create platform image generator.";
msarett18976312016-03-09 14:20:58 -0800866 }
867 break;
msarettfc0b6d12016-03-17 13:50:17 -0700868 }
msarett18976312016-03-09 14:20:58 -0800869 default:
870 SkASSERT(false);
871 return "Invalid image generator mode";
872 }
873
874 // Test deferred decoding path on GPU
875 if (fIsGpu) {
reed9ce9d672016-03-17 10:51:11 -0700876 sk_sp<SkImage> image(SkImage::MakeFromGenerator(gen.release(), nullptr));
msarett18976312016-03-09 14:20:58 -0800877 if (!image) {
878 return "Could not create image from codec image generator.";
879 }
880 canvas->drawImage(image, 0, 0);
881 return "";
882 }
mtklein343a63d2016-03-22 11:46:53 -0700883
msarett18976312016-03-09 14:20:58 -0800884 // Test various color and alpha types on CPU
885 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700886
msarett18976312016-03-09 14:20:58 -0800887 if (kGray_8_SkColorType == decodeInfo.colorType() &&
888 kOpaque_SkAlphaType != decodeInfo.alphaType()) {
889 return Error::Nonfatal("Avoid requesting non-opaque kGray8 decodes.");
890 }
mtklein343a63d2016-03-22 11:46:53 -0700891
msarett18976312016-03-09 14:20:58 -0800892 SkAutoTUnref<SkColorTable> colorTable(nullptr);
893 SkPMColor* colorPtr = nullptr;
894 int* colorCountPtr = nullptr;
895 int maxColors = 256;
896 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
897 SkPMColor colors[256];
898 colorTable.reset(new SkColorTable(colors, maxColors));
899 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
900 colorCountPtr = &maxColors;
901 }
902
903 SkBitmap bitmap;
904 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
905 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
906 decodeInfo.width(), decodeInfo.height());
907 }
mtklein343a63d2016-03-22 11:46:53 -0700908
msarett18976312016-03-09 14:20:58 -0800909 if (!gen->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), colorPtr,
910 colorCountPtr))
911 {
912 return SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str());
913 }
914
915 premultiply_if_necessary(bitmap);
916 canvas->drawBitmap(bitmap, 0, 0);
917 return "";
918}
919
920SkISize ImageGenSrc::size() const {
921 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
922 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
923 if (nullptr == codec) {
924 return SkISize::Make(0, 0);
925 }
926 return codec->getInfo().dimensions();
927}
928
929Name ImageGenSrc::name() const {
930 return SkOSPath::Basename(fPath.c_str());
931}
932
933/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
934
msarett69deca82016-04-29 09:38:40 -0700935ColorCodecSrc::ColorCodecSrc(Path path, Mode mode)
936 : fPath(path)
937 , fMode(mode)
938{}
939
940bool ColorCodecSrc::veto(SinkFlags flags) const {
941 // Test to direct raster backends (8888 and 565).
942 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
943}
944
945Error ColorCodecSrc::draw(SkCanvas* canvas) const {
946 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
947 return Error::Nonfatal("No need to test color correction to 565 backend.");
948 }
949
950 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
951 if (!encoded) {
952 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
953 }
954
955 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
956 if (nullptr == codec.get()) {
957 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
958 }
959
960 SkImageInfo decodeInfo = codec->getInfo().makeColorType(kN32_SkColorType);
961 SkBitmap bitmap;
962 if (!bitmap.tryAllocPixels(decodeInfo)) {
963 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
964 decodeInfo.width(), decodeInfo.height());
965 }
966
967 switch (fMode) {
968 case kBaseline_Mode:
969 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes())) {
970 case SkCodec::kSuccess:
971 break;
972 default:
973 // Everything else is considered a failure.
974 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
975 }
976 canvas->drawBitmap(bitmap, 0, 0);
977 break;
978 default:
979 SkASSERT(false);
980 return "Invalid fMode";
981 }
982 return "";
983}
984
985SkISize ColorCodecSrc::size() const {
986 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
987 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
988 if (nullptr == codec) {
989 return SkISize::Make(0, 0);
990 }
991 return SkISize::Make(codec->getInfo().width(), codec->getInfo().height());
992}
993
994Name ColorCodecSrc::name() const {
995 return SkOSPath::Basename(fPath.c_str());
996}
997
998/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
999
mtkleinf4ba3212015-01-28 15:32:24 -08001000static const SkRect kSKPViewport = {0,0, 1000,1000};
1001
mtklein8d17a132015-01-30 11:42:31 -08001002SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -08001003
1004Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -08001005 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -08001006 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -08001007 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1008 }
reedca2622b2016-03-18 07:25:55 -07001009 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream));
mtklein75d98fd2015-01-18 07:05:01 -08001010 if (!pic) {
1011 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
1012 }
halcanary96fcdcc2015-08-27 07:41:13 -07001013 stream.reset((SkStream*)nullptr); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -07001014
mtkleinf4ba3212015-01-28 15:32:24 -08001015 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -08001016 canvas->drawPicture(pic);
1017 return "";
1018}
1019
1020SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -07001021 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
1022 if (!stream) {
1023 return SkISize::Make(0,0);
1024 }
1025 SkPictInfo info;
1026 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
1027 return SkISize::Make(0,0);
1028 }
1029 SkRect viewport = kSKPViewport;
1030 if (!viewport.intersect(info.fCullRect)) {
1031 return SkISize::Make(0,0);
1032 }
1033 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001034}
1035
1036Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1037
1038/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1039
mtkleinad66f9b2015-02-13 15:11:10 -08001040Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
1041 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
1042 return src.draw(canvas);
1043}
1044
1045/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1046
mtkleinb9eb4ac2015-02-02 18:26:03 -08001047DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
1048
bsalomon85b4b532016-04-05 11:06:27 -07001049GPUSink::GPUSink(GrContextFactory::ContextType ct,
1050 GrContextFactory::ContextOptions options,
mtklein82d28432015-01-15 12:46:02 -08001051 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -07001052 bool diText,
brianosmand93c1202016-03-10 07:49:08 -08001053 SkColorType colorType,
1054 SkColorProfileType profileType,
mtklein82d28432015-01-15 12:46:02 -08001055 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -08001056 : fContextType(ct)
kkinnunen5219fd92015-12-10 06:28:13 -08001057 , fContextOptions(options)
mtklein748ca3b2015-01-15 10:56:12 -08001058 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -07001059 , fUseDIText(diText)
brianosmand93c1202016-03-10 07:49:08 -08001060 , fColorType(colorType)
1061 , fProfileType(profileType)
mtklein82d28432015-01-15 12:46:02 -08001062 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -08001063
joshualitt5f5a8d72015-02-25 14:09:45 -08001064void PreAbandonGpuContextErrorHandler(SkError, void*) {}
1065
bsalomon648c6962015-10-23 09:06:59 -07001066DEFINE_bool(imm, false, "Run gpu configs in immediate mode.");
bsalomon69cfe952015-11-30 13:27:47 -08001067DEFINE_bool(batchClip, false, "Clip each GrBatch to its device bounds for testing.");
bsalomon6dea83f2015-12-03 12:58:06 -08001068DEFINE_bool(batchBounds, false, "Draw a wireframe bounds of each GrBatch.");
bsalomon489147c2015-12-14 12:13:09 -08001069DEFINE_int32(batchLookback, -1, "Maximum GrBatch lookback for combining, negative means default.");
bsalomonaecc0182016-03-07 11:50:44 -08001070DEFINE_int32(batchLookahead, -1, "Maximum GrBatch lookahead for combining, negative means "
1071 "default.");
bsalomon648c6962015-10-23 09:06:59 -07001072
mtkleinb9eb4ac2015-02-02 18:26:03 -08001073Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
kkinnunen5219fd92015-12-10 06:28:13 -08001074 GrContextOptions grOptions;
bsalomon489147c2015-12-14 12:13:09 -08001075 grOptions.fImmediateMode = FLAGS_imm;
1076 grOptions.fClipBatchToBounds = FLAGS_batchClip;
1077 grOptions.fDrawBatchBounds = FLAGS_batchBounds;
1078 grOptions.fMaxBatchLookback = FLAGS_batchLookback;
bsalomonaecc0182016-03-07 11:50:44 -08001079 grOptions.fMaxBatchLookahead = FLAGS_batchLookahead;
kkinnunen64492c42015-12-08 01:24:40 -08001080
kkinnunen5219fd92015-12-10 06:28:13 -08001081 src.modifyGrContextOptions(&grOptions);
1082
1083 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -08001084 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001085 const SkImageInfo info =
brianosmand93c1202016-03-10 07:49:08 -08001086 SkImageInfo::Make(size.width(), size.height(), fColorType,
1087 kPremul_SkAlphaType, fProfileType);
msarett13a036b2016-02-08 09:10:47 -08001088#if SK_SUPPORT_GPU
1089 const int maxDimension = factory.getContextInfo(fContextType, fContextOptions).
1090 fGrContext->caps()->maxTextureSize();
1091 if (maxDimension < SkTMax(size.width(), size.height())) {
1092 return Error::Nonfatal("Src too large to create a texture.\n");
1093 }
1094#endif
1095
reede8f30622016-03-23 18:59:25 -07001096 auto surface(
kkinnunen3e980c32015-12-23 01:33:00 -08001097 NewGpuSurface(&factory, fContextType, fContextOptions, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -08001098 if (!surface) {
1099 return "Could not create a surface.";
1100 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001101 if (FLAGS_preAbandonGpuContext) {
halcanary96fcdcc2015-08-27 07:41:13 -07001102 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr);
joshualitt5f5a8d72015-02-25 14:09:45 -08001103 factory.abandonContexts();
1104 }
mtklein748ca3b2015-01-15 10:56:12 -08001105 SkCanvas* canvas = surface->getCanvas();
1106 Error err = src.draw(canvas);
1107 if (!err.isEmpty()) {
1108 return err;
1109 }
1110 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001111 if (FLAGS_gpuStats) {
1112 canvas->getGrContext()->dumpCacheStats(log);
1113 canvas->getGrContext()->dumpGpuStats(log);
1114 }
mtklein748ca3b2015-01-15 10:56:12 -08001115 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -08001116 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001117 if (FLAGS_abandonGpuContext) {
1118 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001119 } else if (FLAGS_releaseAndAbandonGpuContext) {
1120 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001121 }
mtklein748ca3b2015-01-15 10:56:12 -08001122 return "";
1123}
1124
1125/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1126
halcanary47ef4d52015-03-03 09:13:09 -08001127static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001128 if (src.size().isEmpty()) {
1129 return "Source has empty dimensions";
1130 }
halcanary47ef4d52015-03-03 09:13:09 -08001131 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -08001132 int width = src.size().width(),
1133 height = src.size().height();
1134
halcanary7e798182015-04-14 14:06:18 -07001135 if (FLAGS_multiPage) {
halcanary4ba051c2016-03-10 10:31:53 -08001136 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
halcanary7e798182015-04-14 14:06:18 -07001137 const int kLetterWidth = 612, // 8.5 * 72
1138 kLetterHeight = 792; // 11 * 72
1139 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
1140 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -08001141
halcanary7e798182015-04-14 14:06:18 -07001142 int xPages = ((width - 1) / kLetterWidth) + 1;
1143 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -08001144
halcanary7e798182015-04-14 14:06:18 -07001145 for (int y = 0; y < yPages; ++y) {
1146 for (int x = 0; x < xPages; ++x) {
1147 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
1148 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
1149 SkCanvas* canvas =
1150 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
1151 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001152 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001153 }
1154 canvas->clipRect(letter);
1155 canvas->translate(-letter.width() * x, -letter.height() * y);
1156 Error err = src.draw(canvas);
1157 if (!err.isEmpty()) {
1158 return err;
1159 }
1160 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -07001161 }
halcanaryfd4a9932015-01-28 11:45:58 -08001162 }
halcanary7e798182015-04-14 14:06:18 -07001163 } else {
1164 SkCanvas* canvas =
1165 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1166 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001167 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001168 }
1169 Error err = src.draw(canvas);
1170 if (!err.isEmpty()) {
1171 return err;
1172 }
1173 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001174 }
halcanary7e798182015-04-14 14:06:18 -07001175 if (!doc->close()) {
1176 return "SkDocument::close() returned false";
1177 }
halcanaryfd4a9932015-01-28 11:45:58 -08001178 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001179 return "";
1180}
1181
halcanary47ef4d52015-03-03 09:13:09 -08001182Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary4b656662016-04-27 07:45:18 -07001183 SkDocument::PDFMetadata metadata;
1184 metadata.fTitle = src.name();
1185 metadata.fSubject = "rendering correctness test";
1186 metadata.fCreator = "Skia/DM";
1187 sk_sp<SkDocument> doc = SkDocument::MakePDF(dst, SK_ScalarDefaultRasterDPI,
1188 metadata, nullptr, fPDFA);
halcanary47ef4d52015-03-03 09:13:09 -08001189 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001190 return "SkDocument::MakePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001191 }
1192 return draw_skdocument(src, doc.get(), dst);
1193}
1194
1195/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1196
1197XPSSink::XPSSink() {}
1198
1199Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary4b656662016-04-27 07:45:18 -07001200 sk_sp<SkDocument> doc(SkDocument::MakeXPS(dst));
halcanary47ef4d52015-03-03 09:13:09 -08001201 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001202 return "SkDocument::MakeXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001203 }
1204 return draw_skdocument(src, doc.get(), dst);
1205}
mtklein748ca3b2015-01-15 10:56:12 -08001206/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1207
mtklein9c3f17d2015-01-28 11:35:18 -08001208SKPSink::SKPSink() {}
1209
mtkleinb9eb4ac2015-02-02 18:26:03 -08001210Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001211 SkSize size;
1212 size = src.size();
1213 SkPictureRecorder recorder;
1214 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1215 if (!err.isEmpty()) {
1216 return err;
1217 }
reedca2622b2016-03-18 07:25:55 -07001218 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001219 return "";
1220}
1221
1222/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1223
mtklein8a4527e2015-01-31 20:00:58 -08001224SVGSink::SVGSink() {}
1225
mtkleinb9eb4ac2015-02-02 18:26:03 -08001226Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary385fe4d2015-08-26 13:07:48 -07001227 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
fmalita2aafe6f2015-02-06 12:51:10 -08001228 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
1229 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
1230 xmlWriter));
1231 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -08001232}
1233
1234/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1235
mtklein27c3fdd2016-02-26 14:43:21 -08001236RasterSink::RasterSink(SkColorType colorType, SkColorProfileType profileType)
1237 : fColorType(colorType)
1238 , fProfileType(profileType) {}
mtklein748ca3b2015-01-15 10:56:12 -08001239
mtkleinb9eb4ac2015-02-02 18:26:03 -08001240Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001241 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001242 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1243 SkAlphaType alphaType = kPremul_SkAlphaType;
1244 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1245
mtkleinc8be09a2016-01-04 18:56:57 -08001246 SkMallocPixelRef::ZeroedPRFactory factory;
mtklein27c3fdd2016-02-26 14:43:21 -08001247 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(),
1248 fColorType, alphaType, fProfileType),
mtkleinc8be09a2016-01-04 18:56:57 -08001249 &factory,
1250 nullptr/*colortable*/);
mtklein748ca3b2015-01-15 10:56:12 -08001251 SkCanvas canvas(*dst);
1252 return src.draw(&canvas);
1253}
1254
1255/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1256
mtkleina16e69e2015-05-05 11:38:45 -07001257// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001258// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001259// Several examples below.
1260
mtkleincbf89782016-02-19 14:27:14 -08001261template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001262static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001263 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001264 class ProxySrc : public Src {
1265 public:
mtkleincbf89782016-02-19 14:27:14 -08001266 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001267 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001268 Name name() const override { return "ProxySrc"; }
1269 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001270 private:
mtkleincbf89782016-02-19 14:27:14 -08001271 SkISize fSize;
1272 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001273 };
msarett62d3b102015-12-10 15:14:27 -08001274 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001275}
1276
1277/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1278
mtklein4a34ecb2016-01-08 10:19:35 -08001279DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1280
1281// Is *bitmap identical to what you get drawing src into sink?
1282static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1283 // We can only check raster outputs.
1284 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1285 if (FLAGS_check && bitmap) {
1286 SkBitmap reference;
1287 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001288 SkDynamicMemoryWStream wStream;
1289 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001290 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1291 SkASSERT(err.isEmpty());
1292 if (!err.isEmpty()) {
1293 return err;
1294 }
1295 // The dimensions are a property of the Src only, and so should be identical.
1296 SkASSERT(reference.getSize() == bitmap->getSize());
1297 if (reference.getSize() != bitmap->getSize()) {
1298 return "Dimensions don't match reference";
1299 }
1300 // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy.
1301 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1302 return "Pixels don't match reference";
1303 }
1304 }
1305 return "";
1306}
1307
1308/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1309
mtkleind603b222015-02-17 11:13:33 -08001310static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1311 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1312 matrix->mapRect(&bounds);
1313 matrix->postTranslate(-bounds.x(), -bounds.y());
1314 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
1315}
1316
msarett62d3b102015-12-10 15:14:27 -08001317ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001318
mtkleinb9eb4ac2015-02-02 18:26:03 -08001319Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001320 SkMatrix matrix = fMatrix;
1321 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
msarett62d3b102015-12-10 15:14:27 -08001322 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001323 canvas->concat(matrix);
1324 return src.draw(canvas);
1325 });
mtklein748ca3b2015-01-15 10:56:12 -08001326}
1327
mtkleind603b222015-02-17 11:13:33 -08001328// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1329// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001330ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001331
1332Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1333 Error err = fSink->draw(src, bitmap, stream, log);
1334 if (!err.isEmpty()) {
1335 return err;
1336 }
1337
1338 SkMatrix inverse;
1339 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1340 return "Cannot upright --matrix.";
1341 }
1342 SkMatrix upright = SkMatrix::I();
1343 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1344 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1345 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1346 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1347
1348 SkBitmap uprighted;
1349 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1350 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1351
1352 SkCanvas canvas(uprighted);
1353 canvas.concat(upright);
1354 SkPaint paint;
1355 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1356 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1357
1358 *bitmap = uprighted;
1359 bitmap->lockPixels();
1360 return "";
1361}
1362
mtklein748ca3b2015-01-15 10:56:12 -08001363/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1364
mtkleina16e69e2015-05-05 11:38:45 -07001365Error ViaSerialization::draw(
1366 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001367 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001368 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001369 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001370 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1371 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001372 if (!err.isEmpty()) {
1373 return err;
1374 }
reedca2622b2016-03-18 07:25:55 -07001375 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001376
1377 // Serialize it and then deserialize it.
1378 SkDynamicMemoryWStream wStream;
1379 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -08001380 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -07001381 sk_sp<SkPicture> deserialized(SkPicture::MakeFromStream(rStream));
mtklein748ca3b2015-01-15 10:56:12 -08001382
msarett62d3b102015-12-10 15:14:27 -08001383 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001384 canvas->drawPicture(deserialized);
mtklein4a34ecb2016-01-08 10:19:35 -08001385 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001386 });
mtklein748ca3b2015-01-15 10:56:12 -08001387}
1388
1389/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1390
msarett62d3b102015-12-10 15:14:27 -08001391ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1392 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001393 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001394 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001395 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001396
mtkleinb9eb4ac2015-02-02 18:26:03 -08001397Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001398 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001399 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001400 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1401 SkIntToScalar(size.height()),
1402 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001403 if (!err.isEmpty()) {
1404 return err;
1405 }
reedca2622b2016-03-18 07:25:55 -07001406 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001407
msarett62d3b102015-12-10 15:14:27 -08001408 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001409 const int xTiles = (size.width() + fW - 1) / fW,
1410 yTiles = (size.height() + fH - 1) / fH;
1411 SkMultiPictureDraw mpd(xTiles*yTiles);
reede8f30622016-03-23 18:59:25 -07001412 SkTArray<sk_sp<SkSurface>> surfaces;
1413// surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001414
mtkleina16e69e2015-05-05 11:38:45 -07001415 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1416 for (int j = 0; j < yTiles; j++) {
1417 for (int i = 0; i < xTiles; i++) {
1418 // This lets our ultimate Sink determine the best kind of surface.
1419 // E.g., if it's a GpuSink, the surfaces and images are textures.
reede8f30622016-03-23 18:59:25 -07001420 auto s = canvas->makeSurface(info);
mtkleina16e69e2015-05-05 11:38:45 -07001421 if (!s) {
reede8f30622016-03-23 18:59:25 -07001422 s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001423 }
reede8f30622016-03-23 18:59:25 -07001424 surfaces.push_back(s);
mtkleina16e69e2015-05-05 11:38:45 -07001425 SkCanvas* c = s->getCanvas();
1426 c->translate(SkIntToScalar(-i * fW),
1427 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
reedca2622b2016-03-18 07:25:55 -07001428 mpd.add(c, pic.get());
mtklein748ca3b2015-01-15 10:56:12 -08001429 }
mtklein748ca3b2015-01-15 10:56:12 -08001430 }
mtkleina16e69e2015-05-05 11:38:45 -07001431 mpd.draw();
1432 for (int j = 0; j < yTiles; j++) {
1433 for (int i = 0; i < xTiles; i++) {
reed9ce9d672016-03-17 10:51:11 -07001434 sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot());
mtkleina16e69e2015-05-05 11:38:45 -07001435 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1436 }
1437 }
mtkleina16e69e2015-05-05 11:38:45 -07001438 return "";
1439 });
mtklein748ca3b2015-01-15 10:56:12 -08001440}
1441
mtkleinb7e8d692015-04-07 08:30:32 -07001442/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1443
mtklein4a34ecb2016-01-08 10:19:35 -08001444Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1445 auto size = src.size();
1446 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1447 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001448 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08001449 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1450 SkIntToScalar(size.height())));
1451 if (!err.isEmpty()) {
1452 return err;
1453 }
reedca2622b2016-03-18 07:25:55 -07001454 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08001455 canvas->drawPicture(pic);
1456 return check_against_reference(bitmap, src, fSink);
1457 });
1458}
1459
1460/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1461
mtkleinb7e8d692015-04-07 08:30:32 -07001462// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1463// This tests that any shortcuts we may take while recording that second picture are legal.
1464Error ViaSecondPicture::draw(
1465 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001466 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001467 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001468 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001469 sk_sp<SkPicture> pic;
mtkleina16e69e2015-05-05 11:38:45 -07001470 for (int i = 0; i < 2; i++) {
1471 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1472 SkIntToScalar(size.height())));
1473 if (!err.isEmpty()) {
1474 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001475 }
reedca2622b2016-03-18 07:25:55 -07001476 pic = recorder.finishRecordingAsPicture();
mtkleinb7e8d692015-04-07 08:30:32 -07001477 }
mtkleina16e69e2015-05-05 11:38:45 -07001478 canvas->drawPicture(pic);
mtklein4a34ecb2016-01-08 10:19:35 -08001479 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001480 });
mtkleinb7e8d692015-04-07 08:30:32 -07001481}
1482
mtkleind31c13d2015-05-05 12:59:56 -07001483/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1484
mtklein6fbf4b32015-05-06 11:35:40 -07001485// Draw the Src twice. This can help exercise caching.
1486Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001487 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001488 for (int i = 0; i < 2; i++) {
1489 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1490 canvas->clear(SK_ColorTRANSPARENT);
1491 Error err = src.draw(canvas);
1492 if (err.isEmpty()) {
1493 return err;
1494 }
1495 }
mtklein4a34ecb2016-01-08 10:19:35 -08001496 return check_against_reference(bitmap, src, fSink);
mtklein6fbf4b32015-05-06 11:35:40 -07001497 });
1498}
1499
1500/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1501
mtkleind31c13d2015-05-05 12:59:56 -07001502// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1503// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1504// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1505struct DrawsAsSingletonPictures {
1506 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001507 const SkDrawableList& fDrawables;
mtklein11064df2016-04-06 15:01:57 -07001508 SkRect fBounds;
mtkleind31c13d2015-05-05 12:59:56 -07001509
mtkleind31c13d2015-05-05 12:59:56 -07001510 template <typename T>
1511 void draw(const T& op, SkCanvas* canvas) {
1512 // We must pass SkMatrix::I() as our initial matrix.
1513 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1514 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001515 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1516 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001517 }
1518
mtklein449d9b72015-09-28 10:33:02 -07001519 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001520 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001521 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001522 SkPictureRecorder rec;
mtklein11064df2016-04-06 15:01:57 -07001523 this->draw(op, rec.beginRecording(fBounds));
reedca2622b2016-03-18 07:25:55 -07001524 sk_sp<SkPicture> pic(rec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07001525 fCanvas->drawPicture(pic);
1526 }
1527
mtklein449d9b72015-09-28 10:33:02 -07001528 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001529 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001530 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1531 this->draw(op, fCanvas);
1532 }
mtkleind31c13d2015-05-05 12:59:56 -07001533};
1534
mtkleind31c13d2015-05-05 12:59:56 -07001535// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1536// Then play back that macro picture into our wrapped sink.
1537Error ViaSingletonPictures::draw(
1538 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1539 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001540 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07001541 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1542 SkRecord skr;
1543 SkRecorder recorder(&skr, size.width(), size.height());
1544 Error err = src.draw(&recorder);
1545 if (!err.isEmpty()) {
1546 return err;
1547 }
1548
1549 // Record our macro-picture, with each draw op as its own sub-picture.
1550 SkPictureRecorder macroRec;
1551 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1552 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001553
1554 SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
1555 const SkDrawableList empty;
1556
1557 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1558 macroCanvas,
1559 drawables ? *drawables : empty,
mtklein11064df2016-04-06 15:01:57 -07001560 SkRect::MakeWH((SkScalar)size.width(), (SkScalar)size.height()),
mtkleind2baa902015-07-07 09:43:28 -07001561 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001562 for (int i = 0; i < skr.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -07001563 skr.visit(i, drawsAsSingletonPictures);
mtkleind31c13d2015-05-05 12:59:56 -07001564 }
reedca2622b2016-03-18 07:25:55 -07001565 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07001566
1567 canvas->drawPicture(macroPic);
mtklein4a34ecb2016-01-08 10:19:35 -08001568 return check_against_reference(bitmap, src, fSink);
mtkleind31c13d2015-05-05 12:59:56 -07001569 });
1570}
1571
mtklein748ca3b2015-01-15 10:56:12 -08001572} // namespace DM