blob: 6f6664f27ce2266bd12156f1ae973cada399ec25 [file] [log] [blame]
mtklein748ca3b2015-01-15 10:56:12 -08001#include "DMSrcSink.h"
2#include "SamplePipeControllers.h"
3#include "SkCommonFlags.h"
scroggof24f2242015-03-03 08:59:20 -08004#include "SkCodec.h"
mtklein748ca3b2015-01-15 10:56:12 -08005#include "SkDocument.h"
joshualitt5f5a8d72015-02-25 14:09:45 -08006#include "SkError.h"
mtklein748ca3b2015-01-15 10:56:12 -08007#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -08008#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "SkOSFile.h"
mtkleinffa901a2015-03-16 10:38:07 -070010#include "SkPictureData.h"
mtklein748ca3b2015-01-15 10:56:12 -080011#include "SkPictureRecorder.h"
12#include "SkRandom.h"
fmalita2aafe6f2015-02-06 12:51:10 -080013#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080014#include "SkStream.h"
fmalita2aafe6f2015-02-06 12:51:10 -080015#include "SkXMLWriter.h"
mtklein748ca3b2015-01-15 10:56:12 -080016
scroggof24f2242015-03-03 08:59:20 -080017DEFINE_bool(codec, false, "Use SkCodec instead of SkImageDecoder");
18
mtklein748ca3b2015-01-15 10:56:12 -080019namespace DM {
20
mtklein748ca3b2015-01-15 10:56:12 -080021GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
22
23Error GMSrc::draw(SkCanvas* canvas) const {
24 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
25 canvas->concat(gm->getInitialTransform());
26 gm->draw(canvas);
27 return "";
28}
29
30SkISize GMSrc::size() const {
31 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
32 return gm->getISize();
33}
34
35Name GMSrc::name() const {
36 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
37 return gm->getName();
38}
39
40/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
41
mtkleinedc93bc2015-01-30 13:22:23 -080042ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
mtklein748ca3b2015-01-15 10:56:12 -080043
44Error ImageSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -080045 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080046 if (!encoded) {
47 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
48 }
mtkleinedc93bc2015-01-30 13:22:23 -080049 const SkColorType dstColorType = canvas->imageInfo().colorType();
50 if (fDivisor == 0) {
mtklein748ca3b2015-01-15 10:56:12 -080051 // Decode the full image.
52 SkBitmap bitmap;
scroggof24f2242015-03-03 08:59:20 -080053 if (FLAGS_codec) {
54 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
55 if (!codec) {
56 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
57 }
58 SkImageInfo info;
59 if (!codec->getInfo(&info)) {
60 return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str());
61 }
62 info = info.makeColorType(dstColorType);
63 if (info.alphaType() == kUnpremul_SkAlphaType) {
64 // FIXME: Currently we cannot draw unpremultiplied sources.
65 info = info.makeAlphaType(kPremul_SkAlphaType);
66 }
67 if (!bitmap.tryAllocPixels(info)) {
68 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
69 info.width(), info.height());
70 }
71 SkAutoLockPixels alp(bitmap);
72 const SkImageGenerator::Result result = codec->getPixels(info, bitmap.getPixels(),
73 bitmap.rowBytes());
scroggo56e25dd2015-03-05 11:46:40 -080074 switch (result) {
75 case SkImageGenerator::kSuccess:
76 // We consider incomplete to be valid, since we should still decode what is
77 // available.
78 case SkImageGenerator::kIncompleteInput:
79 break;
80 case SkImageGenerator::kInvalidConversion:
81 return Error::Nonfatal("Incompatible colortype conversion");
82 default:
83 // Everything else is considered a failure.
84 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
scroggof24f2242015-03-03 08:59:20 -080085 }
86 } else {
87 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
88 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
89 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
90 }
scroggo56e25dd2015-03-05 11:46:40 -080091 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) {
92 // Do not draw a bitmap with alpha to a destination without alpha.
93 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
94 }
mtklein748ca3b2015-01-15 10:56:12 -080095 }
mtklein75d98fd2015-01-18 07:05:01 -080096 encoded.reset((SkData*)NULL); // Might as well drop this when we're done with it.
mtklein748ca3b2015-01-15 10:56:12 -080097 canvas->drawBitmap(bitmap, 0,0);
98 return "";
99 }
mtkleinedc93bc2015-01-30 13:22:23 -0800100 // Decode subsets. This is a little involved.
scroggoa1193e42015-01-21 12:09:53 -0800101 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
102 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
mtklein748ca3b2015-01-15 10:56:12 -0800103 if (!decoder) {
104 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
105 }
scroggoa1193e42015-01-21 12:09:53 -0800106 stream->rewind();
mtklein748ca3b2015-01-15 10:56:12 -0800107 int w,h;
scroggoa1193e42015-01-21 12:09:53 -0800108 if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) {
mtklein4089ef72015-03-05 08:40:28 -0800109 return Error::Nonfatal("Subset decoding not supported.");
mtklein748ca3b2015-01-15 10:56:12 -0800110 }
mtkleinedc93bc2015-01-30 13:22:23 -0800111
112 // Divide the image into subsets that cover the entire image.
113 if (fDivisor > w || fDivisor > h) {
114 return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)",
115 fDivisor, fPath.c_str(), w, h);
116 }
117 const int subsetWidth = w / fDivisor,
118 subsetHeight = h / fDivisor;
119 for (int y = 0; y < h; y += subsetHeight) {
120 for (int x = 0; x < w; x += subsetWidth) {
121 SkBitmap subset;
122 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
123 if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
124 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
125 x, y, x+subsetWidth, y+subsetHeight);
126 }
scroggo56e25dd2015-03-05 11:46:40 -0800127 if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) {
128 // Do not draw a bitmap with alpha to a destination without alpha.
129 // This is not an error, but there is nothing interesting to show.
130
131 // This should only happen on the first iteration through the loop.
132 SkASSERT(0 == x && 0 == y);
133
134 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
135 }
mtkleinedc93bc2015-01-30 13:22:23 -0800136 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
mtklein748ca3b2015-01-15 10:56:12 -0800137 }
mtklein748ca3b2015-01-15 10:56:12 -0800138 }
139 return "";
140}
141
142SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -0800143 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
msarett36758742015-03-16 08:27:53 -0700144 if (FLAGS_codec) {
145 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
146 if (!codec) {
147 return SkISize::Make(0,0);
148 }
149 SkImageInfo info;
150 if (!codec->getInfo(&info)) {
151 return SkISize::Make(0,0);
152 }
153 return info.dimensions();
154 } else {
155 SkBitmap bitmap;
156 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
157 encoded->size(),
158 &bitmap,
159 kUnknown_SkColorType,
160 SkImageDecoder::kDecodeBounds_Mode)) {
161 return SkISize::Make(0,0);
162 }
163 return bitmap.dimensions();
mtklein748ca3b2015-01-15 10:56:12 -0800164 }
mtklein748ca3b2015-01-15 10:56:12 -0800165}
166
mtklein9264a952015-01-20 10:11:53 -0800167Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800168 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800169}
mtklein748ca3b2015-01-15 10:56:12 -0800170
171/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
172
mtkleinf4ba3212015-01-28 15:32:24 -0800173static const SkRect kSKPViewport = {0,0, 1000,1000};
174
mtklein8d17a132015-01-30 11:42:31 -0800175SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800176
177Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800178 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800179 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800180 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
181 }
halcanary44906c62015-02-23 10:43:48 -0800182 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream));
mtklein75d98fd2015-01-18 07:05:01 -0800183 if (!pic) {
184 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
185 }
186 stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it.
mtkleinf4ba3212015-01-28 15:32:24 -0800187 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800188 canvas->drawPicture(pic);
189 return "";
190}
191
192SkISize SKPSrc::size() const {
mtkleinffa901a2015-03-16 10:38:07 -0700193 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
194 if (!stream) {
195 return SkISize::Make(0,0);
196 }
197 SkPictInfo info;
198 if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
199 return SkISize::Make(0,0);
200 }
201 SkRect viewport = kSKPViewport;
202 if (!viewport.intersect(info.fCullRect)) {
203 return SkISize::Make(0,0);
204 }
205 return viewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800206}
207
208Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
209
210/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
211
mtkleinad66f9b2015-02-13 15:11:10 -0800212Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
213 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
214 return src.draw(canvas);
215}
216
217/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
218
mtkleinb9eb4ac2015-02-02 18:26:03 -0800219DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
220
mtklein82d28432015-01-15 12:46:02 -0800221GPUSink::GPUSink(GrContextFactory::GLContextType ct,
222 GrGLStandard api,
223 int samples,
224 bool dfText,
225 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800226 : fContextType(ct)
227 , fGpuAPI(api)
228 , fSampleCount(samples)
mtklein82d28432015-01-15 12:46:02 -0800229 , fUseDFText(dfText)
230 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800231
232int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800233 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800234}
235
joshualitt5f5a8d72015-02-25 14:09:45 -0800236void PreAbandonGpuContextErrorHandler(SkError, void*) {}
237
mtkleinb9eb4ac2015-02-02 18:26:03 -0800238Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
mtklein55e88b22015-01-21 15:50:13 -0800239 GrContextFactory factory;
mtkleinf4ba3212015-01-28 15:32:24 -0800240 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800241 const SkImageInfo info =
242 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
243 SkAutoTUnref<SkSurface> surface(
mtklein55e88b22015-01-21 15:50:13 -0800244 NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText));
mtklein748ca3b2015-01-15 10:56:12 -0800245 if (!surface) {
246 return "Could not create a surface.";
247 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800248 if (FLAGS_preAbandonGpuContext) {
249 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, NULL);
250 factory.abandonContexts();
251 }
mtklein748ca3b2015-01-15 10:56:12 -0800252 SkCanvas* canvas = surface->getCanvas();
253 Error err = src.draw(canvas);
254 if (!err.isEmpty()) {
255 return err;
256 }
257 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800258 if (FLAGS_gpuStats) {
259 canvas->getGrContext()->dumpCacheStats(log);
260 canvas->getGrContext()->dumpGpuStats(log);
261 }
mtklein748ca3b2015-01-15 10:56:12 -0800262 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800263 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800264 if (FLAGS_abandonGpuContext) {
265 factory.abandonContexts();
266 }
mtklein748ca3b2015-01-15 10:56:12 -0800267 return "";
268}
269
270/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
271
halcanary47ef4d52015-03-03 09:13:09 -0800272static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
273 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
274 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800275 int width = src.size().width(),
276 height = src.size().height();
277
278 const int kLetterWidth = 612, // 8.5 * 72
279 kLetterHeight = 792; // 11 * 72
280 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
281 SkIntToScalar(kLetterHeight));
282
283 int xPages = ((width - 1) / kLetterWidth) + 1;
284 int yPages = ((height - 1) / kLetterHeight) + 1;
285
286 for (int y = 0; y < yPages; ++y) {
287 for (int x = 0; x < xPages; ++x) {
288 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
289 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
290 SkCanvas* canvas =
291 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
292 canvas->clipRect(letter);
293 canvas->translate(-letter.width() * x, -letter.height() * y);
294 Error err = src.draw(canvas);
295 if (!err.isEmpty()) {
296 return err;
297 }
298 doc->endPage();
299 }
mtklein748ca3b2015-01-15 10:56:12 -0800300 }
mtklein748ca3b2015-01-15 10:56:12 -0800301 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -0800302 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800303 return "";
304}
305
halcanary47ef4d52015-03-03 09:13:09 -0800306PDFSink::PDFSink() {}
307
308Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
309 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
310 if (!doc) {
311 return "SkDocument::CreatePDF() returned NULL";
312 }
313 return draw_skdocument(src, doc.get(), dst);
314}
315
316/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
317
318XPSSink::XPSSink() {}
319
320Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
321 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
322 if (!doc) {
323 return "SkDocument::CreateXPS() returned NULL";
324 }
325 return draw_skdocument(src, doc.get(), dst);
326}
mtklein748ca3b2015-01-15 10:56:12 -0800327/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
328
mtklein9c3f17d2015-01-28 11:35:18 -0800329SKPSink::SKPSink() {}
330
mtkleinb9eb4ac2015-02-02 18:26:03 -0800331Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800332 SkSize size;
333 size = src.size();
334 SkPictureRecorder recorder;
335 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
336 if (!err.isEmpty()) {
337 return err;
338 }
339 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
340 pic->serialize(dst);
341 return "";
342}
343
344/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
345
mtklein8a4527e2015-01-31 20:00:58 -0800346SVGSink::SVGSink() {}
347
mtkleinb9eb4ac2015-02-02 18:26:03 -0800348Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita2aafe6f2015-02-06 12:51:10 -0800349 SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst)));
350 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
351 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
352 xmlWriter));
353 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800354}
355
356/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
357
mtklein748ca3b2015-01-15 10:56:12 -0800358RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
359
mtkleinb9eb4ac2015-02-02 18:26:03 -0800360Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800361 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800362 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
363 SkAlphaType alphaType = kPremul_SkAlphaType;
364 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
365
366 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
367 dst->eraseColor(SK_ColorTRANSPARENT);
368 SkCanvas canvas(*dst);
369 return src.draw(&canvas);
370}
371
372/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
373
mtkleind603b222015-02-17 11:13:33 -0800374static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
375 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
376 matrix->mapRect(&bounds);
377 matrix->postTranslate(-bounds.x(), -bounds.y());
378 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
379}
380
mtklein748ca3b2015-01-15 10:56:12 -0800381ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
382
mtkleinb9eb4ac2015-02-02 18:26:03 -0800383Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800384 // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
385 struct ProxySrc : public Src {
mtkleind603b222015-02-17 11:13:33 -0800386 const Src& fSrc;
387 SkMatrix fMatrix;
388 SkISize fSize;
389
390 ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {
391 fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height());
392 }
mtklein748ca3b2015-01-15 10:56:12 -0800393
394 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
395 canvas->concat(fMatrix);
396 return fSrc.draw(canvas);
397 }
mtkleind603b222015-02-17 11:13:33 -0800398 SkISize size() const SK_OVERRIDE { return fSize; }
mtklein748ca3b2015-01-15 10:56:12 -0800399 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
400 } proxy(src, fMatrix);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800401 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800402}
403
mtkleind603b222015-02-17 11:13:33 -0800404// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
405// This should be pixel-preserving.
406ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
407
408Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
409 Error err = fSink->draw(src, bitmap, stream, log);
410 if (!err.isEmpty()) {
411 return err;
412 }
413
414 SkMatrix inverse;
415 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
416 return "Cannot upright --matrix.";
417 }
418 SkMatrix upright = SkMatrix::I();
419 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
420 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
421 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
422 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
423
424 SkBitmap uprighted;
425 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
426 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
427
428 SkCanvas canvas(uprighted);
429 canvas.concat(upright);
430 SkPaint paint;
431 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
432 canvas.drawBitmap(*bitmap, 0, 0, &paint);
433
434 *bitmap = uprighted;
435 bitmap->lockPixels();
436 return "";
437}
438
mtklein748ca3b2015-01-15 10:56:12 -0800439/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
440
mtklein7edca212015-01-21 13:18:51 -0800441ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
mtklein748ca3b2015-01-15 10:56:12 -0800442
mtkleinb9eb4ac2015-02-02 18:26:03 -0800443Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein7edca212015-01-21 13:18:51 -0800444 // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
mtklein748ca3b2015-01-15 10:56:12 -0800445 struct ProxySrc : public Src {
446 const Src& fSrc;
mtklein7edca212015-01-21 13:18:51 -0800447 ProxySrc(const Src& src) : fSrc(src) {}
mtklein748ca3b2015-01-15 10:56:12 -0800448
449 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
450 SkISize size = this->size();
mtklein748ca3b2015-01-15 10:56:12 -0800451 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
452 SkGPipeWriter pipe;
mtklein7edca212015-01-21 13:18:51 -0800453 const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
454 return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
mtklein748ca3b2015-01-15 10:56:12 -0800455 }
456 SkISize size() const SK_OVERRIDE { return fSrc.size(); }
457 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
mtklein7edca212015-01-21 13:18:51 -0800458 } proxy(src);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800459 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800460}
461
462/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
463
464ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
465
mtkleinb9eb4ac2015-02-02 18:26:03 -0800466Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log)
467 const {
mtklein748ca3b2015-01-15 10:56:12 -0800468 // Record our Src into a picture.
469 SkSize size;
470 size = src.size();
471 SkPictureRecorder recorder;
472 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
473 if (!err.isEmpty()) {
474 return err;
475 }
476 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
477
478 // Serialize it and then deserialize it.
479 SkDynamicMemoryWStream wStream;
480 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -0800481 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
halcanary44906c62015-02-23 10:43:48 -0800482 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream));
mtklein748ca3b2015-01-15 10:56:12 -0800483
484 // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
485 struct ProxySrc : public Src {
486 const SkPicture* fPic;
487 const SkISize fSize;
488 ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {}
489
490 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
491 canvas->drawPicture(fPic);
492 return "";
493 }
494 SkISize size() const SK_OVERRIDE { return fSize; }
495 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
496 } proxy(deserialized, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800497 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800498}
499
500/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
501
502ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
503 : fW(w)
504 , fH(h)
505 , fFactory(factory)
506 , fSink(sink) {}
507
mtkleinb9eb4ac2015-02-02 18:26:03 -0800508Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800509 // Record our Src into a picture.
510 SkSize size;
511 size = src.size();
512 SkPictureRecorder recorder;
513 Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get()));
514 if (!err.isEmpty()) {
515 return err;
516 }
517 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
518
519 // Turn that picture into a Src that draws into our Sink via tiles + MPD.
520 struct ProxySrc : public Src {
521 const int fW, fH;
522 const SkPicture* fPic;
523 const SkISize fSize;
524 ProxySrc(int w, int h, const SkPicture* pic, SkISize size)
525 : fW(w), fH(h), fPic(pic), fSize(size) {}
526
527 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
528 const int xTiles = (fSize.width() + fW - 1) / fW,
529 yTiles = (fSize.height() + fH - 1) / fH;
530 SkMultiPictureDraw mpd(xTiles*yTiles);
531 SkTDArray<SkSurface*> surfaces;
532 surfaces.setReserve(xTiles*yTiles);
533
534 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
535 for (int j = 0; j < yTiles; j++) {
536 for (int i = 0; i < xTiles; i++) {
537 // This lets our ultimate Sink determine the best kind of surface.
538 // E.g., if it's a GpuSink, the surfaces and images are textures.
539 SkSurface* s = canvas->newSurface(info);
540 if (!s) {
541 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
542 }
543 surfaces.push(s);
544 SkCanvas* c = s->getCanvas();
545 c->translate(SkIntToScalar(-i * fW),
546 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
547 mpd.add(c, fPic);
548 }
549 }
550 mpd.draw();
551 for (int j = 0; j < yTiles; j++) {
552 for (int i = 0; i < xTiles; i++) {
553 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
554 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
555 }
556 }
557 surfaces.unrefAll();
558 return "";
559 }
560 SkISize size() const SK_OVERRIDE { return fSize; }
561 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
562 } proxy(fW, fH, pic, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800563 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800564}
565
566} // namespace DM