mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1 | #include "DMSrcSink.h" |
| 2 | #include "SamplePipeControllers.h" |
| 3 | #include "SkCommonFlags.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 4 | #include "SkCodec.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 5 | #include "SkDocument.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 6 | #include "SkError.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 7 | #include "SkMultiPictureDraw.h" |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 8 | #include "SkNullCanvas.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 9 | #include "SkOSFile.h" |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame^] | 10 | #include "SkPictureData.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 11 | #include "SkPictureRecorder.h" |
| 12 | #include "SkRandom.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 13 | #include "SkSVGCanvas.h" |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 14 | #include "SkStream.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 15 | #include "SkXMLWriter.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 16 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 17 | DEFINE_bool(codec, false, "Use SkCodec instead of SkImageDecoder"); |
| 18 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 19 | namespace DM { |
| 20 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 21 | GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
| 22 | |
| 23 | Error GMSrc::draw(SkCanvas* canvas) const { |
| 24 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 25 | canvas->concat(gm->getInitialTransform()); |
| 26 | gm->draw(canvas); |
| 27 | return ""; |
| 28 | } |
| 29 | |
| 30 | SkISize GMSrc::size() const { |
| 31 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 32 | return gm->getISize(); |
| 33 | } |
| 34 | |
| 35 | Name GMSrc::name() const { |
| 36 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 37 | return gm->getName(); |
| 38 | } |
| 39 | |
| 40 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 41 | |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 42 | ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 43 | |
| 44 | Error ImageSrc::draw(SkCanvas* canvas) const { |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 45 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 46 | if (!encoded) { |
| 47 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 48 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 49 | const SkColorType dstColorType = canvas->imageInfo().colorType(); |
| 50 | if (fDivisor == 0) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 51 | // Decode the full image. |
| 52 | SkBitmap bitmap; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 53 | if (FLAGS_codec) { |
| 54 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 55 | if (!codec) { |
| 56 | return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
| 57 | } |
| 58 | SkImageInfo info; |
| 59 | if (!codec->getInfo(&info)) { |
| 60 | return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str()); |
| 61 | } |
| 62 | info = info.makeColorType(dstColorType); |
| 63 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 64 | // FIXME: Currently we cannot draw unpremultiplied sources. |
| 65 | info = info.makeAlphaType(kPremul_SkAlphaType); |
| 66 | } |
| 67 | if (!bitmap.tryAllocPixels(info)) { |
| 68 | return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(), |
| 69 | info.width(), info.height()); |
| 70 | } |
| 71 | SkAutoLockPixels alp(bitmap); |
| 72 | const SkImageGenerator::Result result = codec->getPixels(info, bitmap.getPixels(), |
| 73 | bitmap.rowBytes()); |
scroggo | 56e25dd | 2015-03-05 11:46:40 -0800 | [diff] [blame] | 74 | switch (result) { |
| 75 | case SkImageGenerator::kSuccess: |
| 76 | // We consider incomplete to be valid, since we should still decode what is |
| 77 | // available. |
| 78 | case SkImageGenerator::kIncompleteInput: |
| 79 | break; |
| 80 | case SkImageGenerator::kInvalidConversion: |
| 81 | return Error::Nonfatal("Incompatible colortype conversion"); |
| 82 | default: |
| 83 | // Everything else is considered a failure. |
| 84 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 85 | } |
| 86 | } else { |
| 87 | if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap, |
| 88 | dstColorType, SkImageDecoder::kDecodePixels_Mode)) { |
| 89 | return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
| 90 | } |
scroggo | 56e25dd | 2015-03-05 11:46:40 -0800 | [diff] [blame] | 91 | if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) { |
| 92 | // Do not draw a bitmap with alpha to a destination without alpha. |
| 93 | return Error::Nonfatal("Uninteresting to decode image with alpha into 565."); |
| 94 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 95 | } |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 96 | 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] | 97 | canvas->drawBitmap(bitmap, 0,0); |
| 98 | return ""; |
| 99 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 100 | // Decode subsets. This is a little involved. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 101 | SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
| 102 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 103 | if (!decoder) { |
| 104 | return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()); |
| 105 | } |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 106 | stream->rewind(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 107 | int w,h; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 108 | if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) { |
mtklein | 4089ef7 | 2015-03-05 08:40:28 -0800 | [diff] [blame] | 109 | return Error::Nonfatal("Subset decoding not supported."); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 110 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 111 | |
| 112 | // Divide the image into subsets that cover the entire image. |
| 113 | if (fDivisor > w || fDivisor > h) { |
| 114 | return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)", |
| 115 | fDivisor, fPath.c_str(), w, h); |
| 116 | } |
| 117 | const int subsetWidth = w / fDivisor, |
| 118 | subsetHeight = h / fDivisor; |
| 119 | for (int y = 0; y < h; y += subsetHeight) { |
| 120 | for (int x = 0; x < w; x += subsetWidth) { |
| 121 | SkBitmap subset; |
| 122 | SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight); |
| 123 | if (!decoder->decodeSubset(&subset, rect, dstColorType)) { |
| 124 | return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).", |
| 125 | x, y, x+subsetWidth, y+subsetHeight); |
| 126 | } |
scroggo | 56e25dd | 2015-03-05 11:46:40 -0800 | [diff] [blame] | 127 | if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) { |
| 128 | // Do not draw a bitmap with alpha to a destination without alpha. |
| 129 | // This is not an error, but there is nothing interesting to show. |
| 130 | |
| 131 | // This should only happen on the first iteration through the loop. |
| 132 | SkASSERT(0 == x && 0 == y); |
| 133 | |
| 134 | return Error::Nonfatal("Uninteresting to decode image with alpha into 565."); |
| 135 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 136 | canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 137 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 138 | } |
| 139 | return ""; |
| 140 | } |
| 141 | |
| 142 | SkISize ImageSrc::size() const { |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 143 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
msarett | 3675874 | 2015-03-16 08:27:53 -0700 | [diff] [blame] | 144 | if (FLAGS_codec) { |
| 145 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 146 | if (!codec) { |
| 147 | return SkISize::Make(0,0); |
| 148 | } |
| 149 | SkImageInfo info; |
| 150 | if (!codec->getInfo(&info)) { |
| 151 | return SkISize::Make(0,0); |
| 152 | } |
| 153 | return info.dimensions(); |
| 154 | } else { |
| 155 | SkBitmap bitmap; |
| 156 | if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), |
| 157 | encoded->size(), |
| 158 | &bitmap, |
| 159 | kUnknown_SkColorType, |
| 160 | SkImageDecoder::kDecodeBounds_Mode)) { |
| 161 | return SkISize::Make(0,0); |
| 162 | } |
| 163 | return bitmap.dimensions(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 164 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 165 | } |
| 166 | |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 167 | Name ImageSrc::name() const { |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 168 | return SkOSPath::Basename(fPath.c_str()); |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 169 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 170 | |
| 171 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 172 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 173 | static const SkRect kSKPViewport = {0,0, 1000,1000}; |
| 174 | |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 175 | SKPSrc::SKPSrc(Path path) : fPath(path) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 176 | |
| 177 | Error SKPSrc::draw(SkCanvas* canvas) const { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 178 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 179 | if (!stream) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 180 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 181 | } |
halcanary | 44906c6 | 2015-02-23 10:43:48 -0800 | [diff] [blame] | 182 | SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream)); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 183 | if (!pic) { |
| 184 | return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str()); |
| 185 | } |
| 186 | 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] | 187 | canvas->clipRect(kSKPViewport); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 188 | canvas->drawPicture(pic); |
| 189 | return ""; |
| 190 | } |
| 191 | |
| 192 | SkISize SKPSrc::size() const { |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame^] | 193 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
| 194 | if (!stream) { |
| 195 | return SkISize::Make(0,0); |
| 196 | } |
| 197 | SkPictInfo info; |
| 198 | if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) { |
| 199 | return SkISize::Make(0,0); |
| 200 | } |
| 201 | SkRect viewport = kSKPViewport; |
| 202 | if (!viewport.intersect(info.fCullRect)) { |
| 203 | return SkISize::Make(0,0); |
| 204 | } |
| 205 | return viewport.roundOut().size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 209 | |
| 210 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 211 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 212 | Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const { |
| 213 | SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas()); |
| 214 | return src.draw(canvas); |
| 215 | } |
| 216 | |
| 217 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 218 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 219 | DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?"); |
| 220 | |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 221 | GPUSink::GPUSink(GrContextFactory::GLContextType ct, |
| 222 | GrGLStandard api, |
| 223 | int samples, |
| 224 | bool dfText, |
| 225 | bool threaded) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 226 | : fContextType(ct) |
| 227 | , fGpuAPI(api) |
| 228 | , fSampleCount(samples) |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 229 | , fUseDFText(dfText) |
| 230 | , fThreaded(threaded) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 231 | |
| 232 | int GPUSink::enclave() const { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 233 | return fThreaded ? kAnyThread_Enclave : kGPU_Enclave; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 234 | } |
| 235 | |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 236 | void PreAbandonGpuContextErrorHandler(SkError, void*) {} |
| 237 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 238 | Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 239 | GrContextFactory factory; |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 240 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 241 | const SkImageInfo info = |
| 242 | SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType); |
| 243 | SkAutoTUnref<SkSurface> surface( |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 244 | NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 245 | if (!surface) { |
| 246 | return "Could not create a surface."; |
| 247 | } |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 248 | if (FLAGS_preAbandonGpuContext) { |
| 249 | SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, NULL); |
| 250 | factory.abandonContexts(); |
| 251 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 252 | SkCanvas* canvas = surface->getCanvas(); |
| 253 | Error err = src.draw(canvas); |
| 254 | if (!err.isEmpty()) { |
| 255 | return err; |
| 256 | } |
| 257 | canvas->flush(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 258 | if (FLAGS_gpuStats) { |
| 259 | canvas->getGrContext()->dumpCacheStats(log); |
| 260 | canvas->getGrContext()->dumpGpuStats(log); |
| 261 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 262 | dst->allocPixels(info); |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 263 | canvas->readPixels(dst, 0, 0); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 264 | if (FLAGS_abandonGpuContext) { |
| 265 | factory.abandonContexts(); |
| 266 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 267 | return ""; |
| 268 | } |
| 269 | |
| 270 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 271 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 272 | static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) { |
| 273 | // Print the given DM:Src to a document, breaking on 8.5x11 pages. |
| 274 | SkASSERT(doc); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 275 | int width = src.size().width(), |
| 276 | height = src.size().height(); |
| 277 | |
| 278 | const int kLetterWidth = 612, // 8.5 * 72 |
| 279 | kLetterHeight = 792; // 11 * 72 |
| 280 | const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth), |
| 281 | SkIntToScalar(kLetterHeight)); |
| 282 | |
| 283 | int xPages = ((width - 1) / kLetterWidth) + 1; |
| 284 | int yPages = ((height - 1) / kLetterHeight) + 1; |
| 285 | |
| 286 | for (int y = 0; y < yPages; ++y) { |
| 287 | for (int x = 0; x < xPages; ++x) { |
| 288 | int w = SkTMin(kLetterWidth, width - (x * kLetterWidth)); |
| 289 | int h = SkTMin(kLetterHeight, height - (y * kLetterHeight)); |
| 290 | SkCanvas* canvas = |
| 291 | doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)); |
| 292 | canvas->clipRect(letter); |
| 293 | canvas->translate(-letter.width() * x, -letter.height() * y); |
| 294 | Error err = src.draw(canvas); |
| 295 | if (!err.isEmpty()) { |
| 296 | return err; |
| 297 | } |
| 298 | doc->endPage(); |
| 299 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 300 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 301 | doc->close(); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 302 | dst->flush(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 303 | return ""; |
| 304 | } |
| 305 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 306 | PDFSink::PDFSink() {} |
| 307 | |
| 308 | Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 309 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst)); |
| 310 | if (!doc) { |
| 311 | return "SkDocument::CreatePDF() returned NULL"; |
| 312 | } |
| 313 | return draw_skdocument(src, doc.get(), dst); |
| 314 | } |
| 315 | |
| 316 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 317 | |
| 318 | XPSSink::XPSSink() {} |
| 319 | |
| 320 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 321 | SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst)); |
| 322 | if (!doc) { |
| 323 | return "SkDocument::CreateXPS() returned NULL"; |
| 324 | } |
| 325 | return draw_skdocument(src, doc.get(), dst); |
| 326 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 327 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 328 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 329 | SKPSink::SKPSink() {} |
| 330 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 331 | Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 332 | SkSize size; |
| 333 | size = src.size(); |
| 334 | SkPictureRecorder recorder; |
| 335 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 336 | if (!err.isEmpty()) { |
| 337 | return err; |
| 338 | } |
| 339 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 340 | pic->serialize(dst); |
| 341 | return ""; |
| 342 | } |
| 343 | |
| 344 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 345 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 346 | SVGSink::SVGSink() {} |
| 347 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 348 | Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 349 | SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst))); |
| 350 | SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create( |
| 351 | SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())), |
| 352 | xmlWriter)); |
| 353 | return src.draw(canvas); |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 357 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 358 | RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {} |
| 359 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 360 | Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 361 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 362 | // If there's an appropriate alpha type for this color type, use it, otherwise use premul. |
| 363 | SkAlphaType alphaType = kPremul_SkAlphaType; |
| 364 | (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType); |
| 365 | |
| 366 | dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType)); |
| 367 | dst->eraseColor(SK_ColorTRANSPARENT); |
| 368 | SkCanvas canvas(*dst); |
| 369 | return src.draw(&canvas); |
| 370 | } |
| 371 | |
| 372 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 373 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 374 | static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) { |
| 375 | SkRect bounds = SkRect::MakeIWH(srcW, srcH); |
| 376 | matrix->mapRect(&bounds); |
| 377 | matrix->postTranslate(-bounds.x(), -bounds.y()); |
| 378 | return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())); |
| 379 | } |
| 380 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 381 | ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {} |
| 382 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 383 | Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 384 | // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream. |
| 385 | struct ProxySrc : public Src { |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 386 | const Src& fSrc; |
| 387 | SkMatrix fMatrix; |
| 388 | SkISize fSize; |
| 389 | |
| 390 | ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) { |
| 391 | fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height()); |
| 392 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 393 | |
| 394 | Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 395 | canvas->concat(fMatrix); |
| 396 | return fSrc.draw(canvas); |
| 397 | } |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 398 | SkISize size() const SK_OVERRIDE { return fSize; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 399 | Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this. |
| 400 | } proxy(src, fMatrix); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 401 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 402 | } |
| 403 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 404 | // Undoes any flip or 90 degree rotate without changing the scale of the bitmap. |
| 405 | // This should be pixel-preserving. |
| 406 | ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {} |
| 407 | |
| 408 | Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 409 | Error err = fSink->draw(src, bitmap, stream, log); |
| 410 | if (!err.isEmpty()) { |
| 411 | return err; |
| 412 | } |
| 413 | |
| 414 | SkMatrix inverse; |
| 415 | if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) { |
| 416 | return "Cannot upright --matrix."; |
| 417 | } |
| 418 | SkMatrix upright = SkMatrix::I(); |
| 419 | upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX())); |
| 420 | upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY())); |
| 421 | upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX())); |
| 422 | upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY())); |
| 423 | |
| 424 | SkBitmap uprighted; |
| 425 | SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height()); |
| 426 | uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height())); |
| 427 | |
| 428 | SkCanvas canvas(uprighted); |
| 429 | canvas.concat(upright); |
| 430 | SkPaint paint; |
| 431 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 432 | canvas.drawBitmap(*bitmap, 0, 0, &paint); |
| 433 | |
| 434 | *bitmap = uprighted; |
| 435 | bitmap->lockPixels(); |
| 436 | return ""; |
| 437 | } |
| 438 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 439 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 440 | |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 441 | ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 442 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 443 | Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 444 | // 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] | 445 | struct ProxySrc : public Src { |
| 446 | const Src& fSrc; |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 447 | ProxySrc(const Src& src) : fSrc(src) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 448 | |
| 449 | Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 450 | SkISize size = this->size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 451 | PipeController controller(canvas, &SkImageDecoder::DecodeMemory); |
| 452 | SkGPipeWriter pipe; |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 453 | const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags. |
| 454 | return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 455 | } |
| 456 | SkISize size() const SK_OVERRIDE { return fSrc.size(); } |
| 457 | 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] | 458 | } proxy(src); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 459 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 463 | |
| 464 | ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {} |
| 465 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 466 | Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) |
| 467 | const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 468 | // Record our Src into a picture. |
| 469 | SkSize size; |
| 470 | size = src.size(); |
| 471 | SkPictureRecorder recorder; |
| 472 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 473 | if (!err.isEmpty()) { |
| 474 | return err; |
| 475 | } |
| 476 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 477 | |
| 478 | // Serialize it and then deserialize it. |
| 479 | SkDynamicMemoryWStream wStream; |
| 480 | pic->serialize(&wStream); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 481 | SkAutoTDelete<SkStream> rStream(wStream.detachAsStream()); |
halcanary | 44906c6 | 2015-02-23 10:43:48 -0800 | [diff] [blame] | 482 | SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 483 | |
| 484 | // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream. |
| 485 | struct ProxySrc : public Src { |
| 486 | const SkPicture* fPic; |
| 487 | const SkISize fSize; |
| 488 | ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {} |
| 489 | |
| 490 | Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 491 | canvas->drawPicture(fPic); |
| 492 | return ""; |
| 493 | } |
| 494 | SkISize size() const SK_OVERRIDE { return fSize; } |
| 495 | Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this. |
| 496 | } proxy(deserialized, src.size()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 497 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 501 | |
| 502 | ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink) |
| 503 | : fW(w) |
| 504 | , fH(h) |
| 505 | , fFactory(factory) |
| 506 | , fSink(sink) {} |
| 507 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 508 | Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 509 | // Record our Src into a picture. |
| 510 | SkSize size; |
| 511 | size = src.size(); |
| 512 | SkPictureRecorder recorder; |
| 513 | Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get())); |
| 514 | if (!err.isEmpty()) { |
| 515 | return err; |
| 516 | } |
| 517 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 518 | |
| 519 | // Turn that picture into a Src that draws into our Sink via tiles + MPD. |
| 520 | struct ProxySrc : public Src { |
| 521 | const int fW, fH; |
| 522 | const SkPicture* fPic; |
| 523 | const SkISize fSize; |
| 524 | ProxySrc(int w, int h, const SkPicture* pic, SkISize size) |
| 525 | : fW(w), fH(h), fPic(pic), fSize(size) {} |
| 526 | |
| 527 | Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 528 | const int xTiles = (fSize.width() + fW - 1) / fW, |
| 529 | yTiles = (fSize.height() + fH - 1) / fH; |
| 530 | SkMultiPictureDraw mpd(xTiles*yTiles); |
| 531 | SkTDArray<SkSurface*> surfaces; |
| 532 | surfaces.setReserve(xTiles*yTiles); |
| 533 | |
| 534 | SkImageInfo info = canvas->imageInfo().makeWH(fW, fH); |
| 535 | for (int j = 0; j < yTiles; j++) { |
| 536 | for (int i = 0; i < xTiles; i++) { |
| 537 | // This lets our ultimate Sink determine the best kind of surface. |
| 538 | // E.g., if it's a GpuSink, the surfaces and images are textures. |
| 539 | SkSurface* s = canvas->newSurface(info); |
| 540 | if (!s) { |
| 541 | s = SkSurface::NewRaster(info); // Some canvases can't create surfaces. |
| 542 | } |
| 543 | surfaces.push(s); |
| 544 | SkCanvas* c = s->getCanvas(); |
| 545 | c->translate(SkIntToScalar(-i * fW), |
| 546 | SkIntToScalar(-j * fH)); // Line up the canvas with this tile. |
| 547 | mpd.add(c, fPic); |
| 548 | } |
| 549 | } |
| 550 | mpd.draw(); |
| 551 | for (int j = 0; j < yTiles; j++) { |
| 552 | for (int i = 0; i < xTiles; i++) { |
| 553 | SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot()); |
| 554 | canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH)); |
| 555 | } |
| 556 | } |
| 557 | surfaces.unrefAll(); |
| 558 | return ""; |
| 559 | } |
| 560 | SkISize size() const SK_OVERRIDE { return fSize; } |
| 561 | Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one should be calling this. |
| 562 | } proxy(fW, fH, pic, src.size()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 563 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | } // namespace DM |