blob: 2060ce2d6833561fdece352aca6cf95d1fe8f2b6 [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
halcanary47ef4d52015-03-03 09:13:09 -0800226static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
227 // Print the given DM:Src to a document, breaking on 8.5x11 pages.
228 SkASSERT(doc);
halcanaryfd4a9932015-01-28 11:45:58 -0800229 int width = src.size().width(),
230 height = src.size().height();
231
232 const int kLetterWidth = 612, // 8.5 * 72
233 kLetterHeight = 792; // 11 * 72
234 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
235 SkIntToScalar(kLetterHeight));
236
237 int xPages = ((width - 1) / kLetterWidth) + 1;
238 int yPages = ((height - 1) / kLetterHeight) + 1;
239
240 for (int y = 0; y < yPages; ++y) {
241 for (int x = 0; x < xPages; ++x) {
242 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
243 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
244 SkCanvas* canvas =
245 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
246 canvas->clipRect(letter);
247 canvas->translate(-letter.width() * x, -letter.height() * y);
248 Error err = src.draw(canvas);
249 if (!err.isEmpty()) {
250 return err;
251 }
252 doc->endPage();
253 }
mtklein748ca3b2015-01-15 10:56:12 -0800254 }
mtklein748ca3b2015-01-15 10:56:12 -0800255 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -0800256 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800257 return "";
258}
259
halcanary47ef4d52015-03-03 09:13:09 -0800260PDFSink::PDFSink() {}
261
262Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
263 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
264 if (!doc) {
265 return "SkDocument::CreatePDF() returned NULL";
266 }
267 return draw_skdocument(src, doc.get(), dst);
268}
269
270/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
271
272XPSSink::XPSSink() {}
273
274Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
275 SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
276 if (!doc) {
277 return "SkDocument::CreateXPS() returned NULL";
278 }
279 return draw_skdocument(src, doc.get(), dst);
280}
mtklein748ca3b2015-01-15 10:56:12 -0800281/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
282
mtklein9c3f17d2015-01-28 11:35:18 -0800283SKPSink::SKPSink() {}
284
mtkleinb9eb4ac2015-02-02 18:26:03 -0800285Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
mtklein9c3f17d2015-01-28 11:35:18 -0800286 SkSize size;
287 size = src.size();
288 SkPictureRecorder recorder;
289 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
290 if (!err.isEmpty()) {
291 return err;
292 }
293 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
294 pic->serialize(dst);
295 return "";
296}
297
298/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
299
mtklein8a4527e2015-01-31 20:00:58 -0800300SVGSink::SVGSink() {}
301
mtkleinb9eb4ac2015-02-02 18:26:03 -0800302Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
fmalita2aafe6f2015-02-06 12:51:10 -0800303 SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst)));
304 SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
305 SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
306 xmlWriter));
307 return src.draw(canvas);
mtklein8a4527e2015-01-31 20:00:58 -0800308}
309
310/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
311
mtklein748ca3b2015-01-15 10:56:12 -0800312RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
313
mtkleinb9eb4ac2015-02-02 18:26:03 -0800314Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800315 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800316 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
317 SkAlphaType alphaType = kPremul_SkAlphaType;
318 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
319
320 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
321 dst->eraseColor(SK_ColorTRANSPARENT);
322 SkCanvas canvas(*dst);
323 return src.draw(&canvas);
324}
325
326/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
327
mtkleind603b222015-02-17 11:13:33 -0800328static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
329 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
330 matrix->mapRect(&bounds);
331 matrix->postTranslate(-bounds.x(), -bounds.y());
332 return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
333}
334
mtklein748ca3b2015-01-15 10:56:12 -0800335ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
336
mtkleinb9eb4ac2015-02-02 18:26:03 -0800337Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800338 // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
339 struct ProxySrc : public Src {
mtkleind603b222015-02-17 11:13:33 -0800340 const Src& fSrc;
341 SkMatrix fMatrix;
342 SkISize fSize;
343
344 ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {
345 fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height());
346 }
mtklein748ca3b2015-01-15 10:56:12 -0800347
348 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
349 canvas->concat(fMatrix);
350 return fSrc.draw(canvas);
351 }
mtkleind603b222015-02-17 11:13:33 -0800352 SkISize size() const SK_OVERRIDE { return fSize; }
mtklein748ca3b2015-01-15 10:56:12 -0800353 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
354 } proxy(src, fMatrix);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800355 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800356}
357
mtkleind603b222015-02-17 11:13:33 -0800358// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
359// This should be pixel-preserving.
360ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
361
362Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
363 Error err = fSink->draw(src, bitmap, stream, log);
364 if (!err.isEmpty()) {
365 return err;
366 }
367
368 SkMatrix inverse;
369 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
370 return "Cannot upright --matrix.";
371 }
372 SkMatrix upright = SkMatrix::I();
373 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
374 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
375 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
376 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
377
378 SkBitmap uprighted;
379 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
380 uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
381
382 SkCanvas canvas(uprighted);
383 canvas.concat(upright);
384 SkPaint paint;
385 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
386 canvas.drawBitmap(*bitmap, 0, 0, &paint);
387
388 *bitmap = uprighted;
389 bitmap->lockPixels();
390 return "";
391}
392
mtklein748ca3b2015-01-15 10:56:12 -0800393/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
394
mtklein7edca212015-01-21 13:18:51 -0800395ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
mtklein748ca3b2015-01-15 10:56:12 -0800396
mtkleinb9eb4ac2015-02-02 18:26:03 -0800397Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein7edca212015-01-21 13:18:51 -0800398 // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
mtklein748ca3b2015-01-15 10:56:12 -0800399 struct ProxySrc : public Src {
400 const Src& fSrc;
mtklein7edca212015-01-21 13:18:51 -0800401 ProxySrc(const Src& src) : fSrc(src) {}
mtklein748ca3b2015-01-15 10:56:12 -0800402
403 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
404 SkISize size = this->size();
mtklein748ca3b2015-01-15 10:56:12 -0800405 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
406 SkGPipeWriter pipe;
mtklein7edca212015-01-21 13:18:51 -0800407 const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
408 return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
mtklein748ca3b2015-01-15 10:56:12 -0800409 }
410 SkISize size() const SK_OVERRIDE { return fSrc.size(); }
411 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
mtklein7edca212015-01-21 13:18:51 -0800412 } proxy(src);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800413 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800414}
415
416/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
417
418ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
419
mtkleinb9eb4ac2015-02-02 18:26:03 -0800420Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log)
421 const {
mtklein748ca3b2015-01-15 10:56:12 -0800422 // Record our Src into a picture.
423 SkSize size;
424 size = src.size();
425 SkPictureRecorder recorder;
426 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
427 if (!err.isEmpty()) {
428 return err;
429 }
430 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
431
432 // Serialize it and then deserialize it.
433 SkDynamicMemoryWStream wStream;
434 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -0800435 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
halcanary44906c62015-02-23 10:43:48 -0800436 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream));
mtklein748ca3b2015-01-15 10:56:12 -0800437
438 // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
439 struct ProxySrc : public Src {
440 const SkPicture* fPic;
441 const SkISize fSize;
442 ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {}
443
444 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
445 canvas->drawPicture(fPic);
446 return "";
447 }
448 SkISize size() const SK_OVERRIDE { return fSize; }
449 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
450 } proxy(deserialized, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800451 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800452}
453
454/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
455
456ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
457 : fW(w)
458 , fH(h)
459 , fFactory(factory)
460 , fSink(sink) {}
461
mtkleinb9eb4ac2015-02-02 18:26:03 -0800462Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
mtklein748ca3b2015-01-15 10:56:12 -0800463 // Record our Src into a picture.
464 SkSize size;
465 size = src.size();
466 SkPictureRecorder recorder;
467 Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get()));
468 if (!err.isEmpty()) {
469 return err;
470 }
471 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
472
473 // Turn that picture into a Src that draws into our Sink via tiles + MPD.
474 struct ProxySrc : public Src {
475 const int fW, fH;
476 const SkPicture* fPic;
477 const SkISize fSize;
478 ProxySrc(int w, int h, const SkPicture* pic, SkISize size)
479 : fW(w), fH(h), fPic(pic), fSize(size) {}
480
481 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
482 const int xTiles = (fSize.width() + fW - 1) / fW,
483 yTiles = (fSize.height() + fH - 1) / fH;
484 SkMultiPictureDraw mpd(xTiles*yTiles);
485 SkTDArray<SkSurface*> surfaces;
486 surfaces.setReserve(xTiles*yTiles);
487
488 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
489 for (int j = 0; j < yTiles; j++) {
490 for (int i = 0; i < xTiles; i++) {
491 // This lets our ultimate Sink determine the best kind of surface.
492 // E.g., if it's a GpuSink, the surfaces and images are textures.
493 SkSurface* s = canvas->newSurface(info);
494 if (!s) {
495 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
496 }
497 surfaces.push(s);
498 SkCanvas* c = s->getCanvas();
499 c->translate(SkIntToScalar(-i * fW),
500 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
501 mpd.add(c, fPic);
502 }
503 }
504 mpd.draw();
505 for (int j = 0; j < yTiles; j++) {
506 for (int i = 0; i < xTiles; i++) {
507 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
508 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
509 }
510 }
511 surfaces.unrefAll();
512 return "";
513 }
514 SkISize size() const SK_OVERRIDE { return fSize; }
515 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
516 } proxy(fW, fH, pic, src.size());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800517 return fSink->draw(proxy, bitmap, stream, log);
mtklein748ca3b2015-01-15 10:56:12 -0800518}
519
520} // namespace DM