blob: 67f8ac6edb0da8df4faba5350170e118a4cc0689 [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"
10#include "SkPictureRecorder.h"
11#include "SkRandom.h"
fmalita2aafe6f2015-02-06 12:51:10 -080012#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080013#include "SkStream.h"
fmalita2aafe6f2015-02-06 12:51:10 -080014#include "SkXMLWriter.h"
mtklein748ca3b2015-01-15 10:56:12 -080015
scroggof24f2242015-03-03 08:59:20 -080016DEFINE_bool(codec, false, "Use SkCodec instead of SkImageDecoder");
17
mtklein748ca3b2015-01-15 10:56:12 -080018namespace DM {
19
mtklein748ca3b2015-01-15 10:56:12 -080020GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
21
22Error GMSrc::draw(SkCanvas* canvas) const {
23 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
24 canvas->concat(gm->getInitialTransform());
25 gm->draw(canvas);
26 return "";
27}
28
29SkISize GMSrc::size() const {
30 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
31 return gm->getISize();
32}
33
34Name GMSrc::name() const {
35 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
36 return gm->getName();
37}
38
39/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
40
mtkleinedc93bc2015-01-30 13:22:23 -080041ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
mtklein748ca3b2015-01-15 10:56:12 -080042
43Error ImageSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -080044 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080045 if (!encoded) {
46 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
47 }
mtkleinedc93bc2015-01-30 13:22:23 -080048 const SkColorType dstColorType = canvas->imageInfo().colorType();
49 if (fDivisor == 0) {
mtklein748ca3b2015-01-15 10:56:12 -080050 // Decode the full image.
51 SkBitmap bitmap;
scroggof24f2242015-03-03 08:59:20 -080052 if (FLAGS_codec) {
53 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
54 if (!codec) {
55 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
56 }
57 SkImageInfo info;
58 if (!codec->getInfo(&info)) {
59 return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str());
60 }
61 info = info.makeColorType(dstColorType);
62 if (info.alphaType() == kUnpremul_SkAlphaType) {
63 // FIXME: Currently we cannot draw unpremultiplied sources.
64 info = info.makeAlphaType(kPremul_SkAlphaType);
65 }
66 if (!bitmap.tryAllocPixels(info)) {
67 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
68 info.width(), info.height());
69 }
70 SkAutoLockPixels alp(bitmap);
71 const SkImageGenerator::Result result = codec->getPixels(info, bitmap.getPixels(),
72 bitmap.rowBytes());
73 if (result != SkImageGenerator::kSuccess) {
74 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
75 }
76 } else {
77 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
78 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
79 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
80 }
mtklein748ca3b2015-01-15 10:56:12 -080081 }
mtklein75d98fd2015-01-18 07:05:01 -080082 encoded.reset((SkData*)NULL); // Might as well drop this when we're done with it.
mtklein748ca3b2015-01-15 10:56:12 -080083 canvas->drawBitmap(bitmap, 0,0);
84 return "";
85 }
mtkleinedc93bc2015-01-30 13:22:23 -080086 // Decode subsets. This is a little involved.
scroggoa1193e42015-01-21 12:09:53 -080087 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
88 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
mtklein748ca3b2015-01-15 10:56:12 -080089 if (!decoder) {
90 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
91 }
scroggoa1193e42015-01-21 12:09:53 -080092 stream->rewind();
mtklein748ca3b2015-01-15 10:56:12 -080093 int w,h;
scroggoa1193e42015-01-21 12:09:53 -080094 if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) {
mtklein748ca3b2015-01-15 10:56:12 -080095 return ""; // Not an error. Subset decoding is not always supported.
96 }
mtkleinedc93bc2015-01-30 13:22:23 -080097
98 // Divide the image into subsets that cover the entire image.
99 if (fDivisor > w || fDivisor > h) {
100 return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)",
101 fDivisor, fPath.c_str(), w, h);
102 }
103 const int subsetWidth = w / fDivisor,
104 subsetHeight = h / fDivisor;
105 for (int y = 0; y < h; y += subsetHeight) {
106 for (int x = 0; x < w; x += subsetWidth) {
107 SkBitmap subset;
108 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
109 if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
110 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
111 x, y, x+subsetWidth, y+subsetHeight);
112 }
113 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
mtklein748ca3b2015-01-15 10:56:12 -0800114 }
mtklein748ca3b2015-01-15 10:56:12 -0800115 }
116 return "";
117}
118
119SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -0800120 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -0800121 SkBitmap bitmap;
122 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
123 encoded->size(),
124 &bitmap,
125 kUnknown_SkColorType,
126 SkImageDecoder::kDecodeBounds_Mode)) {
127 return SkISize::Make(0,0);
128 }
129 return bitmap.dimensions();
130}
131
mtklein9264a952015-01-20 10:11:53 -0800132Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800133 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800134}
mtklein748ca3b2015-01-15 10:56:12 -0800135
136/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
137
mtkleinf4ba3212015-01-28 15:32:24 -0800138static const SkRect kSKPViewport = {0,0, 1000,1000};
139
mtklein8d17a132015-01-30 11:42:31 -0800140SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800141
142Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800143 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800144 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800145 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
146 }
halcanary44906c62015-02-23 10:43:48 -0800147 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream));
mtklein75d98fd2015-01-18 07:05:01 -0800148 if (!pic) {
149 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
150 }
151 stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it.
mtkleinf4ba3212015-01-28 15:32:24 -0800152 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800153 canvas->drawPicture(pic);
154 return "";
155}
156
157SkISize SKPSrc::size() const {
mtkleinf4ba3212015-01-28 15:32:24 -0800158 // This may be unnecessarily large.
159 return kSKPViewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800160}
161
162Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
163
164/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
165
mtkleinad66f9b2015-02-13 15:11:10 -0800166Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
167 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
168 return src.draw(canvas);
169}
170
171/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
172
mtkleinb9eb4ac2015-02-02 18:26:03 -0800173DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
174
mtklein82d28432015-01-15 12:46:02 -0800175GPUSink::GPUSink(GrContextFactory::GLContextType ct,
176 GrGLStandard api,
177 int samples,
178 bool dfText,
179 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800180 : fContextType(ct)
181 , fGpuAPI(api)
182 , fSampleCount(samples)
mtklein82d28432015-01-15 12:46:02 -0800183 , fUseDFText(dfText)
184 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800185
186int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800187 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800188}
189
joshualitt5f5a8d72015-02-25 14:09:45 -0800190void PreAbandonGpuContextErrorHandler(SkError, void*) {}
191
mtkleinb9eb4ac2015-02-02 18:26:03 -0800192Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
mtklein55e88b22015-01-21 15:50:13 -0800193 GrContextFactory factory;
mtkleinf4ba3212015-01-28 15:32:24 -0800194 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800195 const SkImageInfo info =
196 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
197 SkAutoTUnref<SkSurface> surface(
mtklein55e88b22015-01-21 15:50:13 -0800198 NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText));
mtklein748ca3b2015-01-15 10:56:12 -0800199 if (!surface) {
200 return "Could not create a surface.";
201 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800202 if (FLAGS_preAbandonGpuContext) {
203 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, NULL);
204 factory.abandonContexts();
205 }
mtklein748ca3b2015-01-15 10:56:12 -0800206 SkCanvas* canvas = surface->getCanvas();
207 Error err = src.draw(canvas);
208 if (!err.isEmpty()) {
209 return err;
210 }
211 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800212 if (FLAGS_gpuStats) {
213 canvas->getGrContext()->dumpCacheStats(log);
214 canvas->getGrContext()->dumpGpuStats(log);
215 }
mtklein748ca3b2015-01-15 10:56:12 -0800216 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800217 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800218 if (FLAGS_abandonGpuContext) {
219 factory.abandonContexts();
220 }
mtklein748ca3b2015-01-15 10:56:12 -0800221 return "";
222}
223
224/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
225
halcanaryc2574f32015-03-03 08:34:14 -0800226PDFSink::PDFSink() {}
227
228Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
229 // Print the given DM:Src to a PDF, breaking on 8.5x11 pages.
230 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
231
halcanaryfd4a9932015-01-28 11:45:58 -0800232 int width = src.size().width(),
233 height = src.size().height();
234
235 const int kLetterWidth = 612, // 8.5 * 72
236 kLetterHeight = 792; // 11 * 72
237 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
238 SkIntToScalar(kLetterHeight));
239
240 int xPages = ((width - 1) / kLetterWidth) + 1;
241 int yPages = ((height - 1) / kLetterHeight) + 1;
242
243 for (int y = 0; y < yPages; ++y) {
244 for (int x = 0; x < xPages; ++x) {
245 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
246 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
247 SkCanvas* canvas =
248 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
249 canvas->clipRect(letter);
250 canvas->translate(-letter.width() * x, -letter.height() * y);
251 Error err = src.draw(canvas);
252 if (!err.isEmpty()) {
253 return err;
254 }
255 doc->endPage();
256 }
mtklein748ca3b2015-01-15 10:56:12 -0800257 }
mtklein748ca3b2015-01-15 10:56:12 -0800258 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -0800259 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800260 return "";
261}
262
263/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
264
mtklein9c3f17d2015-01-28 11:35:18 -0800265SKPSink::SKPSink() {}
266
mtkleinb9eb4ac2015-02-02 18:26:03 -0800267Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800268 SkSize size;
269 size = src.size();
270 SkPictureRecorder recorder;
271 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
272 if (!err.isEmpty()) {
273 return err;
274 }
275 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
276 pic->serialize(dst);
277 return "";
278}
279
280/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
281
mtklein8a4527e2015-01-31 20:00:58 -0800282SVGSink::SVGSink() {}
283
mtkleinb9eb4ac2015-02-02 18:26:03 -0800284Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita2aafe6f2015-02-06 12:51:10 -0800285 SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst)));
286 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
287 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
288 xmlWriter));
289 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800290}
291
292/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
293
mtklein748ca3b2015-01-15 10:56:12 -0800294RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
295
mtkleinb9eb4ac2015-02-02 18:26:03 -0800296Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800297 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800298 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
299 SkAlphaType alphaType = kPremul_SkAlphaType;
300 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
301
302 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
303 dst->eraseColor(SK_ColorTRANSPARENT);
304 SkCanvas canvas(*dst);
305 return src.draw(&canvas);
306}
307
308/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
309
mtkleind603b222015-02-17 11:13:33 -0800310static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
311 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
312 matrix->mapRect(&bounds);
313 matrix->postTranslate(-bounds.x(), -bounds.y());
314 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
315}
316
mtklein748ca3b2015-01-15 10:56:12 -0800317ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
318
mtkleinb9eb4ac2015-02-02 18:26:03 -0800319Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800320 // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
321 struct ProxySrc : public Src {
mtkleind603b222015-02-17 11:13:33 -0800322 const Src& fSrc;
323 SkMatrix fMatrix;
324 SkISize fSize;
325
326 ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {
327 fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height());
328 }
mtklein748ca3b2015-01-15 10:56:12 -0800329
330 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
331 canvas->concat(fMatrix);
332 return fSrc.draw(canvas);
333 }
mtkleind603b222015-02-17 11:13:33 -0800334 SkISize size() const SK_OVERRIDE { return fSize; }
mtklein748ca3b2015-01-15 10:56:12 -0800335 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
336 } proxy(src, fMatrix);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800337 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800338}
339
mtkleind603b222015-02-17 11:13:33 -0800340// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
341// This should be pixel-preserving.
342ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
343
344Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
345 Error err = fSink->draw(src, bitmap, stream, log);
346 if (!err.isEmpty()) {
347 return err;
348 }
349
350 SkMatrix inverse;
351 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
352 return "Cannot upright --matrix.";
353 }
354 SkMatrix upright = SkMatrix::I();
355 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
356 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
357 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
358 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
359
360 SkBitmap uprighted;
361 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
362 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
363
364 SkCanvas canvas(uprighted);
365 canvas.concat(upright);
366 SkPaint paint;
367 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
368 canvas.drawBitmap(*bitmap, 0, 0, &paint);
369
370 *bitmap = uprighted;
371 bitmap->lockPixels();
372 return "";
373}
374
mtklein748ca3b2015-01-15 10:56:12 -0800375/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
376
mtklein7edca212015-01-21 13:18:51 -0800377ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
mtklein748ca3b2015-01-15 10:56:12 -0800378
mtkleinb9eb4ac2015-02-02 18:26:03 -0800379Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein7edca212015-01-21 13:18:51 -0800380 // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
mtklein748ca3b2015-01-15 10:56:12 -0800381 struct ProxySrc : public Src {
382 const Src& fSrc;
mtklein7edca212015-01-21 13:18:51 -0800383 ProxySrc(const Src& src) : fSrc(src) {}
mtklein748ca3b2015-01-15 10:56:12 -0800384
385 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
386 SkISize size = this->size();
mtklein748ca3b2015-01-15 10:56:12 -0800387 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
388 SkGPipeWriter pipe;
mtklein7edca212015-01-21 13:18:51 -0800389 const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
390 return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
mtklein748ca3b2015-01-15 10:56:12 -0800391 }
392 SkISize size() const SK_OVERRIDE { return fSrc.size(); }
393 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
mtklein7edca212015-01-21 13:18:51 -0800394 } proxy(src);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800395 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800396}
397
398/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
399
400ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
401
mtkleinb9eb4ac2015-02-02 18:26:03 -0800402Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log)
403 const {
mtklein748ca3b2015-01-15 10:56:12 -0800404 // Record our Src into a picture.
405 SkSize size;
406 size = src.size();
407 SkPictureRecorder recorder;
408 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
409 if (!err.isEmpty()) {
410 return err;
411 }
412 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
413
414 // Serialize it and then deserialize it.
415 SkDynamicMemoryWStream wStream;
416 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -0800417 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
halcanary44906c62015-02-23 10:43:48 -0800418 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream));
mtklein748ca3b2015-01-15 10:56:12 -0800419
420 // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
421 struct ProxySrc : public Src {
422 const SkPicture* fPic;
423 const SkISize fSize;
424 ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {}
425
426 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
427 canvas->drawPicture(fPic);
428 return "";
429 }
430 SkISize size() const SK_OVERRIDE { return fSize; }
431 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
432 } proxy(deserialized, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800433 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800434}
435
436/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
437
438ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
439 : fW(w)
440 , fH(h)
441 , fFactory(factory)
442 , fSink(sink) {}
443
mtkleinb9eb4ac2015-02-02 18:26:03 -0800444Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800445 // Record our Src into a picture.
446 SkSize size;
447 size = src.size();
448 SkPictureRecorder recorder;
449 Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get()));
450 if (!err.isEmpty()) {
451 return err;
452 }
453 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
454
455 // Turn that picture into a Src that draws into our Sink via tiles + MPD.
456 struct ProxySrc : public Src {
457 const int fW, fH;
458 const SkPicture* fPic;
459 const SkISize fSize;
460 ProxySrc(int w, int h, const SkPicture* pic, SkISize size)
461 : fW(w), fH(h), fPic(pic), fSize(size) {}
462
463 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
464 const int xTiles = (fSize.width() + fW - 1) / fW,
465 yTiles = (fSize.height() + fH - 1) / fH;
466 SkMultiPictureDraw mpd(xTiles*yTiles);
467 SkTDArray<SkSurface*> surfaces;
468 surfaces.setReserve(xTiles*yTiles);
469
470 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
471 for (int j = 0; j < yTiles; j++) {
472 for (int i = 0; i < xTiles; i++) {
473 // This lets our ultimate Sink determine the best kind of surface.
474 // E.g., if it's a GpuSink, the surfaces and images are textures.
475 SkSurface* s = canvas->newSurface(info);
476 if (!s) {
477 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
478 }
479 surfaces.push(s);
480 SkCanvas* c = s->getCanvas();
481 c->translate(SkIntToScalar(-i * fW),
482 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
483 mpd.add(c, fPic);
484 }
485 }
486 mpd.draw();
487 for (int j = 0; j < yTiles; j++) {
488 for (int i = 0; i < xTiles; i++) {
489 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
490 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
491 }
492 }
493 surfaces.unrefAll();
494 return "";
495 }
496 SkISize size() const SK_OVERRIDE { return fSize; }
497 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
498 } proxy(fW, fH, pic, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800499 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800500}
501
502} // namespace DM