blob: bd881e8381739a1c723f8014ed0859fa0a747473 [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"
mtkleinc8be09a2016-01-04 18:56:57 -080017#include "SkMallocPixelRef.h"
mtklein748ca3b2015-01-15 10:56:12 -080018#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080019#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080020#include "SkOSFile.h"
msarett9e9444c2016-02-03 12:39:10 -080021#include "SkOpts.h"
mtkleinffa901a2015-03-16 10:38:07 -070022#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080023#include "SkPictureRecorder.h"
24#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070025#include "SkRecordDraw.h"
26#include "SkRecorder.h"
mtklein2e2ea382015-10-16 10:29:41 -070027#include "SkRemote.h"
fmalita2aafe6f2015-02-06 12:51:10 -080028#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080029#include "SkStream.h"
mtklein449d9b72015-09-28 10:33:02 -070030#include "SkTLogic.h"
fmalita2aafe6f2015-02-06 12:51:10 -080031#include "SkXMLWriter.h"
msarette6dd0042015-10-09 11:07:34 -070032#include "SkSwizzler.h"
mtklein64593522015-11-12 10:41:05 -080033#include <functional>
mtklein748ca3b2015-01-15 10:56:12 -080034
halcanary7a76f9c2016-02-03 11:53:18 -080035#ifdef SK_MOJO
36 #include "SkMojo.mojom.h"
37#endif
38
halcanary7e798182015-04-14 14:06:18 -070039DEFINE_bool(multiPage, false, "For document-type backends, render the source"
40 " into multiple pages");
scroggo3ac66e92016-02-08 15:09:48 -080041DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070042
mtklein748ca3b2015-01-15 10:56:12 -080043namespace DM {
44
mtklein748ca3b2015-01-15 10:56:12 -080045GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
46
47Error GMSrc::draw(SkCanvas* canvas) const {
halcanary96fcdcc2015-08-27 07:41:13 -070048 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080049 canvas->concat(gm->getInitialTransform());
50 gm->draw(canvas);
51 return "";
52}
53
54SkISize GMSrc::size() const {
halcanary96fcdcc2015-08-27 07:41:13 -070055 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080056 return gm->getISize();
57}
58
59Name GMSrc::name() const {
halcanary96fcdcc2015-08-27 07:41:13 -070060 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080061 return gm->getName();
62}
63
bsalomon4ee6bd82015-05-27 13:23:23 -070064void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
halcanary96fcdcc2015-08-27 07:41:13 -070065 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070066 gm->modifyGrContextOptions(options);
67}
68
mtklein748ca3b2015-01-15 10:56:12 -080069/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
70
msarett5cb48852015-11-06 08:56:32 -080071BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode,
msaretta5783ae2015-09-08 15:35:32 -070072 CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
73 : fPath(path)
74 , fStrategy(strategy)
75 , fMode(mode)
76 , fDstColorType(dstColorType)
77 , fSampleSize(sampleSize)
78{}
79
80bool BRDSrc::veto(SinkFlags flags) const {
81 // No need to test to non-raster or indirect backends.
82 return flags.type != SinkFlags::kRaster
83 || flags.approach != SinkFlags::kDirect;
84}
85
msarett5cb48852015-11-06 08:56:32 -080086static SkBitmapRegionDecoder* create_brd(Path path,
87 SkBitmapRegionDecoder::Strategy strategy) {
msaretta5783ae2015-09-08 15:35:32 -070088 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
89 if (!encoded) {
90 return NULL;
91 }
msarett5cb48852015-11-06 08:56:32 -080092 return SkBitmapRegionDecoder::Create(encoded, strategy);
msaretta5783ae2015-09-08 15:35:32 -070093}
94
95Error BRDSrc::draw(SkCanvas* canvas) const {
96 SkColorType colorType = canvas->imageInfo().colorType();
97 if (kRGB_565_SkColorType == colorType &&
98 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
99 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
100 }
101 switch (fDstColorType) {
102 case CodecSrc::kGetFromCanvas_DstColorType:
103 break;
104 case CodecSrc::kIndex8_Always_DstColorType:
105 colorType = kIndex_8_SkColorType;
106 break;
107 case CodecSrc::kGrayscale_Always_DstColorType:
108 colorType = kGray_8_SkColorType;
109 break;
110 }
111
msarett5cb48852015-11-06 08:56:32 -0800112 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700113 if (nullptr == brd.get()) {
114 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
115 }
116
msarett04965c62015-10-12 10:24:38 -0700117 if (!brd->conversionSupported(colorType)) {
mtklein9b439152015-12-09 13:02:26 -0800118 return Error::Nonfatal("Cannot convert to color type.");
msarett04965c62015-10-12 10:24:38 -0700119 }
120
msaretta5783ae2015-09-08 15:35:32 -0700121 const uint32_t width = brd->width();
122 const uint32_t height = brd->height();
123 // Visually inspecting very small output images is not necessary.
124 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
125 return Error::Nonfatal("Scaling very small images is uninteresting.");
126 }
127 switch (fMode) {
128 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700129 SkBitmap bitmap;
130 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
131 fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800132 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700133 }
134 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800135 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700136 }
msarett35e5d1b2015-10-27 12:50:25 -0700137 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700138 return "";
139 }
140 case kDivisor_Mode: {
141 const uint32_t divisor = 2;
142 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800143 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700144 }
145
146 // Use a border to test subsets that extend outside the image.
147 // We will not allow the border to be larger than the image dimensions. Allowing
148 // these large borders causes off by one errors that indicate a problem with the
149 // test suite, not a problem with the implementation.
150 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
151 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
152 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
153
154 // We may need to clear the canvas to avoid uninitialized memory.
155 // Assume we are scaling a 780x780 image with sampleSize = 8.
156 // The output image should be 97x97.
157 // Each subset will be 390x390.
158 // Each scaled subset be 48x48.
159 // Four scaled subsets will only fill a 96x96 image.
160 // The bottom row and last column will not be touched.
161 // This is an unfortunate result of our rounding rules when scaling.
162 // Maybe we need to consider testing scaled subsets without trying to
163 // combine them to match the full scaled image? Or maybe this is the
164 // best we can do?
165 canvas->clear(0);
166
167 for (uint32_t x = 0; x < divisor; x++) {
168 for (uint32_t y = 0; y < divisor; y++) {
169 // Calculate the subset dimensions
170 uint32_t subsetWidth = width / divisor;
171 uint32_t subsetHeight = height / divisor;
172 const int left = x * subsetWidth;
173 const int top = y * subsetHeight;
174
175 // Increase the size of the last subset in each row or column, when the
176 // divisor does not divide evenly into the image dimensions
177 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
178 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
179
180 // Increase the size of the subset in order to have a border on each side
181 const int decodeLeft = left - unscaledBorder;
182 const int decodeTop = top - unscaledBorder;
183 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
184 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700185 SkBitmap bitmap;
186 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
187 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800188 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700189 }
190 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800191 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700192 }
193
msarett35e5d1b2015-10-27 12:50:25 -0700194 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700195 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
196 (SkScalar) (subsetWidth / fSampleSize),
197 (SkScalar) (subsetHeight / fSampleSize)),
198 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
199 (SkScalar) (top / fSampleSize),
200 (SkScalar) (subsetWidth / fSampleSize),
201 (SkScalar) (subsetHeight / fSampleSize)),
202 nullptr);
203 }
204 }
205 return "";
206 }
207 default:
208 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800209 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700210 }
211}
212
213SkISize BRDSrc::size() const {
msarett5cb48852015-11-06 08:56:32 -0800214 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700215 if (brd) {
216 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
217 SkTMax(1, brd->height() / (int) fSampleSize));
218 }
219 return SkISize::Make(0, 0);
220}
221
222static SkString get_scaled_name(const Path& path, float scale) {
223 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
224}
225
226Name BRDSrc::name() const {
227 // We will replicate the names used by CodecSrc so that images can
228 // be compared in Gold.
229 if (1 == fSampleSize) {
230 return SkOSPath::Basename(fPath.c_str());
231 }
msarett4b0778e2015-11-13 09:59:11 -0800232 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700233}
234
235/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
236
scroggo3ac66e92016-02-08 15:09:48 -0800237static bool serial_from_path_name(const SkString& path) {
238 if (!FLAGS_RAW_threading) {
239 static const char* const exts[] = {
240 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
241 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
242 };
243 const char* actualExt = strrchr(path.c_str(), '.');
244 if (actualExt) {
245 actualExt++;
246 for (auto* ext : exts) {
247 if (0 == strcmp(ext, actualExt)) {
248 return true;
249 }
250 }
251 }
252 }
253 return false;
254}
255
scroggoc5560be2016-02-03 09:42:42 -0800256CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
257 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700258 : fPath(path)
259 , fMode(mode)
260 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800261 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700262 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800263 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700264{}
mtklein748ca3b2015-01-15 10:56:12 -0800265
mtklein99cab4e2015-07-31 06:43:04 -0700266bool CodecSrc::veto(SinkFlags flags) const {
msarettb714fb02016-01-22 14:46:42 -0800267 // Test CodecImageGenerator on 8888, 565, and gpu
268 if (kGen_Mode == fMode) {
msarett36c070d2016-02-10 06:48:21 -0800269 // For image generator, we want to test kDirect approaches for kRaster and kGPU,
270 // while skipping everything else.
271 return (flags.type != SinkFlags::kRaster && flags.type != SinkFlags::kGPU) ||
msaretta23659f2016-02-09 12:39:17 -0800272 flags.approach != SinkFlags::kDirect;
msarettb714fb02016-01-22 14:46:42 -0800273 }
274
275 // Test all other modes to direct raster backends (8888 and 565).
276 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700277}
scroggo9b77ddd2015-03-19 06:03:39 -0700278
msarett9e9444c2016-02-03 12:39:10 -0800279// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
280// This allows us to still test unpremultiplied decodes.
281void premultiply_if_necessary(SkBitmap& bitmap) {
282 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
283 return;
284 }
285
286 switch (bitmap.colorType()) {
287 case kN32_SkColorType:
288 for (int y = 0; y < bitmap.height(); y++) {
289 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
290 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
291 }
292 break;
293 case kIndex_8_SkColorType: {
294 SkColorTable* colorTable = bitmap.getColorTable();
295 SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
296 SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count());
297 break;
298 }
299 default:
300 // No need to premultiply kGray or k565 outputs.
301 break;
302 }
msarette1daa482016-02-03 15:31:18 -0800303
304 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
305 // bitmap as kPremul.
306 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800307}
308
scroggoc5560be2016-02-03 09:42:42 -0800309bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
310 CodecSrc::DstColorType dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700311 switch (dstColorType) {
312 case CodecSrc::kIndex8_Always_DstColorType:
313 if (kRGB_565_SkColorType == canvasColorType) {
314 return false;
315 }
scroggoc5560be2016-02-03 09:42:42 -0800316 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700317 break;
318 case CodecSrc::kGrayscale_Always_DstColorType:
msarett55f7bdd2016-02-16 13:24:54 -0800319 if (kRGB_565_SkColorType == canvasColorType ||
320 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
msarett3d9d7a72015-10-21 10:27:10 -0700321 return false;
322 }
scroggoc5560be2016-02-03 09:42:42 -0800323 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700324 break;
325 default:
msarett55f7bdd2016-02-16 13:24:54 -0800326 if (kRGB_565_SkColorType == canvasColorType &&
327 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
328 return false;
329 }
scroggoc5560be2016-02-03 09:42:42 -0800330 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700331 break;
332 }
333
msarett3d9d7a72015-10-21 10:27:10 -0700334 return true;
335}
336
msarettb714fb02016-01-22 14:46:42 -0800337Error test_gen(SkCanvas* canvas, SkData* data) {
msarett01813e82016-01-25 10:51:29 -0800338 SkAutoTDelete<SkImageGenerator> gen = SkCodecImageGenerator::NewFromEncodedCodec(data);
msarettb714fb02016-01-22 14:46:42 -0800339 if (!gen) {
340 return "Could not create image generator.";
341 }
342
343 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
344 // Currently, we will avoid creating a CodecSrc for this case (see DM.cpp).
345 SkASSERT(kGray_8_SkColorType != gen->getInfo().colorType());
346
msarettd03b7ae2016-01-25 08:20:55 -0800347 if (kOpaque_SkAlphaType != gen->getInfo().alphaType() &&
348 kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
349 return Error::Nonfatal("Skip testing non-opaque images to 565.");
350 }
351
msarett01813e82016-01-25 10:51:29 -0800352 SkAutoTDelete<SkImage> image(SkImage::NewFromGenerator(gen.detach(), nullptr));
msarettb714fb02016-01-22 14:46:42 -0800353 if (!image) {
354 return "Could not create image from codec image generator.";
355 }
356
357 canvas->drawImage(image, 0, 0);
358 return "";
359}
360
mtkleine0effd62015-07-29 06:37:28 -0700361Error CodecSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -0800362 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800363 if (!encoded) {
364 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
365 }
msarettb714fb02016-01-22 14:46:42 -0800366
367 // The CodecImageGenerator test does not share much code with the other tests,
368 // so we will handle it in its own function.
369 if (kGen_Mode == fMode) {
370 return test_gen(canvas, encoded);
371 }
372
msarett3d9d7a72015-10-21 10:27:10 -0700373 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700374 if (nullptr == codec.get()) {
375 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700376 }
377
scroggoc5560be2016-02-03 09:42:42 -0800378 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
379 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700380 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
msarett438b2ad2015-04-09 12:43:10 -0700381 }
382
msarett0a242972015-06-11 14:27:27 -0700383 // Try to scale the image if it is desired
384 SkISize size = codec->getScaledDimensions(fScale);
385 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
386 return Error::Nonfatal("Test without scaling is uninteresting.");
387 }
msarettb32758a2015-08-18 13:22:46 -0700388
389 // Visually inspecting very small output images is not necessary. We will
390 // cover these cases in unit testing.
391 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
392 return Error::Nonfatal("Scaling very small images is uninteresting.");
393 }
msarett0a242972015-06-11 14:27:27 -0700394 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
395
msarett438b2ad2015-04-09 12:43:10 -0700396 // Construct a color table for the decode if necessary
halcanary96fcdcc2015-08-27 07:41:13 -0700397 SkAutoTUnref<SkColorTable> colorTable(nullptr);
398 SkPMColor* colorPtr = nullptr;
399 int* colorCountPtr = nullptr;
msarett438b2ad2015-04-09 12:43:10 -0700400 int maxColors = 256;
401 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
402 SkPMColor colors[256];
halcanary385fe4d2015-08-26 13:07:48 -0700403 colorTable.reset(new SkColorTable(colors, maxColors));
msarett438b2ad2015-04-09 12:43:10 -0700404 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
405 colorCountPtr = &maxColors;
406 }
407
scroggo9b77ddd2015-03-19 06:03:39 -0700408 SkBitmap bitmap;
msarettbb25b532016-01-13 09:31:39 -0800409 SkPixelRefFactory* factory = nullptr;
410 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
411 SkCodec::Options options;
412 if (kCodecZeroInit_Mode == fMode) {
413 factory = &zeroFactory;
414 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
415 }
416 if (!bitmap.tryAllocPixels(decodeInfo, factory, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800417 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
scroggo9b77ddd2015-03-19 06:03:39 -0700418 decodeInfo.width(), decodeInfo.height());
419 }
420
scroggo9c59ebc2015-03-25 13:48:49 -0700421 switch (fMode) {
msarettbb25b532016-01-13 09:31:39 -0800422 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700423 case kCodec_Mode: {
msarettbb25b532016-01-13 09:31:39 -0800424 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
msarett438b2ad2015-04-09 12:43:10 -0700425 colorPtr, colorCountPtr)) {
scroggoeb602a52015-07-09 08:16:03 -0700426 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700427 // We consider incomplete to be valid, since we should still decode what is
428 // available.
scroggoeb602a52015-07-09 08:16:03 -0700429 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700430 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700431 default:
432 // Everything else is considered a failure.
433 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
434 }
msarett9e9444c2016-02-03 12:39:10 -0800435 premultiply_if_necessary(bitmap);
emmaleer97002062015-05-27 12:36:10 -0700436 canvas->drawBitmap(bitmap, 0, 0);
scroggo9c59ebc2015-03-25 13:48:49 -0700437 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700438 }
scroggo9c59ebc2015-03-25 13:48:49 -0700439 case kScanline_Mode: {
scroggo46c57472015-09-30 08:57:13 -0700440 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
441 colorCountPtr)) {
msarett9e707a02015-09-01 14:57:57 -0700442 return Error::Nonfatal("Could not start scanline decoder");
scroggo9c59ebc2015-03-25 13:48:49 -0700443 }
scroggo1c005e42015-08-04 09:24:45 -0700444
msarette6dd0042015-10-09 11:07:34 -0700445 void* dst = bitmap.getAddr(0, 0);
446 size_t rowBytes = bitmap.rowBytes();
447 uint32_t height = decodeInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700448 switch (codec->getScanlineOrder()) {
449 case SkCodec::kTopDown_SkScanlineOrder:
450 case SkCodec::kBottomUp_SkScanlineOrder:
451 case SkCodec::kNone_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700452 // We do not need to check the return value. On an incomplete
453 // image, memory will be filled with a default value.
454 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700455 break;
scroggo46c57472015-09-30 08:57:13 -0700456 case SkCodec::kOutOfOrder_SkScanlineOrder: {
msarett10522ff2015-09-07 08:54:01 -0700457 for (int y = 0; y < decodeInfo.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700458 int dstY = codec->outputScanline(y);
msarett10522ff2015-09-07 08:54:01 -0700459 void* dstPtr = bitmap.getAddr(0, dstY);
msarette6dd0042015-10-09 11:07:34 -0700460 // We complete the loop, even if this call begins to fail
461 // due to an incomplete image. This ensures any uninitialized
462 // memory will be filled with the proper value.
463 codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
msarett10522ff2015-09-07 08:54:01 -0700464 }
465 break;
466 }
467 }
468
msarett9e9444c2016-02-03 12:39:10 -0800469 premultiply_if_necessary(bitmap);
emmaleer97002062015-05-27 12:36:10 -0700470 canvas->drawBitmap(bitmap, 0, 0);
471 break;
472 }
msarett0a242972015-06-11 14:27:27 -0700473 case kStripe_Mode: {
474 const int height = decodeInfo.height();
475 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
476 // does not align with image blocks.
477 const int stripeHeight = 37;
478 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
479
480 // Decode odd stripes
scroggo46c57472015-09-30 08:57:13 -0700481 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
482 colorCountPtr)
483 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
msarett9e707a02015-09-01 14:57:57 -0700484 // 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.
msarett5406d6f2015-08-31 06:55:13 -0700487 return Error::Nonfatal("Could not start top-down scanline decoder");
msarett0a242972015-06-11 14:27:27 -0700488 }
msarette6dd0042015-10-09 11:07:34 -0700489
msarett0a242972015-06-11 14:27:27 -0700490 for (int i = 0; i < numStripes; i += 2) {
491 // Skip a stripe
492 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700493 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700494
495 // Read a stripe
496 const int startY = (i + 1) * stripeHeight;
497 const int linesToRead = SkTMin(stripeHeight, height - startY);
498 if (linesToRead > 0) {
msarette6dd0042015-10-09 11:07:34 -0700499 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700500 }
501 }
502
503 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700504 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
505 colorPtr, colorCountPtr);
scroggo1c005e42015-08-04 09:24:45 -0700506 if (SkCodec::kSuccess != startResult) {
507 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700508 }
509 for (int i = 0; i < numStripes; i += 2) {
510 // Read a stripe
511 const int startY = i * stripeHeight;
512 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarette6dd0042015-10-09 11:07:34 -0700513 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700514
515 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700516 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
517 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700518 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700519 }
520 }
msarett9e9444c2016-02-03 12:39:10 -0800521 premultiply_if_necessary(bitmap);
msarett0a242972015-06-11 14:27:27 -0700522 canvas->drawBitmap(bitmap, 0, 0);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700523 break;
msarett0a242972015-06-11 14:27:27 -0700524 }
msarett91c22b22016-02-22 12:27:46 -0800525 case kCroppedScanline_Mode: {
526 const int width = decodeInfo.width();
527 const int height = decodeInfo.height();
528 // This value is chosen because, as we move across the image, it will sometimes
529 // align with the jpeg block sizes and it will sometimes not. This allows us
530 // to test interestingly different code paths in the implementation.
531 const int tileSize = 36;
532
533 SkCodec::Options opts;
534 SkIRect subset;
535 for (int x = 0; x < width; x += tileSize) {
536 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
537 opts.fSubset = &subset;
538 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &opts,
539 colorPtr, colorCountPtr)) {
540 return "Could not start scanline decoder.";
541 }
542
543 codec->getScanlines(bitmap.getAddr(x, 0), height, bitmap.rowBytes());
544 }
545
546 premultiply_if_necessary(bitmap);
547 canvas->drawBitmap(bitmap, 0, 0);
548 break;
549 }
scroggob636b452015-07-22 07:16:20 -0700550 case kSubset_Mode: {
551 // Arbitrarily choose a divisor.
552 int divisor = 2;
553 // Total width/height of the image.
554 const int W = codec->getInfo().width();
555 const int H = codec->getInfo().height();
556 if (divisor > W || divisor > H) {
557 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
558 "for %s with dimensions (%d x %d)", divisor,
559 fPath.c_str(), W, H));
560 }
561 // subset dimensions
562 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
563 const int w = SkAlign2(W / divisor);
564 const int h = SkAlign2(H / divisor);
565 SkIRect subset;
566 SkCodec::Options opts;
567 opts.fSubset = &subset;
568 SkBitmap subsetBm;
569 // We will reuse pixel memory from bitmap.
570 void* pixels = bitmap.getPixels();
571 // Keep track of left and top (for drawing subsetBm into canvas). We could use
572 // fScale * x and fScale * y, but we want integers such that the next subset will start
573 // where the last one ended. So we'll add decodeInfo.width() and height().
574 int left = 0;
575 for (int x = 0; x < W; x += w) {
576 int top = 0;
577 for (int y = 0; y < H; y+= h) {
578 // Do not make the subset go off the edge of the image.
579 const int preScaleW = SkTMin(w, W - x);
580 const int preScaleH = SkTMin(h, H - y);
581 subset.setXYWH(x, y, preScaleW, preScaleH);
582 // And scale
583 // FIXME: Should we have a version of getScaledDimensions that takes a subset
584 // into account?
msarette6dd0042015-10-09 11:07:34 -0700585 decodeInfo = decodeInfo.makeWH(
586 SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)),
587 SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)));
scroggob636b452015-07-22 07:16:20 -0700588 size_t rowBytes = decodeInfo.minRowBytes();
589 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700590 nullptr, nullptr)) {
scroggob636b452015-07-22 07:16:20 -0700591 return SkStringPrintf("could not install pixels for %s.", fPath.c_str());
592 }
593 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
594 &opts, colorPtr, colorCountPtr);
595 switch (result) {
596 case SkCodec::kSuccess:
597 case SkCodec::kIncompleteInput:
598 break;
599 case SkCodec::kInvalidConversion:
600 if (0 == (x|y)) {
601 // First subset is okay to return unimplemented.
602 return Error::Nonfatal("Incompatible colortype conversion");
603 }
604 // If the first subset succeeded, a later one should not fail.
605 // fall through to failure
606 case SkCodec::kUnimplemented:
607 if (0 == (x|y)) {
608 // First subset is okay to return unimplemented.
609 return Error::Nonfatal("subset codec not supported");
610 }
611 // If the first subset succeeded, why would a later one fail?
612 // fall through to failure
613 default:
614 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
615 "from %s with dimensions (%d x %d)\t error %d",
616 x, y, decodeInfo.width(), decodeInfo.height(),
617 fPath.c_str(), W, H, result);
618 }
msarett9e9444c2016-02-03 12:39:10 -0800619 premultiply_if_necessary(subsetBm);
scroggob636b452015-07-22 07:16:20 -0700620 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top));
621 // translate by the scaled height.
622 top += decodeInfo.height();
623 }
624 // translate by the scaled width.
625 left += decodeInfo.width();
626 }
627 return "";
628 }
msarettb714fb02016-01-22 14:46:42 -0800629 default:
630 SkASSERT(false);
631 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700632 }
scroggo9c59ebc2015-03-25 13:48:49 -0700633 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700634}
635
636SkISize CodecSrc::size() const {
637 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700638 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700639 if (nullptr == codec) {
640 return SkISize::Make(0, 0);
641 }
642 return codec->getScaledDimensions(fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700643}
644
645Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700646 if (1.0f == fScale) {
647 return SkOSPath::Basename(fPath.c_str());
msarett0a242972015-06-11 14:27:27 -0700648 }
msaretta5783ae2015-09-08 15:35:32 -0700649 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700650}
651
652/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
653
msarett3d9d7a72015-10-21 10:27:10 -0700654AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800655 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700656 : fPath(path)
657 , fMode(mode)
658 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800659 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700660 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800661 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700662{}
663
664bool AndroidCodecSrc::veto(SinkFlags flags) const {
665 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700666 return flags.type != SinkFlags::kRaster
667 || flags.approach != SinkFlags::kDirect;
668}
669
670Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
671 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
672 if (!encoded) {
673 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
674 }
675 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
676 if (nullptr == codec.get()) {
677 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
678 }
679
scroggoc5560be2016-02-03 09:42:42 -0800680 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
681 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700682 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
683 }
684
685 // Scale the image if it is desired.
686 SkISize size = codec->getSampledDimensions(fSampleSize);
687
688 // Visually inspecting very small output images is not necessary. We will
689 // cover these cases in unit testing.
690 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
691 return Error::Nonfatal("Scaling very small images is uninteresting.");
692 }
693 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
694
695 // Construct a color table for the decode if necessary
696 SkAutoTUnref<SkColorTable> colorTable(nullptr);
697 SkPMColor* colorPtr = nullptr;
698 int* colorCountPtr = nullptr;
699 int maxColors = 256;
700 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
701 SkPMColor colors[256];
702 colorTable.reset(new SkColorTable(colors, maxColors));
703 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
704 colorCountPtr = &maxColors;
705 }
706
707 SkBitmap bitmap;
708 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800709 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
msarett3d9d7a72015-10-21 10:27:10 -0700710 decodeInfo.width(), decodeInfo.height());
711 }
712
713 // Create options for the codec.
714 SkAndroidCodec::AndroidOptions options;
715 options.fColorPtr = colorPtr;
716 options.fColorCount = colorCountPtr;
717 options.fSampleSize = fSampleSize;
718
719 switch (fMode) {
720 case kFullImage_Mode: {
721 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(),
722 &options)) {
723 case SkCodec::kSuccess:
724 case SkCodec::kIncompleteInput:
725 break;
msarett3d9d7a72015-10-21 10:27:10 -0700726 default:
727 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
728 }
msarett9e9444c2016-02-03 12:39:10 -0800729 premultiply_if_necessary(bitmap);
msarett3d9d7a72015-10-21 10:27:10 -0700730 canvas->drawBitmap(bitmap, 0, 0);
731 return "";
732 }
733 case kDivisor_Mode: {
734 const int width = codec->getInfo().width();
735 const int height = codec->getInfo().height();
736 const int divisor = 2;
737 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800738 return Error::Nonfatal("Divisor is larger than image dimension.");
msarett3d9d7a72015-10-21 10:27:10 -0700739 }
740
msarettfa23a9e2015-10-21 13:26:59 -0700741 // Keep track of the final decoded dimensions.
742 int finalScaledWidth = 0;
743 int finalScaledHeight = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700744 for (int x = 0; x < divisor; x++) {
745 for (int y = 0; y < divisor; y++) {
746 // Calculate the subset dimensions
747 int subsetWidth = width / divisor;
748 int subsetHeight = height / divisor;
749 const int left = x * subsetWidth;
750 const int top = y * subsetHeight;
751
752 // Increase the size of the last subset in each row or column, when the
753 // divisor does not divide evenly into the image dimensions
754 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
755 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
756 SkIRect subset = SkIRect::MakeXYWH(left, top, subsetWidth, subsetHeight);
757 if (!codec->getSupportedSubset(&subset)) {
mtklein9b439152015-12-09 13:02:26 -0800758 return "Could not get supported subset to decode.";
msarett3d9d7a72015-10-21 10:27:10 -0700759 }
760 options.fSubset = &subset;
msarettfa23a9e2015-10-21 13:26:59 -0700761 const int scaledWidthOffset = subset.left() / fSampleSize;
762 const int scaledHeightOffset = subset.top() / fSampleSize;
763 void* pixels = bitmap.getAddr(scaledWidthOffset, scaledHeightOffset);
msarett3d9d7a72015-10-21 10:27:10 -0700764 SkISize scaledSubsetSize = codec->getSampledSubsetDimensions(fSampleSize,
765 subset);
766 SkImageInfo subsetDecodeInfo = decodeInfo.makeWH(scaledSubsetSize.width(),
767 scaledSubsetSize.height());
768
msarettfa23a9e2015-10-21 13:26:59 -0700769 if (x + 1 == divisor && y + 1 == divisor) {
770 finalScaledWidth = scaledWidthOffset + scaledSubsetSize.width();
771 finalScaledHeight = scaledHeightOffset + scaledSubsetSize.height();
772 }
773
msarett3d9d7a72015-10-21 10:27:10 -0700774 switch (codec->getAndroidPixels(subsetDecodeInfo, pixels, bitmap.rowBytes(),
775 &options)) {
776 case SkCodec::kSuccess:
777 case SkCodec::kIncompleteInput:
778 break;
779 case SkCodec::kInvalidConversion:
mtklein9b439152015-12-09 13:02:26 -0800780 return Error::Nonfatal("Cannot convert to requested color type.");
msarett3d9d7a72015-10-21 10:27:10 -0700781 default:
782 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
783 }
784 }
785 }
msarettfa23a9e2015-10-21 13:26:59 -0700786
787 SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth,
788 (SkScalar) finalScaledHeight);
msarett9e9444c2016-02-03 12:39:10 -0800789 premultiply_if_necessary(bitmap);
msarettfa23a9e2015-10-21 13:26:59 -0700790 canvas->drawBitmapRect(bitmap, rect, rect, nullptr);
msarett3d9d7a72015-10-21 10:27:10 -0700791 return "";
792 }
793 default:
794 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800795 return "Error: Should not be reached.";
msarett3d9d7a72015-10-21 10:27:10 -0700796 }
797}
798
799SkISize AndroidCodecSrc::size() const {
800 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
801 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
802 if (nullptr == codec) {
803 return SkISize::Make(0, 0);
804 }
805 return codec->getSampledDimensions(fSampleSize);
806}
807
808Name AndroidCodecSrc::name() const {
809 // We will replicate the names used by CodecSrc so that images can
810 // be compared in Gold.
811 if (1 == fSampleSize) {
812 return SkOSPath::Basename(fPath.c_str());
813 }
msarett4b0778e2015-11-13 09:59:11 -0800814 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700815}
816
817/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
818
mtkleinf4ba3212015-01-28 15:32:24 -0800819static const SkRect kSKPViewport = {0,0, 1000,1000};
820
mtklein8d17a132015-01-30 11:42:31 -0800821SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800822
823Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800824 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800825 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800826 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
827 }
msarett8715d472016-02-17 10:02:29 -0800828 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream));
mtklein75d98fd2015-01-18 07:05:01 -0800829 if (!pic) {
830 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
831 }
halcanary96fcdcc2015-08-27 07:41:13 -0700832 stream.reset((SkStream*)nullptr); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -0700833
mtkleinf4ba3212015-01-28 15:32:24 -0800834 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800835 canvas->drawPicture(pic);
836 return "";
837}
838
839SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700840 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
841 if (!stream) {
842 return SkISize::Make(0,0);
843 }
844 SkPictInfo info;
845 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
846 return SkISize::Make(0,0);
847 }
848 SkRect viewport = kSKPViewport;
849 if (!viewport.intersect(info.fCullRect)) {
850 return SkISize::Make(0,0);
851 }
852 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800853}
854
855Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
856
857/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
858
mtkleinad66f9b2015-02-13 15:11:10 -0800859Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
860 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
861 return src.draw(canvas);
862}
863
864/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
865
mtkleinb9eb4ac2015-02-02 18:26:03 -0800866DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
867
mtklein82d28432015-01-15 12:46:02 -0800868GPUSink::GPUSink(GrContextFactory::GLContextType ct,
kkinnunen5219fd92015-12-10 06:28:13 -0800869 GrContextFactory::GLContextOptions options,
mtklein82d28432015-01-15 12:46:02 -0800870 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -0700871 bool diText,
mtklein82d28432015-01-15 12:46:02 -0800872 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800873 : fContextType(ct)
kkinnunen5219fd92015-12-10 06:28:13 -0800874 , fContextOptions(options)
mtklein748ca3b2015-01-15 10:56:12 -0800875 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -0700876 , fUseDIText(diText)
mtklein82d28432015-01-15 12:46:02 -0800877 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800878
joshualitt5f5a8d72015-02-25 14:09:45 -0800879void PreAbandonGpuContextErrorHandler(SkError, void*) {}
880
bsalomon648c6962015-10-23 09:06:59 -0700881DEFINE_bool(imm, false, "Run gpu configs in immediate mode.");
bsalomon69cfe952015-11-30 13:27:47 -0800882DEFINE_bool(batchClip, false, "Clip each GrBatch to its device bounds for testing.");
bsalomon6dea83f2015-12-03 12:58:06 -0800883DEFINE_bool(batchBounds, false, "Draw a wireframe bounds of each GrBatch.");
bsalomon489147c2015-12-14 12:13:09 -0800884DEFINE_int32(batchLookback, -1, "Maximum GrBatch lookback for combining, negative means default.");
bsalomon648c6962015-10-23 09:06:59 -0700885
mtkleinb9eb4ac2015-02-02 18:26:03 -0800886Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
kkinnunen5219fd92015-12-10 06:28:13 -0800887 GrContextOptions grOptions;
bsalomon489147c2015-12-14 12:13:09 -0800888 grOptions.fImmediateMode = FLAGS_imm;
889 grOptions.fClipBatchToBounds = FLAGS_batchClip;
890 grOptions.fDrawBatchBounds = FLAGS_batchBounds;
891 grOptions.fMaxBatchLookback = FLAGS_batchLookback;
kkinnunen64492c42015-12-08 01:24:40 -0800892
kkinnunen5219fd92015-12-10 06:28:13 -0800893 src.modifyGrContextOptions(&grOptions);
894
895 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -0800896 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800897 const SkImageInfo info =
898 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
msarett13a036b2016-02-08 09:10:47 -0800899#if SK_SUPPORT_GPU
900 const int maxDimension = factory.getContextInfo(fContextType, fContextOptions).
901 fGrContext->caps()->maxTextureSize();
902 if (maxDimension < SkTMax(size.width(), size.height())) {
903 return Error::Nonfatal("Src too large to create a texture.\n");
904 }
905#endif
906
mtklein748ca3b2015-01-15 10:56:12 -0800907 SkAutoTUnref<SkSurface> surface(
kkinnunen3e980c32015-12-23 01:33:00 -0800908 NewGpuSurface(&factory, fContextType, fContextOptions, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -0800909 if (!surface) {
910 return "Could not create a surface.";
911 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800912 if (FLAGS_preAbandonGpuContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700913 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr);
joshualitt5f5a8d72015-02-25 14:09:45 -0800914 factory.abandonContexts();
915 }
mtklein748ca3b2015-01-15 10:56:12 -0800916 SkCanvas* canvas = surface->getCanvas();
917 Error err = src.draw(canvas);
918 if (!err.isEmpty()) {
919 return err;
920 }
921 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800922 if (FLAGS_gpuStats) {
923 canvas->getGrContext()->dumpCacheStats(log);
924 canvas->getGrContext()->dumpGpuStats(log);
925 }
mtklein748ca3b2015-01-15 10:56:12 -0800926 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800927 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800928 if (FLAGS_abandonGpuContext) {
929 factory.abandonContexts();
930 }
mtklein748ca3b2015-01-15 10:56:12 -0800931 return "";
932}
933
934/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
935
halcanary47ef4d52015-03-03 09:13:09 -0800936static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
937 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
938 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800939 int width = src.size().width(),
940 height = src.size().height();
941
halcanary7e798182015-04-14 14:06:18 -0700942 if (FLAGS_multiPage) {
943 const int kLetterWidth = 612, // 8.5 * 72
944 kLetterHeight = 792; // 11 * 72
945 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
946 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -0800947
halcanary7e798182015-04-14 14:06:18 -0700948 int xPages = ((width - 1) / kLetterWidth) + 1;
949 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -0800950
halcanary7e798182015-04-14 14:06:18 -0700951 for (int y = 0; y < yPages; ++y) {
952 for (int x = 0; x < xPages; ++x) {
953 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
954 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
955 SkCanvas* canvas =
956 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
957 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700958 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700959 }
960 canvas->clipRect(letter);
961 canvas->translate(-letter.width() * x, -letter.height() * y);
962 Error err = src.draw(canvas);
963 if (!err.isEmpty()) {
964 return err;
965 }
966 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -0700967 }
halcanaryfd4a9932015-01-28 11:45:58 -0800968 }
halcanary7e798182015-04-14 14:06:18 -0700969 } else {
970 SkCanvas* canvas =
971 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
972 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700973 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700974 }
975 Error err = src.draw(canvas);
976 if (!err.isEmpty()) {
977 return err;
978 }
979 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -0800980 }
halcanary7e798182015-04-14 14:06:18 -0700981 if (!doc->close()) {
982 return "SkDocument::close() returned false";
983 }
halcanaryfd4a9932015-01-28 11:45:58 -0800984 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800985 return "";
986}
987
halcanaryc11c62f2015-09-28 11:51:54 -0700988PDFSink::PDFSink(const char* rasterizer) : fRasterizer(rasterizer) {}
halcanary47ef4d52015-03-03 09:13:09 -0800989
990Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
991 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
992 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700993 return "SkDocument::CreatePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800994 }
halcanaryf12a1672015-09-23 12:45:49 -0700995 SkTArray<SkDocument::Attribute> info;
996 info.emplace_back(SkString("Title"), src.name());
997 info.emplace_back(SkString("Subject"),
998 SkString("rendering correctness test"));
999 info.emplace_back(SkString("Creator"), SkString("Skia/DM"));
halcanaryc11c62f2015-09-28 11:51:54 -07001000
1001 info.emplace_back(SkString("Keywords"),
1002 SkStringPrintf("Rasterizer:%s;", fRasterizer));
halcanary70015762016-02-11 07:59:59 -08001003 doc->setMetadata(&info[0], info.count(), nullptr, nullptr);
halcanary47ef4d52015-03-03 09:13:09 -08001004 return draw_skdocument(src, doc.get(), dst);
1005}
1006
1007/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1008
1009XPSSink::XPSSink() {}
1010
1011Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1012 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
1013 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -07001014 return "SkDocument::CreateXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001015 }
1016 return draw_skdocument(src, doc.get(), dst);
1017}
mtklein748ca3b2015-01-15 10:56:12 -08001018/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1019
mtklein9c3f17d2015-01-28 11:35:18 -08001020SKPSink::SKPSink() {}
1021
mtkleinb9eb4ac2015-02-02 18:26:03 -08001022Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001023 SkSize size;
1024 size = src.size();
1025 SkPictureRecorder recorder;
1026 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1027 if (!err.isEmpty()) {
1028 return err;
1029 }
1030 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
1031 pic->serialize(dst);
1032 return "";
1033}
1034
1035/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1036
mtklein8a4527e2015-01-31 20:00:58 -08001037SVGSink::SVGSink() {}
1038
mtkleinb9eb4ac2015-02-02 18:26:03 -08001039Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary385fe4d2015-08-26 13:07:48 -07001040 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
fmalita2aafe6f2015-02-06 12:51:10 -08001041 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
1042 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
1043 xmlWriter));
1044 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -08001045}
1046
1047/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1048
mtklein748ca3b2015-01-15 10:56:12 -08001049RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
1050
mtkleinb9eb4ac2015-02-02 18:26:03 -08001051Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001052 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001053 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1054 SkAlphaType alphaType = kPremul_SkAlphaType;
1055 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1056
mtkleinc8be09a2016-01-04 18:56:57 -08001057 SkMallocPixelRef::ZeroedPRFactory factory;
1058 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType),
1059 &factory,
1060 nullptr/*colortable*/);
mtklein748ca3b2015-01-15 10:56:12 -08001061 SkCanvas canvas(*dst);
1062 return src.draw(&canvas);
1063}
1064
1065/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1066
mtkleina16e69e2015-05-05 11:38:45 -07001067// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001068// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001069// Several examples below.
1070
mtkleincbf89782016-02-19 14:27:14 -08001071template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001072static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001073 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001074 class ProxySrc : public Src {
1075 public:
mtkleincbf89782016-02-19 14:27:14 -08001076 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001077 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
msarett62d3b102015-12-10 15:14:27 -08001078 Name name() const override { sk_throw(); return ""; } // Won't be called.
mtkleina16e69e2015-05-05 11:38:45 -07001079 SkISize size() const override { return fSize; }
1080 private:
mtkleincbf89782016-02-19 14:27:14 -08001081 SkISize fSize;
1082 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001083 };
msarett62d3b102015-12-10 15:14:27 -08001084 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001085}
1086
1087/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1088
mtklein4a34ecb2016-01-08 10:19:35 -08001089DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1090
1091// Is *bitmap identical to what you get drawing src into sink?
1092static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1093 // We can only check raster outputs.
1094 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1095 if (FLAGS_check && bitmap) {
1096 SkBitmap reference;
1097 SkString log;
1098 Error err = sink->draw(src, &reference, nullptr, &log);
1099 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1100 SkASSERT(err.isEmpty());
1101 if (!err.isEmpty()) {
1102 return err;
1103 }
1104 // The dimensions are a property of the Src only, and so should be identical.
1105 SkASSERT(reference.getSize() == bitmap->getSize());
1106 if (reference.getSize() != bitmap->getSize()) {
1107 return "Dimensions don't match reference";
1108 }
1109 // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy.
1110 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1111 return "Pixels don't match reference";
1112 }
1113 }
1114 return "";
1115}
1116
1117/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1118
mtkleind603b222015-02-17 11:13:33 -08001119static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1120 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1121 matrix->mapRect(&bounds);
1122 matrix->postTranslate(-bounds.x(), -bounds.y());
1123 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
1124}
1125
msarett62d3b102015-12-10 15:14:27 -08001126ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001127
mtkleinb9eb4ac2015-02-02 18:26:03 -08001128Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001129 SkMatrix matrix = fMatrix;
1130 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
msarett62d3b102015-12-10 15:14:27 -08001131 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001132 canvas->concat(matrix);
1133 return src.draw(canvas);
1134 });
mtklein748ca3b2015-01-15 10:56:12 -08001135}
1136
mtkleind603b222015-02-17 11:13:33 -08001137// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1138// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001139ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001140
1141Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1142 Error err = fSink->draw(src, bitmap, stream, log);
1143 if (!err.isEmpty()) {
1144 return err;
1145 }
1146
1147 SkMatrix inverse;
1148 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1149 return "Cannot upright --matrix.";
1150 }
1151 SkMatrix upright = SkMatrix::I();
1152 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1153 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1154 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1155 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1156
1157 SkBitmap uprighted;
1158 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1159 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1160
1161 SkCanvas canvas(uprighted);
1162 canvas.concat(upright);
1163 SkPaint paint;
1164 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1165 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1166
1167 *bitmap = uprighted;
1168 bitmap->lockPixels();
1169 return "";
1170}
1171
mtklein748ca3b2015-01-15 10:56:12 -08001172/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1173
mtklein2e2ea382015-10-16 10:29:41 -07001174Error ViaRemote::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001175 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* target) {
mtklein479fe772015-10-21 12:34:01 -07001176 SkAutoTDelete<SkRemote::Encoder> decoder(SkRemote::NewDecoder(target));
1177 SkAutoTDelete<SkRemote::Encoder> cache(fCache ? SkRemote::NewCachingEncoder(decoder)
1178 : nullptr);
1179 SkAutoTDelete<SkCanvas> canvas(SkRemote::NewCanvas(cache ? cache : decoder));
1180 return src.draw(canvas);
mtklein2e2ea382015-10-16 10:29:41 -07001181 });
1182}
1183
mtklein748ca3b2015-01-15 10:56:12 -08001184/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtkleina16e69e2015-05-05 11:38:45 -07001185
mtkleina16e69e2015-05-05 11:38:45 -07001186Error ViaSerialization::draw(
1187 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001188 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001189 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001190 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001191 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1192 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001193 if (!err.isEmpty()) {
1194 return err;
1195 }
1196 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
1197
1198 // Serialize it and then deserialize it.
1199 SkDynamicMemoryWStream wStream;
1200 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -08001201 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
msarett8715d472016-02-17 10:02:29 -08001202 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream));
mtklein748ca3b2015-01-15 10:56:12 -08001203
msarett62d3b102015-12-10 15:14:27 -08001204 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001205 canvas->drawPicture(deserialized);
mtklein4a34ecb2016-01-08 10:19:35 -08001206 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001207 });
mtklein748ca3b2015-01-15 10:56:12 -08001208}
1209
1210/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1211
msarett62d3b102015-12-10 15:14:27 -08001212ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1213 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001214 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001215 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001216 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001217
mtkleinb9eb4ac2015-02-02 18:26:03 -08001218Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001219 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001220 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001221 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1222 SkIntToScalar(size.height()),
1223 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001224 if (!err.isEmpty()) {
1225 return err;
1226 }
mtkleinb7e8d692015-04-07 08:30:32 -07001227 SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001228
msarett62d3b102015-12-10 15:14:27 -08001229 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001230 const int xTiles = (size.width() + fW - 1) / fW,
1231 yTiles = (size.height() + fH - 1) / fH;
1232 SkMultiPictureDraw mpd(xTiles*yTiles);
1233 SkTDArray<SkSurface*> surfaces;
1234 surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001235
mtkleina16e69e2015-05-05 11:38:45 -07001236 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1237 for (int j = 0; j < yTiles; j++) {
1238 for (int i = 0; i < xTiles; i++) {
1239 // This lets our ultimate Sink determine the best kind of surface.
1240 // E.g., if it's a GpuSink, the surfaces and images are textures.
1241 SkSurface* s = canvas->newSurface(info);
1242 if (!s) {
1243 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001244 }
mtkleina16e69e2015-05-05 11:38:45 -07001245 surfaces.push(s);
1246 SkCanvas* c = s->getCanvas();
1247 c->translate(SkIntToScalar(-i * fW),
1248 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
1249 mpd.add(c, pic);
mtklein748ca3b2015-01-15 10:56:12 -08001250 }
mtklein748ca3b2015-01-15 10:56:12 -08001251 }
mtkleina16e69e2015-05-05 11:38:45 -07001252 mpd.draw();
1253 for (int j = 0; j < yTiles; j++) {
1254 for (int i = 0; i < xTiles; i++) {
1255 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
1256 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1257 }
1258 }
1259 surfaces.unrefAll();
1260 return "";
1261 });
mtklein748ca3b2015-01-15 10:56:12 -08001262}
1263
mtkleinb7e8d692015-04-07 08:30:32 -07001264/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1265
mtklein4a34ecb2016-01-08 10:19:35 -08001266Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1267 auto size = src.size();
1268 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1269 SkPictureRecorder recorder;
1270 SkAutoTUnref<SkPicture> pic;
1271 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1272 SkIntToScalar(size.height())));
1273 if (!err.isEmpty()) {
1274 return err;
1275 }
1276 pic.reset(recorder.endRecordingAsPicture());
1277 canvas->drawPicture(pic);
1278 return check_against_reference(bitmap, src, fSink);
1279 });
1280}
1281
1282/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1283
mtkleinb7e8d692015-04-07 08:30:32 -07001284// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1285// This tests that any shortcuts we may take while recording that second picture are legal.
1286Error ViaSecondPicture::draw(
1287 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001288 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001289 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001290 SkPictureRecorder recorder;
1291 SkAutoTUnref<SkPicture> pic;
1292 for (int i = 0; i < 2; i++) {
1293 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1294 SkIntToScalar(size.height())));
1295 if (!err.isEmpty()) {
1296 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001297 }
mtkleina16e69e2015-05-05 11:38:45 -07001298 pic.reset(recorder.endRecordingAsPicture());
mtkleinb7e8d692015-04-07 08:30:32 -07001299 }
mtkleina16e69e2015-05-05 11:38:45 -07001300 canvas->drawPicture(pic);
mtklein4a34ecb2016-01-08 10:19:35 -08001301 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001302 });
mtkleinb7e8d692015-04-07 08:30:32 -07001303}
1304
mtkleind31c13d2015-05-05 12:59:56 -07001305/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1306
mtklein6fbf4b32015-05-06 11:35:40 -07001307// Draw the Src twice. This can help exercise caching.
1308Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001309 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001310 for (int i = 0; i < 2; i++) {
1311 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1312 canvas->clear(SK_ColorTRANSPARENT);
1313 Error err = src.draw(canvas);
1314 if (err.isEmpty()) {
1315 return err;
1316 }
1317 }
mtklein4a34ecb2016-01-08 10:19:35 -08001318 return check_against_reference(bitmap, src, fSink);
mtklein6fbf4b32015-05-06 11:35:40 -07001319 });
1320}
1321
1322/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1323
halcanary7a76f9c2016-02-03 11:53:18 -08001324#ifdef SK_MOJO
1325 Error ViaMojo::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1326 SkPictureRecorder recorder;
1327 SkRect size = SkRect::Make(SkIRect::MakeSize(src.size()));
1328 Error err = src.draw(recorder.beginRecording(size));
1329 if (!err.isEmpty()) {
1330 return err;
1331 }
1332 SkAutoTUnref<SkPicture> skPicture(recorder.endRecording());
1333
1334 SkASSERT(skPicture);
1335 SkDynamicMemoryWStream buffer;
1336 skPicture->serialize(&buffer);
1337 skPicture.reset();
1338 SkMojo::FlattenedPicturePtr mojoPicture = SkMojo::FlattenedPicture::New();
1339 mojoPicture->data.resize(buffer.bytesWritten());
1340 buffer.copyTo(mojoPicture->data.data());
1341 buffer.reset();
1342 SkASSERT(mojoPicture.get() && mojoPicture->data);
1343
1344 size_t flatSize = mojoPicture->GetSerializedSize();
1345 SkAutoMalloc storage(flatSize);
1346 if (!mojoPicture->Serialize(storage.get(), flatSize)) {
1347 return "SkMojo::FlattenedPicture::Serialize failed";
1348 }
1349 mojoPicture = SkMojo::FlattenedPicture::New();
1350 mojoPicture->Deserialize(storage.get());
1351 storage.free();
1352 if (!mojoPicture) {
1353 return "SkMojo::FlattenedPicture::Deserialize failed";
1354 }
1355 SkMemoryStream tmpStream(mojoPicture->data.data(),
1356 mojoPicture->data.size());
1357 skPicture.reset(SkPicture::CreateFromStream(&tmpStream));
1358 mojoPicture.reset();
1359 auto fn = [&](SkCanvas* canvas) -> Error {
1360 canvas->drawPicture(skPicture.get());
1361 return check_against_reference(bitmap, src, fSink);
1362 };
1363 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), fn);
1364 }
1365#else // not SK_MOJO
1366 Error ViaMojo::draw(const Src&, SkBitmap*, SkWStream*, SkString*) const {
1367 return "Mojo is missing!";
1368 }
1369#endif
1370
1371/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1372
mtkleind31c13d2015-05-05 12:59:56 -07001373// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1374// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1375// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1376struct DrawsAsSingletonPictures {
1377 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001378 const SkDrawableList& fDrawables;
mtkleind31c13d2015-05-05 12:59:56 -07001379
mtkleind31c13d2015-05-05 12:59:56 -07001380 template <typename T>
1381 void draw(const T& op, SkCanvas* canvas) {
1382 // We must pass SkMatrix::I() as our initial matrix.
1383 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1384 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001385 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1386 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001387 }
1388
mtklein449d9b72015-09-28 10:33:02 -07001389 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001390 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001391 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001392 SkPictureRecorder rec;
1393 this->draw(op, rec.beginRecording(SkRect::MakeLargest()));
1394 SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture());
1395 fCanvas->drawPicture(pic);
1396 }
1397
mtklein449d9b72015-09-28 10:33:02 -07001398 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001399 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001400 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1401 this->draw(op, fCanvas);
1402 }
mtkleind31c13d2015-05-05 12:59:56 -07001403};
1404
mtkleind31c13d2015-05-05 12:59:56 -07001405// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1406// Then play back that macro picture into our wrapped sink.
1407Error ViaSingletonPictures::draw(
1408 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1409 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001410 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07001411 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1412 SkRecord skr;
1413 SkRecorder recorder(&skr, size.width(), size.height());
1414 Error err = src.draw(&recorder);
1415 if (!err.isEmpty()) {
1416 return err;
1417 }
1418
1419 // Record our macro-picture, with each draw op as its own sub-picture.
1420 SkPictureRecorder macroRec;
1421 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1422 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001423
1424 SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
1425 const SkDrawableList empty;
1426
1427 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1428 macroCanvas,
1429 drawables ? *drawables : empty,
1430 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001431 for (int i = 0; i < skr.count(); i++) {
mtkleind31c13d2015-05-05 12:59:56 -07001432 skr.visit<void>(i, drawsAsSingletonPictures);
1433 }
1434 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture());
1435
1436 canvas->drawPicture(macroPic);
mtklein4a34ecb2016-01-08 10:19:35 -08001437 return check_against_reference(bitmap, src, fSink);
mtkleind31c13d2015-05-05 12:59:56 -07001438 });
1439}
1440
mtklein748ca3b2015-01-15 10:56:12 -08001441} // namespace DM