blob: cea87f7056ff85032d4991c15837131b0ad77a21 [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"
msarett9876ac52016-06-01 14:47:18 -07009#include "Resources.h"
msarett3d9d7a72015-10-21 10:27:10 -070010#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "SkAutoMalloc.h"
scroggof24f2242015-03-03 08:59:20 -080012#include "SkCodec.h"
msarettb714fb02016-01-22 14:46:42 -080013#include "SkCodecImageGenerator.h"
msarett888dc162016-05-23 10:21:17 -070014#include "SkColorSpace.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000015#include "SkColorSpaceXform.h"
Hal Canary95e3c052017-01-11 12:44:43 -050016#include "SkColorSpace_XYZ.h"
mtkleina16e69e2015-05-05 11:38:45 -070017#include "SkCommonFlags.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070018#include "SkData.h"
Hal Canary85c7fe82016-10-25 10:33:27 -040019#include "SkDebugCanvas.h"
reedbabc3de2016-07-08 08:43:27 -070020#include "SkDeferredCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080021#include "SkDocument.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070022#include "SkImageGenerator.h"
msarett18976312016-03-09 14:20:58 -080023#include "SkImageGeneratorCG.h"
msarettfc0b6d12016-03-17 13:50:17 -070024#include "SkImageGeneratorWIC.h"
mtklein8bbbb692016-08-15 12:56:00 -070025#include "SkLiteDL.h"
26#include "SkLiteRecorder.h"
mtkleinc8be09a2016-01-04 18:56:57 -080027#include "SkMallocPixelRef.h"
mtklein748ca3b2015-01-15 10:56:12 -080028#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080029#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080030#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050031#include "SkOSPath.h"
msarett9e9444c2016-02-03 12:39:10 -080032#include "SkOpts.h"
mtkleinffa901a2015-03-16 10:38:07 -070033#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080034#include "SkPictureRecorder.h"
reed54dc4872016-09-13 08:09:45 -070035#include "SkPipe.h"
mtklein748ca3b2015-01-15 10:56:12 -080036#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070037#include "SkRecordDraw.h"
38#include "SkRecorder.h"
fmalita2aafe6f2015-02-06 12:51:10 -080039#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080040#include "SkStream.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000041#include "SkSwizzler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050042#include "SkTLogic.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000043#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050044#include <functional>
mtklein748ca3b2015-01-15 10:56:12 -080045
msarettfc0b6d12016-03-17 13:50:17 -070046#if defined(SK_BUILD_FOR_WIN)
47 #include "SkAutoCoInitialize.h"
48#endif
49
fmalita718df0a2016-07-15 10:33:29 -070050#if defined(SK_XML)
fmalitaa48f0e32016-08-04 06:26:05 -070051 #include "SkSVGDOM.h"
fmalita718df0a2016-07-15 10:33:29 -070052 #include "SkXMLWriter.h"
53#endif
54
halcanary7e798182015-04-14 14:06:18 -070055DEFINE_bool(multiPage, false, "For document-type backends, render the source"
56 " into multiple pages");
scroggo3ac66e92016-02-08 15:09:48 -080057DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070058
bsalomon3724e572016-03-30 18:56:19 -070059using sk_gpu_test::GrContextFactory;
60
mtklein748ca3b2015-01-15 10:56:12 -080061namespace DM {
62
mtklein748ca3b2015-01-15 10:56:12 -080063GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
64
65Error GMSrc::draw(SkCanvas* canvas) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040066 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080067 canvas->concat(gm->getInitialTransform());
68 gm->draw(canvas);
69 return "";
70}
71
72SkISize GMSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040073 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080074 return gm->getISize();
75}
76
77Name GMSrc::name() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040078 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080079 return gm->getName();
80}
81
bsalomon4ee6bd82015-05-27 13:23:23 -070082void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040083 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070084 gm->modifyGrContextOptions(options);
85}
86
mtklein748ca3b2015-01-15 10:56:12 -080087/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
88
msarettd1227a72016-05-18 06:23:57 -070089BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
msaretta5783ae2015-09-08 15:35:32 -070090 : fPath(path)
msaretta5783ae2015-09-08 15:35:32 -070091 , fMode(mode)
92 , fDstColorType(dstColorType)
93 , fSampleSize(sampleSize)
94{}
95
96bool BRDSrc::veto(SinkFlags flags) const {
97 // No need to test to non-raster or indirect backends.
98 return flags.type != SinkFlags::kRaster
99 || flags.approach != SinkFlags::kDirect;
100}
101
msarettd1227a72016-05-18 06:23:57 -0700102static SkBitmapRegionDecoder* create_brd(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700103 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msaretta5783ae2015-09-08 15:35:32 -0700104 if (!encoded) {
105 return NULL;
106 }
reed42943c82016-09-12 12:01:44 -0700107 return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy);
msaretta5783ae2015-09-08 15:35:32 -0700108}
109
Matt Sarett334df3a2016-12-15 18:17:33 -0500110static inline void alpha8_to_gray8(SkBitmap* bitmap) {
111 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
112 // them back to kGray8 so our test framework can draw them correctly.
113 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
114 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
115 .makeAlphaType(kOpaque_SkAlphaType);
116 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
117 }
118}
119
msaretta5783ae2015-09-08 15:35:32 -0700120Error BRDSrc::draw(SkCanvas* canvas) const {
121 SkColorType colorType = canvas->imageInfo().colorType();
122 if (kRGB_565_SkColorType == colorType &&
123 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
124 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
125 }
126 switch (fDstColorType) {
127 case CodecSrc::kGetFromCanvas_DstColorType:
128 break;
129 case CodecSrc::kIndex8_Always_DstColorType:
130 colorType = kIndex_8_SkColorType;
131 break;
132 case CodecSrc::kGrayscale_Always_DstColorType:
133 colorType = kGray_8_SkColorType;
134 break;
msarett34e0ec42016-04-22 16:27:24 -0700135 default:
136 SkASSERT(false);
137 break;
msaretta5783ae2015-09-08 15:35:32 -0700138 }
139
Ben Wagner145dbcd2016-11-03 14:40:50 -0400140 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700141 if (nullptr == brd.get()) {
142 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
143 }
144
msarett04965c62015-10-12 10:24:38 -0700145 if (!brd->conversionSupported(colorType)) {
mtklein9b439152015-12-09 13:02:26 -0800146 return Error::Nonfatal("Cannot convert to color type.");
msarett04965c62015-10-12 10:24:38 -0700147 }
148
msaretta5783ae2015-09-08 15:35:32 -0700149 const uint32_t width = brd->width();
150 const uint32_t height = brd->height();
151 // Visually inspecting very small output images is not necessary.
152 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
153 return Error::Nonfatal("Scaling very small images is uninteresting.");
154 }
155 switch (fMode) {
156 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700157 SkBitmap bitmap;
158 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
159 fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800160 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700161 }
Matt Sarett334df3a2016-12-15 18:17:33 -0500162 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700163 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700164 return "";
165 }
166 case kDivisor_Mode: {
167 const uint32_t divisor = 2;
168 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800169 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700170 }
171
172 // Use a border to test subsets that extend outside the image.
173 // We will not allow the border to be larger than the image dimensions. Allowing
174 // these large borders causes off by one errors that indicate a problem with the
175 // test suite, not a problem with the implementation.
176 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
177 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
178 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
179
180 // We may need to clear the canvas to avoid uninitialized memory.
181 // Assume we are scaling a 780x780 image with sampleSize = 8.
182 // The output image should be 97x97.
183 // Each subset will be 390x390.
184 // Each scaled subset be 48x48.
185 // Four scaled subsets will only fill a 96x96 image.
186 // The bottom row and last column will not be touched.
187 // This is an unfortunate result of our rounding rules when scaling.
188 // Maybe we need to consider testing scaled subsets without trying to
189 // combine them to match the full scaled image? Or maybe this is the
190 // best we can do?
191 canvas->clear(0);
192
193 for (uint32_t x = 0; x < divisor; x++) {
194 for (uint32_t y = 0; y < divisor; y++) {
195 // Calculate the subset dimensions
196 uint32_t subsetWidth = width / divisor;
197 uint32_t subsetHeight = height / divisor;
198 const int left = x * subsetWidth;
199 const int top = y * subsetHeight;
200
201 // Increase the size of the last subset in each row or column, when the
202 // divisor does not divide evenly into the image dimensions
203 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
204 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
205
206 // Increase the size of the subset in order to have a border on each side
207 const int decodeLeft = left - unscaledBorder;
208 const int decodeTop = top - unscaledBorder;
209 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
210 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700211 SkBitmap bitmap;
212 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
213 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800214 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700215 }
msaretta5783ae2015-09-08 15:35:32 -0700216
Matt Sarett334df3a2016-12-15 18:17:33 -0500217 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700218 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700219 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
220 (SkScalar) (subsetWidth / fSampleSize),
221 (SkScalar) (subsetHeight / fSampleSize)),
222 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
223 (SkScalar) (top / fSampleSize),
224 (SkScalar) (subsetWidth / fSampleSize),
225 (SkScalar) (subsetHeight / fSampleSize)),
226 nullptr);
227 }
228 }
229 return "";
230 }
231 default:
232 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800233 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700234 }
235}
236
237SkISize BRDSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400238 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700239 if (brd) {
240 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
241 SkTMax(1, brd->height() / (int) fSampleSize));
242 }
243 return SkISize::Make(0, 0);
244}
245
246static SkString get_scaled_name(const Path& path, float scale) {
247 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
248}
249
250Name BRDSrc::name() const {
251 // We will replicate the names used by CodecSrc so that images can
252 // be compared in Gold.
253 if (1 == fSampleSize) {
254 return SkOSPath::Basename(fPath.c_str());
255 }
msarett4b0778e2015-11-13 09:59:11 -0800256 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700257}
258
259/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
260
scroggo3ac66e92016-02-08 15:09:48 -0800261static bool serial_from_path_name(const SkString& path) {
262 if (!FLAGS_RAW_threading) {
263 static const char* const exts[] = {
264 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
265 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
266 };
267 const char* actualExt = strrchr(path.c_str(), '.');
268 if (actualExt) {
269 actualExt++;
270 for (auto* ext : exts) {
271 if (0 == strcmp(ext, actualExt)) {
272 return true;
273 }
274 }
275 }
276 }
277 return false;
278}
279
scroggoc5560be2016-02-03 09:42:42 -0800280CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
281 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700282 : fPath(path)
283 , fMode(mode)
284 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800285 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700286 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800287 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700288{}
mtklein748ca3b2015-01-15 10:56:12 -0800289
mtklein99cab4e2015-07-31 06:43:04 -0700290bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800291 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800292 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700293}
scroggo9b77ddd2015-03-19 06:03:39 -0700294
msarett34e0ec42016-04-22 16:27:24 -0700295// Allows us to test decodes to non-native 8888.
msarettb1be46b2016-05-17 08:52:11 -0700296static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700297 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
298 return;
299 }
300
301 for (int y = 0; y < bitmap.height(); y++) {
302 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
303 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
304 }
305}
306
msarett9e9444c2016-02-03 12:39:10 -0800307// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
308// This allows us to still test unpremultiplied decodes.
msarettb1be46b2016-05-17 08:52:11 -0700309static void premultiply_if_necessary(SkBitmap& bitmap) {
msarett9e9444c2016-02-03 12:39:10 -0800310 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
311 return;
312 }
313
314 switch (bitmap.colorType()) {
315 case kN32_SkColorType:
316 for (int y = 0; y < bitmap.height(); y++) {
317 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
318 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
319 }
320 break;
321 case kIndex_8_SkColorType: {
322 SkColorTable* colorTable = bitmap.getColorTable();
323 SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
324 SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count());
325 break;
326 }
327 default:
328 // No need to premultiply kGray or k565 outputs.
329 break;
330 }
msarette1daa482016-02-03 15:31:18 -0800331
332 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
333 // bitmap as kPremul.
334 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800335}
336
msarettb1be46b2016-05-17 08:52:11 -0700337static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
scroggoba584892016-05-20 13:56:13 -0700338 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
msarett3d9d7a72015-10-21 10:27:10 -0700339 switch (dstColorType) {
340 case CodecSrc::kIndex8_Always_DstColorType:
341 if (kRGB_565_SkColorType == canvasColorType) {
342 return false;
343 }
scroggoc5560be2016-02-03 09:42:42 -0800344 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700345 break;
346 case CodecSrc::kGrayscale_Always_DstColorType:
scroggo1a361922016-05-20 14:27:16 -0700347 if (kRGB_565_SkColorType == canvasColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700348 return false;
349 }
scroggoc5560be2016-02-03 09:42:42 -0800350 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700351 break;
msarett34e0ec42016-04-22 16:27:24 -0700352 case CodecSrc::kNonNative8888_Always_DstColorType:
353 if (kRGB_565_SkColorType == canvasColorType) {
354 return false;
355 }
356#ifdef SK_PMCOLOR_IS_RGBA
357 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
358#else
359 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
360#endif
361 break;
msarett3d9d7a72015-10-21 10:27:10 -0700362 default:
msarett55f7bdd2016-02-16 13:24:54 -0800363 if (kRGB_565_SkColorType == canvasColorType &&
364 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
365 return false;
366 }
scroggoc5560be2016-02-03 09:42:42 -0800367 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700368 break;
369 }
370
scroggoba584892016-05-20 13:56:13 -0700371 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700372 return true;
373}
374
msarettb1be46b2016-05-17 08:52:11 -0700375static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
376 SkPMColor* colorPtr, int colorCount, CodecSrc::DstColorType dstColorType,
377 SkScalar left = 0, SkScalar top = 0) {
Hal Canary2db83612016-11-04 13:02:54 -0400378 sk_sp<SkColorTable> colorTable(new SkColorTable(colorPtr, colorCount));
msarettb1be46b2016-05-17 08:52:11 -0700379 SkBitmap bitmap;
380 bitmap.installPixels(info, pixels, rowBytes, colorTable.get(), nullptr, nullptr);
381 premultiply_if_necessary(bitmap);
382 swap_rb_if_necessary(bitmap, dstColorType);
383 canvas->drawBitmap(bitmap, left, top);
384}
385
mtkleine0effd62015-07-29 06:37:28 -0700386Error CodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700387 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800388 if (!encoded) {
389 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
390 }
msarettb714fb02016-01-22 14:46:42 -0800391
Ben Wagner145dbcd2016-11-03 14:40:50 -0400392 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700393 if (nullptr == codec.get()) {
394 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700395 }
396
scroggoba584892016-05-20 13:56:13 -0700397 SkImageInfo decodeInfo = codec->getInfo();
398 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
399 fDstAlphaType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700400 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
msarett438b2ad2015-04-09 12:43:10 -0700401 }
402
msarett0a242972015-06-11 14:27:27 -0700403 // Try to scale the image if it is desired
404 SkISize size = codec->getScaledDimensions(fScale);
405 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
406 return Error::Nonfatal("Test without scaling is uninteresting.");
407 }
msarettb32758a2015-08-18 13:22:46 -0700408
409 // Visually inspecting very small output images is not necessary. We will
410 // cover these cases in unit testing.
411 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
412 return Error::Nonfatal("Scaling very small images is uninteresting.");
413 }
msarett0a242972015-06-11 14:27:27 -0700414 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
415
msarettb1be46b2016-05-17 08:52:11 -0700416 const int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
417 const size_t rowBytes = size.width() * bpp;
scroggo19b91532016-10-24 09:03:26 -0700418 const size_t safeSize = decodeInfo.getSafeSize(rowBytes);
419 SkAutoMalloc pixels(safeSize);
msarettb1be46b2016-05-17 08:52:11 -0700420 SkPMColor colorPtr[256];
421 int colorCount = 256;
msarett438b2ad2015-04-09 12:43:10 -0700422
msarettbb25b532016-01-13 09:31:39 -0800423 SkCodec::Options options;
424 if (kCodecZeroInit_Mode == fMode) {
msarettb1be46b2016-05-17 08:52:11 -0700425 memset(pixels.get(), 0, size.height() * rowBytes);
msarettbb25b532016-01-13 09:31:39 -0800426 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
427 }
msarett34e0ec42016-04-22 16:27:24 -0700428
429 SkImageInfo bitmapInfo = decodeInfo;
430 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
431 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
432 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
433 }
scroggo9b77ddd2015-03-19 06:03:39 -0700434
scroggo9c59ebc2015-03-25 13:48:49 -0700435 switch (fMode) {
scroggo19b91532016-10-24 09:03:26 -0700436 case kAnimated_Mode: {
437 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
438 if (frameInfos.size() <= 1) {
439 return SkStringPrintf("%s is not an animated image.", fPath.c_str());
440 }
441
scroggo90e971a2016-10-25 06:11:01 -0700442 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
443 // into. "factor" is the number of frames to draw on one row. There will be
444 // up to "factor" rows as well.
445 const float root = sqrt((float) frameInfos.size());
446 const int factor = sk_float_ceil2int(root);
447
scroggo19b91532016-10-24 09:03:26 -0700448 // Used to cache a frame that future frames will depend on.
449 SkAutoMalloc priorFramePixels;
450 size_t cachedFrame = SkCodec::kNone;
451 for (size_t i = 0; i < frameInfos.size(); i++) {
452 options.fFrameIndex = i;
453 // Check for a prior frame
454 const size_t reqFrame = frameInfos[i].fRequiredFrame;
455 if (reqFrame != SkCodec::kNone && reqFrame == cachedFrame
456 && priorFramePixels.get()) {
457 // Copy into pixels
458 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
459 options.fHasPriorFrame = true;
460 } else {
461 options.fHasPriorFrame = false;
462 }
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500463 SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(),
464 rowBytes, &options,
465 colorPtr, &colorCount);
466 if (SkCodec::kInvalidInput == result && i > 0) {
467 // Some of our test images have truncated later frames. Treat that
468 // the same as incomplete.
469 result = SkCodec::kIncompleteInput;
470 }
scroggo19b91532016-10-24 09:03:26 -0700471 switch (result) {
472 case SkCodec::kSuccess:
scroggo90e971a2016-10-25 06:11:01 -0700473 case SkCodec::kIncompleteInput: {
474 SkAutoCanvasRestore acr(canvas, true);
475 const int xTranslate = (i % factor) * decodeInfo.width();
476 const int yTranslate = (i / factor) * decodeInfo.height();
477 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
scroggo19b91532016-10-24 09:03:26 -0700478 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes,
479 colorPtr, colorCount, fDstColorType);
480 if (result == SkCodec::kIncompleteInput) {
481 return "";
482 }
483 break;
scroggo90e971a2016-10-25 06:11:01 -0700484 }
scroggo53f63b62016-10-27 08:29:13 -0700485 case SkCodec::kInvalidConversion:
486 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType
487 || decodeInfo.colorType() == kIndex_8_SkColorType)) {
488 return Error::Nonfatal(SkStringPrintf(
489 "Cannot decode frame %i to 565/Index8 (%s).", i, fPath.c_str()));
490 }
491 // Fall through.
scroggo19b91532016-10-24 09:03:26 -0700492 default:
493 return SkStringPrintf("Couldn't getPixels for frame %i in %s.",
494 i, fPath.c_str());
495 }
496
497 // If a future frame depends on this one, store it in priorFrame.
498 // (Note that if i+1 does *not* depend on i, then no future frame can.)
499 if (i+1 < frameInfos.size() && frameInfos[i+1].fRequiredFrame == i) {
500 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
501 cachedFrame = i;
502 }
scroggo19b91532016-10-24 09:03:26 -0700503 }
504 break;
505 }
msarettbb25b532016-01-13 09:31:39 -0800506 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700507 case kCodec_Mode: {
msarettb1be46b2016-05-17 08:52:11 -0700508 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options,
509 colorPtr, &colorCount)) {
scroggoeb602a52015-07-09 08:16:03 -0700510 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700511 // We consider incomplete to be valid, since we should still decode what is
512 // available.
scroggoeb602a52015-07-09 08:16:03 -0700513 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700514 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700515 default:
516 // Everything else is considered a failure.
517 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
518 }
msarettb1be46b2016-05-17 08:52:11 -0700519
520 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount,
521 fDstColorType);
scroggo9c59ebc2015-03-25 13:48:49 -0700522 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700523 }
scroggo9c59ebc2015-03-25 13:48:49 -0700524 case kScanline_Mode: {
scroggod8d68552016-06-06 11:26:17 -0700525 void* dst = pixels.get();
526 uint32_t height = decodeInfo.height();
scroggo19b91532016-10-24 09:03:26 -0700527 const bool useIncremental = [this]() {
528 auto exts = { "png", "PNG", "gif", "GIF" };
529 for (auto ext : exts) {
530 if (fPath.endsWith(ext)) {
531 return true;
532 }
533 }
534 return false;
535 }();
536 // ico may use the old scanline method or the new one, depending on whether it
537 // internally holds a bmp or a png.
scroggo8e6c7ad2016-09-16 08:20:38 -0700538 const bool ico = fPath.endsWith("ico");
scroggo19b91532016-10-24 09:03:26 -0700539 bool useOldScanlineMethod = !useIncremental && !ico;
540 if (useIncremental || ico) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700541 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
542 rowBytes, nullptr, colorPtr, &colorCount)) {
543 int rowsDecoded;
544 if (SkCodec::kIncompleteInput == codec->incrementalDecode(&rowsDecoded)) {
545 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
546 SkCodec::kNo_ZeroInitialized, height,
547 rowsDecoded);
scroggo6fb23912016-06-02 14:16:43 -0700548 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700549 } else {
scroggo19b91532016-10-24 09:03:26 -0700550 if (useIncremental) {
551 // Error: These should support incremental decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700552 return "Could not start incremental decode";
553 }
554 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
555 // which should work via startScanlineDecode
556 useOldScanlineMethod = true;
557 }
558 }
559
560 if (useOldScanlineMethod) {
561 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
562 &colorCount)) {
563 return "Could not start scanline decoder";
564 }
565
566 switch (codec->getScanlineOrder()) {
567 case SkCodec::kTopDown_SkScanlineOrder:
568 case SkCodec::kBottomUp_SkScanlineOrder:
569 // We do not need to check the return value. On an incomplete
570 // image, memory will be filled with a default value.
571 codec->getScanlines(dst, height, rowBytes);
572 break;
msarett10522ff2015-09-07 08:54:01 -0700573 }
574 }
575
msarettb1be46b2016-05-17 08:52:11 -0700576 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCount, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700577 break;
578 }
msarett0a242972015-06-11 14:27:27 -0700579 case kStripe_Mode: {
580 const int height = decodeInfo.height();
581 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
582 // does not align with image blocks.
583 const int stripeHeight = 37;
584 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
msarettb1be46b2016-05-17 08:52:11 -0700585 void* dst = pixels.get();
msarett0a242972015-06-11 14:27:27 -0700586
587 // Decode odd stripes
msarettb1be46b2016-05-17 08:52:11 -0700588 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, nullptr, colorPtr,
589 &colorCount)) {
msarettb65e6042016-02-23 05:37:25 -0800590 return "Could not start scanline decoder";
591 }
592
593 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
594 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
595 // to run this test for image types that do not have this scanline ordering.
scroggo12e2f502016-05-16 09:04:18 -0700596 // We only run this on Jpeg, which is always kTopDown.
597 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
msarette6dd0042015-10-09 11:07:34 -0700598
msarett0a242972015-06-11 14:27:27 -0700599 for (int i = 0; i < numStripes; i += 2) {
600 // Skip a stripe
601 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700602 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700603
604 // Read a stripe
605 const int startY = (i + 1) * stripeHeight;
606 const int linesToRead = SkTMin(stripeHeight, height - startY);
607 if (linesToRead > 0) {
msarettb1be46b2016-05-17 08:52:11 -0700608 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
609 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700610 }
611 }
612
613 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700614 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
msarettb1be46b2016-05-17 08:52:11 -0700615 colorPtr, &colorCount);
scroggo1c005e42015-08-04 09:24:45 -0700616 if (SkCodec::kSuccess != startResult) {
617 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700618 }
619 for (int i = 0; i < numStripes; i += 2) {
620 // Read a stripe
621 const int startY = i * stripeHeight;
622 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarettb1be46b2016-05-17 08:52:11 -0700623 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
624 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700625
626 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700627 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
628 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700629 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700630 }
631 }
msarettb1be46b2016-05-17 08:52:11 -0700632
633 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCount, fDstColorType);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700634 break;
msarett0a242972015-06-11 14:27:27 -0700635 }
msarett91c22b22016-02-22 12:27:46 -0800636 case kCroppedScanline_Mode: {
637 const int width = decodeInfo.width();
638 const int height = decodeInfo.height();
639 // This value is chosen because, as we move across the image, it will sometimes
640 // align with the jpeg block sizes and it will sometimes not. This allows us
641 // to test interestingly different code paths in the implementation.
642 const int tileSize = 36;
643
644 SkCodec::Options opts;
645 SkIRect subset;
646 for (int x = 0; x < width; x += tileSize) {
647 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
648 opts.fSubset = &subset;
649 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &opts,
msarettb1be46b2016-05-17 08:52:11 -0700650 colorPtr, &colorCount)) {
msarett91c22b22016-02-22 12:27:46 -0800651 return "Could not start scanline decoder.";
652 }
653
msarettb1be46b2016-05-17 08:52:11 -0700654 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
msarett91c22b22016-02-22 12:27:46 -0800655 }
656
msarettb1be46b2016-05-17 08:52:11 -0700657 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount,
658 fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800659 break;
660 }
scroggob636b452015-07-22 07:16:20 -0700661 case kSubset_Mode: {
662 // Arbitrarily choose a divisor.
663 int divisor = 2;
664 // Total width/height of the image.
665 const int W = codec->getInfo().width();
666 const int H = codec->getInfo().height();
667 if (divisor > W || divisor > H) {
668 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
669 "for %s with dimensions (%d x %d)", divisor,
670 fPath.c_str(), W, H));
671 }
672 // subset dimensions
673 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
674 const int w = SkAlign2(W / divisor);
675 const int h = SkAlign2(H / divisor);
676 SkIRect subset;
677 SkCodec::Options opts;
678 opts.fSubset = &subset;
679 SkBitmap subsetBm;
680 // We will reuse pixel memory from bitmap.
msarettb1be46b2016-05-17 08:52:11 -0700681 void* dst = pixels.get();
scroggob636b452015-07-22 07:16:20 -0700682 // Keep track of left and top (for drawing subsetBm into canvas). We could use
683 // fScale * x and fScale * y, but we want integers such that the next subset will start
684 // where the last one ended. So we'll add decodeInfo.width() and height().
685 int left = 0;
686 for (int x = 0; x < W; x += w) {
687 int top = 0;
688 for (int y = 0; y < H; y+= h) {
689 // Do not make the subset go off the edge of the image.
690 const int preScaleW = SkTMin(w, W - x);
691 const int preScaleH = SkTMin(h, H - y);
692 subset.setXYWH(x, y, preScaleW, preScaleH);
693 // And scale
694 // FIXME: Should we have a version of getScaledDimensions that takes a subset
695 // into account?
msarettc7eb4902016-04-25 07:04:58 -0700696 const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale));
697 const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale));
698 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
699 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
msarettb1be46b2016-05-17 08:52:11 -0700700 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
701 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
702 &opts, colorPtr, &colorCount);
scroggob636b452015-07-22 07:16:20 -0700703 switch (result) {
704 case SkCodec::kSuccess:
705 case SkCodec::kIncompleteInput:
706 break;
scroggob636b452015-07-22 07:16:20 -0700707 default:
708 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
709 "from %s with dimensions (%d x %d)\t error %d",
710 x, y, decodeInfo.width(), decodeInfo.height(),
711 fPath.c_str(), W, H, result);
712 }
msarettb1be46b2016-05-17 08:52:11 -0700713 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, colorPtr,
714 colorCount, fDstColorType, SkIntToScalar(left),
715 SkIntToScalar(top));
716
scroggob636b452015-07-22 07:16:20 -0700717 // translate by the scaled height.
718 top += decodeInfo.height();
719 }
720 // translate by the scaled width.
721 left += decodeInfo.width();
722 }
723 return "";
724 }
msarettb714fb02016-01-22 14:46:42 -0800725 default:
726 SkASSERT(false);
727 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700728 }
scroggo9c59ebc2015-03-25 13:48:49 -0700729 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700730}
731
732SkISize CodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700733 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400734 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700735 if (nullptr == codec) {
736 return SkISize::Make(0, 0);
737 }
scroggo19b91532016-10-24 09:03:26 -0700738
739 auto imageSize = codec->getScaledDimensions(fScale);
740 if (fMode == kAnimated_Mode) {
scroggo90e971a2016-10-25 06:11:01 -0700741 // We'll draw one of each frame, so make it big enough to hold them all
742 // in a grid. The grid will be roughly square, with "factor" frames per
743 // row and up to "factor" rows.
scroggo19b91532016-10-24 09:03:26 -0700744 const size_t count = codec->getFrameInfo().size();
scroggo90e971a2016-10-25 06:11:01 -0700745 const float root = sqrt((float) count);
746 const int factor = sk_float_ceil2int(root);
747 imageSize.fWidth = imageSize.fWidth * factor;
748 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
scroggo19b91532016-10-24 09:03:26 -0700749 }
750 return imageSize;
scroggo9b77ddd2015-03-19 06:03:39 -0700751}
752
753Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700754 if (1.0f == fScale) {
scroggo6e8c68e2016-10-24 13:48:49 -0700755 Name name = SkOSPath::Basename(fPath.c_str());
756 if (fMode == kAnimated_Mode) {
757 name.append("_animated");
758 }
759 return name;
msarett0a242972015-06-11 14:27:27 -0700760 }
scroggo6e8c68e2016-10-24 13:48:49 -0700761 SkASSERT(fMode != kAnimated_Mode);
msaretta5783ae2015-09-08 15:35:32 -0700762 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700763}
764
765/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
766
scroggof8dc9df2016-05-16 09:04:13 -0700767AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800768 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700769 : fPath(path)
msarett3d9d7a72015-10-21 10:27:10 -0700770 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800771 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700772 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800773 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700774{}
775
776bool AndroidCodecSrc::veto(SinkFlags flags) const {
777 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700778 return flags.type != SinkFlags::kRaster
779 || flags.approach != SinkFlags::kDirect;
780}
781
782Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700783 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700784 if (!encoded) {
785 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
786 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400787 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700788 if (nullptr == codec.get()) {
789 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
790 }
791
scroggoba584892016-05-20 13:56:13 -0700792 SkImageInfo decodeInfo = codec->getInfo();
793 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
794 fDstAlphaType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700795 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
796 }
797
798 // Scale the image if it is desired.
799 SkISize size = codec->getSampledDimensions(fSampleSize);
800
801 // Visually inspecting very small output images is not necessary. We will
802 // cover these cases in unit testing.
803 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
804 return Error::Nonfatal("Scaling very small images is uninteresting.");
805 }
806 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
807
msarettb1be46b2016-05-17 08:52:11 -0700808 int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
809 size_t rowBytes = size.width() * bpp;
810 SkAutoMalloc pixels(size.height() * rowBytes);
811 SkPMColor colorPtr[256];
812 int colorCount = 256;
msarett3d9d7a72015-10-21 10:27:10 -0700813
814 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700815 SkImageInfo bitmapInfo = decodeInfo;
816 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
817 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
818 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
819 }
msarett3d9d7a72015-10-21 10:27:10 -0700820
821 // Create options for the codec.
822 SkAndroidCodec::AndroidOptions options;
823 options.fColorPtr = colorPtr;
msarettb1be46b2016-05-17 08:52:11 -0700824 options.fColorCount = &colorCount;
msarett3d9d7a72015-10-21 10:27:10 -0700825 options.fSampleSize = fSampleSize;
826
msarettb1be46b2016-05-17 08:52:11 -0700827 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggof8dc9df2016-05-16 09:04:13 -0700828 case SkCodec::kSuccess:
829 case SkCodec::kIncompleteInput:
830 break;
msarett3d9d7a72015-10-21 10:27:10 -0700831 default:
scroggof8dc9df2016-05-16 09:04:13 -0700832 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700833 }
msarettb1be46b2016-05-17 08:52:11 -0700834 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount, fDstColorType);
scroggof8dc9df2016-05-16 09:04:13 -0700835 return "";
msarett3d9d7a72015-10-21 10:27:10 -0700836}
837
838SkISize AndroidCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700839 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400840 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700841 if (nullptr == codec) {
842 return SkISize::Make(0, 0);
843 }
844 return codec->getSampledDimensions(fSampleSize);
845}
846
847Name AndroidCodecSrc::name() const {
848 // We will replicate the names used by CodecSrc so that images can
849 // be compared in Gold.
850 if (1 == fSampleSize) {
851 return SkOSPath::Basename(fPath.c_str());
852 }
msarett4b0778e2015-11-13 09:59:11 -0800853 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700854}
855
856/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
857
msarett18976312016-03-09 14:20:58 -0800858ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
859 : fPath(path)
860 , fMode(mode)
861 , fDstAlphaType(alphaType)
862 , fIsGpu(isGpu)
863 , fRunSerially(serial_from_path_name(path))
864{}
865
866bool ImageGenSrc::veto(SinkFlags flags) const {
867 if (fIsGpu) {
868 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect;
869 }
870
871 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
872}
873
874Error ImageGenSrc::draw(SkCanvas* canvas) const {
875 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
876 return Error::Nonfatal("Uninteresting to test image generator to 565.");
877 }
878
bungeman38d909e2016-08-02 14:40:46 -0700879 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett18976312016-03-09 14:20:58 -0800880 if (!encoded) {
881 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
882 }
883
msarettfc0b6d12016-03-17 13:50:17 -0700884#if defined(SK_BUILD_FOR_WIN)
885 // Initialize COM in order to test with WIC.
886 SkAutoCoInitialize com;
887 if (!com.succeeded()) {
888 return "Could not initialize COM.";
889 }
890#endif
891
Ben Wagner145dbcd2016-11-03 14:40:50 -0400892 std::unique_ptr<SkImageGenerator> gen(nullptr);
msarett18976312016-03-09 14:20:58 -0800893 switch (fMode) {
894 case kCodec_Mode:
bungeman38d909e2016-08-02 14:40:46 -0700895 gen.reset(SkCodecImageGenerator::NewFromEncodedCodec(encoded.get()));
msarett18976312016-03-09 14:20:58 -0800896 if (!gen) {
897 return "Could not create codec image generator.";
898 }
899 break;
msarettfc0b6d12016-03-17 13:50:17 -0700900 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800901#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
bungeman38d909e2016-08-02 14:40:46 -0700902 gen.reset(SkImageGeneratorCG::NewFromEncodedCG(encoded.get()));
msarettfc0b6d12016-03-17 13:50:17 -0700903#elif defined(SK_BUILD_FOR_WIN)
bungeman38d909e2016-08-02 14:40:46 -0700904 gen.reset(SkImageGeneratorWIC::NewFromEncodedWIC(encoded.get()));
msarettfc0b6d12016-03-17 13:50:17 -0700905#endif
906
msarett18976312016-03-09 14:20:58 -0800907 if (!gen) {
msarettfc0b6d12016-03-17 13:50:17 -0700908 return "Could not create platform image generator.";
msarett18976312016-03-09 14:20:58 -0800909 }
910 break;
msarettfc0b6d12016-03-17 13:50:17 -0700911 }
msarett18976312016-03-09 14:20:58 -0800912 default:
913 SkASSERT(false);
914 return "Invalid image generator mode";
915 }
916
917 // Test deferred decoding path on GPU
918 if (fIsGpu) {
reed9ce9d672016-03-17 10:51:11 -0700919 sk_sp<SkImage> image(SkImage::MakeFromGenerator(gen.release(), nullptr));
msarett18976312016-03-09 14:20:58 -0800920 if (!image) {
921 return "Could not create image from codec image generator.";
922 }
923 canvas->drawImage(image, 0, 0);
924 return "";
925 }
mtklein343a63d2016-03-22 11:46:53 -0700926
msarett18976312016-03-09 14:20:58 -0800927 // Test various color and alpha types on CPU
928 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700929
msarettb1be46b2016-05-17 08:52:11 -0700930 int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
931 size_t rowBytes = decodeInfo.width() * bpp;
932 SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
933 SkPMColor colorPtr[256];
934 int colorCount = 256;
msarett18976312016-03-09 14:20:58 -0800935
msarettb1be46b2016-05-17 08:52:11 -0700936 if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes, colorPtr, &colorCount)) {
msarett18976312016-03-09 14:20:58 -0800937 return SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str());
938 }
939
msarettb1be46b2016-05-17 08:52:11 -0700940 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes, colorPtr, colorCount,
941 CodecSrc::kGetFromCanvas_DstColorType);
msarett18976312016-03-09 14:20:58 -0800942 return "";
943}
944
945SkISize ImageGenSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700946 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400947 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett18976312016-03-09 14:20:58 -0800948 if (nullptr == codec) {
949 return SkISize::Make(0, 0);
950 }
951 return codec->getInfo().dimensions();
952}
953
954Name ImageGenSrc::name() const {
955 return SkOSPath::Basename(fPath.c_str());
956}
957
958/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
959
msarett9ce3a542016-07-15 13:54:38 -0700960ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, SkColorType colorType)
msarett69deca82016-04-29 09:38:40 -0700961 : fPath(path)
962 , fMode(mode)
msarett9ce3a542016-07-15 13:54:38 -0700963 , fColorType(colorType)
msarett69deca82016-04-29 09:38:40 -0700964{}
965
966bool ColorCodecSrc::veto(SinkFlags flags) const {
967 // Test to direct raster backends (8888 and 565).
968 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
969}
970
971Error ColorCodecSrc::draw(SkCanvas* canvas) const {
972 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
973 return Error::Nonfatal("No need to test color correction to 565 backend.");
974 }
975
msarettd1ec89b2016-08-03 12:59:27 -0700976 bool runInLegacyMode = kBaseline_Mode == fMode;
msarettd1ec89b2016-08-03 12:59:27 -0700977 if (runInLegacyMode && canvas->imageInfo().colorSpace()) {
978 return Error::Nonfatal("Skipping tests that are only interesting in legacy mode.");
979 } else if (!runInLegacyMode && !canvas->imageInfo().colorSpace()) {
980 return Error::Nonfatal("Skipping tests that are only interesting in srgb mode.");
msarett9ce3a542016-07-15 13:54:38 -0700981 }
982
bungeman38d909e2016-08-02 14:40:46 -0700983 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett69deca82016-04-29 09:38:40 -0700984 if (!encoded) {
985 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
986 }
987
Ben Wagner145dbcd2016-11-03 14:40:50 -0400988 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett69deca82016-04-29 09:38:40 -0700989 if (nullptr == codec.get()) {
990 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
991 }
992
msarett9876ac52016-06-01 14:47:18 -0700993 // Load the dst ICC profile. This particular dst is fairly similar to Adobe RGB.
994 sk_sp<SkData> dstData = SkData::MakeFromFileName(
msaretta714bc32016-07-29 08:58:33 -0700995 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
msarett9876ac52016-06-01 14:47:18 -0700996 if (!dstData) {
997 return "Cannot read monitor profile. Is the resource path set correctly?";
998 }
999
msarett50ce1f22016-07-29 06:23:33 -07001000 sk_sp<SkColorSpace> dstSpace = nullptr;
1001 if (kDst_sRGB_Mode == fMode) {
Brian Osman526972e2016-10-24 09:24:02 -04001002 dstSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
msarett50ce1f22016-07-29 06:23:33 -07001003 } else if (kDst_HPZR30w_Mode == fMode) {
Brian Osman526972e2016-10-24 09:24:02 -04001004 dstSpace = SkColorSpace::MakeICC(dstData->data(), dstData->size());
msarett50ce1f22016-07-29 06:23:33 -07001005 }
1006
1007 SkImageInfo decodeInfo = codec->getInfo().makeColorType(fColorType).makeColorSpace(dstSpace);
msarettd1ec89b2016-08-03 12:59:27 -07001008 if (kUnpremul_SkAlphaType == decodeInfo.alphaType()) {
1009 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
1010 }
msarett2ecc35f2016-09-08 11:55:16 -07001011 if (kRGBA_F16_SkColorType == fColorType) {
raftias94888332016-10-18 10:02:51 -07001012 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(decodeInfo.colorSpace())->type());
1013 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(decodeInfo.colorSpace());
1014 decodeInfo = decodeInfo.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001015 }
msarettd1ec89b2016-08-03 12:59:27 -07001016
msarett50ce1f22016-07-29 06:23:33 -07001017 SkImageInfo bitmapInfo = decodeInfo;
1018 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
1019 kBGRA_8888_SkColorType == decodeInfo.colorType())
1020 {
1021 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
1022 }
1023
1024 SkBitmap bitmap;
1025 if (!bitmap.tryAllocPixels(bitmapInfo)) {
1026 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
1027 bitmapInfo.width(), bitmapInfo.height());
1028 }
1029
1030 size_t rowBytes = bitmap.rowBytes();
1031 SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), rowBytes);
msarettf7eb6fc2016-09-13 09:04:11 -07001032 if (SkCodec::kSuccess != r && SkCodec::kIncompleteInput != r) {
raftias54761282016-12-01 13:44:07 -05001033 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
msarett50ce1f22016-07-29 06:23:33 -07001034 }
1035
msarett69deca82016-04-29 09:38:40 -07001036 switch (fMode) {
1037 case kBaseline_Mode:
msarett39979d82016-07-28 17:11:18 -07001038 case kDst_sRGB_Mode:
msarett50ce1f22016-07-29 06:23:33 -07001039 case kDst_HPZR30w_Mode:
msarett39979d82016-07-28 17:11:18 -07001040 canvas->drawBitmap(bitmap, 0, 0);
1041 break;
msarett69deca82016-04-29 09:38:40 -07001042 default:
1043 SkASSERT(false);
1044 return "Invalid fMode";
1045 }
1046 return "";
1047}
1048
1049SkISize ColorCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001050 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -04001051 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001052 if (nullptr == codec) {
1053 return SkISize::Make(0, 0);
1054 }
1055 return SkISize::Make(codec->getInfo().width(), codec->getInfo().height());
1056}
1057
1058Name ColorCodecSrc::name() const {
1059 return SkOSPath::Basename(fPath.c_str());
1060}
1061
1062/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1063
mtkleinf4ba3212015-01-28 15:32:24 -08001064static const SkRect kSKPViewport = {0,0, 1000,1000};
1065
mtklein8d17a132015-01-30 11:42:31 -08001066SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -08001067
1068Error SKPSrc::draw(SkCanvas* canvas) const {
bungemanf93d7112016-09-16 06:24:20 -07001069 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
mtklein75d98fd2015-01-18 07:05:01 -08001070 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -08001071 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1072 }
bungemanf93d7112016-09-16 06:24:20 -07001073 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
mtklein75d98fd2015-01-18 07:05:01 -08001074 if (!pic) {
1075 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
1076 }
bungemanf93d7112016-09-16 06:24:20 -07001077 stream = nullptr; // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -07001078
mtkleinf4ba3212015-01-28 15:32:24 -08001079 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -08001080 canvas->drawPicture(pic);
1081 return "";
1082}
1083
1084SkISize SKPSrc::size() const {
bungemanf93d7112016-09-16 06:24:20 -07001085 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
mtkleinffa901a2015-03-16 10:38:07 -07001086 if (!stream) {
1087 return SkISize::Make(0,0);
1088 }
1089 SkPictInfo info;
bungemanf93d7112016-09-16 06:24:20 -07001090 if (!SkPicture::InternalOnly_StreamIsSKP(stream.get(), &info)) {
mtkleinffa901a2015-03-16 10:38:07 -07001091 return SkISize::Make(0,0);
1092 }
1093 SkRect viewport = kSKPViewport;
1094 if (!viewport.intersect(info.fCullRect)) {
1095 return SkISize::Make(0,0);
1096 }
1097 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001098}
1099
1100Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1101
1102/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fmalitaa2b9fdf2016-08-03 19:53:36 -07001103#if defined(SK_XML)
fmalitabdf3e5c2016-09-17 07:26:26 -07001104// Used when the image doesn't have an intrinsic size.
1105static const SkSize kDefaultSVGSize = SkSize::Make(1000, 1000);
fmalitaa2b9fdf2016-08-03 19:53:36 -07001106
fmalitabdf3e5c2016-09-17 07:26:26 -07001107// Used to force-scale tiny fixed-size images.
1108static const SkSize kMinimumSVGSize = SkSize::Make(128, 128);
fmalitaa2b9fdf2016-08-03 19:53:36 -07001109
fmalitaacd2f5c2016-11-08 07:13:45 -08001110SVGSrc::SVGSrc(Path path)
1111 : fName(SkOSPath::Basename(path.c_str()))
1112 , fScale(1) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001113
fmalitaacd2f5c2016-11-08 07:13:45 -08001114 SkFILEStream stream(path.c_str());
1115 if (!stream.isValid()) {
1116 return;
1117 }
1118 fDom = SkSVGDOM::MakeFromStream(stream);
1119 if (!fDom) {
1120 return;
1121 }
1122
1123 const SkSize& sz = fDom->containerSize();
1124 if (sz.isEmpty()) {
1125 // no intrinsic size
1126 fDom->setContainerSize(kDefaultSVGSize);
1127 } else {
1128 fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(),
1129 kMinimumSVGSize.height() / sz.height()));
1130 }
1131}
1132
1133Error SVGSrc::draw(SkCanvas* canvas) const {
fmalitabdf3e5c2016-09-17 07:26:26 -07001134 if (!fDom) {
fmalitaacd2f5c2016-11-08 07:13:45 -08001135 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
fmalitaa2b9fdf2016-08-03 19:53:36 -07001136 }
1137
fmalitaacd2f5c2016-11-08 07:13:45 -08001138 SkAutoCanvasRestore acr(canvas, true);
1139 canvas->scale(fScale, fScale);
1140 fDom->render(canvas);
1141
fmalitaa2b9fdf2016-08-03 19:53:36 -07001142 return "";
1143}
1144
1145SkISize SVGSrc::size() const {
fmalitaacd2f5c2016-11-08 07:13:45 -08001146 if (!fDom) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001147 return SkISize::Make(0, 0);
1148 }
1149
1150 return SkSize::Make(fDom->containerSize().width() * fScale,
1151 fDom->containerSize().height() * fScale).toRound();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001152}
1153
fmalitaacd2f5c2016-11-08 07:13:45 -08001154Name SVGSrc::name() const { return fName; }
fmalitaa2b9fdf2016-08-03 19:53:36 -07001155
fmalita179d8852016-08-16 14:23:29 -07001156bool SVGSrc::veto(SinkFlags flags) const {
1157 // No need to test to non-(raster||gpu) or indirect backends.
1158 bool type_ok = flags.type == SinkFlags::kRaster
1159 || flags.type == SinkFlags::kGPU;
1160
1161 return !type_ok || flags.approach != SinkFlags::kDirect;
1162}
1163
fmalitaa2b9fdf2016-08-03 19:53:36 -07001164#endif // defined(SK_XML)
1165/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein748ca3b2015-01-15 10:56:12 -08001166
halcanary45420a92016-06-02 12:41:14 -07001167MSKPSrc::MSKPSrc(Path path) : fPath(path) {
bungemanf93d7112016-09-16 06:24:20 -07001168 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
halcanary45420a92016-06-02 12:41:14 -07001169 (void)fReader.init(stream.get());
1170}
1171
1172int MSKPSrc::pageCount() const { return fReader.pageCount(); }
1173
1174SkISize MSKPSrc::size() const { return this->size(0); }
1175SkISize MSKPSrc::size(int i) const { return fReader.pageSize(i).toCeil(); }
1176
1177Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); }
1178Error MSKPSrc::draw(int i, SkCanvas* canvas) const {
bungemanf93d7112016-09-16 06:24:20 -07001179 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
halcanary45420a92016-06-02 12:41:14 -07001180 if (!stream) {
1181 return SkStringPrintf("Unable to open file: %s", fPath.c_str());
1182 }
1183 if (fReader.pageCount() == 0) {
1184 return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
1185 }
1186 if (i >= fReader.pageCount()) {
1187 return SkStringPrintf("MultiPictureDocument page number out of range: %d", i);
1188 }
1189 sk_sp<SkPicture> page = fReader.readPage(stream.get(), i);
1190 if (!page) {
1191 return SkStringPrintf("SkMultiPictureDocumentReader failed on page %d: %s",
1192 i, fPath.c_str());
1193 }
1194 canvas->drawPicture(page);
1195 return "";
1196}
1197
1198Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1199
1200/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1201
mtkleinad66f9b2015-02-13 15:11:10 -08001202Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
Mike Reed5df49342016-11-12 08:06:55 -06001203 return src.draw(SkMakeNullCanvas().get());
mtkleinad66f9b2015-02-13 15:11:10 -08001204}
1205
1206/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1207
mtkleinb9eb4ac2015-02-02 18:26:03 -08001208DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
1209
bsalomon85b4b532016-04-05 11:06:27 -07001210GPUSink::GPUSink(GrContextFactory::ContextType ct,
1211 GrContextFactory::ContextOptions options,
mtklein82d28432015-01-15 12:46:02 -08001212 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -07001213 bool diText,
brianosmand93c1202016-03-10 07:49:08 -08001214 SkColorType colorType,
brianosmanb109b8c2016-06-16 13:03:24 -07001215 sk_sp<SkColorSpace> colorSpace,
mtklein82d28432015-01-15 12:46:02 -08001216 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -08001217 : fContextType(ct)
kkinnunen5219fd92015-12-10 06:28:13 -08001218 , fContextOptions(options)
mtklein748ca3b2015-01-15 10:56:12 -08001219 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -07001220 , fUseDIText(diText)
brianosmand93c1202016-03-10 07:49:08 -08001221 , fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001222 , fColorSpace(std::move(colorSpace))
mtklein82d28432015-01-15 12:46:02 -08001223 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -08001224
bsalomon648c6962015-10-23 09:06:59 -07001225DEFINE_bool(imm, false, "Run gpu configs in immediate mode.");
Brian Salomon09d994e2016-12-21 11:14:46 -05001226DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
1227DEFINE_int32(opLookback, -1, "Maximum GrOp lookback for combining, negative means default.");
1228DEFINE_int32(opLookahead, -1, "Maximum GrOp lookahead for combining, negative means default.");
bsalomon648c6962015-10-23 09:06:59 -07001229
mtkleinb9eb4ac2015-02-02 18:26:03 -08001230Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
kkinnunen5219fd92015-12-10 06:28:13 -08001231 GrContextOptions grOptions;
bsalomon489147c2015-12-14 12:13:09 -08001232 grOptions.fImmediateMode = FLAGS_imm;
Brian Salomon09d994e2016-12-21 11:14:46 -05001233 grOptions.fClipDrawOpsToBounds = FLAGS_drawOpClip;
1234 grOptions.fMaxOpCombineLookback = FLAGS_opLookback;
1235 grOptions.fMaxOpCombineLookahead = FLAGS_opLookahead;
kkinnunen64492c42015-12-08 01:24:40 -08001236
kkinnunen5219fd92015-12-10 06:28:13 -08001237 src.modifyGrContextOptions(&grOptions);
1238
1239 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -08001240 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001241 const SkImageInfo info =
brianosmand93c1202016-03-10 07:49:08 -08001242 SkImageInfo::Make(size.width(), size.height(), fColorType,
brianosmanb109b8c2016-06-16 13:03:24 -07001243 kPremul_SkAlphaType, fColorSpace);
msarett13a036b2016-02-08 09:10:47 -08001244#if SK_SUPPORT_GPU
bsalomon8b7451a2016-05-11 06:33:06 -07001245 GrContext* context = factory.getContextInfo(fContextType, fContextOptions).grContext();
1246 const int maxDimension = context->caps()->maxTextureSize();
msarett13a036b2016-02-08 09:10:47 -08001247 if (maxDimension < SkTMax(size.width(), size.height())) {
1248 return Error::Nonfatal("Src too large to create a texture.\n");
1249 }
1250#endif
1251
reede8f30622016-03-23 18:59:25 -07001252 auto surface(
kkinnunen3e980c32015-12-23 01:33:00 -08001253 NewGpuSurface(&factory, fContextType, fContextOptions, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -08001254 if (!surface) {
1255 return "Could not create a surface.";
1256 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001257 if (FLAGS_preAbandonGpuContext) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001258 factory.abandonContexts();
1259 }
mtklein748ca3b2015-01-15 10:56:12 -08001260 SkCanvas* canvas = surface->getCanvas();
1261 Error err = src.draw(canvas);
1262 if (!err.isEmpty()) {
1263 return err;
1264 }
1265 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001266 if (FLAGS_gpuStats) {
1267 canvas->getGrContext()->dumpCacheStats(log);
1268 canvas->getGrContext()->dumpGpuStats(log);
1269 }
mtklein748ca3b2015-01-15 10:56:12 -08001270 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -08001271 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001272 if (FLAGS_abandonGpuContext) {
1273 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001274 } else if (FLAGS_releaseAndAbandonGpuContext) {
1275 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001276 }
mtklein748ca3b2015-01-15 10:56:12 -08001277 return "";
1278}
1279
1280/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1281
halcanary47ef4d52015-03-03 09:13:09 -08001282static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001283 if (src.size().isEmpty()) {
1284 return "Source has empty dimensions";
1285 }
halcanary47ef4d52015-03-03 09:13:09 -08001286 SkASSERT(doc);
halcanary45420a92016-06-02 12:41:14 -07001287 int pageCount = src.pageCount();
1288 for (int i = 0; i < pageCount; ++i) {
1289 int width = src.size(i).width(), height = src.size(i).height();
halcanary7e798182015-04-14 14:06:18 -07001290 SkCanvas* canvas =
1291 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1292 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001293 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001294 }
halcanary45420a92016-06-02 12:41:14 -07001295 Error err = src.draw(i, canvas);
halcanary7e798182015-04-14 14:06:18 -07001296 if (!err.isEmpty()) {
1297 return err;
1298 }
1299 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001300 }
reedd14df7c2016-09-22 14:12:46 -07001301 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -08001302 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001303 return "";
1304}
1305
halcanary47ef4d52015-03-03 09:13:09 -08001306Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary4b656662016-04-27 07:45:18 -07001307 SkDocument::PDFMetadata metadata;
1308 metadata.fTitle = src.name();
1309 metadata.fSubject = "rendering correctness test";
1310 metadata.fCreator = "Skia/DM";
1311 sk_sp<SkDocument> doc = SkDocument::MakePDF(dst, SK_ScalarDefaultRasterDPI,
1312 metadata, nullptr, fPDFA);
halcanary47ef4d52015-03-03 09:13:09 -08001313 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001314 return "SkDocument::MakePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001315 }
1316 return draw_skdocument(src, doc.get(), dst);
1317}
1318
1319/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1320
1321XPSSink::XPSSink() {}
1322
1323Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary4b656662016-04-27 07:45:18 -07001324 sk_sp<SkDocument> doc(SkDocument::MakeXPS(dst));
halcanary47ef4d52015-03-03 09:13:09 -08001325 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001326 return "SkDocument::MakeXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001327 }
1328 return draw_skdocument(src, doc.get(), dst);
1329}
reed54dc4872016-09-13 08:09:45 -07001330
1331/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1332
1333PipeSink::PipeSink() {}
1334
1335Error PipeSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1336 return src.draw(SkPipeSerializer().beginWrite(SkRect::Make(src.size()), dst));
1337}
1338
mtklein748ca3b2015-01-15 10:56:12 -08001339/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1340
mtklein9c3f17d2015-01-28 11:35:18 -08001341SKPSink::SKPSink() {}
1342
mtkleinb9eb4ac2015-02-02 18:26:03 -08001343Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001344 SkSize size;
1345 size = src.size();
1346 SkPictureRecorder recorder;
1347 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1348 if (!err.isEmpty()) {
1349 return err;
1350 }
reedca2622b2016-03-18 07:25:55 -07001351 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001352 return "";
1353}
1354
1355/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1356
Hal Canary85c7fe82016-10-25 10:33:27 -04001357Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1358 SkDebugCanvas debugCanvas(src.size().width(), src.size().height());
1359 Error err = src.draw(&debugCanvas);
1360 if (!err.isEmpty()) {
1361 return err;
1362 }
Mike Reed5df49342016-11-12 08:06:55 -06001363 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
Hal Canary85c7fe82016-10-25 10:33:27 -04001364 UrlDataManager dataManager(SkString("data"));
1365 Json::Value json = debugCanvas.toJSON(
1366 dataManager, debugCanvas.getSize(), nullCanvas.get());
1367 std::string value = Json::StyledWriter().write(json);
1368 return dst->write(value.c_str(), value.size()) ? "" : "SkWStream Error";
1369}
1370
1371/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1372
mtklein8a4527e2015-01-31 20:00:58 -08001373SVGSink::SVGSink() {}
1374
mtkleinb9eb4ac2015-02-02 18:26:03 -08001375Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita718df0a2016-07-15 10:33:29 -07001376#if defined(SK_XML)
Ben Wagner145dbcd2016-11-03 14:40:50 -04001377 std::unique_ptr<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
Mike Reed5df49342016-11-12 08:06:55 -06001378 return src.draw(SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
1379 SkIntToScalar(src.size().height())),
1380 xmlWriter.get()).get());
fmalita718df0a2016-07-15 10:33:29 -07001381#else
1382 return Error("SVG sink is disabled.");
1383#endif // SK_XML
mtklein8a4527e2015-01-31 20:00:58 -08001384}
1385
1386/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1387
brianosmanb109b8c2016-06-16 13:03:24 -07001388RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
mtklein27c3fdd2016-02-26 14:43:21 -08001389 : fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001390 , fColorSpace(std::move(colorSpace)) {}
mtklein748ca3b2015-01-15 10:56:12 -08001391
mtkleinb9eb4ac2015-02-02 18:26:03 -08001392Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001393 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001394 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1395 SkAlphaType alphaType = kPremul_SkAlphaType;
1396 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1397
mtkleinc8be09a2016-01-04 18:56:57 -08001398 SkMallocPixelRef::ZeroedPRFactory factory;
mtklein27c3fdd2016-02-26 14:43:21 -08001399 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(),
brianosmanb109b8c2016-06-16 13:03:24 -07001400 fColorType, alphaType, fColorSpace),
mtkleinc8be09a2016-01-04 18:56:57 -08001401 &factory,
1402 nullptr/*colortable*/);
mtklein748ca3b2015-01-15 10:56:12 -08001403 SkCanvas canvas(*dst);
1404 return src.draw(&canvas);
1405}
1406
1407/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1408
mtkleina16e69e2015-05-05 11:38:45 -07001409// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001410// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001411// Several examples below.
1412
mtkleincbf89782016-02-19 14:27:14 -08001413template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001414static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001415 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001416 class ProxySrc : public Src {
1417 public:
mtkleincbf89782016-02-19 14:27:14 -08001418 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001419 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001420 Name name() const override { return "ProxySrc"; }
1421 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001422 private:
mtkleincbf89782016-02-19 14:27:14 -08001423 SkISize fSize;
1424 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001425 };
msarett62d3b102015-12-10 15:14:27 -08001426 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001427}
1428
1429/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1430
mtklein4a34ecb2016-01-08 10:19:35 -08001431DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1432
1433// Is *bitmap identical to what you get drawing src into sink?
1434static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1435 // We can only check raster outputs.
1436 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1437 if (FLAGS_check && bitmap) {
1438 SkBitmap reference;
1439 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001440 SkDynamicMemoryWStream wStream;
1441 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001442 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1443 SkASSERT(err.isEmpty());
1444 if (!err.isEmpty()) {
1445 return err;
1446 }
1447 // The dimensions are a property of the Src only, and so should be identical.
1448 SkASSERT(reference.getSize() == bitmap->getSize());
1449 if (reference.getSize() != bitmap->getSize()) {
1450 return "Dimensions don't match reference";
1451 }
1452 // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy.
1453 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1454 return "Pixels don't match reference";
1455 }
1456 }
1457 return "";
1458}
1459
1460/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1461
mtkleind603b222015-02-17 11:13:33 -08001462static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1463 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1464 matrix->mapRect(&bounds);
1465 matrix->postTranslate(-bounds.x(), -bounds.y());
1466 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
1467}
1468
msarett62d3b102015-12-10 15:14:27 -08001469ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001470
mtkleinb9eb4ac2015-02-02 18:26:03 -08001471Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001472 SkMatrix matrix = fMatrix;
1473 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
Ben Wagner145dbcd2016-11-03 14:40:50 -04001474 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001475 canvas->concat(matrix);
1476 return src.draw(canvas);
1477 });
mtklein748ca3b2015-01-15 10:56:12 -08001478}
1479
mtkleind603b222015-02-17 11:13:33 -08001480// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1481// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001482ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001483
1484Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1485 Error err = fSink->draw(src, bitmap, stream, log);
1486 if (!err.isEmpty()) {
1487 return err;
1488 }
1489
1490 SkMatrix inverse;
1491 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1492 return "Cannot upright --matrix.";
1493 }
1494 SkMatrix upright = SkMatrix::I();
1495 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1496 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1497 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1498 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1499
1500 SkBitmap uprighted;
1501 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1502 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1503
1504 SkCanvas canvas(uprighted);
1505 canvas.concat(upright);
1506 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07001507 paint.setBlendMode(SkBlendMode::kSrc);
mtkleind603b222015-02-17 11:13:33 -08001508 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1509
1510 *bitmap = uprighted;
1511 bitmap->lockPixels();
1512 return "";
1513}
1514
mtklein748ca3b2015-01-15 10:56:12 -08001515/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1516
mtkleina16e69e2015-05-05 11:38:45 -07001517Error ViaSerialization::draw(
1518 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001519 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001520 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001521 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001522 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1523 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001524 if (!err.isEmpty()) {
1525 return err;
1526 }
reedca2622b2016-03-18 07:25:55 -07001527 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001528
1529 // Serialize it and then deserialize it.
reed39eaf5f2016-09-15 07:19:35 -07001530 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
mtklein748ca3b2015-01-15 10:56:12 -08001531
Ben Wagner145dbcd2016-11-03 14:40:50 -04001532 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001533 canvas->drawPicture(deserialized);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001534 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07001535 });
mtklein748ca3b2015-01-15 10:56:12 -08001536}
1537
1538/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1539
msarett62d3b102015-12-10 15:14:27 -08001540ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1541 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001542 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001543 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001544 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001545
mtkleinb9eb4ac2015-02-02 18:26:03 -08001546Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001547 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001548 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001549 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1550 SkIntToScalar(size.height()),
1551 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001552 if (!err.isEmpty()) {
1553 return err;
1554 }
reedca2622b2016-03-18 07:25:55 -07001555 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001556
Ben Wagner145dbcd2016-11-03 14:40:50 -04001557 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001558 const int xTiles = (size.width() + fW - 1) / fW,
1559 yTiles = (size.height() + fH - 1) / fH;
1560 SkMultiPictureDraw mpd(xTiles*yTiles);
reede8f30622016-03-23 18:59:25 -07001561 SkTArray<sk_sp<SkSurface>> surfaces;
1562// surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001563
mtkleina16e69e2015-05-05 11:38:45 -07001564 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1565 for (int j = 0; j < yTiles; j++) {
1566 for (int i = 0; i < xTiles; i++) {
1567 // This lets our ultimate Sink determine the best kind of surface.
1568 // E.g., if it's a GpuSink, the surfaces and images are textures.
reede8f30622016-03-23 18:59:25 -07001569 auto s = canvas->makeSurface(info);
mtkleina16e69e2015-05-05 11:38:45 -07001570 if (!s) {
reede8f30622016-03-23 18:59:25 -07001571 s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001572 }
reede8f30622016-03-23 18:59:25 -07001573 surfaces.push_back(s);
mtkleina16e69e2015-05-05 11:38:45 -07001574 SkCanvas* c = s->getCanvas();
1575 c->translate(SkIntToScalar(-i * fW),
1576 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
reedca2622b2016-03-18 07:25:55 -07001577 mpd.add(c, pic.get());
mtklein748ca3b2015-01-15 10:56:12 -08001578 }
mtklein748ca3b2015-01-15 10:56:12 -08001579 }
mtkleina16e69e2015-05-05 11:38:45 -07001580 mpd.draw();
1581 for (int j = 0; j < yTiles; j++) {
1582 for (int i = 0; i < xTiles; i++) {
reed9ce9d672016-03-17 10:51:11 -07001583 sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot());
mtkleina16e69e2015-05-05 11:38:45 -07001584 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1585 }
1586 }
mtkleina16e69e2015-05-05 11:38:45 -07001587 return "";
1588 });
mtklein748ca3b2015-01-15 10:56:12 -08001589}
1590
mtkleinb7e8d692015-04-07 08:30:32 -07001591/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1592
mtklein4a34ecb2016-01-08 10:19:35 -08001593Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1594 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001595 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtklein4a34ecb2016-01-08 10:19:35 -08001596 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001597 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08001598 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1599 SkIntToScalar(size.height())));
1600 if (!err.isEmpty()) {
1601 return err;
1602 }
reedca2622b2016-03-18 07:25:55 -07001603 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08001604 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001605 return check_against_reference(bitmap, src, fSink.get());
mtklein4a34ecb2016-01-08 10:19:35 -08001606 });
1607}
1608
1609/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1610
reedbabc3de2016-07-08 08:43:27 -07001611Error ViaDefer::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1612 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001613 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
reedbabc3de2016-07-08 08:43:27 -07001614 SkDeferredCanvas deferred(canvas);
1615 return src.draw(&deferred);
1616 });
1617}
1618
1619/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1620
reed54dc4872016-09-13 08:09:45 -07001621Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1622 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001623 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
reed54dc4872016-09-13 08:09:45 -07001624 SkDynamicMemoryWStream tmpStream;
1625 Error err = src.draw(SkPipeSerializer().beginWrite(SkRect::Make(size), &tmpStream));
1626 if (!err.isEmpty()) {
1627 return err;
1628 }
1629 sk_sp<SkData> data = tmpStream.detachAsData();
1630 SkPipeDeserializer().playback(data->data(), data->size(), canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001631 return check_against_reference(bitmap, src, fSink.get());
reed54dc4872016-09-13 08:09:45 -07001632 });
1633}
1634
1635/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1636
mtkleinb7e8d692015-04-07 08:30:32 -07001637// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1638// This tests that any shortcuts we may take while recording that second picture are legal.
1639Error ViaSecondPicture::draw(
1640 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001641 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001642 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001643 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001644 sk_sp<SkPicture> pic;
mtkleina16e69e2015-05-05 11:38:45 -07001645 for (int i = 0; i < 2; i++) {
1646 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1647 SkIntToScalar(size.height())));
1648 if (!err.isEmpty()) {
1649 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001650 }
reedca2622b2016-03-18 07:25:55 -07001651 pic = recorder.finishRecordingAsPicture();
mtkleinb7e8d692015-04-07 08:30:32 -07001652 }
mtkleina16e69e2015-05-05 11:38:45 -07001653 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001654 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07001655 });
mtkleinb7e8d692015-04-07 08:30:32 -07001656}
1657
mtkleind31c13d2015-05-05 12:59:56 -07001658/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1659
mtklein6fbf4b32015-05-06 11:35:40 -07001660// Draw the Src twice. This can help exercise caching.
1661Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001662 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001663 for (int i = 0; i < 2; i++) {
1664 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1665 canvas->clear(SK_ColorTRANSPARENT);
1666 Error err = src.draw(canvas);
1667 if (err.isEmpty()) {
1668 return err;
1669 }
1670 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001671 return check_against_reference(bitmap, src, fSink.get());
mtklein6fbf4b32015-05-06 11:35:40 -07001672 });
1673}
1674
1675/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1676
mtkleind31c13d2015-05-05 12:59:56 -07001677// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1678// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1679// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1680struct DrawsAsSingletonPictures {
1681 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001682 const SkDrawableList& fDrawables;
mtklein11064df2016-04-06 15:01:57 -07001683 SkRect fBounds;
mtkleind31c13d2015-05-05 12:59:56 -07001684
mtkleind31c13d2015-05-05 12:59:56 -07001685 template <typename T>
1686 void draw(const T& op, SkCanvas* canvas) {
1687 // We must pass SkMatrix::I() as our initial matrix.
1688 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1689 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001690 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1691 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001692 }
1693
mtklein449d9b72015-09-28 10:33:02 -07001694 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001695 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001696 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001697 SkPictureRecorder rec;
mtklein11064df2016-04-06 15:01:57 -07001698 this->draw(op, rec.beginRecording(fBounds));
reedca2622b2016-03-18 07:25:55 -07001699 sk_sp<SkPicture> pic(rec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07001700 fCanvas->drawPicture(pic);
1701 }
1702
mtklein449d9b72015-09-28 10:33:02 -07001703 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001704 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001705 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1706 this->draw(op, fCanvas);
1707 }
mtkleind31c13d2015-05-05 12:59:56 -07001708};
1709
mtkleind31c13d2015-05-05 12:59:56 -07001710// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1711// Then play back that macro picture into our wrapped sink.
1712Error ViaSingletonPictures::draw(
1713 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1714 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001715 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07001716 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1717 SkRecord skr;
1718 SkRecorder recorder(&skr, size.width(), size.height());
1719 Error err = src.draw(&recorder);
1720 if (!err.isEmpty()) {
1721 return err;
1722 }
1723
1724 // Record our macro-picture, with each draw op as its own sub-picture.
1725 SkPictureRecorder macroRec;
1726 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1727 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001728
bungeman6bd52842016-10-27 09:30:08 -07001729 std::unique_ptr<SkDrawableList> drawables(recorder.detachDrawableList());
mtkleind2baa902015-07-07 09:43:28 -07001730 const SkDrawableList empty;
1731
1732 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1733 macroCanvas,
1734 drawables ? *drawables : empty,
mtklein11064df2016-04-06 15:01:57 -07001735 SkRect::MakeWH((SkScalar)size.width(), (SkScalar)size.height()),
mtkleind2baa902015-07-07 09:43:28 -07001736 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001737 for (int i = 0; i < skr.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -07001738 skr.visit(i, drawsAsSingletonPictures);
mtkleind31c13d2015-05-05 12:59:56 -07001739 }
reedca2622b2016-03-18 07:25:55 -07001740 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07001741
1742 canvas->drawPicture(macroPic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001743 return check_against_reference(bitmap, src, fSink.get());
mtkleind31c13d2015-05-05 12:59:56 -07001744 });
1745}
1746
mtklein9c5052f2016-08-06 12:51:51 -07001747/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1748
1749Error ViaLite::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1750 auto size = src.size();
mtklein8bbbb692016-08-15 12:56:00 -07001751 SkRect bounds = {0,0, (SkScalar)size.width(), (SkScalar)size.height()};
Ben Wagner145dbcd2016-11-03 14:40:50 -04001752 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtklein8bbbb692016-08-15 12:56:00 -07001753 sk_sp<SkLiteDL> dl = SkLiteDL::New(bounds);
1754
1755 SkLiteRecorder rec;
1756 rec.reset(dl.get());
1757
1758 Error err = src.draw(&rec);
mtklein9c5052f2016-08-06 12:51:51 -07001759 if (!err.isEmpty()) {
1760 return err;
1761 }
mtklein8bbbb692016-08-15 12:56:00 -07001762 dl->draw(canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001763 return check_against_reference(bitmap, src, fSink.get());
mtklein9c5052f2016-08-06 12:51:51 -07001764 });
1765}
1766
mtklein748ca3b2015-01-15 10:56:12 -08001767} // namespace DM