blob: dc8a913fa1f9ee660063f0cbb479788e48a1beeb [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMSrcSink.h"
msarett9876ac52016-06-01 14:47:18 -07009#include "Resources.h"
msarett3d9d7a72015-10-21 10:27:10 -070010#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "SkAutoMalloc.h"
scroggof24f2242015-03-03 08:59:20 -080012#include "SkCodec.h"
msarettb714fb02016-01-22 14:46:42 -080013#include "SkCodecImageGenerator.h"
msarett888dc162016-05-23 10:21:17 -070014#include "SkColorSpace.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000015#include "SkColorSpaceXform.h"
Mike Klein841101d2017-03-10 09:55:51 -050016#include "SkColorSpaceXformCanvas.h"
Hal Canary95e3c052017-01-11 12:44:43 -050017#include "SkColorSpace_XYZ.h"
mtkleina16e69e2015-05-05 11:38:45 -070018#include "SkCommonFlags.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070019#include "SkData.h"
Hal Canary85c7fe82016-10-25 10:33:27 -040020#include "SkDebugCanvas.h"
reedbabc3de2016-07-08 08:43:27 -070021#include "SkDeferredCanvas.h"
Robert Phillipsad8a43f2017-08-30 12:06:35 -040022#include "SkDeferredDisplayListRecorder.h"
mtklein748ca3b2015-01-15 10:56:12 -080023#include "SkDocument.h"
Brian Osmanf9810662017-08-30 10:02:10 -040024#include "SkExecutor.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070025#include "SkImageGenerator.h"
msarett18976312016-03-09 14:20:58 -080026#include "SkImageGeneratorCG.h"
msarettfc0b6d12016-03-17 13:50:17 -070027#include "SkImageGeneratorWIC.h"
mtklein8bbbb692016-08-15 12:56:00 -070028#include "SkLiteDL.h"
29#include "SkLiteRecorder.h"
mtkleinc8be09a2016-01-04 18:56:57 -080030#include "SkMallocPixelRef.h"
Hal Canary45cde312017-04-03 16:06:42 -040031#include "SkMultiPictureDocumentPriv.h"
mtklein748ca3b2015-01-15 10:56:12 -080032#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080033#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080034#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050035#include "SkOSPath.h"
msarett9e9444c2016-02-03 12:39:10 -080036#include "SkOpts.h"
mtkleinffa901a2015-03-16 10:38:07 -070037#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080038#include "SkPictureRecorder.h"
reed54dc4872016-09-13 08:09:45 -070039#include "SkPipe.h"
mtklein748ca3b2015-01-15 10:56:12 -080040#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070041#include "SkRecordDraw.h"
42#include "SkRecorder.h"
Robert Phillipsad8a43f2017-08-30 12:06:35 -040043#include "SkSurfaceCharacterization.h"
fmalita2aafe6f2015-02-06 12:51:10 -080044#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080045#include "SkStream.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000046#include "SkSwizzler.h"
Robert Phillipsad8a43f2017-08-30 12:06:35 -040047#include "SkTaskGroup.h"
Hal Canary95e3c052017-01-11 12:44:43 -050048#include "SkTLogic.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000049#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050050#include <functional>
Mike Klein45c16fa2017-07-18 18:15:13 -040051#include "../src/jumper/SkJumper.h"
mtklein748ca3b2015-01-15 10:56:12 -080052
msarettfc0b6d12016-03-17 13:50:17 -070053#if defined(SK_BUILD_FOR_WIN)
54 #include "SkAutoCoInitialize.h"
Hal Canary5e221e72017-02-06 09:51:42 -050055 #include "SkHRESULT.h"
56 #include "SkTScopedComPtr.h"
57 #include <XpsObjectModel.h>
msarettfc0b6d12016-03-17 13:50:17 -070058#endif
59
fmalita718df0a2016-07-15 10:33:29 -070060#if defined(SK_XML)
fmalitaa48f0e32016-08-04 06:26:05 -070061 #include "SkSVGDOM.h"
fmalita718df0a2016-07-15 10:33:29 -070062 #include "SkXMLWriter.h"
63#endif
64
halcanary7e798182015-04-14 14:06:18 -070065DEFINE_bool(multiPage, false, "For document-type backends, render the source"
66 " into multiple pages");
scroggo3ac66e92016-02-08 15:09:48 -080067DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
Brian Osmanf9810662017-08-30 10:02:10 -040068DECLARE_int32(gpuThreads);
halcanary7e798182015-04-14 14:06:18 -070069
bsalomon3724e572016-03-30 18:56:19 -070070using sk_gpu_test::GrContextFactory;
71
mtklein748ca3b2015-01-15 10:56:12 -080072namespace DM {
73
mtklein748ca3b2015-01-15 10:56:12 -080074GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
75
76Error GMSrc::draw(SkCanvas* canvas) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040077 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080078 gm->draw(canvas);
79 return "";
80}
81
82SkISize GMSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040083 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080084 return gm->getISize();
85}
86
87Name GMSrc::name() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040088 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080089 return gm->getName();
90}
91
bsalomon4ee6bd82015-05-27 13:23:23 -070092void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -040093 std::unique_ptr<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070094 gm->modifyGrContextOptions(options);
95}
96
mtklein748ca3b2015-01-15 10:56:12 -080097/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
98
msarettd1227a72016-05-18 06:23:57 -070099BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
msaretta5783ae2015-09-08 15:35:32 -0700100 : fPath(path)
msaretta5783ae2015-09-08 15:35:32 -0700101 , fMode(mode)
102 , fDstColorType(dstColorType)
103 , fSampleSize(sampleSize)
104{}
105
106bool BRDSrc::veto(SinkFlags flags) const {
107 // No need to test to non-raster or indirect backends.
108 return flags.type != SinkFlags::kRaster
109 || flags.approach != SinkFlags::kDirect;
110}
111
msarettd1227a72016-05-18 06:23:57 -0700112static SkBitmapRegionDecoder* create_brd(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700113 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msaretta5783ae2015-09-08 15:35:32 -0700114 if (!encoded) {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400115 return nullptr;
msaretta5783ae2015-09-08 15:35:32 -0700116 }
reed42943c82016-09-12 12:01:44 -0700117 return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy);
msaretta5783ae2015-09-08 15:35:32 -0700118}
119
Matt Sarett334df3a2016-12-15 18:17:33 -0500120static inline void alpha8_to_gray8(SkBitmap* bitmap) {
121 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
122 // them back to kGray8 so our test framework can draw them correctly.
123 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
124 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
125 .makeAlphaType(kOpaque_SkAlphaType);
126 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
127 }
128}
129
msaretta5783ae2015-09-08 15:35:32 -0700130Error BRDSrc::draw(SkCanvas* canvas) const {
Matt Saretta40d9c82017-05-19 15:21:05 -0400131 if (canvas->imageInfo().colorSpace() &&
132 kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) {
133 // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only
134 // run these tests on legacy canvases.
135 // We allow an exception for F16, since Android uses F16.
136 return Error::Nonfatal("Skip testing to color correct canvas.");
137 }
138
msaretta5783ae2015-09-08 15:35:32 -0700139 SkColorType colorType = canvas->imageInfo().colorType();
140 if (kRGB_565_SkColorType == colorType &&
141 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
142 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
143 }
144 switch (fDstColorType) {
145 case CodecSrc::kGetFromCanvas_DstColorType:
146 break;
msaretta5783ae2015-09-08 15:35:32 -0700147 case CodecSrc::kGrayscale_Always_DstColorType:
148 colorType = kGray_8_SkColorType;
149 break;
msarett34e0ec42016-04-22 16:27:24 -0700150 default:
151 SkASSERT(false);
152 break;
msaretta5783ae2015-09-08 15:35:32 -0700153 }
154
Ben Wagner145dbcd2016-11-03 14:40:50 -0400155 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700156 if (nullptr == brd.get()) {
157 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
158 }
159
Leon Scroggins III1dc8ecb2017-08-17 13:42:48 -0400160 if (kRGB_565_SkColorType == colorType) {
161 auto recommendedCT = brd->computeOutputColorType(colorType);
162 if (recommendedCT != colorType) {
163 return Error::Nonfatal("Skip decoding non-opaque to 565.");
164 }
165 }
166
msaretta5783ae2015-09-08 15:35:32 -0700167 const uint32_t width = brd->width();
168 const uint32_t height = brd->height();
169 // Visually inspecting very small output images is not necessary.
170 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
171 return Error::Nonfatal("Scaling very small images is uninteresting.");
172 }
173 switch (fMode) {
174 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700175 SkBitmap bitmap;
176 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
Matt Sarett68feef42017-04-11 09:51:32 -0400177 fSampleSize, colorType, false, SkColorSpace::MakeSRGB())) {
mtklein9b439152015-12-09 13:02:26 -0800178 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700179 }
Matt Sarett334df3a2016-12-15 18:17:33 -0500180 alpha8_to_gray8(&bitmap);
Matt Sarett8db74f12017-06-14 13:02:05 +0000181
msarett35e5d1b2015-10-27 12:50:25 -0700182 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700183 return "";
184 }
185 case kDivisor_Mode: {
186 const uint32_t divisor = 2;
187 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800188 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700189 }
190
191 // Use a border to test subsets that extend outside the image.
192 // We will not allow the border to be larger than the image dimensions. Allowing
193 // these large borders causes off by one errors that indicate a problem with the
194 // test suite, not a problem with the implementation.
195 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
196 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
197 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
198
199 // We may need to clear the canvas to avoid uninitialized memory.
200 // Assume we are scaling a 780x780 image with sampleSize = 8.
201 // The output image should be 97x97.
202 // Each subset will be 390x390.
203 // Each scaled subset be 48x48.
204 // Four scaled subsets will only fill a 96x96 image.
205 // The bottom row and last column will not be touched.
206 // This is an unfortunate result of our rounding rules when scaling.
207 // Maybe we need to consider testing scaled subsets without trying to
208 // combine them to match the full scaled image? Or maybe this is the
209 // best we can do?
210 canvas->clear(0);
211
212 for (uint32_t x = 0; x < divisor; x++) {
213 for (uint32_t y = 0; y < divisor; y++) {
214 // Calculate the subset dimensions
215 uint32_t subsetWidth = width / divisor;
216 uint32_t subsetHeight = height / divisor;
217 const int left = x * subsetWidth;
218 const int top = y * subsetHeight;
219
220 // Increase the size of the last subset in each row or column, when the
221 // divisor does not divide evenly into the image dimensions
222 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
223 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
224
225 // Increase the size of the subset in order to have a border on each side
226 const int decodeLeft = left - unscaledBorder;
227 const int decodeTop = top - unscaledBorder;
228 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
229 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700230 SkBitmap bitmap;
231 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
Matt Sarett68feef42017-04-11 09:51:32 -0400232 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false,
233 SkColorSpace::MakeSRGB())) {
mtklein9b439152015-12-09 13:02:26 -0800234 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700235 }
msaretta5783ae2015-09-08 15:35:32 -0700236
Matt Sarett334df3a2016-12-15 18:17:33 -0500237 alpha8_to_gray8(&bitmap);
msarett35e5d1b2015-10-27 12:50:25 -0700238 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700239 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
240 (SkScalar) (subsetWidth / fSampleSize),
241 (SkScalar) (subsetHeight / fSampleSize)),
242 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
243 (SkScalar) (top / fSampleSize),
244 (SkScalar) (subsetWidth / fSampleSize),
245 (SkScalar) (subsetHeight / fSampleSize)),
246 nullptr);
247 }
248 }
249 return "";
250 }
251 default:
252 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800253 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700254 }
255}
256
257SkISize BRDSrc::size() const {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400258 std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath));
msaretta5783ae2015-09-08 15:35:32 -0700259 if (brd) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400260 return {SkTMax(1, brd->width() / (int)fSampleSize),
261 SkTMax(1, brd->height() / (int)fSampleSize)};
msaretta5783ae2015-09-08 15:35:32 -0700262 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400263 return {0, 0};
msaretta5783ae2015-09-08 15:35:32 -0700264}
265
266static SkString get_scaled_name(const Path& path, float scale) {
267 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
268}
269
270Name BRDSrc::name() const {
271 // We will replicate the names used by CodecSrc so that images can
272 // be compared in Gold.
273 if (1 == fSampleSize) {
274 return SkOSPath::Basename(fPath.c_str());
275 }
msarett4b0778e2015-11-13 09:59:11 -0800276 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700277}
278
279/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
280
scroggo3ac66e92016-02-08 15:09:48 -0800281static bool serial_from_path_name(const SkString& path) {
282 if (!FLAGS_RAW_threading) {
283 static const char* const exts[] = {
284 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
285 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
286 };
287 const char* actualExt = strrchr(path.c_str(), '.');
288 if (actualExt) {
289 actualExt++;
290 for (auto* ext : exts) {
291 if (0 == strcmp(ext, actualExt)) {
292 return true;
293 }
294 }
295 }
296 }
297 return false;
298}
299
scroggoc5560be2016-02-03 09:42:42 -0800300CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
301 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700302 : fPath(path)
303 , fMode(mode)
304 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800305 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700306 , fScale(scale)
scroggo3ac66e92016-02-08 15:09:48 -0800307 , fRunSerially(serial_from_path_name(path))
msarett438b2ad2015-04-09 12:43:10 -0700308{}
mtklein748ca3b2015-01-15 10:56:12 -0800309
mtklein99cab4e2015-07-31 06:43:04 -0700310bool CodecSrc::veto(SinkFlags flags) const {
msarett18976312016-03-09 14:20:58 -0800311 // Test to direct raster backends (8888 and 565).
msarettb714fb02016-01-22 14:46:42 -0800312 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700313}
scroggo9b77ddd2015-03-19 06:03:39 -0700314
msarett34e0ec42016-04-22 16:27:24 -0700315// Allows us to test decodes to non-native 8888.
msarettb1be46b2016-05-17 08:52:11 -0700316static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700317 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
318 return;
319 }
320
321 for (int y = 0; y < bitmap.height(); y++) {
322 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
323 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
324 }
325}
326
msarett9e9444c2016-02-03 12:39:10 -0800327// FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339.
328// This allows us to still test unpremultiplied decodes.
msarettb1be46b2016-05-17 08:52:11 -0700329static void premultiply_if_necessary(SkBitmap& bitmap) {
msarett9e9444c2016-02-03 12:39:10 -0800330 if (kUnpremul_SkAlphaType != bitmap.alphaType()) {
331 return;
332 }
333
334 switch (bitmap.colorType()) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400335 case kRGBA_F16_SkColorType: {
336 SkJumper_MemoryCtx ctx = { bitmap.getAddr(0,0), bitmap.rowBytesAsPixels() };
337 SkRasterPipeline_<256> p;
338 p.append(SkRasterPipeline::load_f16, &ctx);
339 p.append(SkRasterPipeline::premul);
340 p.append(SkRasterPipeline::store_f16, &ctx);
341 p.run(0,0, bitmap.width(), bitmap.height());
342 }
Leon Scroggins IIIeceee042017-05-23 16:58:09 -0400343 break;
msarett9e9444c2016-02-03 12:39:10 -0800344 case kN32_SkColorType:
345 for (int y = 0; y < bitmap.height(); y++) {
346 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
347 SkOpts::RGBA_to_rgbA(row, row, bitmap.width());
348 }
349 break;
msarett9e9444c2016-02-03 12:39:10 -0800350 default:
351 // No need to premultiply kGray or k565 outputs.
352 break;
353 }
msarette1daa482016-02-03 15:31:18 -0800354
355 // In the kIndex_8 case, the canvas won't even try to draw unless we mark the
356 // bitmap as kPremul.
357 bitmap.setAlphaType(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800358}
359
msarettb1be46b2016-05-17 08:52:11 -0700360static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
scroggoba584892016-05-20 13:56:13 -0700361 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
msarett3d9d7a72015-10-21 10:27:10 -0700362 switch (dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700363 case CodecSrc::kGrayscale_Always_DstColorType:
scroggo1a361922016-05-20 14:27:16 -0700364 if (kRGB_565_SkColorType == canvasColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700365 return false;
366 }
scroggoc5560be2016-02-03 09:42:42 -0800367 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700368 break;
msarett34e0ec42016-04-22 16:27:24 -0700369 case CodecSrc::kNonNative8888_Always_DstColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400370 if (kRGB_565_SkColorType == canvasColorType
371 || kRGBA_F16_SkColorType == canvasColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700372 return false;
373 }
374#ifdef SK_PMCOLOR_IS_RGBA
375 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
376#else
377 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
378#endif
379 break;
msarett3d9d7a72015-10-21 10:27:10 -0700380 default:
msarett55f7bdd2016-02-16 13:24:54 -0800381 if (kRGB_565_SkColorType == canvasColorType &&
382 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
383 return false;
384 }
Matt Sarett09a1c082017-02-01 15:34:22 -0800385
386 if (kRGBA_F16_SkColorType == canvasColorType) {
387 sk_sp<SkColorSpace> linearSpace =
388 as_CSB(decodeInfo->colorSpace())->makeLinearGamma();
389 *decodeInfo = decodeInfo->makeColorSpace(std::move(linearSpace));
390 }
391
scroggoc5560be2016-02-03 09:42:42 -0800392 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700393 break;
394 }
395
scroggoba584892016-05-20 13:56:13 -0700396 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700397 return true;
398}
399
msarettb1be46b2016-05-17 08:52:11 -0700400static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000401 CodecSrc::DstColorType dstColorType,
msarettb1be46b2016-05-17 08:52:11 -0700402 SkScalar left = 0, SkScalar top = 0) {
msarettb1be46b2016-05-17 08:52:11 -0700403 SkBitmap bitmap;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000404 bitmap.installPixels(info, pixels, rowBytes);
msarettb1be46b2016-05-17 08:52:11 -0700405 premultiply_if_necessary(bitmap);
406 swap_rb_if_necessary(bitmap, dstColorType);
407 canvas->drawBitmap(bitmap, left, top);
408}
409
Matt Sarettf485cf92017-05-26 08:58:03 -0400410// For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or
411// color format conversions should be performed by the codec. Sometimes the output of the
412// decode will be in an interesting color space. On our srgb and f16 backends, we need to
413// "pretend" that the color space is standard sRGB to avoid triggering color conversion
414// at draw time.
415static void set_bitmap_color_space(SkImageInfo* info) {
416 if (kRGBA_F16_SkColorType == info->colorType()) {
417 *info = info->makeColorSpace(SkColorSpace::MakeSRGBLinear());
418 } else {
419 *info = info->makeColorSpace(SkColorSpace::MakeSRGB());
420 }
421}
422
mtkleine0effd62015-07-29 06:37:28 -0700423Error CodecSrc::draw(SkCanvas* canvas) const {
bungeman38d909e2016-08-02 14:40:46 -0700424 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800425 if (!encoded) {
426 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
427 }
msarettb714fb02016-01-22 14:46:42 -0800428
Mike Reedede7bac2017-07-23 15:30:02 -0400429 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700430 if (nullptr == codec.get()) {
431 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700432 }
433
scroggoba584892016-05-20 13:56:13 -0700434 SkImageInfo decodeInfo = codec->getInfo();
435 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
436 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800437 return Error::Nonfatal("Skipping uninteresting test.");
msarett438b2ad2015-04-09 12:43:10 -0700438 }
439
msarett0a242972015-06-11 14:27:27 -0700440 // Try to scale the image if it is desired
441 SkISize size = codec->getScaledDimensions(fScale);
442 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
443 return Error::Nonfatal("Test without scaling is uninteresting.");
444 }
msarettb32758a2015-08-18 13:22:46 -0700445
446 // Visually inspecting very small output images is not necessary. We will
447 // cover these cases in unit testing.
448 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
449 return Error::Nonfatal("Scaling very small images is uninteresting.");
450 }
msarett0a242972015-06-11 14:27:27 -0700451 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
452
msarettb1be46b2016-05-17 08:52:11 -0700453 const int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
454 const size_t rowBytes = size.width() * bpp;
scroggo19b91532016-10-24 09:03:26 -0700455 const size_t safeSize = decodeInfo.getSafeSize(rowBytes);
456 SkAutoMalloc pixels(safeSize);
msarett438b2ad2015-04-09 12:43:10 -0700457
msarettbb25b532016-01-13 09:31:39 -0800458 SkCodec::Options options;
Matt Saretta40d9c82017-05-19 15:21:05 -0400459 options.fPremulBehavior = canvas->imageInfo().colorSpace() ?
460 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
msarettbb25b532016-01-13 09:31:39 -0800461 if (kCodecZeroInit_Mode == fMode) {
msarettb1be46b2016-05-17 08:52:11 -0700462 memset(pixels.get(), 0, size.height() * rowBytes);
msarettbb25b532016-01-13 09:31:39 -0800463 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
464 }
msarett34e0ec42016-04-22 16:27:24 -0700465
466 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400467 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700468 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
469 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
470 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
471 }
scroggo9b77ddd2015-03-19 06:03:39 -0700472
scroggo9c59ebc2015-03-25 13:48:49 -0700473 switch (fMode) {
scroggo19b91532016-10-24 09:03:26 -0700474 case kAnimated_Mode: {
475 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
476 if (frameInfos.size() <= 1) {
477 return SkStringPrintf("%s is not an animated image.", fPath.c_str());
478 }
479
scroggo90e971a2016-10-25 06:11:01 -0700480 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
481 // into. "factor" is the number of frames to draw on one row. There will be
482 // up to "factor" rows as well.
483 const float root = sqrt((float) frameInfos.size());
484 const int factor = sk_float_ceil2int(root);
485
scroggo19b91532016-10-24 09:03:26 -0700486 // Used to cache a frame that future frames will depend on.
487 SkAutoMalloc priorFramePixels;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400488 int cachedFrame = SkCodec::kNone;
489 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
scroggo19b91532016-10-24 09:03:26 -0700490 options.fFrameIndex = i;
491 // Check for a prior frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400492 const int reqFrame = frameInfos[i].fRequiredFrame;
scroggo19b91532016-10-24 09:03:26 -0700493 if (reqFrame != SkCodec::kNone && reqFrame == cachedFrame
494 && priorFramePixels.get()) {
495 // Copy into pixels
496 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400497 options.fPriorFrame = reqFrame;
scroggo19b91532016-10-24 09:03:26 -0700498 } else {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400499 options.fPriorFrame = SkCodec::kNone;
scroggo19b91532016-10-24 09:03:26 -0700500 }
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500501 SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000502 rowBytes, &options);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500503 if (SkCodec::kInvalidInput == result && i > 0) {
504 // Some of our test images have truncated later frames. Treat that
505 // the same as incomplete.
506 result = SkCodec::kIncompleteInput;
507 }
scroggo19b91532016-10-24 09:03:26 -0700508 switch (result) {
509 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400510 case SkCodec::kErrorInInput:
scroggo90e971a2016-10-25 06:11:01 -0700511 case SkCodec::kIncompleteInput: {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400512 // If the next frame depends on this one, store it in priorFrame.
513 // It is possible that we may discard a frame that future frames depend on,
514 // but the codec will simply redecode the discarded frame.
515 // Do this before calling draw_to_canvas, which premultiplies in place. If
516 // we're decoding to unpremul, we want to pass the unmodified frame to the
517 // codec for decoding the next frame.
518 if (static_cast<size_t>(i+1) < frameInfos.size()
519 && frameInfos[i+1].fRequiredFrame == i) {
520 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
521 cachedFrame = i;
522 }
523
scroggo90e971a2016-10-25 06:11:01 -0700524 SkAutoCanvasRestore acr(canvas, true);
525 const int xTranslate = (i % factor) * decodeInfo.width();
526 const int yTranslate = (i / factor) * decodeInfo.height();
527 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
Leon Scroggins571b30f2017-07-11 17:35:31 +0000528 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
Leon Scroggins III674a1842017-07-06 12:26:09 -0400529 if (result != SkCodec::kSuccess) {
scroggo19b91532016-10-24 09:03:26 -0700530 return "";
531 }
532 break;
scroggo90e971a2016-10-25 06:11:01 -0700533 }
scroggo53f63b62016-10-27 08:29:13 -0700534 case SkCodec::kInvalidConversion:
Mike Reed304a07c2017-07-12 15:10:28 -0400535 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) {
scroggo53f63b62016-10-27 08:29:13 -0700536 return Error::Nonfatal(SkStringPrintf(
Mike Reed304a07c2017-07-12 15:10:28 -0400537 "Cannot decode frame %i to 565 (%s).", i, fPath.c_str()));
scroggo53f63b62016-10-27 08:29:13 -0700538 }
539 // Fall through.
scroggo19b91532016-10-24 09:03:26 -0700540 default:
541 return SkStringPrintf("Couldn't getPixels for frame %i in %s.",
542 i, fPath.c_str());
543 }
scroggo19b91532016-10-24 09:03:26 -0700544 }
545 break;
546 }
msarettbb25b532016-01-13 09:31:39 -0800547 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700548 case kCodec_Mode: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000549 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggoeb602a52015-07-09 08:16:03 -0700550 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400551 // We consider these to be valid, since we should still decode what is
scroggo9c59ebc2015-03-25 13:48:49 -0700552 // available.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400553 case SkCodec::kErrorInInput:
scroggoeb602a52015-07-09 08:16:03 -0700554 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700555 break;
scroggo9c59ebc2015-03-25 13:48:49 -0700556 default:
557 // Everything else is considered a failure.
558 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
559 }
msarettb1be46b2016-05-17 08:52:11 -0700560
Leon Scroggins571b30f2017-07-11 17:35:31 +0000561 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggo9c59ebc2015-03-25 13:48:49 -0700562 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700563 }
scroggo9c59ebc2015-03-25 13:48:49 -0700564 case kScanline_Mode: {
scroggod8d68552016-06-06 11:26:17 -0700565 void* dst = pixels.get();
566 uint32_t height = decodeInfo.height();
scroggo19b91532016-10-24 09:03:26 -0700567 const bool useIncremental = [this]() {
568 auto exts = { "png", "PNG", "gif", "GIF" };
569 for (auto ext : exts) {
570 if (fPath.endsWith(ext)) {
571 return true;
572 }
573 }
574 return false;
575 }();
576 // ico may use the old scanline method or the new one, depending on whether it
577 // internally holds a bmp or a png.
scroggo8e6c7ad2016-09-16 08:20:38 -0700578 const bool ico = fPath.endsWith("ico");
scroggo19b91532016-10-24 09:03:26 -0700579 bool useOldScanlineMethod = !useIncremental && !ico;
580 if (useIncremental || ico) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700581 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000582 rowBytes, &options)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700583 int rowsDecoded;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400584 auto result = codec->incrementalDecode(&rowsDecoded);
585 if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700586 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
587 SkCodec::kNo_ZeroInitialized, height,
588 rowsDecoded);
scroggo6fb23912016-06-02 14:16:43 -0700589 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700590 } else {
scroggo19b91532016-10-24 09:03:26 -0700591 if (useIncremental) {
592 // Error: These should support incremental decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700593 return "Could not start incremental decode";
594 }
595 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
596 // which should work via startScanlineDecode
597 useOldScanlineMethod = true;
598 }
599 }
600
601 if (useOldScanlineMethod) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000602 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700603 return "Could not start scanline decoder";
604 }
605
606 switch (codec->getScanlineOrder()) {
607 case SkCodec::kTopDown_SkScanlineOrder:
608 case SkCodec::kBottomUp_SkScanlineOrder:
609 // We do not need to check the return value. On an incomplete
610 // image, memory will be filled with a default value.
611 codec->getScanlines(dst, height, rowBytes);
612 break;
msarett10522ff2015-09-07 08:54:01 -0700613 }
614 }
615
Leon Scroggins571b30f2017-07-11 17:35:31 +0000616 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer97002062015-05-27 12:36:10 -0700617 break;
618 }
msarett0a242972015-06-11 14:27:27 -0700619 case kStripe_Mode: {
620 const int height = decodeInfo.height();
621 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
622 // does not align with image blocks.
623 const int stripeHeight = 37;
624 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
msarettb1be46b2016-05-17 08:52:11 -0700625 void* dst = pixels.get();
msarett0a242972015-06-11 14:27:27 -0700626
627 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000628 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarettb65e6042016-02-23 05:37:25 -0800629 return "Could not start scanline decoder";
630 }
631
632 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
633 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
634 // to run this test for image types that do not have this scanline ordering.
scroggo12e2f502016-05-16 09:04:18 -0700635 // We only run this on Jpeg, which is always kTopDown.
636 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
msarette6dd0042015-10-09 11:07:34 -0700637
msarett0a242972015-06-11 14:27:27 -0700638 for (int i = 0; i < numStripes; i += 2) {
639 // Skip a stripe
640 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700641 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700642
643 // Read a stripe
644 const int startY = (i + 1) * stripeHeight;
645 const int linesToRead = SkTMin(stripeHeight, height - startY);
646 if (linesToRead > 0) {
msarettb1be46b2016-05-17 08:52:11 -0700647 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
648 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700649 }
650 }
651
652 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000653 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
scroggo1c005e42015-08-04 09:24:45 -0700654 if (SkCodec::kSuccess != startResult) {
655 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700656 }
657 for (int i = 0; i < numStripes; i += 2) {
658 // Read a stripe
659 const int startY = i * stripeHeight;
660 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarettb1be46b2016-05-17 08:52:11 -0700661 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
662 rowBytes);
msarett0a242972015-06-11 14:27:27 -0700663
664 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700665 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
666 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700667 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700668 }
669 }
msarettb1be46b2016-05-17 08:52:11 -0700670
Leon Scroggins571b30f2017-07-11 17:35:31 +0000671 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700672 break;
msarett0a242972015-06-11 14:27:27 -0700673 }
msarett91c22b22016-02-22 12:27:46 -0800674 case kCroppedScanline_Mode: {
675 const int width = decodeInfo.width();
676 const int height = decodeInfo.height();
677 // This value is chosen because, as we move across the image, it will sometimes
678 // align with the jpeg block sizes and it will sometimes not. This allows us
679 // to test interestingly different code paths in the implementation.
680 const int tileSize = 36;
msarett91c22b22016-02-22 12:27:46 -0800681 SkIRect subset;
682 for (int x = 0; x < width; x += tileSize) {
683 subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height);
Matt Saretta40d9c82017-05-19 15:21:05 -0400684 options.fSubset = &subset;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000685 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
msarett91c22b22016-02-22 12:27:46 -0800686 return "Could not start scanline decoder.";
687 }
688
msarettb1be46b2016-05-17 08:52:11 -0700689 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
msarett91c22b22016-02-22 12:27:46 -0800690 }
691
Leon Scroggins571b30f2017-07-11 17:35:31 +0000692 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
msarett91c22b22016-02-22 12:27:46 -0800693 break;
694 }
scroggob636b452015-07-22 07:16:20 -0700695 case kSubset_Mode: {
696 // Arbitrarily choose a divisor.
697 int divisor = 2;
698 // Total width/height of the image.
699 const int W = codec->getInfo().width();
700 const int H = codec->getInfo().height();
701 if (divisor > W || divisor > H) {
702 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
703 "for %s with dimensions (%d x %d)", divisor,
704 fPath.c_str(), W, H));
705 }
706 // subset dimensions
707 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
708 const int w = SkAlign2(W / divisor);
709 const int h = SkAlign2(H / divisor);
710 SkIRect subset;
Matt Saretta40d9c82017-05-19 15:21:05 -0400711 options.fSubset = &subset;
scroggob636b452015-07-22 07:16:20 -0700712 SkBitmap subsetBm;
713 // We will reuse pixel memory from bitmap.
msarettb1be46b2016-05-17 08:52:11 -0700714 void* dst = pixels.get();
scroggob636b452015-07-22 07:16:20 -0700715 // Keep track of left and top (for drawing subsetBm into canvas). We could use
716 // fScale * x and fScale * y, but we want integers such that the next subset will start
717 // where the last one ended. So we'll add decodeInfo.width() and height().
718 int left = 0;
719 for (int x = 0; x < W; x += w) {
720 int top = 0;
721 for (int y = 0; y < H; y+= h) {
722 // Do not make the subset go off the edge of the image.
723 const int preScaleW = SkTMin(w, W - x);
724 const int preScaleH = SkTMin(h, H - y);
725 subset.setXYWH(x, y, preScaleW, preScaleH);
726 // And scale
727 // FIXME: Should we have a version of getScaledDimensions that takes a subset
728 // into account?
msarettc7eb4902016-04-25 07:04:58 -0700729 const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale));
730 const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale));
731 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
732 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
msarettb1be46b2016-05-17 08:52:11 -0700733 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
734 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000735 &options);
scroggob636b452015-07-22 07:16:20 -0700736 switch (result) {
737 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400738 case SkCodec::kErrorInInput:
scroggob636b452015-07-22 07:16:20 -0700739 case SkCodec::kIncompleteInput:
740 break;
scroggob636b452015-07-22 07:16:20 -0700741 default:
742 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
743 "from %s with dimensions (%d x %d)\t error %d",
744 x, y, decodeInfo.width(), decodeInfo.height(),
745 fPath.c_str(), W, H, result);
746 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000747 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType,
748 SkIntToScalar(left), SkIntToScalar(top));
msarettb1be46b2016-05-17 08:52:11 -0700749
scroggob636b452015-07-22 07:16:20 -0700750 // translate by the scaled height.
751 top += decodeInfo.height();
752 }
753 // translate by the scaled width.
754 left += decodeInfo.width();
755 }
756 return "";
757 }
msarettb714fb02016-01-22 14:46:42 -0800758 default:
759 SkASSERT(false);
760 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700761 }
scroggo9c59ebc2015-03-25 13:48:49 -0700762 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700763}
764
765SkISize CodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700766 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400767 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700768 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400769 return {0, 0};
scroggo7fac5af2015-09-30 11:33:12 -0700770 }
scroggo19b91532016-10-24 09:03:26 -0700771
772 auto imageSize = codec->getScaledDimensions(fScale);
773 if (fMode == kAnimated_Mode) {
scroggo90e971a2016-10-25 06:11:01 -0700774 // We'll draw one of each frame, so make it big enough to hold them all
775 // in a grid. The grid will be roughly square, with "factor" frames per
776 // row and up to "factor" rows.
scroggo19b91532016-10-24 09:03:26 -0700777 const size_t count = codec->getFrameInfo().size();
scroggo90e971a2016-10-25 06:11:01 -0700778 const float root = sqrt((float) count);
779 const int factor = sk_float_ceil2int(root);
780 imageSize.fWidth = imageSize.fWidth * factor;
781 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
scroggo19b91532016-10-24 09:03:26 -0700782 }
783 return imageSize;
scroggo9b77ddd2015-03-19 06:03:39 -0700784}
785
786Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700787 if (1.0f == fScale) {
scroggo6e8c68e2016-10-24 13:48:49 -0700788 Name name = SkOSPath::Basename(fPath.c_str());
789 if (fMode == kAnimated_Mode) {
790 name.append("_animated");
791 }
792 return name;
msarett0a242972015-06-11 14:27:27 -0700793 }
scroggo6e8c68e2016-10-24 13:48:49 -0700794 SkASSERT(fMode != kAnimated_Mode);
msaretta5783ae2015-09-08 15:35:32 -0700795 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700796}
797
798/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
799
scroggof8dc9df2016-05-16 09:04:13 -0700800AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800801 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700802 : fPath(path)
msarett3d9d7a72015-10-21 10:27:10 -0700803 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800804 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700805 , fSampleSize(sampleSize)
scroggo3ac66e92016-02-08 15:09:48 -0800806 , fRunSerially(serial_from_path_name(path))
msarett3d9d7a72015-10-21 10:27:10 -0700807{}
808
809bool AndroidCodecSrc::veto(SinkFlags flags) const {
810 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700811 return flags.type != SinkFlags::kRaster
812 || flags.approach != SinkFlags::kDirect;
813}
814
815Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
Matt Saretta40d9c82017-05-19 15:21:05 -0400816 if (canvas->imageInfo().colorSpace() &&
817 kRGBA_F16_SkColorType != canvas->imageInfo().colorType()) {
818 // SkAndroidCodec uses legacy premultiplication and blending. Therefore, we only
819 // run these tests on legacy canvases.
820 // We allow an exception for F16, since Android uses F16.
821 return Error::Nonfatal("Skip testing to color correct canvas.");
822 }
823
bungeman38d909e2016-08-02 14:40:46 -0700824 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700825 if (!encoded) {
826 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
827 }
Mike Reedede7bac2017-07-23 15:30:02 -0400828 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
829 if (nullptr == codec) {
msarett3d9d7a72015-10-21 10:27:10 -0700830 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
831 }
832
scroggoba584892016-05-20 13:56:13 -0700833 SkImageInfo decodeInfo = codec->getInfo();
834 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
835 fDstAlphaType)) {
Matt Sarettea518f02017-02-03 11:58:59 -0800836 return Error::Nonfatal("Skipping uninteresting test.");
msarett3d9d7a72015-10-21 10:27:10 -0700837 }
838
839 // Scale the image if it is desired.
840 SkISize size = codec->getSampledDimensions(fSampleSize);
841
842 // Visually inspecting very small output images is not necessary. We will
843 // cover these cases in unit testing.
844 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
845 return Error::Nonfatal("Scaling very small images is uninteresting.");
846 }
847 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
848
msarettb1be46b2016-05-17 08:52:11 -0700849 int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
850 size_t rowBytes = size.width() * bpp;
851 SkAutoMalloc pixels(size.height() * rowBytes);
msarett3d9d7a72015-10-21 10:27:10 -0700852
853 SkBitmap bitmap;
msarett34e0ec42016-04-22 16:27:24 -0700854 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -0400855 set_bitmap_color_space(&bitmapInfo);
msarett34e0ec42016-04-22 16:27:24 -0700856 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
857 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
858 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
859 }
msarett3d9d7a72015-10-21 10:27:10 -0700860
861 // Create options for the codec.
862 SkAndroidCodec::AndroidOptions options;
msarett3d9d7a72015-10-21 10:27:10 -0700863 options.fSampleSize = fSampleSize;
864
msarettb1be46b2016-05-17 08:52:11 -0700865 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
scroggof8dc9df2016-05-16 09:04:13 -0700866 case SkCodec::kSuccess:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400867 case SkCodec::kErrorInInput:
scroggof8dc9df2016-05-16 09:04:13 -0700868 case SkCodec::kIncompleteInput:
869 break;
msarett3d9d7a72015-10-21 10:27:10 -0700870 default:
scroggof8dc9df2016-05-16 09:04:13 -0700871 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
msarett3d9d7a72015-10-21 10:27:10 -0700872 }
Leon Scroggins571b30f2017-07-11 17:35:31 +0000873 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
scroggof8dc9df2016-05-16 09:04:13 -0700874 return "";
msarett3d9d7a72015-10-21 10:27:10 -0700875}
876
877SkISize AndroidCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700878 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400879 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett3d9d7a72015-10-21 10:27:10 -0700880 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400881 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700882 }
883 return codec->getSampledDimensions(fSampleSize);
884}
885
886Name AndroidCodecSrc::name() const {
887 // We will replicate the names used by CodecSrc so that images can
888 // be compared in Gold.
889 if (1 == fSampleSize) {
890 return SkOSPath::Basename(fPath.c_str());
891 }
msarett4b0778e2015-11-13 09:59:11 -0800892 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700893}
894
895/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
896
msarett18976312016-03-09 14:20:58 -0800897ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
898 : fPath(path)
899 , fMode(mode)
900 , fDstAlphaType(alphaType)
901 , fIsGpu(isGpu)
902 , fRunSerially(serial_from_path_name(path))
903{}
904
905bool ImageGenSrc::veto(SinkFlags flags) const {
906 if (fIsGpu) {
Brian Salomonbd7c5512017-03-07 09:08:36 -0500907 // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs.
908 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect ||
909 flags.multisampled == SinkFlags::kMultisampled;
msarett18976312016-03-09 14:20:58 -0800910 }
911
912 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
913}
914
915Error ImageGenSrc::draw(SkCanvas* canvas) const {
916 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
917 return Error::Nonfatal("Uninteresting to test image generator to 565.");
918 }
919
bungeman38d909e2016-08-02 14:40:46 -0700920 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett18976312016-03-09 14:20:58 -0800921 if (!encoded) {
922 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
923 }
924
msarettfc0b6d12016-03-17 13:50:17 -0700925#if defined(SK_BUILD_FOR_WIN)
926 // Initialize COM in order to test with WIC.
927 SkAutoCoInitialize com;
928 if (!com.succeeded()) {
929 return "Could not initialize COM.";
930 }
931#endif
932
Ben Wagner145dbcd2016-11-03 14:40:50 -0400933 std::unique_ptr<SkImageGenerator> gen(nullptr);
msarett18976312016-03-09 14:20:58 -0800934 switch (fMode) {
935 case kCodec_Mode:
Mike Reed185130c2017-02-15 15:14:16 -0500936 gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded);
msarett18976312016-03-09 14:20:58 -0800937 if (!gen) {
938 return "Could not create codec image generator.";
939 }
940 break;
msarettfc0b6d12016-03-17 13:50:17 -0700941 case kPlatform_Mode: {
msarett18976312016-03-09 14:20:58 -0800942#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
bungeman38d909e2016-08-02 14:40:46 -0700943 gen.reset(SkImageGeneratorCG::NewFromEncodedCG(encoded.get()));
msarettfc0b6d12016-03-17 13:50:17 -0700944#elif defined(SK_BUILD_FOR_WIN)
bungeman38d909e2016-08-02 14:40:46 -0700945 gen.reset(SkImageGeneratorWIC::NewFromEncodedWIC(encoded.get()));
msarettfc0b6d12016-03-17 13:50:17 -0700946#endif
947
msarett18976312016-03-09 14:20:58 -0800948 if (!gen) {
msarettfc0b6d12016-03-17 13:50:17 -0700949 return "Could not create platform image generator.";
msarett18976312016-03-09 14:20:58 -0800950 }
951 break;
msarettfc0b6d12016-03-17 13:50:17 -0700952 }
msarett18976312016-03-09 14:20:58 -0800953 default:
954 SkASSERT(false);
955 return "Invalid image generator mode";
956 }
957
958 // Test deferred decoding path on GPU
959 if (fIsGpu) {
Mike Reed185130c2017-02-15 15:14:16 -0500960 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr));
msarett18976312016-03-09 14:20:58 -0800961 if (!image) {
962 return "Could not create image from codec image generator.";
963 }
964 canvas->drawImage(image, 0, 0);
965 return "";
966 }
mtklein343a63d2016-03-22 11:46:53 -0700967
msarett18976312016-03-09 14:20:58 -0800968 // Test various color and alpha types on CPU
969 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
mtklein343a63d2016-03-22 11:46:53 -0700970
Matt Saretta40d9c82017-05-19 15:21:05 -0400971 SkImageGenerator::Options options;
972 options.fBehavior = canvas->imageInfo().colorSpace() ?
973 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
974
msarettb1be46b2016-05-17 08:52:11 -0700975 int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
976 size_t rowBytes = decodeInfo.width() * bpp;
977 SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
Matt Saretta40d9c82017-05-19 15:21:05 -0400978 if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
Matt Sarett05cb4c32017-03-02 12:07:46 -0500979 SkString err =
980 SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str());
981
982#if defined(SK_BUILD_FOR_WIN)
983 if (kPlatform_Mode == fMode) {
984 // Do not issue a fatal error for WIC flakiness.
985 return Error::Nonfatal(err);
986 }
987#endif
988
989 return err;
msarett18976312016-03-09 14:20:58 -0800990 }
991
Matt Sarettf485cf92017-05-26 08:58:03 -0400992 set_bitmap_color_space(&decodeInfo);
Leon Scroggins571b30f2017-07-11 17:35:31 +0000993 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes,
msarettb1be46b2016-05-17 08:52:11 -0700994 CodecSrc::kGetFromCanvas_DstColorType);
msarett18976312016-03-09 14:20:58 -0800995 return "";
996}
997
998SkISize ImageGenSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -0700999 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001000 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett18976312016-03-09 14:20:58 -08001001 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001002 return {0, 0};
msarett18976312016-03-09 14:20:58 -08001003 }
1004 return codec->getInfo().dimensions();
1005}
1006
1007Name ImageGenSrc::name() const {
1008 return SkOSPath::Basename(fPath.c_str());
1009}
1010
1011/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1012
msarett9ce3a542016-07-15 13:54:38 -07001013ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, SkColorType colorType)
msarett69deca82016-04-29 09:38:40 -07001014 : fPath(path)
1015 , fMode(mode)
msarett9ce3a542016-07-15 13:54:38 -07001016 , fColorType(colorType)
msarett69deca82016-04-29 09:38:40 -07001017{}
1018
1019bool ColorCodecSrc::veto(SinkFlags flags) const {
1020 // Test to direct raster backends (8888 and 565).
1021 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
1022}
1023
Matt Sarett6a0feba2017-06-09 10:45:44 -04001024void clamp_if_necessary(const SkBitmap& bitmap, SkColorType dstCT) {
1025 if (kRGBA_F16_SkColorType != bitmap.colorType() || kRGBA_F16_SkColorType == dstCT) {
1026 // No need to clamp if the dst is F16. We will clamp when we encode to PNG.
1027 return;
1028 }
1029
Mike Klein45c16fa2017-07-18 18:15:13 -04001030 SkJumper_MemoryCtx ptr = { bitmap.getAddr(0,0), bitmap.rowBytesAsPixels() };
1031
Matt Sarett6a0feba2017-06-09 10:45:44 -04001032 SkRasterPipeline_<256> p;
1033 p.append(SkRasterPipeline::load_f16, &ptr);
1034 p.append(SkRasterPipeline::clamp_0);
1035 if (kPremul_SkAlphaType == bitmap.alphaType()) {
1036 p.append(SkRasterPipeline::clamp_a);
1037 } else {
1038 p.append(SkRasterPipeline::clamp_1);
1039 }
1040 p.append(SkRasterPipeline::store_f16, &ptr);
1041
Mike Klein45c16fa2017-07-18 18:15:13 -04001042 p.run(0,0, bitmap.width(), bitmap.height());
Matt Sarett6a0feba2017-06-09 10:45:44 -04001043}
1044
msarett69deca82016-04-29 09:38:40 -07001045Error ColorCodecSrc::draw(SkCanvas* canvas) const {
1046 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
1047 return Error::Nonfatal("No need to test color correction to 565 backend.");
1048 }
1049
msarettd1ec89b2016-08-03 12:59:27 -07001050 bool runInLegacyMode = kBaseline_Mode == fMode;
msarettd1ec89b2016-08-03 12:59:27 -07001051 if (runInLegacyMode && canvas->imageInfo().colorSpace()) {
1052 return Error::Nonfatal("Skipping tests that are only interesting in legacy mode.");
1053 } else if (!runInLegacyMode && !canvas->imageInfo().colorSpace()) {
1054 return Error::Nonfatal("Skipping tests that are only interesting in srgb mode.");
msarett9ce3a542016-07-15 13:54:38 -07001055 }
1056
bungeman38d909e2016-08-02 14:40:46 -07001057 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
msarett69deca82016-04-29 09:38:40 -07001058 if (!encoded) {
1059 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1060 }
1061
Mike Reedede7bac2017-07-23 15:30:02 -04001062 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
1063 if (nullptr == codec) {
msarett69deca82016-04-29 09:38:40 -07001064 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
1065 }
1066
msarett9876ac52016-06-01 14:47:18 -07001067 // Load the dst ICC profile. This particular dst is fairly similar to Adobe RGB.
1068 sk_sp<SkData> dstData = SkData::MakeFromFileName(
msaretta714bc32016-07-29 08:58:33 -07001069 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
msarett9876ac52016-06-01 14:47:18 -07001070 if (!dstData) {
1071 return "Cannot read monitor profile. Is the resource path set correctly?";
1072 }
1073
msarett50ce1f22016-07-29 06:23:33 -07001074 sk_sp<SkColorSpace> dstSpace = nullptr;
1075 if (kDst_sRGB_Mode == fMode) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -05001076 dstSpace = SkColorSpace::MakeSRGB();
msarett50ce1f22016-07-29 06:23:33 -07001077 } else if (kDst_HPZR30w_Mode == fMode) {
Brian Osman526972e2016-10-24 09:24:02 -04001078 dstSpace = SkColorSpace::MakeICC(dstData->data(), dstData->size());
msarett50ce1f22016-07-29 06:23:33 -07001079 }
1080
1081 SkImageInfo decodeInfo = codec->getInfo().makeColorType(fColorType).makeColorSpace(dstSpace);
msarettd1ec89b2016-08-03 12:59:27 -07001082 if (kUnpremul_SkAlphaType == decodeInfo.alphaType()) {
1083 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
1084 }
msarett2ecc35f2016-09-08 11:55:16 -07001085 if (kRGBA_F16_SkColorType == fColorType) {
raftias94888332016-10-18 10:02:51 -07001086 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(decodeInfo.colorSpace())->type());
1087 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(decodeInfo.colorSpace());
1088 decodeInfo = decodeInfo.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001089 }
msarettd1ec89b2016-08-03 12:59:27 -07001090
msarett50ce1f22016-07-29 06:23:33 -07001091 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettf485cf92017-05-26 08:58:03 -04001092 set_bitmap_color_space(&bitmapInfo);
msarett50ce1f22016-07-29 06:23:33 -07001093 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
1094 kBGRA_8888_SkColorType == decodeInfo.colorType())
1095 {
1096 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
1097 }
1098
1099 SkBitmap bitmap;
1100 if (!bitmap.tryAllocPixels(bitmapInfo)) {
1101 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
1102 bitmapInfo.width(), bitmapInfo.height());
1103 }
1104
1105 size_t rowBytes = bitmap.rowBytes();
1106 SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), rowBytes);
Leon Scroggins III674a1842017-07-06 12:26:09 -04001107 switch (r) {
1108 case SkCodec::kSuccess:
1109 case SkCodec::kErrorInInput:
1110 case SkCodec::kIncompleteInput:
1111 break;
1112 default:
1113 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
msarett50ce1f22016-07-29 06:23:33 -07001114 }
1115
msarett69deca82016-04-29 09:38:40 -07001116 switch (fMode) {
1117 case kBaseline_Mode:
msarett39979d82016-07-28 17:11:18 -07001118 case kDst_sRGB_Mode:
msarett50ce1f22016-07-29 06:23:33 -07001119 case kDst_HPZR30w_Mode:
Matt Sarett6a0feba2017-06-09 10:45:44 -04001120 // We do not support drawing unclamped F16.
1121 clamp_if_necessary(bitmap, canvas->imageInfo().colorType());
msarett39979d82016-07-28 17:11:18 -07001122 canvas->drawBitmap(bitmap, 0, 0);
1123 break;
msarett69deca82016-04-29 09:38:40 -07001124 default:
1125 SkASSERT(false);
1126 return "Invalid fMode";
1127 }
1128 return "";
1129}
1130
1131SkISize ColorCodecSrc::size() const {
bungeman38d909e2016-08-02 14:40:46 -07001132 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -04001133 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
msarett69deca82016-04-29 09:38:40 -07001134 if (nullptr == codec) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001135 return {0, 0};
msarett69deca82016-04-29 09:38:40 -07001136 }
Hal Canaryfafe1352017-04-11 12:12:02 -04001137 return {codec->getInfo().width(), codec->getInfo().height()};
msarett69deca82016-04-29 09:38:40 -07001138}
1139
1140Name ColorCodecSrc::name() const {
1141 return SkOSPath::Basename(fPath.c_str());
1142}
1143
1144/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1145
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001146static const SkRect kSKPViewport = {0, 0, 1000, 1000};
mtkleinf4ba3212015-01-28 15:32:24 -08001147
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001148SKPSrc::SKPSrc(Path path) : fPath(path) { }
mtklein748ca3b2015-01-15 10:56:12 -08001149
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001150static sk_sp<SkPicture> read_skp(const char* path) {
1151 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtklein75d98fd2015-01-18 07:05:01 -08001152 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001153 return nullptr;
mtklein748ca3b2015-01-15 10:56:12 -08001154 }
bungemanf93d7112016-09-16 06:24:20 -07001155 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
mtklein75d98fd2015-01-18 07:05:01 -08001156 if (!pic) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001157 return nullptr;
mtklein75d98fd2015-01-18 07:05:01 -08001158 }
bungemanf93d7112016-09-16 06:24:20 -07001159 stream = nullptr; // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -07001160
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001161 return pic;
1162}
1163
1164Error SKPSrc::draw(SkCanvas* canvas) const {
1165 sk_sp<SkPicture> pic = read_skp(fPath.c_str());
1166 if (!pic) {
1167 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1168 }
1169
mtkleinf4ba3212015-01-28 15:32:24 -08001170 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -08001171 canvas->drawPicture(pic);
1172 return "";
1173}
1174
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001175static SkRect get_cull_rect_for_skp(const char* path) {
1176 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
mtkleinffa901a2015-03-16 10:38:07 -07001177 if (!stream) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001178 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001179 }
1180 SkPictInfo info;
bungemanf93d7112016-09-16 06:24:20 -07001181 if (!SkPicture::InternalOnly_StreamIsSKP(stream.get(), &info)) {
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001182 return SkRect::MakeEmpty();
mtkleinffa901a2015-03-16 10:38:07 -07001183 }
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001184
1185 return info.fCullRect;
1186}
1187
1188SkISize SKPSrc::size() const {
1189 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
1190 if (!viewport.intersect(kSKPViewport)) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001191 return {0, 0};
mtkleinffa901a2015-03-16 10:38:07 -07001192 }
1193 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -08001194}
1195
1196Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1197
1198/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
Robert Phillipsad8a43f2017-08-30 12:06:35 -04001199
1200static const int kNumDDLXTiles = 4;
1201static const int kNumDDLYTiles = 4;
1202static const int kDDLTileSize = 1024;
1203static const SkRect kDDLSKPViewport = { 0, 0,
1204 kNumDDLXTiles * kDDLTileSize,
1205 kNumDDLYTiles * kDDLTileSize };
1206
1207DDLSKPSrc::DDLSKPSrc(Path path) : fPath(path) { }
1208
1209Error DDLSKPSrc::draw(SkCanvas* canvas) const {
1210 class TileData {
1211 public:
1212 // Note: we could just pass in surface characterization
1213 TileData(sk_sp<SkSurface> surf, const SkIRect& clip)
1214 : fSurface(std::move(surf))
1215 , fClip(clip) {
1216 SkAssertResult(fSurface->characterize(&fCharacterization));
1217 }
1218
1219 // This method operates in parallel
1220 void preprocess(SkPicture* pic) {
1221 SkDeferredDisplayListRecorder recorder(fCharacterization);
1222
1223 SkCanvas* subCanvas = recorder.getCanvas();
1224
1225 subCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
1226 subCanvas->translate(-fClip.fLeft, -fClip.fTop);
1227
1228 // Note: in this use case we only render a picture to the deferred canvas
1229 // but, more generally, clients will use arbitrary draw calls.
1230 subCanvas->drawPicture(pic);
1231
1232 fDisplayList = recorder.detach();
1233 }
1234
1235 // This method operates serially
1236 void draw() {
1237 fSurface->draw(fDisplayList.get());
1238 }
1239
1240 // This method also operates serially
1241 void compose(SkCanvas* dst) {
1242 sk_sp<SkImage> img = fSurface->makeImageSnapshot();
1243 dst->save();
1244 dst->clipRect(SkRect::Make(fClip));
1245 dst->drawImage(std::move(img), fClip.fLeft, fClip.fTop);
1246 dst->restore();
1247 }
1248
1249 private:
1250 sk_sp<SkSurface> fSurface;
1251 SkIRect fClip; // in the device space of the destination canvas
1252 std::unique_ptr<SkDeferredDisplayList> fDisplayList;
1253 SkSurfaceCharacterization fCharacterization;
1254 };
1255
1256 SkTArray<TileData> tileData;
1257 tileData.reserve(16);
1258
1259 sk_sp<SkPicture> pic = read_skp(fPath.c_str());
1260 if (!pic) {
1261 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
1262 }
1263
1264 const SkRect cullRect = pic->cullRect();
1265
1266 // All the destination tiles are the same size
1267 const SkImageInfo tileII = SkImageInfo::MakeN32Premul(kDDLTileSize, kDDLTileSize);
1268
1269 // First, create the destination tiles
1270 for (int y = 0; y < kNumDDLYTiles; ++y) {
1271 for (int x = 0; x < kNumDDLXTiles; ++x) {
1272 SkRect clip = SkRect::MakeXYWH(x * kDDLTileSize, y * kDDLTileSize,
1273 kDDLTileSize, kDDLTileSize);
1274
1275 if (!clip.intersect(cullRect)) {
1276 continue;
1277 }
1278
1279 tileData.push_back(TileData(canvas->makeSurface(tileII), clip.roundOut()));
1280 }
1281 }
1282
1283 // Second, run the cpu pre-processing in threads
1284 SkTaskGroup().batch(tileData.count(), [&](int i) {
1285 tileData[i].preprocess(pic.get());
1286 });
1287
1288 // Third, synchronously render the display lists into the dest tiles
1289 // TODO: it would be cool to not wait until all the tiles are drawn to begin
1290 // drawing to the GPU
1291 for (int i = 0; i < tileData.count(); ++i) {
1292 tileData[i].draw();
1293 }
1294
1295 // Finally, compose the drawn tiles into the result
1296 // Note: the separation between the tiles and the final composition better
1297 // matches Chrome but costs us a copy
1298 for (int i = 0; i < tileData.count(); ++i) {
1299 tileData[i].compose(canvas);
1300 }
1301
1302 return "";
1303}
1304
1305SkISize DDLSKPSrc::size() const {
1306 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
1307 if (!viewport.intersect(kDDLSKPViewport)) {
1308 return {0, 0};
1309 }
1310 return viewport.roundOut().size();
1311}
1312
1313Name DDLSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1314
1315/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fmalitaa2b9fdf2016-08-03 19:53:36 -07001316#if defined(SK_XML)
fmalitabdf3e5c2016-09-17 07:26:26 -07001317// Used when the image doesn't have an intrinsic size.
Hal Canaryfafe1352017-04-11 12:12:02 -04001318static const SkSize kDefaultSVGSize = {1000, 1000};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001319
fmalitabdf3e5c2016-09-17 07:26:26 -07001320// Used to force-scale tiny fixed-size images.
Hal Canaryfafe1352017-04-11 12:12:02 -04001321static const SkSize kMinimumSVGSize = {128, 128};
fmalitaa2b9fdf2016-08-03 19:53:36 -07001322
fmalitaacd2f5c2016-11-08 07:13:45 -08001323SVGSrc::SVGSrc(Path path)
1324 : fName(SkOSPath::Basename(path.c_str()))
1325 , fScale(1) {
fmalitabdf3e5c2016-09-17 07:26:26 -07001326
fmalitaacd2f5c2016-11-08 07:13:45 -08001327 SkFILEStream stream(path.c_str());
1328 if (!stream.isValid()) {
1329 return;
1330 }
1331 fDom = SkSVGDOM::MakeFromStream(stream);
1332 if (!fDom) {
1333 return;
1334 }
1335
1336 const SkSize& sz = fDom->containerSize();
1337 if (sz.isEmpty()) {
1338 // no intrinsic size
1339 fDom->setContainerSize(kDefaultSVGSize);
1340 } else {
1341 fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(),
1342 kMinimumSVGSize.height() / sz.height()));
1343 }
1344}
1345
1346Error SVGSrc::draw(SkCanvas* canvas) const {
fmalitabdf3e5c2016-09-17 07:26:26 -07001347 if (!fDom) {
fmalitaacd2f5c2016-11-08 07:13:45 -08001348 return SkStringPrintf("Unable to parse file: %s", fName.c_str());
fmalitaa2b9fdf2016-08-03 19:53:36 -07001349 }
1350
fmalitaacd2f5c2016-11-08 07:13:45 -08001351 SkAutoCanvasRestore acr(canvas, true);
1352 canvas->scale(fScale, fScale);
1353 fDom->render(canvas);
1354
fmalitaa2b9fdf2016-08-03 19:53:36 -07001355 return "";
1356}
1357
1358SkISize SVGSrc::size() const {
fmalitaacd2f5c2016-11-08 07:13:45 -08001359 if (!fDom) {
Hal Canaryfafe1352017-04-11 12:12:02 -04001360 return {0, 0};
fmalitabdf3e5c2016-09-17 07:26:26 -07001361 }
1362
Hal Canaryfafe1352017-04-11 12:12:02 -04001363 return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale}
1364 .toRound();
fmalitaa2b9fdf2016-08-03 19:53:36 -07001365}
1366
fmalitaacd2f5c2016-11-08 07:13:45 -08001367Name SVGSrc::name() const { return fName; }
fmalitaa2b9fdf2016-08-03 19:53:36 -07001368
fmalita179d8852016-08-16 14:23:29 -07001369bool SVGSrc::veto(SinkFlags flags) const {
Florin Malita93323eb2017-06-16 15:31:17 -04001370 // No need to test to non-(raster||gpu||vector) or indirect backends.
fmalita179d8852016-08-16 14:23:29 -07001371 bool type_ok = flags.type == SinkFlags::kRaster
Florin Malita93323eb2017-06-16 15:31:17 -04001372 || flags.type == SinkFlags::kGPU
1373 || flags.type == SinkFlags::kVector;
fmalita179d8852016-08-16 14:23:29 -07001374
1375 return !type_ok || flags.approach != SinkFlags::kDirect;
1376}
1377
fmalitaa2b9fdf2016-08-03 19:53:36 -07001378#endif // defined(SK_XML)
1379/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein748ca3b2015-01-15 10:56:12 -08001380
halcanary45420a92016-06-02 12:41:14 -07001381MSKPSrc::MSKPSrc(Path path) : fPath(path) {
bungemanf93d7112016-09-16 06:24:20 -07001382 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
Hal Canary45cde312017-04-03 16:06:42 -04001383 int count = SkMultiPictureDocumentReadPageCount(stream.get());
1384 if (count > 0) {
1385 fPages.reset(count);
1386 (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count());
1387 }
halcanary45420a92016-06-02 12:41:14 -07001388}
1389
Hal Canary45cde312017-04-03 16:06:42 -04001390int MSKPSrc::pageCount() const { return fPages.count(); }
halcanary45420a92016-06-02 12:41:14 -07001391
1392SkISize MSKPSrc::size() const { return this->size(0); }
Hal Canary45cde312017-04-03 16:06:42 -04001393SkISize MSKPSrc::size(int i) const {
Hal Canaryfafe1352017-04-11 12:12:02 -04001394 return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0};
Hal Canary45cde312017-04-03 16:06:42 -04001395}
halcanary45420a92016-06-02 12:41:14 -07001396
1397Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); }
1398Error MSKPSrc::draw(int i, SkCanvas* canvas) const {
Hal Canary45cde312017-04-03 16:06:42 -04001399 if (this->pageCount() == 0) {
halcanary45420a92016-06-02 12:41:14 -07001400 return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
1401 }
Hal Canary45cde312017-04-03 16:06:42 -04001402 if (i >= fPages.count() || i < 0) {
halcanary45420a92016-06-02 12:41:14 -07001403 return SkStringPrintf("MultiPictureDocument page number out of range: %d", i);
1404 }
Hal Canary45cde312017-04-03 16:06:42 -04001405 SkPicture* page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001406 if (!page) {
Hal Canary45cde312017-04-03 16:06:42 -04001407 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1408 if (!stream) {
1409 return SkStringPrintf("Unable to open file: %s", fPath.c_str());
1410 }
1411 if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) {
1412 return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i,
1413 fPath.c_str());
1414 }
1415 page = fPages[i].fPicture.get();
halcanary45420a92016-06-02 12:41:14 -07001416 }
1417 canvas->drawPicture(page);
1418 return "";
1419}
1420
1421Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1422
1423/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1424
mtkleinad66f9b2015-02-13 15:11:10 -08001425Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
Mike Reed5df49342016-11-12 08:06:55 -06001426 return src.draw(SkMakeNullCanvas().get());
mtkleinad66f9b2015-02-13 15:11:10 -08001427}
1428
1429/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1430
mtkleinb9eb4ac2015-02-02 18:26:03 -08001431DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
1432
bsalomon85b4b532016-04-05 11:06:27 -07001433GPUSink::GPUSink(GrContextFactory::ContextType ct,
csmartdaltone812d492017-02-21 12:36:05 -07001434 GrContextFactory::ContextOverrides overrides,
mtklein82d28432015-01-15 12:46:02 -08001435 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -07001436 bool diText,
brianosmand93c1202016-03-10 07:49:08 -08001437 SkColorType colorType,
Brian Salomonce5ee602017-07-17 11:31:31 -04001438 SkAlphaType alphaType,
brianosmanb109b8c2016-06-16 13:03:24 -07001439 sk_sp<SkColorSpace> colorSpace,
Brian Osmanf21aa042017-08-21 16:48:46 -04001440 bool threaded,
1441 const GrContextOptions& grCtxOptions)
Brian Salomonce5ee602017-07-17 11:31:31 -04001442 : fContextType(ct)
1443 , fContextOverrides(overrides)
1444 , fSampleCount(samples)
1445 , fUseDIText(diText)
1446 , fColorType(colorType)
1447 , fAlphaType(alphaType)
1448 , fColorSpace(std::move(colorSpace))
Brian Osmanf21aa042017-08-21 16:48:46 -04001449 , fThreaded(threaded)
1450 , fBaseContextOptions(grCtxOptions) {}
mtklein748ca3b2015-01-15 10:56:12 -08001451
Brian Salomon09d994e2016-12-21 11:14:46 -05001452DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
bsalomon648c6962015-10-23 09:06:59 -07001453
Brian Osmanf9810662017-08-30 10:02:10 -04001454Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const {
1455 return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
1456}
1457
1458Error GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
1459 const GrContextOptions& baseOptions) const {
1460 GrContextOptions grOptions = baseOptions;
kkinnunen64492c42015-12-08 01:24:40 -08001461
kkinnunen5219fd92015-12-10 06:28:13 -08001462 src.modifyGrContextOptions(&grOptions);
1463
1464 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -08001465 const SkISize size = src.size();
Brian Salomonce5ee602017-07-17 11:31:31 -04001466 SkImageInfo info =
1467 SkImageInfo::Make(size.width(), size.height(), fColorType, fAlphaType, fColorSpace);
msarett13a036b2016-02-08 09:10:47 -08001468#if SK_SUPPORT_GPU
csmartdaltone812d492017-02-21 12:36:05 -07001469 GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext();
bsalomon8b7451a2016-05-11 06:33:06 -07001470 const int maxDimension = context->caps()->maxTextureSize();
msarett13a036b2016-02-08 09:10:47 -08001471 if (maxDimension < SkTMax(size.width(), size.height())) {
1472 return Error::Nonfatal("Src too large to create a texture.\n");
1473 }
1474#endif
1475
reede8f30622016-03-23 18:59:25 -07001476 auto surface(
csmartdaltone812d492017-02-21 12:36:05 -07001477 NewGpuSurface(&factory, fContextType, fContextOverrides, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -08001478 if (!surface) {
1479 return "Could not create a surface.";
1480 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001481 if (FLAGS_preAbandonGpuContext) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001482 factory.abandonContexts();
1483 }
mtklein748ca3b2015-01-15 10:56:12 -08001484 SkCanvas* canvas = surface->getCanvas();
1485 Error err = src.draw(canvas);
1486 if (!err.isEmpty()) {
1487 return err;
1488 }
1489 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -08001490 if (FLAGS_gpuStats) {
1491 canvas->getGrContext()->dumpCacheStats(log);
1492 canvas->getGrContext()->dumpGpuStats(log);
1493 }
Brian Salomonce5ee602017-07-17 11:31:31 -04001494 if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType) {
1495 // We don't currently support readbacks into these formats on the GPU backend. Convert to
1496 // 32 bit.
1497 info = SkImageInfo::Make(size.width(), size.height(), kRGBA_8888_SkColorType,
1498 kPremul_SkAlphaType, fColorSpace);
1499 }
mtklein748ca3b2015-01-15 10:56:12 -08001500 dst->allocPixels(info);
Mike Reed12e946b2017-04-17 10:53:29 -04001501 canvas->readPixels(*dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -08001502 if (FLAGS_abandonGpuContext) {
1503 factory.abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -07001504 } else if (FLAGS_releaseAndAbandonGpuContext) {
1505 factory.releaseResourcesAndAbandonContexts();
mtklein55e88b22015-01-21 15:50:13 -08001506 }
mtklein748ca3b2015-01-15 10:56:12 -08001507 return "";
1508}
1509
1510/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1511
Brian Osmanf9810662017-08-30 10:02:10 -04001512GPUThreadTestingSink::GPUThreadTestingSink(GrContextFactory::ContextType ct,
1513 GrContextFactory::ContextOverrides overrides,
1514 int samples,
1515 bool diText,
1516 SkColorType colorType,
1517 SkAlphaType alphaType,
1518 sk_sp<SkColorSpace> colorSpace,
1519 bool threaded,
1520 const GrContextOptions& grCtxOptions)
1521 : INHERITED(ct, overrides, samples, diText, colorType, alphaType, std::move(colorSpace),
1522 threaded, grCtxOptions)
1523 , fExecutor(SkExecutor::MakeThreadPool(FLAGS_gpuThreads)) {
1524 SkASSERT(fExecutor);
1525}
1526
1527Error GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1528 SkString* log) const {
1529 // Draw twice, once with worker threads, and once without. Verify that we get the same result.
1530 // Also, force us to only use the software path renderer, so we really stress-test the threaded
1531 // version of that code.
1532 GrContextOptions contextOptions = this->baseContextOptions();
1533 contextOptions.fGpuPathRenderers = GrContextOptions::GpuPathRenderers::kNone;
1534
1535 contextOptions.fExecutor = fExecutor.get();
1536 Error err = this->onDraw(src, dst, wStream, log, contextOptions);
1537 if (!err.isEmpty() || !dst) {
1538 return err;
1539 }
1540
1541 SkBitmap reference;
1542 SkString refLog;
1543 SkDynamicMemoryWStream refStream;
1544 contextOptions.fExecutor = nullptr;
1545 Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1546 if (!refErr.isEmpty()) {
1547 return refErr;
1548 }
1549
1550 // The dimensions are a property of the Src only, and so should be identical.
1551 SkASSERT(reference.getSize() == dst->getSize());
1552 if (reference.getSize() != dst->getSize()) {
1553 return "Dimensions don't match reference";
1554 }
1555 // All SkBitmaps in DM are tight, so this comparison is easy.
1556 if (0 != memcmp(reference.getPixels(), dst->getPixels(), reference.getSize())) {
1557 return "Pixels don't match reference";
1558 }
1559 return "";
1560}
1561
1562/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1563
halcanary47ef4d52015-03-03 09:13:09 -08001564static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
halcanary4ba051c2016-03-10 10:31:53 -08001565 if (src.size().isEmpty()) {
1566 return "Source has empty dimensions";
1567 }
halcanary47ef4d52015-03-03 09:13:09 -08001568 SkASSERT(doc);
halcanary45420a92016-06-02 12:41:14 -07001569 int pageCount = src.pageCount();
1570 for (int i = 0; i < pageCount; ++i) {
1571 int width = src.size(i).width(), height = src.size(i).height();
halcanary7e798182015-04-14 14:06:18 -07001572 SkCanvas* canvas =
1573 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1574 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -07001575 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -07001576 }
halcanary45420a92016-06-02 12:41:14 -07001577 Error err = src.draw(i, canvas);
halcanary7e798182015-04-14 14:06:18 -07001578 if (!err.isEmpty()) {
1579 return err;
1580 }
1581 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -08001582 }
reedd14df7c2016-09-22 14:12:46 -07001583 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -08001584 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -08001585 return "";
1586}
1587
halcanary47ef4d52015-03-03 09:13:09 -08001588Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary4b656662016-04-27 07:45:18 -07001589 SkDocument::PDFMetadata metadata;
1590 metadata.fTitle = src.name();
1591 metadata.fSubject = "rendering correctness test";
1592 metadata.fCreator = "Skia/DM";
Hal Canaryc7d295e2017-07-20 15:36:00 -04001593 sk_sp<SkDocument> doc = SkDocument::MakePDF(dst, fRasterDpi, metadata, nullptr, fPDFA);
halcanary47ef4d52015-03-03 09:13:09 -08001594 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001595 return "SkDocument::MakePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001596 }
1597 return draw_skdocument(src, doc.get(), dst);
1598}
1599
1600/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1601
1602XPSSink::XPSSink() {}
1603
Hal Canaryabc88d22017-02-06 09:26:49 -05001604#ifdef SK_BUILD_FOR_WIN
Hal Canary5e221e72017-02-06 09:51:42 -05001605static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() {
1606 IXpsOMObjectFactory* factory;
1607 HRN(CoCreateInstance(CLSID_XpsOMObjectFactory,
1608 nullptr,
1609 CLSCTX_INPROC_SERVER,
1610 IID_PPV_ARGS(&factory)));
1611 return SkTScopedComPtr<IXpsOMObjectFactory>(factory);
1612}
1613
1614Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
Hal Canaryabc88d22017-02-06 09:26:49 -05001615 SkAutoCoInitialize com;
1616 if (!com.succeeded()) {
1617 return "Could not initialize COM.";
1618 }
Hal Canary5e221e72017-02-06 09:51:42 -05001619 SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory();
1620 if (!factory) {
1621 return "Failed to create XPS Factory.";
1622 }
1623 sk_sp<SkDocument> doc(SkDocument::MakeXPS(dst, factory.get()));
halcanary47ef4d52015-03-03 09:13:09 -08001624 if (!doc) {
halcanary4b656662016-04-27 07:45:18 -07001625 return "SkDocument::MakeXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -08001626 }
1627 return draw_skdocument(src, doc.get(), dst);
1628}
Hal Canary5e221e72017-02-06 09:51:42 -05001629#else
1630Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1631 return "XPS not supported on this platform.";
1632}
1633#endif
reed54dc4872016-09-13 08:09:45 -07001634
1635/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1636
1637PipeSink::PipeSink() {}
1638
1639Error PipeSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1640 return src.draw(SkPipeSerializer().beginWrite(SkRect::Make(src.size()), dst));
1641}
1642
mtklein748ca3b2015-01-15 10:56:12 -08001643/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1644
mtklein9c3f17d2015-01-28 11:35:18 -08001645SKPSink::SKPSink() {}
1646
mtkleinb9eb4ac2015-02-02 18:26:03 -08001647Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -08001648 SkSize size;
1649 size = src.size();
1650 SkPictureRecorder recorder;
1651 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
1652 if (!err.isEmpty()) {
1653 return err;
1654 }
reedca2622b2016-03-18 07:25:55 -07001655 recorder.finishRecordingAsPicture()->serialize(dst);
mtklein9c3f17d2015-01-28 11:35:18 -08001656 return "";
1657}
1658
1659/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1660
Hal Canary85c7fe82016-10-25 10:33:27 -04001661Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1662 SkDebugCanvas debugCanvas(src.size().width(), src.size().height());
1663 Error err = src.draw(&debugCanvas);
1664 if (!err.isEmpty()) {
1665 return err;
1666 }
Mike Reed5df49342016-11-12 08:06:55 -06001667 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
Hal Canary85c7fe82016-10-25 10:33:27 -04001668 UrlDataManager dataManager(SkString("data"));
1669 Json::Value json = debugCanvas.toJSON(
1670 dataManager, debugCanvas.getSize(), nullCanvas.get());
1671 std::string value = Json::StyledWriter().write(json);
1672 return dst->write(value.c_str(), value.size()) ? "" : "SkWStream Error";
1673}
1674
1675/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1676
mtklein8a4527e2015-01-31 20:00:58 -08001677SVGSink::SVGSink() {}
1678
mtkleinb9eb4ac2015-02-02 18:26:03 -08001679Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita718df0a2016-07-15 10:33:29 -07001680#if defined(SK_XML)
Ben Wagner145dbcd2016-11-03 14:40:50 -04001681 std::unique_ptr<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
Mike Reed5df49342016-11-12 08:06:55 -06001682 return src.draw(SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
1683 SkIntToScalar(src.size().height())),
1684 xmlWriter.get()).get());
fmalita718df0a2016-07-15 10:33:29 -07001685#else
1686 return Error("SVG sink is disabled.");
1687#endif // SK_XML
mtklein8a4527e2015-01-31 20:00:58 -08001688}
1689
1690/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1691
brianosmanb109b8c2016-06-16 13:03:24 -07001692RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
mtklein27c3fdd2016-02-26 14:43:21 -08001693 : fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -07001694 , fColorSpace(std::move(colorSpace)) {}
mtklein748ca3b2015-01-15 10:56:12 -08001695
mtkleinb9eb4ac2015-02-02 18:26:03 -08001696Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -08001697 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001698 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
1699 SkAlphaType alphaType = kPremul_SkAlphaType;
1700 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
1701
Mike Reed086a4272017-07-18 10:53:11 -04001702 dst->allocPixelsFlags(SkImageInfo::Make(size.width(), size.height(),
1703 fColorType, alphaType, fColorSpace),
1704 SkBitmap::kZeroPixels_AllocFlag);
mtklein748ca3b2015-01-15 10:56:12 -08001705 SkCanvas canvas(*dst);
1706 return src.draw(&canvas);
1707}
1708
1709/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1710
mtkleina16e69e2015-05-05 11:38:45 -07001711// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -07001712// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -07001713// Several examples below.
1714
mtkleincbf89782016-02-19 14:27:14 -08001715template <typename Fn>
msarett62d3b102015-12-10 15:14:27 -08001716static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleincbf89782016-02-19 14:27:14 -08001717 SkISize size, const Fn& draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001718 class ProxySrc : public Src {
1719 public:
mtkleincbf89782016-02-19 14:27:14 -08001720 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001721 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
halcanaryb4a7f142016-03-30 08:31:27 -07001722 Name name() const override { return "ProxySrc"; }
1723 SkISize size() const override { return fSize; }
mtkleina16e69e2015-05-05 11:38:45 -07001724 private:
mtkleincbf89782016-02-19 14:27:14 -08001725 SkISize fSize;
1726 const Fn& fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001727 };
msarett62d3b102015-12-10 15:14:27 -08001728 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001729}
1730
1731/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1732
mtklein4a34ecb2016-01-08 10:19:35 -08001733DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1734
1735// Is *bitmap identical to what you get drawing src into sink?
1736static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1737 // We can only check raster outputs.
1738 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1739 if (FLAGS_check && bitmap) {
1740 SkBitmap reference;
1741 SkString log;
halcanaryb4a7f142016-03-30 08:31:27 -07001742 SkDynamicMemoryWStream wStream;
1743 Error err = sink->draw(src, &reference, &wStream, &log);
mtklein4a34ecb2016-01-08 10:19:35 -08001744 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1745 SkASSERT(err.isEmpty());
1746 if (!err.isEmpty()) {
1747 return err;
1748 }
1749 // The dimensions are a property of the Src only, and so should be identical.
1750 SkASSERT(reference.getSize() == bitmap->getSize());
1751 if (reference.getSize() != bitmap->getSize()) {
1752 return "Dimensions don't match reference";
1753 }
Brian Osmanf9810662017-08-30 10:02:10 -04001754 // All SkBitmaps in DM are tight, so this comparison is easy.
mtklein4a34ecb2016-01-08 10:19:35 -08001755 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1756 return "Pixels don't match reference";
1757 }
1758 }
1759 return "";
1760}
1761
1762/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1763
mtkleind603b222015-02-17 11:13:33 -08001764static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1765 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1766 matrix->mapRect(&bounds);
1767 matrix->postTranslate(-bounds.x(), -bounds.y());
Hal Canaryfafe1352017-04-11 12:12:02 -04001768 return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())};
mtkleind603b222015-02-17 11:13:33 -08001769}
1770
msarett62d3b102015-12-10 15:14:27 -08001771ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001772
mtkleinb9eb4ac2015-02-02 18:26:03 -08001773Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001774 SkMatrix matrix = fMatrix;
1775 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
Ben Wagner145dbcd2016-11-03 14:40:50 -04001776 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001777 canvas->concat(matrix);
1778 return src.draw(canvas);
1779 });
mtklein748ca3b2015-01-15 10:56:12 -08001780}
1781
mtkleind603b222015-02-17 11:13:33 -08001782// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1783// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001784ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001785
1786Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1787 Error err = fSink->draw(src, bitmap, stream, log);
1788 if (!err.isEmpty()) {
1789 return err;
1790 }
1791
1792 SkMatrix inverse;
1793 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1794 return "Cannot upright --matrix.";
1795 }
1796 SkMatrix upright = SkMatrix::I();
1797 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1798 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1799 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1800 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1801
1802 SkBitmap uprighted;
1803 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1804 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1805
1806 SkCanvas canvas(uprighted);
1807 canvas.concat(upright);
1808 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07001809 paint.setBlendMode(SkBlendMode::kSrc);
mtkleind603b222015-02-17 11:13:33 -08001810 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1811
1812 *bitmap = uprighted;
mtkleind603b222015-02-17 11:13:33 -08001813 return "";
1814}
1815
mtklein748ca3b2015-01-15 10:56:12 -08001816/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1817
mtkleina16e69e2015-05-05 11:38:45 -07001818Error ViaSerialization::draw(
1819 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001820 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001821 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001822 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001823 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1824 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001825 if (!err.isEmpty()) {
1826 return err;
1827 }
reedca2622b2016-03-18 07:25:55 -07001828 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001829
1830 // Serialize it and then deserialize it.
reed39eaf5f2016-09-15 07:19:35 -07001831 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
mtklein748ca3b2015-01-15 10:56:12 -08001832
Ben Wagner145dbcd2016-11-03 14:40:50 -04001833 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001834 canvas->drawPicture(deserialized);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001835 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07001836 });
mtklein748ca3b2015-01-15 10:56:12 -08001837}
1838
1839/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1840
msarett62d3b102015-12-10 15:14:27 -08001841ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1842 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001843 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001844 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001845 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001846
mtkleinb9eb4ac2015-02-02 18:26:03 -08001847Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001848 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001849 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001850 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1851 SkIntToScalar(size.height()),
1852 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001853 if (!err.isEmpty()) {
1854 return err;
1855 }
reedca2622b2016-03-18 07:25:55 -07001856 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001857
Ben Wagner145dbcd2016-11-03 14:40:50 -04001858 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001859 const int xTiles = (size.width() + fW - 1) / fW,
1860 yTiles = (size.height() + fH - 1) / fH;
1861 SkMultiPictureDraw mpd(xTiles*yTiles);
reede8f30622016-03-23 18:59:25 -07001862 SkTArray<sk_sp<SkSurface>> surfaces;
1863// surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001864
mtkleina16e69e2015-05-05 11:38:45 -07001865 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1866 for (int j = 0; j < yTiles; j++) {
1867 for (int i = 0; i < xTiles; i++) {
1868 // This lets our ultimate Sink determine the best kind of surface.
1869 // E.g., if it's a GpuSink, the surfaces and images are textures.
reede8f30622016-03-23 18:59:25 -07001870 auto s = canvas->makeSurface(info);
mtkleina16e69e2015-05-05 11:38:45 -07001871 if (!s) {
reede8f30622016-03-23 18:59:25 -07001872 s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001873 }
reede8f30622016-03-23 18:59:25 -07001874 surfaces.push_back(s);
mtkleina16e69e2015-05-05 11:38:45 -07001875 SkCanvas* c = s->getCanvas();
1876 c->translate(SkIntToScalar(-i * fW),
1877 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
reedca2622b2016-03-18 07:25:55 -07001878 mpd.add(c, pic.get());
mtklein748ca3b2015-01-15 10:56:12 -08001879 }
mtklein748ca3b2015-01-15 10:56:12 -08001880 }
mtkleina16e69e2015-05-05 11:38:45 -07001881 mpd.draw();
1882 for (int j = 0; j < yTiles; j++) {
1883 for (int i = 0; i < xTiles; i++) {
reed9ce9d672016-03-17 10:51:11 -07001884 sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot());
mtkleina16e69e2015-05-05 11:38:45 -07001885 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1886 }
1887 }
mtkleina16e69e2015-05-05 11:38:45 -07001888 return "";
1889 });
mtklein748ca3b2015-01-15 10:56:12 -08001890}
1891
mtkleinb7e8d692015-04-07 08:30:32 -07001892/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1893
mtklein4a34ecb2016-01-08 10:19:35 -08001894Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1895 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001896 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtklein4a34ecb2016-01-08 10:19:35 -08001897 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001898 sk_sp<SkPicture> pic;
mtklein4a34ecb2016-01-08 10:19:35 -08001899 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1900 SkIntToScalar(size.height())));
1901 if (!err.isEmpty()) {
1902 return err;
1903 }
reedca2622b2016-03-18 07:25:55 -07001904 pic = recorder.finishRecordingAsPicture();
mtklein4a34ecb2016-01-08 10:19:35 -08001905 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001906 return check_against_reference(bitmap, src, fSink.get());
mtklein4a34ecb2016-01-08 10:19:35 -08001907 });
1908}
1909
1910/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1911
reedbabc3de2016-07-08 08:43:27 -07001912Error ViaDefer::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1913 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001914 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
Herb Derby1a3c4b82017-03-17 15:51:53 -04001915 SkDeferredCanvas deferred(canvas, SkDeferredCanvas::kEager);
reedbabc3de2016-07-08 08:43:27 -07001916 return src.draw(&deferred);
1917 });
1918}
1919
1920/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1921
reed54dc4872016-09-13 08:09:45 -07001922Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1923 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001924 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
reed54dc4872016-09-13 08:09:45 -07001925 SkDynamicMemoryWStream tmpStream;
1926 Error err = src.draw(SkPipeSerializer().beginWrite(SkRect::Make(size), &tmpStream));
1927 if (!err.isEmpty()) {
1928 return err;
1929 }
1930 sk_sp<SkData> data = tmpStream.detachAsData();
1931 SkPipeDeserializer().playback(data->data(), data->size(), canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001932 return check_against_reference(bitmap, src, fSink.get());
reed54dc4872016-09-13 08:09:45 -07001933 });
1934}
1935
1936/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1937
mtkleinb7e8d692015-04-07 08:30:32 -07001938// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1939// This tests that any shortcuts we may take while recording that second picture are legal.
1940Error ViaSecondPicture::draw(
1941 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001942 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04001943 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001944 SkPictureRecorder recorder;
reedca2622b2016-03-18 07:25:55 -07001945 sk_sp<SkPicture> pic;
mtkleina16e69e2015-05-05 11:38:45 -07001946 for (int i = 0; i < 2; i++) {
1947 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1948 SkIntToScalar(size.height())));
1949 if (!err.isEmpty()) {
1950 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001951 }
reedca2622b2016-03-18 07:25:55 -07001952 pic = recorder.finishRecordingAsPicture();
mtkleinb7e8d692015-04-07 08:30:32 -07001953 }
mtkleina16e69e2015-05-05 11:38:45 -07001954 canvas->drawPicture(pic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001955 return check_against_reference(bitmap, src, fSink.get());
mtkleina16e69e2015-05-05 11:38:45 -07001956 });
mtkleinb7e8d692015-04-07 08:30:32 -07001957}
1958
mtkleind31c13d2015-05-05 12:59:56 -07001959/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1960
mtklein6fbf4b32015-05-06 11:35:40 -07001961// Draw the Src twice. This can help exercise caching.
1962Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001963 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001964 for (int i = 0; i < 2; i++) {
1965 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1966 canvas->clear(SK_ColorTRANSPARENT);
1967 Error err = src.draw(canvas);
1968 if (err.isEmpty()) {
1969 return err;
1970 }
1971 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001972 return check_against_reference(bitmap, src, fSink.get());
mtklein6fbf4b32015-05-06 11:35:40 -07001973 });
1974}
1975
1976/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1977
Mike Reedbae888e2017-02-18 16:50:45 -05001978#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -05001979#include "SkXMLWriter.h"
1980#include "SkSVGCanvas.h"
1981#include "SkSVGDOM.h"
1982
1983Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1984 auto size = src.size();
1985 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1986 SkDynamicMemoryWStream wstream;
1987 SkXMLStreamWriter writer(&wstream);
1988 Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get());
1989 if (!err.isEmpty()) {
1990 return err;
1991 }
1992 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
1993 auto dom = SkSVGDOM::MakeFromStream(*rstream);
1994 if (dom) {
1995 dom->setContainerSize(SkSize::Make(size));
1996 dom->render(canvas);
1997 }
1998 return "";
1999 });
2000}
Mike Reedbae888e2017-02-18 16:50:45 -05002001#endif
Mike Reedf67c4592017-02-17 17:06:11 -05002002
2003/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2004
mtkleind31c13d2015-05-05 12:59:56 -07002005// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
2006// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
2007// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
2008struct DrawsAsSingletonPictures {
2009 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07002010 const SkDrawableList& fDrawables;
mtklein11064df2016-04-06 15:01:57 -07002011 SkRect fBounds;
mtkleind31c13d2015-05-05 12:59:56 -07002012
mtkleind31c13d2015-05-05 12:59:56 -07002013 template <typename T>
2014 void draw(const T& op, SkCanvas* canvas) {
2015 // We must pass SkMatrix::I() as our initial matrix.
2016 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
2017 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07002018 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
2019 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07002020 }
2021
mtklein449d9b72015-09-28 10:33:02 -07002022 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07002023 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07002024 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07002025 SkPictureRecorder rec;
mtklein11064df2016-04-06 15:01:57 -07002026 this->draw(op, rec.beginRecording(fBounds));
reedca2622b2016-03-18 07:25:55 -07002027 sk_sp<SkPicture> pic(rec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07002028 fCanvas->drawPicture(pic);
2029 }
2030
mtklein449d9b72015-09-28 10:33:02 -07002031 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07002032 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07002033 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
2034 this->draw(op, fCanvas);
2035 }
mtkleind31c13d2015-05-05 12:59:56 -07002036};
2037
mtkleind31c13d2015-05-05 12:59:56 -07002038// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
2039// Then play back that macro picture into our wrapped sink.
2040Error ViaSingletonPictures::draw(
2041 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2042 auto size = src.size();
Ben Wagner145dbcd2016-11-03 14:40:50 -04002043 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07002044 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
2045 SkRecord skr;
2046 SkRecorder recorder(&skr, size.width(), size.height());
2047 Error err = src.draw(&recorder);
2048 if (!err.isEmpty()) {
2049 return err;
2050 }
2051
2052 // Record our macro-picture, with each draw op as its own sub-picture.
2053 SkPictureRecorder macroRec;
2054 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
2055 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07002056
bungeman6bd52842016-10-27 09:30:08 -07002057 std::unique_ptr<SkDrawableList> drawables(recorder.detachDrawableList());
mtkleind2baa902015-07-07 09:43:28 -07002058 const SkDrawableList empty;
2059
2060 DrawsAsSingletonPictures drawsAsSingletonPictures = {
2061 macroCanvas,
2062 drawables ? *drawables : empty,
mtklein11064df2016-04-06 15:01:57 -07002063 SkRect::MakeWH((SkScalar)size.width(), (SkScalar)size.height()),
mtkleind2baa902015-07-07 09:43:28 -07002064 };
mtkleinc6ad06a2015-08-19 09:51:00 -07002065 for (int i = 0; i < skr.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -07002066 skr.visit(i, drawsAsSingletonPictures);
mtkleind31c13d2015-05-05 12:59:56 -07002067 }
reedca2622b2016-03-18 07:25:55 -07002068 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture());
mtkleind31c13d2015-05-05 12:59:56 -07002069
2070 canvas->drawPicture(macroPic);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002071 return check_against_reference(bitmap, src, fSink.get());
mtkleind31c13d2015-05-05 12:59:56 -07002072 });
2073}
2074
mtklein9c5052f2016-08-06 12:51:51 -07002075/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2076
2077Error ViaLite::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2078 auto size = src.size();
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002079 SkIRect bounds = {0,0, size.width(), size.height()};
Ben Wagner145dbcd2016-11-03 14:40:50 -04002080 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002081 SkLiteDL dl;
mtklein8bbbb692016-08-15 12:56:00 -07002082 SkLiteRecorder rec;
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002083 rec.reset(&dl, bounds);
mtklein8bbbb692016-08-15 12:56:00 -07002084
2085 Error err = src.draw(&rec);
mtklein9c5052f2016-08-06 12:51:51 -07002086 if (!err.isEmpty()) {
2087 return err;
2088 }
Derek Sollenbergerd7875f52017-03-01 15:33:23 -05002089 dl.draw(canvas);
Ben Wagner145dbcd2016-11-03 14:40:50 -04002090 return check_against_reference(bitmap, src, fSink.get());
mtklein9c5052f2016-08-06 12:51:51 -07002091 });
2092}
2093
Mike Klein841101d2017-03-10 09:55:51 -05002094/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2095
Mike Klein919cc452017-03-18 15:36:52 +00002096ViaCSXform::ViaCSXform(Sink* sink, sk_sp<SkColorSpace> cs, bool colorSpin)
2097 : Via(sink)
2098 , fCS(std::move(cs))
2099 , fColorSpin(colorSpin) {}
Mike Klein841101d2017-03-10 09:55:51 -05002100
2101Error ViaCSXform::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2102 return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(),
2103 [&](SkCanvas* canvas) -> Error {
2104 auto proxy = SkCreateColorSpaceXformCanvas(canvas, fCS);
Mike Klein919cc452017-03-18 15:36:52 +00002105 Error err = src.draw(proxy.get());
2106 if (!err.isEmpty()) {
2107 return err;
2108 }
2109
2110 // Undo the color spin, so we can look at the pixels in Gold.
2111 if (fColorSpin) {
2112 SkBitmap pixels;
2113 pixels.allocPixels(canvas->imageInfo());
Mike Reed12e946b2017-04-17 10:53:29 -04002114 canvas->readPixels(pixels, 0, 0);
Mike Klein919cc452017-03-18 15:36:52 +00002115 for (int y = 0; y < pixels.height(); y++) {
2116 for (int x = 0; x < pixels.width(); x++) {
2117 uint32_t pixel = *pixels.getAddr32(x, y);
2118 uint8_t r = SkGetPackedR32(pixel);
2119 uint8_t g = SkGetPackedG32(pixel);
2120 uint8_t b = SkGetPackedB32(pixel);
2121 uint8_t a = SkGetPackedA32(pixel);
2122 *pixels.getAddr32(x, y) =
2123 SkSwizzle_RGBA_to_PMColor(b << 0 | r << 8 | g << 16 | a << 24);
2124 }
2125 }
2126
2127 canvas->writePixels(pixels, 0, 0);
2128 }
2129
2130 return "";
Mike Klein841101d2017-03-10 09:55:51 -05002131 });
2132}
2133
mtklein748ca3b2015-01-15 10:56:12 -08002134} // namespace DM