blob: d0c468159e8c165c0deb0645788e593408b1f7f8 [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"
Brian Osmane5756ec2017-09-06 17:08:30 -040012#include "SkBase64.h"
scroggof24f2242015-03-03 08:59:20 -080013#include "SkCodec.h"
msarettb714fb02016-01-22 14:46:42 -080014#include "SkCodecImageGenerator.h"
msarett888dc162016-05-23 10:21:17 -070015#include "SkColorSpace.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000016#include "SkColorSpaceXform.h"
Mike Klein841101d2017-03-10 09:55:51 -050017#include "SkColorSpaceXformCanvas.h"
Hal Canary95e3c052017-01-11 12:44:43 -050018#include "SkColorSpace_XYZ.h"
mtkleina16e69e2015-05-05 11:38:45 -070019#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070020#include "SkCommonFlagsGpu.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070021#include "SkData.h"
Hal Canary85c7fe82016-10-25 10:33:27 -040022#include "SkDebugCanvas.h"
Robert Phillipsad8a43f2017-08-30 12:06:35 -040023#include "SkDeferredDisplayListRecorder.h"
mtklein748ca3b2015-01-15 10:56:12 -080024#include "SkDocument.h"
Brian Osmanf9810662017-08-30 10:02:10 -040025#include "SkExecutor.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070026#include "SkImageGenerator.h"
msarett18976312016-03-09 14:20:58 -080027#include "SkImageGeneratorCG.h"
msarettfc0b6d12016-03-17 13:50:17 -070028#include "SkImageGeneratorWIC.h"
mtklein8bbbb692016-08-15 12:56:00 -070029#include "SkLiteDL.h"
30#include "SkLiteRecorder.h"
mtkleinc8be09a2016-01-04 18:56:57 -080031#include "SkMallocPixelRef.h"
Hal Canary45cde312017-04-03 16:06:42 -040032#include "SkMultiPictureDocumentPriv.h"
mtklein748ca3b2015-01-15 10:56:12 -080033#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080034#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080035#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050036#include "SkOSPath.h"
msarett9e9444c2016-02-03 12:39:10 -080037#include "SkOpts.h"
Mike Reede7a58322017-12-20 14:09:20 -050038#include "SkPictureCommon.h"
mtkleinffa901a2015-03-16 10:38:07 -070039#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080040#include "SkPictureRecorder.h"
reed54dc4872016-09-13 08:09:45 -070041#include "SkPipe.h"
Brian Osmane5756ec2017-09-06 17:08:30 -040042#include "SkPngEncoder.h"
mtklein748ca3b2015-01-15 10:56:12 -080043#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070044#include "SkRecordDraw.h"
45#include "SkRecorder.h"
Robert Phillipsad8a43f2017-08-30 12:06:35 -040046#include "SkSurfaceCharacterization.h"
fmalita2aafe6f2015-02-06 12:51:10 -080047#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080048#include "SkStream.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000049#include "SkSwizzler.h"
Robert Phillipsad8a43f2017-08-30 12:06:35 -040050#include "SkTaskGroup.h"
Hal Canary95e3c052017-01-11 12:44:43 -050051#include "SkTLogic.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000052#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050053#include <functional>
Mike Klein45c16fa2017-07-18 18:15:13 -040054#include "../src/jumper/SkJumper.h"
mtklein748ca3b2015-01-15 10:56:12 -080055
msarettfc0b6d12016-03-17 13:50:17 -070056#if defined(SK_BUILD_FOR_WIN)
57 #include "SkAutoCoInitialize.h"
Hal Canary5e221e72017-02-06 09:51:42 -050058 #include "SkHRESULT.h"
59 #include "SkTScopedComPtr.h"
60 #include <XpsObjectModel.h>
msarettfc0b6d12016-03-17 13:50:17 -070061#endif
62
Florin Malita124d5af2017-12-31 17:02:26 -050063#if !defined(SK_BUILD_FOR_GOOGLE3)
64 #include "Skotty.h"
65#endif
66
fmalita718df0a2016-07-15 10:33:29 -070067#if defined(SK_XML)
fmalitaa48f0e32016-08-04 06:26:05 -070068 #include "SkSVGDOM.h"
fmalita718df0a2016-07-15 10:33:29 -070069 #include "SkXMLWriter.h"
70#endif
71
halcanary7e798182015-04-14 14:06:18 -070072DEFINE_bool(multiPage, false, "For document-type backends, render the source"
73 " into multiple pages");
scroggo3ac66e92016-02-08 15:09:48 -080074DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
halcanary7e798182015-04-14 14:06:18 -070075
bsalomon3724e572016-03-30 18:56:19 -070076using sk_gpu_test::GrContextFactory;
77
mtklein748ca3b2015-01-15 10:56:12 -080078namespace DM {
79
mtklein748ca3b2015-01-15 10:56:12 -080080GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
81
82Error GMSrc::draw(SkCanvas* canvas) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040083 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080084 gm->draw(canvas);
85 return "";
86}
87
88SkISize GMSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040089 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080090 return gm->getISize();
91}
92
93Name GMSrc::name() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040094 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080095 return gm->getName();
96}
97
bsalomon4ee6bd82015-05-27 13:23:23 -070098void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040099 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -0700100 gm->modifyGrContextOptions(options);
101}
102
mtklein748ca3b2015-01-15 10:56:12 -0800103/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
104
msarettd1227a72016-05-18 06:23:57 -0700105BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
msaretta5783ae2015-09-08 15:35:32 -0700106 : fPath(path)
msaretta5783ae2015-09-08 15:35:32 -0700107 , fMode(mode)
108 , fDstColorType(dstColorType)
109 , fSampleSize(sampleSize)
110{}
111
112bool BRDSrc::veto(SinkFlags flags) const {
113 // No need to test to non-raster or indirect backends.
114 return flags.type != SinkFlags::kRaster
115 || flags.approach != SinkFlags::kDirect;
116}
117
msarettd1227a72016-05-18 06:23:57 -0700118static SkBitmapRegionDecoder* create_brd(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700119 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msaretta5783ae2015-09-08 15:35:32 -0700120 if (!encoded) {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400121 return nullptr;
msaretta5783ae2015-09-08 15:35:32 -0700122 }
reed42943c82016-09-12 12:01:44 -0700123 return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy);
msaretta5783ae2015-09-08 15:35:32 -0700124}
125
Matt Sarett334df3a2016-12-15 18:17:33 -0500126static inline void alpha8_to_gray8(SkBitmap* bitmap) {
127 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
128 // them back to kGray8 so our test framework can draw them correctly.
129 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
130 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
131 .makeAlphaType(kOpaque_SkAlphaType);
132 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
133 }
134}
135
msaretta5783ae2015-09-08 15:35:32 -0700136Error BRDSrc::draw(SkCanvas* canvas) const {
Matt Saretta40d9c82017-05-19 15:21:05 -0400137 if (canvas->imageInfo().colorSpace() &&
138 kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) {
139 // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only
140 // run these tests on legacy canvases.
141 // We allow an exception for F16, since Android uses F16.
142 return Error::Nonfatal("Skip testing to color correct canvas.");
143 }
144
msaretta5783ae2015-09-08 15:35:32 -0700145 SkColorType colorType = canvas->imageInfo().colorType();
146 if (kRGB_565_SkColorType == colorType &&
147 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
148 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
149 }
150 switch (fDstColorType) {
151 case CodecSrc::kGetFromCanvas_DstColorType:
152 break;
msaretta5783ae2015-09-08 15:35:32 -0700153 case CodecSrc::kGrayscale_Always_DstColorType:
154 colorType = kGray_8_SkColorType;
155 break;
msarett34e0ec42016-04-22 16:27:24 -0700156 default:
157 SkASSERT(false);
158 break;
msaretta5783ae2015-09-08 15:35:32 -0700159 }
160
Ben Wagner145dbcd2016-11-03 14:40:50 -0400161 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700162 if (nullptr == brd.get()) {
163 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
164 }
165
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400166 if (kRGB_565_SkColorType == colorType) {
167 auto recommendedCT = brd->computeOutputColorType(colorType);
168 if (recommendedCT != colorType) {
169 return Error::Nonfatal("Skip decoding non-opaque to 565.");
170 }
171 }
172
msaretta5783ae2015-09-08 15:35:32 -0700173 const uint32_t width = brd->width();
174 const uint32_t height = brd->height();
175 // Visually inspecting very small output images is not necessary.
176 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
177 return Error::Nonfatal("Scaling very small images is uninteresting.");
178 }
179 switch (fMode) {
180 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700181 SkBitmap bitmap;
182 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
Matt Sarett68feef42017-04-11 09:51:32 -0400183 fSampleSize, colorType, false, SkColorSpace::MakeSRGB())) {
mtklein9b439152015-12-09 13:02:26 -0800184 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700185 }
Matt Sarett334df3a2016-12-15 18:17:33 -0500186 alpha8_to_gray8(&bitmap);
Matt Sarett8db74f12017-06-14 13:02:05 +0000187
msarett35e5d1b2015-10-27 12:50:25 -0700188 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700189 return "";
190 }
191 case kDivisor_Mode: {
192 const uint32_t divisor = 2;
193 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800194 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700195 }
196
197 // Use a border to test subsets that extend outside the image.
198 // We will not allow the border to be larger than the image dimensions. Allowing
199 // these large borders causes off by one errors that indicate a problem with the
200 // test suite, not a problem with the implementation.
201 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
202 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
203 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
204
205 // We may need to clear the canvas to avoid uninitialized memory.
206 // Assume we are scaling a 780x780 image with sampleSize = 8.
207 // The output image should be 97x97.
208 // Each subset will be 390x390.
209 // Each scaled subset be 48x48.
210 // Four scaled subsets will only fill a 96x96 image.
211 // The bottom row and last column will not be touched.
212 // This is an unfortunate result of our rounding rules when scaling.
213 // Maybe we need to consider testing scaled subsets without trying to
214 // combine them to match the full scaled image? Or maybe this is the
215 // best we can do?
216 canvas->clear(0);
217
218 for (uint32_t x = 0; x < divisor; x++) {
219 for (uint32_t y = 0; y < divisor; y++) {
220 // Calculate the subset dimensions
221 uint32_t subsetWidth = width / divisor;
222 uint32_t subsetHeight = height / divisor;
223 const int left = x * subsetWidth;
224 const int top = y * subsetHeight;
225
226 // Increase the size of the last subset in each row or column, when the
227 // divisor does not divide evenly into the image dimensions
228 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
229 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
230
231 // Increase the size of the subset in order to have a border on each side
232 const int decodeLeft = left - unscaledBorder;
233 const int decodeTop = top - unscaledBorder;
234 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
235 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700236 SkBitmap bitmap;
237 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
Matt Sarett68feef42017-04-11 09:51:32 -0400238 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false,
239 SkColorSpace::MakeSRGB())) {
mtklein9b439152015-12-09 13:02:26 -0800240 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700241 }
msaretta5783ae2015-09-08 15:35:32 -0700242
Matt Sarett334df3a2016-12-15 18:17:33 -0500243 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700244 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700245 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
246 (SkScalar) (subsetWidth / fSampleSize),
247 (SkScalar) (subsetHeight / fSampleSize)),
248 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
249 (SkScalar) (top / fSampleSize),
250 (SkScalar) (subsetWidth / fSampleSize),
251 (SkScalar) (subsetHeight / fSampleSize)),
252 nullptr);
253 }
254 }
255 return "";
256 }
257 default:
258 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800259 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700260 }
261}
262
263SkISize BRDSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400264 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700265 if (brd) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400266 return {SkTMax(1, brd->width() / (int)fSampleSize),
267 SkTMax(1, brd->height() / (int)fSampleSize)};
msaretta5783ae2015-09-08 15:35:32 -0700268 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400269 return {0, 0};
msaretta5783ae2015-09-08 15:35:32 -0700270}
271
272static SkString get_scaled_name(const Path& path, float scale) {
273 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
274}
275
276Name BRDSrc::name() const {
277 // We will replicate the names used by CodecSrc so that images can
278 // be compared in Gold.
279 if (1 == fSampleSize) {
280 return SkOSPath::Basename(fPath.c_str());
281 }
msarett4b0778e2015-11-13 09:59:11 -0800282 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700283}
284
285/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
286
scroggo3ac66e92016-02-08 15:09:48 -0800287static bool serial_from_path_name(const SkString& path) {
288 if (!FLAGS_RAW_threading) {
289 static const char* const exts[] = {
290 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
291 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
292 };
293 const char* actualExt = strrchr(path.c_str(), '.');
294 if (actualExt) {
295 actualExt++;
296 for (auto* ext : exts) {
297 if (0 == strcmp(ext, actualExt)) {
298 return true;
299 }
300 }
301 }
302 }
303 return false;
304}
305
scroggoc5560be2016-02-03 09:42:42 -0800306CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
307 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700308 : fPath(path)
309 , fMode(mode)
310 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800311 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700312 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800313 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700314{}
mtklein748ca3b2015-01-15 10:56:12 -0800315
mtklein99cab4e2015-07-31 06:43:04 -0700316bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800317 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800318 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700319}
scroggo9b77ddd2015-03-19 06:03:39 -0700320
msarett34e0ec42016-04-22 16:27:24 -0700321// Allows us to test decodes to non-native 8888.
msarettb1be46b2016-05-17 08:52:11 -0700322static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700323 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
324 return;
325 }
326
327 for (int y = 0; y < bitmap.height(); y++) {
328 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
329 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
330 }
331}
332
msarett9e9444c2016-02-03 12:39:10 -0800333// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
334// This allows us to still test unpremultiplied decodes.
msarettb1be46b2016-05-17 08:52:11 -0700335static void premultiply_if_necessary(SkBitmap& bitmap) {
msarett9e9444c2016-02-03 12:39:10 -0800336 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
337 return;
338 }
339
340 switch (bitmap.colorType()) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400341 case kRGBA_F16_SkColorType: {
342 SkJumper_MemoryCtx ctx = { bitmap.getAddr(0,0), bitmap.rowBytesAsPixels() };
343 SkRasterPipeline_<256> p;
344 p.append(SkRasterPipeline::load_f16, &ctx);
345 p.append(SkRasterPipeline::premul);
346 p.append(SkRasterPipeline::store_f16, &ctx);
347 p.run(0,0, bitmap.width(), bitmap.height());
348 }
Leon Scroggins IIIeceee042017-05-23 16:58:09 -0400349 break;
msarett9e9444c2016-02-03 12:39:10 -0800350 case kN32_SkColorType:
351 for (int y = 0; y < bitmap.height(); y++) {
352 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
353 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
354 }
355 break;
msarett9e9444c2016-02-03 12:39:10 -0800356 default:
357 // No need to premultiply kGray or k565 outputs.
358 break;
359 }
msarette1daa482016-02-03 15:31:18 -0800360
361 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
362 // bitmap as kPremul.
363 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800364}
365
msarettb1be46b2016-05-17 08:52:11 -0700366static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
scroggoba584892016-05-20 13:56:13 -0700367 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
msarett3d9d7a72015-10-21 10:27:10 -0700368 switch (dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700369 case CodecSrc::kGrayscale_Always_DstColorType:
scroggo1a361922016-05-20 14:27:16 -0700370 if (kRGB_565_SkColorType == canvasColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700371 return false;
372 }
scroggoc5560be2016-02-03 09:42:42 -0800373 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700374 break;
msarett34e0ec42016-04-22 16:27:24 -0700375 case CodecSrc::kNonNative8888_Always_DstColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400376 if (kRGB_565_SkColorType == canvasColorType
377 || kRGBA_F16_SkColorType == canvasColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700378 return false;
379 }
380#ifdef SK_PMCOLOR_IS_RGBA
381 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
382#else
383 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
384#endif
385 break;
msarett3d9d7a72015-10-21 10:27:10 -0700386 default:
msarett55f7bdd2016-02-16 13:24:54 -0800387 if (kRGB_565_SkColorType == canvasColorType &&
388 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
389 return false;
390 }
Matt Sarett09a1c082017-02-01 15:34:22 -0800391
392 if (kRGBA_F16_SkColorType == canvasColorType) {
Brian Osman36703d92017-12-12 14:09:31 -0500393 sk_sp<SkColorSpace> linearSpace = decodeInfo->colorSpace()->makeLinearGamma();
Matt Sarett09a1c082017-02-01 15:34:22 -0800394 *decodeInfo = decodeInfo->makeColorSpace(std::move(linearSpace));
395 }
396
scroggoc5560be2016-02-03 09:42:42 -0800397 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700398 break;
399 }
400
scroggoba584892016-05-20 13:56:13 -0700401 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700402 return true;
403}
404
msarettb1be46b2016-05-17 08:52:11 -0700405static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000406 CodecSrc::DstColorType dstColorType,
msarettb1be46b2016-05-17 08:52:11 -0700407 SkScalar left = 0, SkScalar top = 0) {
msarettb1be46b2016-05-17 08:52:11 -0700408 SkBitmap bitmap;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000409 bitmap.installPixels(info, pixels, rowBytes);
msarettb1be46b2016-05-17 08:52:11 -0700410 premultiply_if_necessary(bitmap);
411 swap_rb_if_necessary(bitmap, dstColorType);
412 canvas->drawBitmap(bitmap, left, top);
413}
414
Matt Sarettf485cf92017-05-26 08:58:03 -0400415// For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or
416// color format conversions should be performed by the codec. Sometimes the output of the
417// decode will be in an interesting color space. On our srgb and f16 backends, we need to
418// "pretend" that the color space is standard sRGB to avoid triggering color conversion
419// at draw time.
420static void set_bitmap_color_space(SkImageInfo* info) {
421 if (kRGBA_F16_SkColorType == info->colorType()) {
422 *info = info->makeColorSpace(SkColorSpace::MakeSRGBLinear());
423 } else {
424 *info = info->makeColorSpace(SkColorSpace::MakeSRGB());
425 }
426}
427
mtkleine0effd62015-07-29 06:37:28 -0700428Error CodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700429 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800430 if (!encoded) {
431 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
432 }
msarettb714fb02016-01-22 14:46:42 -0800433
Mike Reedede7bac2017-07-23 15:30:02 -0400434 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700435 if (nullptr == codec.get()) {
436 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700437 }
438
scroggoba584892016-05-20 13:56:13 -0700439 SkImageInfo decodeInfo = codec->getInfo();
440 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
441 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800442 return Error::Nonfatal("Skipping uninteresting test.");
msarett438b2ad2015-04-09 12:43:10 -0700443 }
444
msarett0a242972015-06-11 14:27:27 -0700445 // Try to scale the image if it is desired
446 SkISize size = codec->getScaledDimensions(fScale);
447 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
448 return Error::Nonfatal("Test without scaling is uninteresting.");
449 }
msarettb32758a2015-08-18 13:22:46 -0700450
451 // Visually inspecting very small output images is not necessary. We will
452 // cover these cases in unit testing.
453 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
454 return Error::Nonfatal("Scaling very small images is uninteresting.");
455 }
msarett0a242972015-06-11 14:27:27 -0700456 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
457
msarettb1be46b2016-05-17 08:52:11 -0700458 const int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
459 const size_t rowBytes = size.width() * bpp;
Mike Reedf0ffb892017-10-03 14:47:21 -0400460 const size_t safeSize = decodeInfo.computeByteSize(rowBytes);
scroggo19b91532016-10-24 09:03:26 -0700461 SkAutoMalloc pixels(safeSize);
msarett438b2ad2015-04-09 12:43:10 -0700462
msarettbb25b532016-01-13 09:31:39 -0800463 SkCodec::Options options;
Matt Saretta40d9c82017-05-19 15:21:05 -0400464 options.fPremulBehavior = canvas->imageInfo().colorSpace() ?
465 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
msarettbb25b532016-01-13 09:31:39 -0800466 if (kCodecZeroInit_Mode == fMode) {
msarettb1be46b2016-05-17 08:52:11 -0700467 memset(pixels.get(), 0, size.height() * rowBytes);
msarettbb25b532016-01-13 09:31:39 -0800468 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
469 }
msarett34e0ec42016-04-22 16:27:24 -0700470
471 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400472 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700473 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
474 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
475 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
476 }
scroggo9b77ddd2015-03-19 06:03:39 -0700477
scroggo9c59ebc2015-03-25 13:48:49 -0700478 switch (fMode) {
scroggo19b91532016-10-24 09:03:26 -0700479 case kAnimated_Mode: {
480 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
481 if (frameInfos.size() <= 1) {
482 return SkStringPrintf("%s is not an animated image.", fPath.c_str());
483 }
484
scroggo90e971a2016-10-25 06:11:01 -0700485 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
486 // into. "factor" is the number of frames to draw on one row. There will be
487 // up to "factor" rows as well.
488 const float root = sqrt((float) frameInfos.size());
489 const int factor = sk_float_ceil2int(root);
490
scroggo19b91532016-10-24 09:03:26 -0700491 // Used to cache a frame that future frames will depend on.
492 SkAutoMalloc priorFramePixels;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400493 int cachedFrame = SkCodec::kNone;
494 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
scroggo19b91532016-10-24 09:03:26 -0700495 options.fFrameIndex = i;
496 // Check for a prior frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400497 const int reqFrame = frameInfos[i].fRequiredFrame;
scroggo19b91532016-10-24 09:03:26 -0700498 if (reqFrame != SkCodec::kNone && reqFrame == cachedFrame
499 && priorFramePixels.get()) {
500 // Copy into pixels
501 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400502 options.fPriorFrame = reqFrame;
scroggo19b91532016-10-24 09:03:26 -0700503 } else {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400504 options.fPriorFrame = SkCodec::kNone;
scroggo19b91532016-10-24 09:03:26 -0700505 }
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500506 SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000507 rowBytes, &options);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500508 if (SkCodec::kInvalidInput == result && i > 0) {
509 // Some of our test images have truncated later frames. Treat that
510 // the same as incomplete.
511 result = SkCodec::kIncompleteInput;
512 }
scroggo19b91532016-10-24 09:03:26 -0700513 switch (result) {
514 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400515 case SkCodec::kErrorInInput:
scroggo90e971a2016-10-25 06:11:01 -0700516 case SkCodec::kIncompleteInput: {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400517 // If the next frame depends on this one, store it in priorFrame.
518 // It is possible that we may discard a frame that future frames depend on,
519 // but the codec will simply redecode the discarded frame.
520 // Do this before calling draw_to_canvas, which premultiplies in place. If
521 // we're decoding to unpremul, we want to pass the unmodified frame to the
522 // codec for decoding the next frame.
523 if (static_cast<size_t>(i+1) < frameInfos.size()
524 && frameInfos[i+1].fRequiredFrame == i) {
525 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
526 cachedFrame = i;
527 }
528
scroggo90e971a2016-10-25 06:11:01 -0700529 SkAutoCanvasRestore acr(canvas, true);
530 const int xTranslate = (i % factor) * decodeInfo.width();
531 const int yTranslate = (i / factor) * decodeInfo.height();
532 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
Leon Scroggins571b30f2017-07-11 17:35:31 +0000533 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
Leon Scroggins III674a1842017-07-06 12:26:09 -0400534 if (result != SkCodec::kSuccess) {
scroggo19b91532016-10-24 09:03:26 -0700535 return "";
536 }
537 break;
scroggo90e971a2016-10-25 06:11:01 -0700538 }
scroggo53f63b62016-10-27 08:29:13 -0700539 case SkCodec::kInvalidConversion:
Mike Reed304a07c2017-07-12 15:10:28 -0400540 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) {
scroggo53f63b62016-10-27 08:29:13 -0700541 return Error::Nonfatal(SkStringPrintf(
Mike Reed304a07c2017-07-12 15:10:28 -0400542 "Cannot decode frame %i to 565 (%s).", i, fPath.c_str()));
scroggo53f63b62016-10-27 08:29:13 -0700543 }
544 // Fall through.
scroggo19b91532016-10-24 09:03:26 -0700545 default:
546 return SkStringPrintf("Couldn't getPixels for frame %i in %s.",
547 i, fPath.c_str());
548 }
scroggo19b91532016-10-24 09:03:26 -0700549 }
550 break;
551 }
msarettbb25b532016-01-13 09:31:39 -0800552 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700553 case kCodec_Mode: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000554 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggoeb602a52015-07-09 08:16:03 -0700555 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400556 // We consider these to be valid, since we should still decode what is
scroggo9c59ebc2015-03-25 13:48:49 -0700557 // available.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400558 case SkCodec::kErrorInInput:
scroggoeb602a52015-07-09 08:16:03 -0700559 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700560 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700561 default:
562 // Everything else is considered a failure.
563 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
564 }
msarettb1be46b2016-05-17 08:52:11 -0700565
Leon Scroggins571b30f2017-07-11 17:35:31 +0000566 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggo9c59ebc2015-03-25 13:48:49 -0700567 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700568 }
scroggo9c59ebc2015-03-25 13:48:49 -0700569 case kScanline_Mode: {
scroggod8d68552016-06-06 11:26:17 -0700570 void* dst = pixels.get();
571 uint32_t height = decodeInfo.height();
scroggo19b91532016-10-24 09:03:26 -0700572 const bool useIncremental = [this]() {
573 auto exts = { "png", "PNG", "gif", "GIF" };
574 for (auto ext : exts) {
575 if (fPath.endsWith(ext)) {
576 return true;
577 }
578 }
579 return false;
580 }();
581 // ico may use the old scanline method or the new one, depending on whether it
582 // internally holds a bmp or a png.
scroggo8e6c7ad2016-09-16 08:20:38 -0700583 const bool ico = fPath.endsWith("ico");
scroggo19b91532016-10-24 09:03:26 -0700584 bool useOldScanlineMethod = !useIncremental && !ico;
585 if (useIncremental || ico) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700586 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000587 rowBytes, &options)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700588 int rowsDecoded;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400589 auto result = codec->incrementalDecode(&rowsDecoded);
590 if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700591 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
592 SkCodec::kNo_ZeroInitialized, height,
593 rowsDecoded);
scroggo6fb23912016-06-02 14:16:43 -0700594 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700595 } else {
scroggo19b91532016-10-24 09:03:26 -0700596 if (useIncremental) {
597 // Error: These should support incremental decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700598 return "Could not start incremental decode";
599 }
600 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
601 // which should work via startScanlineDecode
602 useOldScanlineMethod = true;
603 }
604 }
605
606 if (useOldScanlineMethod) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000607 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700608 return "Could not start scanline decoder";
609 }
610
611 switch (codec->getScanlineOrder()) {
612 case SkCodec::kTopDown_SkScanlineOrder:
613 case SkCodec::kBottomUp_SkScanlineOrder:
614 // We do not need to check the return value. On an incomplete
615 // image, memory will be filled with a default value.
616 codec->getScanlines(dst, height, rowBytes);
617 break;
msarett10522ff2015-09-07 08:54:01 -0700618 }
619 }
620
Leon Scroggins571b30f2017-07-11 17:35:31 +0000621 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700622 break;
623 }
msarett0a242972015-06-11 14:27:27 -0700624 case kStripe_Mode: {
625 const int height = decodeInfo.height();
626 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
627 // does not align with image blocks.
628 const int stripeHeight = 37;
629 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
msarettb1be46b2016-05-17 08:52:11 -0700630 void* dst = pixels.get();
msarett0a242972015-06-11 14:27:27 -0700631
632 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000633 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarettb65e6042016-02-23 05:37:25 -0800634 return "Could not start scanline decoder";
635 }
636
637 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
638 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
639 // to run this test for image types that do not have this scanline ordering.
scroggo12e2f502016-05-16 09:04:18 -0700640 // We only run this on Jpeg, which is always kTopDown.
641 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
msarette6dd0042015-10-09 11:07:34 -0700642
msarett0a242972015-06-11 14:27:27 -0700643 for (int i = 0; i < numStripes; i += 2) {
644 // Skip a stripe
645 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700646 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700647
648 // Read a stripe
649 const int startY = (i + 1) * stripeHeight;
650 const int linesToRead = SkTMin(stripeHeight, height - startY);
651 if (linesToRead > 0) {
msarettb1be46b2016-05-17 08:52:11 -0700652 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
653 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700654 }
655 }
656
657 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000658 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
scroggo1c005e42015-08-04 09:24:45 -0700659 if (SkCodec::kSuccess != startResult) {
660 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700661 }
662 for (int i = 0; i < numStripes; i += 2) {
663 // Read a stripe
664 const int startY = i * stripeHeight;
665 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarettb1be46b2016-05-17 08:52:11 -0700666 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
667 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700668
669 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700670 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
671 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700672 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700673 }
674 }
msarettb1be46b2016-05-17 08:52:11 -0700675
Leon Scroggins571b30f2017-07-11 17:35:31 +0000676 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700677 break;
msarett0a242972015-06-11 14:27:27 -0700678 }
msarett91c22b22016-02-22 12:27:46 -0800679 case kCroppedScanline_Mode: {
680 const int width = decodeInfo.width();
681 const int height = decodeInfo.height();
682 // This value is chosen because, as we move across the image, it will sometimes
683 // align with the jpeg block sizes and it will sometimes not. This allows us
684 // to test interestingly different code paths in the implementation.
685 const int tileSize = 36;
msarett91c22b22016-02-22 12:27:46 -0800686 SkIRect subset;
687 for (int x = 0; x < width; x += tileSize) {
688 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
Matt Saretta40d9c82017-05-19 15:21:05 -0400689 options.fSubset = &subset;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000690 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarett91c22b22016-02-22 12:27:46 -0800691 return "Could not start scanline decoder.";
692 }
693
msarettb1be46b2016-05-17 08:52:11 -0700694 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
msarett91c22b22016-02-22 12:27:46 -0800695 }
696
Leon Scroggins571b30f2017-07-11 17:35:31 +0000697 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800698 break;
699 }
scroggob636b452015-07-22 07:16:20 -0700700 case kSubset_Mode: {
701 // Arbitrarily choose a divisor.
702 int divisor = 2;
703 // Total width/height of the image.
704 const int W = codec->getInfo().width();
705 const int H = codec->getInfo().height();
706 if (divisor > W || divisor > H) {
707 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
708 "for %s with dimensions (%d x %d)", divisor,
709 fPath.c_str(), W, H));
710 }
711 // subset dimensions
712 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
713 const int w = SkAlign2(W / divisor);
714 const int h = SkAlign2(H / divisor);
715 SkIRect subset;
Matt Saretta40d9c82017-05-19 15:21:05 -0400716 options.fSubset = &subset;
scroggob636b452015-07-22 07:16:20 -0700717 SkBitmap subsetBm;
718 // We will reuse pixel memory from bitmap.
msarettb1be46b2016-05-17 08:52:11 -0700719 void* dst = pixels.get();
scroggob636b452015-07-22 07:16:20 -0700720 // Keep track of left and top (for drawing subsetBm into canvas). We could use
721 // fScale * x and fScale * y, but we want integers such that the next subset will start
722 // where the last one ended. So we'll add decodeInfo.width() and height().
723 int left = 0;
724 for (int x = 0; x < W; x += w) {
725 int top = 0;
726 for (int y = 0; y < H; y+= h) {
727 // Do not make the subset go off the edge of the image.
728 const int preScaleW = SkTMin(w, W - x);
729 const int preScaleH = SkTMin(h, H - y);
730 subset.setXYWH(x, y, preScaleW, preScaleH);
731 // And scale
732 // FIXME: Should we have a version of getScaledDimensions that takes a subset
733 // into account?
msarettc7eb4902016-04-25 07:04:58 -0700734 const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale));
735 const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale));
736 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
737 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
msarettb1be46b2016-05-17 08:52:11 -0700738 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
739 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000740 &options);
scroggob636b452015-07-22 07:16:20 -0700741 switch (result) {
742 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400743 case SkCodec::kErrorInInput:
scroggob636b452015-07-22 07:16:20 -0700744 case SkCodec::kIncompleteInput:
745 break;
scroggob636b452015-07-22 07:16:20 -0700746 default:
747 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
748 "from %s with dimensions (%d x %d)\t error %d",
749 x, y, decodeInfo.width(), decodeInfo.height(),
750 fPath.c_str(), W, H, result);
751 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000752 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType,
753 SkIntToScalar(left), SkIntToScalar(top));
msarettb1be46b2016-05-17 08:52:11 -0700754
scroggob636b452015-07-22 07:16:20 -0700755 // translate by the scaled height.
756 top += decodeInfo.height();
757 }
758 // translate by the scaled width.
759 left += decodeInfo.width();
760 }
761 return "";
762 }
msarettb714fb02016-01-22 14:46:42 -0800763 default:
764 SkASSERT(false);
765 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700766 }
scroggo9c59ebc2015-03-25 13:48:49 -0700767 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700768}
769
770SkISize CodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700771 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400772 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700773 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400774 return {0, 0};
scroggo7fac5af2015-09-30 11:33:12 -0700775 }
scroggo19b91532016-10-24 09:03:26 -0700776
777 auto imageSize = codec->getScaledDimensions(fScale);
778 if (fMode == kAnimated_Mode) {
scroggo90e971a2016-10-25 06:11:01 -0700779 // We'll draw one of each frame, so make it big enough to hold them all
780 // in a grid. The grid will be roughly square, with "factor" frames per
781 // row and up to "factor" rows.
scroggo19b91532016-10-24 09:03:26 -0700782 const size_t count = codec->getFrameInfo().size();
scroggo90e971a2016-10-25 06:11:01 -0700783 const float root = sqrt((float) count);
784 const int factor = sk_float_ceil2int(root);
785 imageSize.fWidth = imageSize.fWidth * factor;
786 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
scroggo19b91532016-10-24 09:03:26 -0700787 }
788 return imageSize;
scroggo9b77ddd2015-03-19 06:03:39 -0700789}
790
791Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700792 if (1.0f == fScale) {
scroggo6e8c68e2016-10-24 13:48:49 -0700793 Name name = SkOSPath::Basename(fPath.c_str());
794 if (fMode == kAnimated_Mode) {
795 name.append("_animated");
796 }
797 return name;
msarett0a242972015-06-11 14:27:27 -0700798 }
scroggo6e8c68e2016-10-24 13:48:49 -0700799 SkASSERT(fMode != kAnimated_Mode);
msaretta5783ae2015-09-08 15:35:32 -0700800 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700801}
802
803/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
804
scroggof8dc9df2016-05-16 09:04:13 -0700805AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800806 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700807 : fPath(path)
msarett3d9d7a72015-10-21 10:27:10 -0700808 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800809 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700810 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800811 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700812{}
813
814bool AndroidCodecSrc::veto(SinkFlags flags) const {
815 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700816 return flags.type != SinkFlags::kRaster
817 || flags.approach != SinkFlags::kDirect;
818}
819
820Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
Matt Saretta40d9c82017-05-19 15:21:05 -0400821 if (canvas->imageInfo().colorSpace() &&
822 kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) {
823 // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only
824 // run these tests on legacy canvases.
825 // We allow an exception for F16, since Android uses F16.
826 return Error::Nonfatal("Skip testing to color correct canvas.");
827 }
828
bungeman38d909e2016-08-02 14:40:46 -0700829 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700830 if (!encoded) {
831 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
832 }
Mike Reedede7bac2017-07-23 15:30:02 -0400833 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
834 if (nullptr == codec) {
msarett3d9d7a72015-10-21 10:27:10 -0700835 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
836 }
837
scroggoba584892016-05-20 13:56:13 -0700838 SkImageInfo decodeInfo = codec->getInfo();
839 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
840 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800841 return Error::Nonfatal("Skipping uninteresting test.");
msarett3d9d7a72015-10-21 10:27:10 -0700842 }
843
844 // Scale the image if it is desired.
845 SkISize size = codec->getSampledDimensions(fSampleSize);
846
847 // Visually inspecting very small output images is not necessary. We will
848 // cover these cases in unit testing.
849 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
850 return Error::Nonfatal("Scaling very small images is uninteresting.");
851 }
852 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
853
msarettb1be46b2016-05-17 08:52:11 -0700854 int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
855 size_t rowBytes = size.width() * bpp;
856 SkAutoMalloc pixels(size.height() * rowBytes);
msarett3d9d7a72015-10-21 10:27:10 -0700857
858 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700859 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400860 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700861 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
862 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
863 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
864 }
msarett3d9d7a72015-10-21 10:27:10 -0700865
866 // Create options for the codec.
867 SkAndroidCodec::AndroidOptions options;
msarett3d9d7a72015-10-21 10:27:10 -0700868 options.fSampleSize = fSampleSize;
869
msarettb1be46b2016-05-17 08:52:11 -0700870 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggof8dc9df2016-05-16 09:04:13 -0700871 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400872 case SkCodec::kErrorInInput:
scroggof8dc9df2016-05-16 09:04:13 -0700873 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 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000878 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, 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()));
Mike Reedede7bac2017-07-23 15:30:02 -0400884 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(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)
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -0400948 gen = SkImageGeneratorCG::MakeFromEncodedCG(encoded);
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 Sarettf485cf92017-05-26 08:58:03 -0400997 set_bitmap_color_space(&decodeInfo);
Leon Scroggins571b30f2017-07-11 17:35:31 +0000998 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes,
msarettb1be46b2016-05-17 08:52:11 -0700999 CodecSrc::kGetFromCanvas_DstColorType);
msarett18976312016-03-09 14:20:58 -08001000 return "";
1001}
1002
1003SkISize ImageGenSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001004 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001005 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett18976312016-03-09 14:20:58 -08001006 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001007 return {0, 0};
msarett18976312016-03-09 14:20:58 -08001008 }
1009 return codec->getInfo().dimensions();
1010}
1011
1012Name ImageGenSrc::name() const {
1013 return SkOSPath::Basename(fPath.c_str());
1014}
1015
1016/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1017
msarett9ce3a542016-07-15 13:54:38 -07001018ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, SkColorType colorType)
msarett69deca82016-04-29 09:38:40 -07001019 : fPath(path)
1020 , fMode(mode)
msarett9ce3a542016-07-15 13:54:38 -07001021 , fColorType(colorType)
msarett69deca82016-04-29 09:38:40 -07001022{}
1023
1024bool ColorCodecSrc::veto(SinkFlags flags) const {
1025 // Test to direct raster backends (8888 and 565).
1026 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
1027}
1028
Matt Sarett6a0feba2017-06-09 10:45:44 -04001029void clamp_if_necessary(const SkBitmap& bitmap, SkColorType dstCT) {
1030 if (kRGBA_F16_SkColorType != bitmap.colorType() || kRGBA_F16_SkColorType == dstCT) {
1031 // No need to clamp if the dst is F16. We will clamp when we encode to PNG.
1032 return;
1033 }
1034
Mike Klein45c16fa2017-07-18 18:15:13 -04001035 SkJumper_MemoryCtx ptr = { bitmap.getAddr(0,0), bitmap.rowBytesAsPixels() };
1036
Matt Sarett6a0feba2017-06-09 10:45:44 -04001037 SkRasterPipeline_<256> p;
1038 p.append(SkRasterPipeline::load_f16, &ptr);
1039 p.append(SkRasterPipeline::clamp_0);
1040 if (kPremul_SkAlphaType == bitmap.alphaType()) {
1041 p.append(SkRasterPipeline::clamp_a);
1042 } else {
1043 p.append(SkRasterPipeline::clamp_1);
1044 }
1045 p.append(SkRasterPipeline::store_f16, &ptr);
1046
Mike Klein45c16fa2017-07-18 18:15:13 -04001047 p.run(0,0, bitmap.width(), bitmap.height());
Matt Sarett6a0feba2017-06-09 10:45:44 -04001048}
1049
msarett69deca82016-04-29 09:38:40 -07001050Error ColorCodecSrc::draw(SkCanvas* canvas) const {
1051 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
1052 return Error::Nonfatal("No need to test color correction to 565 backend.");
1053 }
1054
msarettd1ec89b2016-08-03 12:59:27 -07001055 bool runInLegacyMode = kBaseline_Mode == fMode;
msarettd1ec89b2016-08-03 12:59:27 -07001056 if (runInLegacyMode && canvas->imageInfo().colorSpace()) {
1057 return Error::Nonfatal("Skipping tests that are only interesting in legacy mode.");
1058 } else if (!runInLegacyMode && !canvas->imageInfo().colorSpace()) {
1059 return Error::Nonfatal("Skipping tests that are only interesting in srgb mode.");
msarett9ce3a542016-07-15 13:54:38 -07001060 }
1061
bungeman38d909e2016-08-02 14:40:46 -07001062 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett69deca82016-04-29 09:38:40 -07001063 if (!encoded) {
1064 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1065 }
1066
Mike Reedede7bac2017-07-23 15:30:02 -04001067 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
1068 if (nullptr == codec) {
msarett69deca82016-04-29 09:38:40 -07001069 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
1070 }
1071
msarett9876ac52016-06-01 14:47:18 -07001072 // Load the dst ICC profile. This particular dst is fairly similar to Adobe RGB.
Mike Reed0933bc92017-12-09 01:27:41 +00001073 sk_sp<SkData> dstData = GetResourceAsData("icc_profiles/HP_ZR30w.icc");
msarett9876ac52016-06-01 14:47:18 -07001074 if (!dstData) {
1075 return "Cannot read monitor profile. Is the resource path set correctly?";
1076 }
1077
msarett50ce1f22016-07-29 06:23:33 -07001078 sk_sp<SkColorSpace> dstSpace = nullptr;
1079 if (kDst_sRGB_Mode == fMode) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -05001080 dstSpace = SkColorSpace::MakeSRGB();
msarett50ce1f22016-07-29 06:23:33 -07001081 } else if (kDst_HPZR30w_Mode == fMode) {
Brian Osman526972e2016-10-24 09:24:02 -04001082 dstSpace = SkColorSpace::MakeICC(dstData->data(), dstData->size());
msarett50ce1f22016-07-29 06:23:33 -07001083 }
1084
1085 SkImageInfo decodeInfo = codec->getInfo().makeColorType(fColorType).makeColorSpace(dstSpace);
msarettd1ec89b2016-08-03 12:59:27 -07001086 if (kUnpremul_SkAlphaType == decodeInfo.alphaType()) {
1087 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
1088 }
msarett2ecc35f2016-09-08 11:55:16 -07001089 if (kRGBA_F16_SkColorType == fColorType) {
Brian Osman36703d92017-12-12 14:09:31 -05001090 decodeInfo = decodeInfo.makeColorSpace(decodeInfo.colorSpace()->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001091 }
msarettd1ec89b2016-08-03 12:59:27 -07001092
msarett50ce1f22016-07-29 06:23:33 -07001093 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -04001094 set_bitmap_color_space(&bitmapInfo);
msarett50ce1f22016-07-29 06:23:33 -07001095 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
1096 kBGRA_8888_SkColorType == decodeInfo.colorType())
1097 {
1098 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
1099 }
1100
1101 SkBitmap bitmap;
1102 if (!bitmap.tryAllocPixels(bitmapInfo)) {
1103 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
1104 bitmapInfo.width(), bitmapInfo.height());
1105 }
1106
1107 size_t rowBytes = bitmap.rowBytes();
1108 SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), rowBytes);
Leon Scroggins III674a1842017-07-06 12:26:09 -04001109 switch (r) {
1110 case SkCodec::kSuccess:
1111 case SkCodec::kErrorInInput:
1112 case SkCodec::kIncompleteInput:
1113 break;
1114 default:
1115 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
msarett50ce1f22016-07-29 06:23:33 -07001116 }
1117
msarett69deca82016-04-29 09:38:40 -07001118 switch (fMode) {
1119 case kBaseline_Mode:
msarett39979d82016-07-28 17:11:18 -07001120 case kDst_sRGB_Mode:
msarett50ce1f22016-07-29 06:23:33 -07001121 case kDst_HPZR30w_Mode:
Matt Sarett6a0feba2017-06-09 10:45:44 -04001122 // We do not support drawing unclamped F16.
1123 clamp_if_necessary(bitmap, canvas->imageInfo().colorType());
msarett39979d82016-07-28 17:11:18 -07001124 canvas->drawBitmap(bitmap, 0, 0);
1125 break;
msarett69deca82016-04-29 09:38:40 -07001126 default:
1127 SkASSERT(false);
1128 return "Invalid fMode";
1129 }
1130 return "";
1131}
1132
1133SkISize ColorCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001134 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001135 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001136 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001137 return {0, 0};
msarett69deca82016-04-29 09:38:40 -07001138 }
Hal Canaryfafe1352017-04-11 12:12:02 -04001139 return {codec->getInfo().width(), codec->getInfo().height()};
msarett69deca82016-04-29 09:38:40 -07001140}
1141
1142Name ColorCodecSrc::name() const {
1143 return SkOSPath::Basename(fPath.c_str());
1144}
1145
1146/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1147
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001148static const SkRect kSKPViewport = {0, 0, 1000, 1000};
mtkleinf4ba3212015-01-28 15:32:24 -08001149
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001150SKPSrc::SKPSrc(Path path) : fPath(path) { }
mtklein748ca3b2015-01-15 10:56:12 -08001151
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001152static sk_sp<SkPicture> read_skp(const char* path) {
1153 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtklein75d98fd2015-01-18 07:05:01 -08001154 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001155 return nullptr;
mtklein748ca3b2015-01-15 10:56:12 -08001156 }
bungemanf93d7112016-09-16 06:24:20 -07001157 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
mtklein75d98fd2015-01-18 07:05:01 -08001158 if (!pic) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001159 return nullptr;
mtklein75d98fd2015-01-18 07:05:01 -08001160 }
bungemanf93d7112016-09-16 06:24:20 -07001161 stream = nullptr; // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -07001162
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001163 return pic;
1164}
1165
1166Error SKPSrc::draw(SkCanvas* canvas) const {
1167 sk_sp<SkPicture> pic = read_skp(fPath.c_str());
1168 if (!pic) {
1169 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1170 }
1171
mtkleinf4ba3212015-01-28 15:32:24 -08001172 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -08001173 canvas->drawPicture(pic);
1174 return "";
1175}
1176
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001177static SkRect get_cull_rect_for_skp(const char* path) {
1178 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtkleinffa901a2015-03-16 10:38:07 -07001179 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001180 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001181 }
1182 SkPictInfo info;
Mike Reede7a58322017-12-20 14:09:20 -05001183 if (!SkPicture_StreamIsSKP(stream.get(), &info)) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001184 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001185 }
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001186
1187 return info.fCullRect;
1188}
1189
1190SkISize SKPSrc::size() const {
1191 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
1192 if (!viewport.intersect(kSKPViewport)) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001193 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001194 }
1195 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001196}
1197
1198Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1199
1200/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001201
1202static const int kNumDDLXTiles = 4;
1203static const int kNumDDLYTiles = 4;
1204static const int kDDLTileSize = 1024;
1205static const SkRect kDDLSKPViewport = { 0, 0,
1206 kNumDDLXTiles * kDDLTileSize,
1207 kNumDDLYTiles * kDDLTileSize };
1208
1209DDLSKPSrc::DDLSKPSrc(Path path) : fPath(path) { }
1210
1211Error DDLSKPSrc::draw(SkCanvas* canvas) const {
1212 class TileData {
1213 public:
1214 // Note: we could just pass in surface characterization
1215 TileData(sk_sp<SkSurface> surf, const SkIRect& clip)
1216 : fSurface(std::move(surf))
1217 , fClip(clip) {
1218 SkAssertResult(fSurface->characterize(&fCharacterization));
1219 }
1220
1221 // This method operates in parallel
1222 void preprocess(SkPicture* pic) {
1223 SkDeferredDisplayListRecorder recorder(fCharacterization);
1224
1225 SkCanvas* subCanvas = recorder.getCanvas();
1226
1227 subCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
1228 subCanvas->translate(-fClip.fLeft, -fClip.fTop);
1229
1230 // Note: in this use case we only render a picture to the deferred canvas
1231 // but, more generally, clients will use arbitrary draw calls.
1232 subCanvas->drawPicture(pic);
1233
1234 fDisplayList = recorder.detach();
1235 }
1236
1237 // This method operates serially
1238 void draw() {
1239 fSurface->draw(fDisplayList.get());
1240 }
1241
1242 // This method also operates serially
1243 void compose(SkCanvas* dst) {
1244 sk_sp<SkImage> img = fSurface->makeImageSnapshot();
1245 dst->save();
1246 dst->clipRect(SkRect::Make(fClip));
1247 dst->drawImage(std::move(img), fClip.fLeft, fClip.fTop);
1248 dst->restore();
1249 }
1250
1251 private:
1252 sk_sp<SkSurface> fSurface;
1253 SkIRect fClip; // in the device space of the destination canvas
1254 std::unique_ptr<SkDeferredDisplayList> fDisplayList;
1255 SkSurfaceCharacterization fCharacterization;
1256 };
1257
1258 SkTArray<TileData> tileData;
1259 tileData.reserve(16);
1260
1261 sk_sp<SkPicture> pic = read_skp(fPath.c_str());
1262 if (!pic) {
1263 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1264 }
1265
1266 const SkRect cullRect = pic->cullRect();
1267
1268 // All the destination tiles are the same size
1269 const SkImageInfo tileII = SkImageInfo::MakeN32Premul(kDDLTileSize, kDDLTileSize);
1270
1271 // First, create the destination tiles
1272 for (int y = 0; y < kNumDDLYTiles; ++y) {
1273 for (int x = 0; x < kNumDDLXTiles; ++x) {
1274 SkRect clip = SkRect::MakeXYWH(x * kDDLTileSize, y * kDDLTileSize,
1275 kDDLTileSize, kDDLTileSize);
1276
1277 if (!clip.intersect(cullRect)) {
1278 continue;
1279 }
1280
1281 tileData.push_back(TileData(canvas->makeSurface(tileII), clip.roundOut()));
1282 }
1283 }
1284
1285 // Second, run the cpu pre-processing in threads
1286 SkTaskGroup().batch(tileData.count(), [&](int i) {
1287 tileData[i].preprocess(pic.get());
1288 });
1289
1290 // Third, synchronously render the display lists into the dest tiles
1291 // TODO: it would be cool to not wait until all the tiles are drawn to begin
1292 // drawing to the GPU
1293 for (int i = 0; i < tileData.count(); ++i) {
1294 tileData[i].draw();
1295 }
1296
1297 // Finally, compose the drawn tiles into the result
1298 // Note: the separation between the tiles and the final composition better
1299 // matches Chrome but costs us a copy
1300 for (int i = 0; i < tileData.count(); ++i) {
1301 tileData[i].compose(canvas);
1302 }
1303
1304 return "";
1305}
1306
1307SkISize DDLSKPSrc::size() const {
1308 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
1309 if (!viewport.intersect(kDDLSKPViewport)) {
1310 return {0, 0};
1311 }
1312 return viewport.roundOut().size();
1313}
1314
1315Name DDLSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1316
Florin Malitafc043dc2017-12-31 11:08:42 -05001317/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1318
Florin Malita124d5af2017-12-31 17:02:26 -05001319#if !defined(SK_BUILD_FOR_GOOGLE3)
Florin Malitafc043dc2017-12-31 11:08:42 -05001320SkottySrc::SkottySrc(Path path)
1321 : fName(SkOSPath::Basename(path.c_str())) {
1322
Florin Malita49328072018-01-08 12:51:12 -05001323 fAnimation = skotty::Animation::MakeFromFile(path.c_str());
Florin Malitafc043dc2017-12-31 11:08:42 -05001324 if (!fAnimation) {
1325 return;
1326 }
1327
1328 // Fit kTileCount x kTileCount frames to a 1000x1000 film strip.
1329 static constexpr SkScalar kTargetSize = 1000;
1330 const auto scale = kTargetSize / (kTileCount * std::max(fAnimation->size().width(),
1331 fAnimation->size().height()));
1332 fTileSize = SkSize::Make(scale * fAnimation->size().width(),
1333 scale * fAnimation->size().height()).toCeil();
1334
1335}
1336
1337Error SkottySrc::draw(SkCanvas* canvas) const {
1338 if (!fAnimation) {
1339 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
1340 }
1341
1342 canvas->drawColor(SK_ColorWHITE);
1343
Florin Malitadf2713c2018-01-09 15:51:21 -05001344 SkPaint paint, clockPaint;
Florin Malitafc043dc2017-12-31 11:08:42 -05001345 paint.setColor(0xffa0a0a0);
1346 paint.setStyle(SkPaint::kStroke_Style);
1347 paint.setStrokeWidth(0);
1348
Florin Malitadf2713c2018-01-09 15:51:21 -05001349 clockPaint.setTextSize(12);
1350
Florin Malitafc043dc2017-12-31 11:08:42 -05001351 const auto ip = fAnimation->inPoint() * 1000 / fAnimation->frameRate(),
1352 op = fAnimation->outPoint() * 1000 / fAnimation->frameRate(),
1353 fr = (op - ip) / (kTileCount * kTileCount - 1);
1354
1355 const auto canvas_size = this->size();
1356 for (int i = 0; i < kTileCount; ++i) {
1357 const SkScalar y = i * (fTileSize.height() + 1);
1358 canvas->drawLine(0, .5f + y, canvas_size.width(), .5f + y, paint);
1359
1360 for (int j = 0; j < kTileCount; ++j) {
1361 const SkScalar x = j * (fTileSize.width() + 1);
1362 canvas->drawLine(x + .5f, 0, x + .5f, canvas_size.height(), paint);
1363 SkRect dest = SkRect::MakeXYWH(x, y, fTileSize.width(), fTileSize.height());
1364
Florin Malitafc043dc2017-12-31 11:08:42 -05001365 const auto t = fr * (i * kTileCount + j);
Florin Malitadf2713c2018-01-09 15:51:21 -05001366 {
1367 SkAutoCanvasRestore acr(canvas, true);
1368 canvas->clipRect(dest);
1369 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(fAnimation->size()),
1370 dest,
1371 SkMatrix::kFill_ScaleToFit));
1372
1373 fAnimation->animationTick(t);
1374 fAnimation->render(canvas);
1375 }
1376
1377 const auto label = SkStringPrintf("%.3f", t);
1378 canvas->drawText(label.c_str(), label.size(), dest.x(),
1379 dest.bottom(), clockPaint);
Florin Malitafc043dc2017-12-31 11:08:42 -05001380 }
1381 }
1382
1383 return "";
1384}
1385
1386SkISize SkottySrc::size() const {
1387 // Padding for grid.
1388 return SkISize::Make(kTileCount * (fTileSize.width() + 1),
1389 kTileCount * (fTileSize.height() + 1));
1390}
1391
1392Name SkottySrc::name() const { return fName; }
1393
1394bool SkottySrc::veto(SinkFlags flags) const {
1395 // No need to test to non-(raster||gpu||vector) or indirect backends.
1396 bool type_ok = flags.type == SinkFlags::kRaster
1397 || flags.type == SinkFlags::kGPU
1398 || flags.type == SinkFlags::kVector;
1399
1400 return !type_ok || flags.approach != SinkFlags::kDirect;
1401}
Florin Malita124d5af2017-12-31 17:02:26 -05001402#endif
Florin Malitafc043dc2017-12-31 11:08:42 -05001403
1404/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fmalitaa2b9fdf2016-08-03 19:53:36 -07001405#if defined(SK_XML)
fmalitabdf3e5c2016-09-17 07:26:26 -07001406// Used when the image doesn't have an intrinsic size.
Hal Canaryfafe1352017-04-11 12:12:02 -04001407static const SkSize kDefaultSVGSize = {1000, 1000};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001408
fmalitabdf3e5c2016-09-17 07:26:26 -07001409// Used to force-scale tiny fixed-size images.
Hal Canaryfafe1352017-04-11 12:12:02 -04001410static const SkSize kMinimumSVGSize = {128, 128};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001411
fmalitaacd2f5c2016-11-08 07:13:45 -08001412SVGSrc::SVGSrc(Path path)
1413 : fName(SkOSPath::Basename(path.c_str()))
1414 , fScale(1) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001415
fmalitaacd2f5c2016-11-08 07:13:45 -08001416 SkFILEStream stream(path.c_str());
1417 if (!stream.isValid()) {
1418 return;
1419 }
1420 fDom = SkSVGDOM::MakeFromStream(stream);
1421 if (!fDom) {
1422 return;
1423 }
1424
1425 const SkSize& sz = fDom->containerSize();
1426 if (sz.isEmpty()) {
1427 // no intrinsic size
1428 fDom->setContainerSize(kDefaultSVGSize);
1429 } else {
1430 fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(),
1431 kMinimumSVGSize.height() / sz.height()));
1432 }
1433}
1434
1435Error SVGSrc::draw(SkCanvas* canvas) const {
fmalitabdf3e5c2016-09-17 07:26:26 -07001436 if (!fDom) {
fmalitaacd2f5c2016-11-08 07:13:45 -08001437 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
fmalitaa2b9fdf2016-08-03 19:53:36 -07001438 }
1439
fmalitaacd2f5c2016-11-08 07:13:45 -08001440 SkAutoCanvasRestore acr(canvas, true);
1441 canvas->scale(fScale, fScale);
1442 fDom->render(canvas);
1443
fmalitaa2b9fdf2016-08-03 19:53:36 -07001444 return "";
1445}
1446
1447SkISize SVGSrc::size() const {
fmalitaacd2f5c2016-11-08 07:13:45 -08001448 if (!fDom) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001449 return {0, 0};
fmalitabdf3e5c2016-09-17 07:26:26 -07001450 }
1451
Hal Canaryfafe1352017-04-11 12:12:02 -04001452 return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale}
1453 .toRound();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001454}
1455
fmalitaacd2f5c2016-11-08 07:13:45 -08001456Name SVGSrc::name() const { return fName; }
fmalitaa2b9fdf2016-08-03 19:53:36 -07001457
fmalita179d8852016-08-16 14:23:29 -07001458bool SVGSrc::veto(SinkFlags flags) const {
Florin Malita93323eb2017-06-16 15:31:17 -04001459 // No need to test to non-(raster||gpu||vector) or indirect backends.
fmalita179d8852016-08-16 14:23:29 -07001460 bool type_ok = flags.type == SinkFlags::kRaster
Florin Malita93323eb2017-06-16 15:31:17 -04001461 || flags.type == SinkFlags::kGPU
1462 || flags.type == SinkFlags::kVector;
fmalita179d8852016-08-16 14:23:29 -07001463
1464 return !type_ok || flags.approach != SinkFlags::kDirect;
1465}
1466
fmalitaa2b9fdf2016-08-03 19:53:36 -07001467#endif // defined(SK_XML)
1468/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein748ca3b2015-01-15 10:56:12 -08001469
halcanary45420a92016-06-02 12:41:14 -07001470MSKPSrc::MSKPSrc(Path path) : fPath(path) {
bungemanf93d7112016-09-16 06:24:20 -07001471 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001472 int count = SkMultiPictureDocumentReadPageCount(stream.get());
1473 if (count > 0) {
1474 fPages.reset(count);
1475 (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count());
1476 }
halcanary45420a92016-06-02 12:41:14 -07001477}
1478
Hal Canary45cde312017-04-03 16:06:42 -04001479int MSKPSrc::pageCount() const { return fPages.count(); }
halcanary45420a92016-06-02 12:41:14 -07001480
1481SkISize MSKPSrc::size() const { return this->size(0); }
Hal Canary45cde312017-04-03 16:06:42 -04001482SkISize MSKPSrc::size(int i) const {
Hal Canaryfafe1352017-04-11 12:12:02 -04001483 return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0};
Hal Canary45cde312017-04-03 16:06:42 -04001484}
halcanary45420a92016-06-02 12:41:14 -07001485
1486Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); }
1487Error MSKPSrc::draw(int i, SkCanvas* canvas) const {
Hal Canary45cde312017-04-03 16:06:42 -04001488 if (this->pageCount() == 0) {
halcanary45420a92016-06-02 12:41:14 -07001489 return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
1490 }
Hal Canary45cde312017-04-03 16:06:42 -04001491 if (i >= fPages.count() || i < 0) {
halcanary45420a92016-06-02 12:41:14 -07001492 return SkStringPrintf("MultiPictureDocument page number out of range: %d", i);
1493 }
Hal Canary45cde312017-04-03 16:06:42 -04001494 SkPicture* page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001495 if (!page) {
Hal Canary45cde312017-04-03 16:06:42 -04001496 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1497 if (!stream) {
1498 return SkStringPrintf("Unable to open file: %s", fPath.c_str());
1499 }
1500 if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) {
1501 return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i,
1502 fPath.c_str());
1503 }
1504 page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001505 }
1506 canvas->drawPicture(page);
1507 return "";
1508}
1509
1510Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1511
1512/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1513
mtkleinad66f9b2015-02-13 15:11:10 -08001514Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
Mike Reed5df49342016-11-12 08:06:55 -06001515 return src.draw(SkMakeNullCanvas().get());
mtkleinad66f9b2015-02-13 15:11:10 -08001516}
1517
1518/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1519
Brian Osmane5756ec2017-09-06 17:08:30 -04001520static bool encode_png_base64(const SkBitmap& bitmap, SkString* dst) {
1521 SkPixmap pm;
1522 if (!bitmap.peekPixels(&pm)) {
1523 dst->set("peekPixels failed");
1524 return false;
1525 }
1526
1527 // We're going to embed this PNG in a data URI, so make it as small as possible
1528 SkPngEncoder::Options options;
1529 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
1530 options.fZLibLevel = 9;
1531 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1532 : SkTransferFunctionBehavior::kIgnore;
1533
1534 SkDynamicMemoryWStream wStream;
1535 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
1536 dst->set("SkPngEncoder::Encode failed");
1537 return false;
1538 }
1539
1540 sk_sp<SkData> pngData = wStream.detachAsData();
1541 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
1542
1543 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
1544 // Infra says these can be pretty big, as long as we're only outputting them on failure.
1545 static const size_t kMaxBase64Length = 1024 * 1024;
1546 if (len > kMaxBase64Length) {
1547 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
1548 return false;
1549 }
1550
1551 dst->resize(len);
1552 SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
1553 return true;
1554}
1555
1556static Error compare_bitmaps(const SkBitmap& reference, const SkBitmap& bitmap) {
1557 // The dimensions are a property of the Src only, and so should be identical.
Mike Reedf0ffb892017-10-03 14:47:21 -04001558 SkASSERT(reference.computeByteSize() == bitmap.computeByteSize());
1559 if (reference.computeByteSize() != bitmap.computeByteSize()) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001560 return "Dimensions don't match reference";
1561 }
1562 // All SkBitmaps in DM are tight, so this comparison is easy.
Mike Reedf0ffb892017-10-03 14:47:21 -04001563 if (0 != memcmp(reference.getPixels(), bitmap.getPixels(), reference.computeByteSize())) {
Brian Osmane5756ec2017-09-06 17:08:30 -04001564 SkString encoded;
1565 SkString errString("Pixels don't match reference");
1566 if (encode_png_base64(reference, &encoded)) {
1567 errString.append("\nExpected: data:image/png;base64,");
1568 errString.append(encoded);
1569 } else {
1570 errString.append("\nExpected image failed to encode: ");
1571 errString.append(encoded);
1572 }
1573 if (encode_png_base64(bitmap, &encoded)) {
1574 errString.append("\nActual: data:image/png;base64,");
1575 errString.append(encoded);
1576 } else {
1577 errString.append("\nActual image failed to encode: ");
1578 errString.append(encoded);
1579 }
1580 return errString;
1581 }
1582 return "";
1583}
1584
1585/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1586
mtkleinb9eb4ac2015-02-02 18:26:03 -08001587DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
1588
bsalomon85b4b532016-04-05 11:06:27 -07001589GPUSink::GPUSink(GrContextFactory::ContextType ct,
csmartdaltone812d492017-02-21 12:36:05 -07001590 GrContextFactory::ContextOverrides overrides,
mtklein82d28432015-01-15 12:46:02 -08001591 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -07001592 bool diText,
brianosmand93c1202016-03-10 07:49:08 -08001593 SkColorType colorType,
Brian Salomonce5ee602017-07-17 11:31:31 -04001594 SkAlphaType alphaType,
brianosmanb109b8c2016-06-16 13:03:24 -07001595 sk_sp<SkColorSpace> colorSpace,
Brian Osmanf21aa042017-08-21 16:48:46 -04001596 bool threaded,
1597 const GrContextOptions& grCtxOptions)
Brian Salomonce5ee602017-07-17 11:31:31 -04001598 : fContextType(ct)
1599 , fContextOverrides(overrides)
1600 , fSampleCount(samples)
1601 , fUseDIText(diText)
1602 , fColorType(colorType)
1603 , fAlphaType(alphaType)
1604 , fColorSpace(std::move(colorSpace))
Brian Osmanf21aa042017-08-21 16:48:46 -04001605 , fThreaded(threaded)
1606 , fBaseContextOptions(grCtxOptions) {}
mtklein748ca3b2015-01-15 10:56:12 -08001607
Brian Salomon09d994e2016-12-21 11:14:46 -05001608DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
bsalomon648c6962015-10-23 09:06:59 -07001609
Brian Osmanf9810662017-08-30 10:02:10 -04001610Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const {
1611 return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
1612}
1613
1614Error GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
1615 const GrContextOptions& baseOptions) const {
1616 GrContextOptions grOptions = baseOptions;
kkinnunen64492c42015-12-08 01:24:40 -08001617
kkinnunen5219fd92015-12-10 06:28:13 -08001618 src.modifyGrContextOptions(&grOptions);
1619
1620 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -08001621 const SkISize size = src.size();
Brian Salomonce5ee602017-07-17 11:31:31 -04001622 SkImageInfo info =
1623 SkImageInfo::Make(size.width(), size.height(), fColorType, fAlphaType, fColorSpace);
msarett13a036b2016-02-08 09:10:47 -08001624#if SK_SUPPORT_GPU
csmartdaltone812d492017-02-21 12:36:05 -07001625 GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext();
bsalomon8b7451a2016-05-11 06:33:06 -07001626 const int maxDimension = context->caps()->maxTextureSize();
msarett13a036b2016-02-08 09:10:47 -08001627 if (maxDimension < SkTMax(size.width(), size.height())) {
1628 return Error::Nonfatal("Src too large to create a texture.\n");
1629 }
1630#endif
1631
reede8f30622016-03-23 18:59:25 -07001632 auto surface(
csmartdaltone812d492017-02-21 12:36:05 -07001633 NewGpuSurface(&factory, fContextType, fContextOverrides, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -08001634 if (!surface) {
1635 return "Could not create a surface.";
1636 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001637 if (FLAGS_preAbandonGpuContext) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001638 factory.abandonContexts();
1639 }
mtklein748ca3b2015-01-15 10:56:12 -08001640 SkCanvas* canvas = surface->getCanvas();
1641 Error err = src.draw(canvas);
1642 if (!err.isEmpty()) {
1643 return err;
1644 }
1645 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001646 if (FLAGS_gpuStats) {
1647 canvas->getGrContext()->dumpCacheStats(log);
1648 canvas->getGrContext()->dumpGpuStats(log);
1649 }
Brian Salomonce5ee602017-07-17 11:31:31 -04001650 if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType) {
1651 // We don't currently support readbacks into these formats on the GPU backend. Convert to
1652 // 32 bit.
1653 info = SkImageInfo::Make(size.width(), size.height(), kRGBA_8888_SkColorType,
1654 kPremul_SkAlphaType, fColorSpace);
1655 }
mtklein748ca3b2015-01-15 10:56:12 -08001656 dst->allocPixels(info);
Mike Reed12e946b2017-04-17 10:53:29 -04001657 canvas->readPixels(*dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001658 if (FLAGS_abandonGpuContext) {
1659 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001660 } else if (FLAGS_releaseAndAbandonGpuContext) {
1661 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001662 }
mtklein748ca3b2015-01-15 10:56:12 -08001663 return "";
1664}
1665
1666/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1667
Brian Osmanf9810662017-08-30 10:02:10 -04001668GPUThreadTestingSink::GPUThreadTestingSink(GrContextFactory::ContextType ct,
1669 GrContextFactory::ContextOverrides overrides,
1670 int samples,
1671 bool diText,
1672 SkColorType colorType,
1673 SkAlphaType alphaType,
1674 sk_sp<SkColorSpace> colorSpace,
1675 bool threaded,
1676 const GrContextOptions& grCtxOptions)
1677 : INHERITED(ct, overrides, samples, diText, colorType, alphaType, std::move(colorSpace),
1678 threaded, grCtxOptions)
Chris Dalton040238b2017-12-18 14:22:34 -07001679#if SK_SUPPORT_GPU
Mike Klein022cfa22017-09-01 11:53:16 -04001680 , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)) {
Chris Dalton040238b2017-12-18 14:22:34 -07001681#else
1682 , fExecutor(nullptr) {
1683#endif
Brian Osmanf9810662017-08-30 10:02:10 -04001684 SkASSERT(fExecutor);
1685}
1686
1687Error GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1688 SkString* log) const {
1689 // Draw twice, once with worker threads, and once without. Verify that we get the same result.
1690 // Also, force us to only use the software path renderer, so we really stress-test the threaded
1691 // version of that code.
1692 GrContextOptions contextOptions = this->baseContextOptions();
Brian Osman195c05b2017-08-30 15:14:04 -04001693 contextOptions.fGpuPathRenderers = GpuPathRenderers::kNone;
Brian Osmanf9810662017-08-30 10:02:10 -04001694
1695 contextOptions.fExecutor = fExecutor.get();
1696 Error err = this->onDraw(src, dst, wStream, log, contextOptions);
1697 if (!err.isEmpty() || !dst) {
1698 return err;
1699 }
1700
1701 SkBitmap reference;
1702 SkString refLog;
1703 SkDynamicMemoryWStream refStream;
1704 contextOptions.fExecutor = nullptr;
1705 Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1706 if (!refErr.isEmpty()) {
1707 return refErr;
1708 }
1709
Brian Osmane5756ec2017-09-06 17:08:30 -04001710 return compare_bitmaps(reference, *dst);
Brian Osmanf9810662017-08-30 10:02:10 -04001711}
1712
1713/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1714
halcanary47ef4d52015-03-03 09:13:09 -08001715static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001716 if (src.size().isEmpty()) {
1717 return "Source has empty dimensions";
1718 }
halcanary47ef4d52015-03-03 09:13:09 -08001719 SkASSERT(doc);
halcanary45420a92016-06-02 12:41:14 -07001720 int pageCount = src.pageCount();
1721 for (int i = 0; i < pageCount; ++i) {
1722 int width = src.size(i).width(), height = src.size(i).height();
halcanary7e798182015-04-14 14:06:18 -07001723 SkCanvas* canvas =
1724 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1725 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001726 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001727 }
halcanary45420a92016-06-02 12:41:14 -07001728 Error err = src.draw(i, canvas);
halcanary7e798182015-04-14 14:06:18 -07001729 if (!err.isEmpty()) {
1730 return err;
1731 }
1732 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001733 }
reedd14df7c2016-09-22 14:12:46 -07001734 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -08001735 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001736 return "";
1737}
1738
halcanary47ef4d52015-03-03 09:13:09 -08001739Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary4b656662016-04-27 07:45:18 -07001740 SkDocument::PDFMetadata metadata;
1741 metadata.fTitle = src.name();
1742 metadata.fSubject = "rendering correctness test";
1743 metadata.fCreator = "Skia/DM";
Mike Reeda4daf192017-12-14 13:25:04 -05001744 metadata.fRasterDPI = fRasterDpi;
1745 metadata.fPDFA = fPDFA;
1746 sk_sp<SkDocument> doc = SkDocument::MakePDF(dst, metadata);
halcanary47ef4d52015-03-03 09:13:09 -08001747 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001748 return "SkDocument::MakePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001749 }
1750 return draw_skdocument(src, doc.get(), dst);
1751}
1752
1753/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1754
1755XPSSink::XPSSink() {}
1756
Hal Canaryabc88d22017-02-06 09:26:49 -05001757#ifdef SK_BUILD_FOR_WIN
Hal Canary5e221e72017-02-06 09:51:42 -05001758static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() {
1759 IXpsOMObjectFactory* factory;
1760 HRN(CoCreateInstance(CLSID_XpsOMObjectFactory,
1761 nullptr,
1762 CLSCTX_INPROC_SERVER,
1763 IID_PPV_ARGS(&factory)));
1764 return SkTScopedComPtr<IXpsOMObjectFactory>(factory);
1765}
1766
1767Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canaryabc88d22017-02-06 09:26:49 -05001768 SkAutoCoInitialize com;
1769 if (!com.succeeded()) {
1770 return "Could not initialize COM.";
1771 }
Hal Canary5e221e72017-02-06 09:51:42 -05001772 SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory();
1773 if (!factory) {
1774 return "Failed to create XPS Factory.";
1775 }
1776 sk_sp<SkDocument> doc(SkDocument::MakeXPS(dst, factory.get()));
halcanary47ef4d52015-03-03 09:13:09 -08001777 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001778 return "SkDocument::MakeXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001779 }
1780 return draw_skdocument(src, doc.get(), dst);
1781}
Hal Canary5e221e72017-02-06 09:51:42 -05001782#else
1783Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1784 return "XPS not supported on this platform.";
1785}
1786#endif
reed54dc4872016-09-13 08:09:45 -07001787
1788/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1789
1790PipeSink::PipeSink() {}
1791
1792Error PipeSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1793 return src.draw(SkPipeSerializer().beginWrite(SkRect::Make(src.size()), dst));
1794}
1795
mtklein748ca3b2015-01-15 10:56:12 -08001796/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1797
mtklein9c3f17d2015-01-28 11:35:18 -08001798SKPSink::SKPSink() {}
1799
mtkleinb9eb4ac2015-02-02 18:26:03 -08001800Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001801 SkSize size;
1802 size = src.size();
1803 SkPictureRecorder recorder;
1804 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1805 if (!err.isEmpty()) {
1806 return err;
1807 }
reedca2622b2016-03-18 07:25:55 -07001808 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001809 return "";
1810}
1811
1812/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1813
Hal Canary85c7fe82016-10-25 10:33:27 -04001814Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1815 SkDebugCanvas debugCanvas(src.size().width(), src.size().height());
1816 Error err = src.draw(&debugCanvas);
1817 if (!err.isEmpty()) {
1818 return err;
1819 }
Mike Reed5df49342016-11-12 08:06:55 -06001820 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
Hal Canary85c7fe82016-10-25 10:33:27 -04001821 UrlDataManager dataManager(SkString("data"));
1822 Json::Value json = debugCanvas.toJSON(
1823 dataManager, debugCanvas.getSize(), nullCanvas.get());
1824 std::string value = Json::StyledWriter().write(json);
1825 return dst->write(value.c_str(), value.size()) ? "" : "SkWStream Error";
1826}
1827
1828/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1829
mtklein8a4527e2015-01-31 20:00:58 -08001830SVGSink::SVGSink() {}
1831
mtkleinb9eb4ac2015-02-02 18:26:03 -08001832Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita718df0a2016-07-15 10:33:29 -07001833#if defined(SK_XML)
Ben Wagner145dbcd2016-11-03 14:40:50 -04001834 std::unique_ptr<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
Mike Reed5df49342016-11-12 08:06:55 -06001835 return src.draw(SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
1836 SkIntToScalar(src.size().height())),
1837 xmlWriter.get()).get());
fmalita718df0a2016-07-15 10:33:29 -07001838#else
1839 return Error("SVG sink is disabled.");
1840#endif // SK_XML
mtklein8a4527e2015-01-31 20:00:58 -08001841}
1842
1843/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1844
brianosmanb109b8c2016-06-16 13:03:24 -07001845RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
mtklein27c3fdd2016-02-26 14:43:21 -08001846 : fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001847 , fColorSpace(std::move(colorSpace)) {}
mtklein748ca3b2015-01-15 10:56:12 -08001848
mtkleinb9eb4ac2015-02-02 18:26:03 -08001849Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001850 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001851 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1852 SkAlphaType alphaType = kPremul_SkAlphaType;
1853 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1854
Mike Reed086a4272017-07-18 10:53:11 -04001855 dst->allocPixelsFlags(SkImageInfo::Make(size.width(), size.height(),
1856 fColorType, alphaType, fColorSpace),
1857 SkBitmap::kZeroPixels_AllocFlag);
mtklein748ca3b2015-01-15 10:56:12 -08001858 SkCanvas canvas(*dst);
1859 return src.draw(&canvas);
1860}
1861
1862/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1863
mtkleina16e69e2015-05-05 11:38:45 -07001864// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001865// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001866// Several examples below.
1867
mtkleincbf89782016-02-19 14:27:14 -08001868template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001869static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001870 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001871 class ProxySrc : public Src {
1872 public:
mtkleincbf89782016-02-19 14:27:14 -08001873 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001874 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001875 Name name() const override { return "ProxySrc"; }
1876 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001877 private:
mtkleincbf89782016-02-19 14:27:14 -08001878 SkISize fSize;
1879 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001880 };
msarett62d3b102015-12-10 15:14:27 -08001881 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001882}
1883
1884/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1885
mtklein4a34ecb2016-01-08 10:19:35 -08001886DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1887
1888// Is *bitmap identical to what you get drawing src into sink?
1889static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1890 // We can only check raster outputs.
1891 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1892 if (FLAGS_check && bitmap) {
1893 SkBitmap reference;
1894 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001895 SkDynamicMemoryWStream wStream;
1896 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001897 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1898 SkASSERT(err.isEmpty());
1899 if (!err.isEmpty()) {
1900 return err;
1901 }
Brian Osmane5756ec2017-09-06 17:08:30 -04001902 return compare_bitmaps(reference, *bitmap);
mtklein4a34ecb2016-01-08 10:19:35 -08001903 }
1904 return "";
1905}
1906
1907/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1908
mtkleind603b222015-02-17 11:13:33 -08001909static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1910 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1911 matrix->mapRect(&bounds);
1912 matrix->postTranslate(-bounds.x(), -bounds.y());
Hal Canaryfafe1352017-04-11 12:12:02 -04001913 return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())};
mtkleind603b222015-02-17 11:13:33 -08001914}
1915
msarett62d3b102015-12-10 15:14:27 -08001916ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001917
mtkleinb9eb4ac2015-02-02 18:26:03 -08001918Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001919 SkMatrix matrix = fMatrix;
1920 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
Ben Wagner145dbcd2016-11-03 14:40:50 -04001921 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001922 canvas->concat(matrix);
1923 return src.draw(canvas);
1924 });
mtklein748ca3b2015-01-15 10:56:12 -08001925}
1926
mtkleind603b222015-02-17 11:13:33 -08001927// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1928// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001929ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001930
1931Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1932 Error err = fSink->draw(src, bitmap, stream, log);
1933 if (!err.isEmpty()) {
1934 return err;
1935 }
1936
1937 SkMatrix inverse;
1938 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1939 return "Cannot upright --matrix.";
1940 }
1941 SkMatrix upright = SkMatrix::I();
1942 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1943 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1944 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1945 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1946
1947 SkBitmap uprighted;
1948 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1949 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1950
1951 SkCanvas canvas(uprighted);
1952 canvas.concat(upright);
1953 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07001954 paint.setBlendMode(SkBlendMode::kSrc);
mtkleind603b222015-02-17 11:13:33 -08001955 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1956
1957 *bitmap = uprighted;
mtkleind603b222015-02-17 11:13:33 -08001958 return "";
1959}
1960
mtklein748ca3b2015-01-15 10:56:12 -08001961/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1962
mtkleina16e69e2015-05-05 11:38:45 -07001963Error ViaSerialization::draw(
1964 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001965 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001966 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001967 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001968 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1969 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001970 if (!err.isEmpty()) {
1971 return err;
1972 }
reedca2622b2016-03-18 07:25:55 -07001973 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001974
1975 // Serialize it and then deserialize it.
reed39eaf5f2016-09-15 07:19:35 -07001976 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
mtklein748ca3b2015-01-15 10:56:12 -08001977
Ben Wagner145dbcd2016-11-03 14:40:50 -04001978 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001979 canvas->drawPicture(deserialized);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001980 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07001981 });
mtklein748ca3b2015-01-15 10:56:12 -08001982}
1983
1984/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1985
msarett62d3b102015-12-10 15:14:27 -08001986ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1987 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001988 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001989 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001990 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001991
mtkleinb9eb4ac2015-02-02 18:26:03 -08001992Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001993 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001994 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001995 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1996 SkIntToScalar(size.height()),
1997 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001998 if (!err.isEmpty()) {
1999 return err;
2000 }
reedca2622b2016-03-18 07:25:55 -07002001 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08002002
Ben Wagner145dbcd2016-11-03 14:40:50 -04002003 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07002004 const int xTiles = (size.width() + fW - 1) / fW,
2005 yTiles = (size.height() + fH - 1) / fH;
2006 SkMultiPictureDraw mpd(xTiles*yTiles);
reede8f30622016-03-23 18:59:25 -07002007 SkTArray<sk_sp<SkSurface>> surfaces;
2008// surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08002009
mtkleina16e69e2015-05-05 11:38:45 -07002010 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
2011 for (int j = 0; j < yTiles; j++) {
2012 for (int i = 0; i < xTiles; i++) {
2013 // This lets our ultimate Sink determine the best kind of surface.
2014 // E.g., if it's a GpuSink, the surfaces and images are textures.
reede8f30622016-03-23 18:59:25 -07002015 auto s = canvas->makeSurface(info);
mtkleina16e69e2015-05-05 11:38:45 -07002016 if (!s) {
reede8f30622016-03-23 18:59:25 -07002017 s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08002018 }
reede8f30622016-03-23 18:59:25 -07002019 surfaces.push_back(s);
mtkleina16e69e2015-05-05 11:38:45 -07002020 SkCanvas* c = s->getCanvas();
2021 c->translate(SkIntToScalar(-i * fW),
2022 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
reedca2622b2016-03-18 07:25:55 -07002023 mpd.add(c, pic.get());
mtklein748ca3b2015-01-15 10:56:12 -08002024 }
mtklein748ca3b2015-01-15 10:56:12 -08002025 }
mtkleina16e69e2015-05-05 11:38:45 -07002026 mpd.draw();
2027 for (int j = 0; j < yTiles; j++) {
2028 for (int i = 0; i < xTiles; i++) {
reed9ce9d672016-03-17 10:51:11 -07002029 sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot());
mtkleina16e69e2015-05-05 11:38:45 -07002030 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
2031 }
2032 }
mtkleina16e69e2015-05-05 11:38:45 -07002033 return "";
2034 });
mtklein748ca3b2015-01-15 10:56:12 -08002035}
2036
mtkleinb7e8d692015-04-07 08:30:32 -07002037/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2038
mtklein4a34ecb2016-01-08 10:19:35 -08002039Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2040 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04002041 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtklein4a34ecb2016-01-08 10:19:35 -08002042 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07002043 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08002044 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
2045 SkIntToScalar(size.height())));
2046 if (!err.isEmpty()) {
2047 return err;
2048 }
reedca2622b2016-03-18 07:25:55 -07002049 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08002050 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002051 return check_against_reference(bitmap, src, fSink.get());
mtklein4a34ecb2016-01-08 10:19:35 -08002052 });
2053}
2054
2055/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2056
reed54dc4872016-09-13 08:09:45 -07002057Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2058 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04002059 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
reed54dc4872016-09-13 08:09:45 -07002060 SkDynamicMemoryWStream tmpStream;
2061 Error err = src.draw(SkPipeSerializer().beginWrite(SkRect::Make(size), &tmpStream));
2062 if (!err.isEmpty()) {
2063 return err;
2064 }
2065 sk_sp<SkData> data = tmpStream.detachAsData();
2066 SkPipeDeserializer().playback(data->data(), data->size(), canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002067 return check_against_reference(bitmap, src, fSink.get());
reed54dc4872016-09-13 08:09:45 -07002068 });
2069}
2070
2071/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2072
mtkleinb7e8d692015-04-07 08:30:32 -07002073// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
2074// This tests that any shortcuts we may take while recording that second picture are legal.
2075Error ViaSecondPicture::draw(
2076 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07002077 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04002078 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07002079 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07002080 sk_sp<SkPicture> pic;
mtkleina16e69e2015-05-05 11:38:45 -07002081 for (int i = 0; i < 2; i++) {
2082 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
2083 SkIntToScalar(size.height())));
2084 if (!err.isEmpty()) {
2085 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07002086 }
reedca2622b2016-03-18 07:25:55 -07002087 pic = recorder.finishRecordingAsPicture();
mtkleinb7e8d692015-04-07 08:30:32 -07002088 }
mtkleina16e69e2015-05-05 11:38:45 -07002089 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002090 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07002091 });
mtkleinb7e8d692015-04-07 08:30:32 -07002092}
2093
mtkleind31c13d2015-05-05 12:59:56 -07002094/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2095
mtklein6fbf4b32015-05-06 11:35:40 -07002096// Draw the Src twice. This can help exercise caching.
2097Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -04002098 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07002099 for (int i = 0; i < 2; i++) {
2100 SkAutoCanvasRestore acr(canvas, true/*save now*/);
2101 canvas->clear(SK_ColorTRANSPARENT);
2102 Error err = src.draw(canvas);
2103 if (err.isEmpty()) {
2104 return err;
2105 }
2106 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04002107 return check_against_reference(bitmap, src, fSink.get());
mtklein6fbf4b32015-05-06 11:35:40 -07002108 });
2109}
2110
2111/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2112
Mike Reedbae888e2017-02-18 16:50:45 -05002113#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -05002114#include "SkXMLWriter.h"
2115#include "SkSVGCanvas.h"
2116#include "SkSVGDOM.h"
2117
2118Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2119 auto size = src.size();
2120 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
2121 SkDynamicMemoryWStream wstream;
2122 SkXMLStreamWriter writer(&wstream);
2123 Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get());
2124 if (!err.isEmpty()) {
2125 return err;
2126 }
2127 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
2128 auto dom = SkSVGDOM::MakeFromStream(*rstream);
2129 if (dom) {
2130 dom->setContainerSize(SkSize::Make(size));
2131 dom->render(canvas);
2132 }
2133 return "";
2134 });
2135}
Mike Reedbae888e2017-02-18 16:50:45 -05002136#endif
Mike Reedf67c4592017-02-17 17:06:11 -05002137
2138/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2139
mtkleind31c13d2015-05-05 12:59:56 -07002140// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
2141// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
2142// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
2143struct DrawsAsSingletonPictures {
2144 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07002145 const SkDrawableList& fDrawables;
mtklein11064df2016-04-06 15:01:57 -07002146 SkRect fBounds;
mtkleind31c13d2015-05-05 12:59:56 -07002147
mtkleind31c13d2015-05-05 12:59:56 -07002148 template <typename T>
2149 void draw(const T& op, SkCanvas* canvas) {
2150 // We must pass SkMatrix::I() as our initial matrix.
2151 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
2152 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07002153 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
2154 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07002155 }
2156
mtklein449d9b72015-09-28 10:33:02 -07002157 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07002158 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07002159 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07002160 SkPictureRecorder rec;
mtklein11064df2016-04-06 15:01:57 -07002161 this->draw(op, rec.beginRecording(fBounds));
reedca2622b2016-03-18 07:25:55 -07002162 sk_sp<SkPicture> pic(rec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07002163 fCanvas->drawPicture(pic);
2164 }
2165
mtklein449d9b72015-09-28 10:33:02 -07002166 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07002167 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07002168 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
2169 this->draw(op, fCanvas);
2170 }
mtkleind31c13d2015-05-05 12:59:56 -07002171};
2172
mtkleind31c13d2015-05-05 12:59:56 -07002173// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
2174// Then play back that macro picture into our wrapped sink.
2175Error ViaSingletonPictures::draw(
2176 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2177 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04002178 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07002179 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
2180 SkRecord skr;
2181 SkRecorder recorder(&skr, size.width(), size.height());
2182 Error err = src.draw(&recorder);
2183 if (!err.isEmpty()) {
2184 return err;
2185 }
2186
2187 // Record our macro-picture, with each draw op as its own sub-picture.
2188 SkPictureRecorder macroRec;
2189 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
2190 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07002191
bungeman6bd52842016-10-27 09:30:08 -07002192 std::unique_ptr<SkDrawableList> drawables(recorder.detachDrawableList());
mtkleind2baa902015-07-07 09:43:28 -07002193 const SkDrawableList empty;
2194
2195 DrawsAsSingletonPictures drawsAsSingletonPictures = {
2196 macroCanvas,
2197 drawables ? *drawables : empty,
mtklein11064df2016-04-06 15:01:57 -07002198 SkRect::MakeWH((SkScalar)size.width(), (SkScalar)size.height()),
mtkleind2baa902015-07-07 09:43:28 -07002199 };
mtkleinc6ad06a2015-08-19 09:51:00 -07002200 for (int i = 0; i < skr.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -07002201 skr.visit(i, drawsAsSingletonPictures);
mtkleind31c13d2015-05-05 12:59:56 -07002202 }
reedca2622b2016-03-18 07:25:55 -07002203 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07002204
2205 canvas->drawPicture(macroPic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002206 return check_against_reference(bitmap, src, fSink.get());
mtkleind31c13d2015-05-05 12:59:56 -07002207 });
2208}
2209
mtklein9c5052f2016-08-06 12:51:51 -07002210/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2211
2212Error ViaLite::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2213 auto size = src.size();
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002214 SkIRect bounds = {0,0, size.width(), size.height()};
Ben Wagner145dbcd2016-11-03 14:40:50 -04002215 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002216 SkLiteDL dl;
mtklein8bbbb692016-08-15 12:56:00 -07002217 SkLiteRecorder rec;
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002218 rec.reset(&dl, bounds);
mtklein8bbbb692016-08-15 12:56:00 -07002219
2220 Error err = src.draw(&rec);
mtklein9c5052f2016-08-06 12:51:51 -07002221 if (!err.isEmpty()) {
2222 return err;
2223 }
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002224 dl.draw(canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002225 return check_against_reference(bitmap, src, fSink.get());
mtklein9c5052f2016-08-06 12:51:51 -07002226 });
2227}
2228
Mike Klein841101d2017-03-10 09:55:51 -05002229/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2230
Mike Klein919cc452017-03-18 15:36:52 +00002231ViaCSXform::ViaCSXform(Sink* sink, sk_sp<SkColorSpace> cs, bool colorSpin)
2232 : Via(sink)
2233 , fCS(std::move(cs))
2234 , fColorSpin(colorSpin) {}
Mike Klein841101d2017-03-10 09:55:51 -05002235
2236Error ViaCSXform::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2237 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(),
2238 [&](SkCanvas* canvas) -> Error {
Brian Osman5b7e4702017-10-31 16:41:29 -04002239 {
2240 SkAutoCanvasRestore acr(canvas, true);
2241 auto proxy = SkCreateColorSpaceXformCanvas(canvas, fCS);
2242 Error err = src.draw(proxy.get());
2243 if (!err.isEmpty()) {
2244 return err;
2245 }
Mike Klein919cc452017-03-18 15:36:52 +00002246 }
2247
2248 // Undo the color spin, so we can look at the pixels in Gold.
2249 if (fColorSpin) {
2250 SkBitmap pixels;
2251 pixels.allocPixels(canvas->imageInfo());
Mike Reed12e946b2017-04-17 10:53:29 -04002252 canvas->readPixels(pixels, 0, 0);
Mike Klein919cc452017-03-18 15:36:52 +00002253
Brian Osman5b7e4702017-10-31 16:41:29 -04002254 SkPaint rotateColors;
2255 SkScalar matrix[20] = { 0, 0, 1, 0, 0, // B -> R
2256 1, 0, 0, 0, 0, // R -> G
2257 0, 1, 0, 0, 0, // G -> B
2258 0, 0, 0, 1, 0 };
2259 rotateColors.setBlendMode(SkBlendMode::kSrc);
2260 rotateColors.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
2261 canvas->drawBitmap(pixels, 0, 0, &rotateColors);
Mike Klein919cc452017-03-18 15:36:52 +00002262 }
2263
2264 return "";
Mike Klein841101d2017-03-10 09:55:51 -05002265 });
2266}
2267
mtklein748ca3b2015-01-15 10:56:12 -08002268} // namespace DM