blob: c99f3260817413427e4741d5791e77c435ae40cb [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMSrcSink.h"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
scroggof24f2242015-03-03 08:59:20 -080010#include "SkCodec.h"
msarettb714fb02016-01-22 14:46:42 -080011#include "SkCodecImageGenerator.h"
mtkleina16e69e2015-05-05 11:38:45 -070012#include "SkCommonFlags.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070013#include "SkData.h"
mtklein748ca3b2015-01-15 10:56:12 -080014#include "SkDocument.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080015#include "SkError.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070016#include "SkImageGenerator.h"
msarett18976312016-03-09 14:20:58 -080017#include "SkImageGeneratorCG.h"
msarettfc0b6d12016-03-17 13:50:17 -070018#include "SkImageGeneratorWIC.h"
mtkleinc8be09a2016-01-04 18:56:57 -080019#include "SkMallocPixelRef.h"
mtklein748ca3b2015-01-15 10:56:12 -080020#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080021#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080022#include "SkOSFile.h"
msarett9e9444c2016-02-03 12:39:10 -080023#include "SkOpts.h"
mtkleinffa901a2015-03-16 10:38:07 -070024#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080025#include "SkPictureRecorder.h"
26#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070027#include "SkRecordDraw.h"
28#include "SkRecorder.h"
fmalita2aafe6f2015-02-06 12:51:10 -080029#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080030#include "SkStream.h"
mtklein449d9b72015-09-28 10:33:02 -070031#include "SkTLogic.h"
fmalita2aafe6f2015-02-06 12:51:10 -080032#include "SkXMLWriter.h"
msarette6dd0042015-10-09 11:07:34 -070033#include "SkSwizzler.h"
mtklein64593522015-11-12 10:41:05 -080034#include <functional>
mtklein748ca3b2015-01-15 10:56:12 -080035
msarettfc0b6d12016-03-17 13:50:17 -070036#if defined(SK_BUILD_FOR_WIN)
37 #include "SkAutoCoInitialize.h"
38#endif
39
halcanary7e798182015-04-14 14:06:18 -070040DEFINE_bool(multiPage, false, "For document-type backends, render the source"
41 " into multiple pages");
scroggo3ac66e92016-02-08 15:09:48 -080042DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070043
bsalomon3724e572016-03-30 18:56:19 -070044using sk_gpu_test::GrContextFactory;
45
mtklein748ca3b2015-01-15 10:56:12 -080046namespace DM {
47
mtklein748ca3b2015-01-15 10:56:12 -080048GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
49
50Error GMSrc::draw(SkCanvas* canvas) const {
halcanary96fcdcc2015-08-27 07:41:13 -070051 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080052 canvas->concat(gm->getInitialTransform());
53 gm->draw(canvas);
54 return "";
55}
56
57SkISize GMSrc::size() const {
halcanary96fcdcc2015-08-27 07:41:13 -070058 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080059 return gm->getISize();
60}
61
62Name GMSrc::name() const {
halcanary96fcdcc2015-08-27 07:41:13 -070063 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080064 return gm->getName();
65}
66
bsalomon4ee6bd82015-05-27 13:23:23 -070067void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
halcanary96fcdcc2015-08-27 07:41:13 -070068 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070069 gm->modifyGrContextOptions(options);
70}
71
mtklein748ca3b2015-01-15 10:56:12 -080072/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
73
msarett5cb48852015-11-06 08:56:32 -080074BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode,
msaretta5783ae2015-09-08 15:35:32 -070075 CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
76 : fPath(path)
77 , fStrategy(strategy)
78 , fMode(mode)
79 , fDstColorType(dstColorType)
80 , fSampleSize(sampleSize)
81{}
82
83bool BRDSrc::veto(SinkFlags flags) const {
84 // No need to test to non-raster or indirect backends.
85 return flags.type != SinkFlags::kRaster
86 || flags.approach != SinkFlags::kDirect;
87}
88
msarett5cb48852015-11-06 08:56:32 -080089static SkBitmapRegionDecoder* create_brd(Path path,
90 SkBitmapRegionDecoder::Strategy strategy) {
msaretta5783ae2015-09-08 15:35:32 -070091 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
92 if (!encoded) {
93 return NULL;
94 }
msarett5cb48852015-11-06 08:56:32 -080095 return SkBitmapRegionDecoder::Create(encoded, strategy);
msaretta5783ae2015-09-08 15:35:32 -070096}
97
98Error BRDSrc::draw(SkCanvas* canvas) const {
99 SkColorType colorType = canvas->imageInfo().colorType();
100 if (kRGB_565_SkColorType == colorType &&
101 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
102 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
103 }
104 switch (fDstColorType) {
105 case CodecSrc::kGetFromCanvas_DstColorType:
106 break;
107 case CodecSrc::kIndex8_Always_DstColorType:
108 colorType = kIndex_8_SkColorType;
109 break;
110 case CodecSrc::kGrayscale_Always_DstColorType:
111 colorType = kGray_8_SkColorType;
112 break;
113 }
114
msarett5cb48852015-11-06 08:56:32 -0800115 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700116 if (nullptr == brd.get()) {
117 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
118 }
119
msarett04965c62015-10-12 10:24:38 -0700120 if (!brd->conversionSupported(colorType)) {
mtklein9b439152015-12-09 13:02:26 -0800121 return Error::Nonfatal("Cannot convert to color type.");
msarett04965c62015-10-12 10:24:38 -0700122 }
123
msaretta5783ae2015-09-08 15:35:32 -0700124 const uint32_t width = brd->width();
125 const uint32_t height = brd->height();
126 // Visually inspecting very small output images is not necessary.
127 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
128 return Error::Nonfatal("Scaling very small images is uninteresting.");
129 }
130 switch (fMode) {
131 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700132 SkBitmap bitmap;
133 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
134 fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800135 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700136 }
137 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800138 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700139 }
msarett35e5d1b2015-10-27 12:50:25 -0700140 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700141 return "";
142 }
143 case kDivisor_Mode: {
144 const uint32_t divisor = 2;
145 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800146 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700147 }
148
149 // Use a border to test subsets that extend outside the image.
150 // We will not allow the border to be larger than the image dimensions. Allowing
151 // these large borders causes off by one errors that indicate a problem with the
152 // test suite, not a problem with the implementation.
153 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
154 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
155 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
156
157 // We may need to clear the canvas to avoid uninitialized memory.
158 // Assume we are scaling a 780x780 image with sampleSize = 8.
159 // The output image should be 97x97.
160 // Each subset will be 390x390.
161 // Each scaled subset be 48x48.
162 // Four scaled subsets will only fill a 96x96 image.
163 // The bottom row and last column will not be touched.
164 // This is an unfortunate result of our rounding rules when scaling.
165 // Maybe we need to consider testing scaled subsets without trying to
166 // combine them to match the full scaled image? Or maybe this is the
167 // best we can do?
168 canvas->clear(0);
169
170 for (uint32_t x = 0; x < divisor; x++) {
171 for (uint32_t y = 0; y < divisor; y++) {
172 // Calculate the subset dimensions
173 uint32_t subsetWidth = width / divisor;
174 uint32_t subsetHeight = height / divisor;
175 const int left = x * subsetWidth;
176 const int top = y * subsetHeight;
177
178 // Increase the size of the last subset in each row or column, when the
179 // divisor does not divide evenly into the image dimensions
180 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
181 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
182
183 // Increase the size of the subset in order to have a border on each side
184 const int decodeLeft = left - unscaledBorder;
185 const int decodeTop = top - unscaledBorder;
186 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
187 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700188 SkBitmap bitmap;
189 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
190 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800191 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700192 }
193 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800194 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700195 }
196
msarett35e5d1b2015-10-27 12:50:25 -0700197 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700198 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
199 (SkScalar) (subsetWidth / fSampleSize),
200 (SkScalar) (subsetHeight / fSampleSize)),
201 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
202 (SkScalar) (top / fSampleSize),
203 (SkScalar) (subsetWidth / fSampleSize),
204 (SkScalar) (subsetHeight / fSampleSize)),
205 nullptr);
206 }
207 }
208 return "";
209 }
210 default:
211 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800212 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700213 }
214}
215
216SkISize BRDSrc::size() const {
msarett5cb48852015-11-06 08:56:32 -0800217 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700218 if (brd) {
219 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
220 SkTMax(1, brd->height() / (int) fSampleSize));
221 }
222 return SkISize::Make(0, 0);
223}
224
225static SkString get_scaled_name(const Path& path, float scale) {
226 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
227}
228
229Name BRDSrc::name() const {
230 // We will replicate the names used by CodecSrc so that images can
231 // be compared in Gold.
232 if (1 == fSampleSize) {
233 return SkOSPath::Basename(fPath.c_str());
234 }
msarett4b0778e2015-11-13 09:59:11 -0800235 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700236}
237
238/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
239
scroggo3ac66e92016-02-08 15:09:48 -0800240static bool serial_from_path_name(const SkString& path) {
241 if (!FLAGS_RAW_threading) {
242 static const char* const exts[] = {
243 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
244 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
245 };
246 const char* actualExt = strrchr(path.c_str(), '.');
247 if (actualExt) {
248 actualExt++;
249 for (auto* ext : exts) {
250 if (0 == strcmp(ext, actualExt)) {
251 return true;
252 }
253 }
254 }
255 }
256 return false;
257}
258
scroggoc5560be2016-02-03 09:42:42 -0800259CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
260 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700261 : fPath(path)
262 , fMode(mode)
263 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800264 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700265 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800266 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700267{}
mtklein748ca3b2015-01-15 10:56:12 -0800268
mtklein99cab4e2015-07-31 06:43:04 -0700269bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800270 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800271 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700272}
scroggo9b77ddd2015-03-19 06:03:39 -0700273
msarett9e9444c2016-02-03 12:39:10 -0800274// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
275// This allows us to still test unpremultiplied decodes.
276void premultiply_if_necessary(SkBitmap& bitmap) {
277 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
278 return;
279 }
280
281 switch (bitmap.colorType()) {
282 case kN32_SkColorType:
283 for (int y = 0; y < bitmap.height(); y++) {
284 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
285 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
286 }
287 break;
288 case kIndex_8_SkColorType: {
289 SkColorTable* colorTable = bitmap.getColorTable();
290 SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
291 SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count());
292 break;
293 }
294 default:
295 // No need to premultiply kGray or k565 outputs.
296 break;
297 }
msarette1daa482016-02-03 15:31:18 -0800298
299 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
300 // bitmap as kPremul.
301 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800302}
303
scroggoc5560be2016-02-03 09:42:42 -0800304bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
305 CodecSrc::DstColorType dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700306 switch (dstColorType) {
307 case CodecSrc::kIndex8_Always_DstColorType:
308 if (kRGB_565_SkColorType == canvasColorType) {
309 return false;
310 }
scroggoc5560be2016-02-03 09:42:42 -0800311 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700312 break;
313 case CodecSrc::kGrayscale_Always_DstColorType:
msarett55f7bdd2016-02-16 13:24:54 -0800314 if (kRGB_565_SkColorType == canvasColorType ||
315 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
msarett3d9d7a72015-10-21 10:27:10 -0700316 return false;
317 }
scroggoc5560be2016-02-03 09:42:42 -0800318 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700319 break;
320 default:
msarett55f7bdd2016-02-16 13:24:54 -0800321 if (kRGB_565_SkColorType == canvasColorType &&
322 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
323 return false;
324 }
scroggoc5560be2016-02-03 09:42:42 -0800325 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700326 break;
327 }
328
msarett3d9d7a72015-10-21 10:27:10 -0700329 return true;
330}
331
mtkleine0effd62015-07-29 06:37:28 -0700332Error CodecSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -0800333 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800334 if (!encoded) {
335 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
336 }
msarettb714fb02016-01-22 14:46:42 -0800337
msarett3d9d7a72015-10-21 10:27:10 -0700338 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700339 if (nullptr == codec.get()) {
340 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700341 }
342
scroggoc5560be2016-02-03 09:42:42 -0800343 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
344 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700345 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
msarett438b2ad2015-04-09 12:43:10 -0700346 }
347
msarett0a242972015-06-11 14:27:27 -0700348 // Try to scale the image if it is desired
349 SkISize size = codec->getScaledDimensions(fScale);
350 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
351 return Error::Nonfatal("Test without scaling is uninteresting.");
352 }
msarettb32758a2015-08-18 13:22:46 -0700353
354 // Visually inspecting very small output images is not necessary. We will
355 // cover these cases in unit testing.
356 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
357 return Error::Nonfatal("Scaling very small images is uninteresting.");
358 }
msarett0a242972015-06-11 14:27:27 -0700359 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
360
msarett438b2ad2015-04-09 12:43:10 -0700361 // Construct a color table for the decode if necessary
halcanary96fcdcc2015-08-27 07:41:13 -0700362 SkAutoTUnref<SkColorTable> colorTable(nullptr);
363 SkPMColor* colorPtr = nullptr;
364 int* colorCountPtr = nullptr;
msarett438b2ad2015-04-09 12:43:10 -0700365 int maxColors = 256;
366 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
367 SkPMColor colors[256];
halcanary385fe4d2015-08-26 13:07:48 -0700368 colorTable.reset(new SkColorTable(colors, maxColors));
msarett438b2ad2015-04-09 12:43:10 -0700369 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
370 colorCountPtr = &maxColors;
371 }
372
scroggo9b77ddd2015-03-19 06:03:39 -0700373 SkBitmap bitmap;
msarettbb25b532016-01-13 09:31:39 -0800374 SkPixelRefFactory* factory = nullptr;
375 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
376 SkCodec::Options options;
377 if (kCodecZeroInit_Mode == fMode) {
378 factory = &zeroFactory;
379 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
380 }
381 if (!bitmap.tryAllocPixels(decodeInfo, factory, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800382 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
scroggo9b77ddd2015-03-19 06:03:39 -0700383 decodeInfo.width(), decodeInfo.height());
384 }
385
scroggo9c59ebc2015-03-25 13:48:49 -0700386 switch (fMode) {
msarettbb25b532016-01-13 09:31:39 -0800387 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700388 case kCodec_Mode: {
msarettbb25b532016-01-13 09:31:39 -0800389 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
msarett438b2ad2015-04-09 12:43:10 -0700390 colorPtr, colorCountPtr)) {
scroggoeb602a52015-07-09 08:16:03 -0700391 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700392 // We consider incomplete to be valid, since we should still decode what is
393 // available.
scroggoeb602a52015-07-09 08:16:03 -0700394 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700395 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700396 default:
397 // Everything else is considered a failure.
398 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
399 }
msarett9e9444c2016-02-03 12:39:10 -0800400 premultiply_if_necessary(bitmap);
emmaleer97002062015-05-27 12:36:10 -0700401 canvas->drawBitmap(bitmap, 0, 0);
scroggo9c59ebc2015-03-25 13:48:49 -0700402 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700403 }
scroggo9c59ebc2015-03-25 13:48:49 -0700404 case kScanline_Mode: {
scroggo46c57472015-09-30 08:57:13 -0700405 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
406 colorCountPtr)) {
msarettb65e6042016-02-23 05:37:25 -0800407 return "Could not start scanline decoder";
scroggo9c59ebc2015-03-25 13:48:49 -0700408 }
scroggo1c005e42015-08-04 09:24:45 -0700409
msarette6dd0042015-10-09 11:07:34 -0700410 void* dst = bitmap.getAddr(0, 0);
411 size_t rowBytes = bitmap.rowBytes();
412 uint32_t height = decodeInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700413 switch (codec->getScanlineOrder()) {
414 case SkCodec::kTopDown_SkScanlineOrder:
415 case SkCodec::kBottomUp_SkScanlineOrder:
416 case SkCodec::kNone_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700417 // We do not need to check the return value. On an incomplete
418 // image, memory will be filled with a default value.
419 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700420 break;
scroggo46c57472015-09-30 08:57:13 -0700421 case SkCodec::kOutOfOrder_SkScanlineOrder: {
msarett10522ff2015-09-07 08:54:01 -0700422 for (int y = 0; y < decodeInfo.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700423 int dstY = codec->outputScanline(y);
msarett10522ff2015-09-07 08:54:01 -0700424 void* dstPtr = bitmap.getAddr(0, dstY);
msarette6dd0042015-10-09 11:07:34 -0700425 // We complete the loop, even if this call begins to fail
426 // due to an incomplete image. This ensures any uninitialized
427 // memory will be filled with the proper value.
428 codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
msarett10522ff2015-09-07 08:54:01 -0700429 }
430 break;
431 }
432 }
433
msarett9e9444c2016-02-03 12:39:10 -0800434 premultiply_if_necessary(bitmap);
emmaleer97002062015-05-27 12:36:10 -0700435 canvas->drawBitmap(bitmap, 0, 0);
436 break;
437 }
msarett0a242972015-06-11 14:27:27 -0700438 case kStripe_Mode: {
439 const int height = decodeInfo.height();
440 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
441 // does not align with image blocks.
442 const int stripeHeight = 37;
443 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
444
445 // Decode odd stripes
scroggo46c57472015-09-30 08:57:13 -0700446 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
msarettb65e6042016-02-23 05:37:25 -0800447 colorCountPtr)) {
448 return "Could not start scanline decoder";
449 }
450
451 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
452 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
453 // to run this test for image types that do not have this scanline ordering.
454 if (SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
455 return Error::Nonfatal("kStripe test is only interesting for kTopDown codecs.");
msarett0a242972015-06-11 14:27:27 -0700456 }
msarette6dd0042015-10-09 11:07:34 -0700457
msarett0a242972015-06-11 14:27:27 -0700458 for (int i = 0; i < numStripes; i += 2) {
459 // Skip a stripe
460 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700461 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700462
463 // Read a stripe
464 const int startY = (i + 1) * stripeHeight;
465 const int linesToRead = SkTMin(stripeHeight, height - startY);
466 if (linesToRead > 0) {
msarette6dd0042015-10-09 11:07:34 -0700467 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700468 }
469 }
470
471 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700472 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
473 colorPtr, colorCountPtr);
scroggo1c005e42015-08-04 09:24:45 -0700474 if (SkCodec::kSuccess != startResult) {
475 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700476 }
477 for (int i = 0; i < numStripes; i += 2) {
478 // Read a stripe
479 const int startY = i * stripeHeight;
480 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarette6dd0042015-10-09 11:07:34 -0700481 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700482
483 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700484 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
485 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700486 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700487 }
488 }
msarett9e9444c2016-02-03 12:39:10 -0800489 premultiply_if_necessary(bitmap);
msarett0a242972015-06-11 14:27:27 -0700490 canvas->drawBitmap(bitmap, 0, 0);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700491 break;
msarett0a242972015-06-11 14:27:27 -0700492 }
msarett91c22b22016-02-22 12:27:46 -0800493 case kCroppedScanline_Mode: {
494 const int width = decodeInfo.width();
495 const int height = decodeInfo.height();
496 // This value is chosen because, as we move across the image, it will sometimes
497 // align with the jpeg block sizes and it will sometimes not. This allows us
498 // to test interestingly different code paths in the implementation.
499 const int tileSize = 36;
500
501 SkCodec::Options opts;
502 SkIRect subset;
503 for (int x = 0; x < width; x += tileSize) {
504 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
505 opts.fSubset = &subset;
506 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &opts,
507 colorPtr, colorCountPtr)) {
508 return "Could not start scanline decoder.";
509 }
510
511 codec->getScanlines(bitmap.getAddr(x, 0), height, bitmap.rowBytes());
512 }
513
514 premultiply_if_necessary(bitmap);
515 canvas->drawBitmap(bitmap, 0, 0);
516 break;
517 }
scroggob636b452015-07-22 07:16:20 -0700518 case kSubset_Mode: {
519 // Arbitrarily choose a divisor.
520 int divisor = 2;
521 // Total width/height of the image.
522 const int W = codec->getInfo().width();
523 const int H = codec->getInfo().height();
524 if (divisor > W || divisor > H) {
525 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
526 "for %s with dimensions (%d x %d)", divisor,
527 fPath.c_str(), W, H));
528 }
529 // subset dimensions
530 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
531 const int w = SkAlign2(W / divisor);
532 const int h = SkAlign2(H / divisor);
533 SkIRect subset;
534 SkCodec::Options opts;
535 opts.fSubset = &subset;
536 SkBitmap subsetBm;
537 // We will reuse pixel memory from bitmap.
538 void* pixels = bitmap.getPixels();
539 // Keep track of left and top (for drawing subsetBm into canvas). We could use
540 // fScale * x and fScale * y, but we want integers such that the next subset will start
541 // where the last one ended. So we'll add decodeInfo.width() and height().
542 int left = 0;
543 for (int x = 0; x < W; x += w) {
544 int top = 0;
545 for (int y = 0; y < H; y+= h) {
546 // Do not make the subset go off the edge of the image.
547 const int preScaleW = SkTMin(w, W - x);
548 const int preScaleH = SkTMin(h, H - y);
549 subset.setXYWH(x, y, preScaleW, preScaleH);
550 // And scale
551 // FIXME: Should we have a version of getScaledDimensions that takes a subset
552 // into account?
msarette6dd0042015-10-09 11:07:34 -0700553 decodeInfo = decodeInfo.makeWH(
554 SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)),
555 SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)));
scroggob636b452015-07-22 07:16:20 -0700556 size_t rowBytes = decodeInfo.minRowBytes();
557 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700558 nullptr, nullptr)) {
scroggob636b452015-07-22 07:16:20 -0700559 return SkStringPrintf("could not install pixels for %s.", fPath.c_str());
560 }
561 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
562 &opts, colorPtr, colorCountPtr);
563 switch (result) {
564 case SkCodec::kSuccess:
565 case SkCodec::kIncompleteInput:
566 break;
scroggob636b452015-07-22 07:16:20 -0700567 default:
568 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
569 "from %s with dimensions (%d x %d)\t error %d",
570 x, y, decodeInfo.width(), decodeInfo.height(),
571 fPath.c_str(), W, H, result);
572 }
msarett9e9444c2016-02-03 12:39:10 -0800573 premultiply_if_necessary(subsetBm);
scroggob636b452015-07-22 07:16:20 -0700574 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top));
575 // translate by the scaled height.
576 top += decodeInfo.height();
577 }
578 // translate by the scaled width.
579 left += decodeInfo.width();
580 }
581 return "";
582 }
msarettb714fb02016-01-22 14:46:42 -0800583 default:
584 SkASSERT(false);
585 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700586 }
scroggo9c59ebc2015-03-25 13:48:49 -0700587 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700588}
589
590SkISize CodecSrc::size() const {
591 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700592 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700593 if (nullptr == codec) {
594 return SkISize::Make(0, 0);
595 }
596 return codec->getScaledDimensions(fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700597}
598
599Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700600 if (1.0f == fScale) {
601 return SkOSPath::Basename(fPath.c_str());
msarett0a242972015-06-11 14:27:27 -0700602 }
msaretta5783ae2015-09-08 15:35:32 -0700603 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700604}
605
606/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
607
msarett3d9d7a72015-10-21 10:27:10 -0700608AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800609 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700610 : fPath(path)
611 , fMode(mode)
612 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800613 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700614 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800615 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700616{}
617
618bool AndroidCodecSrc::veto(SinkFlags flags) const {
619 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700620 return flags.type != SinkFlags::kRaster
621 || flags.approach != SinkFlags::kDirect;
622}
623
624Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
625 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
626 if (!encoded) {
627 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
628 }
629 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
630 if (nullptr == codec.get()) {
631 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
632 }
633
scroggoc5560be2016-02-03 09:42:42 -0800634 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
635 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700636 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
637 }
638
639 // Scale the image if it is desired.
640 SkISize size = codec->getSampledDimensions(fSampleSize);
641
642 // Visually inspecting very small output images is not necessary. We will
643 // cover these cases in unit testing.
644 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
645 return Error::Nonfatal("Scaling very small images is uninteresting.");
646 }
647 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
648
649 // Construct a color table for the decode if necessary
650 SkAutoTUnref<SkColorTable> colorTable(nullptr);
651 SkPMColor* colorPtr = nullptr;
652 int* colorCountPtr = nullptr;
653 int maxColors = 256;
654 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
655 SkPMColor colors[256];
656 colorTable.reset(new SkColorTable(colors, maxColors));
657 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
658 colorCountPtr = &maxColors;
659 }
660
661 SkBitmap bitmap;
662 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800663 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
msarett3d9d7a72015-10-21 10:27:10 -0700664 decodeInfo.width(), decodeInfo.height());
665 }
666
667 // Create options for the codec.
668 SkAndroidCodec::AndroidOptions options;
669 options.fColorPtr = colorPtr;
670 options.fColorCount = colorCountPtr;
671 options.fSampleSize = fSampleSize;
672
673 switch (fMode) {
674 case kFullImage_Mode: {
675 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(),
676 &options)) {
677 case SkCodec::kSuccess:
678 case SkCodec::kIncompleteInput:
679 break;
msarett3d9d7a72015-10-21 10:27:10 -0700680 default:
681 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
682 }
msarett9e9444c2016-02-03 12:39:10 -0800683 premultiply_if_necessary(bitmap);
msarett3d9d7a72015-10-21 10:27:10 -0700684 canvas->drawBitmap(bitmap, 0, 0);
685 return "";
686 }
687 case kDivisor_Mode: {
688 const int width = codec->getInfo().width();
689 const int height = codec->getInfo().height();
690 const int divisor = 2;
691 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800692 return Error::Nonfatal("Divisor is larger than image dimension.");
msarett3d9d7a72015-10-21 10:27:10 -0700693 }
694
msarettfa23a9e2015-10-21 13:26:59 -0700695 // Keep track of the final decoded dimensions.
696 int finalScaledWidth = 0;
697 int finalScaledHeight = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700698 for (int x = 0; x < divisor; x++) {
699 for (int y = 0; y < divisor; y++) {
700 // Calculate the subset dimensions
701 int subsetWidth = width / divisor;
702 int subsetHeight = height / divisor;
703 const int left = x * subsetWidth;
704 const int top = y * subsetHeight;
705
706 // Increase the size of the last subset in each row or column, when the
707 // divisor does not divide evenly into the image dimensions
708 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
709 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
710 SkIRect subset = SkIRect::MakeXYWH(left, top, subsetWidth, subsetHeight);
711 if (!codec->getSupportedSubset(&subset)) {
mtklein9b439152015-12-09 13:02:26 -0800712 return "Could not get supported subset to decode.";
msarett3d9d7a72015-10-21 10:27:10 -0700713 }
714 options.fSubset = &subset;
msarettfa23a9e2015-10-21 13:26:59 -0700715 const int scaledWidthOffset = subset.left() / fSampleSize;
716 const int scaledHeightOffset = subset.top() / fSampleSize;
717 void* pixels = bitmap.getAddr(scaledWidthOffset, scaledHeightOffset);
msarett3d9d7a72015-10-21 10:27:10 -0700718 SkISize scaledSubsetSize = codec->getSampledSubsetDimensions(fSampleSize,
719 subset);
720 SkImageInfo subsetDecodeInfo = decodeInfo.makeWH(scaledSubsetSize.width(),
721 scaledSubsetSize.height());
722
msarettfa23a9e2015-10-21 13:26:59 -0700723 if (x + 1 == divisor && y + 1 == divisor) {
724 finalScaledWidth = scaledWidthOffset + scaledSubsetSize.width();
725 finalScaledHeight = scaledHeightOffset + scaledSubsetSize.height();
726 }
727
msarett3d9d7a72015-10-21 10:27:10 -0700728 switch (codec->getAndroidPixels(subsetDecodeInfo, pixels, bitmap.rowBytes(),
729 &options)) {
730 case SkCodec::kSuccess:
731 case SkCodec::kIncompleteInput:
732 break;
msarett3d9d7a72015-10-21 10:27:10 -0700733 default:
734 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
735 }
736 }
737 }
msarettfa23a9e2015-10-21 13:26:59 -0700738
739 SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth,
740 (SkScalar) finalScaledHeight);
msarett9e9444c2016-02-03 12:39:10 -0800741 premultiply_if_necessary(bitmap);
msarettfa23a9e2015-10-21 13:26:59 -0700742 canvas->drawBitmapRect(bitmap, rect, rect, nullptr);
msarett3d9d7a72015-10-21 10:27:10 -0700743 return "";
744 }
745 default:
746 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800747 return "Error: Should not be reached.";
msarett3d9d7a72015-10-21 10:27:10 -0700748 }
749}
750
751SkISize AndroidCodecSrc::size() const {
752 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
753 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
754 if (nullptr == codec) {
755 return SkISize::Make(0, 0);
756 }
757 return codec->getSampledDimensions(fSampleSize);
758}
759
760Name AndroidCodecSrc::name() const {
761 // We will replicate the names used by CodecSrc so that images can
762 // be compared in Gold.
763 if (1 == fSampleSize) {
764 return SkOSPath::Basename(fPath.c_str());
765 }
msarett4b0778e2015-11-13 09:59:11 -0800766 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700767}
768
769/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
770
msarett18976312016-03-09 14:20:58 -0800771ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
772 : fPath(path)
773 , fMode(mode)
774 , fDstAlphaType(alphaType)
775 , fIsGpu(isGpu)
776 , fRunSerially(serial_from_path_name(path))
777{}
778
779bool ImageGenSrc::veto(SinkFlags flags) const {
780 if (fIsGpu) {
781 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect;
782 }
783
784 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
785}
786
787Error ImageGenSrc::draw(SkCanvas* canvas) const {
788 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
789 return Error::Nonfatal("Uninteresting to test image generator to 565.");
790 }
791
792 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
793 if (!encoded) {
794 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
795 }
796
msarettfc0b6d12016-03-17 13:50:17 -0700797#if defined(SK_BUILD_FOR_WIN)
798 // Initialize COM in order to test with WIC.
799 SkAutoCoInitialize com;
800 if (!com.succeeded()) {
801 return "Could not initialize COM.";
802 }
803#endif
804
msarett18976312016-03-09 14:20:58 -0800805 SkAutoTDelete<SkImageGenerator> gen(nullptr);
806 switch (fMode) {
807 case kCodec_Mode:
808 gen.reset(SkCodecImageGenerator::NewFromEncodedCodec(encoded));
809 if (!gen) {
810 return "Could not create codec image generator.";
811 }
812 break;
msarettfc0b6d12016-03-17 13:50:17 -0700813 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800814#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
msarett18976312016-03-09 14:20:58 -0800815 gen.reset(SkImageGeneratorCG::NewFromEncodedCG(encoded));
msarettfc0b6d12016-03-17 13:50:17 -0700816#elif defined(SK_BUILD_FOR_WIN)
817 gen.reset(SkImageGeneratorWIC::NewFromEncodedWIC(encoded));
818#endif
819
msarett18976312016-03-09 14:20:58 -0800820 if (!gen) {
msarettfc0b6d12016-03-17 13:50:17 -0700821 return "Could not create platform image generator.";
msarett18976312016-03-09 14:20:58 -0800822 }
823 break;
msarettfc0b6d12016-03-17 13:50:17 -0700824 }
msarett18976312016-03-09 14:20:58 -0800825 default:
826 SkASSERT(false);
827 return "Invalid image generator mode";
828 }
829
830 // Test deferred decoding path on GPU
831 if (fIsGpu) {
reed9ce9d672016-03-17 10:51:11 -0700832 sk_sp<SkImage> image(SkImage::MakeFromGenerator(gen.release(), nullptr));
msarett18976312016-03-09 14:20:58 -0800833 if (!image) {
834 return "Could not create image from codec image generator.";
835 }
836 canvas->drawImage(image, 0, 0);
837 return "";
838 }
mtklein343a63d2016-03-22 11:46:53 -0700839
msarett18976312016-03-09 14:20:58 -0800840 // Test various color and alpha types on CPU
841 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700842
msarett18976312016-03-09 14:20:58 -0800843 if (kGray_8_SkColorType == decodeInfo.colorType() &&
844 kOpaque_SkAlphaType != decodeInfo.alphaType()) {
845 return Error::Nonfatal("Avoid requesting non-opaque kGray8 decodes.");
846 }
mtklein343a63d2016-03-22 11:46:53 -0700847
msarett18976312016-03-09 14:20:58 -0800848 SkAutoTUnref<SkColorTable> colorTable(nullptr);
849 SkPMColor* colorPtr = nullptr;
850 int* colorCountPtr = nullptr;
851 int maxColors = 256;
852 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
853 SkPMColor colors[256];
854 colorTable.reset(new SkColorTable(colors, maxColors));
855 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
856 colorCountPtr = &maxColors;
857 }
858
859 SkBitmap bitmap;
860 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
861 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
862 decodeInfo.width(), decodeInfo.height());
863 }
mtklein343a63d2016-03-22 11:46:53 -0700864
msarett18976312016-03-09 14:20:58 -0800865 if (!gen->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), colorPtr,
866 colorCountPtr))
867 {
868 return SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str());
869 }
870
871 premultiply_if_necessary(bitmap);
872 canvas->drawBitmap(bitmap, 0, 0);
873 return "";
874}
875
876SkISize ImageGenSrc::size() const {
877 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
878 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
879 if (nullptr == codec) {
880 return SkISize::Make(0, 0);
881 }
882 return codec->getInfo().dimensions();
883}
884
885Name ImageGenSrc::name() const {
886 return SkOSPath::Basename(fPath.c_str());
887}
888
889/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
890
mtkleinf4ba3212015-01-28 15:32:24 -0800891static const SkRect kSKPViewport = {0,0, 1000,1000};
892
mtklein8d17a132015-01-30 11:42:31 -0800893SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800894
895Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800896 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800897 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800898 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
899 }
reedca2622b2016-03-18 07:25:55 -0700900 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream));
mtklein75d98fd2015-01-18 07:05:01 -0800901 if (!pic) {
902 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
903 }
halcanary96fcdcc2015-08-27 07:41:13 -0700904 stream.reset((SkStream*)nullptr); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -0700905
mtkleinf4ba3212015-01-28 15:32:24 -0800906 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800907 canvas->drawPicture(pic);
908 return "";
909}
910
911SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700912 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
913 if (!stream) {
914 return SkISize::Make(0,0);
915 }
916 SkPictInfo info;
917 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
918 return SkISize::Make(0,0);
919 }
920 SkRect viewport = kSKPViewport;
921 if (!viewport.intersect(info.fCullRect)) {
922 return SkISize::Make(0,0);
923 }
924 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800925}
926
927Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
928
929/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
930
mtkleinad66f9b2015-02-13 15:11:10 -0800931Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
932 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
933 return src.draw(canvas);
934}
935
936/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
937
mtkleinb9eb4ac2015-02-02 18:26:03 -0800938DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
939
bsalomon85b4b532016-04-05 11:06:27 -0700940GPUSink::GPUSink(GrContextFactory::ContextType ct,
941 GrContextFactory::ContextOptions options,
mtklein82d28432015-01-15 12:46:02 -0800942 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -0700943 bool diText,
brianosmand93c1202016-03-10 07:49:08 -0800944 SkColorType colorType,
945 SkColorProfileType profileType,
mtklein82d28432015-01-15 12:46:02 -0800946 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800947 : fContextType(ct)
kkinnunen5219fd92015-12-10 06:28:13 -0800948 , fContextOptions(options)
mtklein748ca3b2015-01-15 10:56:12 -0800949 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -0700950 , fUseDIText(diText)
brianosmand93c1202016-03-10 07:49:08 -0800951 , fColorType(colorType)
952 , fProfileType(profileType)
mtklein82d28432015-01-15 12:46:02 -0800953 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800954
joshualitt5f5a8d72015-02-25 14:09:45 -0800955void PreAbandonGpuContextErrorHandler(SkError, void*) {}
956
bsalomon648c6962015-10-23 09:06:59 -0700957DEFINE_bool(imm, false, "Run gpu configs in immediate mode.");
bsalomon69cfe952015-11-30 13:27:47 -0800958DEFINE_bool(batchClip, false, "Clip each GrBatch to its device bounds for testing.");
bsalomon6dea83f2015-12-03 12:58:06 -0800959DEFINE_bool(batchBounds, false, "Draw a wireframe bounds of each GrBatch.");
bsalomon489147c2015-12-14 12:13:09 -0800960DEFINE_int32(batchLookback, -1, "Maximum GrBatch lookback for combining, negative means default.");
bsalomonaecc0182016-03-07 11:50:44 -0800961DEFINE_int32(batchLookahead, -1, "Maximum GrBatch lookahead for combining, negative means "
962 "default.");
bsalomon648c6962015-10-23 09:06:59 -0700963
mtkleinb9eb4ac2015-02-02 18:26:03 -0800964Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
kkinnunen5219fd92015-12-10 06:28:13 -0800965 GrContextOptions grOptions;
bsalomon489147c2015-12-14 12:13:09 -0800966 grOptions.fImmediateMode = FLAGS_imm;
967 grOptions.fClipBatchToBounds = FLAGS_batchClip;
968 grOptions.fDrawBatchBounds = FLAGS_batchBounds;
969 grOptions.fMaxBatchLookback = FLAGS_batchLookback;
bsalomonaecc0182016-03-07 11:50:44 -0800970 grOptions.fMaxBatchLookahead = FLAGS_batchLookahead;
kkinnunen64492c42015-12-08 01:24:40 -0800971
kkinnunen5219fd92015-12-10 06:28:13 -0800972 src.modifyGrContextOptions(&grOptions);
973
974 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -0800975 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800976 const SkImageInfo info =
brianosmand93c1202016-03-10 07:49:08 -0800977 SkImageInfo::Make(size.width(), size.height(), fColorType,
978 kPremul_SkAlphaType, fProfileType);
msarett13a036b2016-02-08 09:10:47 -0800979#if SK_SUPPORT_GPU
980 const int maxDimension = factory.getContextInfo(fContextType, fContextOptions).
981 fGrContext->caps()->maxTextureSize();
982 if (maxDimension < SkTMax(size.width(), size.height())) {
983 return Error::Nonfatal("Src too large to create a texture.\n");
984 }
985#endif
986
reede8f30622016-03-23 18:59:25 -0700987 auto surface(
kkinnunen3e980c32015-12-23 01:33:00 -0800988 NewGpuSurface(&factory, fContextType, fContextOptions, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -0800989 if (!surface) {
990 return "Could not create a surface.";
991 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800992 if (FLAGS_preAbandonGpuContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700993 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr);
joshualitt5f5a8d72015-02-25 14:09:45 -0800994 factory.abandonContexts();
995 }
mtklein748ca3b2015-01-15 10:56:12 -0800996 SkCanvas* canvas = surface->getCanvas();
997 Error err = src.draw(canvas);
998 if (!err.isEmpty()) {
999 return err;
1000 }
1001 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001002 if (FLAGS_gpuStats) {
1003 canvas->getGrContext()->dumpCacheStats(log);
1004 canvas->getGrContext()->dumpGpuStats(log);
1005 }
mtklein748ca3b2015-01-15 10:56:12 -08001006 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -08001007 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001008 if (FLAGS_abandonGpuContext) {
1009 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001010 } else if (FLAGS_releaseAndAbandonGpuContext) {
1011 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001012 }
mtklein748ca3b2015-01-15 10:56:12 -08001013 return "";
1014}
1015
1016/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1017
halcanary47ef4d52015-03-03 09:13:09 -08001018static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001019 if (src.size().isEmpty()) {
1020 return "Source has empty dimensions";
1021 }
halcanary47ef4d52015-03-03 09:13:09 -08001022 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -08001023 int width = src.size().width(),
1024 height = src.size().height();
1025
halcanary7e798182015-04-14 14:06:18 -07001026 if (FLAGS_multiPage) {
halcanary4ba051c2016-03-10 10:31:53 -08001027 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
halcanary7e798182015-04-14 14:06:18 -07001028 const int kLetterWidth = 612, // 8.5 * 72
1029 kLetterHeight = 792; // 11 * 72
1030 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
1031 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -08001032
halcanary7e798182015-04-14 14:06:18 -07001033 int xPages = ((width - 1) / kLetterWidth) + 1;
1034 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -08001035
halcanary7e798182015-04-14 14:06:18 -07001036 for (int y = 0; y < yPages; ++y) {
1037 for (int x = 0; x < xPages; ++x) {
1038 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
1039 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
1040 SkCanvas* canvas =
1041 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
1042 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001043 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001044 }
1045 canvas->clipRect(letter);
1046 canvas->translate(-letter.width() * x, -letter.height() * y);
1047 Error err = src.draw(canvas);
1048 if (!err.isEmpty()) {
1049 return err;
1050 }
1051 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -07001052 }
halcanaryfd4a9932015-01-28 11:45:58 -08001053 }
halcanary7e798182015-04-14 14:06:18 -07001054 } else {
1055 SkCanvas* canvas =
1056 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1057 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001058 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001059 }
1060 Error err = src.draw(canvas);
1061 if (!err.isEmpty()) {
1062 return err;
1063 }
1064 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001065 }
halcanary7e798182015-04-14 14:06:18 -07001066 if (!doc->close()) {
1067 return "SkDocument::close() returned false";
1068 }
halcanaryfd4a9932015-01-28 11:45:58 -08001069 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001070 return "";
1071}
1072
halcanary47ef4d52015-03-03 09:13:09 -08001073Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1074 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
1075 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -07001076 return "SkDocument::CreatePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001077 }
halcanaryf12a1672015-09-23 12:45:49 -07001078 SkTArray<SkDocument::Attribute> info;
1079 info.emplace_back(SkString("Title"), src.name());
1080 info.emplace_back(SkString("Subject"),
1081 SkString("rendering correctness test"));
1082 info.emplace_back(SkString("Creator"), SkString("Skia/DM"));
halcanary70015762016-02-11 07:59:59 -08001083 doc->setMetadata(&info[0], info.count(), nullptr, nullptr);
halcanary47ef4d52015-03-03 09:13:09 -08001084 return draw_skdocument(src, doc.get(), dst);
1085}
1086
1087/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1088
1089XPSSink::XPSSink() {}
1090
1091Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1092 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
1093 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -07001094 return "SkDocument::CreateXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001095 }
1096 return draw_skdocument(src, doc.get(), dst);
1097}
mtklein748ca3b2015-01-15 10:56:12 -08001098/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1099
mtklein9c3f17d2015-01-28 11:35:18 -08001100SKPSink::SKPSink() {}
1101
mtkleinb9eb4ac2015-02-02 18:26:03 -08001102Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001103 SkSize size;
1104 size = src.size();
1105 SkPictureRecorder recorder;
1106 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1107 if (!err.isEmpty()) {
1108 return err;
1109 }
reedca2622b2016-03-18 07:25:55 -07001110 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001111 return "";
1112}
1113
1114/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1115
mtklein8a4527e2015-01-31 20:00:58 -08001116SVGSink::SVGSink() {}
1117
mtkleinb9eb4ac2015-02-02 18:26:03 -08001118Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary385fe4d2015-08-26 13:07:48 -07001119 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
fmalita2aafe6f2015-02-06 12:51:10 -08001120 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
1121 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
1122 xmlWriter));
1123 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -08001124}
1125
1126/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1127
mtklein27c3fdd2016-02-26 14:43:21 -08001128RasterSink::RasterSink(SkColorType colorType, SkColorProfileType profileType)
1129 : fColorType(colorType)
1130 , fProfileType(profileType) {}
mtklein748ca3b2015-01-15 10:56:12 -08001131
mtkleinb9eb4ac2015-02-02 18:26:03 -08001132Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001133 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001134 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1135 SkAlphaType alphaType = kPremul_SkAlphaType;
1136 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1137
mtkleinc8be09a2016-01-04 18:56:57 -08001138 SkMallocPixelRef::ZeroedPRFactory factory;
mtklein27c3fdd2016-02-26 14:43:21 -08001139 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(),
1140 fColorType, alphaType, fProfileType),
mtkleinc8be09a2016-01-04 18:56:57 -08001141 &factory,
1142 nullptr/*colortable*/);
mtklein748ca3b2015-01-15 10:56:12 -08001143 SkCanvas canvas(*dst);
1144 return src.draw(&canvas);
1145}
1146
1147/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1148
mtkleina16e69e2015-05-05 11:38:45 -07001149// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001150// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001151// Several examples below.
1152
mtkleincbf89782016-02-19 14:27:14 -08001153template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001154static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001155 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001156 class ProxySrc : public Src {
1157 public:
mtkleincbf89782016-02-19 14:27:14 -08001158 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001159 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001160 Name name() const override { return "ProxySrc"; }
1161 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001162 private:
mtkleincbf89782016-02-19 14:27:14 -08001163 SkISize fSize;
1164 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001165 };
msarett62d3b102015-12-10 15:14:27 -08001166 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001167}
1168
1169/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1170
mtklein4a34ecb2016-01-08 10:19:35 -08001171DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1172
1173// Is *bitmap identical to what you get drawing src into sink?
1174static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1175 // We can only check raster outputs.
1176 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1177 if (FLAGS_check && bitmap) {
1178 SkBitmap reference;
1179 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001180 SkDynamicMemoryWStream wStream;
1181 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001182 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1183 SkASSERT(err.isEmpty());
1184 if (!err.isEmpty()) {
1185 return err;
1186 }
1187 // The dimensions are a property of the Src only, and so should be identical.
1188 SkASSERT(reference.getSize() == bitmap->getSize());
1189 if (reference.getSize() != bitmap->getSize()) {
1190 return "Dimensions don't match reference";
1191 }
1192 // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy.
1193 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1194 return "Pixels don't match reference";
1195 }
1196 }
1197 return "";
1198}
1199
1200/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1201
mtkleind603b222015-02-17 11:13:33 -08001202static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1203 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1204 matrix->mapRect(&bounds);
1205 matrix->postTranslate(-bounds.x(), -bounds.y());
1206 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
1207}
1208
msarett62d3b102015-12-10 15:14:27 -08001209ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001210
mtkleinb9eb4ac2015-02-02 18:26:03 -08001211Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001212 SkMatrix matrix = fMatrix;
1213 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
msarett62d3b102015-12-10 15:14:27 -08001214 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001215 canvas->concat(matrix);
1216 return src.draw(canvas);
1217 });
mtklein748ca3b2015-01-15 10:56:12 -08001218}
1219
mtkleind603b222015-02-17 11:13:33 -08001220// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1221// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001222ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001223
1224Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1225 Error err = fSink->draw(src, bitmap, stream, log);
1226 if (!err.isEmpty()) {
1227 return err;
1228 }
1229
1230 SkMatrix inverse;
1231 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1232 return "Cannot upright --matrix.";
1233 }
1234 SkMatrix upright = SkMatrix::I();
1235 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1236 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1237 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1238 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1239
1240 SkBitmap uprighted;
1241 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1242 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1243
1244 SkCanvas canvas(uprighted);
1245 canvas.concat(upright);
1246 SkPaint paint;
1247 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1248 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1249
1250 *bitmap = uprighted;
1251 bitmap->lockPixels();
1252 return "";
1253}
1254
mtklein748ca3b2015-01-15 10:56:12 -08001255/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1256
mtkleina16e69e2015-05-05 11:38:45 -07001257Error ViaSerialization::draw(
1258 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001259 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001260 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001261 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001262 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1263 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001264 if (!err.isEmpty()) {
1265 return err;
1266 }
reedca2622b2016-03-18 07:25:55 -07001267 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001268
1269 // Serialize it and then deserialize it.
1270 SkDynamicMemoryWStream wStream;
1271 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -08001272 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -07001273 sk_sp<SkPicture> deserialized(SkPicture::MakeFromStream(rStream));
mtklein748ca3b2015-01-15 10:56:12 -08001274
msarett62d3b102015-12-10 15:14:27 -08001275 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001276 canvas->drawPicture(deserialized);
mtklein4a34ecb2016-01-08 10:19:35 -08001277 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001278 });
mtklein748ca3b2015-01-15 10:56:12 -08001279}
1280
1281/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1282
msarett62d3b102015-12-10 15:14:27 -08001283ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1284 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001285 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001286 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001287 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001288
mtkleinb9eb4ac2015-02-02 18:26:03 -08001289Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001290 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001291 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001292 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1293 SkIntToScalar(size.height()),
1294 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001295 if (!err.isEmpty()) {
1296 return err;
1297 }
reedca2622b2016-03-18 07:25:55 -07001298 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001299
msarett62d3b102015-12-10 15:14:27 -08001300 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001301 const int xTiles = (size.width() + fW - 1) / fW,
1302 yTiles = (size.height() + fH - 1) / fH;
1303 SkMultiPictureDraw mpd(xTiles*yTiles);
reede8f30622016-03-23 18:59:25 -07001304 SkTArray<sk_sp<SkSurface>> surfaces;
1305// surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001306
mtkleina16e69e2015-05-05 11:38:45 -07001307 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1308 for (int j = 0; j < yTiles; j++) {
1309 for (int i = 0; i < xTiles; i++) {
1310 // This lets our ultimate Sink determine the best kind of surface.
1311 // E.g., if it's a GpuSink, the surfaces and images are textures.
reede8f30622016-03-23 18:59:25 -07001312 auto s = canvas->makeSurface(info);
mtkleina16e69e2015-05-05 11:38:45 -07001313 if (!s) {
reede8f30622016-03-23 18:59:25 -07001314 s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001315 }
reede8f30622016-03-23 18:59:25 -07001316 surfaces.push_back(s);
mtkleina16e69e2015-05-05 11:38:45 -07001317 SkCanvas* c = s->getCanvas();
1318 c->translate(SkIntToScalar(-i * fW),
1319 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
reedca2622b2016-03-18 07:25:55 -07001320 mpd.add(c, pic.get());
mtklein748ca3b2015-01-15 10:56:12 -08001321 }
mtklein748ca3b2015-01-15 10:56:12 -08001322 }
mtkleina16e69e2015-05-05 11:38:45 -07001323 mpd.draw();
1324 for (int j = 0; j < yTiles; j++) {
1325 for (int i = 0; i < xTiles; i++) {
reed9ce9d672016-03-17 10:51:11 -07001326 sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot());
mtkleina16e69e2015-05-05 11:38:45 -07001327 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1328 }
1329 }
mtkleina16e69e2015-05-05 11:38:45 -07001330 return "";
1331 });
mtklein748ca3b2015-01-15 10:56:12 -08001332}
1333
mtkleinb7e8d692015-04-07 08:30:32 -07001334/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1335
mtklein4a34ecb2016-01-08 10:19:35 -08001336Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1337 auto size = src.size();
1338 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1339 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001340 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08001341 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1342 SkIntToScalar(size.height())));
1343 if (!err.isEmpty()) {
1344 return err;
1345 }
reedca2622b2016-03-18 07:25:55 -07001346 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08001347 canvas->drawPicture(pic);
1348 return check_against_reference(bitmap, src, fSink);
1349 });
1350}
1351
1352/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1353
mtkleinb7e8d692015-04-07 08:30:32 -07001354// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1355// This tests that any shortcuts we may take while recording that second picture are legal.
1356Error ViaSecondPicture::draw(
1357 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001358 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001359 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001360 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001361 sk_sp<SkPicture> pic;
mtkleina16e69e2015-05-05 11:38:45 -07001362 for (int i = 0; i < 2; i++) {
1363 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1364 SkIntToScalar(size.height())));
1365 if (!err.isEmpty()) {
1366 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001367 }
reedca2622b2016-03-18 07:25:55 -07001368 pic = recorder.finishRecordingAsPicture();
mtkleinb7e8d692015-04-07 08:30:32 -07001369 }
mtkleina16e69e2015-05-05 11:38:45 -07001370 canvas->drawPicture(pic);
mtklein4a34ecb2016-01-08 10:19:35 -08001371 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001372 });
mtkleinb7e8d692015-04-07 08:30:32 -07001373}
1374
mtkleind31c13d2015-05-05 12:59:56 -07001375/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1376
mtklein6fbf4b32015-05-06 11:35:40 -07001377// Draw the Src twice. This can help exercise caching.
1378Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001379 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001380 for (int i = 0; i < 2; i++) {
1381 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1382 canvas->clear(SK_ColorTRANSPARENT);
1383 Error err = src.draw(canvas);
1384 if (err.isEmpty()) {
1385 return err;
1386 }
1387 }
mtklein4a34ecb2016-01-08 10:19:35 -08001388 return check_against_reference(bitmap, src, fSink);
mtklein6fbf4b32015-05-06 11:35:40 -07001389 });
1390}
1391
1392/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1393
mtkleind31c13d2015-05-05 12:59:56 -07001394// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1395// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1396// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1397struct DrawsAsSingletonPictures {
1398 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001399 const SkDrawableList& fDrawables;
mtklein11064df2016-04-06 15:01:57 -07001400 SkRect fBounds;
mtkleind31c13d2015-05-05 12:59:56 -07001401
mtkleind31c13d2015-05-05 12:59:56 -07001402 template <typename T>
1403 void draw(const T& op, SkCanvas* canvas) {
1404 // We must pass SkMatrix::I() as our initial matrix.
1405 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1406 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001407 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1408 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001409 }
1410
mtklein449d9b72015-09-28 10:33:02 -07001411 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001412 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001413 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001414 SkPictureRecorder rec;
mtklein11064df2016-04-06 15:01:57 -07001415 this->draw(op, rec.beginRecording(fBounds));
reedca2622b2016-03-18 07:25:55 -07001416 sk_sp<SkPicture> pic(rec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07001417 fCanvas->drawPicture(pic);
1418 }
1419
mtklein449d9b72015-09-28 10:33:02 -07001420 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001421 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001422 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1423 this->draw(op, fCanvas);
1424 }
mtkleind31c13d2015-05-05 12:59:56 -07001425};
1426
mtkleind31c13d2015-05-05 12:59:56 -07001427// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1428// Then play back that macro picture into our wrapped sink.
1429Error ViaSingletonPictures::draw(
1430 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1431 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001432 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07001433 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1434 SkRecord skr;
1435 SkRecorder recorder(&skr, size.width(), size.height());
1436 Error err = src.draw(&recorder);
1437 if (!err.isEmpty()) {
1438 return err;
1439 }
1440
1441 // Record our macro-picture, with each draw op as its own sub-picture.
1442 SkPictureRecorder macroRec;
1443 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1444 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001445
1446 SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
1447 const SkDrawableList empty;
1448
1449 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1450 macroCanvas,
1451 drawables ? *drawables : empty,
mtklein11064df2016-04-06 15:01:57 -07001452 SkRect::MakeWH((SkScalar)size.width(), (SkScalar)size.height()),
mtkleind2baa902015-07-07 09:43:28 -07001453 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001454 for (int i = 0; i < skr.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -07001455 skr.visit(i, drawsAsSingletonPictures);
mtkleind31c13d2015-05-05 12:59:56 -07001456 }
reedca2622b2016-03-18 07:25:55 -07001457 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07001458
1459 canvas->drawPicture(macroPic);
mtklein4a34ecb2016-01-08 10:19:35 -08001460 return check_against_reference(bitmap, src, fSink);
mtkleind31c13d2015-05-05 12:59:56 -07001461 });
1462}
1463
mtklein748ca3b2015-01-15 10:56:12 -08001464} // namespace DM