blob: e67d4355e4ca4caf4f7605422b2bd73f8b91a3ac [file] [log] [blame]
mtklein748ca3b2015-01-15 10:56:12 -08001#include "DMSrcSink.h"
2#include "SamplePipeControllers.h"
3#include "SkCommonFlags.h"
4#include "SkDocument.h"
joshualitt5f5a8d72015-02-25 14:09:45 -08005#include "SkError.h"
mtklein748ca3b2015-01-15 10:56:12 -08006#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -08007#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -08008#include "SkOSFile.h"
9#include "SkPictureRecorder.h"
10#include "SkRandom.h"
fmalita2aafe6f2015-02-06 12:51:10 -080011#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080012#include "SkStream.h"
fmalita2aafe6f2015-02-06 12:51:10 -080013#include "SkXMLWriter.h"
mtklein748ca3b2015-01-15 10:56:12 -080014
15namespace DM {
16
mtklein748ca3b2015-01-15 10:56:12 -080017GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
18
19Error GMSrc::draw(SkCanvas* canvas) const {
20 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
21 canvas->concat(gm->getInitialTransform());
22 gm->draw(canvas);
23 return "";
24}
25
26SkISize GMSrc::size() const {
27 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
28 return gm->getISize();
29}
30
31Name GMSrc::name() const {
32 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
33 return gm->getName();
34}
35
36/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
37
mtkleinedc93bc2015-01-30 13:22:23 -080038ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
mtklein748ca3b2015-01-15 10:56:12 -080039
40Error ImageSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -080041 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080042 if (!encoded) {
43 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
44 }
mtkleinedc93bc2015-01-30 13:22:23 -080045 const SkColorType dstColorType = canvas->imageInfo().colorType();
46 if (fDivisor == 0) {
mtklein748ca3b2015-01-15 10:56:12 -080047 // Decode the full image.
48 SkBitmap bitmap;
mtkleinedc93bc2015-01-30 13:22:23 -080049 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
50 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
mtklein748ca3b2015-01-15 10:56:12 -080051 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
52 }
mtklein75d98fd2015-01-18 07:05:01 -080053 encoded.reset((SkData*)NULL); // Might as well drop this when we're done with it.
mtklein748ca3b2015-01-15 10:56:12 -080054 canvas->drawBitmap(bitmap, 0,0);
55 return "";
56 }
mtkleinedc93bc2015-01-30 13:22:23 -080057 // Decode subsets. This is a little involved.
scroggoa1193e42015-01-21 12:09:53 -080058 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
59 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
mtklein748ca3b2015-01-15 10:56:12 -080060 if (!decoder) {
61 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
62 }
scroggoa1193e42015-01-21 12:09:53 -080063 stream->rewind();
mtklein748ca3b2015-01-15 10:56:12 -080064 int w,h;
scroggoa1193e42015-01-21 12:09:53 -080065 if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) {
mtklein748ca3b2015-01-15 10:56:12 -080066 return ""; // Not an error. Subset decoding is not always supported.
67 }
mtkleinedc93bc2015-01-30 13:22:23 -080068
69 // Divide the image into subsets that cover the entire image.
70 if (fDivisor > w || fDivisor > h) {
71 return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)",
72 fDivisor, fPath.c_str(), w, h);
73 }
74 const int subsetWidth = w / fDivisor,
75 subsetHeight = h / fDivisor;
76 for (int y = 0; y < h; y += subsetHeight) {
77 for (int x = 0; x < w; x += subsetWidth) {
78 SkBitmap subset;
79 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
80 if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
81 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
82 x, y, x+subsetWidth, y+subsetHeight);
83 }
84 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
mtklein748ca3b2015-01-15 10:56:12 -080085 }
mtklein748ca3b2015-01-15 10:56:12 -080086 }
87 return "";
88}
89
90SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -080091 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080092 SkBitmap bitmap;
93 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
94 encoded->size(),
95 &bitmap,
96 kUnknown_SkColorType,
97 SkImageDecoder::kDecodeBounds_Mode)) {
98 return SkISize::Make(0,0);
99 }
100 return bitmap.dimensions();
101}
102
mtklein9264a952015-01-20 10:11:53 -0800103Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800104 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800105}
mtklein748ca3b2015-01-15 10:56:12 -0800106
107/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
108
mtkleinf4ba3212015-01-28 15:32:24 -0800109static const SkRect kSKPViewport = {0,0, 1000,1000};
110
mtklein8d17a132015-01-30 11:42:31 -0800111SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800112
113Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800114 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800115 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800116 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
117 }
halcanary44906c62015-02-23 10:43:48 -0800118 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream));
mtklein75d98fd2015-01-18 07:05:01 -0800119 if (!pic) {
120 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
121 }
122 stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it.
mtkleinf4ba3212015-01-28 15:32:24 -0800123 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800124 canvas->drawPicture(pic);
125 return "";
126}
127
128SkISize SKPSrc::size() const {
mtkleinf4ba3212015-01-28 15:32:24 -0800129 // This may be unnecessarily large.
130 return kSKPViewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800131}
132
133Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
134
135/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
136
mtkleinad66f9b2015-02-13 15:11:10 -0800137Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
138 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
139 return src.draw(canvas);
140}
141
142/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
143
mtkleinb9eb4ac2015-02-02 18:26:03 -0800144DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
145
mtklein82d28432015-01-15 12:46:02 -0800146GPUSink::GPUSink(GrContextFactory::GLContextType ct,
147 GrGLStandard api,
148 int samples,
149 bool dfText,
150 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800151 : fContextType(ct)
152 , fGpuAPI(api)
153 , fSampleCount(samples)
mtklein82d28432015-01-15 12:46:02 -0800154 , fUseDFText(dfText)
155 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800156
157int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800158 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800159}
160
joshualitt5f5a8d72015-02-25 14:09:45 -0800161void PreAbandonGpuContextErrorHandler(SkError, void*) {}
162
mtkleinb9eb4ac2015-02-02 18:26:03 -0800163Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
mtklein55e88b22015-01-21 15:50:13 -0800164 GrContextFactory factory;
mtkleinf4ba3212015-01-28 15:32:24 -0800165 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800166 const SkImageInfo info =
167 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
168 SkAutoTUnref<SkSurface> surface(
mtklein55e88b22015-01-21 15:50:13 -0800169 NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText));
mtklein748ca3b2015-01-15 10:56:12 -0800170 if (!surface) {
171 return "Could not create a surface.";
172 }
joshualitt5f5a8d72015-02-25 14:09:45 -0800173 if (FLAGS_preAbandonGpuContext) {
174 SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, NULL);
175 factory.abandonContexts();
176 }
mtklein748ca3b2015-01-15 10:56:12 -0800177 SkCanvas* canvas = surface->getCanvas();
178 Error err = src.draw(canvas);
179 if (!err.isEmpty()) {
180 return err;
181 }
182 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800183 if (FLAGS_gpuStats) {
184 canvas->getGrContext()->dumpCacheStats(log);
185 canvas->getGrContext()->dumpGpuStats(log);
186 }
mtklein748ca3b2015-01-15 10:56:12 -0800187 dst->allocPixels(info);
joshualitt5f5a8d72015-02-25 14:09:45 -0800188 canvas->readPixels(dst, 0, 0);
mtklein55e88b22015-01-21 15:50:13 -0800189 if (FLAGS_abandonGpuContext) {
190 factory.abandonContexts();
191 }
mtklein748ca3b2015-01-15 10:56:12 -0800192 return "";
193}
194
195/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
196
197PDFSink::PDFSink() {}
198
mtkleinb9eb4ac2015-02-02 18:26:03 -0800199Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanaryfd4a9932015-01-28 11:45:58 -0800200 // Print the given DM:Src to a PDF, breaking on 8.5x11 pages.
mtklein748ca3b2015-01-15 10:56:12 -0800201 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
mtklein748ca3b2015-01-15 10:56:12 -0800202
halcanaryfd4a9932015-01-28 11:45:58 -0800203 int width = src.size().width(),
204 height = src.size().height();
205
206 const int kLetterWidth = 612, // 8.5 * 72
207 kLetterHeight = 792; // 11 * 72
208 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
209 SkIntToScalar(kLetterHeight));
210
211 int xPages = ((width - 1) / kLetterWidth) + 1;
212 int yPages = ((height - 1) / kLetterHeight) + 1;
213
214 for (int y = 0; y < yPages; ++y) {
215 for (int x = 0; x < xPages; ++x) {
216 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
217 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
218 SkCanvas* canvas =
219 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
220 canvas->clipRect(letter);
221 canvas->translate(-letter.width() * x, -letter.height() * y);
222 Error err = src.draw(canvas);
223 if (!err.isEmpty()) {
224 return err;
225 }
226 doc->endPage();
227 }
mtklein748ca3b2015-01-15 10:56:12 -0800228 }
mtklein748ca3b2015-01-15 10:56:12 -0800229 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -0800230 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800231 return "";
232}
233
234/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
235
mtklein9c3f17d2015-01-28 11:35:18 -0800236SKPSink::SKPSink() {}
237
mtkleinb9eb4ac2015-02-02 18:26:03 -0800238Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800239 SkSize size;
240 size = src.size();
241 SkPictureRecorder recorder;
242 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
243 if (!err.isEmpty()) {
244 return err;
245 }
246 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
247 pic->serialize(dst);
248 return "";
249}
250
251/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
252
mtklein8a4527e2015-01-31 20:00:58 -0800253SVGSink::SVGSink() {}
254
mtkleinb9eb4ac2015-02-02 18:26:03 -0800255Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita2aafe6f2015-02-06 12:51:10 -0800256 SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst)));
257 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
258 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
259 xmlWriter));
260 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800261}
262
263/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
264
mtklein748ca3b2015-01-15 10:56:12 -0800265RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
266
mtkleinb9eb4ac2015-02-02 18:26:03 -0800267Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800268 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800269 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
270 SkAlphaType alphaType = kPremul_SkAlphaType;
271 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
272
273 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
274 dst->eraseColor(SK_ColorTRANSPARENT);
275 SkCanvas canvas(*dst);
276 return src.draw(&canvas);
277}
278
279/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
280
mtkleind603b222015-02-17 11:13:33 -0800281static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
282 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
283 matrix->mapRect(&bounds);
284 matrix->postTranslate(-bounds.x(), -bounds.y());
285 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
286}
287
mtklein748ca3b2015-01-15 10:56:12 -0800288ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
289
mtkleinb9eb4ac2015-02-02 18:26:03 -0800290Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800291 // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
292 struct ProxySrc : public Src {
mtkleind603b222015-02-17 11:13:33 -0800293 const Src& fSrc;
294 SkMatrix fMatrix;
295 SkISize fSize;
296
297 ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {
298 fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height());
299 }
mtklein748ca3b2015-01-15 10:56:12 -0800300
301 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
302 canvas->concat(fMatrix);
303 return fSrc.draw(canvas);
304 }
mtkleind603b222015-02-17 11:13:33 -0800305 SkISize size() const SK_OVERRIDE { return fSize; }
mtklein748ca3b2015-01-15 10:56:12 -0800306 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
307 } proxy(src, fMatrix);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800308 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800309}
310
mtkleind603b222015-02-17 11:13:33 -0800311// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
312// This should be pixel-preserving.
313ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
314
315Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
316 Error err = fSink->draw(src, bitmap, stream, log);
317 if (!err.isEmpty()) {
318 return err;
319 }
320
321 SkMatrix inverse;
322 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
323 return "Cannot upright --matrix.";
324 }
325 SkMatrix upright = SkMatrix::I();
326 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
327 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
328 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
329 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
330
331 SkBitmap uprighted;
332 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
333 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
334
335 SkCanvas canvas(uprighted);
336 canvas.concat(upright);
337 SkPaint paint;
338 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
339 canvas.drawBitmap(*bitmap, 0, 0, &paint);
340
341 *bitmap = uprighted;
342 bitmap->lockPixels();
343 return "";
344}
345
mtklein748ca3b2015-01-15 10:56:12 -0800346/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
347
mtklein7edca212015-01-21 13:18:51 -0800348ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
mtklein748ca3b2015-01-15 10:56:12 -0800349
mtkleinb9eb4ac2015-02-02 18:26:03 -0800350Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein7edca212015-01-21 13:18:51 -0800351 // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
mtklein748ca3b2015-01-15 10:56:12 -0800352 struct ProxySrc : public Src {
353 const Src& fSrc;
mtklein7edca212015-01-21 13:18:51 -0800354 ProxySrc(const Src& src) : fSrc(src) {}
mtklein748ca3b2015-01-15 10:56:12 -0800355
356 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
357 SkISize size = this->size();
mtklein748ca3b2015-01-15 10:56:12 -0800358 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
359 SkGPipeWriter pipe;
mtklein7edca212015-01-21 13:18:51 -0800360 const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
361 return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
mtklein748ca3b2015-01-15 10:56:12 -0800362 }
363 SkISize size() const SK_OVERRIDE { return fSrc.size(); }
364 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
mtklein7edca212015-01-21 13:18:51 -0800365 } proxy(src);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800366 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800367}
368
369/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
370
371ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
372
mtkleinb9eb4ac2015-02-02 18:26:03 -0800373Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log)
374 const {
mtklein748ca3b2015-01-15 10:56:12 -0800375 // Record our Src into a picture.
376 SkSize size;
377 size = src.size();
378 SkPictureRecorder recorder;
379 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
380 if (!err.isEmpty()) {
381 return err;
382 }
383 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
384
385 // Serialize it and then deserialize it.
386 SkDynamicMemoryWStream wStream;
387 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -0800388 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
halcanary44906c62015-02-23 10:43:48 -0800389 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream));
mtklein748ca3b2015-01-15 10:56:12 -0800390
391 // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
392 struct ProxySrc : public Src {
393 const SkPicture* fPic;
394 const SkISize fSize;
395 ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {}
396
397 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
398 canvas->drawPicture(fPic);
399 return "";
400 }
401 SkISize size() const SK_OVERRIDE { return fSize; }
402 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
403 } proxy(deserialized, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800404 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800405}
406
407/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
408
409ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
410 : fW(w)
411 , fH(h)
412 , fFactory(factory)
413 , fSink(sink) {}
414
mtkleinb9eb4ac2015-02-02 18:26:03 -0800415Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800416 // Record our Src into a picture.
417 SkSize size;
418 size = src.size();
419 SkPictureRecorder recorder;
420 Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get()));
421 if (!err.isEmpty()) {
422 return err;
423 }
424 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
425
426 // Turn that picture into a Src that draws into our Sink via tiles + MPD.
427 struct ProxySrc : public Src {
428 const int fW, fH;
429 const SkPicture* fPic;
430 const SkISize fSize;
431 ProxySrc(int w, int h, const SkPicture* pic, SkISize size)
432 : fW(w), fH(h), fPic(pic), fSize(size) {}
433
434 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
435 const int xTiles = (fSize.width() + fW - 1) / fW,
436 yTiles = (fSize.height() + fH - 1) / fH;
437 SkMultiPictureDraw mpd(xTiles*yTiles);
438 SkTDArray<SkSurface*> surfaces;
439 surfaces.setReserve(xTiles*yTiles);
440
441 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
442 for (int j = 0; j < yTiles; j++) {
443 for (int i = 0; i < xTiles; i++) {
444 // This lets our ultimate Sink determine the best kind of surface.
445 // E.g., if it's a GpuSink, the surfaces and images are textures.
446 SkSurface* s = canvas->newSurface(info);
447 if (!s) {
448 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
449 }
450 surfaces.push(s);
451 SkCanvas* c = s->getCanvas();
452 c->translate(SkIntToScalar(-i * fW),
453 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
454 mpd.add(c, fPic);
455 }
456 }
457 mpd.draw();
458 for (int j = 0; j < yTiles; j++) {
459 for (int i = 0; i < xTiles; i++) {
460 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
461 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
462 }
463 }
464 surfaces.unrefAll();
465 return "";
466 }
467 SkISize size() const SK_OVERRIDE { return fSize; }
468 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
469 } proxy(fW, fH, pic, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800470 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800471}
472
473} // namespace DM