scroggo | 478652e | 2015-03-25 07:11:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 8 | #include "DMSrcSink.h" |
| 9 | #include "SamplePipeControllers.h" |
| 10 | #include "SkCommonFlags.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 11 | #include "SkCodec.h" |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 12 | #include "SkData.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 13 | #include "SkDocument.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 14 | #include "SkError.h" |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 15 | #include "SkImageGenerator.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 16 | #include "SkMultiPictureDraw.h" |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 17 | #include "SkNullCanvas.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 18 | #include "SkOSFile.h" |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 19 | #include "SkPictureData.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 20 | #include "SkPictureRecorder.h" |
| 21 | #include "SkRandom.h" |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 22 | #include "SkScanlineDecoder.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 23 | #include "SkSVGCanvas.h" |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 24 | #include "SkStream.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 25 | #include "SkXMLWriter.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 26 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 27 | DEFINE_bool(multiPage, false, "For document-type backends, render the source" |
| 28 | " into multiple pages"); |
| 29 | |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 30 | static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { |
| 31 | SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); |
| 32 | return encoded && SkInstallDiscardablePixelRef(encoded, dst); |
| 33 | } |
| 34 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 35 | namespace DM { |
| 36 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 37 | GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
| 38 | |
| 39 | Error GMSrc::draw(SkCanvas* canvas) const { |
| 40 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 41 | canvas->concat(gm->getInitialTransform()); |
| 42 | gm->draw(canvas); |
| 43 | return ""; |
| 44 | } |
| 45 | |
| 46 | SkISize GMSrc::size() const { |
| 47 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 48 | return gm->getISize(); |
| 49 | } |
| 50 | |
| 51 | Name GMSrc::name() const { |
| 52 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 53 | return gm->getName(); |
| 54 | } |
| 55 | |
| 56 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 57 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 58 | CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType) |
| 59 | : fPath(path) |
| 60 | , fMode(mode) |
| 61 | , fDstColorType(dstColorType) |
| 62 | {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 63 | |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 64 | Error CodecSrc::draw(SkCanvas* canvas) const { |
| 65 | SkImageInfo canvasInfo; |
| 66 | if (NULL == canvas->peekPixels(&canvasInfo, NULL)) { |
| 67 | // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferred decode to |
| 68 | // let the GPU handle it. |
| 69 | return Error::Nonfatal("No need to test decoding to non-raster backend."); |
| 70 | } |
| 71 | |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 72 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 73 | if (!encoded) { |
| 74 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 75 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 76 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 77 | if (NULL == codec.get()) { |
| 78 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 79 | } |
| 80 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 81 | // Choose the color type to decode to |
| 82 | SkImageInfo decodeInfo = codec->getInfo(); |
| 83 | SkColorType canvasColorType = canvasInfo.colorType(); |
| 84 | switch (fDstColorType) { |
| 85 | case kIndex8_Always_DstColorType: |
| 86 | case kGrayscale_Always_DstColorType: |
| 87 | if (kRGB_565_SkColorType == canvasColorType) { |
| 88 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 89 | } |
| 90 | break; |
| 91 | default: |
| 92 | decodeInfo = decodeInfo.makeColorType(canvasColorType); |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | // Construct a color table for the decode if necessary |
| 97 | SkAutoTUnref<SkColorTable> colorTable(NULL); |
| 98 | SkPMColor* colorPtr = NULL; |
| 99 | int* colorCountPtr = NULL; |
| 100 | int maxColors = 256; |
| 101 | if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 102 | SkPMColor colors[256]; |
| 103 | colorTable.reset(SkNEW_ARGS(SkColorTable, (colors, maxColors))); |
| 104 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 105 | colorCountPtr = &maxColors; |
| 106 | } |
| 107 | |
| 108 | // FIXME: Currently we cannot draw unpremultiplied sources. |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 109 | if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) { |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 110 | decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType); |
| 111 | } |
| 112 | |
| 113 | SkBitmap bitmap; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 114 | if (!bitmap.tryAllocPixels(decodeInfo, NULL, colorTable.get())) { |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 115 | return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(), |
| 116 | decodeInfo.width(), decodeInfo.height()); |
| 117 | } |
| 118 | |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 119 | switch (fMode) { |
| 120 | case kNormal_Mode: |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 121 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), NULL, |
| 122 | colorPtr, colorCountPtr)) { |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 123 | case SkImageGenerator::kSuccess: |
| 124 | // We consider incomplete to be valid, since we should still decode what is |
| 125 | // available. |
| 126 | case SkImageGenerator::kIncompleteInput: |
| 127 | break; |
| 128 | case SkImageGenerator::kInvalidConversion: |
| 129 | return Error::Nonfatal("Incompatible colortype conversion"); |
| 130 | default: |
| 131 | // Everything else is considered a failure. |
| 132 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 133 | } |
| 134 | break; |
| 135 | case kScanline_Mode: { |
| 136 | SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(decodeInfo); |
| 137 | if (NULL == scanlineDecoder) { |
| 138 | return Error::Nonfatal("Cannot use scanline decoder for all images"); |
| 139 | } |
| 140 | for (int y = 0; y < decodeInfo.height(); ++y) { |
| 141 | const SkImageGenerator::Result result = scanlineDecoder->getScanlines( |
| 142 | bitmap.getAddr(0, y), 1, 0); |
| 143 | switch (result) { |
| 144 | case SkImageGenerator::kSuccess: |
| 145 | case SkImageGenerator::kIncompleteInput: |
| 146 | break; |
| 147 | default: |
| 148 | return SkStringPrintf("%s failed after %d scanlines with error message %d", |
| 149 | fPath.c_str(), y-1, (int) result); |
| 150 | } |
| 151 | } |
| 152 | break; |
| 153 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 154 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 155 | canvas->drawBitmap(bitmap, 0, 0); |
| 156 | return ""; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | SkISize CodecSrc::size() const { |
| 160 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 161 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 162 | if (NULL != codec) { |
| 163 | return codec->getInfo().dimensions(); |
| 164 | } else { |
| 165 | return SkISize::Make(0, 0); |
| 166 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | Name CodecSrc::name() const { |
| 170 | return SkOSPath::Basename(fPath.c_str()); |
| 171 | } |
| 172 | |
| 173 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 174 | |
| 175 | ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
| 176 | |
| 177 | Error ImageSrc::draw(SkCanvas* canvas) const { |
| 178 | SkImageInfo canvasInfo; |
| 179 | if (NULL == canvas->peekPixels(&canvasInfo, NULL)) { |
| 180 | // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YUV. |
| 181 | return Error::Nonfatal("No need to test decoding to non-raster backend."); |
| 182 | } |
| 183 | |
| 184 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 185 | if (!encoded) { |
| 186 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 187 | } |
| 188 | const SkColorType dstColorType = canvasInfo.colorType(); |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 189 | if (fDivisor == 0) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 190 | // Decode the full image. |
| 191 | SkBitmap bitmap; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 192 | if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap, |
| 193 | dstColorType, SkImageDecoder::kDecodePixels_Mode)) { |
| 194 | return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
| 195 | } |
| 196 | if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) { |
| 197 | // Do not draw a bitmap with alpha to a destination without alpha. |
| 198 | return Error::Nonfatal("Uninteresting to decode image with alpha into 565."); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 199 | } |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 200 | 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] | 201 | canvas->drawBitmap(bitmap, 0,0); |
| 202 | return ""; |
| 203 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 204 | // Decode subsets. This is a little involved. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 205 | SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
| 206 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 207 | if (!decoder) { |
| 208 | return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()); |
| 209 | } |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 210 | stream->rewind(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 211 | int w,h; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 212 | if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) { |
mtklein | 4089ef7 | 2015-03-05 08:40:28 -0800 | [diff] [blame] | 213 | return Error::Nonfatal("Subset decoding not supported."); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 214 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 215 | |
| 216 | // Divide the image into subsets that cover the entire image. |
| 217 | if (fDivisor > w || fDivisor > h) { |
| 218 | return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)", |
| 219 | fDivisor, fPath.c_str(), w, h); |
| 220 | } |
| 221 | const int subsetWidth = w / fDivisor, |
| 222 | subsetHeight = h / fDivisor; |
| 223 | for (int y = 0; y < h; y += subsetHeight) { |
| 224 | for (int x = 0; x < w; x += subsetWidth) { |
| 225 | SkBitmap subset; |
| 226 | SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight); |
| 227 | if (!decoder->decodeSubset(&subset, rect, dstColorType)) { |
| 228 | return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).", |
| 229 | x, y, x+subsetWidth, y+subsetHeight); |
| 230 | } |
scroggo | 56e25dd | 2015-03-05 11:46:40 -0800 | [diff] [blame] | 231 | if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) { |
| 232 | // Do not draw a bitmap with alpha to a destination without alpha. |
| 233 | // This is not an error, but there is nothing interesting to show. |
| 234 | |
| 235 | // This should only happen on the first iteration through the loop. |
| 236 | SkASSERT(0 == x && 0 == y); |
| 237 | |
| 238 | return Error::Nonfatal("Uninteresting to decode image with alpha into 565."); |
| 239 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 240 | canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 241 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 242 | } |
| 243 | return ""; |
| 244 | } |
| 245 | |
| 246 | SkISize ImageSrc::size() const { |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 247 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 248 | SkBitmap bitmap; |
| 249 | if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), |
| 250 | encoded->size(), |
| 251 | &bitmap, |
| 252 | kUnknown_SkColorType, |
| 253 | SkImageDecoder::kDecodeBounds_Mode)) { |
| 254 | return SkISize::Make(0,0); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 255 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 256 | return bitmap.dimensions(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 257 | } |
| 258 | |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 259 | Name ImageSrc::name() const { |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 260 | return SkOSPath::Basename(fPath.c_str()); |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 261 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 262 | |
| 263 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 264 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 265 | static const SkRect kSKPViewport = {0,0, 1000,1000}; |
| 266 | |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 267 | SKPSrc::SKPSrc(Path path) : fPath(path) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 268 | |
| 269 | Error SKPSrc::draw(SkCanvas* canvas) const { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 270 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 271 | if (!stream) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 272 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 273 | } |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 274 | SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap)); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 275 | if (!pic) { |
| 276 | return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str()); |
| 277 | } |
| 278 | stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 279 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 280 | canvas->clipRect(kSKPViewport); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 281 | // Testing TextBlob batching requires that we see individual text blobs more than once |
| 282 | // TODO remove this and add a flag to DM so we can run skps multiple times |
| 283 | //#define DOUBLE_LOOP |
| 284 | #ifdef DOUBLE_LOOP |
| 285 | { |
| 286 | SkAutoCanvasRestore acr(canvas, true); |
| 287 | #endif |
| 288 | canvas->drawPicture(pic); |
| 289 | #ifdef DOUBLE_LOOP |
| 290 | } |
| 291 | canvas->clear(0); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 292 | canvas->drawPicture(pic); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 293 | #endif |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 294 | return ""; |
| 295 | } |
| 296 | |
| 297 | SkISize SKPSrc::size() const { |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 298 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
| 299 | if (!stream) { |
| 300 | return SkISize::Make(0,0); |
| 301 | } |
| 302 | SkPictInfo info; |
| 303 | if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) { |
| 304 | return SkISize::Make(0,0); |
| 305 | } |
| 306 | SkRect viewport = kSKPViewport; |
| 307 | if (!viewport.intersect(info.fCullRect)) { |
| 308 | return SkISize::Make(0,0); |
| 309 | } |
| 310 | return viewport.roundOut().size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 314 | |
| 315 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 316 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 317 | Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const { |
| 318 | SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas()); |
| 319 | return src.draw(canvas); |
| 320 | } |
| 321 | |
| 322 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 323 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 324 | DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?"); |
| 325 | |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 326 | GPUSink::GPUSink(GrContextFactory::GLContextType ct, |
| 327 | GrGLStandard api, |
| 328 | int samples, |
| 329 | bool dfText, |
| 330 | bool threaded) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 331 | : fContextType(ct) |
| 332 | , fGpuAPI(api) |
| 333 | , fSampleCount(samples) |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 334 | , fUseDFText(dfText) |
| 335 | , fThreaded(threaded) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 336 | |
| 337 | int GPUSink::enclave() const { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 338 | return fThreaded ? kAnyThread_Enclave : kGPU_Enclave; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 339 | } |
| 340 | |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 341 | void PreAbandonGpuContextErrorHandler(SkError, void*) {} |
| 342 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 343 | Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 344 | GrContextFactory factory; |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 345 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 346 | const SkImageInfo info = |
| 347 | SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType); |
| 348 | SkAutoTUnref<SkSurface> surface( |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 349 | NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 350 | if (!surface) { |
| 351 | return "Could not create a surface."; |
| 352 | } |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 353 | if (FLAGS_preAbandonGpuContext) { |
| 354 | SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, NULL); |
| 355 | factory.abandonContexts(); |
| 356 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 357 | SkCanvas* canvas = surface->getCanvas(); |
| 358 | Error err = src.draw(canvas); |
| 359 | if (!err.isEmpty()) { |
| 360 | return err; |
| 361 | } |
| 362 | canvas->flush(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 363 | if (FLAGS_gpuStats) { |
| 364 | canvas->getGrContext()->dumpCacheStats(log); |
| 365 | canvas->getGrContext()->dumpGpuStats(log); |
| 366 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 367 | dst->allocPixels(info); |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 368 | canvas->readPixels(dst, 0, 0); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 369 | if (FLAGS_abandonGpuContext) { |
| 370 | factory.abandonContexts(); |
| 371 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 372 | return ""; |
| 373 | } |
| 374 | |
| 375 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 376 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 377 | static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) { |
| 378 | // Print the given DM:Src to a document, breaking on 8.5x11 pages. |
| 379 | SkASSERT(doc); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 380 | int width = src.size().width(), |
| 381 | height = src.size().height(); |
| 382 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 383 | if (FLAGS_multiPage) { |
| 384 | const int kLetterWidth = 612, // 8.5 * 72 |
| 385 | kLetterHeight = 792; // 11 * 72 |
| 386 | const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth), |
| 387 | SkIntToScalar(kLetterHeight)); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 388 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 389 | int xPages = ((width - 1) / kLetterWidth) + 1; |
| 390 | int yPages = ((height - 1) / kLetterHeight) + 1; |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 391 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 392 | for (int y = 0; y < yPages; ++y) { |
| 393 | for (int x = 0; x < xPages; ++x) { |
| 394 | int w = SkTMin(kLetterWidth, width - (x * kLetterWidth)); |
| 395 | int h = SkTMin(kLetterHeight, height - (y * kLetterHeight)); |
| 396 | SkCanvas* canvas = |
| 397 | doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)); |
| 398 | if (!canvas) { |
| 399 | return "SkDocument::beginPage(w,h) returned NULL"; |
| 400 | } |
| 401 | canvas->clipRect(letter); |
| 402 | canvas->translate(-letter.width() * x, -letter.height() * y); |
| 403 | Error err = src.draw(canvas); |
| 404 | if (!err.isEmpty()) { |
| 405 | return err; |
| 406 | } |
| 407 | doc->endPage(); |
djsollen | 2ab9000 | 2015-04-03 06:38:31 -0700 | [diff] [blame] | 408 | } |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 409 | } |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 410 | } else { |
| 411 | SkCanvas* canvas = |
| 412 | doc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); |
| 413 | if (!canvas) { |
| 414 | return "SkDocument::beginPage(w,h) returned NULL"; |
| 415 | } |
| 416 | Error err = src.draw(canvas); |
| 417 | if (!err.isEmpty()) { |
| 418 | return err; |
| 419 | } |
| 420 | doc->endPage(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 421 | } |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 422 | if (!doc->close()) { |
| 423 | return "SkDocument::close() returned false"; |
| 424 | } |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 425 | dst->flush(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 426 | return ""; |
| 427 | } |
| 428 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 429 | PDFSink::PDFSink() {} |
| 430 | |
| 431 | Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 432 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst)); |
| 433 | if (!doc) { |
| 434 | return "SkDocument::CreatePDF() returned NULL"; |
| 435 | } |
| 436 | return draw_skdocument(src, doc.get(), dst); |
| 437 | } |
| 438 | |
| 439 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 440 | |
| 441 | XPSSink::XPSSink() {} |
| 442 | |
| 443 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 444 | SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst)); |
| 445 | if (!doc) { |
| 446 | return "SkDocument::CreateXPS() returned NULL"; |
| 447 | } |
| 448 | return draw_skdocument(src, doc.get(), dst); |
| 449 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 450 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 451 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 452 | SKPSink::SKPSink() {} |
| 453 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 454 | Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 455 | SkSize size; |
| 456 | size = src.size(); |
| 457 | SkPictureRecorder recorder; |
| 458 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 459 | if (!err.isEmpty()) { |
| 460 | return err; |
| 461 | } |
| 462 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 463 | pic->serialize(dst); |
| 464 | return ""; |
| 465 | } |
| 466 | |
| 467 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 468 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 469 | SVGSink::SVGSink() {} |
| 470 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 471 | Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 472 | SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst))); |
| 473 | SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create( |
| 474 | SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())), |
| 475 | xmlWriter)); |
| 476 | return src.draw(canvas); |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 480 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 481 | RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {} |
| 482 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 483 | Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 484 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 485 | // If there's an appropriate alpha type for this color type, use it, otherwise use premul. |
| 486 | SkAlphaType alphaType = kPremul_SkAlphaType; |
| 487 | (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType); |
| 488 | |
| 489 | dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType)); |
| 490 | dst->eraseColor(SK_ColorTRANSPARENT); |
| 491 | SkCanvas canvas(*dst); |
| 492 | return src.draw(&canvas); |
| 493 | } |
| 494 | |
| 495 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 496 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 497 | static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) { |
| 498 | SkRect bounds = SkRect::MakeIWH(srcW, srcH); |
| 499 | matrix->mapRect(&bounds); |
| 500 | matrix->postTranslate(-bounds.x(), -bounds.y()); |
| 501 | return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())); |
| 502 | } |
| 503 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 504 | ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {} |
| 505 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 506 | Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 507 | // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream. |
| 508 | struct ProxySrc : public Src { |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 509 | const Src& fSrc; |
| 510 | SkMatrix fMatrix; |
| 511 | SkISize fSize; |
| 512 | |
| 513 | ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) { |
| 514 | fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height()); |
| 515 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 516 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 517 | Error draw(SkCanvas* canvas) const override { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 518 | canvas->concat(fMatrix); |
| 519 | return fSrc.draw(canvas); |
| 520 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 521 | SkISize size() const override { return fSize; } |
| 522 | Name name() const override { sk_throw(); return ""; } // No one should be calling this. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 523 | } proxy(src, fMatrix); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 524 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 525 | } |
| 526 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 527 | // Undoes any flip or 90 degree rotate without changing the scale of the bitmap. |
| 528 | // This should be pixel-preserving. |
| 529 | ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {} |
| 530 | |
| 531 | Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 532 | Error err = fSink->draw(src, bitmap, stream, log); |
| 533 | if (!err.isEmpty()) { |
| 534 | return err; |
| 535 | } |
| 536 | |
| 537 | SkMatrix inverse; |
| 538 | if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) { |
| 539 | return "Cannot upright --matrix."; |
| 540 | } |
| 541 | SkMatrix upright = SkMatrix::I(); |
| 542 | upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX())); |
| 543 | upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY())); |
| 544 | upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX())); |
| 545 | upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY())); |
| 546 | |
| 547 | SkBitmap uprighted; |
| 548 | SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height()); |
| 549 | uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height())); |
| 550 | |
| 551 | SkCanvas canvas(uprighted); |
| 552 | canvas.concat(upright); |
| 553 | SkPaint paint; |
| 554 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 555 | canvas.drawBitmap(*bitmap, 0, 0, &paint); |
| 556 | |
| 557 | *bitmap = uprighted; |
| 558 | bitmap->lockPixels(); |
| 559 | return ""; |
| 560 | } |
| 561 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 562 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 563 | |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 564 | ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 565 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 566 | Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 567 | // 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] | 568 | struct ProxySrc : public Src { |
| 569 | const Src& fSrc; |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 570 | ProxySrc(const Src& src) : fSrc(src) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 571 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 572 | Error draw(SkCanvas* canvas) const override { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 573 | SkISize size = this->size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 574 | PipeController controller(canvas, &SkImageDecoder::DecodeMemory); |
| 575 | SkGPipeWriter pipe; |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 576 | const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags. |
| 577 | return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 578 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 579 | SkISize size() const override { return fSrc.size(); } |
| 580 | Name name() const override { sk_throw(); return ""; } // No one should be calling this. |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 581 | } proxy(src); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 582 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 586 | |
| 587 | ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {} |
| 588 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 589 | Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) |
| 590 | const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 591 | // Record our Src into a picture. |
| 592 | SkSize size; |
| 593 | size = src.size(); |
| 594 | SkPictureRecorder recorder; |
| 595 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 596 | if (!err.isEmpty()) { |
| 597 | return err; |
| 598 | } |
| 599 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 600 | |
| 601 | // Serialize it and then deserialize it. |
| 602 | SkDynamicMemoryWStream wStream; |
| 603 | pic->serialize(&wStream); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 604 | SkAutoTDelete<SkStream> rStream(wStream.detachAsStream()); |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 605 | SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 606 | |
| 607 | // Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream. |
| 608 | struct ProxySrc : public Src { |
| 609 | const SkPicture* fPic; |
| 610 | const SkISize fSize; |
| 611 | ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {} |
| 612 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 613 | Error draw(SkCanvas* canvas) const override { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 614 | canvas->drawPicture(fPic); |
| 615 | return ""; |
| 616 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 617 | SkISize size() const override { return fSize; } |
| 618 | Name name() const override { sk_throw(); return ""; } // No one should be calling this. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 619 | } proxy(deserialized, src.size()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 620 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 624 | |
| 625 | ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink) |
| 626 | : fW(w) |
| 627 | , fH(h) |
| 628 | , fFactory(factory) |
| 629 | , fSink(sink) {} |
| 630 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 631 | Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 632 | // Record our Src into a picture. |
| 633 | SkSize size; |
| 634 | size = src.size(); |
| 635 | SkPictureRecorder recorder; |
| 636 | Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get())); |
| 637 | if (!err.isEmpty()) { |
| 638 | return err; |
| 639 | } |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 640 | SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 641 | |
| 642 | // Turn that picture into a Src that draws into our Sink via tiles + MPD. |
| 643 | struct ProxySrc : public Src { |
| 644 | const int fW, fH; |
| 645 | const SkPicture* fPic; |
| 646 | const SkISize fSize; |
| 647 | ProxySrc(int w, int h, const SkPicture* pic, SkISize size) |
| 648 | : fW(w), fH(h), fPic(pic), fSize(size) {} |
| 649 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 650 | Error draw(SkCanvas* canvas) const override { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 651 | const int xTiles = (fSize.width() + fW - 1) / fW, |
| 652 | yTiles = (fSize.height() + fH - 1) / fH; |
| 653 | SkMultiPictureDraw mpd(xTiles*yTiles); |
| 654 | SkTDArray<SkSurface*> surfaces; |
| 655 | surfaces.setReserve(xTiles*yTiles); |
| 656 | |
| 657 | SkImageInfo info = canvas->imageInfo().makeWH(fW, fH); |
| 658 | for (int j = 0; j < yTiles; j++) { |
| 659 | for (int i = 0; i < xTiles; i++) { |
| 660 | // This lets our ultimate Sink determine the best kind of surface. |
| 661 | // E.g., if it's a GpuSink, the surfaces and images are textures. |
| 662 | SkSurface* s = canvas->newSurface(info); |
| 663 | if (!s) { |
| 664 | s = SkSurface::NewRaster(info); // Some canvases can't create surfaces. |
| 665 | } |
| 666 | surfaces.push(s); |
| 667 | SkCanvas* c = s->getCanvas(); |
| 668 | c->translate(SkIntToScalar(-i * fW), |
| 669 | SkIntToScalar(-j * fH)); // Line up the canvas with this tile. |
| 670 | mpd.add(c, fPic); |
| 671 | } |
| 672 | } |
| 673 | mpd.draw(); |
| 674 | for (int j = 0; j < yTiles; j++) { |
| 675 | for (int i = 0; i < xTiles; i++) { |
| 676 | SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot()); |
| 677 | canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH)); |
| 678 | } |
| 679 | } |
| 680 | surfaces.unrefAll(); |
| 681 | return ""; |
| 682 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 683 | SkISize size() const override { return fSize; } |
| 684 | Name name() const override { sk_throw(); return ""; } // No one should be calling this. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 685 | } proxy(fW, fH, pic, src.size()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 686 | return fSink->draw(proxy, bitmap, stream, log); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 687 | } |
| 688 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 689 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 690 | |
| 691 | ViaSecondPicture::ViaSecondPicture(Sink* sink) : fSink(sink) {} |
| 692 | |
| 693 | // Draw the Src into two pictures, then draw the second picture into the wrapped Sink. |
| 694 | // This tests that any shortcuts we may take while recording that second picture are legal. |
| 695 | Error ViaSecondPicture::draw( |
| 696 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 697 | struct ProxySrc : public Src { |
| 698 | const Src& fSrc; |
| 699 | ProxySrc(const Src& src) : fSrc(src) {} |
| 700 | |
| 701 | Error draw(SkCanvas* canvas) const override { |
| 702 | SkSize size; |
| 703 | size = fSrc.size(); |
| 704 | SkPictureRecorder recorder; |
| 705 | SkAutoTUnref<SkPicture> pic; |
| 706 | for (int i = 0; i < 2; i++) { |
| 707 | Error err = fSrc.draw(recorder.beginRecording(size.width(), size.height())); |
| 708 | if (!err.isEmpty()) { |
| 709 | return err; |
| 710 | } |
| 711 | pic.reset(recorder.endRecordingAsPicture()); |
| 712 | } |
| 713 | canvas->drawPicture(pic); |
| 714 | return ""; |
| 715 | } |
| 716 | SkISize size() const override { return fSrc.size(); } |
| 717 | Name name() const override { sk_throw(); return ""; } // No one should be calling this. |
| 718 | } proxy(src); |
| 719 | return fSink->draw(proxy, bitmap, stream, log); |
| 720 | } |
| 721 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 722 | } // namespace DM |