mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1 | #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" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 9 | #include "SkSVGCanvas.h" |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 10 | #include "SkStream.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 11 | #include "SkXMLWriter.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 12 | |
| 13 | namespace DM { |
| 14 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 15 | GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
| 16 | |
| 17 | Error GMSrc::draw(SkCanvas* canvas) const { |
| 18 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 19 | canvas->concat(gm->getInitialTransform()); |
| 20 | gm->draw(canvas); |
| 21 | return ""; |
| 22 | } |
| 23 | |
| 24 | SkISize GMSrc::size() const { |
| 25 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 26 | return gm->getISize(); |
| 27 | } |
| 28 | |
| 29 | Name GMSrc::name() const { |
| 30 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 31 | return gm->getName(); |
| 32 | } |
| 33 | |
| 34 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 35 | |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 36 | ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 37 | |
| 38 | Error ImageSrc::draw(SkCanvas* canvas) const { |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 39 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 40 | if (!encoded) { |
| 41 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 42 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 43 | const SkColorType dstColorType = canvas->imageInfo().colorType(); |
| 44 | if (fDivisor == 0) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 45 | // Decode the full image. |
| 46 | SkBitmap bitmap; |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 47 | if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap, |
| 48 | dstColorType, SkImageDecoder::kDecodePixels_Mode)) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 49 | return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
| 50 | } |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 51 | encoded.reset((SkData*)NULL); // Might as well drop this when we're done with it. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 52 | canvas->drawBitmap(bitmap, 0,0); |
| 53 | return ""; |
| 54 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 55 | // Decode subsets. This is a little involved. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 56 | SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
| 57 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 58 | if (!decoder) { |
| 59 | return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()); |
| 60 | } |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 61 | stream->rewind(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 62 | int w,h; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 63 | if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 64 | return ""; // Not an error. Subset decoding is not always supported. |
| 65 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 66 | |
| 67 | // Divide the image into subsets that cover the entire image. |
| 68 | if (fDivisor > w || fDivisor > h) { |
| 69 | return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)", |
| 70 | fDivisor, fPath.c_str(), w, h); |
| 71 | } |
| 72 | const int subsetWidth = w / fDivisor, |
| 73 | subsetHeight = h / fDivisor; |
| 74 | for (int y = 0; y < h; y += subsetHeight) { |
| 75 | for (int x = 0; x < w; x += subsetWidth) { |
| 76 | SkBitmap subset; |
| 77 | SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight); |
| 78 | if (!decoder->decodeSubset(&subset, rect, dstColorType)) { |
| 79 | return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).", |
| 80 | x, y, x+subsetWidth, y+subsetHeight); |
| 81 | } |
| 82 | canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 83 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 84 | } |
| 85 | return ""; |
| 86 | } |
| 87 | |
| 88 | SkISize ImageSrc::size() const { |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 89 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 90 | SkBitmap bitmap; |
| 91 | if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), |
| 92 | encoded->size(), |
| 93 | &bitmap, |
| 94 | kUnknown_SkColorType, |
| 95 | SkImageDecoder::kDecodeBounds_Mode)) { |
| 96 | return SkISize::Make(0,0); |
| 97 | } |
| 98 | return bitmap.dimensions(); |
| 99 | } |
| 100 | |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 101 | Name ImageSrc::name() const { |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 102 | return SkOSPath::Basename(fPath.c_str()); |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 103 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 104 | |
| 105 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 106 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 107 | static const SkRect kSKPViewport = {0,0, 1000,1000}; |
| 108 | |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 109 | SKPSrc::SKPSrc(Path path) : fPath(path) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 110 | |
| 111 | Error SKPSrc::draw(SkCanvas* canvas) const { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 112 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 113 | if (!stream) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 114 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 115 | } |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 116 | SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream)); |
| 117 | if (!pic) { |
| 118 | return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str()); |
| 119 | } |
| 120 | stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it. |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 121 | canvas->clipRect(kSKPViewport); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 122 | canvas->drawPicture(pic); |
| 123 | return ""; |
| 124 | } |
| 125 | |
| 126 | SkISize SKPSrc::size() const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 127 | // This may be unnecessarily large. |
| 128 | return kSKPViewport.roundOut().size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 132 | |
| 133 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 134 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 135 | DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?"); |
| 136 | |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 137 | GPUSink::GPUSink(GrContextFactory::GLContextType ct, |
| 138 | GrGLStandard api, |
| 139 | int samples, |
| 140 | bool dfText, |
| 141 | bool threaded) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 142 | : fContextType(ct) |
| 143 | , fGpuAPI(api) |
| 144 | , fSampleCount(samples) |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 145 | , fUseDFText(dfText) |
| 146 | , fThreaded(threaded) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 147 | |
| 148 | int GPUSink::enclave() const { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 149 | return fThreaded ? kAnyThread_Enclave : kGPU_Enclave; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 150 | } |
| 151 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 152 | Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 153 | GrContextFactory factory; |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 154 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 155 | const SkImageInfo info = |
| 156 | SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType); |
| 157 | SkAutoTUnref<SkSurface> surface( |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 158 | NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 159 | if (!surface) { |
| 160 | return "Could not create a surface."; |
| 161 | } |
| 162 | SkCanvas* canvas = surface->getCanvas(); |
| 163 | Error err = src.draw(canvas); |
| 164 | if (!err.isEmpty()) { |
| 165 | return err; |
| 166 | } |
| 167 | canvas->flush(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 168 | if (FLAGS_gpuStats) { |
| 169 | canvas->getGrContext()->dumpCacheStats(log); |
| 170 | canvas->getGrContext()->dumpGpuStats(log); |
| 171 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 172 | dst->allocPixels(info); |
| 173 | canvas->readPixels(dst, 0,0); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 174 | if (FLAGS_abandonGpuContext) { |
| 175 | factory.abandonContexts(); |
| 176 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 177 | return ""; |
| 178 | } |
| 179 | |
| 180 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 181 | |
| 182 | PDFSink::PDFSink() {} |
| 183 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 184 | Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 185 | // Print the given DM:Src to a PDF, breaking on 8.5x11 pages. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 186 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 187 | |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 188 | int width = src.size().width(), |
| 189 | height = src.size().height(); |
| 190 | |
| 191 | const int kLetterWidth = 612, // 8.5 * 72 |
| 192 | kLetterHeight = 792; // 11 * 72 |
| 193 | const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth), |
| 194 | SkIntToScalar(kLetterHeight)); |
| 195 | |
| 196 | int xPages = ((width - 1) / kLetterWidth) + 1; |
| 197 | int yPages = ((height - 1) / kLetterHeight) + 1; |
| 198 | |
| 199 | for (int y = 0; y < yPages; ++y) { |
| 200 | for (int x = 0; x < xPages; ++x) { |
| 201 | int w = SkTMin(kLetterWidth, width - (x * kLetterWidth)); |
| 202 | int h = SkTMin(kLetterHeight, height - (y * kLetterHeight)); |
| 203 | SkCanvas* canvas = |
| 204 | doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)); |
| 205 | canvas->clipRect(letter); |
| 206 | canvas->translate(-letter.width() * x, -letter.height() * y); |
| 207 | Error err = src.draw(canvas); |
| 208 | if (!err.isEmpty()) { |
| 209 | return err; |
| 210 | } |
| 211 | doc->endPage(); |
| 212 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 213 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 214 | doc->close(); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 215 | dst->flush(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 216 | return ""; |
| 217 | } |
| 218 | |
| 219 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 220 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 221 | SKPSink::SKPSink() {} |
| 222 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 223 | Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 224 | SkSize size; |
| 225 | size = src.size(); |
| 226 | SkPictureRecorder recorder; |
| 227 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 228 | if (!err.isEmpty()) { |
| 229 | return err; |
| 230 | } |
| 231 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 232 | pic->serialize(dst); |
| 233 | return ""; |
| 234 | } |
| 235 | |
| 236 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 237 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 238 | SVGSink::SVGSink() {} |
| 239 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 240 | Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 241 | SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst))); |
| 242 | SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create( |
| 243 | SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())), |
| 244 | xmlWriter)); |
| 245 | return src.draw(canvas); |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 249 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 250 | RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {} |
| 251 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 252 | Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 253 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 254 | // If there's an appropriate alpha type for this color type, use it, otherwise use premul. |
| 255 | SkAlphaType alphaType = kPremul_SkAlphaType; |
| 256 | (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType); |
| 257 | |
| 258 | dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType)); |
| 259 | dst->eraseColor(SK_ColorTRANSPARENT); |
| 260 | SkCanvas canvas(*dst); |
| 261 | return src.draw(&canvas); |
| 262 | } |
| 263 | |
| 264 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 265 | |
| 266 | ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {} |
| 267 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 268 | Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 269 | // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream. |
| 270 | struct ProxySrc : public Src { |
| 271 | const Src& fSrc; |
| 272 | SkMatrix fMatrix; |
| 273 | ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {} |
| 274 | |
| 275 | Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 276 | canvas->concat(fMatrix); |
| 277 | return fSrc.draw(canvas); |
| 278 | } |
| 279 | SkISize size() const SK_OVERRIDE { return fSrc.size(); } |
| 280 | Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this. |
| 281 | } proxy(src, fMatrix); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 282 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 286 | |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 287 | ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 288 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 289 | Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 290 | // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 291 | struct ProxySrc : public Src { |
| 292 | const Src& fSrc; |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 293 | ProxySrc(const Src& src) : fSrc(src) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 294 | |
| 295 | Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 296 | SkISize size = this->size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 297 | PipeController controller(canvas, &SkImageDecoder::DecodeMemory); |
| 298 | SkGPipeWriter pipe; |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 299 | const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags. |
| 300 | return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 301 | } |
| 302 | SkISize size() const SK_OVERRIDE { return fSrc.size(); } |
| 303 | Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this. |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 304 | } proxy(src); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 305 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 309 | |
| 310 | ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {} |
| 311 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 312 | Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) |
| 313 | const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 314 | // Record our Src into a picture. |
| 315 | SkSize size; |
| 316 | size = src.size(); |
| 317 | SkPictureRecorder recorder; |
| 318 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 319 | if (!err.isEmpty()) { |
| 320 | return err; |
| 321 | } |
| 322 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 323 | |
| 324 | // Serialize it and then deserialize it. |
| 325 | SkDynamicMemoryWStream wStream; |
| 326 | pic->serialize(&wStream); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 327 | SkAutoTDelete<SkStream> rStream(wStream.detachAsStream()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 328 | SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream)); |
| 329 | |
| 330 | // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream. |
| 331 | struct ProxySrc : public Src { |
| 332 | const SkPicture* fPic; |
| 333 | const SkISize fSize; |
| 334 | ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {} |
| 335 | |
| 336 | Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 337 | canvas->drawPicture(fPic); |
| 338 | return ""; |
| 339 | } |
| 340 | SkISize size() const SK_OVERRIDE { return fSize; } |
| 341 | Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this. |
| 342 | } proxy(deserialized, src.size()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 343 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 347 | |
| 348 | ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink) |
| 349 | : fW(w) |
| 350 | , fH(h) |
| 351 | , fFactory(factory) |
| 352 | , fSink(sink) {} |
| 353 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 354 | Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 355 | // Record our Src into a picture. |
| 356 | SkSize size; |
| 357 | size = src.size(); |
| 358 | SkPictureRecorder recorder; |
| 359 | Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get())); |
| 360 | if (!err.isEmpty()) { |
| 361 | return err; |
| 362 | } |
| 363 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 364 | |
| 365 | // Turn that picture into a Src that draws into our Sink via tiles + MPD. |
| 366 | struct ProxySrc : public Src { |
| 367 | const int fW, fH; |
| 368 | const SkPicture* fPic; |
| 369 | const SkISize fSize; |
| 370 | ProxySrc(int w, int h, const SkPicture* pic, SkISize size) |
| 371 | : fW(w), fH(h), fPic(pic), fSize(size) {} |
| 372 | |
| 373 | Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 374 | const int xTiles = (fSize.width() + fW - 1) / fW, |
| 375 | yTiles = (fSize.height() + fH - 1) / fH; |
| 376 | SkMultiPictureDraw mpd(xTiles*yTiles); |
| 377 | SkTDArray<SkSurface*> surfaces; |
| 378 | surfaces.setReserve(xTiles*yTiles); |
| 379 | |
| 380 | SkImageInfo info = canvas->imageInfo().makeWH(fW, fH); |
| 381 | for (int j = 0; j < yTiles; j++) { |
| 382 | for (int i = 0; i < xTiles; i++) { |
| 383 | // This lets our ultimate Sink determine the best kind of surface. |
| 384 | // E.g., if it's a GpuSink, the surfaces and images are textures. |
| 385 | SkSurface* s = canvas->newSurface(info); |
| 386 | if (!s) { |
| 387 | s = SkSurface::NewRaster(info); // Some canvases can't create surfaces. |
| 388 | } |
| 389 | surfaces.push(s); |
| 390 | SkCanvas* c = s->getCanvas(); |
| 391 | c->translate(SkIntToScalar(-i * fW), |
| 392 | SkIntToScalar(-j * fH)); // Line up the canvas with this tile. |
| 393 | mpd.add(c, fPic); |
| 394 | } |
| 395 | } |
| 396 | mpd.draw(); |
| 397 | for (int j = 0; j < yTiles; j++) { |
| 398 | for (int i = 0; i < xTiles; i++) { |
| 399 | SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot()); |
| 400 | canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH)); |
| 401 | } |
| 402 | } |
| 403 | surfaces.unrefAll(); |
| 404 | return ""; |
| 405 | } |
| 406 | SkISize size() const SK_OVERRIDE { return fSize; } |
| 407 | Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this. |
| 408 | } proxy(fW, fH, pic, src.size()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 409 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | } // namespace DM |