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