blob: d98a9cab7ae81b0f84c8a4b37fdd87ff82624e07 [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"
mtklein748ca3b2015-01-15 10:56:12 -080031
halcanary7e798182015-04-14 14:06:18 -070032DEFINE_bool(multiPage, false, "For document-type backends, render the source"
33 " into multiple pages");
34
mtkleinb3e5e4d2015-03-25 13:13:43 -070035static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) {
36 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size));
reedd1146452015-09-25 06:56:57 -070037 return encoded && SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst);
mtkleinb3e5e4d2015-03-25 13:13:43 -070038}
39
mtklein748ca3b2015-01-15 10:56:12 -080040namespace DM {
41
mtklein748ca3b2015-01-15 10:56:12 -080042GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
43
44Error GMSrc::draw(SkCanvas* canvas) const {
halcanary96fcdcc2015-08-27 07:41:13 -070045 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080046 canvas->concat(gm->getInitialTransform());
47 gm->draw(canvas);
48 return "";
49}
50
51SkISize GMSrc::size() const {
halcanary96fcdcc2015-08-27 07:41:13 -070052 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080053 return gm->getISize();
54}
55
56Name GMSrc::name() const {
halcanary96fcdcc2015-08-27 07:41:13 -070057 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080058 return gm->getName();
59}
60
bsalomon4ee6bd82015-05-27 13:23:23 -070061void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
halcanary96fcdcc2015-08-27 07:41:13 -070062 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070063 gm->modifyGrContextOptions(options);
64}
65
mtklein748ca3b2015-01-15 10:56:12 -080066/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
67
msaretta5783ae2015-09-08 15:35:32 -070068BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoderInterface::Strategy strategy, Mode mode,
69 CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
70 : fPath(path)
71 , fStrategy(strategy)
72 , fMode(mode)
73 , fDstColorType(dstColorType)
74 , fSampleSize(sampleSize)
75{}
76
77bool BRDSrc::veto(SinkFlags flags) const {
78 // No need to test to non-raster or indirect backends.
79 return flags.type != SinkFlags::kRaster
80 || flags.approach != SinkFlags::kDirect;
81}
82
83static SkBitmapRegionDecoderInterface* create_brd(Path path,
84 SkBitmapRegionDecoderInterface::Strategy strategy) {
85 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
86 if (!encoded) {
87 return NULL;
88 }
89 return SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(new SkMemoryStream(encoded),
90 strategy);
91}
92
93Error BRDSrc::draw(SkCanvas* canvas) const {
94 SkColorType colorType = canvas->imageInfo().colorType();
95 if (kRGB_565_SkColorType == colorType &&
96 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
97 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
98 }
99 switch (fDstColorType) {
100 case CodecSrc::kGetFromCanvas_DstColorType:
101 break;
102 case CodecSrc::kIndex8_Always_DstColorType:
103 colorType = kIndex_8_SkColorType;
104 break;
105 case CodecSrc::kGrayscale_Always_DstColorType:
106 colorType = kGray_8_SkColorType;
107 break;
108 }
109
110 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(create_brd(fPath, fStrategy));
111 if (nullptr == brd.get()) {
112 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
113 }
114
115 const uint32_t width = brd->width();
116 const uint32_t height = brd->height();
117 // Visually inspecting very small output images is not necessary.
118 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
119 return Error::Nonfatal("Scaling very small images is uninteresting.");
120 }
121 switch (fMode) {
122 case kFullImage_Mode: {
123 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(0, 0, width, height, fSampleSize,
124 colorType));
125 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) {
126 return Error::Nonfatal("Cannot convert to color type.\n");
127 }
128 canvas->drawBitmap(*bitmap, 0, 0);
129 return "";
130 }
131 case kDivisor_Mode: {
132 const uint32_t divisor = 2;
133 if (width < divisor || height < divisor) {
134 return Error::Nonfatal("Divisor is larger than image dimension.\n");
135 }
136
137 // Use a border to test subsets that extend outside the image.
138 // We will not allow the border to be larger than the image dimensions. Allowing
139 // these large borders causes off by one errors that indicate a problem with the
140 // test suite, not a problem with the implementation.
141 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
142 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
143 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
144
145 // We may need to clear the canvas to avoid uninitialized memory.
146 // Assume we are scaling a 780x780 image with sampleSize = 8.
147 // The output image should be 97x97.
148 // Each subset will be 390x390.
149 // Each scaled subset be 48x48.
150 // Four scaled subsets will only fill a 96x96 image.
151 // The bottom row and last column will not be touched.
152 // This is an unfortunate result of our rounding rules when scaling.
153 // Maybe we need to consider testing scaled subsets without trying to
154 // combine them to match the full scaled image? Or maybe this is the
155 // best we can do?
156 canvas->clear(0);
157
158 for (uint32_t x = 0; x < divisor; x++) {
159 for (uint32_t y = 0; y < divisor; y++) {
160 // Calculate the subset dimensions
161 uint32_t subsetWidth = width / divisor;
162 uint32_t subsetHeight = height / divisor;
163 const int left = x * subsetWidth;
164 const int top = y * subsetHeight;
165
166 // Increase the size of the last subset in each row or column, when the
167 // divisor does not divide evenly into the image dimensions
168 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
169 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
170
171 // Increase the size of the subset in order to have a border on each side
172 const int decodeLeft = left - unscaledBorder;
173 const int decodeTop = top - unscaledBorder;
174 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
175 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
176 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(decodeLeft,
177 decodeTop, decodeWidth, decodeHeight, fSampleSize, colorType));
178 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) {
179 return Error::Nonfatal("Cannot convert to color type.\n");
180 }
181
182 canvas->drawBitmapRect(*bitmap,
183 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
184 (SkScalar) (subsetWidth / fSampleSize),
185 (SkScalar) (subsetHeight / fSampleSize)),
186 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
187 (SkScalar) (top / fSampleSize),
188 (SkScalar) (subsetWidth / fSampleSize),
189 (SkScalar) (subsetHeight / fSampleSize)),
190 nullptr);
191 }
192 }
193 return "";
194 }
195 default:
196 SkASSERT(false);
197 return "Error: Should not be reached.\n";
198 }
199}
200
201SkISize BRDSrc::size() const {
202 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(create_brd(fPath, fStrategy));
203 if (brd) {
204 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
205 SkTMax(1, brd->height() / (int) fSampleSize));
206 }
207 return SkISize::Make(0, 0);
208}
209
210static SkString get_scaled_name(const Path& path, float scale) {
211 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
212}
213
214Name BRDSrc::name() const {
215 // We will replicate the names used by CodecSrc so that images can
216 // be compared in Gold.
217 if (1 == fSampleSize) {
218 return SkOSPath::Basename(fPath.c_str());
219 }
msarett7f691442015-09-22 11:56:16 -0700220 return get_scaled_name(fPath, get_scale_from_sample_size(fSampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700221}
222
223/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
224
msarett0a242972015-06-11 14:27:27 -0700225CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale)
msarett438b2ad2015-04-09 12:43:10 -0700226 : fPath(path)
227 , fMode(mode)
228 , fDstColorType(dstColorType)
msarett0a242972015-06-11 14:27:27 -0700229 , fScale(scale)
msarett438b2ad2015-04-09 12:43:10 -0700230{}
mtklein748ca3b2015-01-15 10:56:12 -0800231
mtklein99cab4e2015-07-31 06:43:04 -0700232bool CodecSrc::veto(SinkFlags flags) const {
233 // No need to test decoding to non-raster or indirect backend.
mtkleine0effd62015-07-29 06:37:28 -0700234 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferred decode to
235 // let the GPU handle it.
mtklein99cab4e2015-07-31 06:43:04 -0700236 return flags.type != SinkFlags::kRaster
237 || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700238}
scroggo9b77ddd2015-03-19 06:03:39 -0700239
mtkleine0effd62015-07-29 06:37:28 -0700240Error CodecSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -0800241 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800242 if (!encoded) {
243 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
244 }
msarett9e707a02015-09-01 14:57:57 -0700245 SkAutoTDelete<SkCodec> codec(NULL);
246 if (kScaledCodec_Mode == fMode) {
247 codec.reset(SkScaledCodec::NewFromData(encoded));
248 // TODO (msarett): This should fall through to a fatal error once we support scaled
249 // codecs for all image types.
halcanary96fcdcc2015-08-27 07:41:13 -0700250 if (nullptr == codec.get()) {
msarett9e707a02015-09-01 14:57:57 -0700251 return Error::Nonfatal(SkStringPrintf("Couldn't create scaled codec for %s.",
252 fPath.c_str()));
emmaleer8f4ba762015-08-14 07:44:46 -0700253 }
msarett9e707a02015-09-01 14:57:57 -0700254 } else {
255 codec.reset(SkCodec::NewFromData(encoded));
256 }
257 if (nullptr == codec.get()) {
258 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700259 }
260
msarett438b2ad2015-04-09 12:43:10 -0700261 // Choose the color type to decode to
262 SkImageInfo decodeInfo = codec->getInfo();
mtkleine0effd62015-07-29 06:37:28 -0700263 SkColorType canvasColorType = canvas->imageInfo().colorType();
msarett438b2ad2015-04-09 12:43:10 -0700264 switch (fDstColorType) {
265 case kIndex8_Always_DstColorType:
msarette16b04a2015-04-15 07:32:19 -0700266 decodeInfo = codec->getInfo().makeColorType(kIndex_8_SkColorType);
267 if (kRGB_565_SkColorType == canvasColorType) {
268 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
269 }
270 break;
msarett438b2ad2015-04-09 12:43:10 -0700271 case kGrayscale_Always_DstColorType:
msarette16b04a2015-04-15 07:32:19 -0700272 decodeInfo = codec->getInfo().makeColorType(kGray_8_SkColorType);
msarett438b2ad2015-04-09 12:43:10 -0700273 if (kRGB_565_SkColorType == canvasColorType) {
274 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
275 }
276 break;
277 default:
278 decodeInfo = decodeInfo.makeColorType(canvasColorType);
279 break;
280 }
281
msarett0a242972015-06-11 14:27:27 -0700282 // Try to scale the image if it is desired
283 SkISize size = codec->getScaledDimensions(fScale);
284 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
285 return Error::Nonfatal("Test without scaling is uninteresting.");
286 }
msarettb32758a2015-08-18 13:22:46 -0700287
288 // Visually inspecting very small output images is not necessary. We will
289 // cover these cases in unit testing.
290 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
291 return Error::Nonfatal("Scaling very small images is uninteresting.");
292 }
msarett0a242972015-06-11 14:27:27 -0700293 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
294
msarett438b2ad2015-04-09 12:43:10 -0700295 // Construct a color table for the decode if necessary
halcanary96fcdcc2015-08-27 07:41:13 -0700296 SkAutoTUnref<SkColorTable> colorTable(nullptr);
297 SkPMColor* colorPtr = nullptr;
298 int* colorCountPtr = nullptr;
msarett438b2ad2015-04-09 12:43:10 -0700299 int maxColors = 256;
300 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
301 SkPMColor colors[256];
halcanary385fe4d2015-08-26 13:07:48 -0700302 colorTable.reset(new SkColorTable(colors, maxColors));
msarett438b2ad2015-04-09 12:43:10 -0700303 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
304 colorCountPtr = &maxColors;
305 }
306
307 // FIXME: Currently we cannot draw unpremultiplied sources.
scroggo9b77ddd2015-03-19 06:03:39 -0700308 if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) {
scroggo9b77ddd2015-03-19 06:03:39 -0700309 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
310 }
311
312 SkBitmap bitmap;
halcanary96fcdcc2015-08-27 07:41:13 -0700313 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
scroggo9b77ddd2015-03-19 06:03:39 -0700314 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
315 decodeInfo.width(), decodeInfo.height());
316 }
317
scroggo9c59ebc2015-03-25 13:48:49 -0700318 switch (fMode) {
msarett9e707a02015-09-01 14:57:57 -0700319 case kScaledCodec_Mode:
320 case kCodec_Mode: {
halcanary96fcdcc2015-08-27 07:41:13 -0700321 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), nullptr,
msarett438b2ad2015-04-09 12:43:10 -0700322 colorPtr, colorCountPtr)) {
scroggoeb602a52015-07-09 08:16:03 -0700323 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700324 // We consider incomplete to be valid, since we should still decode what is
325 // available.
scroggoeb602a52015-07-09 08:16:03 -0700326 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700327 break;
scroggoeb602a52015-07-09 08:16:03 -0700328 case SkCodec::kInvalidConversion:
scroggo9c59ebc2015-03-25 13:48:49 -0700329 return Error::Nonfatal("Incompatible colortype conversion");
330 default:
331 // Everything else is considered a failure.
332 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
333 }
emmaleer97002062015-05-27 12:36:10 -0700334 canvas->drawBitmap(bitmap, 0, 0);
scroggo9c59ebc2015-03-25 13:48:49 -0700335 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700336 }
scroggo9c59ebc2015-03-25 13:48:49 -0700337 case kScanline_Mode: {
scroggo46c57472015-09-30 08:57:13 -0700338 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
339 colorCountPtr)) {
msarett9e707a02015-09-01 14:57:57 -0700340 return Error::Nonfatal("Could not start scanline decoder");
scroggo9c59ebc2015-03-25 13:48:49 -0700341 }
scroggo1c005e42015-08-04 09:24:45 -0700342
msarett10522ff2015-09-07 08:54:01 -0700343 SkCodec::Result result = SkCodec::kUnimplemented;
scroggo46c57472015-09-30 08:57:13 -0700344 switch (codec->getScanlineOrder()) {
345 case SkCodec::kTopDown_SkScanlineOrder:
346 case SkCodec::kBottomUp_SkScanlineOrder:
347 case SkCodec::kNone_SkScanlineOrder:
348 result = codec->getScanlines(bitmap.getAddr(0, 0),
msarett10522ff2015-09-07 08:54:01 -0700349 decodeInfo.height(), bitmap.rowBytes());
350 break;
scroggo46c57472015-09-30 08:57:13 -0700351 case SkCodec::kOutOfOrder_SkScanlineOrder: {
msarett10522ff2015-09-07 08:54:01 -0700352 for (int y = 0; y < decodeInfo.height(); y++) {
scroggo46c57472015-09-30 08:57:13 -0700353 int dstY = codec->nextScanline();
msarett10522ff2015-09-07 08:54:01 -0700354 void* dstPtr = bitmap.getAddr(0, dstY);
scroggo46c57472015-09-30 08:57:13 -0700355 result = codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
msarett10522ff2015-09-07 08:54:01 -0700356 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
357 return SkStringPrintf("%s failed with error message %d",
358 fPath.c_str(), (int) result);
359 }
360 }
361 break;
362 }
363 }
364
emmaleer0a4c3cb2015-06-22 10:40:21 -0700365 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700366 case SkCodec::kSuccess:
367 case SkCodec::kIncompleteInput:
emmaleer0a4c3cb2015-06-22 10:40:21 -0700368 break;
369 default:
370 return SkStringPrintf("%s failed with error message %d",
371 fPath.c_str(), (int) result);
scroggo9c59ebc2015-03-25 13:48:49 -0700372 }
emmaleer97002062015-05-27 12:36:10 -0700373 canvas->drawBitmap(bitmap, 0, 0);
374 break;
375 }
376 case kScanline_Subset_Mode: {
377 //this mode decodes the image in divisor*divisor subsets, using a scanline decoder
378 const int divisor = 2;
379 const int w = decodeInfo.width();
380 const int h = decodeInfo.height();
emmaleer97002062015-05-27 12:36:10 -0700381 if (divisor > w || divisor > h) {
msarett70542572015-06-19 07:44:05 -0700382 return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d is too big"
383 "for %s with dimensions (%d x %d)", divisor, fPath.c_str(), w, h));
emmaleer97002062015-05-27 12:36:10 -0700384 }
385 const int subsetWidth = w/divisor;
386 const int subsetHeight = h/divisor;
387 // One of our subsets will be larger to contain any pixels that do not divide evenly.
388 const int extraX = w % divisor;
389 const int extraY = h % divisor;
390 /*
391 * if w or h are not evenly divided by divisor need to adjust width and height of end
392 * subsets to cover entire image.
393 * Add extraX and extraY to largestSubsetBm's width and height to adjust width
394 * and height of end subsets.
395 * subsetBm is extracted from largestSubsetBm.
396 * subsetBm's size is determined based on the current subset and may be larger for end
397 * subsets.
398 */
msarett0a242972015-06-11 14:27:27 -0700399 SkImageInfo largestSubsetDecodeInfo =
emmaleer97002062015-05-27 12:36:10 -0700400 decodeInfo.makeWH(subsetWidth + extraX, subsetHeight + extraY);
401 SkBitmap largestSubsetBm;
msarett9e707a02015-09-01 14:57:57 -0700402 if (!largestSubsetBm.tryAllocPixels(largestSubsetDecodeInfo, nullptr,
403 colorTable.get())) {
emmaleer97002062015-05-27 12:36:10 -0700404 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
405 largestSubsetDecodeInfo.width(), largestSubsetDecodeInfo.height());
406 }
emmaleer0a4c3cb2015-06-22 10:40:21 -0700407 const size_t rowBytes = decodeInfo.minRowBytes();
halcanary385fe4d2015-08-26 13:07:48 -0700408 char* buffer = new char[largestSubsetDecodeInfo.height() * rowBytes];
emmaleer0a4c3cb2015-06-22 10:40:21 -0700409 SkAutoTDeleteArray<char> lineDeleter(buffer);
emmaleer97002062015-05-27 12:36:10 -0700410 for (int col = 0; col < divisor; col++) {
411 //currentSubsetWidth may be larger than subsetWidth for rightmost subsets
412 const int currentSubsetWidth = (col + 1 == divisor) ?
413 subsetWidth + extraX : subsetWidth;
414 const int x = col * subsetWidth;
415 for (int row = 0; row < divisor; row++) {
416 //currentSubsetHeight may be larger than subsetHeight for bottom subsets
417 const int currentSubsetHeight = (row + 1 == divisor) ?
418 subsetHeight + extraY : subsetHeight;
419 const int y = row * subsetHeight;
420 //create scanline decoder for each subset
scroggo46c57472015-09-30 08:57:13 -0700421 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
422 colorCountPtr)
423 // TODO (msarett): Support this mode for all scanline orderings.
424 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
emmaleer97002062015-05-27 12:36:10 -0700425 if (x == 0 && y == 0) {
426 //first try, image may not be compatible
msarett5406d6f2015-08-31 06:55:13 -0700427 return Error::Nonfatal("Could not start top-down scanline decoder");
emmaleer97002062015-05-27 12:36:10 -0700428 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700429 return "Error scanline decoder is nullptr";
emmaleer97002062015-05-27 12:36:10 -0700430 }
431 }
432 //skip to first line of subset
scroggo46c57472015-09-30 08:57:13 -0700433 const SkCodec::Result skipResult = codec->skipScanlines(y);
emmaleer97002062015-05-27 12:36:10 -0700434 switch (skipResult) {
scroggoeb602a52015-07-09 08:16:03 -0700435 case SkCodec::kSuccess:
436 case SkCodec::kIncompleteInput:
emmaleer97002062015-05-27 12:36:10 -0700437 break;
438 default:
439 return SkStringPrintf("%s failed after attempting to skip %d scanlines"
440 "with error message %d", fPath.c_str(), y, (int) skipResult);
441 }
442 //create and set size of subsetBm
443 SkBitmap subsetBm;
444 SkIRect bounds = SkIRect::MakeWH(subsetWidth, subsetHeight);
445 bounds.setXYWH(0, 0, currentSubsetWidth, currentSubsetHeight);
446 SkAssertResult(largestSubsetBm.extractSubset(&subsetBm, bounds));
447 SkAutoLockPixels autlockSubsetBm(subsetBm, true);
scroggoeb602a52015-07-09 08:16:03 -0700448 const SkCodec::Result subsetResult =
scroggo46c57472015-09-30 08:57:13 -0700449 codec->getScanlines(buffer, currentSubsetHeight, rowBytes);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700450 switch (subsetResult) {
scroggoeb602a52015-07-09 08:16:03 -0700451 case SkCodec::kSuccess:
452 case SkCodec::kIncompleteInput:
emmaleer0a4c3cb2015-06-22 10:40:21 -0700453 break;
454 default:
mtkleind2baa902015-07-07 09:43:28 -0700455 return SkStringPrintf("%s failed with error message %d",
emmaleer0a4c3cb2015-06-22 10:40:21 -0700456 fPath.c_str(), (int) subsetResult);
emmaleer97002062015-05-27 12:36:10 -0700457 }
emmaleer0a4c3cb2015-06-22 10:40:21 -0700458 const size_t bpp = decodeInfo.bytesPerPixel();
mtkleind2baa902015-07-07 09:43:28 -0700459 /*
460 * we copy all the lines at once becuase when calling getScanlines for
461 * interlaced pngs the entire image must be read regardless of the number
emmaleer0a4c3cb2015-06-22 10:40:21 -0700462 * of lines requested. Reading an interlaced png in a loop, line-by-line, would
463 * decode the entire image height times, which is very slow
464 * it is aknowledged that copying each line as you read it in a loop
465 * may be faster for other types of images. Since this is a correctness test
466 * that's okay.
467 */
mtkleind2baa902015-07-07 09:43:28 -0700468 char* bufferRow = buffer;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700469 for (int subsetY = 0; subsetY < currentSubsetHeight; ++subsetY) {
mtkleind2baa902015-07-07 09:43:28 -0700470 memcpy(subsetBm.getAddr(0, subsetY), bufferRow + x*bpp,
emmaleer0a4c3cb2015-06-22 10:40:21 -0700471 currentSubsetWidth*bpp);
472 bufferRow += rowBytes;
473 }
mtkleind2baa902015-07-07 09:43:28 -0700474
scroggo4358f132015-07-30 11:33:04 -0700475 subsetBm.notifyPixelsChanged();
emmaleer97002062015-05-27 12:36:10 -0700476 canvas->drawBitmap(subsetBm, SkIntToScalar(x), SkIntToScalar(y));
477 }
478 }
scroggo9c59ebc2015-03-25 13:48:49 -0700479 break;
480 }
msarett0a242972015-06-11 14:27:27 -0700481 case kStripe_Mode: {
482 const int height = decodeInfo.height();
483 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
484 // does not align with image blocks.
485 const int stripeHeight = 37;
486 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
487
488 // Decode odd stripes
scroggo46c57472015-09-30 08:57:13 -0700489 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
490 colorCountPtr)
491 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
msarett9e707a02015-09-01 14:57:57 -0700492 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
493 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
494 // to run this test for image types that do not have this scanline ordering.
msarett5406d6f2015-08-31 06:55:13 -0700495 return Error::Nonfatal("Could not start top-down scanline decoder");
msarett0a242972015-06-11 14:27:27 -0700496 }
497 for (int i = 0; i < numStripes; i += 2) {
498 // Skip a stripe
499 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700500 SkCodec::Result result = codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700501 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700502 case SkCodec::kSuccess:
503 case SkCodec::kIncompleteInput:
msarett0a242972015-06-11 14:27:27 -0700504 break;
505 default:
506 return SkStringPrintf("Cannot skip scanlines for %s.", fPath.c_str());
507 }
508
509 // Read a stripe
510 const int startY = (i + 1) * stripeHeight;
511 const int linesToRead = SkTMin(stripeHeight, height - startY);
512 if (linesToRead > 0) {
scroggo46c57472015-09-30 08:57:13 -0700513 result = codec->getScanlines(bitmap.getAddr(0, startY),
msarett0a242972015-06-11 14:27:27 -0700514 linesToRead, bitmap.rowBytes());
515 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700516 case SkCodec::kSuccess:
517 case SkCodec::kIncompleteInput:
msarett0a242972015-06-11 14:27:27 -0700518 break;
519 default:
520 return SkStringPrintf("Cannot get scanlines for %s.", fPath.c_str());
521 }
522 }
523 }
524
525 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700526 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
527 colorPtr, colorCountPtr);
scroggo1c005e42015-08-04 09:24:45 -0700528 if (SkCodec::kSuccess != startResult) {
529 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700530 }
531 for (int i = 0; i < numStripes; i += 2) {
532 // Read a stripe
533 const int startY = i * stripeHeight;
534 const int linesToRead = SkTMin(stripeHeight, height - startY);
scroggo46c57472015-09-30 08:57:13 -0700535 SkCodec::Result result = codec->getScanlines(bitmap.getAddr(0, startY),
msarett0a242972015-06-11 14:27:27 -0700536 linesToRead, bitmap.rowBytes());
537 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700538 case SkCodec::kSuccess:
539 case SkCodec::kIncompleteInput:
msarett0a242972015-06-11 14:27:27 -0700540 break;
541 default:
542 return SkStringPrintf("Cannot get scanlines for %s.", fPath.c_str());
543 }
544
545 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700546 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
547 if (linesToSkip > 0) {
scroggo46c57472015-09-30 08:57:13 -0700548 result = codec->skipScanlines(linesToSkip);
msarettf6db27e2015-06-12 09:34:04 -0700549 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700550 case SkCodec::kSuccess:
551 case SkCodec::kIncompleteInput:
msarettf6db27e2015-06-12 09:34:04 -0700552 break;
553 default:
554 return SkStringPrintf("Cannot skip scanlines for %s.", fPath.c_str());
555 }
msarett0a242972015-06-11 14:27:27 -0700556 }
557 }
558 canvas->drawBitmap(bitmap, 0, 0);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700559 break;
msarett0a242972015-06-11 14:27:27 -0700560 }
scroggob636b452015-07-22 07:16:20 -0700561 case kSubset_Mode: {
562 // Arbitrarily choose a divisor.
563 int divisor = 2;
564 // Total width/height of the image.
565 const int W = codec->getInfo().width();
566 const int H = codec->getInfo().height();
567 if (divisor > W || divisor > H) {
568 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
569 "for %s with dimensions (%d x %d)", divisor,
570 fPath.c_str(), W, H));
571 }
572 // subset dimensions
573 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
574 const int w = SkAlign2(W / divisor);
575 const int h = SkAlign2(H / divisor);
576 SkIRect subset;
577 SkCodec::Options opts;
578 opts.fSubset = &subset;
579 SkBitmap subsetBm;
580 // We will reuse pixel memory from bitmap.
581 void* pixels = bitmap.getPixels();
582 // Keep track of left and top (for drawing subsetBm into canvas). We could use
583 // fScale * x and fScale * y, but we want integers such that the next subset will start
584 // where the last one ended. So we'll add decodeInfo.width() and height().
585 int left = 0;
586 for (int x = 0; x < W; x += w) {
587 int top = 0;
588 for (int y = 0; y < H; y+= h) {
589 // Do not make the subset go off the edge of the image.
590 const int preScaleW = SkTMin(w, W - x);
591 const int preScaleH = SkTMin(h, H - y);
592 subset.setXYWH(x, y, preScaleW, preScaleH);
593 // And scale
594 // FIXME: Should we have a version of getScaledDimensions that takes a subset
595 // into account?
596 decodeInfo = decodeInfo.makeWH(SkScalarRoundToInt(preScaleW * fScale),
597 SkScalarRoundToInt(preScaleH * fScale));
598 size_t rowBytes = decodeInfo.minRowBytes();
599 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700600 nullptr, nullptr)) {
scroggob636b452015-07-22 07:16:20 -0700601 return SkStringPrintf("could not install pixels for %s.", fPath.c_str());
602 }
603 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
604 &opts, colorPtr, colorCountPtr);
605 switch (result) {
606 case SkCodec::kSuccess:
607 case SkCodec::kIncompleteInput:
608 break;
609 case SkCodec::kInvalidConversion:
610 if (0 == (x|y)) {
611 // First subset is okay to return unimplemented.
612 return Error::Nonfatal("Incompatible colortype conversion");
613 }
614 // If the first subset succeeded, a later one should not fail.
615 // fall through to failure
616 case SkCodec::kUnimplemented:
617 if (0 == (x|y)) {
618 // First subset is okay to return unimplemented.
619 return Error::Nonfatal("subset codec not supported");
620 }
621 // If the first subset succeeded, why would a later one fail?
622 // fall through to failure
623 default:
624 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
625 "from %s with dimensions (%d x %d)\t error %d",
626 x, y, decodeInfo.width(), decodeInfo.height(),
627 fPath.c_str(), W, H, result);
628 }
629 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top));
630 // translate by the scaled height.
631 top += decodeInfo.height();
632 }
633 // translate by the scaled width.
634 left += decodeInfo.width();
635 }
636 return "";
637 }
scroggo9b77ddd2015-03-19 06:03:39 -0700638 }
scroggo9c59ebc2015-03-25 13:48:49 -0700639 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700640}
641
642SkISize CodecSrc::size() const {
643 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
scroggo7fac5af2015-09-30 11:33:12 -0700644 SkAutoTDelete<SkCodec> codec(nullptr);
645
646 if (kScaledCodec_Mode == fMode) {
647 codec.reset(SkScaledCodec::NewFromData(encoded));
648 } else {
emmaleer8f4ba762015-08-14 07:44:46 -0700649 codec.reset(SkCodec::NewFromData(encoded));
msarett9bde9182015-03-25 05:27:48 -0700650 }
scroggo7fac5af2015-09-30 11:33:12 -0700651
652 if (nullptr == codec) {
653 return SkISize::Make(0, 0);
654 }
655 return codec->getScaledDimensions(fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700656}
657
658Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700659 if (1.0f == fScale) {
660 return SkOSPath::Basename(fPath.c_str());
msarett0a242972015-06-11 14:27:27 -0700661 }
msaretta5783ae2015-09-08 15:35:32 -0700662 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700663}
664
665/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
666
667ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
668
mtklein99cab4e2015-07-31 06:43:04 -0700669bool ImageSrc::veto(SinkFlags flags) const {
670 // No need to test decoding to non-raster or indirect backend.
mtkleine0effd62015-07-29 06:37:28 -0700671 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YUV.
mtklein99cab4e2015-07-31 06:43:04 -0700672 return flags.type != SinkFlags::kRaster
673 || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700674}
scroggo9b77ddd2015-03-19 06:03:39 -0700675
mtkleine0effd62015-07-29 06:37:28 -0700676Error ImageSrc::draw(SkCanvas* canvas) const {
scroggo9b77ddd2015-03-19 06:03:39 -0700677 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
678 if (!encoded) {
679 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
680 }
mtkleine0effd62015-07-29 06:37:28 -0700681 const SkColorType dstColorType = canvas->imageInfo().colorType();
mtkleinedc93bc2015-01-30 13:22:23 -0800682 if (fDivisor == 0) {
mtklein748ca3b2015-01-15 10:56:12 -0800683 // Decode the full image.
684 SkBitmap bitmap;
scroggo9b77ddd2015-03-19 06:03:39 -0700685 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
686 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
687 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
688 }
689 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) {
690 // Do not draw a bitmap with alpha to a destination without alpha.
691 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
mtklein748ca3b2015-01-15 10:56:12 -0800692 }
halcanary96fcdcc2015-08-27 07:41:13 -0700693 encoded.reset((SkData*)nullptr); // Might as well drop this when we're done with it.
mtklein748ca3b2015-01-15 10:56:12 -0800694 canvas->drawBitmap(bitmap, 0,0);
695 return "";
696 }
mtkleinedc93bc2015-01-30 13:22:23 -0800697 // Decode subsets. This is a little involved.
scroggoa1193e42015-01-21 12:09:53 -0800698 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
699 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
mtklein748ca3b2015-01-15 10:56:12 -0800700 if (!decoder) {
701 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
702 }
scroggoa1193e42015-01-21 12:09:53 -0800703 stream->rewind();
mtklein748ca3b2015-01-15 10:56:12 -0800704 int w,h;
msarett70542572015-06-19 07:44:05 -0700705 if (!decoder->buildTileIndex(stream.detach(), &w, &h)) {
mtklein4089ef72015-03-05 08:40:28 -0800706 return Error::Nonfatal("Subset decoding not supported.");
mtklein748ca3b2015-01-15 10:56:12 -0800707 }
mtkleinedc93bc2015-01-30 13:22:23 -0800708
709 // Divide the image into subsets that cover the entire image.
710 if (fDivisor > w || fDivisor > h) {
msarett70542572015-06-19 07:44:05 -0700711 return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d is too big"
712 "for %s with dimensions (%d x %d)", fDivisor, fPath.c_str(), w, h));
mtkleinedc93bc2015-01-30 13:22:23 -0800713 }
714 const int subsetWidth = w / fDivisor,
715 subsetHeight = h / fDivisor;
716 for (int y = 0; y < h; y += subsetHeight) {
717 for (int x = 0; x < w; x += subsetWidth) {
718 SkBitmap subset;
719 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
720 if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
721 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
722 x, y, x+subsetWidth, y+subsetHeight);
723 }
scroggo56e25dd2015-03-05 11:46:40 -0800724 if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) {
725 // Do not draw a bitmap with alpha to a destination without alpha.
726 // This is not an error, but there is nothing interesting to show.
727
728 // This should only happen on the first iteration through the loop.
729 SkASSERT(0 == x && 0 == y);
730
731 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
732 }
mtkleinedc93bc2015-01-30 13:22:23 -0800733 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
mtklein748ca3b2015-01-15 10:56:12 -0800734 }
mtklein748ca3b2015-01-15 10:56:12 -0800735 }
736 return "";
737}
738
739SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -0800740 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
scroggo9b77ddd2015-03-19 06:03:39 -0700741 SkBitmap bitmap;
742 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
743 encoded->size(),
744 &bitmap,
745 kUnknown_SkColorType,
746 SkImageDecoder::kDecodeBounds_Mode)) {
747 return SkISize::Make(0,0);
mtklein748ca3b2015-01-15 10:56:12 -0800748 }
scroggo9b77ddd2015-03-19 06:03:39 -0700749 return bitmap.dimensions();
mtklein748ca3b2015-01-15 10:56:12 -0800750}
751
mtklein9264a952015-01-20 10:11:53 -0800752Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800753 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800754}
mtklein748ca3b2015-01-15 10:56:12 -0800755
756/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
757
mtkleinf4ba3212015-01-28 15:32:24 -0800758static const SkRect kSKPViewport = {0,0, 1000,1000};
759
mtklein8d17a132015-01-30 11:42:31 -0800760SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800761
762Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800763 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800764 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800765 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
766 }
mtkleinb3e5e4d2015-03-25 13:13:43 -0700767 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap));
mtklein75d98fd2015-01-18 07:05:01 -0800768 if (!pic) {
769 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
770 }
halcanary96fcdcc2015-08-27 07:41:13 -0700771 stream.reset((SkStream*)nullptr); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -0700772
mtkleinf4ba3212015-01-28 15:32:24 -0800773 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800774 canvas->drawPicture(pic);
775 return "";
776}
777
778SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700779 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
780 if (!stream) {
781 return SkISize::Make(0,0);
782 }
783 SkPictInfo info;
784 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
785 return SkISize::Make(0,0);
786 }
787 SkRect viewport = kSKPViewport;
788 if (!viewport.intersect(info.fCullRect)) {
789 return SkISize::Make(0,0);
790 }
791 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800792}
793
794Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
795
796/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
797
mtkleinad66f9b2015-02-13 15:11:10 -0800798Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
799 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
800 return src.draw(canvas);
801}
802
803/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
804
mtkleinb9eb4ac2015-02-02 18:26:03 -0800805DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
806
mtklein82d28432015-01-15 12:46:02 -0800807GPUSink::GPUSink(GrContextFactory::GLContextType ct,
808 GrGLStandard api,
809 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -0700810 bool diText,
mtklein82d28432015-01-15 12:46:02 -0800811 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800812 : fContextType(ct)
813 , fGpuAPI(api)
814 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -0700815 , fUseDIText(diText)
mtklein82d28432015-01-15 12:46:02 -0800816 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800817
818int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800819 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800820}
821
joshualitt5f5a8d72015-02-25 14:09:45 -0800822void PreAbandonGpuContextErrorHandler(SkError, void*) {}
823
mtkleinb9eb4ac2015-02-02 18:26:03 -0800824Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
bsalomon4ee6bd82015-05-27 13:23:23 -0700825 GrContextOptions options;
826 src.modifyGrContextOptions(&options);
827
828 GrContextFactory factory(options);
mtkleinf4ba3212015-01-28 15:32:24 -0800829 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800830 const SkImageInfo info =
831 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
832 SkAutoTUnref<SkSurface> surface(
bsalomonafcd7cd2015-08-31 12:39:41 -0700833 NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -0800834 if (!surface) {
835 return "Could not create a surface.";
836 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800837 if (FLAGS_preAbandonGpuContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700838 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr);
joshualitt5f5a8d72015-02-25 14:09:45 -0800839 factory.abandonContexts();
840 }
mtklein748ca3b2015-01-15 10:56:12 -0800841 SkCanvas* canvas = surface->getCanvas();
842 Error err = src.draw(canvas);
843 if (!err.isEmpty()) {
844 return err;
845 }
846 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800847 if (FLAGS_gpuStats) {
848 canvas->getGrContext()->dumpCacheStats(log);
849 canvas->getGrContext()->dumpGpuStats(log);
850 }
mtklein748ca3b2015-01-15 10:56:12 -0800851 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800852 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800853 if (FLAGS_abandonGpuContext) {
854 factory.abandonContexts();
855 }
mtklein748ca3b2015-01-15 10:56:12 -0800856 return "";
857}
858
859/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
860
halcanary47ef4d52015-03-03 09:13:09 -0800861static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
862 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
863 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800864 int width = src.size().width(),
865 height = src.size().height();
866
halcanary7e798182015-04-14 14:06:18 -0700867 if (FLAGS_multiPage) {
868 const int kLetterWidth = 612, // 8.5 * 72
869 kLetterHeight = 792; // 11 * 72
870 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
871 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -0800872
halcanary7e798182015-04-14 14:06:18 -0700873 int xPages = ((width - 1) / kLetterWidth) + 1;
874 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -0800875
halcanary7e798182015-04-14 14:06:18 -0700876 for (int y = 0; y < yPages; ++y) {
877 for (int x = 0; x < xPages; ++x) {
878 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
879 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
880 SkCanvas* canvas =
881 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
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 canvas->clipRect(letter);
886 canvas->translate(-letter.width() * x, -letter.height() * y);
887 Error err = src.draw(canvas);
888 if (!err.isEmpty()) {
889 return err;
890 }
891 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -0700892 }
halcanaryfd4a9932015-01-28 11:45:58 -0800893 }
halcanary7e798182015-04-14 14:06:18 -0700894 } else {
895 SkCanvas* canvas =
896 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
897 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700898 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700899 }
900 Error err = src.draw(canvas);
901 if (!err.isEmpty()) {
902 return err;
903 }
904 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -0800905 }
halcanary7e798182015-04-14 14:06:18 -0700906 if (!doc->close()) {
907 return "SkDocument::close() returned false";
908 }
halcanaryfd4a9932015-01-28 11:45:58 -0800909 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800910 return "";
911}
912
halcanaryc11c62f2015-09-28 11:51:54 -0700913PDFSink::PDFSink(const char* rasterizer) : fRasterizer(rasterizer) {}
halcanary47ef4d52015-03-03 09:13:09 -0800914
915Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
916 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
917 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700918 return "SkDocument::CreatePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800919 }
halcanaryf12a1672015-09-23 12:45:49 -0700920 SkTArray<SkDocument::Attribute> info;
921 info.emplace_back(SkString("Title"), src.name());
922 info.emplace_back(SkString("Subject"),
923 SkString("rendering correctness test"));
924 info.emplace_back(SkString("Creator"), SkString("Skia/DM"));
halcanaryc11c62f2015-09-28 11:51:54 -0700925
926 info.emplace_back(SkString("Keywords"),
927 SkStringPrintf("Rasterizer:%s;", fRasterizer));
halcanaryf12a1672015-09-23 12:45:49 -0700928 doc->setMetadata(info, nullptr, nullptr);
halcanary47ef4d52015-03-03 09:13:09 -0800929 return draw_skdocument(src, doc.get(), dst);
930}
931
932/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
933
934XPSSink::XPSSink() {}
935
936Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
937 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
938 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700939 return "SkDocument::CreateXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800940 }
941 return draw_skdocument(src, doc.get(), dst);
942}
mtklein748ca3b2015-01-15 10:56:12 -0800943/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
944
mtklein9c3f17d2015-01-28 11:35:18 -0800945SKPSink::SKPSink() {}
946
mtkleinb9eb4ac2015-02-02 18:26:03 -0800947Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800948 SkSize size;
949 size = src.size();
950 SkPictureRecorder recorder;
951 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
952 if (!err.isEmpty()) {
953 return err;
954 }
955 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
956 pic->serialize(dst);
957 return "";
958}
959
960/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
961
mtklein8a4527e2015-01-31 20:00:58 -0800962SVGSink::SVGSink() {}
963
mtkleinb9eb4ac2015-02-02 18:26:03 -0800964Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary385fe4d2015-08-26 13:07:48 -0700965 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
fmalita2aafe6f2015-02-06 12:51:10 -0800966 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
967 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
968 xmlWriter));
969 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800970}
971
972/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
973
mtklein748ca3b2015-01-15 10:56:12 -0800974RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
975
mtkleinb9eb4ac2015-02-02 18:26:03 -0800976Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800977 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800978 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
979 SkAlphaType alphaType = kPremul_SkAlphaType;
980 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
981
982 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
983 dst->eraseColor(SK_ColorTRANSPARENT);
984 SkCanvas canvas(*dst);
985 return src.draw(&canvas);
986}
987
988/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
989
mtkleina16e69e2015-05-05 11:38:45 -0700990// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -0700991// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -0700992// Several examples below.
993
mtkleina16e69e2015-05-05 11:38:45 -0700994static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtkleine44b5082015-05-07 10:53:34 -0700995 SkISize size, SkFunction<Error(SkCanvas*)> draw) {
mtkleina16e69e2015-05-05 11:38:45 -0700996 class ProxySrc : public Src {
997 public:
mtkleine44b5082015-05-07 10:53:34 -0700998 ProxySrc(SkISize size, SkFunction<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -0700999 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
1000 Name name() const override { sk_throw(); return ""; } // Won't be called.
1001 SkISize size() const override { return fSize; }
1002 private:
mtkleine44b5082015-05-07 10:53:34 -07001003 SkISize fSize;
1004 SkFunction<Error(SkCanvas*)> fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001005 };
1006 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
1007}
1008
1009/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1010
mtkleind603b222015-02-17 11:13:33 -08001011static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1012 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1013 matrix->mapRect(&bounds);
1014 matrix->postTranslate(-bounds.x(), -bounds.y());
1015 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
1016}
1017
mtklein78829242015-05-06 07:54:07 -07001018ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001019
mtkleinb9eb4ac2015-02-02 18:26:03 -08001020Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001021 SkMatrix matrix = fMatrix;
1022 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
1023 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
1024 canvas->concat(matrix);
1025 return src.draw(canvas);
1026 });
mtklein748ca3b2015-01-15 10:56:12 -08001027}
1028
mtkleind603b222015-02-17 11:13:33 -08001029// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1030// This should be pixel-preserving.
mtklein78829242015-05-06 07:54:07 -07001031ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001032
1033Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1034 Error err = fSink->draw(src, bitmap, stream, log);
1035 if (!err.isEmpty()) {
1036 return err;
1037 }
1038
1039 SkMatrix inverse;
1040 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1041 return "Cannot upright --matrix.";
1042 }
1043 SkMatrix upright = SkMatrix::I();
1044 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1045 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1046 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1047 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1048
1049 SkBitmap uprighted;
1050 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1051 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1052
1053 SkCanvas canvas(uprighted);
1054 canvas.concat(upright);
1055 SkPaint paint;
1056 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1057 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1058
1059 *bitmap = uprighted;
1060 bitmap->lockPixels();
1061 return "";
1062}
1063
mtklein748ca3b2015-01-15 10:56:12 -08001064/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1065
mtkleinb9eb4ac2015-02-02 18:26:03 -08001066Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001067 auto size = src.size();
1068 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
1069 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
1070 SkGPipeWriter pipe;
reed451af502015-08-19 08:18:04 -07001071 const uint32_t kFlags = 0;
mtkleina16e69e2015-05-05 11:38:45 -07001072 return src.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
1073 });
mtklein748ca3b2015-01-15 10:56:12 -08001074}
1075
1076/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtkleina16e69e2015-05-05 11:38:45 -07001077
mtkleina16e69e2015-05-05 11:38:45 -07001078Error ViaSerialization::draw(
1079 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001080 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001081 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001082 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001083 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1084 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001085 if (!err.isEmpty()) {
1086 return err;
1087 }
1088 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
1089
1090 // Serialize it and then deserialize it.
1091 SkDynamicMemoryWStream wStream;
1092 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -08001093 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
mtkleinb3e5e4d2015-03-25 13:13:43 -07001094 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap));
mtklein748ca3b2015-01-15 10:56:12 -08001095
mtkleina16e69e2015-05-05 11:38:45 -07001096 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
1097 canvas->drawPicture(deserialized);
1098 return "";
1099 });
mtklein748ca3b2015-01-15 10:56:12 -08001100}
1101
1102/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1103
1104ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
mtklein78829242015-05-06 07:54:07 -07001105 : Via(sink)
1106 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001107 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001108 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001109
mtkleinb9eb4ac2015-02-02 18:26:03 -08001110Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001111 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001112 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001113 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1114 SkIntToScalar(size.height()),
1115 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001116 if (!err.isEmpty()) {
1117 return err;
1118 }
mtkleinb7e8d692015-04-07 08:30:32 -07001119 SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001120
mtkleina16e69e2015-05-05 11:38:45 -07001121 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
1122 const int xTiles = (size.width() + fW - 1) / fW,
1123 yTiles = (size.height() + fH - 1) / fH;
1124 SkMultiPictureDraw mpd(xTiles*yTiles);
1125 SkTDArray<SkSurface*> surfaces;
1126 surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001127
mtkleina16e69e2015-05-05 11:38:45 -07001128 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1129 for (int j = 0; j < yTiles; j++) {
1130 for (int i = 0; i < xTiles; i++) {
1131 // This lets our ultimate Sink determine the best kind of surface.
1132 // E.g., if it's a GpuSink, the surfaces and images are textures.
1133 SkSurface* s = canvas->newSurface(info);
1134 if (!s) {
1135 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001136 }
mtkleina16e69e2015-05-05 11:38:45 -07001137 surfaces.push(s);
1138 SkCanvas* c = s->getCanvas();
1139 c->translate(SkIntToScalar(-i * fW),
1140 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
1141 mpd.add(c, pic);
mtklein748ca3b2015-01-15 10:56:12 -08001142 }
mtklein748ca3b2015-01-15 10:56:12 -08001143 }
mtkleina16e69e2015-05-05 11:38:45 -07001144 mpd.draw();
1145 for (int j = 0; j < yTiles; j++) {
1146 for (int i = 0; i < xTiles; i++) {
1147 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
1148 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1149 }
1150 }
1151 surfaces.unrefAll();
1152 return "";
1153 });
mtklein748ca3b2015-01-15 10:56:12 -08001154}
1155
mtkleinb7e8d692015-04-07 08:30:32 -07001156/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1157
mtkleinb7e8d692015-04-07 08:30:32 -07001158// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1159// This tests that any shortcuts we may take while recording that second picture are legal.
1160Error ViaSecondPicture::draw(
1161 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001162 auto size = src.size();
1163 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1164 SkPictureRecorder recorder;
1165 SkAutoTUnref<SkPicture> pic;
1166 for (int i = 0; i < 2; i++) {
1167 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1168 SkIntToScalar(size.height())));
1169 if (!err.isEmpty()) {
1170 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001171 }
mtkleina16e69e2015-05-05 11:38:45 -07001172 pic.reset(recorder.endRecordingAsPicture());
mtkleinb7e8d692015-04-07 08:30:32 -07001173 }
mtkleina16e69e2015-05-05 11:38:45 -07001174 canvas->drawPicture(pic);
1175 return "";
1176 });
mtkleinb7e8d692015-04-07 08:30:32 -07001177}
1178
mtkleind31c13d2015-05-05 12:59:56 -07001179/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1180
mtklein6fbf4b32015-05-06 11:35:40 -07001181// Draw the Src twice. This can help exercise caching.
1182Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1183 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
1184 for (int i = 0; i < 2; i++) {
1185 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1186 canvas->clear(SK_ColorTRANSPARENT);
1187 Error err = src.draw(canvas);
1188 if (err.isEmpty()) {
1189 return err;
1190 }
1191 }
1192 return "";
1193 });
1194}
1195
1196/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1197
mtkleind31c13d2015-05-05 12:59:56 -07001198// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1199// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1200// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1201struct DrawsAsSingletonPictures {
1202 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001203 const SkDrawableList& fDrawables;
mtkleind31c13d2015-05-05 12:59:56 -07001204
mtkleind31c13d2015-05-05 12:59:56 -07001205 template <typename T>
1206 void draw(const T& op, SkCanvas* canvas) {
1207 // We must pass SkMatrix::I() as our initial matrix.
1208 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1209 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001210 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1211 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001212 }
1213
mtklein449d9b72015-09-28 10:33:02 -07001214 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001215 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001216 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001217 SkPictureRecorder rec;
1218 this->draw(op, rec.beginRecording(SkRect::MakeLargest()));
1219 SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture());
1220 fCanvas->drawPicture(pic);
1221 }
1222
mtklein449d9b72015-09-28 10:33:02 -07001223 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001224 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001225 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1226 this->draw(op, fCanvas);
1227 }
mtkleind31c13d2015-05-05 12:59:56 -07001228};
1229
mtkleind31c13d2015-05-05 12:59:56 -07001230// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1231// Then play back that macro picture into our wrapped sink.
1232Error ViaSingletonPictures::draw(
1233 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1234 auto size = src.size();
1235 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1236 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1237 SkRecord skr;
1238 SkRecorder recorder(&skr, size.width(), size.height());
1239 Error err = src.draw(&recorder);
1240 if (!err.isEmpty()) {
1241 return err;
1242 }
1243
1244 // Record our macro-picture, with each draw op as its own sub-picture.
1245 SkPictureRecorder macroRec;
1246 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1247 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001248
1249 SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
1250 const SkDrawableList empty;
1251
1252 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1253 macroCanvas,
1254 drawables ? *drawables : empty,
1255 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001256 for (int i = 0; i < skr.count(); i++) {
mtkleind31c13d2015-05-05 12:59:56 -07001257 skr.visit<void>(i, drawsAsSingletonPictures);
1258 }
1259 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture());
1260
1261 canvas->drawPicture(macroPic);
1262 return "";
1263 });
1264}
1265
mtklein748ca3b2015-01-15 10:56:12 -08001266} // namespace DM