blob: 88371a8e28fdf68e1e0e5e041588a3a475e4adc1 [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"
9#include "SamplePipeControllers.h"
scroggof24f2242015-03-03 08:59:20 -080010#include "SkCodec.h"
msarett7f691442015-09-22 11:56:16 -070011#include "SkCodecTools.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"
mtkleine44b5082015-05-07 10:53:34 -070016#include "SkFunction.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070017#include "SkImageGenerator.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"
fmalita2aafe6f2015-02-06 12:51:10 -080026#include "SkSVGCanvas.h"
mtklein449d9b72015-09-28 10:33:02 -070027#include "SkScaledCodec.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 "SkScaledCodec.h"
32#include "SkSwizzler.h"
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
msaretta5783ae2015-09-08 15:35:32 -070070BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoderInterface::Strategy strategy, Mode mode,
71 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
85static SkBitmapRegionDecoderInterface* create_brd(Path path,
86 SkBitmapRegionDecoderInterface::Strategy strategy) {
87 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
88 if (!encoded) {
89 return NULL;
90 }
91 return SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(new SkMemoryStream(encoded),
92 strategy);
93}
94
95Error BRDSrc::draw(SkCanvas* canvas) const {
96 SkColorType colorType = canvas->imageInfo().colorType();
97 if (kRGB_565_SkColorType == colorType &&
98 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
99 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
100 }
101 switch (fDstColorType) {
102 case CodecSrc::kGetFromCanvas_DstColorType:
103 break;
104 case CodecSrc::kIndex8_Always_DstColorType:
105 colorType = kIndex_8_SkColorType;
106 break;
107 case CodecSrc::kGrayscale_Always_DstColorType:
108 colorType = kGray_8_SkColorType;
109 break;
110 }
111
112 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(create_brd(fPath, fStrategy));
113 if (nullptr == brd.get()) {
114 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
115 }
116
117 const uint32_t width = brd->width();
118 const uint32_t height = brd->height();
119 // Visually inspecting very small output images is not necessary.
120 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
121 return Error::Nonfatal("Scaling very small images is uninteresting.");
122 }
123 switch (fMode) {
124 case kFullImage_Mode: {
125 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(0, 0, width, height, fSampleSize,
126 colorType));
127 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) {
128 return Error::Nonfatal("Cannot convert to color type.\n");
129 }
130 canvas->drawBitmap(*bitmap, 0, 0);
131 return "";
132 }
133 case kDivisor_Mode: {
134 const uint32_t divisor = 2;
135 if (width < divisor || height < divisor) {
136 return Error::Nonfatal("Divisor is larger than image dimension.\n");
137 }
138
139 // Use a border to test subsets that extend outside the image.
140 // We will not allow the border to be larger than the image dimensions. Allowing
141 // these large borders causes off by one errors that indicate a problem with the
142 // test suite, not a problem with the implementation.
143 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
144 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
145 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
146
147 // We may need to clear the canvas to avoid uninitialized memory.
148 // Assume we are scaling a 780x780 image with sampleSize = 8.
149 // The output image should be 97x97.
150 // Each subset will be 390x390.
151 // Each scaled subset be 48x48.
152 // Four scaled subsets will only fill a 96x96 image.
153 // The bottom row and last column will not be touched.
154 // This is an unfortunate result of our rounding rules when scaling.
155 // Maybe we need to consider testing scaled subsets without trying to
156 // combine them to match the full scaled image? Or maybe this is the
157 // best we can do?
158 canvas->clear(0);
159
160 for (uint32_t x = 0; x < divisor; x++) {
161 for (uint32_t y = 0; y < divisor; y++) {
162 // Calculate the subset dimensions
163 uint32_t subsetWidth = width / divisor;
164 uint32_t subsetHeight = height / divisor;
165 const int left = x * subsetWidth;
166 const int top = y * subsetHeight;
167
168 // Increase the size of the last subset in each row or column, when the
169 // divisor does not divide evenly into the image dimensions
170 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
171 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
172
173 // Increase the size of the subset in order to have a border on each side
174 const int decodeLeft = left - unscaledBorder;
175 const int decodeTop = top - unscaledBorder;
176 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
177 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
178 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(decodeLeft,
179 decodeTop, decodeWidth, decodeHeight, fSampleSize, colorType));
180 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) {
181 return Error::Nonfatal("Cannot convert to color type.\n");
182 }
183
184 canvas->drawBitmapRect(*bitmap,
185 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
186 (SkScalar) (subsetWidth / fSampleSize),
187 (SkScalar) (subsetHeight / fSampleSize)),
188 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
189 (SkScalar) (top / fSampleSize),
190 (SkScalar) (subsetWidth / fSampleSize),
191 (SkScalar) (subsetHeight / fSampleSize)),
192 nullptr);
193 }
194 }
195 return "";
196 }
197 default:
198 SkASSERT(false);
199 return "Error: Should not be reached.\n";
200 }
201}
202
203SkISize BRDSrc::size() const {
204 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(create_brd(fPath, fStrategy));
205 if (brd) {
206 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
207 SkTMax(1, brd->height() / (int) fSampleSize));
208 }
209 return SkISize::Make(0, 0);
210}
211
212static SkString get_scaled_name(const Path& path, float scale) {
213 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
214}
215
216Name BRDSrc::name() const {
217 // We will replicate the names used by CodecSrc so that images can
218 // be compared in Gold.
219 if (1 == fSampleSize) {
220 return SkOSPath::Basename(fPath.c_str());
221 }
msarett7f691442015-09-22 11:56:16 -0700222 return get_scaled_name(fPath, get_scale_from_sample_size(fSampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700223}
224
225/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
226
msarett0a242972015-06-11 14:27:27 -0700227CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale)
msarett438b2ad2015-04-09 12:43:10 -0700228 : fPath(path)
229 , fMode(mode)
230 , fDstColorType(dstColorType)
msarett0a242972015-06-11 14:27:27 -0700231 , fScale(scale)
msarett438b2ad2015-04-09 12:43:10 -0700232{}
mtklein748ca3b2015-01-15 10:56:12 -0800233
mtklein99cab4e2015-07-31 06:43:04 -0700234bool CodecSrc::veto(SinkFlags flags) const {
235 // No need to test decoding to non-raster or indirect backend.
mtkleine0effd62015-07-29 06:37:28 -0700236 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferred decode to
237 // let the GPU handle it.
mtklein99cab4e2015-07-31 06:43:04 -0700238 return flags.type != SinkFlags::kRaster
239 || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700240}
scroggo9b77ddd2015-03-19 06:03:39 -0700241
mtkleine0effd62015-07-29 06:37:28 -0700242Error CodecSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -0800243 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800244 if (!encoded) {
245 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
246 }
msarett9e707a02015-09-01 14:57:57 -0700247 SkAutoTDelete<SkCodec> codec(NULL);
248 if (kScaledCodec_Mode == fMode) {
249 codec.reset(SkScaledCodec::NewFromData(encoded));
250 // TODO (msarett): This should fall through to a fatal error once we support scaled
251 // codecs for all image types.
halcanary96fcdcc2015-08-27 07:41:13 -0700252 if (nullptr == codec.get()) {
msarett9e707a02015-09-01 14:57:57 -0700253 return Error::Nonfatal(SkStringPrintf("Couldn't create scaled codec for %s.",
254 fPath.c_str()));
emmaleer8f4ba762015-08-14 07:44:46 -0700255 }
msarett9e707a02015-09-01 14:57:57 -0700256 } else {
257 codec.reset(SkCodec::NewFromData(encoded));
258 }
259 if (nullptr == codec.get()) {
260 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700261 }
262
msarett438b2ad2015-04-09 12:43:10 -0700263 // Choose the color type to decode to
264 SkImageInfo decodeInfo = codec->getInfo();
mtkleine0effd62015-07-29 06:37:28 -0700265 SkColorType canvasColorType = canvas->imageInfo().colorType();
msarett438b2ad2015-04-09 12:43:10 -0700266 switch (fDstColorType) {
267 case kIndex8_Always_DstColorType:
msarette16b04a2015-04-15 07:32:19 -0700268 decodeInfo = codec->getInfo().makeColorType(kIndex_8_SkColorType);
269 if (kRGB_565_SkColorType == canvasColorType) {
270 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
271 }
272 break;
msarett438b2ad2015-04-09 12:43:10 -0700273 case kGrayscale_Always_DstColorType:
msarette16b04a2015-04-15 07:32:19 -0700274 decodeInfo = codec->getInfo().makeColorType(kGray_8_SkColorType);
msarett438b2ad2015-04-09 12:43:10 -0700275 if (kRGB_565_SkColorType == canvasColorType) {
276 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
277 }
278 break;
279 default:
280 decodeInfo = decodeInfo.makeColorType(canvasColorType);
281 break;
282 }
283
msarett0a242972015-06-11 14:27:27 -0700284 // Try to scale the image if it is desired
285 SkISize size = codec->getScaledDimensions(fScale);
286 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
287 return Error::Nonfatal("Test without scaling is uninteresting.");
288 }
msarettb32758a2015-08-18 13:22:46 -0700289
290 // Visually inspecting very small output images is not necessary. We will
291 // cover these cases in unit testing.
292 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
293 return Error::Nonfatal("Scaling very small images is uninteresting.");
294 }
msarett0a242972015-06-11 14:27:27 -0700295 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
296
msarett438b2ad2015-04-09 12:43:10 -0700297 // Construct a color table for the decode if necessary
halcanary96fcdcc2015-08-27 07:41:13 -0700298 SkAutoTUnref<SkColorTable> colorTable(nullptr);
299 SkPMColor* colorPtr = nullptr;
300 int* colorCountPtr = nullptr;
msarett438b2ad2015-04-09 12:43:10 -0700301 int maxColors = 256;
302 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
303 SkPMColor colors[256];
halcanary385fe4d2015-08-26 13:07:48 -0700304 colorTable.reset(new SkColorTable(colors, maxColors));
msarett438b2ad2015-04-09 12:43:10 -0700305 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
306 colorCountPtr = &maxColors;
307 }
308
309 // FIXME: Currently we cannot draw unpremultiplied sources.
scroggo9b77ddd2015-03-19 06:03:39 -0700310 if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) {
scroggo9b77ddd2015-03-19 06:03:39 -0700311 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
312 }
313
314 SkBitmap bitmap;
halcanary96fcdcc2015-08-27 07:41:13 -0700315 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
scroggo9b77ddd2015-03-19 06:03:39 -0700316 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
317 decodeInfo.width(), decodeInfo.height());
318 }
319
scroggo9c59ebc2015-03-25 13:48:49 -0700320 switch (fMode) {
msarett9e707a02015-09-01 14:57:57 -0700321 case kScaledCodec_Mode:
322 case kCodec_Mode: {
halcanary96fcdcc2015-08-27 07:41:13 -0700323 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), nullptr,
msarett438b2ad2015-04-09 12:43:10 -0700324 colorPtr, colorCountPtr)) {
scroggoeb602a52015-07-09 08:16:03 -0700325 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700326 // We consider incomplete to be valid, since we should still decode what is
327 // available.
scroggoeb602a52015-07-09 08:16:03 -0700328 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700329 break;
scroggoeb602a52015-07-09 08:16:03 -0700330 case SkCodec::kInvalidConversion:
scroggo9c59ebc2015-03-25 13:48:49 -0700331 return Error::Nonfatal("Incompatible colortype conversion");
332 default:
333 // Everything else is considered a failure.
334 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
335 }
emmaleer97002062015-05-27 12:36:10 -0700336 canvas->drawBitmap(bitmap, 0, 0);
scroggo9c59ebc2015-03-25 13:48:49 -0700337 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700338 }
scroggo9c59ebc2015-03-25 13:48:49 -0700339 case kScanline_Mode: {
scroggo46c57472015-09-30 08:57:13 -0700340 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
341 colorCountPtr)) {
msarett9e707a02015-09-01 14:57:57 -0700342 return Error::Nonfatal("Could not start scanline decoder");
scroggo9c59ebc2015-03-25 13:48:49 -0700343 }
scroggo1c005e42015-08-04 09:24:45 -0700344
msarette6dd0042015-10-09 11:07:34 -0700345 void* dst = bitmap.getAddr(0, 0);
346 size_t rowBytes = bitmap.rowBytes();
347 uint32_t height = decodeInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700348 switch (codec->getScanlineOrder()) {
349 case SkCodec::kTopDown_SkScanlineOrder:
350 case SkCodec::kBottomUp_SkScanlineOrder:
351 case SkCodec::kNone_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700352 // We do not need to check the return value. On an incomplete
353 // image, memory will be filled with a default value.
354 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700355 break;
scroggo46c57472015-09-30 08:57:13 -0700356 case SkCodec::kOutOfOrder_SkScanlineOrder: {
msarett10522ff2015-09-07 08:54:01 -0700357 for (int y = 0; y < decodeInfo.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700358 int dstY = codec->outputScanline(y);
msarett10522ff2015-09-07 08:54:01 -0700359 void* dstPtr = bitmap.getAddr(0, dstY);
msarette6dd0042015-10-09 11:07:34 -0700360 // We complete the loop, even if this call begins to fail
361 // due to an incomplete image. This ensures any uninitialized
362 // memory will be filled with the proper value.
363 codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
msarett10522ff2015-09-07 08:54:01 -0700364 }
365 break;
366 }
367 }
368
emmaleer97002062015-05-27 12:36:10 -0700369 canvas->drawBitmap(bitmap, 0, 0);
370 break;
371 }
372 case kScanline_Subset_Mode: {
373 //this mode decodes the image in divisor*divisor subsets, using a scanline decoder
374 const int divisor = 2;
375 const int w = decodeInfo.width();
376 const int h = decodeInfo.height();
emmaleer97002062015-05-27 12:36:10 -0700377 if (divisor > w || divisor > h) {
msarett70542572015-06-19 07:44:05 -0700378 return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d is too big"
379 "for %s with dimensions (%d x %d)", divisor, fPath.c_str(), w, h));
emmaleer97002062015-05-27 12:36:10 -0700380 }
381 const int subsetWidth = w/divisor;
382 const int subsetHeight = h/divisor;
383 // One of our subsets will be larger to contain any pixels that do not divide evenly.
384 const int extraX = w % divisor;
385 const int extraY = h % divisor;
386 /*
387 * if w or h are not evenly divided by divisor need to adjust width and height of end
388 * subsets to cover entire image.
389 * Add extraX and extraY to largestSubsetBm's width and height to adjust width
390 * and height of end subsets.
391 * subsetBm is extracted from largestSubsetBm.
392 * subsetBm's size is determined based on the current subset and may be larger for end
393 * subsets.
394 */
msarett0a242972015-06-11 14:27:27 -0700395 SkImageInfo largestSubsetDecodeInfo =
emmaleer97002062015-05-27 12:36:10 -0700396 decodeInfo.makeWH(subsetWidth + extraX, subsetHeight + extraY);
397 SkBitmap largestSubsetBm;
msarett9e707a02015-09-01 14:57:57 -0700398 if (!largestSubsetBm.tryAllocPixels(largestSubsetDecodeInfo, nullptr,
399 colorTable.get())) {
emmaleer97002062015-05-27 12:36:10 -0700400 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
401 largestSubsetDecodeInfo.width(), largestSubsetDecodeInfo.height());
402 }
emmaleer0a4c3cb2015-06-22 10:40:21 -0700403 const size_t rowBytes = decodeInfo.minRowBytes();
halcanary385fe4d2015-08-26 13:07:48 -0700404 char* buffer = new char[largestSubsetDecodeInfo.height() * rowBytes];
emmaleer0a4c3cb2015-06-22 10:40:21 -0700405 SkAutoTDeleteArray<char> lineDeleter(buffer);
emmaleer97002062015-05-27 12:36:10 -0700406 for (int col = 0; col < divisor; col++) {
407 //currentSubsetWidth may be larger than subsetWidth for rightmost subsets
408 const int currentSubsetWidth = (col + 1 == divisor) ?
409 subsetWidth + extraX : subsetWidth;
410 const int x = col * subsetWidth;
411 for (int row = 0; row < divisor; row++) {
412 //currentSubsetHeight may be larger than subsetHeight for bottom subsets
413 const int currentSubsetHeight = (row + 1 == divisor) ?
414 subsetHeight + extraY : subsetHeight;
415 const int y = row * subsetHeight;
416 //create scanline decoder for each subset
scroggo46c57472015-09-30 08:57:13 -0700417 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
418 colorCountPtr)
419 // TODO (msarett): Support this mode for all scanline orderings.
420 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
emmaleer97002062015-05-27 12:36:10 -0700421 if (x == 0 && y == 0) {
422 //first try, image may not be compatible
msarett5406d6f2015-08-31 06:55:13 -0700423 return Error::Nonfatal("Could not start top-down scanline decoder");
emmaleer97002062015-05-27 12:36:10 -0700424 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700425 return "Error scanline decoder is nullptr";
emmaleer97002062015-05-27 12:36:10 -0700426 }
427 }
msarette6dd0042015-10-09 11:07:34 -0700428 // Skip to the first line of subset. We ignore the result value here.
429 // If the skip value fails, this will indicate an incomplete image.
430 // This means that the call to getScanlines() will also fail, but it
431 // will fill the buffer with a default value, so we can still draw the
432 // image.
433 codec->skipScanlines(y);
434
emmaleer97002062015-05-27 12:36:10 -0700435 //create and set size of subsetBm
436 SkBitmap subsetBm;
scroggo6634cbb2015-09-30 13:01:55 -0700437 SkIRect bounds = SkIRect::MakeWH(currentSubsetWidth, currentSubsetHeight);
emmaleer97002062015-05-27 12:36:10 -0700438 SkAssertResult(largestSubsetBm.extractSubset(&subsetBm, bounds));
439 SkAutoLockPixels autlockSubsetBm(subsetBm, true);
msarette6dd0042015-10-09 11:07:34 -0700440 codec->getScanlines(buffer, currentSubsetHeight, rowBytes);
441
emmaleer0a4c3cb2015-06-22 10:40:21 -0700442 const size_t bpp = decodeInfo.bytesPerPixel();
mtkleind2baa902015-07-07 09:43:28 -0700443 char* bufferRow = buffer;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700444 for (int subsetY = 0; subsetY < currentSubsetHeight; ++subsetY) {
mtkleind2baa902015-07-07 09:43:28 -0700445 memcpy(subsetBm.getAddr(0, subsetY), bufferRow + x*bpp,
emmaleer0a4c3cb2015-06-22 10:40:21 -0700446 currentSubsetWidth*bpp);
447 bufferRow += rowBytes;
448 }
mtkleind2baa902015-07-07 09:43:28 -0700449
scroggo4358f132015-07-30 11:33:04 -0700450 subsetBm.notifyPixelsChanged();
emmaleer97002062015-05-27 12:36:10 -0700451 canvas->drawBitmap(subsetBm, SkIntToScalar(x), SkIntToScalar(y));
452 }
453 }
scroggo9c59ebc2015-03-25 13:48:49 -0700454 break;
455 }
msarett0a242972015-06-11 14:27:27 -0700456 case kStripe_Mode: {
457 const int height = decodeInfo.height();
458 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
459 // does not align with image blocks.
460 const int stripeHeight = 37;
461 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
462
463 // Decode odd stripes
scroggo46c57472015-09-30 08:57:13 -0700464 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
465 colorCountPtr)
466 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
msarett9e707a02015-09-01 14:57:57 -0700467 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
468 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
469 // to run this test for image types that do not have this scanline ordering.
msarett5406d6f2015-08-31 06:55:13 -0700470 return Error::Nonfatal("Could not start top-down scanline decoder");
msarett0a242972015-06-11 14:27:27 -0700471 }
msarette6dd0042015-10-09 11:07:34 -0700472
msarett0a242972015-06-11 14:27:27 -0700473 for (int i = 0; i < numStripes; i += 2) {
474 // Skip a stripe
475 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700476 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700477
478 // Read a stripe
479 const int startY = (i + 1) * stripeHeight;
480 const int linesToRead = SkTMin(stripeHeight, height - startY);
481 if (linesToRead > 0) {
msarette6dd0042015-10-09 11:07:34 -0700482 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700483 }
484 }
485
486 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700487 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
488 colorPtr, colorCountPtr);
scroggo1c005e42015-08-04 09:24:45 -0700489 if (SkCodec::kSuccess != startResult) {
490 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700491 }
492 for (int i = 0; i < numStripes; i += 2) {
493 // Read a stripe
494 const int startY = i * stripeHeight;
495 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarette6dd0042015-10-09 11:07:34 -0700496 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700497
498 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700499 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
500 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700501 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700502 }
503 }
504 canvas->drawBitmap(bitmap, 0, 0);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700505 break;
msarett0a242972015-06-11 14:27:27 -0700506 }
scroggob636b452015-07-22 07:16:20 -0700507 case kSubset_Mode: {
508 // Arbitrarily choose a divisor.
509 int divisor = 2;
510 // Total width/height of the image.
511 const int W = codec->getInfo().width();
512 const int H = codec->getInfo().height();
513 if (divisor > W || divisor > H) {
514 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
515 "for %s with dimensions (%d x %d)", divisor,
516 fPath.c_str(), W, H));
517 }
518 // subset dimensions
519 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
520 const int w = SkAlign2(W / divisor);
521 const int h = SkAlign2(H / divisor);
522 SkIRect subset;
523 SkCodec::Options opts;
524 opts.fSubset = &subset;
525 SkBitmap subsetBm;
526 // We will reuse pixel memory from bitmap.
527 void* pixels = bitmap.getPixels();
528 // Keep track of left and top (for drawing subsetBm into canvas). We could use
529 // fScale * x and fScale * y, but we want integers such that the next subset will start
530 // where the last one ended. So we'll add decodeInfo.width() and height().
531 int left = 0;
532 for (int x = 0; x < W; x += w) {
533 int top = 0;
534 for (int y = 0; y < H; y+= h) {
535 // Do not make the subset go off the edge of the image.
536 const int preScaleW = SkTMin(w, W - x);
537 const int preScaleH = SkTMin(h, H - y);
538 subset.setXYWH(x, y, preScaleW, preScaleH);
539 // And scale
540 // FIXME: Should we have a version of getScaledDimensions that takes a subset
541 // into account?
msarette6dd0042015-10-09 11:07:34 -0700542 decodeInfo = decodeInfo.makeWH(
543 SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)),
544 SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)));
scroggob636b452015-07-22 07:16:20 -0700545 size_t rowBytes = decodeInfo.minRowBytes();
546 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700547 nullptr, nullptr)) {
scroggob636b452015-07-22 07:16:20 -0700548 return SkStringPrintf("could not install pixels for %s.", fPath.c_str());
549 }
550 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
551 &opts, colorPtr, colorCountPtr);
552 switch (result) {
553 case SkCodec::kSuccess:
554 case SkCodec::kIncompleteInput:
555 break;
556 case SkCodec::kInvalidConversion:
557 if (0 == (x|y)) {
558 // First subset is okay to return unimplemented.
559 return Error::Nonfatal("Incompatible colortype conversion");
560 }
561 // If the first subset succeeded, a later one should not fail.
562 // fall through to failure
563 case SkCodec::kUnimplemented:
564 if (0 == (x|y)) {
565 // First subset is okay to return unimplemented.
566 return Error::Nonfatal("subset codec not supported");
567 }
568 // If the first subset succeeded, why would a later one fail?
569 // fall through to failure
570 default:
571 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
572 "from %s with dimensions (%d x %d)\t error %d",
573 x, y, decodeInfo.width(), decodeInfo.height(),
574 fPath.c_str(), W, H, result);
575 }
576 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top));
577 // translate by the scaled height.
578 top += decodeInfo.height();
579 }
580 // translate by the scaled width.
581 left += decodeInfo.width();
582 }
583 return "";
584 }
scroggo9b77ddd2015-03-19 06:03:39 -0700585 }
scroggo9c59ebc2015-03-25 13:48:49 -0700586 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700587}
588
589SkISize CodecSrc::size() const {
590 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
scroggo7fac5af2015-09-30 11:33:12 -0700591 SkAutoTDelete<SkCodec> codec(nullptr);
592
593 if (kScaledCodec_Mode == fMode) {
594 codec.reset(SkScaledCodec::NewFromData(encoded));
595 } else {
emmaleer8f4ba762015-08-14 07:44:46 -0700596 codec.reset(SkCodec::NewFromData(encoded));
msarett9bde9182015-03-25 05:27:48 -0700597 }
scroggo7fac5af2015-09-30 11:33:12 -0700598
599 if (nullptr == codec) {
600 return SkISize::Make(0, 0);
601 }
602 return codec->getScaledDimensions(fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700603}
604
605Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700606 if (1.0f == fScale) {
607 return SkOSPath::Basename(fPath.c_str());
msarett0a242972015-06-11 14:27:27 -0700608 }
msaretta5783ae2015-09-08 15:35:32 -0700609 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700610}
611
612/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
613
614ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
615
mtklein99cab4e2015-07-31 06:43:04 -0700616bool ImageSrc::veto(SinkFlags flags) const {
617 // No need to test decoding to non-raster or indirect backend.
mtkleine0effd62015-07-29 06:37:28 -0700618 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YUV.
mtklein99cab4e2015-07-31 06:43:04 -0700619 return flags.type != SinkFlags::kRaster
620 || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700621}
scroggo9b77ddd2015-03-19 06:03:39 -0700622
mtkleine0effd62015-07-29 06:37:28 -0700623Error ImageSrc::draw(SkCanvas* canvas) const {
scroggo9b77ddd2015-03-19 06:03:39 -0700624 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
625 if (!encoded) {
626 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
627 }
mtkleine0effd62015-07-29 06:37:28 -0700628 const SkColorType dstColorType = canvas->imageInfo().colorType();
mtkleinedc93bc2015-01-30 13:22:23 -0800629 if (fDivisor == 0) {
mtklein748ca3b2015-01-15 10:56:12 -0800630 // Decode the full image.
631 SkBitmap bitmap;
scroggo9b77ddd2015-03-19 06:03:39 -0700632 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
633 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
634 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
635 }
636 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) {
637 // Do not draw a bitmap with alpha to a destination without alpha.
638 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
mtklein748ca3b2015-01-15 10:56:12 -0800639 }
halcanary96fcdcc2015-08-27 07:41:13 -0700640 encoded.reset((SkData*)nullptr); // Might as well drop this when we're done with it.
mtklein748ca3b2015-01-15 10:56:12 -0800641 canvas->drawBitmap(bitmap, 0,0);
642 return "";
643 }
mtkleinedc93bc2015-01-30 13:22:23 -0800644 // Decode subsets. This is a little involved.
scroggoa1193e42015-01-21 12:09:53 -0800645 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
646 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
mtklein748ca3b2015-01-15 10:56:12 -0800647 if (!decoder) {
648 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
649 }
scroggoa1193e42015-01-21 12:09:53 -0800650 stream->rewind();
mtklein748ca3b2015-01-15 10:56:12 -0800651 int w,h;
msarett70542572015-06-19 07:44:05 -0700652 if (!decoder->buildTileIndex(stream.detach(), &w, &h)) {
mtklein4089ef72015-03-05 08:40:28 -0800653 return Error::Nonfatal("Subset decoding not supported.");
mtklein748ca3b2015-01-15 10:56:12 -0800654 }
mtkleinedc93bc2015-01-30 13:22:23 -0800655
656 // Divide the image into subsets that cover the entire image.
657 if (fDivisor > w || fDivisor > h) {
msarett70542572015-06-19 07:44:05 -0700658 return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d is too big"
659 "for %s with dimensions (%d x %d)", fDivisor, fPath.c_str(), w, h));
mtkleinedc93bc2015-01-30 13:22:23 -0800660 }
661 const int subsetWidth = w / fDivisor,
662 subsetHeight = h / fDivisor;
663 for (int y = 0; y < h; y += subsetHeight) {
664 for (int x = 0; x < w; x += subsetWidth) {
665 SkBitmap subset;
666 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
667 if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
668 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
669 x, y, x+subsetWidth, y+subsetHeight);
670 }
scroggo56e25dd2015-03-05 11:46:40 -0800671 if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) {
672 // Do not draw a bitmap with alpha to a destination without alpha.
673 // This is not an error, but there is nothing interesting to show.
674
675 // This should only happen on the first iteration through the loop.
676 SkASSERT(0 == x && 0 == y);
677
678 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
679 }
mtkleinedc93bc2015-01-30 13:22:23 -0800680 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
mtklein748ca3b2015-01-15 10:56:12 -0800681 }
mtklein748ca3b2015-01-15 10:56:12 -0800682 }
683 return "";
684}
685
686SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -0800687 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
scroggo9b77ddd2015-03-19 06:03:39 -0700688 SkBitmap bitmap;
689 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
690 encoded->size(),
691 &bitmap,
692 kUnknown_SkColorType,
693 SkImageDecoder::kDecodeBounds_Mode)) {
694 return SkISize::Make(0,0);
mtklein748ca3b2015-01-15 10:56:12 -0800695 }
scroggo9b77ddd2015-03-19 06:03:39 -0700696 return bitmap.dimensions();
mtklein748ca3b2015-01-15 10:56:12 -0800697}
698
mtklein9264a952015-01-20 10:11:53 -0800699Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800700 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800701}
mtklein748ca3b2015-01-15 10:56:12 -0800702
703/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
704
mtkleinf4ba3212015-01-28 15:32:24 -0800705static const SkRect kSKPViewport = {0,0, 1000,1000};
706
mtklein8d17a132015-01-30 11:42:31 -0800707SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800708
709Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800710 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800711 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800712 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
713 }
mtkleinb3e5e4d2015-03-25 13:13:43 -0700714 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap));
mtklein75d98fd2015-01-18 07:05:01 -0800715 if (!pic) {
716 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
717 }
halcanary96fcdcc2015-08-27 07:41:13 -0700718 stream.reset((SkStream*)nullptr); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -0700719
mtkleinf4ba3212015-01-28 15:32:24 -0800720 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800721 canvas->drawPicture(pic);
722 return "";
723}
724
725SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700726 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
727 if (!stream) {
728 return SkISize::Make(0,0);
729 }
730 SkPictInfo info;
731 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
732 return SkISize::Make(0,0);
733 }
734 SkRect viewport = kSKPViewport;
735 if (!viewport.intersect(info.fCullRect)) {
736 return SkISize::Make(0,0);
737 }
738 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800739}
740
741Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
742
743/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
744
mtkleinad66f9b2015-02-13 15:11:10 -0800745Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
746 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
747 return src.draw(canvas);
748}
749
750/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
751
mtkleinb9eb4ac2015-02-02 18:26:03 -0800752DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
753
mtklein82d28432015-01-15 12:46:02 -0800754GPUSink::GPUSink(GrContextFactory::GLContextType ct,
755 GrGLStandard api,
756 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -0700757 bool diText,
mtklein82d28432015-01-15 12:46:02 -0800758 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800759 : fContextType(ct)
760 , fGpuAPI(api)
761 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -0700762 , fUseDIText(diText)
mtklein82d28432015-01-15 12:46:02 -0800763 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800764
765int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800766 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800767}
768
joshualitt5f5a8d72015-02-25 14:09:45 -0800769void PreAbandonGpuContextErrorHandler(SkError, void*) {}
770
mtkleinb9eb4ac2015-02-02 18:26:03 -0800771Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
bsalomon4ee6bd82015-05-27 13:23:23 -0700772 GrContextOptions options;
773 src.modifyGrContextOptions(&options);
774
775 GrContextFactory factory(options);
mtkleinf4ba3212015-01-28 15:32:24 -0800776 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800777 const SkImageInfo info =
778 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
779 SkAutoTUnref<SkSurface> surface(
bsalomonafcd7cd2015-08-31 12:39:41 -0700780 NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -0800781 if (!surface) {
782 return "Could not create a surface.";
783 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800784 if (FLAGS_preAbandonGpuContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700785 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr);
joshualitt5f5a8d72015-02-25 14:09:45 -0800786 factory.abandonContexts();
787 }
mtklein748ca3b2015-01-15 10:56:12 -0800788 SkCanvas* canvas = surface->getCanvas();
789 Error err = src.draw(canvas);
790 if (!err.isEmpty()) {
791 return err;
792 }
793 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800794 if (FLAGS_gpuStats) {
795 canvas->getGrContext()->dumpCacheStats(log);
796 canvas->getGrContext()->dumpGpuStats(log);
797 }
mtklein748ca3b2015-01-15 10:56:12 -0800798 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800799 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800800 if (FLAGS_abandonGpuContext) {
801 factory.abandonContexts();
802 }
mtklein748ca3b2015-01-15 10:56:12 -0800803 return "";
804}
805
806/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
807
halcanary47ef4d52015-03-03 09:13:09 -0800808static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
809 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
810 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800811 int width = src.size().width(),
812 height = src.size().height();
813
halcanary7e798182015-04-14 14:06:18 -0700814 if (FLAGS_multiPage) {
815 const int kLetterWidth = 612, // 8.5 * 72
816 kLetterHeight = 792; // 11 * 72
817 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
818 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -0800819
halcanary7e798182015-04-14 14:06:18 -0700820 int xPages = ((width - 1) / kLetterWidth) + 1;
821 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -0800822
halcanary7e798182015-04-14 14:06:18 -0700823 for (int y = 0; y < yPages; ++y) {
824 for (int x = 0; x < xPages; ++x) {
825 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
826 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
827 SkCanvas* canvas =
828 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
829 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700830 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700831 }
832 canvas->clipRect(letter);
833 canvas->translate(-letter.width() * x, -letter.height() * y);
834 Error err = src.draw(canvas);
835 if (!err.isEmpty()) {
836 return err;
837 }
838 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -0700839 }
halcanaryfd4a9932015-01-28 11:45:58 -0800840 }
halcanary7e798182015-04-14 14:06:18 -0700841 } else {
842 SkCanvas* canvas =
843 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
844 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700845 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700846 }
847 Error err = src.draw(canvas);
848 if (!err.isEmpty()) {
849 return err;
850 }
851 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -0800852 }
halcanary7e798182015-04-14 14:06:18 -0700853 if (!doc->close()) {
854 return "SkDocument::close() returned false";
855 }
halcanaryfd4a9932015-01-28 11:45:58 -0800856 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800857 return "";
858}
859
halcanaryc11c62f2015-09-28 11:51:54 -0700860PDFSink::PDFSink(const char* rasterizer) : fRasterizer(rasterizer) {}
halcanary47ef4d52015-03-03 09:13:09 -0800861
862Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
863 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
864 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700865 return "SkDocument::CreatePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800866 }
halcanaryf12a1672015-09-23 12:45:49 -0700867 SkTArray<SkDocument::Attribute> info;
868 info.emplace_back(SkString("Title"), src.name());
869 info.emplace_back(SkString("Subject"),
870 SkString("rendering correctness test"));
871 info.emplace_back(SkString("Creator"), SkString("Skia/DM"));
halcanaryc11c62f2015-09-28 11:51:54 -0700872
873 info.emplace_back(SkString("Keywords"),
874 SkStringPrintf("Rasterizer:%s;", fRasterizer));
halcanaryf12a1672015-09-23 12:45:49 -0700875 doc->setMetadata(info, nullptr, nullptr);
halcanary47ef4d52015-03-03 09:13:09 -0800876 return draw_skdocument(src, doc.get(), dst);
877}
878
879/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
880
881XPSSink::XPSSink() {}
882
883Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
884 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
885 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700886 return "SkDocument::CreateXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800887 }
888 return draw_skdocument(src, doc.get(), dst);
889}
mtklein748ca3b2015-01-15 10:56:12 -0800890/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
891
mtklein9c3f17d2015-01-28 11:35:18 -0800892SKPSink::SKPSink() {}
893
mtkleinb9eb4ac2015-02-02 18:26:03 -0800894Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800895 SkSize size;
896 size = src.size();
897 SkPictureRecorder recorder;
898 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
899 if (!err.isEmpty()) {
900 return err;
901 }
902 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
903 pic->serialize(dst);
904 return "";
905}
906
907/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
908
mtklein8a4527e2015-01-31 20:00:58 -0800909SVGSink::SVGSink() {}
910
mtkleinb9eb4ac2015-02-02 18:26:03 -0800911Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary385fe4d2015-08-26 13:07:48 -0700912 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
fmalita2aafe6f2015-02-06 12:51:10 -0800913 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
914 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
915 xmlWriter));
916 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800917}
918
919/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
920
mtklein748ca3b2015-01-15 10:56:12 -0800921RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
922
mtkleinb9eb4ac2015-02-02 18:26:03 -0800923Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800924 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800925 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
926 SkAlphaType alphaType = kPremul_SkAlphaType;
927 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
928
929 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
930 dst->eraseColor(SK_ColorTRANSPARENT);
931 SkCanvas canvas(*dst);
932 return src.draw(&canvas);
933}
934
935/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
936
mtkleina16e69e2015-05-05 11:38:45 -0700937// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -0700938// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -0700939// Several examples below.
940
mtkleina16e69e2015-05-05 11:38:45 -0700941static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleine44b5082015-05-07 10:53:34 -0700942 SkISize size, SkFunction<Error(SkCanvas*)> draw) {
mtkleina16e69e2015-05-05 11:38:45 -0700943 class ProxySrc : public Src {
944 public:
mtkleine44b5082015-05-07 10:53:34 -0700945 ProxySrc(SkISize size, SkFunction<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -0700946 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
947 Name name() const override { sk_throw(); return ""; } // Won't be called.
948 SkISize size() const override { return fSize; }
949 private:
mtkleine44b5082015-05-07 10:53:34 -0700950 SkISize fSize;
951 SkFunction<Error(SkCanvas*)> fDraw;
mtkleina16e69e2015-05-05 11:38:45 -0700952 };
953 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
954}
955
956/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
957
mtkleind603b222015-02-17 11:13:33 -0800958static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
959 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
960 matrix->mapRect(&bounds);
961 matrix->postTranslate(-bounds.x(), -bounds.y());
962 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
963}
964
mtklein78829242015-05-06 07:54:07 -0700965ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -0800966
mtkleinb9eb4ac2015-02-02 18:26:03 -0800967Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -0700968 SkMatrix matrix = fMatrix;
969 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
970 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
971 canvas->concat(matrix);
972 return src.draw(canvas);
973 });
mtklein748ca3b2015-01-15 10:56:12 -0800974}
975
mtkleind603b222015-02-17 11:13:33 -0800976// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
977// This should be pixel-preserving.
mtklein78829242015-05-06 07:54:07 -0700978ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -0800979
980Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
981 Error err = fSink->draw(src, bitmap, stream, log);
982 if (!err.isEmpty()) {
983 return err;
984 }
985
986 SkMatrix inverse;
987 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
988 return "Cannot upright --matrix.";
989 }
990 SkMatrix upright = SkMatrix::I();
991 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
992 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
993 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
994 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
995
996 SkBitmap uprighted;
997 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
998 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
999
1000 SkCanvas canvas(uprighted);
1001 canvas.concat(upright);
1002 SkPaint paint;
1003 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1004 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1005
1006 *bitmap = uprighted;
1007 bitmap->lockPixels();
1008 return "";
1009}
1010
mtklein748ca3b2015-01-15 10:56:12 -08001011/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1012
mtkleinb9eb4ac2015-02-02 18:26:03 -08001013Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001014 auto size = src.size();
1015 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
1016 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
1017 SkGPipeWriter pipe;
reed451af502015-08-19 08:18:04 -07001018 const uint32_t kFlags = 0;
mtkleina16e69e2015-05-05 11:38:45 -07001019 return src.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
1020 });
mtklein748ca3b2015-01-15 10:56:12 -08001021}
1022
1023/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtkleina16e69e2015-05-05 11:38:45 -07001024
mtkleina16e69e2015-05-05 11:38:45 -07001025Error ViaSerialization::draw(
1026 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001027 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001028 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001029 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001030 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1031 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001032 if (!err.isEmpty()) {
1033 return err;
1034 }
1035 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
1036
1037 // Serialize it and then deserialize it.
1038 SkDynamicMemoryWStream wStream;
1039 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -08001040 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
mtkleinb3e5e4d2015-03-25 13:13:43 -07001041 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap));
mtklein748ca3b2015-01-15 10:56:12 -08001042
mtkleina16e69e2015-05-05 11:38:45 -07001043 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
1044 canvas->drawPicture(deserialized);
1045 return "";
1046 });
mtklein748ca3b2015-01-15 10:56:12 -08001047}
1048
1049/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1050
1051ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
mtklein78829242015-05-06 07:54:07 -07001052 : Via(sink)
1053 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001054 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001055 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001056
mtkleinb9eb4ac2015-02-02 18:26:03 -08001057Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001058 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001059 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001060 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1061 SkIntToScalar(size.height()),
1062 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001063 if (!err.isEmpty()) {
1064 return err;
1065 }
mtkleinb7e8d692015-04-07 08:30:32 -07001066 SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001067
mtkleina16e69e2015-05-05 11:38:45 -07001068 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
1069 const int xTiles = (size.width() + fW - 1) / fW,
1070 yTiles = (size.height() + fH - 1) / fH;
1071 SkMultiPictureDraw mpd(xTiles*yTiles);
1072 SkTDArray<SkSurface*> surfaces;
1073 surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001074
mtkleina16e69e2015-05-05 11:38:45 -07001075 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1076 for (int j = 0; j < yTiles; j++) {
1077 for (int i = 0; i < xTiles; i++) {
1078 // This lets our ultimate Sink determine the best kind of surface.
1079 // E.g., if it's a GpuSink, the surfaces and images are textures.
1080 SkSurface* s = canvas->newSurface(info);
1081 if (!s) {
1082 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001083 }
mtkleina16e69e2015-05-05 11:38:45 -07001084 surfaces.push(s);
1085 SkCanvas* c = s->getCanvas();
1086 c->translate(SkIntToScalar(-i * fW),
1087 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
1088 mpd.add(c, pic);
mtklein748ca3b2015-01-15 10:56:12 -08001089 }
mtklein748ca3b2015-01-15 10:56:12 -08001090 }
mtkleina16e69e2015-05-05 11:38:45 -07001091 mpd.draw();
1092 for (int j = 0; j < yTiles; j++) {
1093 for (int i = 0; i < xTiles; i++) {
1094 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
1095 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1096 }
1097 }
1098 surfaces.unrefAll();
1099 return "";
1100 });
mtklein748ca3b2015-01-15 10:56:12 -08001101}
1102
mtkleinb7e8d692015-04-07 08:30:32 -07001103/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1104
mtkleinb7e8d692015-04-07 08:30:32 -07001105// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1106// This tests that any shortcuts we may take while recording that second picture are legal.
1107Error ViaSecondPicture::draw(
1108 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001109 auto size = src.size();
1110 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1111 SkPictureRecorder recorder;
1112 SkAutoTUnref<SkPicture> pic;
1113 for (int i = 0; i < 2; i++) {
1114 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1115 SkIntToScalar(size.height())));
1116 if (!err.isEmpty()) {
1117 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001118 }
mtkleina16e69e2015-05-05 11:38:45 -07001119 pic.reset(recorder.endRecordingAsPicture());
mtkleinb7e8d692015-04-07 08:30:32 -07001120 }
mtkleina16e69e2015-05-05 11:38:45 -07001121 canvas->drawPicture(pic);
1122 return "";
1123 });
mtkleinb7e8d692015-04-07 08:30:32 -07001124}
1125
mtkleind31c13d2015-05-05 12:59:56 -07001126/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1127
mtklein6fbf4b32015-05-06 11:35:40 -07001128// Draw the Src twice. This can help exercise caching.
1129Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1130 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
1131 for (int i = 0; i < 2; i++) {
1132 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1133 canvas->clear(SK_ColorTRANSPARENT);
1134 Error err = src.draw(canvas);
1135 if (err.isEmpty()) {
1136 return err;
1137 }
1138 }
1139 return "";
1140 });
1141}
1142
1143/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1144
mtkleind31c13d2015-05-05 12:59:56 -07001145// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1146// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1147// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1148struct DrawsAsSingletonPictures {
1149 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001150 const SkDrawableList& fDrawables;
mtkleind31c13d2015-05-05 12:59:56 -07001151
mtkleind31c13d2015-05-05 12:59:56 -07001152 template <typename T>
1153 void draw(const T& op, SkCanvas* canvas) {
1154 // We must pass SkMatrix::I() as our initial matrix.
1155 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1156 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001157 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1158 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001159 }
1160
mtklein449d9b72015-09-28 10:33:02 -07001161 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001162 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001163 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001164 SkPictureRecorder rec;
1165 this->draw(op, rec.beginRecording(SkRect::MakeLargest()));
1166 SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture());
1167 fCanvas->drawPicture(pic);
1168 }
1169
mtklein449d9b72015-09-28 10:33:02 -07001170 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001171 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001172 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1173 this->draw(op, fCanvas);
1174 }
mtkleind31c13d2015-05-05 12:59:56 -07001175};
1176
mtkleind31c13d2015-05-05 12:59:56 -07001177// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1178// Then play back that macro picture into our wrapped sink.
1179Error ViaSingletonPictures::draw(
1180 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1181 auto size = src.size();
1182 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1183 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1184 SkRecord skr;
1185 SkRecorder recorder(&skr, size.width(), size.height());
1186 Error err = src.draw(&recorder);
1187 if (!err.isEmpty()) {
1188 return err;
1189 }
1190
1191 // Record our macro-picture, with each draw op as its own sub-picture.
1192 SkPictureRecorder macroRec;
1193 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1194 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001195
1196 SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
1197 const SkDrawableList empty;
1198
1199 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1200 macroCanvas,
1201 drawables ? *drawables : empty,
1202 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001203 for (int i = 0; i < skr.count(); i++) {
mtkleind31c13d2015-05-05 12:59:56 -07001204 skr.visit<void>(i, drawsAsSingletonPictures);
1205 }
1206 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture());
1207
1208 canvas->drawPicture(macroPic);
1209 return "";
1210 });
1211}
1212
mtklein748ca3b2015-01-15 10:56:12 -08001213} // namespace DM