blob: 9712fd73a024d2723dd3fa30d0c7956fdbd0f823 [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMSrcSink.h"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
scroggof24f2242015-03-03 08:59:20 -080010#include "SkCodec.h"
msarettb714fb02016-01-22 14:46:42 -080011#include "SkCodecImageGenerator.h"
mtkleina16e69e2015-05-05 11:38:45 -070012#include "SkCommonFlags.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070013#include "SkData.h"
mtklein748ca3b2015-01-15 10:56:12 -080014#include "SkDocument.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080015#include "SkError.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070016#include "SkImageGenerator.h"
mtkleinc8be09a2016-01-04 18:56:57 -080017#include "SkMallocPixelRef.h"
mtklein748ca3b2015-01-15 10:56:12 -080018#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080019#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080020#include "SkOSFile.h"
mtkleinffa901a2015-03-16 10:38:07 -070021#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080022#include "SkPictureRecorder.h"
23#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070024#include "SkRecordDraw.h"
25#include "SkRecorder.h"
mtklein2e2ea382015-10-16 10:29:41 -070026#include "SkRemote.h"
fmalita2aafe6f2015-02-06 12:51:10 -080027#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080028#include "SkStream.h"
mtklein449d9b72015-09-28 10:33:02 -070029#include "SkTLogic.h"
fmalita2aafe6f2015-02-06 12:51:10 -080030#include "SkXMLWriter.h"
msarette6dd0042015-10-09 11:07:34 -070031#include "SkSwizzler.h"
mtklein64593522015-11-12 10:41:05 -080032#include <functional>
mtklein748ca3b2015-01-15 10:56:12 -080033
halcanary7e798182015-04-14 14:06:18 -070034DEFINE_bool(multiPage, false, "For document-type backends, render the source"
35 " into multiple pages");
36
mtkleinb3e5e4d2015-03-25 13:13:43 -070037static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) {
38 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size));
reedd1146452015-09-25 06:56:57 -070039 return encoded && SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst);
mtkleinb3e5e4d2015-03-25 13:13:43 -070040}
41
mtklein748ca3b2015-01-15 10:56:12 -080042namespace DM {
43
mtklein748ca3b2015-01-15 10:56:12 -080044GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
45
46Error GMSrc::draw(SkCanvas* canvas) const {
halcanary96fcdcc2015-08-27 07:41:13 -070047 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080048 canvas->concat(gm->getInitialTransform());
49 gm->draw(canvas);
50 return "";
51}
52
53SkISize GMSrc::size() const {
halcanary96fcdcc2015-08-27 07:41:13 -070054 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080055 return gm->getISize();
56}
57
58Name GMSrc::name() const {
halcanary96fcdcc2015-08-27 07:41:13 -070059 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080060 return gm->getName();
61}
62
bsalomon4ee6bd82015-05-27 13:23:23 -070063void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
halcanary96fcdcc2015-08-27 07:41:13 -070064 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070065 gm->modifyGrContextOptions(options);
66}
67
mtklein748ca3b2015-01-15 10:56:12 -080068/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
69
msarett5cb48852015-11-06 08:56:32 -080070BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode,
msaretta5783ae2015-09-08 15:35:32 -070071 CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
72 : fPath(path)
73 , fStrategy(strategy)
74 , fMode(mode)
75 , fDstColorType(dstColorType)
76 , fSampleSize(sampleSize)
77{}
78
79bool BRDSrc::veto(SinkFlags flags) const {
80 // No need to test to non-raster or indirect backends.
81 return flags.type != SinkFlags::kRaster
82 || flags.approach != SinkFlags::kDirect;
83}
84
msarett5cb48852015-11-06 08:56:32 -080085static SkBitmapRegionDecoder* create_brd(Path path,
86 SkBitmapRegionDecoder::Strategy strategy) {
msaretta5783ae2015-09-08 15:35:32 -070087 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
88 if (!encoded) {
89 return NULL;
90 }
msarett5cb48852015-11-06 08:56:32 -080091 return SkBitmapRegionDecoder::Create(encoded, strategy);
msaretta5783ae2015-09-08 15:35:32 -070092}
93
94Error BRDSrc::draw(SkCanvas* canvas) const {
95 SkColorType colorType = canvas->imageInfo().colorType();
96 if (kRGB_565_SkColorType == colorType &&
97 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
98 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
99 }
100 switch (fDstColorType) {
101 case CodecSrc::kGetFromCanvas_DstColorType:
102 break;
103 case CodecSrc::kIndex8_Always_DstColorType:
104 colorType = kIndex_8_SkColorType;
105 break;
106 case CodecSrc::kGrayscale_Always_DstColorType:
107 colorType = kGray_8_SkColorType;
108 break;
109 }
110
msarett5cb48852015-11-06 08:56:32 -0800111 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700112 if (nullptr == brd.get()) {
113 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
114 }
115
msarett04965c62015-10-12 10:24:38 -0700116 if (!brd->conversionSupported(colorType)) {
mtklein9b439152015-12-09 13:02:26 -0800117 return Error::Nonfatal("Cannot convert to color type.");
msarett04965c62015-10-12 10:24:38 -0700118 }
119
msaretta5783ae2015-09-08 15:35:32 -0700120 const uint32_t width = brd->width();
121 const uint32_t height = brd->height();
122 // Visually inspecting very small output images is not necessary.
123 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
124 return Error::Nonfatal("Scaling very small images is uninteresting.");
125 }
126 switch (fMode) {
127 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700128 SkBitmap bitmap;
129 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
130 fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800131 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700132 }
133 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800134 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700135 }
msarett35e5d1b2015-10-27 12:50:25 -0700136 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700137 return "";
138 }
139 case kDivisor_Mode: {
140 const uint32_t divisor = 2;
141 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800142 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700143 }
144
145 // Use a border to test subsets that extend outside the image.
146 // We will not allow the border to be larger than the image dimensions. Allowing
147 // these large borders causes off by one errors that indicate a problem with the
148 // test suite, not a problem with the implementation.
149 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
150 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
151 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
152
153 // We may need to clear the canvas to avoid uninitialized memory.
154 // Assume we are scaling a 780x780 image with sampleSize = 8.
155 // The output image should be 97x97.
156 // Each subset will be 390x390.
157 // Each scaled subset be 48x48.
158 // Four scaled subsets will only fill a 96x96 image.
159 // The bottom row and last column will not be touched.
160 // This is an unfortunate result of our rounding rules when scaling.
161 // Maybe we need to consider testing scaled subsets without trying to
162 // combine them to match the full scaled image? Or maybe this is the
163 // best we can do?
164 canvas->clear(0);
165
166 for (uint32_t x = 0; x < divisor; x++) {
167 for (uint32_t y = 0; y < divisor; y++) {
168 // Calculate the subset dimensions
169 uint32_t subsetWidth = width / divisor;
170 uint32_t subsetHeight = height / divisor;
171 const int left = x * subsetWidth;
172 const int top = y * subsetHeight;
173
174 // Increase the size of the last subset in each row or column, when the
175 // divisor does not divide evenly into the image dimensions
176 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
177 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
178
179 // Increase the size of the subset in order to have a border on each side
180 const int decodeLeft = left - unscaledBorder;
181 const int decodeTop = top - unscaledBorder;
182 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
183 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700184 SkBitmap bitmap;
185 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
186 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800187 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700188 }
189 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800190 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700191 }
192
msarett35e5d1b2015-10-27 12:50:25 -0700193 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700194 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
195 (SkScalar) (subsetWidth / fSampleSize),
196 (SkScalar) (subsetHeight / fSampleSize)),
197 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
198 (SkScalar) (top / fSampleSize),
199 (SkScalar) (subsetWidth / fSampleSize),
200 (SkScalar) (subsetHeight / fSampleSize)),
201 nullptr);
202 }
203 }
204 return "";
205 }
206 default:
207 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800208 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700209 }
210}
211
212SkISize BRDSrc::size() const {
msarett5cb48852015-11-06 08:56:32 -0800213 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700214 if (brd) {
215 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
216 SkTMax(1, brd->height() / (int) fSampleSize));
217 }
218 return SkISize::Make(0, 0);
219}
220
221static SkString get_scaled_name(const Path& path, float scale) {
222 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
223}
224
225Name BRDSrc::name() const {
226 // We will replicate the names used by CodecSrc so that images can
227 // be compared in Gold.
228 if (1 == fSampleSize) {
229 return SkOSPath::Basename(fPath.c_str());
230 }
msarett4b0778e2015-11-13 09:59:11 -0800231 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700232}
233
234/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
235
scroggoc5560be2016-02-03 09:42:42 -0800236CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
237 float scale)
msarett438b2ad2015-04-09 12:43:10 -0700238 : fPath(path)
239 , fMode(mode)
240 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800241 , fDstAlphaType(dstAlphaType)
msarett0a242972015-06-11 14:27:27 -0700242 , fScale(scale)
msarett438b2ad2015-04-09 12:43:10 -0700243{}
mtklein748ca3b2015-01-15 10:56:12 -0800244
mtklein99cab4e2015-07-31 06:43:04 -0700245bool CodecSrc::veto(SinkFlags flags) const {
msarettb714fb02016-01-22 14:46:42 -0800246 // Test CodecImageGenerator on 8888, 565, and gpu
247 if (kGen_Mode == fMode) {
248 return (flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect) &&
249 flags.type != SinkFlags::kGPU;
250 }
251
252 // Test all other modes to direct raster backends (8888 and 565).
253 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700254}
scroggo9b77ddd2015-03-19 06:03:39 -0700255
scroggoc5560be2016-02-03 09:42:42 -0800256bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
257 CodecSrc::DstColorType dstColorType) {
msarett3d9d7a72015-10-21 10:27:10 -0700258 switch (dstColorType) {
259 case CodecSrc::kIndex8_Always_DstColorType:
260 if (kRGB_565_SkColorType == canvasColorType) {
261 return false;
262 }
scroggoc5560be2016-02-03 09:42:42 -0800263 *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700264 break;
265 case CodecSrc::kGrayscale_Always_DstColorType:
266 if (kRGB_565_SkColorType == canvasColorType) {
267 return false;
268 }
scroggoc5560be2016-02-03 09:42:42 -0800269 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700270 break;
271 default:
scroggoc5560be2016-02-03 09:42:42 -0800272 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
msarett3d9d7a72015-10-21 10:27:10 -0700273 break;
274 }
275
msarett3d9d7a72015-10-21 10:27:10 -0700276 return true;
277}
278
msarettb714fb02016-01-22 14:46:42 -0800279Error test_gen(SkCanvas* canvas, SkData* data) {
msarett01813e82016-01-25 10:51:29 -0800280 SkAutoTDelete<SkImageGenerator> gen = SkCodecImageGenerator::NewFromEncodedCodec(data);
msarettb714fb02016-01-22 14:46:42 -0800281 if (!gen) {
282 return "Could not create image generator.";
283 }
284
285 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
286 // Currently, we will avoid creating a CodecSrc for this case (see DM.cpp).
287 SkASSERT(kGray_8_SkColorType != gen->getInfo().colorType());
288
msarettd03b7ae2016-01-25 08:20:55 -0800289 if (kOpaque_SkAlphaType != gen->getInfo().alphaType() &&
290 kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
291 return Error::Nonfatal("Skip testing non-opaque images to 565.");
292 }
293
msarett01813e82016-01-25 10:51:29 -0800294 SkAutoTDelete<SkImage> image(SkImage::NewFromGenerator(gen.detach(), nullptr));
msarettb714fb02016-01-22 14:46:42 -0800295 if (!image) {
296 return "Could not create image from codec image generator.";
297 }
298
299 canvas->drawImage(image, 0, 0);
300 return "";
301}
302
mtkleine0effd62015-07-29 06:37:28 -0700303Error CodecSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -0800304 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800305 if (!encoded) {
306 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
307 }
msarettb714fb02016-01-22 14:46:42 -0800308
309 // The CodecImageGenerator test does not share much code with the other tests,
310 // so we will handle it in its own function.
311 if (kGen_Mode == fMode) {
312 return test_gen(canvas, encoded);
313 }
314
msarett3d9d7a72015-10-21 10:27:10 -0700315 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700316 if (nullptr == codec.get()) {
317 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700318 }
319
scroggoc5560be2016-02-03 09:42:42 -0800320 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
321 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700322 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
msarett438b2ad2015-04-09 12:43:10 -0700323 }
324
msarett0a242972015-06-11 14:27:27 -0700325 // Try to scale the image if it is desired
326 SkISize size = codec->getScaledDimensions(fScale);
327 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
328 return Error::Nonfatal("Test without scaling is uninteresting.");
329 }
msarettb32758a2015-08-18 13:22:46 -0700330
331 // Visually inspecting very small output images is not necessary. We will
332 // cover these cases in unit testing.
333 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
334 return Error::Nonfatal("Scaling very small images is uninteresting.");
335 }
msarett0a242972015-06-11 14:27:27 -0700336 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
337
msarett438b2ad2015-04-09 12:43:10 -0700338 // Construct a color table for the decode if necessary
halcanary96fcdcc2015-08-27 07:41:13 -0700339 SkAutoTUnref<SkColorTable> colorTable(nullptr);
340 SkPMColor* colorPtr = nullptr;
341 int* colorCountPtr = nullptr;
msarett438b2ad2015-04-09 12:43:10 -0700342 int maxColors = 256;
343 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
344 SkPMColor colors[256];
halcanary385fe4d2015-08-26 13:07:48 -0700345 colorTable.reset(new SkColorTable(colors, maxColors));
msarett438b2ad2015-04-09 12:43:10 -0700346 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
347 colorCountPtr = &maxColors;
348 }
349
scroggo9b77ddd2015-03-19 06:03:39 -0700350 SkBitmap bitmap;
msarettbb25b532016-01-13 09:31:39 -0800351 SkPixelRefFactory* factory = nullptr;
352 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
353 SkCodec::Options options;
354 if (kCodecZeroInit_Mode == fMode) {
355 factory = &zeroFactory;
356 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
357 }
358 if (!bitmap.tryAllocPixels(decodeInfo, factory, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800359 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
scroggo9b77ddd2015-03-19 06:03:39 -0700360 decodeInfo.width(), decodeInfo.height());
361 }
362
scroggo9c59ebc2015-03-25 13:48:49 -0700363 switch (fMode) {
msarettbb25b532016-01-13 09:31:39 -0800364 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700365 case kCodec_Mode: {
msarettbb25b532016-01-13 09:31:39 -0800366 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
msarett438b2ad2015-04-09 12:43:10 -0700367 colorPtr, colorCountPtr)) {
scroggoeb602a52015-07-09 08:16:03 -0700368 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700369 // We consider incomplete to be valid, since we should still decode what is
370 // available.
scroggoeb602a52015-07-09 08:16:03 -0700371 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700372 break;
scroggoeb602a52015-07-09 08:16:03 -0700373 case SkCodec::kInvalidConversion:
scroggo9c59ebc2015-03-25 13:48:49 -0700374 return Error::Nonfatal("Incompatible colortype conversion");
375 default:
376 // Everything else is considered a failure.
377 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
378 }
emmaleer97002062015-05-27 12:36:10 -0700379 canvas->drawBitmap(bitmap, 0, 0);
scroggo9c59ebc2015-03-25 13:48:49 -0700380 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700381 }
scroggo9c59ebc2015-03-25 13:48:49 -0700382 case kScanline_Mode: {
scroggo46c57472015-09-30 08:57:13 -0700383 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
384 colorCountPtr)) {
msarett9e707a02015-09-01 14:57:57 -0700385 return Error::Nonfatal("Could not start scanline decoder");
scroggo9c59ebc2015-03-25 13:48:49 -0700386 }
scroggo1c005e42015-08-04 09:24:45 -0700387
msarette6dd0042015-10-09 11:07:34 -0700388 void* dst = bitmap.getAddr(0, 0);
389 size_t rowBytes = bitmap.rowBytes();
390 uint32_t height = decodeInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700391 switch (codec->getScanlineOrder()) {
392 case SkCodec::kTopDown_SkScanlineOrder:
393 case SkCodec::kBottomUp_SkScanlineOrder:
394 case SkCodec::kNone_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700395 // We do not need to check the return value. On an incomplete
396 // image, memory will be filled with a default value.
397 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700398 break;
scroggo46c57472015-09-30 08:57:13 -0700399 case SkCodec::kOutOfOrder_SkScanlineOrder: {
msarett10522ff2015-09-07 08:54:01 -0700400 for (int y = 0; y < decodeInfo.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700401 int dstY = codec->outputScanline(y);
msarett10522ff2015-09-07 08:54:01 -0700402 void* dstPtr = bitmap.getAddr(0, dstY);
msarette6dd0042015-10-09 11:07:34 -0700403 // We complete the loop, even if this call begins to fail
404 // due to an incomplete image. This ensures any uninitialized
405 // memory will be filled with the proper value.
406 codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
msarett10522ff2015-09-07 08:54:01 -0700407 }
408 break;
409 }
410 }
411
emmaleer97002062015-05-27 12:36:10 -0700412 canvas->drawBitmap(bitmap, 0, 0);
413 break;
414 }
msarett0a242972015-06-11 14:27:27 -0700415 case kStripe_Mode: {
416 const int height = decodeInfo.height();
417 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
418 // does not align with image blocks.
419 const int stripeHeight = 37;
420 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
421
422 // Decode odd stripes
scroggo46c57472015-09-30 08:57:13 -0700423 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
424 colorCountPtr)
425 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
msarett9e707a02015-09-01 14:57:57 -0700426 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
427 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
428 // to run this test for image types that do not have this scanline ordering.
msarett5406d6f2015-08-31 06:55:13 -0700429 return Error::Nonfatal("Could not start top-down scanline decoder");
msarett0a242972015-06-11 14:27:27 -0700430 }
msarette6dd0042015-10-09 11:07:34 -0700431
msarett0a242972015-06-11 14:27:27 -0700432 for (int i = 0; i < numStripes; i += 2) {
433 // Skip a stripe
434 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700435 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700436
437 // Read a stripe
438 const int startY = (i + 1) * stripeHeight;
439 const int linesToRead = SkTMin(stripeHeight, height - startY);
440 if (linesToRead > 0) {
msarette6dd0042015-10-09 11:07:34 -0700441 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700442 }
443 }
444
445 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700446 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
447 colorPtr, colorCountPtr);
scroggo1c005e42015-08-04 09:24:45 -0700448 if (SkCodec::kSuccess != startResult) {
449 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700450 }
451 for (int i = 0; i < numStripes; i += 2) {
452 // Read a stripe
453 const int startY = i * stripeHeight;
454 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarette6dd0042015-10-09 11:07:34 -0700455 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700456
457 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700458 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
459 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700460 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700461 }
462 }
463 canvas->drawBitmap(bitmap, 0, 0);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700464 break;
msarett0a242972015-06-11 14:27:27 -0700465 }
scroggob636b452015-07-22 07:16:20 -0700466 case kSubset_Mode: {
467 // Arbitrarily choose a divisor.
468 int divisor = 2;
469 // Total width/height of the image.
470 const int W = codec->getInfo().width();
471 const int H = codec->getInfo().height();
472 if (divisor > W || divisor > H) {
473 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
474 "for %s with dimensions (%d x %d)", divisor,
475 fPath.c_str(), W, H));
476 }
477 // subset dimensions
478 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
479 const int w = SkAlign2(W / divisor);
480 const int h = SkAlign2(H / divisor);
481 SkIRect subset;
482 SkCodec::Options opts;
483 opts.fSubset = &subset;
484 SkBitmap subsetBm;
485 // We will reuse pixel memory from bitmap.
486 void* pixels = bitmap.getPixels();
487 // Keep track of left and top (for drawing subsetBm into canvas). We could use
488 // fScale * x and fScale * y, but we want integers such that the next subset will start
489 // where the last one ended. So we'll add decodeInfo.width() and height().
490 int left = 0;
491 for (int x = 0; x < W; x += w) {
492 int top = 0;
493 for (int y = 0; y < H; y+= h) {
494 // Do not make the subset go off the edge of the image.
495 const int preScaleW = SkTMin(w, W - x);
496 const int preScaleH = SkTMin(h, H - y);
497 subset.setXYWH(x, y, preScaleW, preScaleH);
498 // And scale
499 // FIXME: Should we have a version of getScaledDimensions that takes a subset
500 // into account?
msarette6dd0042015-10-09 11:07:34 -0700501 decodeInfo = decodeInfo.makeWH(
502 SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)),
503 SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)));
scroggob636b452015-07-22 07:16:20 -0700504 size_t rowBytes = decodeInfo.minRowBytes();
505 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700506 nullptr, nullptr)) {
scroggob636b452015-07-22 07:16:20 -0700507 return SkStringPrintf("could not install pixels for %s.", fPath.c_str());
508 }
509 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
510 &opts, colorPtr, colorCountPtr);
511 switch (result) {
512 case SkCodec::kSuccess:
513 case SkCodec::kIncompleteInput:
514 break;
515 case SkCodec::kInvalidConversion:
516 if (0 == (x|y)) {
517 // First subset is okay to return unimplemented.
518 return Error::Nonfatal("Incompatible colortype conversion");
519 }
520 // If the first subset succeeded, a later one should not fail.
521 // fall through to failure
522 case SkCodec::kUnimplemented:
523 if (0 == (x|y)) {
524 // First subset is okay to return unimplemented.
525 return Error::Nonfatal("subset codec not supported");
526 }
527 // If the first subset succeeded, why would a later one fail?
528 // fall through to failure
529 default:
530 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
531 "from %s with dimensions (%d x %d)\t error %d",
532 x, y, decodeInfo.width(), decodeInfo.height(),
533 fPath.c_str(), W, H, result);
534 }
535 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top));
536 // translate by the scaled height.
537 top += decodeInfo.height();
538 }
539 // translate by the scaled width.
540 left += decodeInfo.width();
541 }
542 return "";
543 }
msarettb714fb02016-01-22 14:46:42 -0800544 default:
545 SkASSERT(false);
546 return "Invalid fMode";
scroggo9b77ddd2015-03-19 06:03:39 -0700547 }
scroggo9c59ebc2015-03-25 13:48:49 -0700548 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700549}
550
551SkISize CodecSrc::size() const {
552 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700553 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700554 if (nullptr == codec) {
555 return SkISize::Make(0, 0);
556 }
557 return codec->getScaledDimensions(fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700558}
559
560Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700561 if (1.0f == fScale) {
562 return SkOSPath::Basename(fPath.c_str());
msarett0a242972015-06-11 14:27:27 -0700563 }
msaretta5783ae2015-09-08 15:35:32 -0700564 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700565}
566
567/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
568
msarett3d9d7a72015-10-21 10:27:10 -0700569AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800570 SkAlphaType dstAlphaType, int sampleSize)
msarett3d9d7a72015-10-21 10:27:10 -0700571 : fPath(path)
572 , fMode(mode)
573 , fDstColorType(dstColorType)
scroggoc5560be2016-02-03 09:42:42 -0800574 , fDstAlphaType(dstAlphaType)
msarett3d9d7a72015-10-21 10:27:10 -0700575 , fSampleSize(sampleSize)
576{}
577
578bool AndroidCodecSrc::veto(SinkFlags flags) const {
579 // No need to test decoding to non-raster or indirect backend.
msarett3d9d7a72015-10-21 10:27:10 -0700580 return flags.type != SinkFlags::kRaster
581 || flags.approach != SinkFlags::kDirect;
582}
583
584Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
585 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
586 if (!encoded) {
587 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
588 }
589 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
590 if (nullptr == codec.get()) {
591 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
592 }
593
scroggoc5560be2016-02-03 09:42:42 -0800594 SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType);
595 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) {
msarett3d9d7a72015-10-21 10:27:10 -0700596 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
597 }
598
599 // Scale the image if it is desired.
600 SkISize size = codec->getSampledDimensions(fSampleSize);
601
602 // Visually inspecting very small output images is not necessary. We will
603 // cover these cases in unit testing.
604 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
605 return Error::Nonfatal("Scaling very small images is uninteresting.");
606 }
607 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
608
609 // Construct a color table for the decode if necessary
610 SkAutoTUnref<SkColorTable> colorTable(nullptr);
611 SkPMColor* colorPtr = nullptr;
612 int* colorCountPtr = nullptr;
613 int maxColors = 256;
614 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
615 SkPMColor colors[256];
616 colorTable.reset(new SkColorTable(colors, maxColors));
617 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
618 colorCountPtr = &maxColors;
619 }
620
621 SkBitmap bitmap;
622 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800623 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
msarett3d9d7a72015-10-21 10:27:10 -0700624 decodeInfo.width(), decodeInfo.height());
625 }
626
627 // Create options for the codec.
628 SkAndroidCodec::AndroidOptions options;
629 options.fColorPtr = colorPtr;
630 options.fColorCount = colorCountPtr;
631 options.fSampleSize = fSampleSize;
632
633 switch (fMode) {
634 case kFullImage_Mode: {
635 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(),
636 &options)) {
637 case SkCodec::kSuccess:
638 case SkCodec::kIncompleteInput:
639 break;
640 case SkCodec::kInvalidConversion:
mtklein9b439152015-12-09 13:02:26 -0800641 return Error::Nonfatal("Cannot convert to requested color type.");
msarett3d9d7a72015-10-21 10:27:10 -0700642 default:
643 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
644 }
645 canvas->drawBitmap(bitmap, 0, 0);
646 return "";
647 }
648 case kDivisor_Mode: {
649 const int width = codec->getInfo().width();
650 const int height = codec->getInfo().height();
651 const int divisor = 2;
652 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800653 return Error::Nonfatal("Divisor is larger than image dimension.");
msarett3d9d7a72015-10-21 10:27:10 -0700654 }
655
msarettfa23a9e2015-10-21 13:26:59 -0700656 // Keep track of the final decoded dimensions.
657 int finalScaledWidth = 0;
658 int finalScaledHeight = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700659 for (int x = 0; x < divisor; x++) {
660 for (int y = 0; y < divisor; y++) {
661 // Calculate the subset dimensions
662 int subsetWidth = width / divisor;
663 int subsetHeight = height / divisor;
664 const int left = x * subsetWidth;
665 const int top = y * subsetHeight;
666
667 // Increase the size of the last subset in each row or column, when the
668 // divisor does not divide evenly into the image dimensions
669 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
670 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
671 SkIRect subset = SkIRect::MakeXYWH(left, top, subsetWidth, subsetHeight);
672 if (!codec->getSupportedSubset(&subset)) {
mtklein9b439152015-12-09 13:02:26 -0800673 return "Could not get supported subset to decode.";
msarett3d9d7a72015-10-21 10:27:10 -0700674 }
675 options.fSubset = &subset;
msarettfa23a9e2015-10-21 13:26:59 -0700676 const int scaledWidthOffset = subset.left() / fSampleSize;
677 const int scaledHeightOffset = subset.top() / fSampleSize;
678 void* pixels = bitmap.getAddr(scaledWidthOffset, scaledHeightOffset);
msarett3d9d7a72015-10-21 10:27:10 -0700679 SkISize scaledSubsetSize = codec->getSampledSubsetDimensions(fSampleSize,
680 subset);
681 SkImageInfo subsetDecodeInfo = decodeInfo.makeWH(scaledSubsetSize.width(),
682 scaledSubsetSize.height());
683
msarettfa23a9e2015-10-21 13:26:59 -0700684 if (x + 1 == divisor && y + 1 == divisor) {
685 finalScaledWidth = scaledWidthOffset + scaledSubsetSize.width();
686 finalScaledHeight = scaledHeightOffset + scaledSubsetSize.height();
687 }
688
msarett3d9d7a72015-10-21 10:27:10 -0700689 switch (codec->getAndroidPixels(subsetDecodeInfo, pixels, bitmap.rowBytes(),
690 &options)) {
691 case SkCodec::kSuccess:
692 case SkCodec::kIncompleteInput:
693 break;
694 case SkCodec::kInvalidConversion:
mtklein9b439152015-12-09 13:02:26 -0800695 return Error::Nonfatal("Cannot convert to requested color type.");
msarett3d9d7a72015-10-21 10:27:10 -0700696 default:
697 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
698 }
699 }
700 }
msarettfa23a9e2015-10-21 13:26:59 -0700701
702 SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth,
703 (SkScalar) finalScaledHeight);
704 canvas->drawBitmapRect(bitmap, rect, rect, nullptr);
msarett3d9d7a72015-10-21 10:27:10 -0700705 return "";
706 }
707 default:
708 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800709 return "Error: Should not be reached.";
msarett3d9d7a72015-10-21 10:27:10 -0700710 }
711}
712
713SkISize AndroidCodecSrc::size() const {
714 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
715 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
716 if (nullptr == codec) {
717 return SkISize::Make(0, 0);
718 }
719 return codec->getSampledDimensions(fSampleSize);
720}
721
722Name AndroidCodecSrc::name() const {
723 // We will replicate the names used by CodecSrc so that images can
724 // be compared in Gold.
725 if (1 == fSampleSize) {
726 return SkOSPath::Basename(fPath.c_str());
727 }
msarett4b0778e2015-11-13 09:59:11 -0800728 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700729}
730
731/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
732
mtkleinf4ba3212015-01-28 15:32:24 -0800733static const SkRect kSKPViewport = {0,0, 1000,1000};
734
mtklein8d17a132015-01-30 11:42:31 -0800735SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800736
737Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800738 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800739 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800740 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
741 }
mtkleinb3e5e4d2015-03-25 13:13:43 -0700742 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap));
mtklein75d98fd2015-01-18 07:05:01 -0800743 if (!pic) {
744 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
745 }
halcanary96fcdcc2015-08-27 07:41:13 -0700746 stream.reset((SkStream*)nullptr); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -0700747
mtkleinf4ba3212015-01-28 15:32:24 -0800748 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800749 canvas->drawPicture(pic);
750 return "";
751}
752
753SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700754 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
755 if (!stream) {
756 return SkISize::Make(0,0);
757 }
758 SkPictInfo info;
759 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
760 return SkISize::Make(0,0);
761 }
762 SkRect viewport = kSKPViewport;
763 if (!viewport.intersect(info.fCullRect)) {
764 return SkISize::Make(0,0);
765 }
766 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800767}
768
769Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
770
771/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
772
mtkleinad66f9b2015-02-13 15:11:10 -0800773Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
774 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
775 return src.draw(canvas);
776}
777
778/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
779
mtkleinb9eb4ac2015-02-02 18:26:03 -0800780DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
781
mtklein82d28432015-01-15 12:46:02 -0800782GPUSink::GPUSink(GrContextFactory::GLContextType ct,
kkinnunen5219fd92015-12-10 06:28:13 -0800783 GrContextFactory::GLContextOptions options,
mtklein82d28432015-01-15 12:46:02 -0800784 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -0700785 bool diText,
mtklein82d28432015-01-15 12:46:02 -0800786 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800787 : fContextType(ct)
kkinnunen5219fd92015-12-10 06:28:13 -0800788 , fContextOptions(options)
mtklein748ca3b2015-01-15 10:56:12 -0800789 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -0700790 , fUseDIText(diText)
mtklein82d28432015-01-15 12:46:02 -0800791 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800792
793int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800794 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800795}
796
joshualitt5f5a8d72015-02-25 14:09:45 -0800797void PreAbandonGpuContextErrorHandler(SkError, void*) {}
798
bsalomon648c6962015-10-23 09:06:59 -0700799DEFINE_bool(imm, false, "Run gpu configs in immediate mode.");
bsalomon69cfe952015-11-30 13:27:47 -0800800DEFINE_bool(batchClip, false, "Clip each GrBatch to its device bounds for testing.");
bsalomon6dea83f2015-12-03 12:58:06 -0800801DEFINE_bool(batchBounds, false, "Draw a wireframe bounds of each GrBatch.");
bsalomon489147c2015-12-14 12:13:09 -0800802DEFINE_int32(batchLookback, -1, "Maximum GrBatch lookback for combining, negative means default.");
bsalomon648c6962015-10-23 09:06:59 -0700803
mtkleinb9eb4ac2015-02-02 18:26:03 -0800804Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
kkinnunen5219fd92015-12-10 06:28:13 -0800805 GrContextOptions grOptions;
bsalomon489147c2015-12-14 12:13:09 -0800806 grOptions.fImmediateMode = FLAGS_imm;
807 grOptions.fClipBatchToBounds = FLAGS_batchClip;
808 grOptions.fDrawBatchBounds = FLAGS_batchBounds;
809 grOptions.fMaxBatchLookback = FLAGS_batchLookback;
kkinnunen64492c42015-12-08 01:24:40 -0800810
kkinnunen5219fd92015-12-10 06:28:13 -0800811 src.modifyGrContextOptions(&grOptions);
812
813 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -0800814 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800815 const SkImageInfo info =
816 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
817 SkAutoTUnref<SkSurface> surface(
kkinnunen3e980c32015-12-23 01:33:00 -0800818 NewGpuSurface(&factory, fContextType, fContextOptions, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -0800819 if (!surface) {
820 return "Could not create a surface.";
821 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800822 if (FLAGS_preAbandonGpuContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700823 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr);
joshualitt5f5a8d72015-02-25 14:09:45 -0800824 factory.abandonContexts();
825 }
mtklein748ca3b2015-01-15 10:56:12 -0800826 SkCanvas* canvas = surface->getCanvas();
827 Error err = src.draw(canvas);
828 if (!err.isEmpty()) {
829 return err;
830 }
831 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800832 if (FLAGS_gpuStats) {
833 canvas->getGrContext()->dumpCacheStats(log);
834 canvas->getGrContext()->dumpGpuStats(log);
835 }
mtklein748ca3b2015-01-15 10:56:12 -0800836 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800837 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800838 if (FLAGS_abandonGpuContext) {
839 factory.abandonContexts();
840 }
mtklein748ca3b2015-01-15 10:56:12 -0800841 return "";
842}
843
844/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
845
halcanary47ef4d52015-03-03 09:13:09 -0800846static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
847 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
848 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800849 int width = src.size().width(),
850 height = src.size().height();
851
halcanary7e798182015-04-14 14:06:18 -0700852 if (FLAGS_multiPage) {
853 const int kLetterWidth = 612, // 8.5 * 72
854 kLetterHeight = 792; // 11 * 72
855 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
856 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -0800857
halcanary7e798182015-04-14 14:06:18 -0700858 int xPages = ((width - 1) / kLetterWidth) + 1;
859 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -0800860
halcanary7e798182015-04-14 14:06:18 -0700861 for (int y = 0; y < yPages; ++y) {
862 for (int x = 0; x < xPages; ++x) {
863 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
864 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
865 SkCanvas* canvas =
866 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
867 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700868 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700869 }
870 canvas->clipRect(letter);
871 canvas->translate(-letter.width() * x, -letter.height() * y);
872 Error err = src.draw(canvas);
873 if (!err.isEmpty()) {
874 return err;
875 }
876 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -0700877 }
halcanaryfd4a9932015-01-28 11:45:58 -0800878 }
halcanary7e798182015-04-14 14:06:18 -0700879 } else {
880 SkCanvas* canvas =
881 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
882 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700883 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700884 }
885 Error err = src.draw(canvas);
886 if (!err.isEmpty()) {
887 return err;
888 }
889 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -0800890 }
halcanary7e798182015-04-14 14:06:18 -0700891 if (!doc->close()) {
892 return "SkDocument::close() returned false";
893 }
halcanaryfd4a9932015-01-28 11:45:58 -0800894 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800895 return "";
896}
897
halcanaryc11c62f2015-09-28 11:51:54 -0700898PDFSink::PDFSink(const char* rasterizer) : fRasterizer(rasterizer) {}
halcanary47ef4d52015-03-03 09:13:09 -0800899
900Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
901 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
902 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700903 return "SkDocument::CreatePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800904 }
halcanaryf12a1672015-09-23 12:45:49 -0700905 SkTArray<SkDocument::Attribute> info;
906 info.emplace_back(SkString("Title"), src.name());
907 info.emplace_back(SkString("Subject"),
908 SkString("rendering correctness test"));
909 info.emplace_back(SkString("Creator"), SkString("Skia/DM"));
halcanaryc11c62f2015-09-28 11:51:54 -0700910
911 info.emplace_back(SkString("Keywords"),
912 SkStringPrintf("Rasterizer:%s;", fRasterizer));
halcanaryf12a1672015-09-23 12:45:49 -0700913 doc->setMetadata(info, nullptr, nullptr);
halcanary47ef4d52015-03-03 09:13:09 -0800914 return draw_skdocument(src, doc.get(), dst);
915}
916
917/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
918
919XPSSink::XPSSink() {}
920
921Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
922 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
923 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700924 return "SkDocument::CreateXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800925 }
926 return draw_skdocument(src, doc.get(), dst);
927}
mtklein748ca3b2015-01-15 10:56:12 -0800928/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
929
mtklein9c3f17d2015-01-28 11:35:18 -0800930SKPSink::SKPSink() {}
931
mtkleinb9eb4ac2015-02-02 18:26:03 -0800932Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800933 SkSize size;
934 size = src.size();
935 SkPictureRecorder recorder;
936 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
937 if (!err.isEmpty()) {
938 return err;
939 }
940 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
941 pic->serialize(dst);
942 return "";
943}
944
945/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
946
mtklein8a4527e2015-01-31 20:00:58 -0800947SVGSink::SVGSink() {}
948
mtkleinb9eb4ac2015-02-02 18:26:03 -0800949Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary385fe4d2015-08-26 13:07:48 -0700950 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
fmalita2aafe6f2015-02-06 12:51:10 -0800951 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
952 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
953 xmlWriter));
954 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800955}
956
957/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
958
mtklein748ca3b2015-01-15 10:56:12 -0800959RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
960
mtkleinb9eb4ac2015-02-02 18:26:03 -0800961Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800962 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800963 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
964 SkAlphaType alphaType = kPremul_SkAlphaType;
965 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
966
mtkleinc8be09a2016-01-04 18:56:57 -0800967 SkMallocPixelRef::ZeroedPRFactory factory;
968 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType),
969 &factory,
970 nullptr/*colortable*/);
mtklein748ca3b2015-01-15 10:56:12 -0800971 SkCanvas canvas(*dst);
972 return src.draw(&canvas);
973}
974
975/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
976
mtkleina16e69e2015-05-05 11:38:45 -0700977// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -0700978// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -0700979// Several examples below.
980
msarett62d3b102015-12-10 15:14:27 -0800981static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtklein64593522015-11-12 10:41:05 -0800982 SkISize size, std::function<Error(SkCanvas*)> draw) {
mtkleina16e69e2015-05-05 11:38:45 -0700983 class ProxySrc : public Src {
984 public:
msarett62d3b102015-12-10 15:14:27 -0800985 ProxySrc(SkISize size, std::function<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -0700986 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
msarett62d3b102015-12-10 15:14:27 -0800987 Name name() const override { sk_throw(); return ""; } // Won't be called.
mtkleina16e69e2015-05-05 11:38:45 -0700988 SkISize size() const override { return fSize; }
989 private:
mtklein64593522015-11-12 10:41:05 -0800990 SkISize fSize;
991 std::function<Error(SkCanvas*)> fDraw;
mtkleina16e69e2015-05-05 11:38:45 -0700992 };
msarett62d3b102015-12-10 15:14:27 -0800993 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -0700994}
995
996/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
997
mtklein4a34ecb2016-01-08 10:19:35 -0800998DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
999
1000// Is *bitmap identical to what you get drawing src into sink?
1001static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1002 // We can only check raster outputs.
1003 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1004 if (FLAGS_check && bitmap) {
1005 SkBitmap reference;
1006 SkString log;
1007 Error err = sink->draw(src, &reference, nullptr, &log);
1008 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1009 SkASSERT(err.isEmpty());
1010 if (!err.isEmpty()) {
1011 return err;
1012 }
1013 // The dimensions are a property of the Src only, and so should be identical.
1014 SkASSERT(reference.getSize() == bitmap->getSize());
1015 if (reference.getSize() != bitmap->getSize()) {
1016 return "Dimensions don't match reference";
1017 }
1018 // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy.
1019 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1020 return "Pixels don't match reference";
1021 }
1022 }
1023 return "";
1024}
1025
1026/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1027
mtkleind603b222015-02-17 11:13:33 -08001028static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1029 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1030 matrix->mapRect(&bounds);
1031 matrix->postTranslate(-bounds.x(), -bounds.y());
1032 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
1033}
1034
msarett62d3b102015-12-10 15:14:27 -08001035ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001036
mtkleinb9eb4ac2015-02-02 18:26:03 -08001037Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001038 SkMatrix matrix = fMatrix;
1039 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
msarett62d3b102015-12-10 15:14:27 -08001040 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001041 canvas->concat(matrix);
1042 return src.draw(canvas);
1043 });
mtklein748ca3b2015-01-15 10:56:12 -08001044}
1045
mtkleind603b222015-02-17 11:13:33 -08001046// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1047// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001048ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001049
1050Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1051 Error err = fSink->draw(src, bitmap, stream, log);
1052 if (!err.isEmpty()) {
1053 return err;
1054 }
1055
1056 SkMatrix inverse;
1057 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1058 return "Cannot upright --matrix.";
1059 }
1060 SkMatrix upright = SkMatrix::I();
1061 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1062 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1063 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1064 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1065
1066 SkBitmap uprighted;
1067 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1068 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1069
1070 SkCanvas canvas(uprighted);
1071 canvas.concat(upright);
1072 SkPaint paint;
1073 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1074 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1075
1076 *bitmap = uprighted;
1077 bitmap->lockPixels();
1078 return "";
1079}
1080
mtklein748ca3b2015-01-15 10:56:12 -08001081/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1082
mtklein2e2ea382015-10-16 10:29:41 -07001083Error ViaRemote::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001084 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* target) {
mtklein479fe772015-10-21 12:34:01 -07001085 SkAutoTDelete<SkRemote::Encoder> decoder(SkRemote::NewDecoder(target));
1086 SkAutoTDelete<SkRemote::Encoder> cache(fCache ? SkRemote::NewCachingEncoder(decoder)
1087 : nullptr);
1088 SkAutoTDelete<SkCanvas> canvas(SkRemote::NewCanvas(cache ? cache : decoder));
1089 return src.draw(canvas);
mtklein2e2ea382015-10-16 10:29:41 -07001090 });
1091}
1092
mtklein748ca3b2015-01-15 10:56:12 -08001093/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtkleina16e69e2015-05-05 11:38:45 -07001094
mtkleina16e69e2015-05-05 11:38:45 -07001095Error ViaSerialization::draw(
1096 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001097 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001098 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001099 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001100 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1101 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001102 if (!err.isEmpty()) {
1103 return err;
1104 }
1105 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
1106
1107 // Serialize it and then deserialize it.
1108 SkDynamicMemoryWStream wStream;
1109 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -08001110 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
mtkleinb3e5e4d2015-03-25 13:13:43 -07001111 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap));
mtklein748ca3b2015-01-15 10:56:12 -08001112
msarett62d3b102015-12-10 15:14:27 -08001113 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001114 canvas->drawPicture(deserialized);
mtklein4a34ecb2016-01-08 10:19:35 -08001115 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001116 });
mtklein748ca3b2015-01-15 10:56:12 -08001117}
1118
1119/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1120
msarett62d3b102015-12-10 15:14:27 -08001121ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1122 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001123 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001124 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001125 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001126
mtkleinb9eb4ac2015-02-02 18:26:03 -08001127Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001128 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001129 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001130 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1131 SkIntToScalar(size.height()),
1132 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001133 if (!err.isEmpty()) {
1134 return err;
1135 }
mtkleinb7e8d692015-04-07 08:30:32 -07001136 SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001137
msarett62d3b102015-12-10 15:14:27 -08001138 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001139 const int xTiles = (size.width() + fW - 1) / fW,
1140 yTiles = (size.height() + fH - 1) / fH;
1141 SkMultiPictureDraw mpd(xTiles*yTiles);
1142 SkTDArray<SkSurface*> surfaces;
1143 surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001144
mtkleina16e69e2015-05-05 11:38:45 -07001145 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1146 for (int j = 0; j < yTiles; j++) {
1147 for (int i = 0; i < xTiles; i++) {
1148 // This lets our ultimate Sink determine the best kind of surface.
1149 // E.g., if it's a GpuSink, the surfaces and images are textures.
1150 SkSurface* s = canvas->newSurface(info);
1151 if (!s) {
1152 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001153 }
mtkleina16e69e2015-05-05 11:38:45 -07001154 surfaces.push(s);
1155 SkCanvas* c = s->getCanvas();
1156 c->translate(SkIntToScalar(-i * fW),
1157 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
1158 mpd.add(c, pic);
mtklein748ca3b2015-01-15 10:56:12 -08001159 }
mtklein748ca3b2015-01-15 10:56:12 -08001160 }
mtkleina16e69e2015-05-05 11:38:45 -07001161 mpd.draw();
1162 for (int j = 0; j < yTiles; j++) {
1163 for (int i = 0; i < xTiles; i++) {
1164 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
1165 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1166 }
1167 }
1168 surfaces.unrefAll();
1169 return "";
1170 });
mtklein748ca3b2015-01-15 10:56:12 -08001171}
1172
mtkleinb7e8d692015-04-07 08:30:32 -07001173/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1174
mtklein4a34ecb2016-01-08 10:19:35 -08001175Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1176 auto size = src.size();
1177 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1178 SkPictureRecorder recorder;
1179 SkAutoTUnref<SkPicture> pic;
1180 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1181 SkIntToScalar(size.height())));
1182 if (!err.isEmpty()) {
1183 return err;
1184 }
1185 pic.reset(recorder.endRecordingAsPicture());
1186 canvas->drawPicture(pic);
1187 return check_against_reference(bitmap, src, fSink);
1188 });
1189}
1190
1191/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1192
mtkleinb7e8d692015-04-07 08:30:32 -07001193// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1194// This tests that any shortcuts we may take while recording that second picture are legal.
1195Error ViaSecondPicture::draw(
1196 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001197 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001198 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001199 SkPictureRecorder recorder;
1200 SkAutoTUnref<SkPicture> pic;
1201 for (int i = 0; i < 2; i++) {
1202 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1203 SkIntToScalar(size.height())));
1204 if (!err.isEmpty()) {
1205 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001206 }
mtkleina16e69e2015-05-05 11:38:45 -07001207 pic.reset(recorder.endRecordingAsPicture());
mtkleinb7e8d692015-04-07 08:30:32 -07001208 }
mtkleina16e69e2015-05-05 11:38:45 -07001209 canvas->drawPicture(pic);
mtklein4a34ecb2016-01-08 10:19:35 -08001210 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001211 });
mtkleinb7e8d692015-04-07 08:30:32 -07001212}
1213
mtkleind31c13d2015-05-05 12:59:56 -07001214/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1215
mtklein6fbf4b32015-05-06 11:35:40 -07001216// Draw the Src twice. This can help exercise caching.
1217Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001218 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001219 for (int i = 0; i < 2; i++) {
1220 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1221 canvas->clear(SK_ColorTRANSPARENT);
1222 Error err = src.draw(canvas);
1223 if (err.isEmpty()) {
1224 return err;
1225 }
1226 }
mtklein4a34ecb2016-01-08 10:19:35 -08001227 return check_against_reference(bitmap, src, fSink);
mtklein6fbf4b32015-05-06 11:35:40 -07001228 });
1229}
1230
1231/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1232
mtkleind31c13d2015-05-05 12:59:56 -07001233// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1234// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1235// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1236struct DrawsAsSingletonPictures {
1237 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001238 const SkDrawableList& fDrawables;
mtkleind31c13d2015-05-05 12:59:56 -07001239
mtkleind31c13d2015-05-05 12:59:56 -07001240 template <typename T>
1241 void draw(const T& op, SkCanvas* canvas) {
1242 // We must pass SkMatrix::I() as our initial matrix.
1243 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1244 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001245 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1246 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001247 }
1248
mtklein449d9b72015-09-28 10:33:02 -07001249 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001250 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001251 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001252 SkPictureRecorder rec;
1253 this->draw(op, rec.beginRecording(SkRect::MakeLargest()));
1254 SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture());
1255 fCanvas->drawPicture(pic);
1256 }
1257
mtklein449d9b72015-09-28 10:33:02 -07001258 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001259 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001260 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1261 this->draw(op, fCanvas);
1262 }
mtkleind31c13d2015-05-05 12:59:56 -07001263};
1264
mtkleind31c13d2015-05-05 12:59:56 -07001265// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1266// Then play back that macro picture into our wrapped sink.
1267Error ViaSingletonPictures::draw(
1268 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1269 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001270 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07001271 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1272 SkRecord skr;
1273 SkRecorder recorder(&skr, size.width(), size.height());
1274 Error err = src.draw(&recorder);
1275 if (!err.isEmpty()) {
1276 return err;
1277 }
1278
1279 // Record our macro-picture, with each draw op as its own sub-picture.
1280 SkPictureRecorder macroRec;
1281 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1282 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001283
1284 SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
1285 const SkDrawableList empty;
1286
1287 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1288 macroCanvas,
1289 drawables ? *drawables : empty,
1290 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001291 for (int i = 0; i < skr.count(); i++) {
mtkleind31c13d2015-05-05 12:59:56 -07001292 skr.visit<void>(i, drawsAsSingletonPictures);
1293 }
1294 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture());
1295
1296 canvas->drawPicture(macroPic);
mtklein4a34ecb2016-01-08 10:19:35 -08001297 return check_against_reference(bitmap, src, fSink);
mtkleind31c13d2015-05-05 12:59:56 -07001298 });
1299}
1300
mtklein748ca3b2015-01-15 10:56:12 -08001301} // namespace DM