blob: bbfa5199e61f06bead0549fad66d11fa40313a23 [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMSrcSink.h"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
scroggof24f2242015-03-03 08:59:20 -080010#include "SkCodec.h"
mtkleina16e69e2015-05-05 11:38:45 -070011#include "SkCommonFlags.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070012#include "SkData.h"
mtklein748ca3b2015-01-15 10:56:12 -080013#include "SkDocument.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080014#include "SkError.h"
mtkleinb3e5e4d2015-03-25 13:13:43 -070015#include "SkImageGenerator.h"
mtkleinc8be09a2016-01-04 18:56:57 -080016#include "SkMallocPixelRef.h"
mtklein748ca3b2015-01-15 10:56:12 -080017#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080018#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080019#include "SkOSFile.h"
mtkleinffa901a2015-03-16 10:38:07 -070020#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080021#include "SkPictureRecorder.h"
22#include "SkRandom.h"
mtkleind31c13d2015-05-05 12:59:56 -070023#include "SkRecordDraw.h"
24#include "SkRecorder.h"
mtklein2e2ea382015-10-16 10:29:41 -070025#include "SkRemote.h"
fmalita2aafe6f2015-02-06 12:51:10 -080026#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080027#include "SkStream.h"
mtklein449d9b72015-09-28 10:33:02 -070028#include "SkTLogic.h"
fmalita2aafe6f2015-02-06 12:51:10 -080029#include "SkXMLWriter.h"
msarette6dd0042015-10-09 11:07:34 -070030#include "SkSwizzler.h"
mtklein64593522015-11-12 10:41:05 -080031#include <functional>
mtklein748ca3b2015-01-15 10:56:12 -080032
halcanary7e798182015-04-14 14:06:18 -070033DEFINE_bool(multiPage, false, "For document-type backends, render the source"
34 " into multiple pages");
35
mtkleinb3e5e4d2015-03-25 13:13:43 -070036static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) {
37 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size));
reedd1146452015-09-25 06:56:57 -070038 return encoded && SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst);
mtkleinb3e5e4d2015-03-25 13:13:43 -070039}
40
mtklein748ca3b2015-01-15 10:56:12 -080041namespace DM {
42
mtklein748ca3b2015-01-15 10:56:12 -080043GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
44
45Error GMSrc::draw(SkCanvas* canvas) const {
halcanary96fcdcc2015-08-27 07:41:13 -070046 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080047 canvas->concat(gm->getInitialTransform());
48 gm->draw(canvas);
49 return "";
50}
51
52SkISize GMSrc::size() const {
halcanary96fcdcc2015-08-27 07:41:13 -070053 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080054 return gm->getISize();
55}
56
57Name GMSrc::name() const {
halcanary96fcdcc2015-08-27 07:41:13 -070058 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
mtklein748ca3b2015-01-15 10:56:12 -080059 return gm->getName();
60}
61
bsalomon4ee6bd82015-05-27 13:23:23 -070062void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
halcanary96fcdcc2015-08-27 07:41:13 -070063 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
bsalomon4ee6bd82015-05-27 13:23:23 -070064 gm->modifyGrContextOptions(options);
65}
66
mtklein748ca3b2015-01-15 10:56:12 -080067/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
68
msarett5cb48852015-11-06 08:56:32 -080069BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode,
msaretta5783ae2015-09-08 15:35:32 -070070 CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
71 : fPath(path)
72 , fStrategy(strategy)
73 , fMode(mode)
74 , fDstColorType(dstColorType)
75 , fSampleSize(sampleSize)
76{}
77
78bool BRDSrc::veto(SinkFlags flags) const {
79 // No need to test to non-raster or indirect backends.
80 return flags.type != SinkFlags::kRaster
81 || flags.approach != SinkFlags::kDirect;
82}
83
msarett5cb48852015-11-06 08:56:32 -080084static SkBitmapRegionDecoder* create_brd(Path path,
85 SkBitmapRegionDecoder::Strategy strategy) {
msaretta5783ae2015-09-08 15:35:32 -070086 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
87 if (!encoded) {
88 return NULL;
89 }
msarett5cb48852015-11-06 08:56:32 -080090 return SkBitmapRegionDecoder::Create(encoded, strategy);
msaretta5783ae2015-09-08 15:35:32 -070091}
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
msarett5cb48852015-11-06 08:56:32 -0800110 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700111 if (nullptr == brd.get()) {
112 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
113 }
114
msarett04965c62015-10-12 10:24:38 -0700115 if (!brd->conversionSupported(colorType)) {
mtklein9b439152015-12-09 13:02:26 -0800116 return Error::Nonfatal("Cannot convert to color type.");
msarett04965c62015-10-12 10:24:38 -0700117 }
118
msaretta5783ae2015-09-08 15:35:32 -0700119 const uint32_t width = brd->width();
120 const uint32_t height = brd->height();
121 // Visually inspecting very small output images is not necessary.
122 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
123 return Error::Nonfatal("Scaling very small images is uninteresting.");
124 }
125 switch (fMode) {
126 case kFullImage_Mode: {
msarett35e5d1b2015-10-27 12:50:25 -0700127 SkBitmap bitmap;
128 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
129 fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800130 return "Cannot decode (full) region.";
msarett35e5d1b2015-10-27 12:50:25 -0700131 }
132 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800133 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700134 }
msarett35e5d1b2015-10-27 12:50:25 -0700135 canvas->drawBitmap(bitmap, 0, 0);
msaretta5783ae2015-09-08 15:35:32 -0700136 return "";
137 }
138 case kDivisor_Mode: {
139 const uint32_t divisor = 2;
140 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800141 return Error::Nonfatal("Divisor is larger than image dimension.");
msaretta5783ae2015-09-08 15:35:32 -0700142 }
143
144 // Use a border to test subsets that extend outside the image.
145 // We will not allow the border to be larger than the image dimensions. Allowing
146 // these large borders causes off by one errors that indicate a problem with the
147 // test suite, not a problem with the implementation.
148 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor);
149 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
150 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
151
152 // We may need to clear the canvas to avoid uninitialized memory.
153 // Assume we are scaling a 780x780 image with sampleSize = 8.
154 // The output image should be 97x97.
155 // Each subset will be 390x390.
156 // Each scaled subset be 48x48.
157 // Four scaled subsets will only fill a 96x96 image.
158 // The bottom row and last column will not be touched.
159 // This is an unfortunate result of our rounding rules when scaling.
160 // Maybe we need to consider testing scaled subsets without trying to
161 // combine them to match the full scaled image? Or maybe this is the
162 // best we can do?
163 canvas->clear(0);
164
165 for (uint32_t x = 0; x < divisor; x++) {
166 for (uint32_t y = 0; y < divisor; y++) {
167 // Calculate the subset dimensions
168 uint32_t subsetWidth = width / divisor;
169 uint32_t subsetHeight = height / divisor;
170 const int left = x * subsetWidth;
171 const int top = y * subsetHeight;
172
173 // Increase the size of the last subset in each row or column, when the
174 // divisor does not divide evenly into the image dimensions
175 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
176 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
177
178 // Increase the size of the subset in order to have a border on each side
179 const int decodeLeft = left - unscaledBorder;
180 const int decodeTop = top - unscaledBorder;
181 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
182 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
msarett35e5d1b2015-10-27 12:50:25 -0700183 SkBitmap bitmap;
184 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
185 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false)) {
mtklein9b439152015-12-09 13:02:26 -0800186 return "Cannot decode region.";
msarett35e5d1b2015-10-27 12:50:25 -0700187 }
188 if (colorType != bitmap.colorType()) {
mtklein9b439152015-12-09 13:02:26 -0800189 return Error::Nonfatal("Cannot convert to color type.");
msaretta5783ae2015-09-08 15:35:32 -0700190 }
191
msarett35e5d1b2015-10-27 12:50:25 -0700192 canvas->drawBitmapRect(bitmap,
msaretta5783ae2015-09-08 15:35:32 -0700193 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
194 (SkScalar) (subsetWidth / fSampleSize),
195 (SkScalar) (subsetHeight / fSampleSize)),
196 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
197 (SkScalar) (top / fSampleSize),
198 (SkScalar) (subsetWidth / fSampleSize),
199 (SkScalar) (subsetHeight / fSampleSize)),
200 nullptr);
201 }
202 }
203 return "";
204 }
205 default:
206 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800207 return "Error: Should not be reached.";
msaretta5783ae2015-09-08 15:35:32 -0700208 }
209}
210
211SkISize BRDSrc::size() const {
msarett5cb48852015-11-06 08:56:32 -0800212 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
msaretta5783ae2015-09-08 15:35:32 -0700213 if (brd) {
214 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
215 SkTMax(1, brd->height() / (int) fSampleSize));
216 }
217 return SkISize::Make(0, 0);
218}
219
220static SkString get_scaled_name(const Path& path, float scale) {
221 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
222}
223
224Name BRDSrc::name() const {
225 // We will replicate the names used by CodecSrc so that images can
226 // be compared in Gold.
227 if (1 == fSampleSize) {
228 return SkOSPath::Basename(fPath.c_str());
229 }
msarett4b0778e2015-11-13 09:59:11 -0800230 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700231}
232
233/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
234
msarett0a242972015-06-11 14:27:27 -0700235CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale)
msarett438b2ad2015-04-09 12:43:10 -0700236 : fPath(path)
237 , fMode(mode)
238 , fDstColorType(dstColorType)
msarett0a242972015-06-11 14:27:27 -0700239 , fScale(scale)
msarett438b2ad2015-04-09 12:43:10 -0700240{}
mtklein748ca3b2015-01-15 10:56:12 -0800241
mtklein99cab4e2015-07-31 06:43:04 -0700242bool CodecSrc::veto(SinkFlags flags) const {
243 // No need to test decoding to non-raster or indirect backend.
mtkleine0effd62015-07-29 06:37:28 -0700244 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferred decode to
245 // let the GPU handle it.
mtklein99cab4e2015-07-31 06:43:04 -0700246 return flags.type != SinkFlags::kRaster
247 || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700248}
scroggo9b77ddd2015-03-19 06:03:39 -0700249
msarett3d9d7a72015-10-21 10:27:10 -0700250bool get_decode_info(SkImageInfo* decodeInfo, const SkImageInfo& defaultInfo,
251 SkColorType canvasColorType, CodecSrc::DstColorType dstColorType) {
252 switch (dstColorType) {
253 case CodecSrc::kIndex8_Always_DstColorType:
254 if (kRGB_565_SkColorType == canvasColorType) {
255 return false;
256 }
257 *decodeInfo = defaultInfo.makeColorType(kIndex_8_SkColorType);
258 break;
259 case CodecSrc::kGrayscale_Always_DstColorType:
260 if (kRGB_565_SkColorType == canvasColorType) {
261 return false;
262 }
263 *decodeInfo = defaultInfo.makeColorType(kGray_8_SkColorType);
264 break;
265 default:
266 *decodeInfo = defaultInfo.makeColorType(canvasColorType);
267 break;
268 }
269
270 // FIXME: Currently we cannot draw unpremultiplied sources.
271 if (decodeInfo->alphaType() == kUnpremul_SkAlphaType) {
msarett90978142015-10-27 07:12:24 -0700272 *decodeInfo = decodeInfo->makeAlphaType(kPremul_SkAlphaType);
msarett3d9d7a72015-10-21 10:27:10 -0700273 }
274 return true;
275}
276
mtkleine0effd62015-07-29 06:37:28 -0700277Error CodecSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -0800278 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800279 if (!encoded) {
280 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
281 }
msarett3d9d7a72015-10-21 10:27:10 -0700282 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9e707a02015-09-01 14:57:57 -0700283 if (nullptr == codec.get()) {
284 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -0700285 }
286
msarett3d9d7a72015-10-21 10:27:10 -0700287 SkImageInfo decodeInfo;
288 if (!get_decode_info(&decodeInfo, codec->getInfo(), canvas->imageInfo().colorType(),
289 fDstColorType)) {
290 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
msarett438b2ad2015-04-09 12:43:10 -0700291 }
292
msarett0a242972015-06-11 14:27:27 -0700293 // Try to scale the image if it is desired
294 SkISize size = codec->getScaledDimensions(fScale);
295 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
296 return Error::Nonfatal("Test without scaling is uninteresting.");
297 }
msarettb32758a2015-08-18 13:22:46 -0700298
299 // Visually inspecting very small output images is not necessary. We will
300 // cover these cases in unit testing.
301 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
302 return Error::Nonfatal("Scaling very small images is uninteresting.");
303 }
msarett0a242972015-06-11 14:27:27 -0700304 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
305
msarett438b2ad2015-04-09 12:43:10 -0700306 // Construct a color table for the decode if necessary
halcanary96fcdcc2015-08-27 07:41:13 -0700307 SkAutoTUnref<SkColorTable> colorTable(nullptr);
308 SkPMColor* colorPtr = nullptr;
309 int* colorCountPtr = nullptr;
msarett438b2ad2015-04-09 12:43:10 -0700310 int maxColors = 256;
311 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
312 SkPMColor colors[256];
halcanary385fe4d2015-08-26 13:07:48 -0700313 colorTable.reset(new SkColorTable(colors, maxColors));
msarett438b2ad2015-04-09 12:43:10 -0700314 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
315 colorCountPtr = &maxColors;
316 }
317
scroggo9b77ddd2015-03-19 06:03:39 -0700318 SkBitmap bitmap;
msarettbb25b532016-01-13 09:31:39 -0800319 SkPixelRefFactory* factory = nullptr;
320 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
321 SkCodec::Options options;
322 if (kCodecZeroInit_Mode == fMode) {
323 factory = &zeroFactory;
324 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
325 }
326 if (!bitmap.tryAllocPixels(decodeInfo, factory, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800327 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
scroggo9b77ddd2015-03-19 06:03:39 -0700328 decodeInfo.width(), decodeInfo.height());
329 }
330
scroggo9c59ebc2015-03-25 13:48:49 -0700331 switch (fMode) {
msarettbb25b532016-01-13 09:31:39 -0800332 case kCodecZeroInit_Mode:
msarett9e707a02015-09-01 14:57:57 -0700333 case kCodec_Mode: {
msarettbb25b532016-01-13 09:31:39 -0800334 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
msarett438b2ad2015-04-09 12:43:10 -0700335 colorPtr, colorCountPtr)) {
scroggoeb602a52015-07-09 08:16:03 -0700336 case SkCodec::kSuccess:
scroggo9c59ebc2015-03-25 13:48:49 -0700337 // We consider incomplete to be valid, since we should still decode what is
338 // available.
scroggoeb602a52015-07-09 08:16:03 -0700339 case SkCodec::kIncompleteInput:
scroggo9c59ebc2015-03-25 13:48:49 -0700340 break;
scroggoeb602a52015-07-09 08:16:03 -0700341 case SkCodec::kInvalidConversion:
scroggo9c59ebc2015-03-25 13:48:49 -0700342 return Error::Nonfatal("Incompatible colortype conversion");
343 default:
344 // Everything else is considered a failure.
345 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
346 }
emmaleer97002062015-05-27 12:36:10 -0700347 canvas->drawBitmap(bitmap, 0, 0);
scroggo9c59ebc2015-03-25 13:48:49 -0700348 break;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700349 }
scroggo9c59ebc2015-03-25 13:48:49 -0700350 case kScanline_Mode: {
scroggo46c57472015-09-30 08:57:13 -0700351 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
352 colorCountPtr)) {
msarett9e707a02015-09-01 14:57:57 -0700353 return Error::Nonfatal("Could not start scanline decoder");
scroggo9c59ebc2015-03-25 13:48:49 -0700354 }
scroggo1c005e42015-08-04 09:24:45 -0700355
msarette6dd0042015-10-09 11:07:34 -0700356 void* dst = bitmap.getAddr(0, 0);
357 size_t rowBytes = bitmap.rowBytes();
358 uint32_t height = decodeInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700359 switch (codec->getScanlineOrder()) {
360 case SkCodec::kTopDown_SkScanlineOrder:
361 case SkCodec::kBottomUp_SkScanlineOrder:
362 case SkCodec::kNone_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700363 // We do not need to check the return value. On an incomplete
364 // image, memory will be filled with a default value.
365 codec->getScanlines(dst, height, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700366 break;
scroggo46c57472015-09-30 08:57:13 -0700367 case SkCodec::kOutOfOrder_SkScanlineOrder: {
msarett10522ff2015-09-07 08:54:01 -0700368 for (int y = 0; y < decodeInfo.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700369 int dstY = codec->outputScanline(y);
msarett10522ff2015-09-07 08:54:01 -0700370 void* dstPtr = bitmap.getAddr(0, dstY);
msarette6dd0042015-10-09 11:07:34 -0700371 // We complete the loop, even if this call begins to fail
372 // due to an incomplete image. This ensures any uninitialized
373 // memory will be filled with the proper value.
374 codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
msarett10522ff2015-09-07 08:54:01 -0700375 }
376 break;
377 }
378 }
379
emmaleer97002062015-05-27 12:36:10 -0700380 canvas->drawBitmap(bitmap, 0, 0);
381 break;
382 }
msarett0a242972015-06-11 14:27:27 -0700383 case kStripe_Mode: {
384 const int height = decodeInfo.height();
385 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
386 // does not align with image blocks.
387 const int stripeHeight = 37;
388 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
389
390 // Decode odd stripes
scroggo46c57472015-09-30 08:57:13 -0700391 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
392 colorCountPtr)
393 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
msarett9e707a02015-09-01 14:57:57 -0700394 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
395 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
396 // to run this test for image types that do not have this scanline ordering.
msarett5406d6f2015-08-31 06:55:13 -0700397 return Error::Nonfatal("Could not start top-down scanline decoder");
msarett0a242972015-06-11 14:27:27 -0700398 }
msarette6dd0042015-10-09 11:07:34 -0700399
msarett0a242972015-06-11 14:27:27 -0700400 for (int i = 0; i < numStripes; i += 2) {
401 // Skip a stripe
402 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
msarette6dd0042015-10-09 11:07:34 -0700403 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700404
405 // Read a stripe
406 const int startY = (i + 1) * stripeHeight;
407 const int linesToRead = SkTMin(stripeHeight, height - startY);
408 if (linesToRead > 0) {
msarette6dd0042015-10-09 11:07:34 -0700409 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700410 }
411 }
412
413 // Decode even stripes
scroggo46c57472015-09-30 08:57:13 -0700414 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
415 colorPtr, colorCountPtr);
scroggo1c005e42015-08-04 09:24:45 -0700416 if (SkCodec::kSuccess != startResult) {
417 return "Failed to restart scanline decoder with same parameters.";
msarett0a242972015-06-11 14:27:27 -0700418 }
419 for (int i = 0; i < numStripes; i += 2) {
420 // Read a stripe
421 const int startY = i * stripeHeight;
422 const int linesToRead = SkTMin(stripeHeight, height - startY);
msarette6dd0042015-10-09 11:07:34 -0700423 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
msarett0a242972015-06-11 14:27:27 -0700424
425 // Skip a stripe
msarettf6db27e2015-06-12 09:34:04 -0700426 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
427 if (linesToSkip > 0) {
msarette6dd0042015-10-09 11:07:34 -0700428 codec->skipScanlines(linesToSkip);
msarett0a242972015-06-11 14:27:27 -0700429 }
430 }
431 canvas->drawBitmap(bitmap, 0, 0);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700432 break;
msarett0a242972015-06-11 14:27:27 -0700433 }
scroggob636b452015-07-22 07:16:20 -0700434 case kSubset_Mode: {
435 // Arbitrarily choose a divisor.
436 int divisor = 2;
437 // Total width/height of the image.
438 const int W = codec->getInfo().width();
439 const int H = codec->getInfo().height();
440 if (divisor > W || divisor > H) {
441 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big "
442 "for %s with dimensions (%d x %d)", divisor,
443 fPath.c_str(), W, H));
444 }
445 // subset dimensions
446 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
447 const int w = SkAlign2(W / divisor);
448 const int h = SkAlign2(H / divisor);
449 SkIRect subset;
450 SkCodec::Options opts;
451 opts.fSubset = &subset;
452 SkBitmap subsetBm;
453 // We will reuse pixel memory from bitmap.
454 void* pixels = bitmap.getPixels();
455 // Keep track of left and top (for drawing subsetBm into canvas). We could use
456 // fScale * x and fScale * y, but we want integers such that the next subset will start
457 // where the last one ended. So we'll add decodeInfo.width() and height().
458 int left = 0;
459 for (int x = 0; x < W; x += w) {
460 int top = 0;
461 for (int y = 0; y < H; y+= h) {
462 // Do not make the subset go off the edge of the image.
463 const int preScaleW = SkTMin(w, W - x);
464 const int preScaleH = SkTMin(h, H - y);
465 subset.setXYWH(x, y, preScaleW, preScaleH);
466 // And scale
467 // FIXME: Should we have a version of getScaledDimensions that takes a subset
468 // into account?
msarette6dd0042015-10-09 11:07:34 -0700469 decodeInfo = decodeInfo.makeWH(
470 SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)),
471 SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)));
scroggob636b452015-07-22 07:16:20 -0700472 size_t rowBytes = decodeInfo.minRowBytes();
473 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700474 nullptr, nullptr)) {
scroggob636b452015-07-22 07:16:20 -0700475 return SkStringPrintf("could not install pixels for %s.", fPath.c_str());
476 }
477 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
478 &opts, colorPtr, colorCountPtr);
479 switch (result) {
480 case SkCodec::kSuccess:
481 case SkCodec::kIncompleteInput:
482 break;
483 case SkCodec::kInvalidConversion:
484 if (0 == (x|y)) {
485 // First subset is okay to return unimplemented.
486 return Error::Nonfatal("Incompatible colortype conversion");
487 }
488 // If the first subset succeeded, a later one should not fail.
489 // fall through to failure
490 case SkCodec::kUnimplemented:
491 if (0 == (x|y)) {
492 // First subset is okay to return unimplemented.
493 return Error::Nonfatal("subset codec not supported");
494 }
495 // If the first subset succeeded, why would a later one fail?
496 // fall through to failure
497 default:
498 return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) "
499 "from %s with dimensions (%d x %d)\t error %d",
500 x, y, decodeInfo.width(), decodeInfo.height(),
501 fPath.c_str(), W, H, result);
502 }
503 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top));
504 // translate by the scaled height.
505 top += decodeInfo.height();
506 }
507 // translate by the scaled width.
508 left += decodeInfo.width();
509 }
510 return "";
511 }
scroggo9b77ddd2015-03-19 06:03:39 -0700512 }
scroggo9c59ebc2015-03-25 13:48:49 -0700513 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700514}
515
516SkISize CodecSrc::size() const {
517 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
msarett3d9d7a72015-10-21 10:27:10 -0700518 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo7fac5af2015-09-30 11:33:12 -0700519 if (nullptr == codec) {
520 return SkISize::Make(0, 0);
521 }
522 return codec->getScaledDimensions(fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700523}
524
525Name CodecSrc::name() const {
msarett0a242972015-06-11 14:27:27 -0700526 if (1.0f == fScale) {
527 return SkOSPath::Basename(fPath.c_str());
msarett0a242972015-06-11 14:27:27 -0700528 }
msaretta5783ae2015-09-08 15:35:32 -0700529 return get_scaled_name(fPath, fScale);
scroggo9b77ddd2015-03-19 06:03:39 -0700530}
531
532/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
533
msarett3d9d7a72015-10-21 10:27:10 -0700534AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType,
535 int sampleSize)
536 : fPath(path)
537 , fMode(mode)
538 , fDstColorType(dstColorType)
539 , fSampleSize(sampleSize)
540{}
541
542bool AndroidCodecSrc::veto(SinkFlags flags) const {
543 // No need to test decoding to non-raster or indirect backend.
544 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferred decode to
545 // let the GPU handle it.
546 return flags.type != SinkFlags::kRaster
547 || flags.approach != SinkFlags::kDirect;
548}
549
550Error AndroidCodecSrc::draw(SkCanvas* canvas) const {
551 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
552 if (!encoded) {
553 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
554 }
555 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
556 if (nullptr == codec.get()) {
557 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str());
558 }
559
560 SkImageInfo decodeInfo;
561 if (!get_decode_info(&decodeInfo, codec->getInfo(), canvas->imageInfo().colorType(),
562 fDstColorType)) {
563 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
564 }
565
566 // Scale the image if it is desired.
567 SkISize size = codec->getSampledDimensions(fSampleSize);
568
569 // Visually inspecting very small output images is not necessary. We will
570 // cover these cases in unit testing.
571 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
572 return Error::Nonfatal("Scaling very small images is uninteresting.");
573 }
574 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
575
576 // Construct a color table for the decode if necessary
577 SkAutoTUnref<SkColorTable> colorTable(nullptr);
578 SkPMColor* colorPtr = nullptr;
579 int* colorCountPtr = nullptr;
580 int maxColors = 256;
581 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
582 SkPMColor colors[256];
583 colorTable.reset(new SkColorTable(colors, maxColors));
584 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
585 colorCountPtr = &maxColors;
586 }
587
588 SkBitmap bitmap;
589 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) {
mtklein9b439152015-12-09 13:02:26 -0800590 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
msarett3d9d7a72015-10-21 10:27:10 -0700591 decodeInfo.width(), decodeInfo.height());
592 }
593
594 // Create options for the codec.
595 SkAndroidCodec::AndroidOptions options;
596 options.fColorPtr = colorPtr;
597 options.fColorCount = colorCountPtr;
598 options.fSampleSize = fSampleSize;
599
600 switch (fMode) {
601 case kFullImage_Mode: {
602 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(),
603 &options)) {
604 case SkCodec::kSuccess:
605 case SkCodec::kIncompleteInput:
606 break;
607 case SkCodec::kInvalidConversion:
mtklein9b439152015-12-09 13:02:26 -0800608 return Error::Nonfatal("Cannot convert to requested color type.");
msarett3d9d7a72015-10-21 10:27:10 -0700609 default:
610 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
611 }
612 canvas->drawBitmap(bitmap, 0, 0);
613 return "";
614 }
615 case kDivisor_Mode: {
616 const int width = codec->getInfo().width();
617 const int height = codec->getInfo().height();
618 const int divisor = 2;
619 if (width < divisor || height < divisor) {
mtklein9b439152015-12-09 13:02:26 -0800620 return Error::Nonfatal("Divisor is larger than image dimension.");
msarett3d9d7a72015-10-21 10:27:10 -0700621 }
622
msarettfa23a9e2015-10-21 13:26:59 -0700623 // Keep track of the final decoded dimensions.
624 int finalScaledWidth = 0;
625 int finalScaledHeight = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700626 for (int x = 0; x < divisor; x++) {
627 for (int y = 0; y < divisor; y++) {
628 // Calculate the subset dimensions
629 int subsetWidth = width / divisor;
630 int subsetHeight = height / divisor;
631 const int left = x * subsetWidth;
632 const int top = y * subsetHeight;
633
634 // Increase the size of the last subset in each row or column, when the
635 // divisor does not divide evenly into the image dimensions
636 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
637 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
638 SkIRect subset = SkIRect::MakeXYWH(left, top, subsetWidth, subsetHeight);
639 if (!codec->getSupportedSubset(&subset)) {
mtklein9b439152015-12-09 13:02:26 -0800640 return "Could not get supported subset to decode.";
msarett3d9d7a72015-10-21 10:27:10 -0700641 }
642 options.fSubset = &subset;
msarettfa23a9e2015-10-21 13:26:59 -0700643 const int scaledWidthOffset = subset.left() / fSampleSize;
644 const int scaledHeightOffset = subset.top() / fSampleSize;
645 void* pixels = bitmap.getAddr(scaledWidthOffset, scaledHeightOffset);
msarett3d9d7a72015-10-21 10:27:10 -0700646 SkISize scaledSubsetSize = codec->getSampledSubsetDimensions(fSampleSize,
647 subset);
648 SkImageInfo subsetDecodeInfo = decodeInfo.makeWH(scaledSubsetSize.width(),
649 scaledSubsetSize.height());
650
msarettfa23a9e2015-10-21 13:26:59 -0700651 if (x + 1 == divisor && y + 1 == divisor) {
652 finalScaledWidth = scaledWidthOffset + scaledSubsetSize.width();
653 finalScaledHeight = scaledHeightOffset + scaledSubsetSize.height();
654 }
655
msarett3d9d7a72015-10-21 10:27:10 -0700656 switch (codec->getAndroidPixels(subsetDecodeInfo, pixels, bitmap.rowBytes(),
657 &options)) {
658 case SkCodec::kSuccess:
659 case SkCodec::kIncompleteInput:
660 break;
661 case SkCodec::kInvalidConversion:
mtklein9b439152015-12-09 13:02:26 -0800662 return Error::Nonfatal("Cannot convert to requested color type.");
msarett3d9d7a72015-10-21 10:27:10 -0700663 default:
664 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
665 }
666 }
667 }
msarettfa23a9e2015-10-21 13:26:59 -0700668
669 SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth,
670 (SkScalar) finalScaledHeight);
671 canvas->drawBitmapRect(bitmap, rect, rect, nullptr);
msarett3d9d7a72015-10-21 10:27:10 -0700672 return "";
673 }
674 default:
675 SkASSERT(false);
mtklein9b439152015-12-09 13:02:26 -0800676 return "Error: Should not be reached.";
msarett3d9d7a72015-10-21 10:27:10 -0700677 }
678}
679
680SkISize AndroidCodecSrc::size() const {
681 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
682 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
683 if (nullptr == codec) {
684 return SkISize::Make(0, 0);
685 }
686 return codec->getSampledDimensions(fSampleSize);
687}
688
689Name AndroidCodecSrc::name() const {
690 // We will replicate the names used by CodecSrc so that images can
691 // be compared in Gold.
692 if (1 == fSampleSize) {
693 return SkOSPath::Basename(fPath.c_str());
694 }
msarett4b0778e2015-11-13 09:59:11 -0800695 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700696}
697
698/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
699
msarett164d3022015-11-10 15:09:03 -0800700ImageSrc::ImageSrc(Path path) : fPath(path) {}
scroggo9b77ddd2015-03-19 06:03:39 -0700701
mtklein99cab4e2015-07-31 06:43:04 -0700702bool ImageSrc::veto(SinkFlags flags) const {
703 // No need to test decoding to non-raster or indirect backend.
mtkleine0effd62015-07-29 06:37:28 -0700704 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YUV.
mtklein99cab4e2015-07-31 06:43:04 -0700705 return flags.type != SinkFlags::kRaster
706 || flags.approach != SinkFlags::kDirect;
mtkleine0effd62015-07-29 06:37:28 -0700707}
scroggo9b77ddd2015-03-19 06:03:39 -0700708
mtkleine0effd62015-07-29 06:37:28 -0700709Error ImageSrc::draw(SkCanvas* canvas) const {
scroggo9b77ddd2015-03-19 06:03:39 -0700710 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
711 if (!encoded) {
712 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
713 }
mtkleine0effd62015-07-29 06:37:28 -0700714 const SkColorType dstColorType = canvas->imageInfo().colorType();
mtkleinedc93bc2015-01-30 13:22:23 -0800715
msarett164d3022015-11-10 15:09:03 -0800716 // Decode the full image.
717 SkBitmap bitmap;
718 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
719 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
720 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
mtkleinedc93bc2015-01-30 13:22:23 -0800721 }
msarett164d3022015-11-10 15:09:03 -0800722 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) {
723 // Do not draw a bitmap with alpha to a destination without alpha.
724 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
mtklein748ca3b2015-01-15 10:56:12 -0800725 }
msarett164d3022015-11-10 15:09:03 -0800726 encoded.reset((SkData*)nullptr); // Might as well drop this when we're done with it.
727 canvas->drawBitmap(bitmap, 0,0);
mtklein748ca3b2015-01-15 10:56:12 -0800728 return "";
729}
730
731SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -0800732 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
scroggo9b77ddd2015-03-19 06:03:39 -0700733 SkBitmap bitmap;
734 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
735 encoded->size(),
736 &bitmap,
737 kUnknown_SkColorType,
738 SkImageDecoder::kDecodeBounds_Mode)) {
739 return SkISize::Make(0,0);
mtklein748ca3b2015-01-15 10:56:12 -0800740 }
scroggo9b77ddd2015-03-19 06:03:39 -0700741 return bitmap.dimensions();
mtklein748ca3b2015-01-15 10:56:12 -0800742}
743
mtklein9264a952015-01-20 10:11:53 -0800744Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800745 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800746}
mtklein748ca3b2015-01-15 10:56:12 -0800747
748/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
749
mtkleinf4ba3212015-01-28 15:32:24 -0800750static const SkRect kSKPViewport = {0,0, 1000,1000};
751
mtklein8d17a132015-01-30 11:42:31 -0800752SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800753
754Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800755 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800756 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800757 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
758 }
mtkleinb3e5e4d2015-03-25 13:13:43 -0700759 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap));
mtklein75d98fd2015-01-18 07:05:01 -0800760 if (!pic) {
761 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
762 }
halcanary96fcdcc2015-08-27 07:41:13 -0700763 stream.reset((SkStream*)nullptr); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -0700764
mtkleinf4ba3212015-01-28 15:32:24 -0800765 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800766 canvas->drawPicture(pic);
767 return "";
768}
769
770SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700771 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
772 if (!stream) {
773 return SkISize::Make(0,0);
774 }
775 SkPictInfo info;
776 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
777 return SkISize::Make(0,0);
778 }
779 SkRect viewport = kSKPViewport;
780 if (!viewport.intersect(info.fCullRect)) {
781 return SkISize::Make(0,0);
782 }
783 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800784}
785
786Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
787
788/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
789
mtkleinad66f9b2015-02-13 15:11:10 -0800790Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
791 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
792 return src.draw(canvas);
793}
794
795/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
796
mtkleinb9eb4ac2015-02-02 18:26:03 -0800797DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
798
mtklein82d28432015-01-15 12:46:02 -0800799GPUSink::GPUSink(GrContextFactory::GLContextType ct,
kkinnunen5219fd92015-12-10 06:28:13 -0800800 GrContextFactory::GLContextOptions options,
mtklein82d28432015-01-15 12:46:02 -0800801 int samples,
bsalomonafcd7cd2015-08-31 12:39:41 -0700802 bool diText,
mtklein82d28432015-01-15 12:46:02 -0800803 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800804 : fContextType(ct)
kkinnunen5219fd92015-12-10 06:28:13 -0800805 , fContextOptions(options)
mtklein748ca3b2015-01-15 10:56:12 -0800806 , fSampleCount(samples)
bsalomonafcd7cd2015-08-31 12:39:41 -0700807 , fUseDIText(diText)
mtklein82d28432015-01-15 12:46:02 -0800808 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800809
810int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800811 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800812}
813
joshualitt5f5a8d72015-02-25 14:09:45 -0800814void PreAbandonGpuContextErrorHandler(SkError, void*) {}
815
bsalomon648c6962015-10-23 09:06:59 -0700816DEFINE_bool(imm, false, "Run gpu configs in immediate mode.");
bsalomon69cfe952015-11-30 13:27:47 -0800817DEFINE_bool(batchClip, false, "Clip each GrBatch to its device bounds for testing.");
bsalomon6dea83f2015-12-03 12:58:06 -0800818DEFINE_bool(batchBounds, false, "Draw a wireframe bounds of each GrBatch.");
bsalomon489147c2015-12-14 12:13:09 -0800819DEFINE_int32(batchLookback, -1, "Maximum GrBatch lookback for combining, negative means default.");
bsalomon648c6962015-10-23 09:06:59 -0700820
mtkleinb9eb4ac2015-02-02 18:26:03 -0800821Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
kkinnunen5219fd92015-12-10 06:28:13 -0800822 GrContextOptions grOptions;
bsalomon489147c2015-12-14 12:13:09 -0800823 grOptions.fImmediateMode = FLAGS_imm;
824 grOptions.fClipBatchToBounds = FLAGS_batchClip;
825 grOptions.fDrawBatchBounds = FLAGS_batchBounds;
826 grOptions.fMaxBatchLookback = FLAGS_batchLookback;
kkinnunen64492c42015-12-08 01:24:40 -0800827
kkinnunen5219fd92015-12-10 06:28:13 -0800828 src.modifyGrContextOptions(&grOptions);
829
830 GrContextFactory factory(grOptions);
mtkleinf4ba3212015-01-28 15:32:24 -0800831 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800832 const SkImageInfo info =
833 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
834 SkAutoTUnref<SkSurface> surface(
kkinnunen3e980c32015-12-23 01:33:00 -0800835 NewGpuSurface(&factory, fContextType, fContextOptions, info, fSampleCount, fUseDIText));
mtklein748ca3b2015-01-15 10:56:12 -0800836 if (!surface) {
837 return "Could not create a surface.";
838 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800839 if (FLAGS_preAbandonGpuContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700840 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr);
joshualitt5f5a8d72015-02-25 14:09:45 -0800841 factory.abandonContexts();
842 }
mtklein748ca3b2015-01-15 10:56:12 -0800843 SkCanvas* canvas = surface->getCanvas();
844 Error err = src.draw(canvas);
845 if (!err.isEmpty()) {
846 return err;
847 }
848 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800849 if (FLAGS_gpuStats) {
850 canvas->getGrContext()->dumpCacheStats(log);
851 canvas->getGrContext()->dumpGpuStats(log);
852 }
mtklein748ca3b2015-01-15 10:56:12 -0800853 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800854 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800855 if (FLAGS_abandonGpuContext) {
856 factory.abandonContexts();
857 }
mtklein748ca3b2015-01-15 10:56:12 -0800858 return "";
859}
860
861/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
862
halcanary47ef4d52015-03-03 09:13:09 -0800863static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
864 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
865 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800866 int width = src.size().width(),
867 height = src.size().height();
868
halcanary7e798182015-04-14 14:06:18 -0700869 if (FLAGS_multiPage) {
870 const int kLetterWidth = 612, // 8.5 * 72
871 kLetterHeight = 792; // 11 * 72
872 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
873 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -0800874
halcanary7e798182015-04-14 14:06:18 -0700875 int xPages = ((width - 1) / kLetterWidth) + 1;
876 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -0800877
halcanary7e798182015-04-14 14:06:18 -0700878 for (int y = 0; y < yPages; ++y) {
879 for (int x = 0; x < xPages; ++x) {
880 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
881 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
882 SkCanvas* canvas =
883 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
884 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700885 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700886 }
887 canvas->clipRect(letter);
888 canvas->translate(-letter.width() * x, -letter.height() * y);
889 Error err = src.draw(canvas);
890 if (!err.isEmpty()) {
891 return err;
892 }
893 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -0700894 }
halcanaryfd4a9932015-01-28 11:45:58 -0800895 }
halcanary7e798182015-04-14 14:06:18 -0700896 } else {
897 SkCanvas* canvas =
898 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
899 if (!canvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700900 return "SkDocument::beginPage(w,h) returned nullptr";
halcanary7e798182015-04-14 14:06:18 -0700901 }
902 Error err = src.draw(canvas);
903 if (!err.isEmpty()) {
904 return err;
905 }
906 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -0800907 }
halcanary7e798182015-04-14 14:06:18 -0700908 if (!doc->close()) {
909 return "SkDocument::close() returned false";
910 }
halcanaryfd4a9932015-01-28 11:45:58 -0800911 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800912 return "";
913}
914
halcanaryc11c62f2015-09-28 11:51:54 -0700915PDFSink::PDFSink(const char* rasterizer) : fRasterizer(rasterizer) {}
halcanary47ef4d52015-03-03 09:13:09 -0800916
917Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
918 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
919 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700920 return "SkDocument::CreatePDF() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800921 }
halcanaryf12a1672015-09-23 12:45:49 -0700922 SkTArray<SkDocument::Attribute> info;
923 info.emplace_back(SkString("Title"), src.name());
924 info.emplace_back(SkString("Subject"),
925 SkString("rendering correctness test"));
926 info.emplace_back(SkString("Creator"), SkString("Skia/DM"));
halcanaryc11c62f2015-09-28 11:51:54 -0700927
928 info.emplace_back(SkString("Keywords"),
929 SkStringPrintf("Rasterizer:%s;", fRasterizer));
halcanaryf12a1672015-09-23 12:45:49 -0700930 doc->setMetadata(info, nullptr, nullptr);
halcanary47ef4d52015-03-03 09:13:09 -0800931 return draw_skdocument(src, doc.get(), dst);
932}
933
934/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
935
936XPSSink::XPSSink() {}
937
938Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
939 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
940 if (!doc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700941 return "SkDocument::CreateXPS() returned nullptr";
halcanary47ef4d52015-03-03 09:13:09 -0800942 }
943 return draw_skdocument(src, doc.get(), dst);
944}
mtklein748ca3b2015-01-15 10:56:12 -0800945/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
946
mtklein9c3f17d2015-01-28 11:35:18 -0800947SKPSink::SKPSink() {}
948
mtkleinb9eb4ac2015-02-02 18:26:03 -0800949Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800950 SkSize size;
951 size = src.size();
952 SkPictureRecorder recorder;
953 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
954 if (!err.isEmpty()) {
955 return err;
956 }
957 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
958 pic->serialize(dst);
959 return "";
960}
961
962/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
963
mtklein8a4527e2015-01-31 20:00:58 -0800964SVGSink::SVGSink() {}
965
mtkleinb9eb4ac2015-02-02 18:26:03 -0800966Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanary385fe4d2015-08-26 13:07:48 -0700967 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst));
fmalita2aafe6f2015-02-06 12:51:10 -0800968 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
969 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
970 xmlWriter));
971 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800972}
973
974/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
975
mtklein748ca3b2015-01-15 10:56:12 -0800976RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
977
mtkleinb9eb4ac2015-02-02 18:26:03 -0800978Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800979 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800980 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
981 SkAlphaType alphaType = kPremul_SkAlphaType;
982 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
983
mtkleinc8be09a2016-01-04 18:56:57 -0800984 SkMallocPixelRef::ZeroedPRFactory factory;
985 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType),
986 &factory,
987 nullptr/*colortable*/);
mtklein748ca3b2015-01-15 10:56:12 -0800988 SkCanvas canvas(*dst);
989 return src.draw(&canvas);
990}
991
992/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
993
mtkleina16e69e2015-05-05 11:38:45 -0700994// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
mtkleine44b5082015-05-07 10:53:34 -0700995// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
mtkleina16e69e2015-05-05 11:38:45 -0700996// Several examples below.
997
msarett62d3b102015-12-10 15:14:27 -0800998static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
mtklein64593522015-11-12 10:41:05 -0800999 SkISize size, std::function<Error(SkCanvas*)> draw) {
mtkleina16e69e2015-05-05 11:38:45 -07001000 class ProxySrc : public Src {
1001 public:
msarett62d3b102015-12-10 15:14:27 -08001002 ProxySrc(SkISize size, std::function<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {}
mtkleina16e69e2015-05-05 11:38:45 -07001003 Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
msarett62d3b102015-12-10 15:14:27 -08001004 Name name() const override { sk_throw(); return ""; } // Won't be called.
mtkleina16e69e2015-05-05 11:38:45 -07001005 SkISize size() const override { return fSize; }
1006 private:
mtklein64593522015-11-12 10:41:05 -08001007 SkISize fSize;
1008 std::function<Error(SkCanvas*)> fDraw;
mtkleina16e69e2015-05-05 11:38:45 -07001009 };
msarett62d3b102015-12-10 15:14:27 -08001010 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
mtkleina16e69e2015-05-05 11:38:45 -07001011}
1012
1013/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1014
mtklein4a34ecb2016-01-08 10:19:35 -08001015DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
1016
1017// Is *bitmap identical to what you get drawing src into sink?
1018static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
1019 // We can only check raster outputs.
1020 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
1021 if (FLAGS_check && bitmap) {
1022 SkBitmap reference;
1023 SkString log;
1024 Error err = sink->draw(src, &reference, nullptr, &log);
1025 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
1026 SkASSERT(err.isEmpty());
1027 if (!err.isEmpty()) {
1028 return err;
1029 }
1030 // The dimensions are a property of the Src only, and so should be identical.
1031 SkASSERT(reference.getSize() == bitmap->getSize());
1032 if (reference.getSize() != bitmap->getSize()) {
1033 return "Dimensions don't match reference";
1034 }
1035 // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy.
1036 if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) {
1037 return "Pixels don't match reference";
1038 }
1039 }
1040 return "";
1041}
1042
1043/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1044
mtkleind603b222015-02-17 11:13:33 -08001045static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
1046 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
1047 matrix->mapRect(&bounds);
1048 matrix->postTranslate(-bounds.x(), -bounds.y());
1049 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
1050}
1051
msarett62d3b102015-12-10 15:14:27 -08001052ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtklein748ca3b2015-01-15 10:56:12 -08001053
mtkleinb9eb4ac2015-02-02 18:26:03 -08001054Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001055 SkMatrix matrix = fMatrix;
1056 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
msarett62d3b102015-12-10 15:14:27 -08001057 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001058 canvas->concat(matrix);
1059 return src.draw(canvas);
1060 });
mtklein748ca3b2015-01-15 10:56:12 -08001061}
1062
mtkleind603b222015-02-17 11:13:33 -08001063// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
1064// This should be pixel-preserving.
msarett62d3b102015-12-10 15:14:27 -08001065ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
mtkleind603b222015-02-17 11:13:33 -08001066
1067Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1068 Error err = fSink->draw(src, bitmap, stream, log);
1069 if (!err.isEmpty()) {
1070 return err;
1071 }
1072
1073 SkMatrix inverse;
1074 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
1075 return "Cannot upright --matrix.";
1076 }
1077 SkMatrix upright = SkMatrix::I();
1078 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
1079 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
1080 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
1081 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
1082
1083 SkBitmap uprighted;
1084 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
1085 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
1086
1087 SkCanvas canvas(uprighted);
1088 canvas.concat(upright);
1089 SkPaint paint;
1090 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1091 canvas.drawBitmap(*bitmap, 0, 0, &paint);
1092
1093 *bitmap = uprighted;
1094 bitmap->lockPixels();
1095 return "";
1096}
1097
mtklein748ca3b2015-01-15 10:56:12 -08001098/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1099
mtklein2e2ea382015-10-16 10:29:41 -07001100Error ViaRemote::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001101 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* target) {
mtklein479fe772015-10-21 12:34:01 -07001102 SkAutoTDelete<SkRemote::Encoder> decoder(SkRemote::NewDecoder(target));
1103 SkAutoTDelete<SkRemote::Encoder> cache(fCache ? SkRemote::NewCachingEncoder(decoder)
1104 : nullptr);
1105 SkAutoTDelete<SkCanvas> canvas(SkRemote::NewCanvas(cache ? cache : decoder));
1106 return src.draw(canvas);
mtklein2e2ea382015-10-16 10:29:41 -07001107 });
1108}
1109
mtklein748ca3b2015-01-15 10:56:12 -08001110/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtkleina16e69e2015-05-05 11:38:45 -07001111
mtkleina16e69e2015-05-05 11:38:45 -07001112Error ViaSerialization::draw(
1113 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -08001114 // Record our Src into a picture.
mtkleina16e69e2015-05-05 11:38:45 -07001115 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001116 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001117 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1118 SkIntToScalar(size.height())));
mtklein748ca3b2015-01-15 10:56:12 -08001119 if (!err.isEmpty()) {
1120 return err;
1121 }
1122 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
1123
1124 // Serialize it and then deserialize it.
1125 SkDynamicMemoryWStream wStream;
1126 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -08001127 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
mtkleinb3e5e4d2015-03-25 13:13:43 -07001128 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap));
mtklein748ca3b2015-01-15 10:56:12 -08001129
msarett62d3b102015-12-10 15:14:27 -08001130 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001131 canvas->drawPicture(deserialized);
mtklein4a34ecb2016-01-08 10:19:35 -08001132 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001133 });
mtklein748ca3b2015-01-15 10:56:12 -08001134}
1135
1136/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1137
msarett62d3b102015-12-10 15:14:27 -08001138ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
1139 : Via(sink)
mtklein78829242015-05-06 07:54:07 -07001140 , fW(w)
mtklein748ca3b2015-01-15 10:56:12 -08001141 , fH(h)
mtklein78829242015-05-06 07:54:07 -07001142 , fFactory(factory) {}
mtklein748ca3b2015-01-15 10:56:12 -08001143
mtkleinb9eb4ac2015-02-02 18:26:03 -08001144Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001145 auto size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -08001146 SkPictureRecorder recorder;
mtkleina16e69e2015-05-05 11:38:45 -07001147 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1148 SkIntToScalar(size.height()),
1149 fFactory.get()));
mtklein748ca3b2015-01-15 10:56:12 -08001150 if (!err.isEmpty()) {
1151 return err;
1152 }
mtkleinb7e8d692015-04-07 08:30:32 -07001153 SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -08001154
msarett62d3b102015-12-10 15:14:27 -08001155 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
mtkleina16e69e2015-05-05 11:38:45 -07001156 const int xTiles = (size.width() + fW - 1) / fW,
1157 yTiles = (size.height() + fH - 1) / fH;
1158 SkMultiPictureDraw mpd(xTiles*yTiles);
1159 SkTDArray<SkSurface*> surfaces;
1160 surfaces.setReserve(xTiles*yTiles);
mtklein748ca3b2015-01-15 10:56:12 -08001161
mtkleina16e69e2015-05-05 11:38:45 -07001162 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
1163 for (int j = 0; j < yTiles; j++) {
1164 for (int i = 0; i < xTiles; i++) {
1165 // This lets our ultimate Sink determine the best kind of surface.
1166 // E.g., if it's a GpuSink, the surfaces and images are textures.
1167 SkSurface* s = canvas->newSurface(info);
1168 if (!s) {
1169 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
mtklein748ca3b2015-01-15 10:56:12 -08001170 }
mtkleina16e69e2015-05-05 11:38:45 -07001171 surfaces.push(s);
1172 SkCanvas* c = s->getCanvas();
1173 c->translate(SkIntToScalar(-i * fW),
1174 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
1175 mpd.add(c, pic);
mtklein748ca3b2015-01-15 10:56:12 -08001176 }
mtklein748ca3b2015-01-15 10:56:12 -08001177 }
mtkleina16e69e2015-05-05 11:38:45 -07001178 mpd.draw();
1179 for (int j = 0; j < yTiles; j++) {
1180 for (int i = 0; i < xTiles; i++) {
1181 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
1182 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
1183 }
1184 }
1185 surfaces.unrefAll();
1186 return "";
1187 });
mtklein748ca3b2015-01-15 10:56:12 -08001188}
1189
mtkleinb7e8d692015-04-07 08:30:32 -07001190/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1191
mtklein4a34ecb2016-01-08 10:19:35 -08001192Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1193 auto size = src.size();
1194 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
1195 SkPictureRecorder recorder;
1196 SkAutoTUnref<SkPicture> pic;
1197 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1198 SkIntToScalar(size.height())));
1199 if (!err.isEmpty()) {
1200 return err;
1201 }
1202 pic.reset(recorder.endRecordingAsPicture());
1203 canvas->drawPicture(pic);
1204 return check_against_reference(bitmap, src, fSink);
1205 });
1206}
1207
1208/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1209
mtkleinb7e8d692015-04-07 08:30:32 -07001210// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
1211// This tests that any shortcuts we may take while recording that second picture are legal.
1212Error ViaSecondPicture::draw(
1213 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtkleina16e69e2015-05-05 11:38:45 -07001214 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001215 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleina16e69e2015-05-05 11:38:45 -07001216 SkPictureRecorder recorder;
1217 SkAutoTUnref<SkPicture> pic;
1218 for (int i = 0; i < 2; i++) {
1219 Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
1220 SkIntToScalar(size.height())));
1221 if (!err.isEmpty()) {
1222 return err;
mtkleinb7e8d692015-04-07 08:30:32 -07001223 }
mtkleina16e69e2015-05-05 11:38:45 -07001224 pic.reset(recorder.endRecordingAsPicture());
mtkleinb7e8d692015-04-07 08:30:32 -07001225 }
mtkleina16e69e2015-05-05 11:38:45 -07001226 canvas->drawPicture(pic);
mtklein4a34ecb2016-01-08 10:19:35 -08001227 return check_against_reference(bitmap, src, fSink);
mtkleina16e69e2015-05-05 11:38:45 -07001228 });
mtkleinb7e8d692015-04-07 08:30:32 -07001229}
1230
mtkleind31c13d2015-05-05 12:59:56 -07001231/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1232
mtklein6fbf4b32015-05-06 11:35:40 -07001233// Draw the Src twice. This can help exercise caching.
1234Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
msarett62d3b102015-12-10 15:14:27 -08001235 return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
mtklein6fbf4b32015-05-06 11:35:40 -07001236 for (int i = 0; i < 2; i++) {
1237 SkAutoCanvasRestore acr(canvas, true/*save now*/);
1238 canvas->clear(SK_ColorTRANSPARENT);
1239 Error err = src.draw(canvas);
1240 if (err.isEmpty()) {
1241 return err;
1242 }
1243 }
mtklein4a34ecb2016-01-08 10:19:35 -08001244 return check_against_reference(bitmap, src, fSink);
mtklein6fbf4b32015-05-06 11:35:40 -07001245 });
1246}
1247
1248/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1249
mtkleind31c13d2015-05-05 12:59:56 -07001250// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
1251// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
1252// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
1253struct DrawsAsSingletonPictures {
1254 SkCanvas* fCanvas;
mtkleind2baa902015-07-07 09:43:28 -07001255 const SkDrawableList& fDrawables;
mtkleind31c13d2015-05-05 12:59:56 -07001256
mtkleind31c13d2015-05-05 12:59:56 -07001257 template <typename T>
1258 void draw(const T& op, SkCanvas* canvas) {
1259 // We must pass SkMatrix::I() as our initial matrix.
1260 // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
1261 // which would have the funky effect of applying transforms over and over.
mtkleind2baa902015-07-07 09:43:28 -07001262 SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
1263 d(op);
mtkleind31c13d2015-05-05 12:59:56 -07001264 }
1265
mtklein449d9b72015-09-28 10:33:02 -07001266 // Draws get their own picture.
mtkleind31c13d2015-05-05 12:59:56 -07001267 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001268 SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) {
mtkleind31c13d2015-05-05 12:59:56 -07001269 SkPictureRecorder rec;
1270 this->draw(op, rec.beginRecording(SkRect::MakeLargest()));
1271 SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture());
1272 fCanvas->drawPicture(pic);
1273 }
1274
mtklein449d9b72015-09-28 10:33:02 -07001275 // We'll just issue non-draws directly.
mtkleind31c13d2015-05-05 12:59:56 -07001276 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -07001277 skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) {
1278 this->draw(op, fCanvas);
1279 }
mtkleind31c13d2015-05-05 12:59:56 -07001280};
1281
mtkleind31c13d2015-05-05 12:59:56 -07001282// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
1283// Then play back that macro picture into our wrapped sink.
1284Error ViaSingletonPictures::draw(
1285 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
1286 auto size = src.size();
msarett62d3b102015-12-10 15:14:27 -08001287 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
mtkleind31c13d2015-05-05 12:59:56 -07001288 // Use low-level (Skia-private) recording APIs so we can read the SkRecord.
1289 SkRecord skr;
1290 SkRecorder recorder(&skr, size.width(), size.height());
1291 Error err = src.draw(&recorder);
1292 if (!err.isEmpty()) {
1293 return err;
1294 }
1295
1296 // Record our macro-picture, with each draw op as its own sub-picture.
1297 SkPictureRecorder macroRec;
1298 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
1299 SkIntToScalar(size.height()));
mtkleind2baa902015-07-07 09:43:28 -07001300
1301 SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
1302 const SkDrawableList empty;
1303
1304 DrawsAsSingletonPictures drawsAsSingletonPictures = {
1305 macroCanvas,
1306 drawables ? *drawables : empty,
1307 };
mtkleinc6ad06a2015-08-19 09:51:00 -07001308 for (int i = 0; i < skr.count(); i++) {
mtkleind31c13d2015-05-05 12:59:56 -07001309 skr.visit<void>(i, drawsAsSingletonPictures);
1310 }
1311 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture());
1312
1313 canvas->drawPicture(macroPic);
mtklein4a34ecb2016-01-08 10:19:35 -08001314 return check_against_reference(bitmap, src, fSink);
mtkleind31c13d2015-05-05 12:59:56 -07001315 });
1316}
1317
mtklein748ca3b2015-01-15 10:56:12 -08001318} // namespace DM