blob: e70dd3e2b0def9a2825f01c10573b7eb98e773c3 [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"
10#include "SkCommonFlags.h"
scroggof24f2242015-03-03 08:59:20 -080011#include "SkCodec.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"
mtklein748ca3b2015-01-15 10:56:12 -080016#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -080017#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -080018#include "SkOSFile.h"
mtkleinffa901a2015-03-16 10:38:07 -070019#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080020#include "SkPictureRecorder.h"
21#include "SkRandom.h"
scroggo9c59ebc2015-03-25 13:48:49 -070022#include "SkScanlineDecoder.h"
fmalita2aafe6f2015-02-06 12:51:10 -080023#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080024#include "SkStream.h"
fmalita2aafe6f2015-02-06 12:51:10 -080025#include "SkXMLWriter.h"
mtklein748ca3b2015-01-15 10:56:12 -080026
halcanary7e798182015-04-14 14:06:18 -070027DEFINE_bool(multiPage, false, "For document-type backends, render the source"
28 " into multiple pages");
29
mtkleinb3e5e4d2015-03-25 13:13:43 -070030static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) {
31 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size));
32 return encoded && SkInstallDiscardablePixelRef(encoded, dst);
33}
34
mtklein748ca3b2015-01-15 10:56:12 -080035namespace DM {
36
mtklein748ca3b2015-01-15 10:56:12 -080037GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
38
39Error GMSrc::draw(SkCanvas* canvas) const {
40 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
41 canvas->concat(gm->getInitialTransform());
42 gm->draw(canvas);
43 return "";
44}
45
46SkISize GMSrc::size() const {
47 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
48 return gm->getISize();
49}
50
51Name GMSrc::name() const {
52 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
53 return gm->getName();
54}
55
56/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
57
msarett438b2ad2015-04-09 12:43:10 -070058CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType)
59 : fPath(path)
60 , fMode(mode)
61 , fDstColorType(dstColorType)
62{}
mtklein748ca3b2015-01-15 10:56:12 -080063
scroggo9b77ddd2015-03-19 06:03:39 -070064Error CodecSrc::draw(SkCanvas* canvas) const {
65 SkImageInfo canvasInfo;
66 if (NULL == canvas->peekPixels(&canvasInfo, NULL)) {
67 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferred decode to
68 // let the GPU handle it.
69 return Error::Nonfatal("No need to test decoding to non-raster backend.");
70 }
71
mtklein75d98fd2015-01-18 07:05:01 -080072 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080073 if (!encoded) {
74 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
75 }
scroggo9b77ddd2015-03-19 06:03:39 -070076 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett438b2ad2015-04-09 12:43:10 -070077 if (NULL == codec.get()) {
78 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
scroggo9b77ddd2015-03-19 06:03:39 -070079 }
80
msarett438b2ad2015-04-09 12:43:10 -070081 // Choose the color type to decode to
82 SkImageInfo decodeInfo = codec->getInfo();
83 SkColorType canvasColorType = canvasInfo.colorType();
84 switch (fDstColorType) {
85 case kIndex8_Always_DstColorType:
86 case kGrayscale_Always_DstColorType:
87 if (kRGB_565_SkColorType == canvasColorType) {
88 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
89 }
90 break;
91 default:
92 decodeInfo = decodeInfo.makeColorType(canvasColorType);
93 break;
94 }
95
96 // Construct a color table for the decode if necessary
97 SkAutoTUnref<SkColorTable> colorTable(NULL);
98 SkPMColor* colorPtr = NULL;
99 int* colorCountPtr = NULL;
100 int maxColors = 256;
101 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
102 SkPMColor colors[256];
103 colorTable.reset(SkNEW_ARGS(SkColorTable, (colors, maxColors)));
104 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
105 colorCountPtr = &maxColors;
106 }
107
108 // FIXME: Currently we cannot draw unpremultiplied sources.
scroggo9b77ddd2015-03-19 06:03:39 -0700109 if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) {
scroggo9b77ddd2015-03-19 06:03:39 -0700110 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
111 }
112
113 SkBitmap bitmap;
msarett438b2ad2015-04-09 12:43:10 -0700114 if (!bitmap.tryAllocPixels(decodeInfo, NULL, colorTable.get())) {
scroggo9b77ddd2015-03-19 06:03:39 -0700115 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
116 decodeInfo.width(), decodeInfo.height());
117 }
118
scroggo9c59ebc2015-03-25 13:48:49 -0700119 switch (fMode) {
120 case kNormal_Mode:
msarett438b2ad2015-04-09 12:43:10 -0700121 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), NULL,
122 colorPtr, colorCountPtr)) {
scroggo9c59ebc2015-03-25 13:48:49 -0700123 case SkImageGenerator::kSuccess:
124 // We consider incomplete to be valid, since we should still decode what is
125 // available.
126 case SkImageGenerator::kIncompleteInput:
127 break;
128 case SkImageGenerator::kInvalidConversion:
129 return Error::Nonfatal("Incompatible colortype conversion");
130 default:
131 // Everything else is considered a failure.
132 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
133 }
134 break;
135 case kScanline_Mode: {
136 SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(decodeInfo);
137 if (NULL == scanlineDecoder) {
138 return Error::Nonfatal("Cannot use scanline decoder for all images");
139 }
140 for (int y = 0; y < decodeInfo.height(); ++y) {
141 const SkImageGenerator::Result result = scanlineDecoder->getScanlines(
142 bitmap.getAddr(0, y), 1, 0);
143 switch (result) {
144 case SkImageGenerator::kSuccess:
145 case SkImageGenerator::kIncompleteInput:
146 break;
147 default:
148 return SkStringPrintf("%s failed after %d scanlines with error message %d",
149 fPath.c_str(), y-1, (int) result);
150 }
151 }
152 break;
153 }
scroggo9b77ddd2015-03-19 06:03:39 -0700154 }
scroggo9c59ebc2015-03-25 13:48:49 -0700155 canvas->drawBitmap(bitmap, 0, 0);
156 return "";
scroggo9b77ddd2015-03-19 06:03:39 -0700157}
158
159SkISize CodecSrc::size() const {
160 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
161 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
msarett9bde9182015-03-25 05:27:48 -0700162 if (NULL != codec) {
163 return codec->getInfo().dimensions();
164 } else {
165 return SkISize::Make(0, 0);
166 }
scroggo9b77ddd2015-03-19 06:03:39 -0700167}
168
169Name CodecSrc::name() const {
170 return SkOSPath::Basename(fPath.c_str());
171}
172
173/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
174
175ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
176
177Error ImageSrc::draw(SkCanvas* canvas) const {
178 SkImageInfo canvasInfo;
179 if (NULL == canvas->peekPixels(&canvasInfo, NULL)) {
180 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YUV.
181 return Error::Nonfatal("No need to test decoding to non-raster backend.");
182 }
183
184 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
185 if (!encoded) {
186 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
187 }
188 const SkColorType dstColorType = canvasInfo.colorType();
mtkleinedc93bc2015-01-30 13:22:23 -0800189 if (fDivisor == 0) {
mtklein748ca3b2015-01-15 10:56:12 -0800190 // Decode the full image.
191 SkBitmap bitmap;
scroggo9b77ddd2015-03-19 06:03:39 -0700192 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
193 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
194 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
195 }
196 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) {
197 // Do not draw a bitmap with alpha to a destination without alpha.
198 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
mtklein748ca3b2015-01-15 10:56:12 -0800199 }
mtklein75d98fd2015-01-18 07:05:01 -0800200 encoded.reset((SkData*)NULL); // Might as well drop this when we're done with it.
mtklein748ca3b2015-01-15 10:56:12 -0800201 canvas->drawBitmap(bitmap, 0,0);
202 return "";
203 }
mtkleinedc93bc2015-01-30 13:22:23 -0800204 // Decode subsets. This is a little involved.
scroggoa1193e42015-01-21 12:09:53 -0800205 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
206 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
mtklein748ca3b2015-01-15 10:56:12 -0800207 if (!decoder) {
208 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
209 }
scroggoa1193e42015-01-21 12:09:53 -0800210 stream->rewind();
mtklein748ca3b2015-01-15 10:56:12 -0800211 int w,h;
scroggoa1193e42015-01-21 12:09:53 -0800212 if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) {
mtklein4089ef72015-03-05 08:40:28 -0800213 return Error::Nonfatal("Subset decoding not supported.");
mtklein748ca3b2015-01-15 10:56:12 -0800214 }
mtkleinedc93bc2015-01-30 13:22:23 -0800215
216 // Divide the image into subsets that cover the entire image.
217 if (fDivisor > w || fDivisor > h) {
218 return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)",
219 fDivisor, fPath.c_str(), w, h);
220 }
221 const int subsetWidth = w / fDivisor,
222 subsetHeight = h / fDivisor;
223 for (int y = 0; y < h; y += subsetHeight) {
224 for (int x = 0; x < w; x += subsetWidth) {
225 SkBitmap subset;
226 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
227 if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
228 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
229 x, y, x+subsetWidth, y+subsetHeight);
230 }
scroggo56e25dd2015-03-05 11:46:40 -0800231 if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) {
232 // Do not draw a bitmap with alpha to a destination without alpha.
233 // This is not an error, but there is nothing interesting to show.
234
235 // This should only happen on the first iteration through the loop.
236 SkASSERT(0 == x && 0 == y);
237
238 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
239 }
mtkleinedc93bc2015-01-30 13:22:23 -0800240 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
mtklein748ca3b2015-01-15 10:56:12 -0800241 }
mtklein748ca3b2015-01-15 10:56:12 -0800242 }
243 return "";
244}
245
246SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -0800247 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
scroggo9b77ddd2015-03-19 06:03:39 -0700248 SkBitmap bitmap;
249 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
250 encoded->size(),
251 &bitmap,
252 kUnknown_SkColorType,
253 SkImageDecoder::kDecodeBounds_Mode)) {
254 return SkISize::Make(0,0);
mtklein748ca3b2015-01-15 10:56:12 -0800255 }
scroggo9b77ddd2015-03-19 06:03:39 -0700256 return bitmap.dimensions();
mtklein748ca3b2015-01-15 10:56:12 -0800257}
258
mtklein9264a952015-01-20 10:11:53 -0800259Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800260 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800261}
mtklein748ca3b2015-01-15 10:56:12 -0800262
263/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
264
mtkleinf4ba3212015-01-28 15:32:24 -0800265static const SkRect kSKPViewport = {0,0, 1000,1000};
266
mtklein8d17a132015-01-30 11:42:31 -0800267SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800268
269Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800270 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800271 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800272 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
273 }
mtkleinb3e5e4d2015-03-25 13:13:43 -0700274 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap));
mtklein75d98fd2015-01-18 07:05:01 -0800275 if (!pic) {
276 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
277 }
278 stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it.
joshualitt7c3a2f82015-03-31 13:32:05 -0700279
mtkleinf4ba3212015-01-28 15:32:24 -0800280 canvas->clipRect(kSKPViewport);
joshualitt7c3a2f82015-03-31 13:32:05 -0700281 // Testing TextBlob batching requires that we see individual text blobs more than once
282 // TODO remove this and add a flag to DM so we can run skps multiple times
283//#define DOUBLE_LOOP
284#ifdef DOUBLE_LOOP
285 {
286 SkAutoCanvasRestore acr(canvas, true);
287#endif
288 canvas->drawPicture(pic);
289#ifdef DOUBLE_LOOP
290 }
291 canvas->clear(0);
mtklein748ca3b2015-01-15 10:56:12 -0800292 canvas->drawPicture(pic);
joshualitt7c3a2f82015-03-31 13:32:05 -0700293#endif
mtklein748ca3b2015-01-15 10:56:12 -0800294 return "";
295}
296
297SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700298 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
299 if (!stream) {
300 return SkISize::Make(0,0);
301 }
302 SkPictInfo info;
303 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
304 return SkISize::Make(0,0);
305 }
306 SkRect viewport = kSKPViewport;
307 if (!viewport.intersect(info.fCullRect)) {
308 return SkISize::Make(0,0);
309 }
310 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800311}
312
313Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
314
315/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
316
mtkleinad66f9b2015-02-13 15:11:10 -0800317Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
318 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
319 return src.draw(canvas);
320}
321
322/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
323
mtkleinb9eb4ac2015-02-02 18:26:03 -0800324DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
325
mtklein82d28432015-01-15 12:46:02 -0800326GPUSink::GPUSink(GrContextFactory::GLContextType ct,
327 GrGLStandard api,
328 int samples,
329 bool dfText,
330 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800331 : fContextType(ct)
332 , fGpuAPI(api)
333 , fSampleCount(samples)
mtklein82d28432015-01-15 12:46:02 -0800334 , fUseDFText(dfText)
335 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800336
337int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800338 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800339}
340
joshualitt5f5a8d72015-02-25 14:09:45 -0800341void PreAbandonGpuContextErrorHandler(SkError, void*) {}
342
mtkleinb9eb4ac2015-02-02 18:26:03 -0800343Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
mtklein55e88b22015-01-21 15:50:13 -0800344 GrContextFactory factory;
mtkleinf4ba3212015-01-28 15:32:24 -0800345 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800346 const SkImageInfo info =
347 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
348 SkAutoTUnref<SkSurface> surface(
mtklein55e88b22015-01-21 15:50:13 -0800349 NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText));
mtklein748ca3b2015-01-15 10:56:12 -0800350 if (!surface) {
351 return "Could not create a surface.";
352 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800353 if (FLAGS_preAbandonGpuContext) {
354 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, NULL);
355 factory.abandonContexts();
356 }
mtklein748ca3b2015-01-15 10:56:12 -0800357 SkCanvas* canvas = surface->getCanvas();
358 Error err = src.draw(canvas);
359 if (!err.isEmpty()) {
360 return err;
361 }
362 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800363 if (FLAGS_gpuStats) {
364 canvas->getGrContext()->dumpCacheStats(log);
365 canvas->getGrContext()->dumpGpuStats(log);
366 }
mtklein748ca3b2015-01-15 10:56:12 -0800367 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800368 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800369 if (FLAGS_abandonGpuContext) {
370 factory.abandonContexts();
371 }
mtklein748ca3b2015-01-15 10:56:12 -0800372 return "";
373}
374
375/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
376
halcanary47ef4d52015-03-03 09:13:09 -0800377static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
378 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
379 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800380 int width = src.size().width(),
381 height = src.size().height();
382
halcanary7e798182015-04-14 14:06:18 -0700383 if (FLAGS_multiPage) {
384 const int kLetterWidth = 612, // 8.5 * 72
385 kLetterHeight = 792; // 11 * 72
386 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
387 SkIntToScalar(kLetterHeight));
halcanaryfd4a9932015-01-28 11:45:58 -0800388
halcanary7e798182015-04-14 14:06:18 -0700389 int xPages = ((width - 1) / kLetterWidth) + 1;
390 int yPages = ((height - 1) / kLetterHeight) + 1;
halcanaryfd4a9932015-01-28 11:45:58 -0800391
halcanary7e798182015-04-14 14:06:18 -0700392 for (int y = 0; y < yPages; ++y) {
393 for (int x = 0; x < xPages; ++x) {
394 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
395 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
396 SkCanvas* canvas =
397 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
398 if (!canvas) {
399 return "SkDocument::beginPage(w,h) returned NULL";
400 }
401 canvas->clipRect(letter);
402 canvas->translate(-letter.width() * x, -letter.height() * y);
403 Error err = src.draw(canvas);
404 if (!err.isEmpty()) {
405 return err;
406 }
407 doc->endPage();
djsollen2ab90002015-04-03 06:38:31 -0700408 }
halcanaryfd4a9932015-01-28 11:45:58 -0800409 }
halcanary7e798182015-04-14 14:06:18 -0700410 } else {
411 SkCanvas* canvas =
412 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
413 if (!canvas) {
414 return "SkDocument::beginPage(w,h) returned NULL";
415 }
416 Error err = src.draw(canvas);
417 if (!err.isEmpty()) {
418 return err;
419 }
420 doc->endPage();
mtklein748ca3b2015-01-15 10:56:12 -0800421 }
halcanary7e798182015-04-14 14:06:18 -0700422 if (!doc->close()) {
423 return "SkDocument::close() returned false";
424 }
halcanaryfd4a9932015-01-28 11:45:58 -0800425 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800426 return "";
427}
428
halcanary47ef4d52015-03-03 09:13:09 -0800429PDFSink::PDFSink() {}
430
431Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
432 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
433 if (!doc) {
434 return "SkDocument::CreatePDF() returned NULL";
435 }
436 return draw_skdocument(src, doc.get(), dst);
437}
438
439/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
440
441XPSSink::XPSSink() {}
442
443Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
444 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
445 if (!doc) {
446 return "SkDocument::CreateXPS() returned NULL";
447 }
448 return draw_skdocument(src, doc.get(), dst);
449}
mtklein748ca3b2015-01-15 10:56:12 -0800450/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
451
mtklein9c3f17d2015-01-28 11:35:18 -0800452SKPSink::SKPSink() {}
453
mtkleinb9eb4ac2015-02-02 18:26:03 -0800454Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800455 SkSize size;
456 size = src.size();
457 SkPictureRecorder recorder;
458 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
459 if (!err.isEmpty()) {
460 return err;
461 }
462 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
463 pic->serialize(dst);
464 return "";
465}
466
467/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
468
mtklein8a4527e2015-01-31 20:00:58 -0800469SVGSink::SVGSink() {}
470
mtkleinb9eb4ac2015-02-02 18:26:03 -0800471Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita2aafe6f2015-02-06 12:51:10 -0800472 SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst)));
473 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
474 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
475 xmlWriter));
476 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800477}
478
479/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
480
mtklein748ca3b2015-01-15 10:56:12 -0800481RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
482
mtkleinb9eb4ac2015-02-02 18:26:03 -0800483Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800484 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800485 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
486 SkAlphaType alphaType = kPremul_SkAlphaType;
487 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
488
489 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
490 dst->eraseColor(SK_ColorTRANSPARENT);
491 SkCanvas canvas(*dst);
492 return src.draw(&canvas);
493}
494
495/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
496
mtkleind603b222015-02-17 11:13:33 -0800497static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
498 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
499 matrix->mapRect(&bounds);
500 matrix->postTranslate(-bounds.x(), -bounds.y());
501 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
502}
503
mtklein748ca3b2015-01-15 10:56:12 -0800504ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
505
mtkleinb9eb4ac2015-02-02 18:26:03 -0800506Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800507 // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
508 struct ProxySrc : public Src {
mtkleind603b222015-02-17 11:13:33 -0800509 const Src& fSrc;
510 SkMatrix fMatrix;
511 SkISize fSize;
512
513 ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {
514 fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height());
515 }
mtklein748ca3b2015-01-15 10:56:12 -0800516
mtklein36352bf2015-03-25 18:17:31 -0700517 Error draw(SkCanvas* canvas) const override {
mtklein748ca3b2015-01-15 10:56:12 -0800518 canvas->concat(fMatrix);
519 return fSrc.draw(canvas);
520 }
mtklein36352bf2015-03-25 18:17:31 -0700521 SkISize size() const override { return fSize; }
522 Name name() const override { sk_throw(); return ""; } // No one should be calling this.
mtklein748ca3b2015-01-15 10:56:12 -0800523 } proxy(src, fMatrix);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800524 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800525}
526
mtkleind603b222015-02-17 11:13:33 -0800527// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
528// This should be pixel-preserving.
529ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
530
531Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
532 Error err = fSink->draw(src, bitmap, stream, log);
533 if (!err.isEmpty()) {
534 return err;
535 }
536
537 SkMatrix inverse;
538 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
539 return "Cannot upright --matrix.";
540 }
541 SkMatrix upright = SkMatrix::I();
542 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
543 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
544 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
545 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
546
547 SkBitmap uprighted;
548 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
549 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
550
551 SkCanvas canvas(uprighted);
552 canvas.concat(upright);
553 SkPaint paint;
554 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
555 canvas.drawBitmap(*bitmap, 0, 0, &paint);
556
557 *bitmap = uprighted;
558 bitmap->lockPixels();
559 return "";
560}
561
mtklein748ca3b2015-01-15 10:56:12 -0800562/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
563
mtklein7edca212015-01-21 13:18:51 -0800564ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
mtklein748ca3b2015-01-15 10:56:12 -0800565
mtkleinb9eb4ac2015-02-02 18:26:03 -0800566Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein7edca212015-01-21 13:18:51 -0800567 // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
mtklein748ca3b2015-01-15 10:56:12 -0800568 struct ProxySrc : public Src {
569 const Src& fSrc;
mtklein7edca212015-01-21 13:18:51 -0800570 ProxySrc(const Src& src) : fSrc(src) {}
mtklein748ca3b2015-01-15 10:56:12 -0800571
mtklein36352bf2015-03-25 18:17:31 -0700572 Error draw(SkCanvas* canvas) const override {
mtklein748ca3b2015-01-15 10:56:12 -0800573 SkISize size = this->size();
mtklein748ca3b2015-01-15 10:56:12 -0800574 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
575 SkGPipeWriter pipe;
mtklein7edca212015-01-21 13:18:51 -0800576 const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
577 return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
mtklein748ca3b2015-01-15 10:56:12 -0800578 }
mtklein36352bf2015-03-25 18:17:31 -0700579 SkISize size() const override { return fSrc.size(); }
580 Name name() const override { sk_throw(); return ""; } // No one should be calling this.
mtklein7edca212015-01-21 13:18:51 -0800581 } proxy(src);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800582 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800583}
584
585/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
586
587ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
588
mtkleinb9eb4ac2015-02-02 18:26:03 -0800589Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log)
590 const {
mtklein748ca3b2015-01-15 10:56:12 -0800591 // Record our Src into a picture.
592 SkSize size;
593 size = src.size();
594 SkPictureRecorder recorder;
595 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
596 if (!err.isEmpty()) {
597 return err;
598 }
599 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
600
601 // Serialize it and then deserialize it.
602 SkDynamicMemoryWStream wStream;
603 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -0800604 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
mtkleinb3e5e4d2015-03-25 13:13:43 -0700605 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap));
mtklein748ca3b2015-01-15 10:56:12 -0800606
607 // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
608 struct ProxySrc : public Src {
609 const SkPicture* fPic;
610 const SkISize fSize;
611 ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {}
612
mtklein36352bf2015-03-25 18:17:31 -0700613 Error draw(SkCanvas* canvas) const override {
mtklein748ca3b2015-01-15 10:56:12 -0800614 canvas->drawPicture(fPic);
615 return "";
616 }
mtklein36352bf2015-03-25 18:17:31 -0700617 SkISize size() const override { return fSize; }
618 Name name() const override { sk_throw(); return ""; } // No one should be calling this.
mtklein748ca3b2015-01-15 10:56:12 -0800619 } proxy(deserialized, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800620 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800621}
622
623/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
624
625ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
626 : fW(w)
627 , fH(h)
628 , fFactory(factory)
629 , fSink(sink) {}
630
mtkleinb9eb4ac2015-02-02 18:26:03 -0800631Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800632 // Record our Src into a picture.
633 SkSize size;
634 size = src.size();
635 SkPictureRecorder recorder;
636 Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get()));
637 if (!err.isEmpty()) {
638 return err;
639 }
mtkleinb7e8d692015-04-07 08:30:32 -0700640 SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture());
mtklein748ca3b2015-01-15 10:56:12 -0800641
642 // Turn that picture into a Src that draws into our Sink via tiles + MPD.
643 struct ProxySrc : public Src {
644 const int fW, fH;
645 const SkPicture* fPic;
646 const SkISize fSize;
647 ProxySrc(int w, int h, const SkPicture* pic, SkISize size)
648 : fW(w), fH(h), fPic(pic), fSize(size) {}
649
mtklein36352bf2015-03-25 18:17:31 -0700650 Error draw(SkCanvas* canvas) const override {
mtklein748ca3b2015-01-15 10:56:12 -0800651 const int xTiles = (fSize.width() + fW - 1) / fW,
652 yTiles = (fSize.height() + fH - 1) / fH;
653 SkMultiPictureDraw mpd(xTiles*yTiles);
654 SkTDArray<SkSurface*> surfaces;
655 surfaces.setReserve(xTiles*yTiles);
656
657 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
658 for (int j = 0; j < yTiles; j++) {
659 for (int i = 0; i < xTiles; i++) {
660 // This lets our ultimate Sink determine the best kind of surface.
661 // E.g., if it's a GpuSink, the surfaces and images are textures.
662 SkSurface* s = canvas->newSurface(info);
663 if (!s) {
664 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
665 }
666 surfaces.push(s);
667 SkCanvas* c = s->getCanvas();
668 c->translate(SkIntToScalar(-i * fW),
669 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
670 mpd.add(c, fPic);
671 }
672 }
673 mpd.draw();
674 for (int j = 0; j < yTiles; j++) {
675 for (int i = 0; i < xTiles; i++) {
676 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
677 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
678 }
679 }
680 surfaces.unrefAll();
681 return "";
682 }
mtklein36352bf2015-03-25 18:17:31 -0700683 SkISize size() const override { return fSize; }
684 Name name() const override { sk_throw(); return ""; } // No one should be calling this.
mtklein748ca3b2015-01-15 10:56:12 -0800685 } proxy(fW, fH, pic, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800686 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800687}
688
mtkleinb7e8d692015-04-07 08:30:32 -0700689/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
690
691ViaSecondPicture::ViaSecondPicture(Sink* sink) : fSink(sink) {}
692
693// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
694// This tests that any shortcuts we may take while recording that second picture are legal.
695Error ViaSecondPicture::draw(
696 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
697 struct ProxySrc : public Src {
698 const Src& fSrc;
699 ProxySrc(const Src& src) : fSrc(src) {}
700
701 Error draw(SkCanvas* canvas) const override {
702 SkSize size;
703 size = fSrc.size();
704 SkPictureRecorder recorder;
705 SkAutoTUnref<SkPicture> pic;
706 for (int i = 0; i < 2; i++) {
707 Error err = fSrc.draw(recorder.beginRecording(size.width(), size.height()));
708 if (!err.isEmpty()) {
709 return err;
710 }
711 pic.reset(recorder.endRecordingAsPicture());
712 }
713 canvas->drawPicture(pic);
714 return "";
715 }
716 SkISize size() const override { return fSrc.size(); }
717 Name name() const override { sk_throw(); return ""; } // No one should be calling this.
718 } proxy(src);
719 return fSink->draw(proxy, bitmap, stream, log);
720}
721
mtklein748ca3b2015-01-15 10:56:12 -0800722} // namespace DM