blob: b44f1d78180e94e9d9f5505789024e5db42c4007 [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"
5#include "SkMultiPictureDraw.h"
mtkleinad66f9b2015-02-13 15:11:10 -08006#include "SkNullCanvas.h"
mtklein748ca3b2015-01-15 10:56:12 -08007#include "SkOSFile.h"
8#include "SkPictureRecorder.h"
9#include "SkRandom.h"
fmalita2aafe6f2015-02-06 12:51:10 -080010#include "SkSVGCanvas.h"
scroggoa1193e42015-01-21 12:09:53 -080011#include "SkStream.h"
fmalita2aafe6f2015-02-06 12:51:10 -080012#include "SkXMLWriter.h"
mtklein748ca3b2015-01-15 10:56:12 -080013
14namespace DM {
15
mtklein748ca3b2015-01-15 10:56:12 -080016GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
17
18Error GMSrc::draw(SkCanvas* canvas) const {
19 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
20 canvas->concat(gm->getInitialTransform());
21 gm->draw(canvas);
22 return "";
23}
24
25SkISize GMSrc::size() const {
26 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
27 return gm->getISize();
28}
29
30Name GMSrc::name() const {
31 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
32 return gm->getName();
33}
34
35/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
36
mtkleinedc93bc2015-01-30 13:22:23 -080037ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
mtklein748ca3b2015-01-15 10:56:12 -080038
39Error ImageSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -080040 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080041 if (!encoded) {
42 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
43 }
mtkleinedc93bc2015-01-30 13:22:23 -080044 const SkColorType dstColorType = canvas->imageInfo().colorType();
45 if (fDivisor == 0) {
mtklein748ca3b2015-01-15 10:56:12 -080046 // Decode the full image.
47 SkBitmap bitmap;
mtkleinedc93bc2015-01-30 13:22:23 -080048 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
49 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
mtklein748ca3b2015-01-15 10:56:12 -080050 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
51 }
mtklein75d98fd2015-01-18 07:05:01 -080052 encoded.reset((SkData*)NULL); // Might as well drop this when we're done with it.
mtklein748ca3b2015-01-15 10:56:12 -080053 canvas->drawBitmap(bitmap, 0,0);
54 return "";
55 }
mtkleinedc93bc2015-01-30 13:22:23 -080056 // Decode subsets. This is a little involved.
scroggoa1193e42015-01-21 12:09:53 -080057 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
58 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
mtklein748ca3b2015-01-15 10:56:12 -080059 if (!decoder) {
60 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
61 }
scroggoa1193e42015-01-21 12:09:53 -080062 stream->rewind();
mtklein748ca3b2015-01-15 10:56:12 -080063 int w,h;
scroggoa1193e42015-01-21 12:09:53 -080064 if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) {
mtklein748ca3b2015-01-15 10:56:12 -080065 return ""; // Not an error. Subset decoding is not always supported.
66 }
mtkleinedc93bc2015-01-30 13:22:23 -080067
68 // Divide the image into subsets that cover the entire image.
69 if (fDivisor > w || fDivisor > h) {
70 return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)",
71 fDivisor, fPath.c_str(), w, h);
72 }
73 const int subsetWidth = w / fDivisor,
74 subsetHeight = h / fDivisor;
75 for (int y = 0; y < h; y += subsetHeight) {
76 for (int x = 0; x < w; x += subsetWidth) {
77 SkBitmap subset;
78 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
79 if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
80 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
81 x, y, x+subsetWidth, y+subsetHeight);
82 }
83 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
mtklein748ca3b2015-01-15 10:56:12 -080084 }
mtklein748ca3b2015-01-15 10:56:12 -080085 }
86 return "";
87}
88
89SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -080090 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080091 SkBitmap bitmap;
92 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
93 encoded->size(),
94 &bitmap,
95 kUnknown_SkColorType,
96 SkImageDecoder::kDecodeBounds_Mode)) {
97 return SkISize::Make(0,0);
98 }
99 return bitmap.dimensions();
100}
101
mtklein9264a952015-01-20 10:11:53 -0800102Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800103 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800104}
mtklein748ca3b2015-01-15 10:56:12 -0800105
106/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
107
mtkleinf4ba3212015-01-28 15:32:24 -0800108static const SkRect kSKPViewport = {0,0, 1000,1000};
109
mtklein8d17a132015-01-30 11:42:31 -0800110SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800111
112Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800113 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800114 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800115 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
116 }
mtklein75d98fd2015-01-18 07:05:01 -0800117 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream));
118 if (!pic) {
119 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
120 }
121 stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it.
mtkleinf4ba3212015-01-28 15:32:24 -0800122 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800123 canvas->drawPicture(pic);
124 return "";
125}
126
127SkISize SKPSrc::size() const {
mtkleinf4ba3212015-01-28 15:32:24 -0800128 // This may be unnecessarily large.
129 return kSKPViewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800130}
131
132Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
133
134/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
135
mtkleinad66f9b2015-02-13 15:11:10 -0800136Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
137 SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
138 return src.draw(canvas);
139}
140
141/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
142
mtkleinb9eb4ac2015-02-02 18:26:03 -0800143DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
144
mtklein82d28432015-01-15 12:46:02 -0800145GPUSink::GPUSink(GrContextFactory::GLContextType ct,
146 GrGLStandard api,
147 int samples,
148 bool dfText,
149 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800150 : fContextType(ct)
151 , fGpuAPI(api)
152 , fSampleCount(samples)
mtklein82d28432015-01-15 12:46:02 -0800153 , fUseDFText(dfText)
154 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800155
156int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800157 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800158}
159
mtkleinb9eb4ac2015-02-02 18:26:03 -0800160Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
mtklein55e88b22015-01-21 15:50:13 -0800161 GrContextFactory factory;
mtkleinf4ba3212015-01-28 15:32:24 -0800162 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800163 const SkImageInfo info =
164 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
165 SkAutoTUnref<SkSurface> surface(
mtklein55e88b22015-01-21 15:50:13 -0800166 NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText));
mtklein748ca3b2015-01-15 10:56:12 -0800167 if (!surface) {
168 return "Could not create a surface.";
169 }
170 SkCanvas* canvas = surface->getCanvas();
171 Error err = src.draw(canvas);
172 if (!err.isEmpty()) {
173 return err;
174 }
175 canvas->flush();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800176 if (FLAGS_gpuStats) {
177 canvas->getGrContext()->dumpCacheStats(log);
178 canvas->getGrContext()->dumpGpuStats(log);
179 }
mtklein748ca3b2015-01-15 10:56:12 -0800180 dst->allocPixels(info);
181 canvas->readPixels(dst, 0,0);
mtklein55e88b22015-01-21 15:50:13 -0800182 if (FLAGS_abandonGpuContext) {
183 factory.abandonContexts();
184 }
mtklein748ca3b2015-01-15 10:56:12 -0800185 return "";
186}
187
188/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
189
190PDFSink::PDFSink() {}
191
mtkleinb9eb4ac2015-02-02 18:26:03 -0800192Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
halcanaryfd4a9932015-01-28 11:45:58 -0800193 // Print the given DM:Src to a PDF, breaking on 8.5x11 pages.
mtklein748ca3b2015-01-15 10:56:12 -0800194 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
mtklein748ca3b2015-01-15 10:56:12 -0800195
halcanaryfd4a9932015-01-28 11:45:58 -0800196 int width = src.size().width(),
197 height = src.size().height();
198
199 const int kLetterWidth = 612, // 8.5 * 72
200 kLetterHeight = 792; // 11 * 72
201 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
202 SkIntToScalar(kLetterHeight));
203
204 int xPages = ((width - 1) / kLetterWidth) + 1;
205 int yPages = ((height - 1) / kLetterHeight) + 1;
206
207 for (int y = 0; y < yPages; ++y) {
208 for (int x = 0; x < xPages; ++x) {
209 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
210 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
211 SkCanvas* canvas =
212 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
213 canvas->clipRect(letter);
214 canvas->translate(-letter.width() * x, -letter.height() * y);
215 Error err = src.draw(canvas);
216 if (!err.isEmpty()) {
217 return err;
218 }
219 doc->endPage();
220 }
mtklein748ca3b2015-01-15 10:56:12 -0800221 }
mtklein748ca3b2015-01-15 10:56:12 -0800222 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -0800223 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800224 return "";
225}
226
227/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
228
mtklein9c3f17d2015-01-28 11:35:18 -0800229SKPSink::SKPSink() {}
230
mtkleinb9eb4ac2015-02-02 18:26:03 -0800231Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800232 SkSize size;
233 size = src.size();
234 SkPictureRecorder recorder;
235 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
236 if (!err.isEmpty()) {
237 return err;
238 }
239 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
240 pic->serialize(dst);
241 return "";
242}
243
244/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
245
mtklein8a4527e2015-01-31 20:00:58 -0800246SVGSink::SVGSink() {}
247
mtkleinb9eb4ac2015-02-02 18:26:03 -0800248Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita2aafe6f2015-02-06 12:51:10 -0800249 SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst)));
250 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
251 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
252 xmlWriter));
253 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800254}
255
256/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
257
mtklein748ca3b2015-01-15 10:56:12 -0800258RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
259
mtkleinb9eb4ac2015-02-02 18:26:03 -0800260Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800261 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800262 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
263 SkAlphaType alphaType = kPremul_SkAlphaType;
264 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
265
266 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
267 dst->eraseColor(SK_ColorTRANSPARENT);
268 SkCanvas canvas(*dst);
269 return src.draw(&canvas);
270}
271
272/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
273
mtkleind603b222015-02-17 11:13:33 -0800274static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
275 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
276 matrix->mapRect(&bounds);
277 matrix->postTranslate(-bounds.x(), -bounds.y());
278 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
279}
280
mtklein748ca3b2015-01-15 10:56:12 -0800281ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
282
mtkleinb9eb4ac2015-02-02 18:26:03 -0800283Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800284 // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
285 struct ProxySrc : public Src {
mtkleind603b222015-02-17 11:13:33 -0800286 const Src& fSrc;
287 SkMatrix fMatrix;
288 SkISize fSize;
289
290 ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {
291 fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height());
292 }
mtklein748ca3b2015-01-15 10:56:12 -0800293
294 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
295 canvas->concat(fMatrix);
296 return fSrc.draw(canvas);
297 }
mtkleind603b222015-02-17 11:13:33 -0800298 SkISize size() const SK_OVERRIDE { return fSize; }
mtklein748ca3b2015-01-15 10:56:12 -0800299 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
300 } proxy(src, fMatrix);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800301 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800302}
303
mtkleind603b222015-02-17 11:13:33 -0800304// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
305// This should be pixel-preserving.
306ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
307
308Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
309 Error err = fSink->draw(src, bitmap, stream, log);
310 if (!err.isEmpty()) {
311 return err;
312 }
313
314 SkMatrix inverse;
315 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
316 return "Cannot upright --matrix.";
317 }
318 SkMatrix upright = SkMatrix::I();
319 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
320 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
321 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
322 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
323
324 SkBitmap uprighted;
325 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
326 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
327
328 SkCanvas canvas(uprighted);
329 canvas.concat(upright);
330 SkPaint paint;
331 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
332 canvas.drawBitmap(*bitmap, 0, 0, &paint);
333
334 *bitmap = uprighted;
335 bitmap->lockPixels();
336 return "";
337}
338
mtklein748ca3b2015-01-15 10:56:12 -0800339/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
340
mtklein7edca212015-01-21 13:18:51 -0800341ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
mtklein748ca3b2015-01-15 10:56:12 -0800342
mtkleinb9eb4ac2015-02-02 18:26:03 -0800343Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein7edca212015-01-21 13:18:51 -0800344 // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
mtklein748ca3b2015-01-15 10:56:12 -0800345 struct ProxySrc : public Src {
346 const Src& fSrc;
mtklein7edca212015-01-21 13:18:51 -0800347 ProxySrc(const Src& src) : fSrc(src) {}
mtklein748ca3b2015-01-15 10:56:12 -0800348
349 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
350 SkISize size = this->size();
mtklein748ca3b2015-01-15 10:56:12 -0800351 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
352 SkGPipeWriter pipe;
mtklein7edca212015-01-21 13:18:51 -0800353 const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
354 return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
mtklein748ca3b2015-01-15 10:56:12 -0800355 }
356 SkISize size() const SK_OVERRIDE { return fSrc.size(); }
357 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
mtklein7edca212015-01-21 13:18:51 -0800358 } proxy(src);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800359 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800360}
361
362/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
363
364ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
365
mtkleinb9eb4ac2015-02-02 18:26:03 -0800366Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log)
367 const {
mtklein748ca3b2015-01-15 10:56:12 -0800368 // Record our Src into a picture.
369 SkSize size;
370 size = src.size();
371 SkPictureRecorder recorder;
372 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
373 if (!err.isEmpty()) {
374 return err;
375 }
376 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
377
378 // Serialize it and then deserialize it.
379 SkDynamicMemoryWStream wStream;
380 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -0800381 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
mtklein748ca3b2015-01-15 10:56:12 -0800382 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream));
383
384 // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
385 struct ProxySrc : public Src {
386 const SkPicture* fPic;
387 const SkISize fSize;
388 ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {}
389
390 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
391 canvas->drawPicture(fPic);
392 return "";
393 }
394 SkISize size() const SK_OVERRIDE { return fSize; }
395 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
396 } proxy(deserialized, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800397 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800398}
399
400/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
401
402ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
403 : fW(w)
404 , fH(h)
405 , fFactory(factory)
406 , fSink(sink) {}
407
mtkleinb9eb4ac2015-02-02 18:26:03 -0800408Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800409 // Record our Src into a picture.
410 SkSize size;
411 size = src.size();
412 SkPictureRecorder recorder;
413 Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get()));
414 if (!err.isEmpty()) {
415 return err;
416 }
417 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
418
419 // Turn that picture into a Src that draws into our Sink via tiles + MPD.
420 struct ProxySrc : public Src {
421 const int fW, fH;
422 const SkPicture* fPic;
423 const SkISize fSize;
424 ProxySrc(int w, int h, const SkPicture* pic, SkISize size)
425 : fW(w), fH(h), fPic(pic), fSize(size) {}
426
427 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
428 const int xTiles = (fSize.width() + fW - 1) / fW,
429 yTiles = (fSize.height() + fH - 1) / fH;
430 SkMultiPictureDraw mpd(xTiles*yTiles);
431 SkTDArray<SkSurface*> surfaces;
432 surfaces.setReserve(xTiles*yTiles);
433
434 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
435 for (int j = 0; j < yTiles; j++) {
436 for (int i = 0; i < xTiles; i++) {
437 // This lets our ultimate Sink determine the best kind of surface.
438 // E.g., if it's a GpuSink, the surfaces and images are textures.
439 SkSurface* s = canvas->newSurface(info);
440 if (!s) {
441 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
442 }
443 surfaces.push(s);
444 SkCanvas* c = s->getCanvas();
445 c->translate(SkIntToScalar(-i * fW),
446 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
447 mpd.add(c, fPic);
448 }
449 }
450 mpd.draw();
451 for (int j = 0; j < yTiles; j++) {
452 for (int i = 0; i < xTiles; i++) {
453 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
454 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
455 }
456 }
457 surfaces.unrefAll();
458 return "";
459 }
460 SkISize size() const SK_OVERRIDE { return fSize; }
461 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
462 } proxy(fW, fH, pic, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800463 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800464}
465
466} // namespace DM