blob: baf1871ba3520b405efea00198745a0d3d807787 [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"
6#include "SkOSFile.h"
7#include "SkPictureRecorder.h"
8#include "SkRandom.h"
mtklein8a4527e2015-01-31 20:00:58 -08009#include "SkSVGDevice.h"
scroggoa1193e42015-01-21 12:09:53 -080010#include "SkStream.h"
mtklein748ca3b2015-01-15 10:56:12 -080011
12namespace DM {
13
mtklein748ca3b2015-01-15 10:56:12 -080014GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
15
16Error GMSrc::draw(SkCanvas* canvas) const {
17 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
18 canvas->concat(gm->getInitialTransform());
19 gm->draw(canvas);
20 return "";
21}
22
23SkISize GMSrc::size() const {
24 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
25 return gm->getISize();
26}
27
28Name GMSrc::name() const {
29 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
30 return gm->getName();
31}
32
33/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
34
mtkleinedc93bc2015-01-30 13:22:23 -080035ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
mtklein748ca3b2015-01-15 10:56:12 -080036
37Error ImageSrc::draw(SkCanvas* canvas) const {
mtklein75d98fd2015-01-18 07:05:01 -080038 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080039 if (!encoded) {
40 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
41 }
mtkleinedc93bc2015-01-30 13:22:23 -080042 const SkColorType dstColorType = canvas->imageInfo().colorType();
43 if (fDivisor == 0) {
mtklein748ca3b2015-01-15 10:56:12 -080044 // Decode the full image.
45 SkBitmap bitmap;
mtkleinedc93bc2015-01-30 13:22:23 -080046 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
47 dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
mtklein748ca3b2015-01-15 10:56:12 -080048 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
49 }
mtklein75d98fd2015-01-18 07:05:01 -080050 encoded.reset((SkData*)NULL); // Might as well drop this when we're done with it.
mtklein748ca3b2015-01-15 10:56:12 -080051 canvas->drawBitmap(bitmap, 0,0);
52 return "";
53 }
mtkleinedc93bc2015-01-30 13:22:23 -080054 // Decode subsets. This is a little involved.
scroggoa1193e42015-01-21 12:09:53 -080055 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
56 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
mtklein748ca3b2015-01-15 10:56:12 -080057 if (!decoder) {
58 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
59 }
scroggoa1193e42015-01-21 12:09:53 -080060 stream->rewind();
mtklein748ca3b2015-01-15 10:56:12 -080061 int w,h;
scroggoa1193e42015-01-21 12:09:53 -080062 if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) {
mtklein748ca3b2015-01-15 10:56:12 -080063 return ""; // Not an error. Subset decoding is not always supported.
64 }
mtkleinedc93bc2015-01-30 13:22:23 -080065
66 // Divide the image into subsets that cover the entire image.
67 if (fDivisor > w || fDivisor > h) {
68 return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)",
69 fDivisor, fPath.c_str(), w, h);
70 }
71 const int subsetWidth = w / fDivisor,
72 subsetHeight = h / fDivisor;
73 for (int y = 0; y < h; y += subsetHeight) {
74 for (int x = 0; x < w; x += subsetWidth) {
75 SkBitmap subset;
76 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
77 if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
78 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
79 x, y, x+subsetWidth, y+subsetHeight);
80 }
81 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
mtklein748ca3b2015-01-15 10:56:12 -080082 }
mtklein748ca3b2015-01-15 10:56:12 -080083 }
84 return "";
85}
86
87SkISize ImageSrc::size() const {
mtklein75d98fd2015-01-18 07:05:01 -080088 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
mtklein748ca3b2015-01-15 10:56:12 -080089 SkBitmap bitmap;
90 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
91 encoded->size(),
92 &bitmap,
93 kUnknown_SkColorType,
94 SkImageDecoder::kDecodeBounds_Mode)) {
95 return SkISize::Make(0,0);
96 }
97 return bitmap.dimensions();
98}
99
mtklein9264a952015-01-20 10:11:53 -0800100Name ImageSrc::name() const {
mtkleinedc93bc2015-01-30 13:22:23 -0800101 return SkOSPath::Basename(fPath.c_str());
mtklein9264a952015-01-20 10:11:53 -0800102}
mtklein748ca3b2015-01-15 10:56:12 -0800103
104/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
105
mtkleinf4ba3212015-01-28 15:32:24 -0800106static const SkRect kSKPViewport = {0,0, 1000,1000};
107
mtklein8d17a132015-01-30 11:42:31 -0800108SKPSrc::SKPSrc(Path path) : fPath(path) {}
mtklein748ca3b2015-01-15 10:56:12 -0800109
110Error SKPSrc::draw(SkCanvas* canvas) const {
scroggoa1193e42015-01-21 12:09:53 -0800111 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
mtklein75d98fd2015-01-18 07:05:01 -0800112 if (!stream) {
mtklein748ca3b2015-01-15 10:56:12 -0800113 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
114 }
mtklein75d98fd2015-01-18 07:05:01 -0800115 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream));
116 if (!pic) {
117 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
118 }
119 stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it.
mtkleinf4ba3212015-01-28 15:32:24 -0800120 canvas->clipRect(kSKPViewport);
mtklein748ca3b2015-01-15 10:56:12 -0800121 canvas->drawPicture(pic);
122 return "";
123}
124
125SkISize SKPSrc::size() const {
mtkleinf4ba3212015-01-28 15:32:24 -0800126 // This may be unnecessarily large.
127 return kSKPViewport.roundOut().size();
mtklein748ca3b2015-01-15 10:56:12 -0800128}
129
130Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
131
132/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
133
mtklein82d28432015-01-15 12:46:02 -0800134GPUSink::GPUSink(GrContextFactory::GLContextType ct,
135 GrGLStandard api,
136 int samples,
137 bool dfText,
138 bool threaded)
mtklein748ca3b2015-01-15 10:56:12 -0800139 : fContextType(ct)
140 , fGpuAPI(api)
141 , fSampleCount(samples)
mtklein82d28432015-01-15 12:46:02 -0800142 , fUseDFText(dfText)
143 , fThreaded(threaded) {}
mtklein748ca3b2015-01-15 10:56:12 -0800144
145int GPUSink::enclave() const {
mtklein55e88b22015-01-21 15:50:13 -0800146 return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
mtklein748ca3b2015-01-15 10:56:12 -0800147}
148
mtklein748ca3b2015-01-15 10:56:12 -0800149Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*) const {
mtklein55e88b22015-01-21 15:50:13 -0800150 GrContextFactory factory;
mtkleinf4ba3212015-01-28 15:32:24 -0800151 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800152 const SkImageInfo info =
153 SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
154 SkAutoTUnref<SkSurface> surface(
mtklein55e88b22015-01-21 15:50:13 -0800155 NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText));
mtklein748ca3b2015-01-15 10:56:12 -0800156 if (!surface) {
157 return "Could not create a surface.";
158 }
159 SkCanvas* canvas = surface->getCanvas();
160 Error err = src.draw(canvas);
161 if (!err.isEmpty()) {
162 return err;
163 }
164 canvas->flush();
165 dst->allocPixels(info);
166 canvas->readPixels(dst, 0,0);
mtklein55e88b22015-01-21 15:50:13 -0800167 if (FLAGS_abandonGpuContext) {
168 factory.abandonContexts();
169 }
mtklein748ca3b2015-01-15 10:56:12 -0800170 return "";
171}
172
173/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
174
175PDFSink::PDFSink() {}
176
177Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst) const {
halcanaryfd4a9932015-01-28 11:45:58 -0800178 // Print the given DM:Src to a PDF, breaking on 8.5x11 pages.
mtklein748ca3b2015-01-15 10:56:12 -0800179 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
mtklein748ca3b2015-01-15 10:56:12 -0800180
halcanaryfd4a9932015-01-28 11:45:58 -0800181 int width = src.size().width(),
182 height = src.size().height();
183
184 const int kLetterWidth = 612, // 8.5 * 72
185 kLetterHeight = 792; // 11 * 72
186 const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
187 SkIntToScalar(kLetterHeight));
188
189 int xPages = ((width - 1) / kLetterWidth) + 1;
190 int yPages = ((height - 1) / kLetterHeight) + 1;
191
192 for (int y = 0; y < yPages; ++y) {
193 for (int x = 0; x < xPages; ++x) {
194 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
195 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
196 SkCanvas* canvas =
197 doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
198 canvas->clipRect(letter);
199 canvas->translate(-letter.width() * x, -letter.height() * y);
200 Error err = src.draw(canvas);
201 if (!err.isEmpty()) {
202 return err;
203 }
204 doc->endPage();
205 }
mtklein748ca3b2015-01-15 10:56:12 -0800206 }
mtklein748ca3b2015-01-15 10:56:12 -0800207 doc->close();
halcanaryfd4a9932015-01-28 11:45:58 -0800208 dst->flush();
mtklein748ca3b2015-01-15 10:56:12 -0800209 return "";
210}
211
212/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
213
mtklein9c3f17d2015-01-28 11:35:18 -0800214SKPSink::SKPSink() {}
215
216Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst) const {
217 SkSize size;
218 size = src.size();
219 SkPictureRecorder recorder;
220 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
221 if (!err.isEmpty()) {
222 return err;
223 }
224 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
225 pic->serialize(dst);
226 return "";
227}
228
229/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
230
mtklein8a4527e2015-01-31 20:00:58 -0800231SVGSink::SVGSink() {}
232
233Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst) const {
234 SkAutoTUnref<SkBaseDevice> device(SkSVGDevice::Create(src.size(), dst));
235 SkCanvas canvas(device);
236 return src.draw(&canvas);
237}
238
239/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
240
mtklein748ca3b2015-01-15 10:56:12 -0800241RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
242
243Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*) const {
mtkleinf4ba3212015-01-28 15:32:24 -0800244 const SkISize size = src.size();
mtklein748ca3b2015-01-15 10:56:12 -0800245 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
246 SkAlphaType alphaType = kPremul_SkAlphaType;
247 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
248
249 dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
250 dst->eraseColor(SK_ColorTRANSPARENT);
251 SkCanvas canvas(*dst);
252 return src.draw(&canvas);
253}
254
255/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
256
257ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
258
259Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream) const {
260 // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
261 struct ProxySrc : public Src {
262 const Src& fSrc;
263 SkMatrix fMatrix;
264 ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {}
265
266 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
267 canvas->concat(fMatrix);
268 return fSrc.draw(canvas);
269 }
270 SkISize size() const SK_OVERRIDE { return fSrc.size(); }
271 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
272 } proxy(src, fMatrix);
273 return fSink->draw(proxy, bitmap, stream);
274}
275
276/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
277
mtklein7edca212015-01-21 13:18:51 -0800278ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
mtklein748ca3b2015-01-15 10:56:12 -0800279
280Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream) const {
mtklein7edca212015-01-21 13:18:51 -0800281 // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
mtklein748ca3b2015-01-15 10:56:12 -0800282 struct ProxySrc : public Src {
283 const Src& fSrc;
mtklein7edca212015-01-21 13:18:51 -0800284 ProxySrc(const Src& src) : fSrc(src) {}
mtklein748ca3b2015-01-15 10:56:12 -0800285
286 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
287 SkISize size = this->size();
mtklein748ca3b2015-01-15 10:56:12 -0800288 PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
289 SkGPipeWriter pipe;
mtklein7edca212015-01-21 13:18:51 -0800290 const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
291 return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
mtklein748ca3b2015-01-15 10:56:12 -0800292 }
293 SkISize size() const SK_OVERRIDE { return fSrc.size(); }
294 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
mtklein7edca212015-01-21 13:18:51 -0800295 } proxy(src);
mtklein748ca3b2015-01-15 10:56:12 -0800296 return fSink->draw(proxy, bitmap, stream);
297}
298
299/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
300
301ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
302
303Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream) const {
304 // Record our Src into a picture.
305 SkSize size;
306 size = src.size();
307 SkPictureRecorder recorder;
308 Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
309 if (!err.isEmpty()) {
310 return err;
311 }
312 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
313
314 // Serialize it and then deserialize it.
315 SkDynamicMemoryWStream wStream;
316 pic->serialize(&wStream);
scroggoa1193e42015-01-21 12:09:53 -0800317 SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
mtklein748ca3b2015-01-15 10:56:12 -0800318 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream));
319
320 // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
321 struct ProxySrc : public Src {
322 const SkPicture* fPic;
323 const SkISize fSize;
324 ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {}
325
326 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
327 canvas->drawPicture(fPic);
328 return "";
329 }
330 SkISize size() const SK_OVERRIDE { return fSize; }
331 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
332 } proxy(deserialized, src.size());
333 return fSink->draw(proxy, bitmap, stream);
334}
335
336/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
337
338ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
339 : fW(w)
340 , fH(h)
341 , fFactory(factory)
342 , fSink(sink) {}
343
344Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream) const {
345 // Record our Src into a picture.
346 SkSize size;
347 size = src.size();
348 SkPictureRecorder recorder;
349 Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get()));
350 if (!err.isEmpty()) {
351 return err;
352 }
353 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
354
355 // Turn that picture into a Src that draws into our Sink via tiles + MPD.
356 struct ProxySrc : public Src {
357 const int fW, fH;
358 const SkPicture* fPic;
359 const SkISize fSize;
360 ProxySrc(int w, int h, const SkPicture* pic, SkISize size)
361 : fW(w), fH(h), fPic(pic), fSize(size) {}
362
363 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
364 const int xTiles = (fSize.width() + fW - 1) / fW,
365 yTiles = (fSize.height() + fH - 1) / fH;
366 SkMultiPictureDraw mpd(xTiles*yTiles);
367 SkTDArray<SkSurface*> surfaces;
368 surfaces.setReserve(xTiles*yTiles);
369
370 SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
371 for (int j = 0; j < yTiles; j++) {
372 for (int i = 0; i < xTiles; i++) {
373 // This lets our ultimate Sink determine the best kind of surface.
374 // E.g., if it's a GpuSink, the surfaces and images are textures.
375 SkSurface* s = canvas->newSurface(info);
376 if (!s) {
377 s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
378 }
379 surfaces.push(s);
380 SkCanvas* c = s->getCanvas();
381 c->translate(SkIntToScalar(-i * fW),
382 SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
383 mpd.add(c, fPic);
384 }
385 }
386 mpd.draw();
387 for (int j = 0; j < yTiles; j++) {
388 for (int i = 0; i < xTiles; i++) {
389 SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
390 canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
391 }
392 }
393 surfaces.unrefAll();
394 return "";
395 }
396 SkISize size() const SK_OVERRIDE { return fSize; }
397 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this.
398 } proxy(fW, fH, pic, src.size());
399 return fSink->draw(proxy, bitmap, stream);
400}
401
402} // namespace DM