blob: e5d18cdaafd86af6d66ff5f3b4660619b64cc933 [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
kjlubick1de415f2016-02-10 11:25:07 -080043static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) {
44 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size));
45 return encoded && SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst);
46}
47
mtklein748ca3b2015-01-15 10:56:12 -080048namespace DM {
49
mtklein748ca3b2015-01-15 10:56:12 -080050GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
51
52Error GMSrc::draw(SkCanvas* canvas) const {
halcanary96fcdcc2015-08-27 07:41:13 -070053 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080054 canvas->concat(gm->getInitialTransform());
55 gm->draw(canvas);
56 return "";
57}
58
59SkISize GMSrc::size() const {
halcanary96fcdcc2015-08-27 07:41:13 -070060 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080061 return gm->getISize();
62}
63
64Name GMSrc::name() const {
halcanary96fcdcc2015-08-27 07:41:13 -070065 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080066 return gm->getName();
67}
68
bsalomon4ee6bd82015-05-27 13:23:23 -070069void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
halcanary96fcdcc2015-08-27 07:41:13 -070070 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070071 gm->modifyGrContextOptions(options);
72}
73
mtklein748ca3b2015-01-15 10:56:12 -080074/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
75
msarett5cb48852015-11-06 08:56:32 -080076BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode,
msaretta5783ae2015-09-08 15:35:32 -070077 CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
78 : fPath(path)
79 , fStrategy(strategy)
80 , fMode(mode)
81 , fDstColorType(dstColorType)
82 , fSampleSize(sampleSize)
83{}
84
85bool BRDSrc::veto(SinkFlags flags) const {
86 // No need to test to non-raster or indirect backends.
87 return flags.type != SinkFlags::kRaster
88 || flags.approach != SinkFlags::kDirect;
89}
90
msarett5cb48852015-11-06 08:56:32 -080091static SkBitmapRegionDecoder* create_brd(Path path,
92 SkBitmapRegionDecoder::Strategy strategy) {
msaretta5783ae2015-09-08 15:35:32 -070093 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
94 if (!encoded) {
95 return NULL;
96 }
msarett5cb48852015-11-06 08:56:32 -080097 return SkBitmapRegionDecoder::Create(encoded, strategy);
msaretta5783ae2015-09-08 15:35:32 -070098}
99
100Error BRDSrc::draw(SkCanvas* canvas) const {
101 SkColorType colorType = canvas->imageInfo().colorType();
102 if (kRGB_565_SkColorType == colorType &&
103 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
104 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
105 }
106 switch (fDstColorType) {
107 case CodecSrc::kGetFromCanvas_DstColorType:
108 break;
109 case CodecSrc::kIndex8_Always_DstColorType:
110 colorType = kIndex_8_SkColorType;
111 break;
112 case CodecSrc::kGrayscale_Always_DstColorType:
113 colorType = kGray_8_SkColorType;
114 break;
115 }
116
msarett5cb48852015-11-06 08:56:32 -0800117 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700118 if (nullptr == brd.get()) {
119 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
120 }
121
msarett04965c62015-10-12 10:24:38 -0700122 if (!brd->conversionSupported(colorType)) {
mtklein9b439152015-12-09 13:02:26 -0800123 return Error::Nonfatal("Cannot convert to color type.");
msarett04965c62015-10-12 10:24:38 -0700124 }
125
msaretta5783ae2015-09-08 15:35:32 -0700126 const uint32_t width = brd->width();
127 const uint32_t height = brd->height();
128 // Visually inspecting very small output images is not necessary.
129 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
130 return Error::Nonfatal("Scaling very small images is uninteresting.");
131 }
132 switch (fMode) {
133 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700134 SkBitmap bitmap;
135 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
136 fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800137 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700138 }
139 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800140 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700141 }
msarett35e5d1b2015-10-27 12:50:25 -0700142 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700143 return "";
144 }
145 case kDivisor_Mode: {
146 const uint32_t divisor = 2;
147 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800148 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700149 }
150
151 // Use a border to test subsets that extend outside the image.
152 // We will not allow the border to be larger than the image dimensions. Allowing
153 // these large borders causes off by one errors that indicate a problem with the
154 // test suite, not a problem with the implementation.
155 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
156 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
157 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
158
159 // We may need to clear the canvas to avoid uninitialized memory.
160 // Assume we are scaling a 780x780 image with sampleSize = 8.
161 // The output image should be 97x97.
162 // Each subset will be 390x390.
163 // Each scaled subset be 48x48.
164 // Four scaled subsets will only fill a 96x96 image.
165 // The bottom row and last column will not be touched.
166 // This is an unfortunate result of our rounding rules when scaling.
167 // Maybe we need to consider testing scaled subsets without trying to
168 // combine them to match the full scaled image? Or maybe this is the
169 // best we can do?
170 canvas->clear(0);
171
172 for (uint32_t x = 0; x < divisor; x++) {
173 for (uint32_t y = 0; y < divisor; y++) {
174 // Calculate the subset dimensions
175 uint32_t subsetWidth = width / divisor;
176 uint32_t subsetHeight = height / divisor;
177 const int left = x * subsetWidth;
178 const int top = y * subsetHeight;
179
180 // Increase the size of the last subset in each row or column, when the
181 // divisor does not divide evenly into the image dimensions
182 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
183 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
184
185 // Increase the size of the subset in order to have a border on each side
186 const int decodeLeft = left - unscaledBorder;
187 const int decodeTop = top - unscaledBorder;
188 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
189 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700190 SkBitmap bitmap;
191 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
192 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800193 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700194 }
195 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800196 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700197 }
198
msarett35e5d1b2015-10-27 12:50:25 -0700199 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700200 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
201 (SkScalar) (subsetWidth / fSampleSize),
202 (SkScalar) (subsetHeight / fSampleSize)),
203 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
204 (SkScalar) (top / fSampleSize),
205 (SkScalar) (subsetWidth / fSampleSize),
206 (SkScalar) (subsetHeight / fSampleSize)),
207 nullptr);
208 }
209 }
210 return "";
211 }
212 default:
213 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800214 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700215 }
216}
217
218SkISize BRDSrc::size() const {
msarett5cb48852015-11-06 08:56:32 -0800219 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700220 if (brd) {
221 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
222 SkTMax(1, brd->height() / (int) fSampleSize));
223 }
224 return SkISize::Make(0, 0);
225}
226
227static SkString get_scaled_name(const Path& path, float scale) {
228 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
229}
230
231Name BRDSrc::name() const {
232 // We will replicate the names used by CodecSrc so that images can
233 // be compared in Gold.
234 if (1 == fSampleSize) {
235 return SkOSPath::Basename(fPath.c_str());
236 }
msarett4b0778e2015-11-13 09:59:11 -0800237 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700238}
239
240/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
241
scroggo3ac66e92016-02-08 15:09:48 -0800242static bool serial_from_path_name(const SkString& path) {
243 if (!FLAGS_RAW_threading) {
244 static const char* const exts[] = {
245 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
246 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
247 };
248 const char* actualExt = strrchr(path.c_str(), '.');
249 if (actualExt) {
250 actualExt++;
251 for (auto* ext : exts) {
252 if (0 == strcmp(ext, actualExt)) {
253 return true;
254 }
255 }
256 }
257 }
258 return false;
259}
260
scroggoc5560be2016-02-03 09:42:42 -0800261CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
262 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700263 : fPath(path)
264 , fMode(mode)
265 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800266 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700267 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800268 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700269{}
mtklein748ca3b2015-01-15 10:56:12 -0800270
mtklein99cab4e2015-07-31 06:43:04 -0700271bool CodecSrc::veto(SinkFlags flags) const {
msarettb714fb02016-01-22 14:46:42 -0800272 // Test CodecImageGenerator on 8888, 565, and gpu
273 if (kGen_Mode == fMode) {
msarett36c070d2016-02-10 06:48:21 -0800274 // For image generator, we want to test kDirect approaches for kRaster and kGPU,
275 // while skipping everything else.
276 return (flags.type != SinkFlags::kRaster && flags.type != SinkFlags::kGPU) ||
msaretta23659f2016-02-09 12:39:17 -0800277 flags.approach != SinkFlags::kDirect;
msarettb714fb02016-01-22 14:46:42 -0800278 }
279
280 // Test all other modes to direct raster backends (8888 and 565).
281 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700282}
scroggo9b77ddd2015-03-19 06:03:39 -0700283
msarett9e9444c2016-02-03 12:39:10 -0800284// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
285// This allows us to still test unpremultiplied decodes.
286void premultiply_if_necessary(SkBitmap& bitmap) {
287 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
288 return;
289 }
290
291 switch (bitmap.colorType()) {
292 case kN32_SkColorType:
293 for (int y = 0; y < bitmap.height(); y++) {
294 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
295 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
296 }
297 break;
298 case kIndex_8_SkColorType: {
299 SkColorTable* colorTable = bitmap.getColorTable();
300 SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
301 SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count());
302 break;
303 }
304 default:
305 // No need to premultiply kGray or k565 outputs.
306 break;
307 }
msarette1daa482016-02-03 15:31:18 -0800308
309 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
310 // bitmap as kPremul.
311 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800312}
313
scroggoc5560be2016-02-03 09:42:42 -0800314bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
315 CodecSrc::DstColorType dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700316 switch (dstColorType) {
317 case CodecSrc::kIndex8_Always_DstColorType:
318 if (kRGB_565_SkColorType == canvasColorType) {
319 return false;
320 }
scroggoc5560be2016-02-03 09:42:42 -0800321 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700322 break;
323 case CodecSrc::kGrayscale_Always_DstColorType:
msarett55f7bdd2016-02-16 13:24:54 -0800324 if (kRGB_565_SkColorType == canvasColorType ||
325 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
msarett3d9d7a72015-10-21 10:27:10 -0700326 return false;
327 }
scroggoc5560be2016-02-03 09:42:42 -0800328 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700329 break;
330 default:
msarett55f7bdd2016-02-16 13:24:54 -0800331 if (kRGB_565_SkColorType == canvasColorType &&
332 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
333 return false;
334 }
scroggoc5560be2016-02-03 09:42:42 -0800335 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700336 break;
337 }
338
msarett3d9d7a72015-10-21 10:27:10 -0700339 return true;
340}
341
msarettb714fb02016-01-22 14:46:42 -0800342Error test_gen(SkCanvas* canvas, SkData* data) {
msarett01813e82016-01-25 10:51:29 -0800343 SkAutoTDelete<SkImageGenerator> gen = SkCodecImageGenerator::NewFromEncodedCodec(data);
msarettb714fb02016-01-22 14:46:42 -0800344 if (!gen) {
345 return "Could not create image generator.";
346 }
347
348 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
349 // Currently, we will avoid creating a CodecSrc for this case (see DM.cpp).
350 SkASSERT(kGray_8_SkColorType != gen->getInfo().colorType());
351
msarettd03b7ae2016-01-25 08:20:55 -0800352 if (kOpaque_SkAlphaType != gen->getInfo().alphaType() &&
353 kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
354 return Error::Nonfatal("Skip testing non-opaque images to 565.");
355 }
356
msarett01813e82016-01-25 10:51:29 -0800357 SkAutoTDelete<SkImage> image(SkImage::NewFromGenerator(gen.detach(), nullptr));
msarettb714fb02016-01-22 14:46:42 -0800358 if (!image) {
359 return "Could not create image from codec image generator.";
360 }
361
362 canvas->drawImage(image, 0, 0);
363 return "";
364}
365
mtkleine0effd62015-07-29 06:37:28 -0700366Error CodecSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -0800367 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800368 if (!encoded) {
369 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
370 }
msarettb714fb02016-01-22 14:46:42 -0800371
372 // The CodecImageGenerator test does not share much code with the other tests,
373 // so we will handle it in its own function.
374 if (kGen_Mode == fMode) {
375 return test_gen(canvas, encoded);
376 }
377
msarett3d9d7a72015-10-21 10:27:10 -0700378 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700379 if (nullptr == codec.get()) {
380 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700381 }
382
scroggoc5560be2016-02-03 09:42:42 -0800383 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
384 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700385 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
msarett438b2ad2015-04-09 12:43:10 -0700386 }
387
msarett0a242972015-06-11 14:27:27 -0700388 // Try to scale the image if it is desired
389 SkISize size = codec->getScaledDimensions(fScale);
390 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
391 return Error::Nonfatal("Test without scaling is uninteresting.");
392 }
msarettb32758a2015-08-18 13:22:46 -0700393
394 // Visually inspecting very small output images is not necessary. We will
395 // cover these cases in unit testing.
396 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
397 return Error::Nonfatal("Scaling very small images is uninteresting.");
398 }
msarett0a242972015-06-11 14:27:27 -0700399 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
400
msarett438b2ad2015-04-09 12:43:10 -0700401 // Construct a color table for the decode if necessary
halcanary96fcdcc2015-08-27 07:41:13 -0700402 SkAutoTUnref<SkColorTable> colorTable(nullptr);
403 SkPMColor* colorPtr = nullptr;
404 int* colorCountPtr = nullptr;
msarett438b2ad2015-04-09 12:43:10 -0700405 int maxColors = 256;
406 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
407 SkPMColor colors[256];
halcanary385fe4d2015-08-26 13:07:48 -0700408 colorTable.reset(new SkColorTable(colors, maxColors));
msarett438b2ad2015-04-09 12:43:10 -0700409 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
410 colorCountPtr = &maxColors;
411 }
412
scroggo9b77ddd2015-03-19 06:03:39 -0700413 SkBitmap bitmap;
msarettbb25b532016-01-13 09:31:39 -0800414 SkPixelRefFactory* factory = nullptr;
415 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
416 SkCodec::Options options;
417 if (kCodecZeroInit_Mode == fMode) {
418 factory = &zeroFactory;
419 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
420 }
421 if (!bitmap.tryAllocPixels(decodeInfo, factory, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800422 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
scroggo9b77ddd2015-03-19 06:03:39 -0700423 decodeInfo.width(), decodeInfo.height());
424 }
425
scroggo9c59ebc2015-03-25 13:48:49 -0700426 switch (fMode) {
msarettbb25b532016-01-13 09:31:39 -0800427 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700428 case kCodec_Mode: {
msarettbb25b532016-01-13 09:31:39 -0800429 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
msarett438b2ad2015-04-09 12:43:10 -0700430 colorPtr, colorCountPtr)) {
scroggoeb602a52015-07-09 08:16:03 -0700431 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700432 // We consider incomplete to be valid, since we should still decode what is
433 // available.
scroggoeb602a52015-07-09 08:16:03 -0700434 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700435 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700436 default:
437 // Everything else is considered a failure.
438 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
439 }
msarett9e9444c2016-02-03 12:39:10 -0800440 premultiply_if_necessary(bitmap);
emmaleer97002062015-05-27 12:36:10 -0700441 canvas->drawBitmap(bitmap, 0, 0);
scroggo9c59ebc2015-03-25 13:48:49 -0700442 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700443 }
scroggo9c59ebc2015-03-25 13:48:49 -0700444 case kScanline_Mode: {
scroggo46c57472015-09-30 08:57:13 -0700445 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
446 colorCountPtr)) {
msarett9e707a02015-09-01 14:57:57 -0700447 return Error::Nonfatal("Could not start scanline decoder");
scroggo9c59ebc2015-03-25 13:48:49 -0700448 }
scroggo1c005e42015-08-04 09:24:45 -0700449
msarette6dd0042015-10-09 11:07:34 -0700450 void* dst = bitmap.getAddr(0, 0);
451 size_t rowBytes = bitmap.rowBytes();
452 uint32_t height = decodeInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700453 switch (codec->getScanlineOrder()) {
454 case SkCodec::kTopDown_SkScanlineOrder:
455 case SkCodec::kBottomUp_SkScanlineOrder:
456 case SkCodec::kNone_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700457 // We do not need to check the return value. On an incomplete
458 // image, memory will be filled with a default value.
459 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700460 break;
scroggo46c57472015-09-30 08:57:13 -0700461 case SkCodec::kOutOfOrder_SkScanlineOrder: {
msarett10522ff2015-09-07 08:54:01 -0700462 for (int y = 0; y < decodeInfo.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700463 int dstY = codec->outputScanline(y);
msarett10522ff2015-09-07 08:54:01 -0700464 void* dstPtr = bitmap.getAddr(0, dstY);
msarette6dd0042015-10-09 11:07:34 -0700465 // We complete the loop, even if this call begins to fail
466 // due to an incomplete image. This ensures any uninitialized
467 // memory will be filled with the proper value.
468 codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
msarett10522ff2015-09-07 08:54:01 -0700469 }
470 break;
471 }
472 }
473
msarett9e9444c2016-02-03 12:39:10 -0800474 premultiply_if_necessary(bitmap);
emmaleer97002062015-05-27 12:36:10 -0700475 canvas->drawBitmap(bitmap, 0, 0);
476 break;
477 }
msarett0a242972015-06-11 14:27:27 -0700478 case kStripe_Mode: {
479 const int height = decodeInfo.height();
480 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
481 // does not align with image blocks.
482 const int stripeHeight = 37;
483 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
484
485 // Decode odd stripes
scroggo46c57472015-09-30 08:57:13 -0700486 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
487 colorCountPtr)
488 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
msarett9e707a02015-09-01 14:57:57 -0700489 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
490 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
491 // to run this test for image types that do not have this scanline ordering.
msarett5406d6f2015-08-31 06:55:13 -0700492 return Error::Nonfatal("Could not start top-down scanline decoder");
msarett0a242972015-06-11 14:27:27 -0700493 }
msarette6dd0042015-10-09 11:07:34 -0700494
msarett0a242972015-06-11 14:27:27 -0700495 for (int i = 0; i < numStripes; i += 2) {
496 // Skip a stripe
497 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700498 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700499
500 // Read a stripe
501 const int startY = (i + 1) * stripeHeight;
502 const int linesToRead = SkTMin(stripeHeight, height - startY);
503 if (linesToRead > 0) {
msarette6dd0042015-10-09 11:07:34 -0700504 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700505 }
506 }
507
508 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700509 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
510 colorPtr, colorCountPtr);
scroggo1c005e42015-08-04 09:24:45 -0700511 if (SkCodec::kSuccess != startResult) {
512 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700513 }
514 for (int i = 0; i < numStripes; i += 2) {
515 // Read a stripe
516 const int startY = i * stripeHeight;
517 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarette6dd0042015-10-09 11:07:34 -0700518 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700519
520 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700521 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
522 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700523 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700524 }
525 }
msarett9e9444c2016-02-03 12:39:10 -0800526 premultiply_if_necessary(bitmap);
msarett0a242972015-06-11 14:27:27 -0700527 canvas->drawBitmap(bitmap, 0, 0);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700528 break;
msarett0a242972015-06-11 14:27:27 -0700529 }
scroggob636b452015-07-22 07:16:20 -0700530 case kSubset_Mode: {
531 // Arbitrarily choose a divisor.
532 int divisor = 2;
533 // Total width/height of the image.
534 const int W = codec->getInfo().width();
535 const int H = codec->getInfo().height();
536 if (divisor > W || divisor > H) {
537 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
538 "for %s with dimensions (%d x %d)", divisor,
539 fPath.c_str(), W, H));
540 }
541 // subset dimensions
542 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
543 const int w = SkAlign2(W / divisor);
544 const int h = SkAlign2(H / divisor);
545 SkIRect subset;
546 SkCodec::Options opts;
547 opts.fSubset = &subset;
548 SkBitmap subsetBm;
549 // We will reuse pixel memory from bitmap.
550 void* pixels = bitmap.getPixels();
551 // Keep track of left and top (for drawing subsetBm into canvas). We could use
552 // fScale * x and fScale * y, but we want integers such that the next subset will start
553 // where the last one ended. So we'll add decodeInfo.width() and height().
554 int left = 0;
555 for (int x = 0; x < W; x += w) {
556 int top = 0;
557 for (int y = 0; y < H; y+= h) {
558 // Do not make the subset go off the edge of the image.
559 const int preScaleW = SkTMin(w, W - x);
560 const int preScaleH = SkTMin(h, H - y);
561 subset.setXYWH(x, y, preScaleW, preScaleH);
562 // And scale
563 // FIXME: Should we have a version of getScaledDimensions that takes a subset
564 // into account?
msarette6dd0042015-10-09 11:07:34 -0700565 decodeInfo = decodeInfo.makeWH(
566 SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)),
567 SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)));
scroggob636b452015-07-22 07:16:20 -0700568 size_t rowBytes = decodeInfo.minRowBytes();
569 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700570 nullptr, nullptr)) {
scroggob636b452015-07-22 07:16:20 -0700571 return SkStringPrintf("could not install pixels for %s.", fPath.c_str());
572 }
573 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
574 &opts, colorPtr, colorCountPtr);
575 switch (result) {
576 case SkCodec::kSuccess:
577 case SkCodec::kIncompleteInput:
578 break;
579 case SkCodec::kInvalidConversion:
580 if (0 == (x|y)) {
581 // First subset is okay to return unimplemented.
582 return Error::Nonfatal("Incompatible colortype conversion");
583 }
584 // If the first subset succeeded, a later one should not fail.
585 // fall through to failure
586 case SkCodec::kUnimplemented:
587 if (0 == (x|y)) {
588 // First subset is okay to return unimplemented.
589 return Error::Nonfatal("subset codec not supported");
590 }
591 // If the first subset succeeded, why would a later one fail?
592 // fall through to failure
593 default:
594 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
595 "from %s with dimensions (%d x %d)\t error %d",
596 x, y, decodeInfo.width(), decodeInfo.height(),
597 fPath.c_str(), W, H, result);
598 }
msarett9e9444c2016-02-03 12:39:10 -0800599 premultiply_if_necessary(subsetBm);
scroggob636b452015-07-22 07:16:20 -0700600 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top));
601 // translate by the scaled height.
602 top += decodeInfo.height();
603 }
604 // translate by the scaled width.
605 left += decodeInfo.width();
606 }
607 return "";
608 }
msarettb714fb02016-01-22 14:46:42 -0800609 default:
610 SkASSERT(false);
611 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700612 }
scroggo9c59ebc2015-03-25 13:48:49 -0700613 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700614}
615
616SkISize CodecSrc::size() const {
617 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700618 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700619 if (nullptr == codec) {
620 return SkISize::Make(0, 0);
621 }
622 return codec->getScaledDimensions(fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700623}
624
625Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700626 if (1.0f == fScale) {
627 return SkOSPath::Basename(fPath.c_str());
msarett0a242972015-06-11 14:27:27 -0700628 }
msaretta5783ae2015-09-08 15:35:32 -0700629 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700630}
631
632/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
633
msarett3d9d7a72015-10-21 10:27:10 -0700634AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800635 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700636 : fPath(path)
637 , fMode(mode)
638 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800639 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700640 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800641 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700642{}
643
644bool AndroidCodecSrc::veto(SinkFlags flags) const {
645 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700646 return flags.type != SinkFlags::kRaster
647 || flags.approach != SinkFlags::kDirect;
648}
649
650Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
651 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
652 if (!encoded) {
653 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
654 }
655 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
656 if (nullptr == codec.get()) {
657 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
658 }
659
scroggoc5560be2016-02-03 09:42:42 -0800660 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
661 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700662 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
663 }
664
665 // Scale the image if it is desired.
666 SkISize size = codec->getSampledDimensions(fSampleSize);
667
668 // Visually inspecting very small output images is not necessary. We will
669 // cover these cases in unit testing.
670 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
671 return Error::Nonfatal("Scaling very small images is uninteresting.");
672 }
673 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
674
675 // Construct a color table for the decode if necessary
676 SkAutoTUnref<SkColorTable> colorTable(nullptr);
677 SkPMColor* colorPtr = nullptr;
678 int* colorCountPtr = nullptr;
679 int maxColors = 256;
680 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
681 SkPMColor colors[256];
682 colorTable.reset(new SkColorTable(colors, maxColors));
683 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
684 colorCountPtr = &maxColors;
685 }
686
687 SkBitmap bitmap;
688 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800689 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
msarett3d9d7a72015-10-21 10:27:10 -0700690 decodeInfo.width(), decodeInfo.height());
691 }
692
693 // Create options for the codec.
694 SkAndroidCodec::AndroidOptions options;
695 options.fColorPtr = colorPtr;
696 options.fColorCount = colorCountPtr;
697 options.fSampleSize = fSampleSize;
698
699 switch (fMode) {
700 case kFullImage_Mode: {
701 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(),
702 &options)) {
703 case SkCodec::kSuccess:
704 case SkCodec::kIncompleteInput:
705 break;
msarett3d9d7a72015-10-21 10:27:10 -0700706 default:
707 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
708 }
msarett9e9444c2016-02-03 12:39:10 -0800709 premultiply_if_necessary(bitmap);
msarett3d9d7a72015-10-21 10:27:10 -0700710 canvas->drawBitmap(bitmap, 0, 0);
711 return "";
712 }
713 case kDivisor_Mode: {
714 const int width = codec->getInfo().width();
715 const int height = codec->getInfo().height();
716 const int divisor = 2;
717 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800718 return Error::Nonfatal("Divisor is larger than image dimension.");
msarett3d9d7a72015-10-21 10:27:10 -0700719 }
720
msarettfa23a9e2015-10-21 13:26:59 -0700721 // Keep track of the final decoded dimensions.
722 int finalScaledWidth = 0;
723 int finalScaledHeight = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700724 for (int x = 0; x < divisor; x++) {
725 for (int y = 0; y < divisor; y++) {
726 // Calculate the subset dimensions
727 int subsetWidth = width / divisor;
728 int subsetHeight = height / divisor;
729 const int left = x * subsetWidth;
730 const int top = y * subsetHeight;
731
732 // Increase the size of the last subset in each row or column, when the
733 // divisor does not divide evenly into the image dimensions
734 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
735 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
736 SkIRect subset = SkIRect::MakeXYWH(left, top, subsetWidth, subsetHeight);
737 if (!codec->getSupportedSubset(&subset)) {
mtklein9b439152015-12-09 13:02:26 -0800738 return "Could not get supported subset to decode.";
msarett3d9d7a72015-10-21 10:27:10 -0700739 }
740 options.fSubset = &subset;
msarettfa23a9e2015-10-21 13:26:59 -0700741 const int scaledWidthOffset = subset.left() / fSampleSize;
742 const int scaledHeightOffset = subset.top() / fSampleSize;
743 void* pixels = bitmap.getAddr(scaledWidthOffset, scaledHeightOffset);
msarett3d9d7a72015-10-21 10:27:10 -0700744 SkISize scaledSubsetSize = codec->getSampledSubsetDimensions(fSampleSize,
745 subset);
746 SkImageInfo subsetDecodeInfo = decodeInfo.makeWH(scaledSubsetSize.width(),
747 scaledSubsetSize.height());
748
msarettfa23a9e2015-10-21 13:26:59 -0700749 if (x + 1 == divisor && y + 1 == divisor) {
750 finalScaledWidth = scaledWidthOffset + scaledSubsetSize.width();
751 finalScaledHeight = scaledHeightOffset + scaledSubsetSize.height();
752 }
753
msarett3d9d7a72015-10-21 10:27:10 -0700754 switch (codec->getAndroidPixels(subsetDecodeInfo, pixels, bitmap.rowBytes(),
755 &options)) {
756 case SkCodec::kSuccess:
757 case SkCodec::kIncompleteInput:
758 break;
759 case SkCodec::kInvalidConversion:
mtklein9b439152015-12-09 13:02:26 -0800760 return Error::Nonfatal("Cannot convert to requested color type.");
msarett3d9d7a72015-10-21 10:27:10 -0700761 default:
762 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
763 }
764 }
765 }
msarettfa23a9e2015-10-21 13:26:59 -0700766
767 SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth,
768 (SkScalar) finalScaledHeight);
msarett9e9444c2016-02-03 12:39:10 -0800769 premultiply_if_necessary(bitmap);
msarettfa23a9e2015-10-21 13:26:59 -0700770 canvas->drawBitmapRect(bitmap, rect, rect, nullptr);
msarett3d9d7a72015-10-21 10:27:10 -0700771 return "";
772 }
773 default:
774 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800775 return "Error: Should not be reached.";
msarett3d9d7a72015-10-21 10:27:10 -0700776 }
777}
778
779SkISize AndroidCodecSrc::size() const {
780 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
781 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
782 if (nullptr == codec) {
783 return SkISize::Make(0, 0);
784 }
785 return codec->getSampledDimensions(fSampleSize);
786}
787
788Name AndroidCodecSrc::name() const {
789 // We will replicate the names used by CodecSrc so that images can
790 // be compared in Gold.
791 if (1 == fSampleSize) {
792 return SkOSPath::Basename(fPath.c_str());
793 }
msarett4b0778e2015-11-13 09:59:11 -0800794 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700795}
796
797/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
798
mtkleinf4ba3212015-01-28 15:32:24 -0800799static const SkRect kSKPViewport = {0,0, 1000,1000};
800
mtklein8d17a132015-01-30 11:42:31 -0800801SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800802
803Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800804 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800805 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800806 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
807 }
kjlubick1de415f2016-02-10 11:25:07 -0800808 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap));
mtklein75d98fd2015-01-18 07:05:01 -0800809 if (!pic) {
810 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
811 }
halcanary96fcdcc2015-08-27 07:41:13 -0700812 stream.reset((SkStream*)nullptr); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -0700813
mtkleinf4ba3212015-01-28 15:32:24 -0800814 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800815 canvas->drawPicture(pic);
816 return "";
817}
818
819SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700820 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
821 if (!stream) {
822 return SkISize::Make(0,0);
823 }
824 SkPictInfo info;
825 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
826 return SkISize::Make(0,0);
827 }
828 SkRect viewport = kSKPViewport;
829 if (!viewport.intersect(info.fCullRect)) {
830 return SkISize::Make(0,0);
831 }
832 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800833}
834
835Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
836
837/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
838
mtkleinad66f9b2015-02-13 15:11:10 -0800839Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
840 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
841 return src.draw(canvas);
842}
843
844/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
845
mtkleinb9eb4ac2015-02-02 18:26:03 -0800846DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
847
mtklein82d28432015-01-15 12:46:02 -0800848GPUSink::GPUSink(GrContextFactory::GLContextType ct,
kkinnunen5219fd92015-12-10 06:28:13 -0800849 GrContextFactory::GLContextOptions options,
mtklein82d28432015-01-15 12:46:02 -0800850 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -0700851 bool diText,
mtklein82d28432015-01-15 12:46:02 -0800852 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800853 : fContextType(ct)
kkinnunen5219fd92015-12-10 06:28:13 -0800854 , fContextOptions(options)
mtklein748ca3b2015-01-15 10:56:12 -0800855 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -0700856 , fUseDIText(diText)
mtklein82d28432015-01-15 12:46:02 -0800857 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800858
joshualitt5f5a8d72015-02-25 14:09:45 -0800859void PreAbandonGpuContextErrorHandler(SkError, void*) {}
860
bsalomon648c6962015-10-23 09:06:59 -0700861DEFINE_bool(imm, false, "Run gpu configs in immediate mode.");
bsalomon69cfe952015-11-30 13:27:47 -0800862DEFINE_bool(batchClip, false, "Clip each GrBatch to its device bounds for testing.");
bsalomon6dea83f2015-12-03 12:58:06 -0800863DEFINE_bool(batchBounds, false, "Draw a wireframe bounds of each GrBatch.");
bsalomon489147c2015-12-14 12:13:09 -0800864DEFINE_int32(batchLookback, -1, "Maximum GrBatch lookback for combining, negative means default.");
bsalomon648c6962015-10-23 09:06:59 -0700865
mtkleinb9eb4ac2015-02-02 18:26:03 -0800866Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
kkinnunen5219fd92015-12-10 06:28:13 -0800867 GrContextOptions grOptions;
bsalomon489147c2015-12-14 12:13:09 -0800868 grOptions.fImmediateMode = FLAGS_imm;
869 grOptions.fClipBatchToBounds = FLAGS_batchClip;
870 grOptions.fDrawBatchBounds = FLAGS_batchBounds;
871 grOptions.fMaxBatchLookback = FLAGS_batchLookback;
kkinnunen64492c42015-12-08 01:24:40 -0800872
kkinnunen5219fd92015-12-10 06:28:13 -0800873 src.modifyGrContextOptions(&grOptions);
874
875 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -0800876 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800877 const SkImageInfo info =
878 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
msarett13a036b2016-02-08 09:10:47 -0800879#if SK_SUPPORT_GPU
880 const int maxDimension = factory.getContextInfo(fContextType, fContextOptions).
881 fGrContext->caps()->maxTextureSize();
882 if (maxDimension < SkTMax(size.width(), size.height())) {
883 return Error::Nonfatal("Src too large to create a texture.\n");
884 }
885#endif
886
mtklein748ca3b2015-01-15 10:56:12 -0800887 SkAutoTUnref<SkSurface> surface(
kkinnunen3e980c32015-12-23 01:33:00 -0800888 NewGpuSurface(&factory, fContextType, fContextOptions, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -0800889 if (!surface) {
890 return "Could not create a surface.";
891 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800892 if (FLAGS_preAbandonGpuContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700893 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr);
joshualitt5f5a8d72015-02-25 14:09:45 -0800894 factory.abandonContexts();
895 }
mtklein748ca3b2015-01-15 10:56:12 -0800896 SkCanvas* canvas = surface->getCanvas();
897 Error err = src.draw(canvas);
898 if (!err.isEmpty()) {
899 return err;
900 }
901 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800902 if (FLAGS_gpuStats) {
903 canvas->getGrContext()->dumpCacheStats(log);
904 canvas->getGrContext()->dumpGpuStats(log);
905 }
mtklein748ca3b2015-01-15 10:56:12 -0800906 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800907 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800908 if (FLAGS_abandonGpuContext) {
909 factory.abandonContexts();
910 }
mtklein748ca3b2015-01-15 10:56:12 -0800911 return "";
912}
913
914/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
915
halcanary47ef4d52015-03-03 09:13:09 -0800916static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
917 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
918 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800919 int width = src.size().width(),
920 height = src.size().height();
921
halcanary7e798182015-04-14 14:06:18 -0700922 if (FLAGS_multiPage) {
923 const int kLetterWidth = 612, // 8.5 * 72
924 kLetterHeight = 792; // 11 * 72
925 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
926 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -0800927
halcanary7e798182015-04-14 14:06:18 -0700928 int xPages = ((width - 1) / kLetterWidth) + 1;
929 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -0800930
halcanary7e798182015-04-14 14:06:18 -0700931 for (int y = 0; y < yPages; ++y) {
932 for (int x = 0; x < xPages; ++x) {
933 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
934 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
935 SkCanvas* canvas =
936 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
937 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700938 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700939 }
940 canvas->clipRect(letter);
941 canvas->translate(-letter.width() * x, -letter.height() * y);
942 Error err = src.draw(canvas);
943 if (!err.isEmpty()) {
944 return err;
945 }
946 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -0700947 }
halcanaryfd4a9932015-01-28 11:45:58 -0800948 }
halcanary7e798182015-04-14 14:06:18 -0700949 } else {
950 SkCanvas* canvas =
951 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
952 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700953 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700954 }
955 Error err = src.draw(canvas);
956 if (!err.isEmpty()) {
957 return err;
958 }
959 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -0800960 }
halcanary7e798182015-04-14 14:06:18 -0700961 if (!doc->close()) {
962 return "SkDocument::close() returned false";
963 }
halcanaryfd4a9932015-01-28 11:45:58 -0800964 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800965 return "";
966}
967
halcanaryc11c62f2015-09-28 11:51:54 -0700968PDFSink::PDFSink(const char* rasterizer) : fRasterizer(rasterizer) {}
halcanary47ef4d52015-03-03 09:13:09 -0800969
970Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
971 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
972 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700973 return "SkDocument::CreatePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800974 }
halcanaryf12a1672015-09-23 12:45:49 -0700975 SkTArray<SkDocument::Attribute> info;
976 info.emplace_back(SkString("Title"), src.name());
977 info.emplace_back(SkString("Subject"),
978 SkString("rendering correctness test"));
979 info.emplace_back(SkString("Creator"), SkString("Skia/DM"));
halcanaryc11c62f2015-09-28 11:51:54 -0700980
981 info.emplace_back(SkString("Keywords"),
982 SkStringPrintf("Rasterizer:%s;", fRasterizer));
halcanary70015762016-02-11 07:59:59 -0800983 doc->setMetadata(&info[0], info.count(), nullptr, nullptr);
halcanary47ef4d52015-03-03 09:13:09 -0800984 return draw_skdocument(src, doc.get(), dst);
985}
986
987/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
988
989XPSSink::XPSSink() {}
990
991Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
992 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
993 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700994 return "SkDocument::CreateXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800995 }
996 return draw_skdocument(src, doc.get(), dst);
997}
mtklein748ca3b2015-01-15 10:56:12 -0800998/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
999
mtklein9c3f17d2015-01-28 11:35:18 -08001000SKPSink::SKPSink() {}
1001
mtkleinb9eb4ac2015-02-02 18:26:03 -08001002Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001003 SkSize size;
1004 size = src.size();
1005 SkPictureRecorder recorder;
1006 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1007 if (!err.isEmpty()) {
1008 return err;
1009 }
1010 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
1011 pic->serialize(dst);
1012 return "";
1013}
1014
1015/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1016
mtklein8a4527e2015-01-31 20:00:58 -08001017SVGSink::SVGSink() {}
1018
mtkleinb9eb4ac2015-02-02 18:26:03 -08001019Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary385fe4d2015-08-26 13:07:48 -07001020 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
fmalita2aafe6f2015-02-06 12:51:10 -08001021 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
1022 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
1023 xmlWriter));
1024 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -08001025}
1026
1027/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1028
mtklein748ca3b2015-01-15 10:56:12 -08001029RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
1030
mtkleinb9eb4ac2015-02-02 18:26:03 -08001031Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001032 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001033 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1034 SkAlphaType alphaType = kPremul_SkAlphaType;
1035 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1036
mtkleinc8be09a2016-01-04 18:56:57 -08001037 SkMallocPixelRef::ZeroedPRFactory factory;
1038 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType),
1039 &factory,
1040 nullptr/*colortable*/);
mtklein748ca3b2015-01-15 10:56:12 -08001041 SkCanvas canvas(*dst);
1042 return src.draw(&canvas);
1043}
1044
1045/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1046
mtkleina16e69e2015-05-05 11:38:45 -07001047// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001048// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001049// Several examples below.
1050
msarett62d3b102015-12-10 15:14:27 -08001051static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtklein64593522015-11-12 10:41:05 -08001052 SkISize size, std::function<Error(SkCanvas*)> draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001053 class ProxySrc : public Src {
1054 public:
msarett62d3b102015-12-10 15:14:27 -08001055 ProxySrc(SkISize size, std::function<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001056 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
msarett62d3b102015-12-10 15:14:27 -08001057 Name name() const override { sk_throw(); return ""; } // Won't be called.
mtkleina16e69e2015-05-05 11:38:45 -07001058 SkISize size() const override { return fSize; }
1059 private:
mtklein64593522015-11-12 10:41:05 -08001060 SkISize fSize;
1061 std::function<Error(SkCanvas*)> fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001062 };
msarett62d3b102015-12-10 15:14:27 -08001063 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001064}
1065
1066/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1067
mtklein4a34ecb2016-01-08 10:19:35 -08001068DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1069
1070// Is *bitmap identical to what you get drawing src into sink?
1071static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1072 // We can only check raster outputs.
1073 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1074 if (FLAGS_check && bitmap) {
1075 SkBitmap reference;
1076 SkString log;
1077 Error err = sink->draw(src, &reference, nullptr, &log);
1078 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1079 SkASSERT(err.isEmpty());
1080 if (!err.isEmpty()) {
1081 return err;
1082 }
1083 // The dimensions are a property of the Src only, and so should be identical.
1084 SkASSERT(reference.getSize() == bitmap->getSize());
1085 if (reference.getSize() != bitmap->getSize()) {
1086 return "Dimensions don't match reference";
1087 }
1088 // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy.
1089 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1090 return "Pixels don't match reference";
1091 }
1092 }
1093 return "";
1094}
1095
1096/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1097
mtkleind603b222015-02-17 11:13:33 -08001098static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1099 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1100 matrix->mapRect(&bounds);
1101 matrix->postTranslate(-bounds.x(), -bounds.y());
1102 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
1103}
1104
msarett62d3b102015-12-10 15:14:27 -08001105ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001106
mtkleinb9eb4ac2015-02-02 18:26:03 -08001107Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001108 SkMatrix matrix = fMatrix;
1109 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
msarett62d3b102015-12-10 15:14:27 -08001110 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001111 canvas->concat(matrix);
1112 return src.draw(canvas);
1113 });
mtklein748ca3b2015-01-15 10:56:12 -08001114}
1115
mtkleind603b222015-02-17 11:13:33 -08001116// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1117// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001118ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001119
1120Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1121 Error err = fSink->draw(src, bitmap, stream, log);
1122 if (!err.isEmpty()) {
1123 return err;
1124 }
1125
1126 SkMatrix inverse;
1127 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1128 return "Cannot upright --matrix.";
1129 }
1130 SkMatrix upright = SkMatrix::I();
1131 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1132 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1133 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1134 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1135
1136 SkBitmap uprighted;
1137 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1138 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1139
1140 SkCanvas canvas(uprighted);
1141 canvas.concat(upright);
1142 SkPaint paint;
1143 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1144 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1145
1146 *bitmap = uprighted;
1147 bitmap->lockPixels();
1148 return "";
1149}
1150
mtklein748ca3b2015-01-15 10:56:12 -08001151/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1152
mtklein2e2ea382015-10-16 10:29:41 -07001153Error ViaRemote::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001154 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* target) {
mtklein479fe772015-10-21 12:34:01 -07001155 SkAutoTDelete<SkRemote::Encoder> decoder(SkRemote::NewDecoder(target));
1156 SkAutoTDelete<SkRemote::Encoder> cache(fCache ? SkRemote::NewCachingEncoder(decoder)
1157 : nullptr);
1158 SkAutoTDelete<SkCanvas> canvas(SkRemote::NewCanvas(cache ? cache : decoder));
1159 return src.draw(canvas);
mtklein2e2ea382015-10-16 10:29:41 -07001160 });
1161}
1162
mtklein748ca3b2015-01-15 10:56:12 -08001163/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtkleina16e69e2015-05-05 11:38:45 -07001164
mtkleina16e69e2015-05-05 11:38:45 -07001165Error ViaSerialization::draw(
1166 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001167 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001168 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001169 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001170 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1171 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001172 if (!err.isEmpty()) {
1173 return err;
1174 }
1175 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
1176
1177 // Serialize it and then deserialize it.
1178 SkDynamicMemoryWStream wStream;
1179 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -08001180 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
kjlubick1de415f2016-02-10 11:25:07 -08001181 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap));
mtklein748ca3b2015-01-15 10:56:12 -08001182
msarett62d3b102015-12-10 15:14:27 -08001183 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001184 canvas->drawPicture(deserialized);
mtklein4a34ecb2016-01-08 10:19:35 -08001185 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001186 });
mtklein748ca3b2015-01-15 10:56:12 -08001187}
1188
1189/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1190
msarett62d3b102015-12-10 15:14:27 -08001191ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1192 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001193 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001194 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001195 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001196
mtkleinb9eb4ac2015-02-02 18:26:03 -08001197Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001198 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001199 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001200 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1201 SkIntToScalar(size.height()),
1202 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001203 if (!err.isEmpty()) {
1204 return err;
1205 }
mtkleinb7e8d692015-04-07 08:30:32 -07001206 SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001207
msarett62d3b102015-12-10 15:14:27 -08001208 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001209 const int xTiles = (size.width() + fW - 1) / fW,
1210 yTiles = (size.height() + fH - 1) / fH;
1211 SkMultiPictureDraw mpd(xTiles*yTiles);
1212 SkTDArray<SkSurface*> surfaces;
1213 surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001214
mtkleina16e69e2015-05-05 11:38:45 -07001215 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1216 for (int j = 0; j < yTiles; j++) {
1217 for (int i = 0; i < xTiles; i++) {
1218 // This lets our ultimate Sink determine the best kind of surface.
1219 // E.g., if it's a GpuSink, the surfaces and images are textures.
1220 SkSurface* s = canvas->newSurface(info);
1221 if (!s) {
1222 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001223 }
mtkleina16e69e2015-05-05 11:38:45 -07001224 surfaces.push(s);
1225 SkCanvas* c = s->getCanvas();
1226 c->translate(SkIntToScalar(-i * fW),
1227 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
1228 mpd.add(c, pic);
mtklein748ca3b2015-01-15 10:56:12 -08001229 }
mtklein748ca3b2015-01-15 10:56:12 -08001230 }
mtkleina16e69e2015-05-05 11:38:45 -07001231 mpd.draw();
1232 for (int j = 0; j < yTiles; j++) {
1233 for (int i = 0; i < xTiles; i++) {
1234 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
1235 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1236 }
1237 }
1238 surfaces.unrefAll();
1239 return "";
1240 });
mtklein748ca3b2015-01-15 10:56:12 -08001241}
1242
mtkleinb7e8d692015-04-07 08:30:32 -07001243/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1244
mtklein4a34ecb2016-01-08 10:19:35 -08001245Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1246 auto size = src.size();
1247 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1248 SkPictureRecorder recorder;
1249 SkAutoTUnref<SkPicture> pic;
1250 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1251 SkIntToScalar(size.height())));
1252 if (!err.isEmpty()) {
1253 return err;
1254 }
1255 pic.reset(recorder.endRecordingAsPicture());
1256 canvas->drawPicture(pic);
1257 return check_against_reference(bitmap, src, fSink);
1258 });
1259}
1260
1261/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1262
mtkleinb7e8d692015-04-07 08:30:32 -07001263// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1264// This tests that any shortcuts we may take while recording that second picture are legal.
1265Error ViaSecondPicture::draw(
1266 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001267 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001268 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001269 SkPictureRecorder recorder;
1270 SkAutoTUnref<SkPicture> pic;
1271 for (int i = 0; i < 2; i++) {
1272 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1273 SkIntToScalar(size.height())));
1274 if (!err.isEmpty()) {
1275 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001276 }
mtkleina16e69e2015-05-05 11:38:45 -07001277 pic.reset(recorder.endRecordingAsPicture());
mtkleinb7e8d692015-04-07 08:30:32 -07001278 }
mtkleina16e69e2015-05-05 11:38:45 -07001279 canvas->drawPicture(pic);
mtklein4a34ecb2016-01-08 10:19:35 -08001280 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001281 });
mtkleinb7e8d692015-04-07 08:30:32 -07001282}
1283
mtkleind31c13d2015-05-05 12:59:56 -07001284/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1285
mtklein6fbf4b32015-05-06 11:35:40 -07001286// Draw the Src twice. This can help exercise caching.
1287Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001288 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001289 for (int i = 0; i < 2; i++) {
1290 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1291 canvas->clear(SK_ColorTRANSPARENT);
1292 Error err = src.draw(canvas);
1293 if (err.isEmpty()) {
1294 return err;
1295 }
1296 }
mtklein4a34ecb2016-01-08 10:19:35 -08001297 return check_against_reference(bitmap, src, fSink);
mtklein6fbf4b32015-05-06 11:35:40 -07001298 });
1299}
1300
1301/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1302
halcanary7a76f9c2016-02-03 11:53:18 -08001303#ifdef SK_MOJO
1304 Error ViaMojo::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1305 SkPictureRecorder recorder;
1306 SkRect size = SkRect::Make(SkIRect::MakeSize(src.size()));
1307 Error err = src.draw(recorder.beginRecording(size));
1308 if (!err.isEmpty()) {
1309 return err;
1310 }
1311 SkAutoTUnref<SkPicture> skPicture(recorder.endRecording());
1312
1313 SkASSERT(skPicture);
1314 SkDynamicMemoryWStream buffer;
1315 skPicture->serialize(&buffer);
1316 skPicture.reset();
1317 SkMojo::FlattenedPicturePtr mojoPicture = SkMojo::FlattenedPicture::New();
1318 mojoPicture->data.resize(buffer.bytesWritten());
1319 buffer.copyTo(mojoPicture->data.data());
1320 buffer.reset();
1321 SkASSERT(mojoPicture.get() && mojoPicture->data);
1322
1323 size_t flatSize = mojoPicture->GetSerializedSize();
1324 SkAutoMalloc storage(flatSize);
1325 if (!mojoPicture->Serialize(storage.get(), flatSize)) {
1326 return "SkMojo::FlattenedPicture::Serialize failed";
1327 }
1328 mojoPicture = SkMojo::FlattenedPicture::New();
1329 mojoPicture->Deserialize(storage.get());
1330 storage.free();
1331 if (!mojoPicture) {
1332 return "SkMojo::FlattenedPicture::Deserialize failed";
1333 }
1334 SkMemoryStream tmpStream(mojoPicture->data.data(),
1335 mojoPicture->data.size());
1336 skPicture.reset(SkPicture::CreateFromStream(&tmpStream));
1337 mojoPicture.reset();
1338 auto fn = [&](SkCanvas* canvas) -> Error {
1339 canvas->drawPicture(skPicture.get());
1340 return check_against_reference(bitmap, src, fSink);
1341 };
1342 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), fn);
1343 }
1344#else // not SK_MOJO
1345 Error ViaMojo::draw(const Src&, SkBitmap*, SkWStream*, SkString*) const {
1346 return "Mojo is missing!";
1347 }
1348#endif
1349
1350/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1351
mtkleind31c13d2015-05-05 12:59:56 -07001352// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1353// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1354// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1355struct DrawsAsSingletonPictures {
1356 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001357 const SkDrawableList& fDrawables;
mtkleind31c13d2015-05-05 12:59:56 -07001358
mtkleind31c13d2015-05-05 12:59:56 -07001359 template <typename T>
1360 void draw(const T& op, SkCanvas* canvas) {
1361 // We must pass SkMatrix::I() as our initial matrix.
1362 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1363 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001364 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1365 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001366 }
1367
mtklein449d9b72015-09-28 10:33:02 -07001368 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001369 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001370 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001371 SkPictureRecorder rec;
1372 this->draw(op, rec.beginRecording(SkRect::MakeLargest()));
1373 SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture());
1374 fCanvas->drawPicture(pic);
1375 }
1376
mtklein449d9b72015-09-28 10:33:02 -07001377 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001378 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001379 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1380 this->draw(op, fCanvas);
1381 }
mtkleind31c13d2015-05-05 12:59:56 -07001382};
1383
mtkleind31c13d2015-05-05 12:59:56 -07001384// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1385// Then play back that macro picture into our wrapped sink.
1386Error ViaSingletonPictures::draw(
1387 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1388 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001389 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07001390 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1391 SkRecord skr;
1392 SkRecorder recorder(&skr, size.width(), size.height());
1393 Error err = src.draw(&recorder);
1394 if (!err.isEmpty()) {
1395 return err;
1396 }
1397
1398 // Record our macro-picture, with each draw op as its own sub-picture.
1399 SkPictureRecorder macroRec;
1400 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1401 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001402
1403 SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
1404 const SkDrawableList empty;
1405
1406 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1407 macroCanvas,
1408 drawables ? *drawables : empty,
1409 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001410 for (int i = 0; i < skr.count(); i++) {
mtkleind31c13d2015-05-05 12:59:56 -07001411 skr.visit<void>(i, drawsAsSingletonPictures);
1412 }
1413 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture());
1414
1415 canvas->drawPicture(macroPic);
mtklein4a34ecb2016-01-08 10:19:35 -08001416 return check_against_reference(bitmap, src, fSink);
mtkleind31c13d2015-05-05 12:59:56 -07001417 });
1418}
1419
mtklein748ca3b2015-01-15 10:56:12 -08001420} // namespace DM