blob: 643a1a48d08160c98caea59c6a7e92386ffb00ba [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"
Mike Klein841101d2017-03-10 09:55:51 -050016#include "SkColorSpaceXformCanvas.h"
Hal Canary95e3c052017-01-11 12:44:43 -050017#include "SkColorSpace_XYZ.h"
mtkleina16e69e2015-05-05 11:38:45 -070018#include "SkCommonFlags.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070019#include "SkData.h"
Hal Canary85c7fe82016-10-25 10:33:27 -040020#include "SkDebugCanvas.h"
reedbabc3de2016-07-08 08:43:27 -070021#include "SkDeferredCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080022#include "SkDocument.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070023#include "SkImageGenerator.h"
msarett18976312016-03-09 14:20:58 -080024#include "SkImageGeneratorCG.h"
msarettfc0b6d12016-03-17 13:50:17 -070025#include "SkImageGeneratorWIC.h"
mtklein8bbbb692016-08-15 12:56:00 -070026#include "SkLiteDL.h"
27#include "SkLiteRecorder.h"
mtkleinc8be09a2016-01-04 18:56:57 -080028#include "SkMallocPixelRef.h"
Hal Canary45cde312017-04-03 16:06:42 -040029#include "SkMultiPictureDocumentPriv.h"
mtklein748ca3b2015-01-15 10:56:12 -080030#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080031#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080032#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050033#include "SkOSPath.h"
msarett9e9444c2016-02-03 12:39:10 -080034#include "SkOpts.h"
mtkleinffa901a2015-03-16 10:38:07 -070035#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080036#include "SkPictureRecorder.h"
reed54dc4872016-09-13 08:09:45 -070037#include "SkPipe.h"
mtklein748ca3b2015-01-15 10:56:12 -080038#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070039#include "SkRecordDraw.h"
40#include "SkRecorder.h"
fmalita2aafe6f2015-02-06 12:51:10 -080041#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080042#include "SkStream.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000043#include "SkSwizzler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050044#include "SkTLogic.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000045#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050046#include <functional>
mtklein748ca3b2015-01-15 10:56:12 -080047
msarettfc0b6d12016-03-17 13:50:17 -070048#if defined(SK_BUILD_FOR_WIN)
49 #include "SkAutoCoInitialize.h"
Hal Canary5e221e72017-02-06 09:51:42 -050050 #include "SkHRESULT.h"
51 #include "SkTScopedComPtr.h"
52 #include <XpsObjectModel.h>
msarettfc0b6d12016-03-17 13:50:17 -070053#endif
54
fmalita718df0a2016-07-15 10:33:29 -070055#if defined(SK_XML)
fmalitaa48f0e32016-08-04 06:26:05 -070056 #include "SkSVGDOM.h"
fmalita718df0a2016-07-15 10:33:29 -070057 #include "SkXMLWriter.h"
58#endif
59
halcanary7e798182015-04-14 14:06:18 -070060DEFINE_bool(multiPage, false, "For document-type backends, render the source"
61 " into multiple pages");
scroggo3ac66e92016-02-08 15:09:48 -080062DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070063
bsalomon3724e572016-03-30 18:56:19 -070064using sk_gpu_test::GrContextFactory;
65
mtklein748ca3b2015-01-15 10:56:12 -080066namespace DM {
67
mtklein748ca3b2015-01-15 10:56:12 -080068GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
69
70Error GMSrc::draw(SkCanvas* canvas) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040071 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080072 canvas->concat(gm->getInitialTransform());
73 gm->draw(canvas);
74 return "";
75}
76
77SkISize GMSrc::size() 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->getISize();
80}
81
82Name GMSrc::name() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040083 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080084 return gm->getName();
85}
86
bsalomon4ee6bd82015-05-27 13:23:23 -070087void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040088 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070089 gm->modifyGrContextOptions(options);
90}
91
mtklein748ca3b2015-01-15 10:56:12 -080092/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
93
msarettd1227a72016-05-18 06:23:57 -070094BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
msaretta5783ae2015-09-08 15:35:32 -070095 : fPath(path)
msaretta5783ae2015-09-08 15:35:32 -070096 , fMode(mode)
97 , fDstColorType(dstColorType)
98 , fSampleSize(sampleSize)
99{}
100
101bool BRDSrc::veto(SinkFlags flags) const {
102 // No need to test to non-raster or indirect backends.
103 return flags.type != SinkFlags::kRaster
104 || flags.approach != SinkFlags::kDirect;
105}
106
msarettd1227a72016-05-18 06:23:57 -0700107static SkBitmapRegionDecoder* create_brd(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700108 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msaretta5783ae2015-09-08 15:35:32 -0700109 if (!encoded) {
110 return NULL;
111 }
reed42943c82016-09-12 12:01:44 -0700112 return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy);
msaretta5783ae2015-09-08 15:35:32 -0700113}
114
Matt Sarett334df3a2016-12-15 18:17:33 -0500115static inline void alpha8_to_gray8(SkBitmap* bitmap) {
116 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
117 // them back to kGray8 so our test framework can draw them correctly.
118 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
119 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
120 .makeAlphaType(kOpaque_SkAlphaType);
121 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
122 }
123}
124
msaretta5783ae2015-09-08 15:35:32 -0700125Error BRDSrc::draw(SkCanvas* canvas) const {
Matt Saretta40d9c82017-05-19 15:21:05 -0400126 if (canvas->imageInfo().colorSpace() &&
127 kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) {
128 // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only
129 // run these tests on legacy canvases.
130 // We allow an exception for F16, since Android uses F16.
131 return Error::Nonfatal("Skip testing to color correct canvas.");
132 }
133
msaretta5783ae2015-09-08 15:35:32 -0700134 SkColorType colorType = canvas->imageInfo().colorType();
135 if (kRGB_565_SkColorType == colorType &&
136 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
137 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
138 }
139 switch (fDstColorType) {
140 case CodecSrc::kGetFromCanvas_DstColorType:
141 break;
142 case CodecSrc::kIndex8_Always_DstColorType:
143 colorType = kIndex_8_SkColorType;
144 break;
145 case CodecSrc::kGrayscale_Always_DstColorType:
146 colorType = kGray_8_SkColorType;
147 break;
msarett34e0ec42016-04-22 16:27:24 -0700148 default:
149 SkASSERT(false);
150 break;
msaretta5783ae2015-09-08 15:35:32 -0700151 }
152
Ben Wagner145dbcd2016-11-03 14:40:50 -0400153 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700154 if (nullptr == brd.get()) {
155 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
156 }
157
msarett04965c62015-10-12 10:24:38 -0700158 if (!brd->conversionSupported(colorType)) {
mtklein9b439152015-12-09 13:02:26 -0800159 return Error::Nonfatal("Cannot convert to color type.");
msarett04965c62015-10-12 10:24:38 -0700160 }
161
msaretta5783ae2015-09-08 15:35:32 -0700162 const uint32_t width = brd->width();
163 const uint32_t height = brd->height();
164 // Visually inspecting very small output images is not necessary.
165 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
166 return Error::Nonfatal("Scaling very small images is uninteresting.");
167 }
168 switch (fMode) {
169 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700170 SkBitmap bitmap;
171 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
Matt Sarett68feef42017-04-11 09:51:32 -0400172 fSampleSize, colorType, false, SkColorSpace::MakeSRGB())) {
mtklein9b439152015-12-09 13:02:26 -0800173 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700174 }
Matt Sarett334df3a2016-12-15 18:17:33 -0500175 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700176 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700177 return "";
178 }
179 case kDivisor_Mode: {
180 const uint32_t divisor = 2;
181 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800182 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700183 }
184
185 // Use a border to test subsets that extend outside the image.
186 // We will not allow the border to be larger than the image dimensions. Allowing
187 // these large borders causes off by one errors that indicate a problem with the
188 // test suite, not a problem with the implementation.
189 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
190 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
191 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
192
193 // We may need to clear the canvas to avoid uninitialized memory.
194 // Assume we are scaling a 780x780 image with sampleSize = 8.
195 // The output image should be 97x97.
196 // Each subset will be 390x390.
197 // Each scaled subset be 48x48.
198 // Four scaled subsets will only fill a 96x96 image.
199 // The bottom row and last column will not be touched.
200 // This is an unfortunate result of our rounding rules when scaling.
201 // Maybe we need to consider testing scaled subsets without trying to
202 // combine them to match the full scaled image? Or maybe this is the
203 // best we can do?
204 canvas->clear(0);
205
206 for (uint32_t x = 0; x < divisor; x++) {
207 for (uint32_t y = 0; y < divisor; y++) {
208 // Calculate the subset dimensions
209 uint32_t subsetWidth = width / divisor;
210 uint32_t subsetHeight = height / divisor;
211 const int left = x * subsetWidth;
212 const int top = y * subsetHeight;
213
214 // Increase the size of the last subset in each row or column, when the
215 // divisor does not divide evenly into the image dimensions
216 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
217 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
218
219 // Increase the size of the subset in order to have a border on each side
220 const int decodeLeft = left - unscaledBorder;
221 const int decodeTop = top - unscaledBorder;
222 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
223 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700224 SkBitmap bitmap;
225 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
Matt Sarett68feef42017-04-11 09:51:32 -0400226 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false,
227 SkColorSpace::MakeSRGB())) {
mtklein9b439152015-12-09 13:02:26 -0800228 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700229 }
msaretta5783ae2015-09-08 15:35:32 -0700230
Matt Sarett334df3a2016-12-15 18:17:33 -0500231 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700232 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700233 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
234 (SkScalar) (subsetWidth / fSampleSize),
235 (SkScalar) (subsetHeight / fSampleSize)),
236 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
237 (SkScalar) (top / fSampleSize),
238 (SkScalar) (subsetWidth / fSampleSize),
239 (SkScalar) (subsetHeight / fSampleSize)),
240 nullptr);
241 }
242 }
243 return "";
244 }
245 default:
246 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800247 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700248 }
249}
250
251SkISize BRDSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400252 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700253 if (brd) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400254 return {SkTMax(1, brd->width() / (int)fSampleSize),
255 SkTMax(1, brd->height() / (int)fSampleSize)};
msaretta5783ae2015-09-08 15:35:32 -0700256 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400257 return {0, 0};
msaretta5783ae2015-09-08 15:35:32 -0700258}
259
260static SkString get_scaled_name(const Path& path, float scale) {
261 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
262}
263
264Name BRDSrc::name() const {
265 // We will replicate the names used by CodecSrc so that images can
266 // be compared in Gold.
267 if (1 == fSampleSize) {
268 return SkOSPath::Basename(fPath.c_str());
269 }
msarett4b0778e2015-11-13 09:59:11 -0800270 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700271}
272
273/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
274
scroggo3ac66e92016-02-08 15:09:48 -0800275static bool serial_from_path_name(const SkString& path) {
276 if (!FLAGS_RAW_threading) {
277 static const char* const exts[] = {
278 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
279 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
280 };
281 const char* actualExt = strrchr(path.c_str(), '.');
282 if (actualExt) {
283 actualExt++;
284 for (auto* ext : exts) {
285 if (0 == strcmp(ext, actualExt)) {
286 return true;
287 }
288 }
289 }
290 }
291 return false;
292}
293
scroggoc5560be2016-02-03 09:42:42 -0800294CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
295 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700296 : fPath(path)
297 , fMode(mode)
298 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800299 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700300 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800301 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700302{}
mtklein748ca3b2015-01-15 10:56:12 -0800303
mtklein99cab4e2015-07-31 06:43:04 -0700304bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800305 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800306 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700307}
scroggo9b77ddd2015-03-19 06:03:39 -0700308
msarett34e0ec42016-04-22 16:27:24 -0700309// Allows us to test decodes to non-native 8888.
msarettb1be46b2016-05-17 08:52:11 -0700310static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700311 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
312 return;
313 }
314
315 for (int y = 0; y < bitmap.height(); y++) {
316 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
317 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
318 }
319}
320
msarett9e9444c2016-02-03 12:39:10 -0800321// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
322// This allows us to still test unpremultiplied decodes.
msarettb1be46b2016-05-17 08:52:11 -0700323static void premultiply_if_necessary(SkBitmap& bitmap) {
msarett9e9444c2016-02-03 12:39:10 -0800324 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
325 return;
326 }
327
328 switch (bitmap.colorType()) {
Leon Scroggins IIIeceee042017-05-23 16:58:09 -0400329 case kRGBA_F16_SkColorType:
330 for (int y = 0; y < bitmap.height(); y++) {
331 void* row = bitmap.getAddr(0, y);
332 SkRasterPipeline p;
333 p.append(SkRasterPipeline::load_f16, &row);
334 p.append(SkRasterPipeline::premul);
335 p.append(SkRasterPipeline::store_f16, &row);
336 p.run(0, bitmap.width());
337 }
338 break;
msarett9e9444c2016-02-03 12:39:10 -0800339 case kN32_SkColorType:
340 for (int y = 0; y < bitmap.height(); y++) {
341 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
342 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
343 }
344 break;
345 case kIndex_8_SkColorType: {
346 SkColorTable* colorTable = bitmap.getColorTable();
347 SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
348 SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count());
349 break;
350 }
351 default:
352 // No need to premultiply kGray or k565 outputs.
353 break;
354 }
msarette1daa482016-02-03 15:31:18 -0800355
356 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
357 // bitmap as kPremul.
358 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800359}
360
msarettb1be46b2016-05-17 08:52:11 -0700361static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
scroggoba584892016-05-20 13:56:13 -0700362 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
msarett3d9d7a72015-10-21 10:27:10 -0700363 switch (dstColorType) {
364 case CodecSrc::kIndex8_Always_DstColorType:
365 if (kRGB_565_SkColorType == canvasColorType) {
366 return false;
367 }
scroggoc5560be2016-02-03 09:42:42 -0800368 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700369 break;
370 case CodecSrc::kGrayscale_Always_DstColorType:
scroggo1a361922016-05-20 14:27:16 -0700371 if (kRGB_565_SkColorType == canvasColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700372 return false;
373 }
scroggoc5560be2016-02-03 09:42:42 -0800374 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700375 break;
msarett34e0ec42016-04-22 16:27:24 -0700376 case CodecSrc::kNonNative8888_Always_DstColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400377 if (kRGB_565_SkColorType == canvasColorType
378 || kRGBA_F16_SkColorType == canvasColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700379 return false;
380 }
381#ifdef SK_PMCOLOR_IS_RGBA
382 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
383#else
384 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
385#endif
386 break;
msarett3d9d7a72015-10-21 10:27:10 -0700387 default:
msarett55f7bdd2016-02-16 13:24:54 -0800388 if (kRGB_565_SkColorType == canvasColorType &&
389 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
390 return false;
391 }
Matt Sarett09a1c082017-02-01 15:34:22 -0800392
393 if (kRGBA_F16_SkColorType == canvasColorType) {
394 sk_sp<SkColorSpace> linearSpace =
395 as_CSB(decodeInfo->colorSpace())->makeLinearGamma();
396 *decodeInfo = decodeInfo->makeColorSpace(std::move(linearSpace));
397 }
398
scroggoc5560be2016-02-03 09:42:42 -0800399 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700400 break;
401 }
402
scroggoba584892016-05-20 13:56:13 -0700403 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700404 return true;
405}
406
msarettb1be46b2016-05-17 08:52:11 -0700407static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
408 SkPMColor* colorPtr, int colorCount, CodecSrc::DstColorType dstColorType,
409 SkScalar left = 0, SkScalar top = 0) {
Hal Canary2db83612016-11-04 13:02:54 -0400410 sk_sp<SkColorTable> colorTable(new SkColorTable(colorPtr, colorCount));
msarettb1be46b2016-05-17 08:52:11 -0700411 SkBitmap bitmap;
412 bitmap.installPixels(info, pixels, rowBytes, colorTable.get(), nullptr, nullptr);
413 premultiply_if_necessary(bitmap);
414 swap_rb_if_necessary(bitmap, dstColorType);
415 canvas->drawBitmap(bitmap, left, top);
416}
417
mtkleine0effd62015-07-29 06:37:28 -0700418Error CodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700419 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800420 if (!encoded) {
421 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
422 }
msarettb714fb02016-01-22 14:46:42 -0800423
Ben Wagner145dbcd2016-11-03 14:40:50 -0400424 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700425 if (nullptr == codec.get()) {
426 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700427 }
428
scroggoba584892016-05-20 13:56:13 -0700429 SkImageInfo decodeInfo = codec->getInfo();
430 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
431 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800432 return Error::Nonfatal("Skipping uninteresting test.");
msarett438b2ad2015-04-09 12:43:10 -0700433 }
434
msarett0a242972015-06-11 14:27:27 -0700435 // Try to scale the image if it is desired
436 SkISize size = codec->getScaledDimensions(fScale);
437 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
438 return Error::Nonfatal("Test without scaling is uninteresting.");
439 }
msarettb32758a2015-08-18 13:22:46 -0700440
441 // Visually inspecting very small output images is not necessary. We will
442 // cover these cases in unit testing.
443 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
444 return Error::Nonfatal("Scaling very small images is uninteresting.");
445 }
msarett0a242972015-06-11 14:27:27 -0700446 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
447
msarettb1be46b2016-05-17 08:52:11 -0700448 const int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
449 const size_t rowBytes = size.width() * bpp;
scroggo19b91532016-10-24 09:03:26 -0700450 const size_t safeSize = decodeInfo.getSafeSize(rowBytes);
451 SkAutoMalloc pixels(safeSize);
msarettb1be46b2016-05-17 08:52:11 -0700452 SkPMColor colorPtr[256];
453 int colorCount = 256;
msarett438b2ad2015-04-09 12:43:10 -0700454
msarettbb25b532016-01-13 09:31:39 -0800455 SkCodec::Options options;
Matt Saretta40d9c82017-05-19 15:21:05 -0400456 options.fPremulBehavior = canvas->imageInfo().colorSpace() ?
457 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
msarettbb25b532016-01-13 09:31:39 -0800458 if (kCodecZeroInit_Mode == fMode) {
msarettb1be46b2016-05-17 08:52:11 -0700459 memset(pixels.get(), 0, size.height() * rowBytes);
msarettbb25b532016-01-13 09:31:39 -0800460 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
461 }
msarett34e0ec42016-04-22 16:27:24 -0700462
463 SkImageInfo bitmapInfo = decodeInfo;
464 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
465 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
466 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
467 }
scroggo9b77ddd2015-03-19 06:03:39 -0700468
scroggo9c59ebc2015-03-25 13:48:49 -0700469 switch (fMode) {
scroggo19b91532016-10-24 09:03:26 -0700470 case kAnimated_Mode: {
471 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
472 if (frameInfos.size() <= 1) {
473 return SkStringPrintf("%s is not an animated image.", fPath.c_str());
474 }
475
scroggo90e971a2016-10-25 06:11:01 -0700476 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
477 // into. "factor" is the number of frames to draw on one row. There will be
478 // up to "factor" rows as well.
479 const float root = sqrt((float) frameInfos.size());
480 const int factor = sk_float_ceil2int(root);
481
scroggo19b91532016-10-24 09:03:26 -0700482 // Used to cache a frame that future frames will depend on.
483 SkAutoMalloc priorFramePixels;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400484 int cachedFrame = SkCodec::kNone;
485 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
scroggo19b91532016-10-24 09:03:26 -0700486 options.fFrameIndex = i;
487 // Check for a prior frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400488 const int reqFrame = frameInfos[i].fRequiredFrame;
scroggo19b91532016-10-24 09:03:26 -0700489 if (reqFrame != SkCodec::kNone && reqFrame == cachedFrame
490 && priorFramePixels.get()) {
491 // Copy into pixels
492 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
493 options.fHasPriorFrame = true;
494 } else {
495 options.fHasPriorFrame = false;
496 }
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500497 SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(),
498 rowBytes, &options,
499 colorPtr, &colorCount);
500 if (SkCodec::kInvalidInput == result && i > 0) {
501 // Some of our test images have truncated later frames. Treat that
502 // the same as incomplete.
503 result = SkCodec::kIncompleteInput;
504 }
scroggo19b91532016-10-24 09:03:26 -0700505 switch (result) {
506 case SkCodec::kSuccess:
scroggo90e971a2016-10-25 06:11:01 -0700507 case SkCodec::kIncompleteInput: {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400508 // If the next frame depends on this one, store it in priorFrame.
509 // It is possible that we may discard a frame that future frames depend on,
510 // but the codec will simply redecode the discarded frame.
511 // Do this before calling draw_to_canvas, which premultiplies in place. If
512 // we're decoding to unpremul, we want to pass the unmodified frame to the
513 // codec for decoding the next frame.
514 if (static_cast<size_t>(i+1) < frameInfos.size()
515 && frameInfos[i+1].fRequiredFrame == i) {
516 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
517 cachedFrame = i;
518 }
519
scroggo90e971a2016-10-25 06:11:01 -0700520 SkAutoCanvasRestore acr(canvas, true);
521 const int xTranslate = (i % factor) * decodeInfo.width();
522 const int yTranslate = (i / factor) * decodeInfo.height();
523 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
scroggo19b91532016-10-24 09:03:26 -0700524 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes,
525 colorPtr, colorCount, fDstColorType);
526 if (result == SkCodec::kIncompleteInput) {
527 return "";
528 }
529 break;
scroggo90e971a2016-10-25 06:11:01 -0700530 }
scroggo53f63b62016-10-27 08:29:13 -0700531 case SkCodec::kInvalidConversion:
532 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType
533 || decodeInfo.colorType() == kIndex_8_SkColorType)) {
534 return Error::Nonfatal(SkStringPrintf(
535 "Cannot decode frame %i to 565/Index8 (%s).", i, fPath.c_str()));
536 }
537 // Fall through.
scroggo19b91532016-10-24 09:03:26 -0700538 default:
539 return SkStringPrintf("Couldn't getPixels for frame %i in %s.",
540 i, fPath.c_str());
541 }
scroggo19b91532016-10-24 09:03:26 -0700542 }
543 break;
544 }
msarettbb25b532016-01-13 09:31:39 -0800545 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700546 case kCodec_Mode: {
msarettb1be46b2016-05-17 08:52:11 -0700547 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options,
548 colorPtr, &colorCount)) {
scroggoeb602a52015-07-09 08:16:03 -0700549 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700550 // We consider incomplete to be valid, since we should still decode what is
551 // available.
scroggoeb602a52015-07-09 08:16:03 -0700552 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700553 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700554 default:
555 // Everything else is considered a failure.
556 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
557 }
msarettb1be46b2016-05-17 08:52:11 -0700558
559 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount,
560 fDstColorType);
scroggo9c59ebc2015-03-25 13:48:49 -0700561 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700562 }
scroggo9c59ebc2015-03-25 13:48:49 -0700563 case kScanline_Mode: {
scroggod8d68552016-06-06 11:26:17 -0700564 void* dst = pixels.get();
565 uint32_t height = decodeInfo.height();
scroggo19b91532016-10-24 09:03:26 -0700566 const bool useIncremental = [this]() {
567 auto exts = { "png", "PNG", "gif", "GIF" };
568 for (auto ext : exts) {
569 if (fPath.endsWith(ext)) {
570 return true;
571 }
572 }
573 return false;
574 }();
575 // ico may use the old scanline method or the new one, depending on whether it
576 // internally holds a bmp or a png.
scroggo8e6c7ad2016-09-16 08:20:38 -0700577 const bool ico = fPath.endsWith("ico");
scroggo19b91532016-10-24 09:03:26 -0700578 bool useOldScanlineMethod = !useIncremental && !ico;
579 if (useIncremental || ico) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700580 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
Matt Saretta40d9c82017-05-19 15:21:05 -0400581 rowBytes, &options, colorPtr, &colorCount)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700582 int rowsDecoded;
583 if (SkCodec::kIncompleteInput == codec->incrementalDecode(&rowsDecoded)) {
584 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
585 SkCodec::kNo_ZeroInitialized, height,
586 rowsDecoded);
scroggo6fb23912016-06-02 14:16:43 -0700587 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700588 } else {
scroggo19b91532016-10-24 09:03:26 -0700589 if (useIncremental) {
590 // Error: These should support incremental decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700591 return "Could not start incremental decode";
592 }
593 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
594 // which should work via startScanlineDecode
595 useOldScanlineMethod = true;
596 }
597 }
598
599 if (useOldScanlineMethod) {
600 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
601 &colorCount)) {
602 return "Could not start scanline decoder";
603 }
604
605 switch (codec->getScanlineOrder()) {
606 case SkCodec::kTopDown_SkScanlineOrder:
607 case SkCodec::kBottomUp_SkScanlineOrder:
608 // We do not need to check the return value. On an incomplete
609 // image, memory will be filled with a default value.
610 codec->getScanlines(dst, height, rowBytes);
611 break;
msarett10522ff2015-09-07 08:54:01 -0700612 }
613 }
614
msarettb1be46b2016-05-17 08:52:11 -0700615 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCount, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700616 break;
617 }
msarett0a242972015-06-11 14:27:27 -0700618 case kStripe_Mode: {
619 const int height = decodeInfo.height();
620 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
621 // does not align with image blocks.
622 const int stripeHeight = 37;
623 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
msarettb1be46b2016-05-17 08:52:11 -0700624 void* dst = pixels.get();
msarett0a242972015-06-11 14:27:27 -0700625
626 // Decode odd stripes
Matt Saretta40d9c82017-05-19 15:21:05 -0400627 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options, colorPtr,
msarettb1be46b2016-05-17 08:52:11 -0700628 &colorCount)) {
msarettb65e6042016-02-23 05:37:25 -0800629 return "Could not start scanline decoder";
630 }
631
632 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
633 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
634 // to run this test for image types that do not have this scanline ordering.
scroggo12e2f502016-05-16 09:04:18 -0700635 // We only run this on Jpeg, which is always kTopDown.
636 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
msarette6dd0042015-10-09 11:07:34 -0700637
msarett0a242972015-06-11 14:27:27 -0700638 for (int i = 0; i < numStripes; i += 2) {
639 // Skip a stripe
640 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700641 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700642
643 // Read a stripe
644 const int startY = (i + 1) * stripeHeight;
645 const int linesToRead = SkTMin(stripeHeight, height - startY);
646 if (linesToRead > 0) {
msarettb1be46b2016-05-17 08:52:11 -0700647 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
648 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700649 }
650 }
651
652 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700653 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
msarettb1be46b2016-05-17 08:52:11 -0700654 colorPtr, &colorCount);
scroggo1c005e42015-08-04 09:24:45 -0700655 if (SkCodec::kSuccess != startResult) {
656 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700657 }
658 for (int i = 0; i < numStripes; i += 2) {
659 // Read a stripe
660 const int startY = i * stripeHeight;
661 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarettb1be46b2016-05-17 08:52:11 -0700662 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
663 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700664
665 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700666 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
667 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700668 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700669 }
670 }
msarettb1be46b2016-05-17 08:52:11 -0700671
672 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCount, fDstColorType);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700673 break;
msarett0a242972015-06-11 14:27:27 -0700674 }
msarett91c22b22016-02-22 12:27:46 -0800675 case kCroppedScanline_Mode: {
676 const int width = decodeInfo.width();
677 const int height = decodeInfo.height();
678 // This value is chosen because, as we move across the image, it will sometimes
679 // align with the jpeg block sizes and it will sometimes not. This allows us
680 // to test interestingly different code paths in the implementation.
681 const int tileSize = 36;
msarett91c22b22016-02-22 12:27:46 -0800682 SkIRect subset;
683 for (int x = 0; x < width; x += tileSize) {
684 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
Matt Saretta40d9c82017-05-19 15:21:05 -0400685 options.fSubset = &subset;
686 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options,
msarettb1be46b2016-05-17 08:52:11 -0700687 colorPtr, &colorCount)) {
msarett91c22b22016-02-22 12:27:46 -0800688 return "Could not start scanline decoder.";
689 }
690
msarettb1be46b2016-05-17 08:52:11 -0700691 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
msarett91c22b22016-02-22 12:27:46 -0800692 }
693
msarettb1be46b2016-05-17 08:52:11 -0700694 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount,
695 fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800696 break;
697 }
scroggob636b452015-07-22 07:16:20 -0700698 case kSubset_Mode: {
699 // Arbitrarily choose a divisor.
700 int divisor = 2;
701 // Total width/height of the image.
702 const int W = codec->getInfo().width();
703 const int H = codec->getInfo().height();
704 if (divisor > W || divisor > H) {
705 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
706 "for %s with dimensions (%d x %d)", divisor,
707 fPath.c_str(), W, H));
708 }
709 // subset dimensions
710 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
711 const int w = SkAlign2(W / divisor);
712 const int h = SkAlign2(H / divisor);
713 SkIRect subset;
Matt Saretta40d9c82017-05-19 15:21:05 -0400714 options.fSubset = &subset;
scroggob636b452015-07-22 07:16:20 -0700715 SkBitmap subsetBm;
716 // We will reuse pixel memory from bitmap.
msarettb1be46b2016-05-17 08:52:11 -0700717 void* dst = pixels.get();
scroggob636b452015-07-22 07:16:20 -0700718 // Keep track of left and top (for drawing subsetBm into canvas). We could use
719 // fScale * x and fScale * y, but we want integers such that the next subset will start
720 // where the last one ended. So we'll add decodeInfo.width() and height().
721 int left = 0;
722 for (int x = 0; x < W; x += w) {
723 int top = 0;
724 for (int y = 0; y < H; y+= h) {
725 // Do not make the subset go off the edge of the image.
726 const int preScaleW = SkTMin(w, W - x);
727 const int preScaleH = SkTMin(h, H - y);
728 subset.setXYWH(x, y, preScaleW, preScaleH);
729 // And scale
730 // FIXME: Should we have a version of getScaledDimensions that takes a subset
731 // into account?
msarettc7eb4902016-04-25 07:04:58 -0700732 const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale));
733 const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale));
734 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
735 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
msarettb1be46b2016-05-17 08:52:11 -0700736 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
737 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
Matt Saretta40d9c82017-05-19 15:21:05 -0400738 &options, colorPtr, &colorCount);
scroggob636b452015-07-22 07:16:20 -0700739 switch (result) {
740 case SkCodec::kSuccess:
741 case SkCodec::kIncompleteInput:
742 break;
scroggob636b452015-07-22 07:16:20 -0700743 default:
744 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
745 "from %s with dimensions (%d x %d)\t error %d",
746 x, y, decodeInfo.width(), decodeInfo.height(),
747 fPath.c_str(), W, H, result);
748 }
msarettb1be46b2016-05-17 08:52:11 -0700749 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, colorPtr,
750 colorCount, fDstColorType, SkIntToScalar(left),
751 SkIntToScalar(top));
752
scroggob636b452015-07-22 07:16:20 -0700753 // translate by the scaled height.
754 top += decodeInfo.height();
755 }
756 // translate by the scaled width.
757 left += decodeInfo.width();
758 }
759 return "";
760 }
msarettb714fb02016-01-22 14:46:42 -0800761 default:
762 SkASSERT(false);
763 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700764 }
scroggo9c59ebc2015-03-25 13:48:49 -0700765 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700766}
767
768SkISize CodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700769 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400770 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700771 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400772 return {0, 0};
scroggo7fac5af2015-09-30 11:33:12 -0700773 }
scroggo19b91532016-10-24 09:03:26 -0700774
775 auto imageSize = codec->getScaledDimensions(fScale);
776 if (fMode == kAnimated_Mode) {
scroggo90e971a2016-10-25 06:11:01 -0700777 // We'll draw one of each frame, so make it big enough to hold them all
778 // in a grid. The grid will be roughly square, with "factor" frames per
779 // row and up to "factor" rows.
scroggo19b91532016-10-24 09:03:26 -0700780 const size_t count = codec->getFrameInfo().size();
scroggo90e971a2016-10-25 06:11:01 -0700781 const float root = sqrt((float) count);
782 const int factor = sk_float_ceil2int(root);
783 imageSize.fWidth = imageSize.fWidth * factor;
784 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
scroggo19b91532016-10-24 09:03:26 -0700785 }
786 return imageSize;
scroggo9b77ddd2015-03-19 06:03:39 -0700787}
788
789Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700790 if (1.0f == fScale) {
scroggo6e8c68e2016-10-24 13:48:49 -0700791 Name name = SkOSPath::Basename(fPath.c_str());
792 if (fMode == kAnimated_Mode) {
793 name.append("_animated");
794 }
795 return name;
msarett0a242972015-06-11 14:27:27 -0700796 }
scroggo6e8c68e2016-10-24 13:48:49 -0700797 SkASSERT(fMode != kAnimated_Mode);
msaretta5783ae2015-09-08 15:35:32 -0700798 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700799}
800
801/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
802
scroggof8dc9df2016-05-16 09:04:13 -0700803AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800804 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700805 : fPath(path)
msarett3d9d7a72015-10-21 10:27:10 -0700806 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800807 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700808 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800809 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700810{}
811
812bool AndroidCodecSrc::veto(SinkFlags flags) const {
813 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700814 return flags.type != SinkFlags::kRaster
815 || flags.approach != SinkFlags::kDirect;
816}
817
818Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
Matt Saretta40d9c82017-05-19 15:21:05 -0400819 if (canvas->imageInfo().colorSpace() &&
820 kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) {
821 // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only
822 // run these tests on legacy canvases.
823 // We allow an exception for F16, since Android uses F16.
824 return Error::Nonfatal("Skip testing to color correct canvas.");
825 }
826
bungeman38d909e2016-08-02 14:40:46 -0700827 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700828 if (!encoded) {
829 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
830 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400831 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700832 if (nullptr == codec.get()) {
833 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
834 }
835
scroggoba584892016-05-20 13:56:13 -0700836 SkImageInfo decodeInfo = codec->getInfo();
837 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
838 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800839 return Error::Nonfatal("Skipping uninteresting test.");
msarett3d9d7a72015-10-21 10:27:10 -0700840 }
841
842 // Scale the image if it is desired.
843 SkISize size = codec->getSampledDimensions(fSampleSize);
844
845 // Visually inspecting very small output images is not necessary. We will
846 // cover these cases in unit testing.
847 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
848 return Error::Nonfatal("Scaling very small images is uninteresting.");
849 }
850 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
851
msarettb1be46b2016-05-17 08:52:11 -0700852 int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
853 size_t rowBytes = size.width() * bpp;
854 SkAutoMalloc pixels(size.height() * rowBytes);
855 SkPMColor colorPtr[256];
856 int colorCount = 256;
msarett3d9d7a72015-10-21 10:27:10 -0700857
858 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700859 SkImageInfo bitmapInfo = decodeInfo;
860 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
861 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
862 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
863 }
msarett3d9d7a72015-10-21 10:27:10 -0700864
865 // Create options for the codec.
866 SkAndroidCodec::AndroidOptions options;
867 options.fColorPtr = colorPtr;
msarettb1be46b2016-05-17 08:52:11 -0700868 options.fColorCount = &colorCount;
msarett3d9d7a72015-10-21 10:27:10 -0700869 options.fSampleSize = fSampleSize;
870
msarettb1be46b2016-05-17 08:52:11 -0700871 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggof8dc9df2016-05-16 09:04:13 -0700872 case SkCodec::kSuccess:
873 case SkCodec::kIncompleteInput:
874 break;
msarett3d9d7a72015-10-21 10:27:10 -0700875 default:
scroggof8dc9df2016-05-16 09:04:13 -0700876 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700877 }
msarettb1be46b2016-05-17 08:52:11 -0700878 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount, fDstColorType);
scroggof8dc9df2016-05-16 09:04:13 -0700879 return "";
msarett3d9d7a72015-10-21 10:27:10 -0700880}
881
882SkISize AndroidCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700883 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400884 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700885 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400886 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700887 }
888 return codec->getSampledDimensions(fSampleSize);
889}
890
891Name AndroidCodecSrc::name() const {
892 // We will replicate the names used by CodecSrc so that images can
893 // be compared in Gold.
894 if (1 == fSampleSize) {
895 return SkOSPath::Basename(fPath.c_str());
896 }
msarett4b0778e2015-11-13 09:59:11 -0800897 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700898}
899
900/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
901
msarett18976312016-03-09 14:20:58 -0800902ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
903 : fPath(path)
904 , fMode(mode)
905 , fDstAlphaType(alphaType)
906 , fIsGpu(isGpu)
907 , fRunSerially(serial_from_path_name(path))
908{}
909
910bool ImageGenSrc::veto(SinkFlags flags) const {
911 if (fIsGpu) {
Brian Salomonbd7c5512017-03-07 09:08:36 -0500912 // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs.
913 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect ||
914 flags.multisampled == SinkFlags::kMultisampled;
msarett18976312016-03-09 14:20:58 -0800915 }
916
917 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
918}
919
920Error ImageGenSrc::draw(SkCanvas* canvas) const {
921 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
922 return Error::Nonfatal("Uninteresting to test image generator to 565.");
923 }
924
bungeman38d909e2016-08-02 14:40:46 -0700925 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett18976312016-03-09 14:20:58 -0800926 if (!encoded) {
927 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
928 }
929
msarettfc0b6d12016-03-17 13:50:17 -0700930#if defined(SK_BUILD_FOR_WIN)
931 // Initialize COM in order to test with WIC.
932 SkAutoCoInitialize com;
933 if (!com.succeeded()) {
934 return "Could not initialize COM.";
935 }
936#endif
937
Ben Wagner145dbcd2016-11-03 14:40:50 -0400938 std::unique_ptr<SkImageGenerator> gen(nullptr);
msarett18976312016-03-09 14:20:58 -0800939 switch (fMode) {
940 case kCodec_Mode:
Mike Reed185130c2017-02-15 15:14:16 -0500941 gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded);
msarett18976312016-03-09 14:20:58 -0800942 if (!gen) {
943 return "Could not create codec image generator.";
944 }
945 break;
msarettfc0b6d12016-03-17 13:50:17 -0700946 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800947#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
bungeman38d909e2016-08-02 14:40:46 -0700948 gen.reset(SkImageGeneratorCG::NewFromEncodedCG(encoded.get()));
msarettfc0b6d12016-03-17 13:50:17 -0700949#elif defined(SK_BUILD_FOR_WIN)
bungeman38d909e2016-08-02 14:40:46 -0700950 gen.reset(SkImageGeneratorWIC::NewFromEncodedWIC(encoded.get()));
msarettfc0b6d12016-03-17 13:50:17 -0700951#endif
952
msarett18976312016-03-09 14:20:58 -0800953 if (!gen) {
msarettfc0b6d12016-03-17 13:50:17 -0700954 return "Could not create platform image generator.";
msarett18976312016-03-09 14:20:58 -0800955 }
956 break;
msarettfc0b6d12016-03-17 13:50:17 -0700957 }
msarett18976312016-03-09 14:20:58 -0800958 default:
959 SkASSERT(false);
960 return "Invalid image generator mode";
961 }
962
963 // Test deferred decoding path on GPU
964 if (fIsGpu) {
Mike Reed185130c2017-02-15 15:14:16 -0500965 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr));
msarett18976312016-03-09 14:20:58 -0800966 if (!image) {
967 return "Could not create image from codec image generator.";
968 }
969 canvas->drawImage(image, 0, 0);
970 return "";
971 }
mtklein343a63d2016-03-22 11:46:53 -0700972
msarett18976312016-03-09 14:20:58 -0800973 // Test various color and alpha types on CPU
974 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700975
Matt Saretta40d9c82017-05-19 15:21:05 -0400976 SkImageGenerator::Options options;
977 options.fBehavior = canvas->imageInfo().colorSpace() ?
978 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
979
msarettb1be46b2016-05-17 08:52:11 -0700980 int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
981 size_t rowBytes = decodeInfo.width() * bpp;
982 SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
Matt Saretta40d9c82017-05-19 15:21:05 -0400983 if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
Matt Sarett05cb4c32017-03-02 12:07:46 -0500984 SkString err =
985 SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str());
986
987#if defined(SK_BUILD_FOR_WIN)
988 if (kPlatform_Mode == fMode) {
989 // Do not issue a fatal error for WIC flakiness.
990 return Error::Nonfatal(err);
991 }
992#endif
993
994 return err;
msarett18976312016-03-09 14:20:58 -0800995 }
996
Matt Sarettebb1b5c2017-05-12 11:41:27 -0400997 SkPMColor colorPtr[256];
998 int colorCount = 256;
msarettb1be46b2016-05-17 08:52:11 -0700999 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes, colorPtr, colorCount,
1000 CodecSrc::kGetFromCanvas_DstColorType);
msarett18976312016-03-09 14:20:58 -08001001 return "";
1002}
1003
1004SkISize ImageGenSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001005 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -04001006 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett18976312016-03-09 14:20:58 -08001007 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001008 return {0, 0};
msarett18976312016-03-09 14:20:58 -08001009 }
1010 return codec->getInfo().dimensions();
1011}
1012
1013Name ImageGenSrc::name() const {
1014 return SkOSPath::Basename(fPath.c_str());
1015}
1016
1017/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1018
msarett9ce3a542016-07-15 13:54:38 -07001019ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, SkColorType colorType)
msarett69deca82016-04-29 09:38:40 -07001020 : fPath(path)
1021 , fMode(mode)
msarett9ce3a542016-07-15 13:54:38 -07001022 , fColorType(colorType)
msarett69deca82016-04-29 09:38:40 -07001023{}
1024
1025bool ColorCodecSrc::veto(SinkFlags flags) const {
1026 // Test to direct raster backends (8888 and 565).
1027 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
1028}
1029
1030Error ColorCodecSrc::draw(SkCanvas* canvas) const {
1031 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
1032 return Error::Nonfatal("No need to test color correction to 565 backend.");
1033 }
1034
msarettd1ec89b2016-08-03 12:59:27 -07001035 bool runInLegacyMode = kBaseline_Mode == fMode;
msarettd1ec89b2016-08-03 12:59:27 -07001036 if (runInLegacyMode && canvas->imageInfo().colorSpace()) {
1037 return Error::Nonfatal("Skipping tests that are only interesting in legacy mode.");
1038 } else if (!runInLegacyMode && !canvas->imageInfo().colorSpace()) {
1039 return Error::Nonfatal("Skipping tests that are only interesting in srgb mode.");
msarett9ce3a542016-07-15 13:54:38 -07001040 }
1041
bungeman38d909e2016-08-02 14:40:46 -07001042 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett69deca82016-04-29 09:38:40 -07001043 if (!encoded) {
1044 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1045 }
1046
Ben Wagner145dbcd2016-11-03 14:40:50 -04001047 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001048 if (nullptr == codec.get()) {
1049 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
1050 }
1051
msarett9876ac52016-06-01 14:47:18 -07001052 // Load the dst ICC profile. This particular dst is fairly similar to Adobe RGB.
1053 sk_sp<SkData> dstData = SkData::MakeFromFileName(
msaretta714bc32016-07-29 08:58:33 -07001054 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
msarett9876ac52016-06-01 14:47:18 -07001055 if (!dstData) {
1056 return "Cannot read monitor profile. Is the resource path set correctly?";
1057 }
1058
msarett50ce1f22016-07-29 06:23:33 -07001059 sk_sp<SkColorSpace> dstSpace = nullptr;
1060 if (kDst_sRGB_Mode == fMode) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -05001061 dstSpace = SkColorSpace::MakeSRGB();
msarett50ce1f22016-07-29 06:23:33 -07001062 } else if (kDst_HPZR30w_Mode == fMode) {
Brian Osman526972e2016-10-24 09:24:02 -04001063 dstSpace = SkColorSpace::MakeICC(dstData->data(), dstData->size());
msarett50ce1f22016-07-29 06:23:33 -07001064 }
1065
1066 SkImageInfo decodeInfo = codec->getInfo().makeColorType(fColorType).makeColorSpace(dstSpace);
msarettd1ec89b2016-08-03 12:59:27 -07001067 if (kUnpremul_SkAlphaType == decodeInfo.alphaType()) {
1068 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
1069 }
msarett2ecc35f2016-09-08 11:55:16 -07001070 if (kRGBA_F16_SkColorType == fColorType) {
raftias94888332016-10-18 10:02:51 -07001071 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(decodeInfo.colorSpace())->type());
1072 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(decodeInfo.colorSpace());
1073 decodeInfo = decodeInfo.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001074 }
msarettd1ec89b2016-08-03 12:59:27 -07001075
msarett50ce1f22016-07-29 06:23:33 -07001076 SkImageInfo bitmapInfo = decodeInfo;
1077 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
1078 kBGRA_8888_SkColorType == decodeInfo.colorType())
1079 {
1080 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
1081 }
1082
1083 SkBitmap bitmap;
1084 if (!bitmap.tryAllocPixels(bitmapInfo)) {
1085 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
1086 bitmapInfo.width(), bitmapInfo.height());
1087 }
1088
1089 size_t rowBytes = bitmap.rowBytes();
1090 SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), rowBytes);
msarettf7eb6fc2016-09-13 09:04:11 -07001091 if (SkCodec::kSuccess != r && SkCodec::kIncompleteInput != r) {
raftias54761282016-12-01 13:44:07 -05001092 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
msarett50ce1f22016-07-29 06:23:33 -07001093 }
1094
msarett69deca82016-04-29 09:38:40 -07001095 switch (fMode) {
1096 case kBaseline_Mode:
msarett39979d82016-07-28 17:11:18 -07001097 case kDst_sRGB_Mode:
msarett50ce1f22016-07-29 06:23:33 -07001098 case kDst_HPZR30w_Mode:
msarett39979d82016-07-28 17:11:18 -07001099 canvas->drawBitmap(bitmap, 0, 0);
1100 break;
msarett69deca82016-04-29 09:38:40 -07001101 default:
1102 SkASSERT(false);
1103 return "Invalid fMode";
1104 }
1105 return "";
1106}
1107
1108SkISize ColorCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001109 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -04001110 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001111 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001112 return {0, 0};
msarett69deca82016-04-29 09:38:40 -07001113 }
Hal Canaryfafe1352017-04-11 12:12:02 -04001114 return {codec->getInfo().width(), codec->getInfo().height()};
msarett69deca82016-04-29 09:38:40 -07001115}
1116
1117Name ColorCodecSrc::name() const {
1118 return SkOSPath::Basename(fPath.c_str());
1119}
1120
1121/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1122
mtkleinf4ba3212015-01-28 15:32:24 -08001123static const SkRect kSKPViewport = {0,0, 1000,1000};
1124
mtklein8d17a132015-01-30 11:42:31 -08001125SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -08001126
1127Error SKPSrc::draw(SkCanvas* canvas) const {
bungemanf93d7112016-09-16 06:24:20 -07001128 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
mtklein75d98fd2015-01-18 07:05:01 -08001129 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -08001130 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1131 }
bungemanf93d7112016-09-16 06:24:20 -07001132 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
mtklein75d98fd2015-01-18 07:05:01 -08001133 if (!pic) {
1134 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
1135 }
bungemanf93d7112016-09-16 06:24:20 -07001136 stream = nullptr; // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -07001137
mtkleinf4ba3212015-01-28 15:32:24 -08001138 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -08001139 canvas->drawPicture(pic);
1140 return "";
1141}
1142
1143SkISize SKPSrc::size() const {
bungemanf93d7112016-09-16 06:24:20 -07001144 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
mtkleinffa901a2015-03-16 10:38:07 -07001145 if (!stream) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001146 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001147 }
1148 SkPictInfo info;
bungemanf93d7112016-09-16 06:24:20 -07001149 if (!SkPicture::InternalOnly_StreamIsSKP(stream.get(), &info)) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001150 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001151 }
1152 SkRect viewport = kSKPViewport;
1153 if (!viewport.intersect(info.fCullRect)) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001154 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001155 }
1156 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001157}
1158
1159Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1160
1161/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fmalitaa2b9fdf2016-08-03 19:53:36 -07001162#if defined(SK_XML)
fmalitabdf3e5c2016-09-17 07:26:26 -07001163// Used when the image doesn't have an intrinsic size.
Hal Canaryfafe1352017-04-11 12:12:02 -04001164static const SkSize kDefaultSVGSize = {1000, 1000};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001165
fmalitabdf3e5c2016-09-17 07:26:26 -07001166// Used to force-scale tiny fixed-size images.
Hal Canaryfafe1352017-04-11 12:12:02 -04001167static const SkSize kMinimumSVGSize = {128, 128};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001168
fmalitaacd2f5c2016-11-08 07:13:45 -08001169SVGSrc::SVGSrc(Path path)
1170 : fName(SkOSPath::Basename(path.c_str()))
1171 , fScale(1) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001172
fmalitaacd2f5c2016-11-08 07:13:45 -08001173 SkFILEStream stream(path.c_str());
1174 if (!stream.isValid()) {
1175 return;
1176 }
1177 fDom = SkSVGDOM::MakeFromStream(stream);
1178 if (!fDom) {
1179 return;
1180 }
1181
1182 const SkSize& sz = fDom->containerSize();
1183 if (sz.isEmpty()) {
1184 // no intrinsic size
1185 fDom->setContainerSize(kDefaultSVGSize);
1186 } else {
1187 fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(),
1188 kMinimumSVGSize.height() / sz.height()));
1189 }
1190}
1191
1192Error SVGSrc::draw(SkCanvas* canvas) const {
fmalitabdf3e5c2016-09-17 07:26:26 -07001193 if (!fDom) {
fmalitaacd2f5c2016-11-08 07:13:45 -08001194 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
fmalitaa2b9fdf2016-08-03 19:53:36 -07001195 }
1196
fmalitaacd2f5c2016-11-08 07:13:45 -08001197 SkAutoCanvasRestore acr(canvas, true);
1198 canvas->scale(fScale, fScale);
1199 fDom->render(canvas);
1200
fmalitaa2b9fdf2016-08-03 19:53:36 -07001201 return "";
1202}
1203
1204SkISize SVGSrc::size() const {
fmalitaacd2f5c2016-11-08 07:13:45 -08001205 if (!fDom) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001206 return {0, 0};
fmalitabdf3e5c2016-09-17 07:26:26 -07001207 }
1208
Hal Canaryfafe1352017-04-11 12:12:02 -04001209 return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale}
1210 .toRound();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001211}
1212
fmalitaacd2f5c2016-11-08 07:13:45 -08001213Name SVGSrc::name() const { return fName; }
fmalitaa2b9fdf2016-08-03 19:53:36 -07001214
fmalita179d8852016-08-16 14:23:29 -07001215bool SVGSrc::veto(SinkFlags flags) const {
1216 // No need to test to non-(raster||gpu) or indirect backends.
1217 bool type_ok = flags.type == SinkFlags::kRaster
1218 || flags.type == SinkFlags::kGPU;
1219
1220 return !type_ok || flags.approach != SinkFlags::kDirect;
1221}
1222
fmalitaa2b9fdf2016-08-03 19:53:36 -07001223#endif // defined(SK_XML)
1224/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein748ca3b2015-01-15 10:56:12 -08001225
halcanary45420a92016-06-02 12:41:14 -07001226MSKPSrc::MSKPSrc(Path path) : fPath(path) {
bungemanf93d7112016-09-16 06:24:20 -07001227 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001228 int count = SkMultiPictureDocumentReadPageCount(stream.get());
1229 if (count > 0) {
1230 fPages.reset(count);
1231 (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count());
1232 }
halcanary45420a92016-06-02 12:41:14 -07001233}
1234
Hal Canary45cde312017-04-03 16:06:42 -04001235int MSKPSrc::pageCount() const { return fPages.count(); }
halcanary45420a92016-06-02 12:41:14 -07001236
1237SkISize MSKPSrc::size() const { return this->size(0); }
Hal Canary45cde312017-04-03 16:06:42 -04001238SkISize MSKPSrc::size(int i) const {
Hal Canaryfafe1352017-04-11 12:12:02 -04001239 return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0};
Hal Canary45cde312017-04-03 16:06:42 -04001240}
halcanary45420a92016-06-02 12:41:14 -07001241
1242Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); }
1243Error MSKPSrc::draw(int i, SkCanvas* canvas) const {
Hal Canary45cde312017-04-03 16:06:42 -04001244 if (this->pageCount() == 0) {
halcanary45420a92016-06-02 12:41:14 -07001245 return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
1246 }
Hal Canary45cde312017-04-03 16:06:42 -04001247 if (i >= fPages.count() || i < 0) {
halcanary45420a92016-06-02 12:41:14 -07001248 return SkStringPrintf("MultiPictureDocument page number out of range: %d", i);
1249 }
Hal Canary45cde312017-04-03 16:06:42 -04001250 SkPicture* page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001251 if (!page) {
Hal Canary45cde312017-04-03 16:06:42 -04001252 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1253 if (!stream) {
1254 return SkStringPrintf("Unable to open file: %s", fPath.c_str());
1255 }
1256 if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) {
1257 return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i,
1258 fPath.c_str());
1259 }
1260 page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001261 }
1262 canvas->drawPicture(page);
1263 return "";
1264}
1265
1266Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1267
1268/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1269
mtkleinad66f9b2015-02-13 15:11:10 -08001270Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
Mike Reed5df49342016-11-12 08:06:55 -06001271 return src.draw(SkMakeNullCanvas().get());
mtkleinad66f9b2015-02-13 15:11:10 -08001272}
1273
1274/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1275
mtkleinb9eb4ac2015-02-02 18:26:03 -08001276DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
1277
bsalomon85b4b532016-04-05 11:06:27 -07001278GPUSink::GPUSink(GrContextFactory::ContextType ct,
csmartdaltone812d492017-02-21 12:36:05 -07001279 GrContextFactory::ContextOverrides overrides,
mtklein82d28432015-01-15 12:46:02 -08001280 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -07001281 bool diText,
brianosmand93c1202016-03-10 07:49:08 -08001282 SkColorType colorType,
brianosmanb109b8c2016-06-16 13:03:24 -07001283 sk_sp<SkColorSpace> colorSpace,
mtklein82d28432015-01-15 12:46:02 -08001284 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -08001285 : fContextType(ct)
csmartdaltone812d492017-02-21 12:36:05 -07001286 , fContextOverrides(overrides)
mtklein748ca3b2015-01-15 10:56:12 -08001287 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -07001288 , fUseDIText(diText)
brianosmand93c1202016-03-10 07:49:08 -08001289 , fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001290 , fColorSpace(std::move(colorSpace))
mtklein82d28432015-01-15 12:46:02 -08001291 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -08001292
bsalomon648c6962015-10-23 09:06:59 -07001293DEFINE_bool(imm, false, "Run gpu configs in immediate mode.");
Brian Salomon09d994e2016-12-21 11:14:46 -05001294DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
bsalomon648c6962015-10-23 09:06:59 -07001295
mtkleinb9eb4ac2015-02-02 18:26:03 -08001296Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
kkinnunen5219fd92015-12-10 06:28:13 -08001297 GrContextOptions grOptions;
bsalomon489147c2015-12-14 12:13:09 -08001298 grOptions.fImmediateMode = FLAGS_imm;
kkinnunen64492c42015-12-08 01:24:40 -08001299
kkinnunen5219fd92015-12-10 06:28:13 -08001300 src.modifyGrContextOptions(&grOptions);
1301
1302 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -08001303 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001304 const SkImageInfo info =
brianosmand93c1202016-03-10 07:49:08 -08001305 SkImageInfo::Make(size.width(), size.height(), fColorType,
brianosmanb109b8c2016-06-16 13:03:24 -07001306 kPremul_SkAlphaType, fColorSpace);
msarett13a036b2016-02-08 09:10:47 -08001307#if SK_SUPPORT_GPU
csmartdaltone812d492017-02-21 12:36:05 -07001308 GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext();
bsalomon8b7451a2016-05-11 06:33:06 -07001309 const int maxDimension = context->caps()->maxTextureSize();
msarett13a036b2016-02-08 09:10:47 -08001310 if (maxDimension < SkTMax(size.width(), size.height())) {
1311 return Error::Nonfatal("Src too large to create a texture.\n");
1312 }
1313#endif
1314
reede8f30622016-03-23 18:59:25 -07001315 auto surface(
csmartdaltone812d492017-02-21 12:36:05 -07001316 NewGpuSurface(&factory, fContextType, fContextOverrides, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -08001317 if (!surface) {
1318 return "Could not create a surface.";
1319 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001320 if (FLAGS_preAbandonGpuContext) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001321 factory.abandonContexts();
1322 }
mtklein748ca3b2015-01-15 10:56:12 -08001323 SkCanvas* canvas = surface->getCanvas();
1324 Error err = src.draw(canvas);
1325 if (!err.isEmpty()) {
1326 return err;
1327 }
1328 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001329 if (FLAGS_gpuStats) {
1330 canvas->getGrContext()->dumpCacheStats(log);
1331 canvas->getGrContext()->dumpGpuStats(log);
1332 }
mtklein748ca3b2015-01-15 10:56:12 -08001333 dst->allocPixels(info);
Mike Reed12e946b2017-04-17 10:53:29 -04001334 canvas->readPixels(*dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001335 if (FLAGS_abandonGpuContext) {
1336 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001337 } else if (FLAGS_releaseAndAbandonGpuContext) {
1338 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001339 }
mtklein748ca3b2015-01-15 10:56:12 -08001340 return "";
1341}
1342
1343/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1344
halcanary47ef4d52015-03-03 09:13:09 -08001345static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001346 if (src.size().isEmpty()) {
1347 return "Source has empty dimensions";
1348 }
halcanary47ef4d52015-03-03 09:13:09 -08001349 SkASSERT(doc);
halcanary45420a92016-06-02 12:41:14 -07001350 int pageCount = src.pageCount();
1351 for (int i = 0; i < pageCount; ++i) {
1352 int width = src.size(i).width(), height = src.size(i).height();
halcanary7e798182015-04-14 14:06:18 -07001353 SkCanvas* canvas =
1354 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1355 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001356 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001357 }
halcanary45420a92016-06-02 12:41:14 -07001358 Error err = src.draw(i, canvas);
halcanary7e798182015-04-14 14:06:18 -07001359 if (!err.isEmpty()) {
1360 return err;
1361 }
1362 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001363 }
reedd14df7c2016-09-22 14:12:46 -07001364 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -08001365 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001366 return "";
1367}
1368
halcanary47ef4d52015-03-03 09:13:09 -08001369Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary4b656662016-04-27 07:45:18 -07001370 SkDocument::PDFMetadata metadata;
1371 metadata.fTitle = src.name();
1372 metadata.fSubject = "rendering correctness test";
1373 metadata.fCreator = "Skia/DM";
1374 sk_sp<SkDocument> doc = SkDocument::MakePDF(dst, SK_ScalarDefaultRasterDPI,
1375 metadata, nullptr, fPDFA);
halcanary47ef4d52015-03-03 09:13:09 -08001376 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001377 return "SkDocument::MakePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001378 }
1379 return draw_skdocument(src, doc.get(), dst);
1380}
1381
1382/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1383
1384XPSSink::XPSSink() {}
1385
Hal Canaryabc88d22017-02-06 09:26:49 -05001386#ifdef SK_BUILD_FOR_WIN
Hal Canary5e221e72017-02-06 09:51:42 -05001387static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() {
1388 IXpsOMObjectFactory* factory;
1389 HRN(CoCreateInstance(CLSID_XpsOMObjectFactory,
1390 nullptr,
1391 CLSCTX_INPROC_SERVER,
1392 IID_PPV_ARGS(&factory)));
1393 return SkTScopedComPtr<IXpsOMObjectFactory>(factory);
1394}
1395
1396Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canaryabc88d22017-02-06 09:26:49 -05001397 SkAutoCoInitialize com;
1398 if (!com.succeeded()) {
1399 return "Could not initialize COM.";
1400 }
Hal Canary5e221e72017-02-06 09:51:42 -05001401 SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory();
1402 if (!factory) {
1403 return "Failed to create XPS Factory.";
1404 }
1405 sk_sp<SkDocument> doc(SkDocument::MakeXPS(dst, factory.get()));
halcanary47ef4d52015-03-03 09:13:09 -08001406 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001407 return "SkDocument::MakeXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001408 }
1409 return draw_skdocument(src, doc.get(), dst);
1410}
Hal Canary5e221e72017-02-06 09:51:42 -05001411#else
1412Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1413 return "XPS not supported on this platform.";
1414}
1415#endif
reed54dc4872016-09-13 08:09:45 -07001416
1417/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1418
1419PipeSink::PipeSink() {}
1420
1421Error PipeSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1422 return src.draw(SkPipeSerializer().beginWrite(SkRect::Make(src.size()), dst));
1423}
1424
mtklein748ca3b2015-01-15 10:56:12 -08001425/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1426
mtklein9c3f17d2015-01-28 11:35:18 -08001427SKPSink::SKPSink() {}
1428
mtkleinb9eb4ac2015-02-02 18:26:03 -08001429Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001430 SkSize size;
1431 size = src.size();
1432 SkPictureRecorder recorder;
1433 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1434 if (!err.isEmpty()) {
1435 return err;
1436 }
reedca2622b2016-03-18 07:25:55 -07001437 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001438 return "";
1439}
1440
1441/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1442
Hal Canary85c7fe82016-10-25 10:33:27 -04001443Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1444 SkDebugCanvas debugCanvas(src.size().width(), src.size().height());
1445 Error err = src.draw(&debugCanvas);
1446 if (!err.isEmpty()) {
1447 return err;
1448 }
Mike Reed5df49342016-11-12 08:06:55 -06001449 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
Hal Canary85c7fe82016-10-25 10:33:27 -04001450 UrlDataManager dataManager(SkString("data"));
1451 Json::Value json = debugCanvas.toJSON(
1452 dataManager, debugCanvas.getSize(), nullCanvas.get());
1453 std::string value = Json::StyledWriter().write(json);
1454 return dst->write(value.c_str(), value.size()) ? "" : "SkWStream Error";
1455}
1456
1457/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1458
mtklein8a4527e2015-01-31 20:00:58 -08001459SVGSink::SVGSink() {}
1460
mtkleinb9eb4ac2015-02-02 18:26:03 -08001461Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita718df0a2016-07-15 10:33:29 -07001462#if defined(SK_XML)
Ben Wagner145dbcd2016-11-03 14:40:50 -04001463 std::unique_ptr<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
Mike Reed5df49342016-11-12 08:06:55 -06001464 return src.draw(SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
1465 SkIntToScalar(src.size().height())),
1466 xmlWriter.get()).get());
fmalita718df0a2016-07-15 10:33:29 -07001467#else
1468 return Error("SVG sink is disabled.");
1469#endif // SK_XML
mtklein8a4527e2015-01-31 20:00:58 -08001470}
1471
1472/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1473
brianosmanb109b8c2016-06-16 13:03:24 -07001474RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
mtklein27c3fdd2016-02-26 14:43:21 -08001475 : fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001476 , fColorSpace(std::move(colorSpace)) {}
mtklein748ca3b2015-01-15 10:56:12 -08001477
mtkleinb9eb4ac2015-02-02 18:26:03 -08001478Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001479 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001480 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1481 SkAlphaType alphaType = kPremul_SkAlphaType;
1482 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1483
mtklein27c3fdd2016-02-26 14:43:21 -08001484 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(),
brianosmanb109b8c2016-06-16 13:03:24 -07001485 fColorType, alphaType, fColorSpace),
Mike Reed6b3155c2017-04-03 14:41:44 -04001486 nullptr/*colortable*/, SkBitmap::kZeroPixels_AllocFlag);
mtklein748ca3b2015-01-15 10:56:12 -08001487 SkCanvas canvas(*dst);
1488 return src.draw(&canvas);
1489}
1490
1491/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1492
mtkleina16e69e2015-05-05 11:38:45 -07001493// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001494// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001495// Several examples below.
1496
mtkleincbf89782016-02-19 14:27:14 -08001497template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001498static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001499 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001500 class ProxySrc : public Src {
1501 public:
mtkleincbf89782016-02-19 14:27:14 -08001502 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001503 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001504 Name name() const override { return "ProxySrc"; }
1505 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001506 private:
mtkleincbf89782016-02-19 14:27:14 -08001507 SkISize fSize;
1508 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001509 };
msarett62d3b102015-12-10 15:14:27 -08001510 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001511}
1512
1513/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1514
mtklein4a34ecb2016-01-08 10:19:35 -08001515DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1516
1517// Is *bitmap identical to what you get drawing src into sink?
1518static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1519 // We can only check raster outputs.
1520 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1521 if (FLAGS_check && bitmap) {
1522 SkBitmap reference;
1523 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001524 SkDynamicMemoryWStream wStream;
1525 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001526 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1527 SkASSERT(err.isEmpty());
1528 if (!err.isEmpty()) {
1529 return err;
1530 }
1531 // The dimensions are a property of the Src only, and so should be identical.
1532 SkASSERT(reference.getSize() == bitmap->getSize());
1533 if (reference.getSize() != bitmap->getSize()) {
1534 return "Dimensions don't match reference";
1535 }
1536 // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy.
1537 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1538 return "Pixels don't match reference";
1539 }
1540 }
1541 return "";
1542}
1543
1544/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1545
mtkleind603b222015-02-17 11:13:33 -08001546static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1547 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1548 matrix->mapRect(&bounds);
1549 matrix->postTranslate(-bounds.x(), -bounds.y());
Hal Canaryfafe1352017-04-11 12:12:02 -04001550 return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())};
mtkleind603b222015-02-17 11:13:33 -08001551}
1552
msarett62d3b102015-12-10 15:14:27 -08001553ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001554
mtkleinb9eb4ac2015-02-02 18:26:03 -08001555Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001556 SkMatrix matrix = fMatrix;
1557 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
Ben Wagner145dbcd2016-11-03 14:40:50 -04001558 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001559 canvas->concat(matrix);
1560 return src.draw(canvas);
1561 });
mtklein748ca3b2015-01-15 10:56:12 -08001562}
1563
mtkleind603b222015-02-17 11:13:33 -08001564// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1565// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001566ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001567
1568Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1569 Error err = fSink->draw(src, bitmap, stream, log);
1570 if (!err.isEmpty()) {
1571 return err;
1572 }
1573
1574 SkMatrix inverse;
1575 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1576 return "Cannot upright --matrix.";
1577 }
1578 SkMatrix upright = SkMatrix::I();
1579 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1580 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1581 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1582 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1583
1584 SkBitmap uprighted;
1585 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1586 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1587
1588 SkCanvas canvas(uprighted);
1589 canvas.concat(upright);
1590 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07001591 paint.setBlendMode(SkBlendMode::kSrc);
mtkleind603b222015-02-17 11:13:33 -08001592 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1593
1594 *bitmap = uprighted;
mtkleind603b222015-02-17 11:13:33 -08001595 return "";
1596}
1597
mtklein748ca3b2015-01-15 10:56:12 -08001598/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1599
mtkleina16e69e2015-05-05 11:38:45 -07001600Error ViaSerialization::draw(
1601 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001602 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001603 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001604 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001605 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1606 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001607 if (!err.isEmpty()) {
1608 return err;
1609 }
reedca2622b2016-03-18 07:25:55 -07001610 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001611
1612 // Serialize it and then deserialize it.
reed39eaf5f2016-09-15 07:19:35 -07001613 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
mtklein748ca3b2015-01-15 10:56:12 -08001614
Ben Wagner145dbcd2016-11-03 14:40:50 -04001615 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001616 canvas->drawPicture(deserialized);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001617 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07001618 });
mtklein748ca3b2015-01-15 10:56:12 -08001619}
1620
1621/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1622
msarett62d3b102015-12-10 15:14:27 -08001623ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1624 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001625 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001626 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001627 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001628
mtkleinb9eb4ac2015-02-02 18:26:03 -08001629Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001630 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001631 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001632 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1633 SkIntToScalar(size.height()),
1634 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001635 if (!err.isEmpty()) {
1636 return err;
1637 }
reedca2622b2016-03-18 07:25:55 -07001638 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001639
Ben Wagner145dbcd2016-11-03 14:40:50 -04001640 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001641 const int xTiles = (size.width() + fW - 1) / fW,
1642 yTiles = (size.height() + fH - 1) / fH;
1643 SkMultiPictureDraw mpd(xTiles*yTiles);
reede8f30622016-03-23 18:59:25 -07001644 SkTArray<sk_sp<SkSurface>> surfaces;
1645// surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001646
mtkleina16e69e2015-05-05 11:38:45 -07001647 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1648 for (int j = 0; j < yTiles; j++) {
1649 for (int i = 0; i < xTiles; i++) {
1650 // This lets our ultimate Sink determine the best kind of surface.
1651 // E.g., if it's a GpuSink, the surfaces and images are textures.
reede8f30622016-03-23 18:59:25 -07001652 auto s = canvas->makeSurface(info);
mtkleina16e69e2015-05-05 11:38:45 -07001653 if (!s) {
reede8f30622016-03-23 18:59:25 -07001654 s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001655 }
reede8f30622016-03-23 18:59:25 -07001656 surfaces.push_back(s);
mtkleina16e69e2015-05-05 11:38:45 -07001657 SkCanvas* c = s->getCanvas();
1658 c->translate(SkIntToScalar(-i * fW),
1659 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
reedca2622b2016-03-18 07:25:55 -07001660 mpd.add(c, pic.get());
mtklein748ca3b2015-01-15 10:56:12 -08001661 }
mtklein748ca3b2015-01-15 10:56:12 -08001662 }
mtkleina16e69e2015-05-05 11:38:45 -07001663 mpd.draw();
1664 for (int j = 0; j < yTiles; j++) {
1665 for (int i = 0; i < xTiles; i++) {
reed9ce9d672016-03-17 10:51:11 -07001666 sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot());
mtkleina16e69e2015-05-05 11:38:45 -07001667 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1668 }
1669 }
mtkleina16e69e2015-05-05 11:38:45 -07001670 return "";
1671 });
mtklein748ca3b2015-01-15 10:56:12 -08001672}
1673
mtkleinb7e8d692015-04-07 08:30:32 -07001674/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1675
mtklein4a34ecb2016-01-08 10:19:35 -08001676Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1677 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001678 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtklein4a34ecb2016-01-08 10:19:35 -08001679 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001680 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08001681 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1682 SkIntToScalar(size.height())));
1683 if (!err.isEmpty()) {
1684 return err;
1685 }
reedca2622b2016-03-18 07:25:55 -07001686 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08001687 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001688 return check_against_reference(bitmap, src, fSink.get());
mtklein4a34ecb2016-01-08 10:19:35 -08001689 });
1690}
1691
1692/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1693
reedbabc3de2016-07-08 08:43:27 -07001694Error ViaDefer::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1695 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001696 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
Herb Derby1a3c4b82017-03-17 15:51:53 -04001697 SkDeferredCanvas deferred(canvas, SkDeferredCanvas::kEager);
reedbabc3de2016-07-08 08:43:27 -07001698 return src.draw(&deferred);
1699 });
1700}
1701
1702/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1703
reed54dc4872016-09-13 08:09:45 -07001704Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1705 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001706 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
reed54dc4872016-09-13 08:09:45 -07001707 SkDynamicMemoryWStream tmpStream;
1708 Error err = src.draw(SkPipeSerializer().beginWrite(SkRect::Make(size), &tmpStream));
1709 if (!err.isEmpty()) {
1710 return err;
1711 }
1712 sk_sp<SkData> data = tmpStream.detachAsData();
1713 SkPipeDeserializer().playback(data->data(), data->size(), canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001714 return check_against_reference(bitmap, src, fSink.get());
reed54dc4872016-09-13 08:09:45 -07001715 });
1716}
1717
1718/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1719
mtkleinb7e8d692015-04-07 08:30:32 -07001720// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1721// This tests that any shortcuts we may take while recording that second picture are legal.
1722Error ViaSecondPicture::draw(
1723 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001724 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001725 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001726 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001727 sk_sp<SkPicture> pic;
mtkleina16e69e2015-05-05 11:38:45 -07001728 for (int i = 0; i < 2; i++) {
1729 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1730 SkIntToScalar(size.height())));
1731 if (!err.isEmpty()) {
1732 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001733 }
reedca2622b2016-03-18 07:25:55 -07001734 pic = recorder.finishRecordingAsPicture();
mtkleinb7e8d692015-04-07 08:30:32 -07001735 }
mtkleina16e69e2015-05-05 11:38:45 -07001736 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001737 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07001738 });
mtkleinb7e8d692015-04-07 08:30:32 -07001739}
1740
mtkleind31c13d2015-05-05 12:59:56 -07001741/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1742
mtklein6fbf4b32015-05-06 11:35:40 -07001743// Draw the Src twice. This can help exercise caching.
1744Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001745 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001746 for (int i = 0; i < 2; i++) {
1747 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1748 canvas->clear(SK_ColorTRANSPARENT);
1749 Error err = src.draw(canvas);
1750 if (err.isEmpty()) {
1751 return err;
1752 }
1753 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001754 return check_against_reference(bitmap, src, fSink.get());
mtklein6fbf4b32015-05-06 11:35:40 -07001755 });
1756}
1757
1758/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1759
Mike Reedbae888e2017-02-18 16:50:45 -05001760#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -05001761#include "SkXMLWriter.h"
1762#include "SkSVGCanvas.h"
1763#include "SkSVGDOM.h"
1764
1765Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1766 auto size = src.size();
1767 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1768 SkDynamicMemoryWStream wstream;
1769 SkXMLStreamWriter writer(&wstream);
1770 Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get());
1771 if (!err.isEmpty()) {
1772 return err;
1773 }
1774 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
1775 auto dom = SkSVGDOM::MakeFromStream(*rstream);
1776 if (dom) {
1777 dom->setContainerSize(SkSize::Make(size));
1778 dom->render(canvas);
1779 }
1780 return "";
1781 });
1782}
Mike Reedbae888e2017-02-18 16:50:45 -05001783#endif
Mike Reedf67c4592017-02-17 17:06:11 -05001784
1785/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1786
mtkleind31c13d2015-05-05 12:59:56 -07001787// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1788// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1789// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1790struct DrawsAsSingletonPictures {
1791 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001792 const SkDrawableList& fDrawables;
mtklein11064df2016-04-06 15:01:57 -07001793 SkRect fBounds;
mtkleind31c13d2015-05-05 12:59:56 -07001794
mtkleind31c13d2015-05-05 12:59:56 -07001795 template <typename T>
1796 void draw(const T& op, SkCanvas* canvas) {
1797 // We must pass SkMatrix::I() as our initial matrix.
1798 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1799 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001800 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1801 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001802 }
1803
mtklein449d9b72015-09-28 10:33:02 -07001804 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001805 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001806 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001807 SkPictureRecorder rec;
mtklein11064df2016-04-06 15:01:57 -07001808 this->draw(op, rec.beginRecording(fBounds));
reedca2622b2016-03-18 07:25:55 -07001809 sk_sp<SkPicture> pic(rec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07001810 fCanvas->drawPicture(pic);
1811 }
1812
mtklein449d9b72015-09-28 10:33:02 -07001813 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001814 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001815 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1816 this->draw(op, fCanvas);
1817 }
mtkleind31c13d2015-05-05 12:59:56 -07001818};
1819
mtkleind31c13d2015-05-05 12:59:56 -07001820// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1821// Then play back that macro picture into our wrapped sink.
1822Error ViaSingletonPictures::draw(
1823 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1824 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001825 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07001826 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1827 SkRecord skr;
1828 SkRecorder recorder(&skr, size.width(), size.height());
1829 Error err = src.draw(&recorder);
1830 if (!err.isEmpty()) {
1831 return err;
1832 }
1833
1834 // Record our macro-picture, with each draw op as its own sub-picture.
1835 SkPictureRecorder macroRec;
1836 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1837 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001838
bungeman6bd52842016-10-27 09:30:08 -07001839 std::unique_ptr<SkDrawableList> drawables(recorder.detachDrawableList());
mtkleind2baa902015-07-07 09:43:28 -07001840 const SkDrawableList empty;
1841
1842 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1843 macroCanvas,
1844 drawables ? *drawables : empty,
mtklein11064df2016-04-06 15:01:57 -07001845 SkRect::MakeWH((SkScalar)size.width(), (SkScalar)size.height()),
mtkleind2baa902015-07-07 09:43:28 -07001846 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001847 for (int i = 0; i < skr.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -07001848 skr.visit(i, drawsAsSingletonPictures);
mtkleind31c13d2015-05-05 12:59:56 -07001849 }
reedca2622b2016-03-18 07:25:55 -07001850 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07001851
1852 canvas->drawPicture(macroPic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001853 return check_against_reference(bitmap, src, fSink.get());
mtkleind31c13d2015-05-05 12:59:56 -07001854 });
1855}
1856
mtklein9c5052f2016-08-06 12:51:51 -07001857/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1858
1859Error ViaLite::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1860 auto size = src.size();
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05001861 SkIRect bounds = {0,0, size.width(), size.height()};
Ben Wagner145dbcd2016-11-03 14:40:50 -04001862 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05001863 SkLiteDL dl;
mtklein8bbbb692016-08-15 12:56:00 -07001864 SkLiteRecorder rec;
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05001865 rec.reset(&dl, bounds);
mtklein8bbbb692016-08-15 12:56:00 -07001866
1867 Error err = src.draw(&rec);
mtklein9c5052f2016-08-06 12:51:51 -07001868 if (!err.isEmpty()) {
1869 return err;
1870 }
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05001871 dl.draw(canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001872 return check_against_reference(bitmap, src, fSink.get());
mtklein9c5052f2016-08-06 12:51:51 -07001873 });
1874}
1875
Mike Klein841101d2017-03-10 09:55:51 -05001876/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1877
Mike Klein919cc452017-03-18 15:36:52 +00001878ViaCSXform::ViaCSXform(Sink* sink, sk_sp<SkColorSpace> cs, bool colorSpin)
1879 : Via(sink)
1880 , fCS(std::move(cs))
1881 , fColorSpin(colorSpin) {}
Mike Klein841101d2017-03-10 09:55:51 -05001882
1883Error ViaCSXform::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1884 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(),
1885 [&](SkCanvas* canvas) -> Error {
1886 auto proxy = SkCreateColorSpaceXformCanvas(canvas, fCS);
Mike Klein919cc452017-03-18 15:36:52 +00001887 Error err = src.draw(proxy.get());
1888 if (!err.isEmpty()) {
1889 return err;
1890 }
1891
1892 // Undo the color spin, so we can look at the pixels in Gold.
1893 if (fColorSpin) {
1894 SkBitmap pixels;
1895 pixels.allocPixels(canvas->imageInfo());
Mike Reed12e946b2017-04-17 10:53:29 -04001896 canvas->readPixels(pixels, 0, 0);
Mike Klein919cc452017-03-18 15:36:52 +00001897 for (int y = 0; y < pixels.height(); y++) {
1898 for (int x = 0; x < pixels.width(); x++) {
1899 uint32_t pixel = *pixels.getAddr32(x, y);
1900 uint8_t r = SkGetPackedR32(pixel);
1901 uint8_t g = SkGetPackedG32(pixel);
1902 uint8_t b = SkGetPackedB32(pixel);
1903 uint8_t a = SkGetPackedA32(pixel);
1904 *pixels.getAddr32(x, y) =
1905 SkSwizzle_RGBA_to_PMColor(b << 0 | r << 8 | g << 16 | a << 24);
1906 }
1907 }
1908
1909 canvas->writePixels(pixels, 0, 0);
1910 }
1911
1912 return "";
Mike Klein841101d2017-03-10 09:55:51 -05001913 });
1914}
1915
mtklein748ca3b2015-01-15 10:56:12 -08001916} // namespace DM