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" |
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 | e44b508 | 2015-05-07 10:53:34 -0700 | [diff] [blame] | 15 | #include "SkFunction.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" |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 29 | #include "SkScaledCodec.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 30 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 31 | DEFINE_bool(multiPage, false, "For document-type backends, render the source" |
| 32 | " into multiple pages"); |
| 33 | |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 34 | static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { |
| 35 | SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); |
| 36 | return encoded && SkInstallDiscardablePixelRef(encoded, dst); |
| 37 | } |
| 38 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 39 | namespace DM { |
| 40 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 41 | GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
| 42 | |
| 43 | Error GMSrc::draw(SkCanvas* canvas) const { |
| 44 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 45 | canvas->concat(gm->getInitialTransform()); |
| 46 | gm->draw(canvas); |
| 47 | return ""; |
| 48 | } |
| 49 | |
| 50 | SkISize GMSrc::size() const { |
| 51 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 52 | return gm->getISize(); |
| 53 | } |
| 54 | |
| 55 | Name GMSrc::name() const { |
| 56 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 57 | return gm->getName(); |
| 58 | } |
| 59 | |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 60 | void GMSrc::modifyGrContextOptions(GrContextOptions* options) const { |
| 61 | SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 62 | gm->modifyGrContextOptions(options); |
| 63 | } |
| 64 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 65 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 66 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 67 | CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 68 | : fPath(path) |
| 69 | , fMode(mode) |
| 70 | , fDstColorType(dstColorType) |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 71 | , fScale(scale) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 72 | {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 73 | |
mtklein | 99cab4e | 2015-07-31 06:43:04 -0700 | [diff] [blame] | 74 | bool CodecSrc::veto(SinkFlags flags) const { |
| 75 | // No need to test decoding to non-raster or indirect backend. |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 76 | // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferred decode to |
| 77 | // let the GPU handle it. |
mtklein | 99cab4e | 2015-07-31 06:43:04 -0700 | [diff] [blame] | 78 | return flags.type != SinkFlags::kRaster |
| 79 | || flags.approach != SinkFlags::kDirect; |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 80 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 81 | |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 82 | Error CodecSrc::draw(SkCanvas* canvas) const { |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 83 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 84 | if (!encoded) { |
| 85 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 86 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 87 | SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromData(encoded)); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 88 | if (NULL == codec.get()) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 89 | // scaledCodec not supported, try normal codec |
| 90 | codec.reset(SkCodec::NewFromData(encoded)); |
| 91 | if (NULL == codec.get()) { |
| 92 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| 93 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 94 | } |
| 95 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 96 | // Choose the color type to decode to |
| 97 | SkImageInfo decodeInfo = codec->getInfo(); |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 98 | SkColorType canvasColorType = canvas->imageInfo().colorType(); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 99 | switch (fDstColorType) { |
| 100 | case kIndex8_Always_DstColorType: |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 101 | decodeInfo = codec->getInfo().makeColorType(kIndex_8_SkColorType); |
| 102 | if (kRGB_565_SkColorType == canvasColorType) { |
| 103 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 104 | } |
| 105 | break; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 106 | case kGrayscale_Always_DstColorType: |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 107 | decodeInfo = codec->getInfo().makeColorType(kGray_8_SkColorType); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 108 | if (kRGB_565_SkColorType == canvasColorType) { |
| 109 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 110 | } |
| 111 | break; |
| 112 | default: |
| 113 | decodeInfo = decodeInfo.makeColorType(canvasColorType); |
| 114 | break; |
| 115 | } |
| 116 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 117 | // Try to scale the image if it is desired |
| 118 | SkISize size = codec->getScaledDimensions(fScale); |
| 119 | if (size == decodeInfo.dimensions() && 1.0f != fScale) { |
| 120 | return Error::Nonfatal("Test without scaling is uninteresting."); |
| 121 | } |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 122 | |
| 123 | // Visually inspecting very small output images is not necessary. We will |
| 124 | // cover these cases in unit testing. |
| 125 | if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) { |
| 126 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 127 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 128 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 129 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 130 | // Construct a color table for the decode if necessary |
| 131 | SkAutoTUnref<SkColorTable> colorTable(NULL); |
| 132 | SkPMColor* colorPtr = NULL; |
| 133 | int* colorCountPtr = NULL; |
| 134 | int maxColors = 256; |
| 135 | if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 136 | SkPMColor colors[256]; |
| 137 | colorTable.reset(SkNEW_ARGS(SkColorTable, (colors, maxColors))); |
| 138 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 139 | colorCountPtr = &maxColors; |
| 140 | } |
| 141 | |
| 142 | // FIXME: Currently we cannot draw unpremultiplied sources. |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 143 | if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) { |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 144 | decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType); |
| 145 | } |
| 146 | |
| 147 | SkBitmap bitmap; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 148 | if (!bitmap.tryAllocPixels(decodeInfo, NULL, colorTable.get())) { |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 149 | return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(), |
| 150 | decodeInfo.width(), decodeInfo.height()); |
| 151 | } |
| 152 | |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 153 | switch (fMode) { |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 154 | case kNormal_Mode: { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 155 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), NULL, |
| 156 | colorPtr, colorCountPtr)) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 157 | case SkCodec::kSuccess: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 158 | // We consider incomplete to be valid, since we should still decode what is |
| 159 | // available. |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 160 | case SkCodec::kIncompleteInput: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 161 | break; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 162 | case SkCodec::kInvalidConversion: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 163 | return Error::Nonfatal("Incompatible colortype conversion"); |
| 164 | default: |
| 165 | // Everything else is considered a failure. |
| 166 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 167 | } |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 168 | canvas->drawBitmap(bitmap, 0, 0); |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 169 | break; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 170 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 171 | case kScanline_Mode: { |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 172 | SkAutoTDelete<SkScanlineDecoder> scanlineDecoder( |
| 173 | SkScanlineDecoder::NewFromData(encoded)); |
| 174 | if (NULL == scanlineDecoder || SkCodec::kSuccess != |
| 175 | scanlineDecoder->start(decodeInfo, NULL, colorPtr, colorCountPtr)) { |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 176 | return Error::Nonfatal("Cannot use scanline decoder for all images"); |
| 177 | } |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 178 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 179 | const SkCodec::Result result = scanlineDecoder->getScanlines( |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 180 | bitmap.getAddr(0, 0), decodeInfo.height(), bitmap.rowBytes()); |
| 181 | switch (result) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 182 | case SkCodec::kSuccess: |
| 183 | case SkCodec::kIncompleteInput: |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 184 | break; |
| 185 | default: |
| 186 | return SkStringPrintf("%s failed with error message %d", |
| 187 | fPath.c_str(), (int) result); |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 188 | } |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 189 | canvas->drawBitmap(bitmap, 0, 0); |
| 190 | break; |
| 191 | } |
| 192 | case kScanline_Subset_Mode: { |
| 193 | //this mode decodes the image in divisor*divisor subsets, using a scanline decoder |
| 194 | const int divisor = 2; |
| 195 | const int w = decodeInfo.width(); |
| 196 | const int h = decodeInfo.height(); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 197 | if (divisor > w || divisor > h) { |
msarett | 7054257 | 2015-06-19 07:44:05 -0700 | [diff] [blame] | 198 | return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d is too big" |
| 199 | "for %s with dimensions (%d x %d)", divisor, fPath.c_str(), w, h)); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 200 | } |
| 201 | const int subsetWidth = w/divisor; |
| 202 | const int subsetHeight = h/divisor; |
| 203 | // One of our subsets will be larger to contain any pixels that do not divide evenly. |
| 204 | const int extraX = w % divisor; |
| 205 | const int extraY = h % divisor; |
| 206 | /* |
| 207 | * if w or h are not evenly divided by divisor need to adjust width and height of end |
| 208 | * subsets to cover entire image. |
| 209 | * Add extraX and extraY to largestSubsetBm's width and height to adjust width |
| 210 | * and height of end subsets. |
| 211 | * subsetBm is extracted from largestSubsetBm. |
| 212 | * subsetBm's size is determined based on the current subset and may be larger for end |
| 213 | * subsets. |
| 214 | */ |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 215 | SkImageInfo largestSubsetDecodeInfo = |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 216 | decodeInfo.makeWH(subsetWidth + extraX, subsetHeight + extraY); |
| 217 | SkBitmap largestSubsetBm; |
| 218 | if (!largestSubsetBm.tryAllocPixels(largestSubsetDecodeInfo, NULL, colorTable.get())) { |
| 219 | return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(), |
| 220 | largestSubsetDecodeInfo.width(), largestSubsetDecodeInfo.height()); |
| 221 | } |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 222 | const size_t rowBytes = decodeInfo.minRowBytes(); |
| 223 | char* buffer = SkNEW_ARRAY(char, largestSubsetDecodeInfo.height() * rowBytes); |
| 224 | SkAutoTDeleteArray<char> lineDeleter(buffer); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 225 | for (int col = 0; col < divisor; col++) { |
| 226 | //currentSubsetWidth may be larger than subsetWidth for rightmost subsets |
| 227 | const int currentSubsetWidth = (col + 1 == divisor) ? |
| 228 | subsetWidth + extraX : subsetWidth; |
| 229 | const int x = col * subsetWidth; |
| 230 | for (int row = 0; row < divisor; row++) { |
| 231 | //currentSubsetHeight may be larger than subsetHeight for bottom subsets |
| 232 | const int currentSubsetHeight = (row + 1 == divisor) ? |
| 233 | subsetHeight + extraY : subsetHeight; |
| 234 | const int y = row * subsetHeight; |
| 235 | //create scanline decoder for each subset |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 236 | SkAutoTDelete<SkScanlineDecoder> subsetScanlineDecoder( |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 237 | SkScanlineDecoder::NewFromData(encoded)); |
| 238 | if (NULL == subsetScanlineDecoder || SkCodec::kSuccess != |
| 239 | subsetScanlineDecoder->start( |
| 240 | decodeInfo, NULL, colorPtr, colorCountPtr)) |
| 241 | { |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 242 | if (x == 0 && y == 0) { |
| 243 | //first try, image may not be compatible |
| 244 | return Error::Nonfatal("Cannot use scanline decoder for all images"); |
| 245 | } else { |
| 246 | return "Error scanline decoder is NULL"; |
| 247 | } |
| 248 | } |
| 249 | //skip to first line of subset |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 250 | const SkCodec::Result skipResult = |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 251 | subsetScanlineDecoder->skipScanlines(y); |
| 252 | switch (skipResult) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 253 | case SkCodec::kSuccess: |
| 254 | case SkCodec::kIncompleteInput: |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 255 | break; |
| 256 | default: |
| 257 | return SkStringPrintf("%s failed after attempting to skip %d scanlines" |
| 258 | "with error message %d", fPath.c_str(), y, (int) skipResult); |
| 259 | } |
| 260 | //create and set size of subsetBm |
| 261 | SkBitmap subsetBm; |
| 262 | SkIRect bounds = SkIRect::MakeWH(subsetWidth, subsetHeight); |
| 263 | bounds.setXYWH(0, 0, currentSubsetWidth, currentSubsetHeight); |
| 264 | SkAssertResult(largestSubsetBm.extractSubset(&subsetBm, bounds)); |
| 265 | SkAutoLockPixels autlockSubsetBm(subsetBm, true); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 266 | const SkCodec::Result subsetResult = |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 267 | subsetScanlineDecoder->getScanlines(buffer, currentSubsetHeight, rowBytes); |
| 268 | switch (subsetResult) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 269 | case SkCodec::kSuccess: |
| 270 | case SkCodec::kIncompleteInput: |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 271 | break; |
| 272 | default: |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 273 | return SkStringPrintf("%s failed with error message %d", |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 274 | fPath.c_str(), (int) subsetResult); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 275 | } |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 276 | const size_t bpp = decodeInfo.bytesPerPixel(); |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 277 | /* |
| 278 | * we copy all the lines at once becuase when calling getScanlines for |
| 279 | * interlaced pngs the entire image must be read regardless of the number |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 280 | * of lines requested. Reading an interlaced png in a loop, line-by-line, would |
| 281 | * decode the entire image height times, which is very slow |
| 282 | * it is aknowledged that copying each line as you read it in a loop |
| 283 | * may be faster for other types of images. Since this is a correctness test |
| 284 | * that's okay. |
| 285 | */ |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 286 | char* bufferRow = buffer; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 287 | for (int subsetY = 0; subsetY < currentSubsetHeight; ++subsetY) { |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 288 | memcpy(subsetBm.getAddr(0, subsetY), bufferRow + x*bpp, |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 289 | currentSubsetWidth*bpp); |
| 290 | bufferRow += rowBytes; |
| 291 | } |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 292 | |
scroggo | 4358f13 | 2015-07-30 11:33:04 -0700 | [diff] [blame] | 293 | subsetBm.notifyPixelsChanged(); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 294 | canvas->drawBitmap(subsetBm, SkIntToScalar(x), SkIntToScalar(y)); |
| 295 | } |
| 296 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 297 | break; |
| 298 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 299 | case kStripe_Mode: { |
| 300 | const int height = decodeInfo.height(); |
| 301 | // This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 302 | // does not align with image blocks. |
| 303 | const int stripeHeight = 37; |
| 304 | const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
| 305 | |
| 306 | // Decode odd stripes |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 307 | SkAutoTDelete<SkScanlineDecoder> decoder(SkScanlineDecoder::NewFromData(encoded)); |
| 308 | if (NULL == decoder || SkCodec::kSuccess != |
| 309 | decoder->start(decodeInfo, NULL, colorPtr, colorCountPtr)) { |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 310 | return Error::Nonfatal("Cannot use scanline decoder for all images"); |
| 311 | } |
| 312 | for (int i = 0; i < numStripes; i += 2) { |
| 313 | // Skip a stripe |
| 314 | const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 315 | SkCodec::Result result = decoder->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 316 | switch (result) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 317 | case SkCodec::kSuccess: |
| 318 | case SkCodec::kIncompleteInput: |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 319 | break; |
| 320 | default: |
| 321 | return SkStringPrintf("Cannot skip scanlines for %s.", fPath.c_str()); |
| 322 | } |
| 323 | |
| 324 | // Read a stripe |
| 325 | const int startY = (i + 1) * stripeHeight; |
| 326 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 327 | if (linesToRead > 0) { |
| 328 | result = decoder->getScanlines(bitmap.getAddr(0, startY), |
| 329 | linesToRead, bitmap.rowBytes()); |
| 330 | switch (result) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 331 | case SkCodec::kSuccess: |
| 332 | case SkCodec::kIncompleteInput: |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 333 | break; |
| 334 | default: |
| 335 | return SkStringPrintf("Cannot get scanlines for %s.", fPath.c_str()); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Decode even stripes |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 341 | const SkCodec::Result startResult = decoder->start(decodeInfo, NULL, colorPtr, |
| 342 | colorCountPtr); |
| 343 | if (SkCodec::kSuccess != startResult) { |
| 344 | return "Failed to restart scanline decoder with same parameters."; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 345 | } |
| 346 | for (int i = 0; i < numStripes; i += 2) { |
| 347 | // Read a stripe |
| 348 | const int startY = i * stripeHeight; |
| 349 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 350 | SkCodec::Result result = decoder->getScanlines(bitmap.getAddr(0, startY), |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 351 | linesToRead, bitmap.rowBytes()); |
| 352 | switch (result) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 353 | case SkCodec::kSuccess: |
| 354 | case SkCodec::kIncompleteInput: |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 355 | break; |
| 356 | default: |
| 357 | return SkStringPrintf("Cannot get scanlines for %s.", fPath.c_str()); |
| 358 | } |
| 359 | |
| 360 | // Skip a stripe |
msarett | f6db27e | 2015-06-12 09:34:04 -0700 | [diff] [blame] | 361 | const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); |
| 362 | if (linesToSkip > 0) { |
| 363 | result = decoder->skipScanlines(linesToSkip); |
| 364 | switch (result) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 365 | case SkCodec::kSuccess: |
| 366 | case SkCodec::kIncompleteInput: |
msarett | f6db27e | 2015-06-12 09:34:04 -0700 | [diff] [blame] | 367 | break; |
| 368 | default: |
| 369 | return SkStringPrintf("Cannot skip scanlines for %s.", fPath.c_str()); |
| 370 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | canvas->drawBitmap(bitmap, 0, 0); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 374 | break; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 375 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 376 | case kSubset_Mode: { |
| 377 | // Arbitrarily choose a divisor. |
| 378 | int divisor = 2; |
| 379 | // Total width/height of the image. |
| 380 | const int W = codec->getInfo().width(); |
| 381 | const int H = codec->getInfo().height(); |
| 382 | if (divisor > W || divisor > H) { |
| 383 | return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big " |
| 384 | "for %s with dimensions (%d x %d)", divisor, |
| 385 | fPath.c_str(), W, H)); |
| 386 | } |
| 387 | // subset dimensions |
| 388 | // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 389 | const int w = SkAlign2(W / divisor); |
| 390 | const int h = SkAlign2(H / divisor); |
| 391 | SkIRect subset; |
| 392 | SkCodec::Options opts; |
| 393 | opts.fSubset = ⊂ |
| 394 | SkBitmap subsetBm; |
| 395 | // We will reuse pixel memory from bitmap. |
| 396 | void* pixels = bitmap.getPixels(); |
| 397 | // Keep track of left and top (for drawing subsetBm into canvas). We could use |
| 398 | // fScale * x and fScale * y, but we want integers such that the next subset will start |
| 399 | // where the last one ended. So we'll add decodeInfo.width() and height(). |
| 400 | int left = 0; |
| 401 | for (int x = 0; x < W; x += w) { |
| 402 | int top = 0; |
| 403 | for (int y = 0; y < H; y+= h) { |
| 404 | // Do not make the subset go off the edge of the image. |
| 405 | const int preScaleW = SkTMin(w, W - x); |
| 406 | const int preScaleH = SkTMin(h, H - y); |
| 407 | subset.setXYWH(x, y, preScaleW, preScaleH); |
| 408 | // And scale |
| 409 | // FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 410 | // into account? |
| 411 | decodeInfo = decodeInfo.makeWH(SkScalarRoundToInt(preScaleW * fScale), |
| 412 | SkScalarRoundToInt(preScaleH * fScale)); |
| 413 | size_t rowBytes = decodeInfo.minRowBytes(); |
| 414 | if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(), |
| 415 | NULL, NULL)) { |
| 416 | return SkStringPrintf("could not install pixels for %s.", fPath.c_str()); |
| 417 | } |
| 418 | const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes, |
| 419 | &opts, colorPtr, colorCountPtr); |
| 420 | switch (result) { |
| 421 | case SkCodec::kSuccess: |
| 422 | case SkCodec::kIncompleteInput: |
| 423 | break; |
| 424 | case SkCodec::kInvalidConversion: |
| 425 | if (0 == (x|y)) { |
| 426 | // First subset is okay to return unimplemented. |
| 427 | return Error::Nonfatal("Incompatible colortype conversion"); |
| 428 | } |
| 429 | // If the first subset succeeded, a later one should not fail. |
| 430 | // fall through to failure |
| 431 | case SkCodec::kUnimplemented: |
| 432 | if (0 == (x|y)) { |
| 433 | // First subset is okay to return unimplemented. |
| 434 | return Error::Nonfatal("subset codec not supported"); |
| 435 | } |
| 436 | // If the first subset succeeded, why would a later one fail? |
| 437 | // fall through to failure |
| 438 | default: |
| 439 | return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) " |
| 440 | "from %s with dimensions (%d x %d)\t error %d", |
| 441 | x, y, decodeInfo.width(), decodeInfo.height(), |
| 442 | fPath.c_str(), W, H, result); |
| 443 | } |
| 444 | canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top)); |
| 445 | // translate by the scaled height. |
| 446 | top += decodeInfo.height(); |
| 447 | } |
| 448 | // translate by the scaled width. |
| 449 | left += decodeInfo.width(); |
| 450 | } |
| 451 | return ""; |
| 452 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 453 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 454 | return ""; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | SkISize CodecSrc::size() const { |
| 458 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 459 | SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromData(encoded)); |
| 460 | if (NULL == codec) { |
| 461 | // scaledCodec not supported, try regular codec |
| 462 | codec.reset(SkCodec::NewFromData(encoded)); |
| 463 | if (NULL == codec) { |
| 464 | return SkISize::Make(0, 0); |
| 465 | } |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 466 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 467 | SkISize size = codec->getScaledDimensions(fScale); |
| 468 | return size; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | Name CodecSrc::name() const { |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 472 | if (1.0f == fScale) { |
| 473 | return SkOSPath::Basename(fPath.c_str()); |
| 474 | } else { |
| 475 | return SkStringPrintf("%s_%.3f", SkOSPath::Basename(fPath.c_str()).c_str(), fScale); |
| 476 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 480 | |
| 481 | ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
| 482 | |
mtklein | 99cab4e | 2015-07-31 06:43:04 -0700 | [diff] [blame] | 483 | bool ImageSrc::veto(SinkFlags flags) const { |
| 484 | // No need to test decoding to non-raster or indirect backend. |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 485 | // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YUV. |
mtklein | 99cab4e | 2015-07-31 06:43:04 -0700 | [diff] [blame] | 486 | return flags.type != SinkFlags::kRaster |
| 487 | || flags.approach != SinkFlags::kDirect; |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 488 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 489 | |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 490 | Error ImageSrc::draw(SkCanvas* canvas) const { |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 491 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 492 | if (!encoded) { |
| 493 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 494 | } |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 495 | const SkColorType dstColorType = canvas->imageInfo().colorType(); |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 496 | if (fDivisor == 0) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 497 | // Decode the full image. |
| 498 | SkBitmap bitmap; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 499 | if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap, |
| 500 | dstColorType, SkImageDecoder::kDecodePixels_Mode)) { |
| 501 | return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
| 502 | } |
| 503 | if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) { |
| 504 | // Do not draw a bitmap with alpha to a destination without alpha. |
| 505 | return Error::Nonfatal("Uninteresting to decode image with alpha into 565."); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 506 | } |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 507 | 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] | 508 | canvas->drawBitmap(bitmap, 0,0); |
| 509 | return ""; |
| 510 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 511 | // Decode subsets. This is a little involved. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 512 | SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
| 513 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 514 | if (!decoder) { |
| 515 | return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()); |
| 516 | } |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 517 | stream->rewind(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 518 | int w,h; |
msarett | 7054257 | 2015-06-19 07:44:05 -0700 | [diff] [blame] | 519 | if (!decoder->buildTileIndex(stream.detach(), &w, &h)) { |
mtklein | 4089ef7 | 2015-03-05 08:40:28 -0800 | [diff] [blame] | 520 | return Error::Nonfatal("Subset decoding not supported."); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 521 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 522 | |
| 523 | // Divide the image into subsets that cover the entire image. |
| 524 | if (fDivisor > w || fDivisor > h) { |
msarett | 7054257 | 2015-06-19 07:44:05 -0700 | [diff] [blame] | 525 | return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d is too big" |
| 526 | "for %s with dimensions (%d x %d)", fDivisor, fPath.c_str(), w, h)); |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 527 | } |
| 528 | const int subsetWidth = w / fDivisor, |
| 529 | subsetHeight = h / fDivisor; |
| 530 | for (int y = 0; y < h; y += subsetHeight) { |
| 531 | for (int x = 0; x < w; x += subsetWidth) { |
| 532 | SkBitmap subset; |
| 533 | SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight); |
| 534 | if (!decoder->decodeSubset(&subset, rect, dstColorType)) { |
| 535 | return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).", |
| 536 | x, y, x+subsetWidth, y+subsetHeight); |
| 537 | } |
scroggo | 56e25dd | 2015-03-05 11:46:40 -0800 | [diff] [blame] | 538 | if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) { |
| 539 | // Do not draw a bitmap with alpha to a destination without alpha. |
| 540 | // This is not an error, but there is nothing interesting to show. |
| 541 | |
| 542 | // This should only happen on the first iteration through the loop. |
| 543 | SkASSERT(0 == x && 0 == y); |
| 544 | |
| 545 | return Error::Nonfatal("Uninteresting to decode image with alpha into 565."); |
| 546 | } |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 547 | canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 548 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 549 | } |
| 550 | return ""; |
| 551 | } |
| 552 | |
| 553 | SkISize ImageSrc::size() const { |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 554 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 555 | SkBitmap bitmap; |
| 556 | if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), |
| 557 | encoded->size(), |
| 558 | &bitmap, |
| 559 | kUnknown_SkColorType, |
| 560 | SkImageDecoder::kDecodeBounds_Mode)) { |
| 561 | return SkISize::Make(0,0); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 562 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 563 | return bitmap.dimensions(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 564 | } |
| 565 | |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 566 | Name ImageSrc::name() const { |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 567 | return SkOSPath::Basename(fPath.c_str()); |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 568 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 569 | |
| 570 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 571 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 572 | static const SkRect kSKPViewport = {0,0, 1000,1000}; |
| 573 | |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 574 | SKPSrc::SKPSrc(Path path) : fPath(path) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 575 | |
| 576 | Error SKPSrc::draw(SkCanvas* canvas) const { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 577 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 578 | if (!stream) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 579 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 580 | } |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 581 | SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap)); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 582 | if (!pic) { |
| 583 | return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str()); |
| 584 | } |
| 585 | 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] | 586 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 587 | canvas->clipRect(kSKPViewport); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 588 | canvas->drawPicture(pic); |
| 589 | return ""; |
| 590 | } |
| 591 | |
| 592 | SkISize SKPSrc::size() const { |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 593 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
| 594 | if (!stream) { |
| 595 | return SkISize::Make(0,0); |
| 596 | } |
| 597 | SkPictInfo info; |
| 598 | if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) { |
| 599 | return SkISize::Make(0,0); |
| 600 | } |
| 601 | SkRect viewport = kSKPViewport; |
| 602 | if (!viewport.intersect(info.fCullRect)) { |
| 603 | return SkISize::Make(0,0); |
| 604 | } |
| 605 | return viewport.roundOut().size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 609 | |
| 610 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 611 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 612 | Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const { |
| 613 | SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas()); |
| 614 | return src.draw(canvas); |
| 615 | } |
| 616 | |
| 617 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 618 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 619 | DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?"); |
| 620 | |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 621 | GPUSink::GPUSink(GrContextFactory::GLContextType ct, |
| 622 | GrGLStandard api, |
| 623 | int samples, |
| 624 | bool dfText, |
| 625 | bool threaded) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 626 | : fContextType(ct) |
| 627 | , fGpuAPI(api) |
| 628 | , fSampleCount(samples) |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 629 | , fUseDFText(dfText) |
| 630 | , fThreaded(threaded) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 631 | |
| 632 | int GPUSink::enclave() const { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 633 | return fThreaded ? kAnyThread_Enclave : kGPU_Enclave; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 634 | } |
| 635 | |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 636 | void PreAbandonGpuContextErrorHandler(SkError, void*) {} |
| 637 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 638 | Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const { |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 639 | GrContextOptions options; |
| 640 | src.modifyGrContextOptions(&options); |
| 641 | |
| 642 | GrContextFactory factory(options); |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 643 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 644 | const SkImageInfo info = |
| 645 | SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType); |
| 646 | SkAutoTUnref<SkSurface> surface( |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 647 | NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 648 | if (!surface) { |
| 649 | return "Could not create a surface."; |
| 650 | } |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 651 | if (FLAGS_preAbandonGpuContext) { |
| 652 | SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, NULL); |
| 653 | factory.abandonContexts(); |
| 654 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 655 | SkCanvas* canvas = surface->getCanvas(); |
| 656 | Error err = src.draw(canvas); |
| 657 | if (!err.isEmpty()) { |
| 658 | return err; |
| 659 | } |
| 660 | canvas->flush(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 661 | if (FLAGS_gpuStats) { |
| 662 | canvas->getGrContext()->dumpCacheStats(log); |
| 663 | canvas->getGrContext()->dumpGpuStats(log); |
| 664 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 665 | dst->allocPixels(info); |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 666 | canvas->readPixels(dst, 0, 0); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 667 | if (FLAGS_abandonGpuContext) { |
| 668 | factory.abandonContexts(); |
| 669 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 670 | return ""; |
| 671 | } |
| 672 | |
| 673 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 674 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 675 | static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) { |
| 676 | // Print the given DM:Src to a document, breaking on 8.5x11 pages. |
| 677 | SkASSERT(doc); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 678 | int width = src.size().width(), |
| 679 | height = src.size().height(); |
| 680 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 681 | if (FLAGS_multiPage) { |
| 682 | const int kLetterWidth = 612, // 8.5 * 72 |
| 683 | kLetterHeight = 792; // 11 * 72 |
| 684 | const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth), |
| 685 | SkIntToScalar(kLetterHeight)); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 686 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 687 | int xPages = ((width - 1) / kLetterWidth) + 1; |
| 688 | int yPages = ((height - 1) / kLetterHeight) + 1; |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 689 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 690 | for (int y = 0; y < yPages; ++y) { |
| 691 | for (int x = 0; x < xPages; ++x) { |
| 692 | int w = SkTMin(kLetterWidth, width - (x * kLetterWidth)); |
| 693 | int h = SkTMin(kLetterHeight, height - (y * kLetterHeight)); |
| 694 | SkCanvas* canvas = |
| 695 | doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)); |
| 696 | if (!canvas) { |
| 697 | return "SkDocument::beginPage(w,h) returned NULL"; |
| 698 | } |
| 699 | canvas->clipRect(letter); |
| 700 | canvas->translate(-letter.width() * x, -letter.height() * y); |
| 701 | Error err = src.draw(canvas); |
| 702 | if (!err.isEmpty()) { |
| 703 | return err; |
| 704 | } |
| 705 | doc->endPage(); |
djsollen | 2ab9000 | 2015-04-03 06:38:31 -0700 | [diff] [blame] | 706 | } |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 707 | } |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 708 | } else { |
| 709 | SkCanvas* canvas = |
| 710 | doc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); |
| 711 | if (!canvas) { |
| 712 | return "SkDocument::beginPage(w,h) returned NULL"; |
| 713 | } |
| 714 | Error err = src.draw(canvas); |
| 715 | if (!err.isEmpty()) { |
| 716 | return err; |
| 717 | } |
| 718 | doc->endPage(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 719 | } |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 720 | if (!doc->close()) { |
| 721 | return "SkDocument::close() returned false"; |
| 722 | } |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 723 | dst->flush(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 724 | return ""; |
| 725 | } |
| 726 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 727 | PDFSink::PDFSink() {} |
| 728 | |
| 729 | Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 730 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst)); |
| 731 | if (!doc) { |
| 732 | return "SkDocument::CreatePDF() returned NULL"; |
| 733 | } |
| 734 | return draw_skdocument(src, doc.get(), dst); |
| 735 | } |
| 736 | |
| 737 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 738 | |
| 739 | XPSSink::XPSSink() {} |
| 740 | |
| 741 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 742 | SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst)); |
| 743 | if (!doc) { |
| 744 | return "SkDocument::CreateXPS() returned NULL"; |
| 745 | } |
| 746 | return draw_skdocument(src, doc.get(), dst); |
| 747 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 748 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 749 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 750 | SKPSink::SKPSink() {} |
| 751 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 752 | Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 753 | SkSize size; |
| 754 | size = src.size(); |
| 755 | SkPictureRecorder recorder; |
| 756 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 757 | if (!err.isEmpty()) { |
| 758 | return err; |
| 759 | } |
| 760 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 761 | pic->serialize(dst); |
| 762 | return ""; |
| 763 | } |
| 764 | |
| 765 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 766 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 767 | SVGSink::SVGSink() {} |
| 768 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 769 | Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 770 | SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst))); |
| 771 | SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create( |
| 772 | SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())), |
| 773 | xmlWriter)); |
| 774 | return src.draw(canvas); |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 778 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 779 | RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {} |
| 780 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 781 | Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 782 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 783 | // If there's an appropriate alpha type for this color type, use it, otherwise use premul. |
| 784 | SkAlphaType alphaType = kPremul_SkAlphaType; |
| 785 | (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType); |
| 786 | |
| 787 | dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType)); |
| 788 | dst->eraseColor(SK_ColorTRANSPARENT); |
| 789 | SkCanvas canvas(*dst); |
| 790 | return src.draw(&canvas); |
| 791 | } |
| 792 | |
| 793 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 794 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 795 | // Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(), |
mtklein | e44b508 | 2015-05-07 10:53:34 -0700 | [diff] [blame] | 796 | // passing the Sink draw() arguments, a size, and a function draws into an SkCanvas. |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 797 | // Several examples below. |
| 798 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 799 | static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log, |
mtklein | e44b508 | 2015-05-07 10:53:34 -0700 | [diff] [blame] | 800 | SkISize size, SkFunction<Error(SkCanvas*)> draw) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 801 | class ProxySrc : public Src { |
| 802 | public: |
mtklein | e44b508 | 2015-05-07 10:53:34 -0700 | [diff] [blame] | 803 | ProxySrc(SkISize size, SkFunction<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {} |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 804 | Error draw(SkCanvas* canvas) const override { return fDraw(canvas); } |
| 805 | Name name() const override { sk_throw(); return ""; } // Won't be called. |
| 806 | SkISize size() const override { return fSize; } |
| 807 | private: |
mtklein | e44b508 | 2015-05-07 10:53:34 -0700 | [diff] [blame] | 808 | SkISize fSize; |
| 809 | SkFunction<Error(SkCanvas*)> fDraw; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 810 | }; |
| 811 | return sink->draw(ProxySrc(size, draw), bitmap, stream, log); |
| 812 | } |
| 813 | |
| 814 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 815 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 816 | static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) { |
| 817 | SkRect bounds = SkRect::MakeIWH(srcW, srcH); |
| 818 | matrix->mapRect(&bounds); |
| 819 | matrix->postTranslate(-bounds.x(), -bounds.y()); |
| 820 | return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())); |
| 821 | } |
| 822 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 823 | ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 824 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 825 | Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 826 | SkMatrix matrix = fMatrix; |
| 827 | SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height()); |
| 828 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) { |
| 829 | canvas->concat(matrix); |
| 830 | return src.draw(canvas); |
| 831 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 832 | } |
| 833 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 834 | // Undoes any flip or 90 degree rotate without changing the scale of the bitmap. |
| 835 | // This should be pixel-preserving. |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 836 | ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 837 | |
| 838 | Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 839 | Error err = fSink->draw(src, bitmap, stream, log); |
| 840 | if (!err.isEmpty()) { |
| 841 | return err; |
| 842 | } |
| 843 | |
| 844 | SkMatrix inverse; |
| 845 | if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) { |
| 846 | return "Cannot upright --matrix."; |
| 847 | } |
| 848 | SkMatrix upright = SkMatrix::I(); |
| 849 | upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX())); |
| 850 | upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY())); |
| 851 | upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX())); |
| 852 | upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY())); |
| 853 | |
| 854 | SkBitmap uprighted; |
| 855 | SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height()); |
| 856 | uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height())); |
| 857 | |
| 858 | SkCanvas canvas(uprighted); |
| 859 | canvas.concat(upright); |
| 860 | SkPaint paint; |
| 861 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 862 | canvas.drawBitmap(*bitmap, 0, 0, &paint); |
| 863 | |
| 864 | *bitmap = uprighted; |
| 865 | bitmap->lockPixels(); |
| 866 | return ""; |
| 867 | } |
| 868 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 869 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 870 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 871 | Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 872 | auto size = src.size(); |
| 873 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) { |
| 874 | PipeController controller(canvas, &SkImageDecoder::DecodeMemory); |
| 875 | SkGPipeWriter pipe; |
reed | 451af50 | 2015-08-19 08:18:04 -0700 | [diff] [blame] | 876 | const uint32_t kFlags = 0; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 877 | return src.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height())); |
| 878 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 882 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 883 | Error ViaSerialization::draw( |
| 884 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 885 | // Record our Src into a picture. |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 886 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 887 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 888 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 889 | SkIntToScalar(size.height()))); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 890 | if (!err.isEmpty()) { |
| 891 | return err; |
| 892 | } |
| 893 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 894 | |
| 895 | // Serialize it and then deserialize it. |
| 896 | SkDynamicMemoryWStream wStream; |
| 897 | pic->serialize(&wStream); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 898 | SkAutoTDelete<SkStream> rStream(wStream.detachAsStream()); |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 899 | SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 900 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 901 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) { |
| 902 | canvas->drawPicture(deserialized); |
| 903 | return ""; |
| 904 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 908 | |
| 909 | ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 910 | : Via(sink) |
| 911 | , fW(w) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 912 | , fH(h) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 913 | , fFactory(factory) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 914 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 915 | Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 916 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 917 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 918 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 919 | SkIntToScalar(size.height()), |
| 920 | fFactory.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 921 | if (!err.isEmpty()) { |
| 922 | return err; |
| 923 | } |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 924 | SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 925 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 926 | return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) { |
| 927 | const int xTiles = (size.width() + fW - 1) / fW, |
| 928 | yTiles = (size.height() + fH - 1) / fH; |
| 929 | SkMultiPictureDraw mpd(xTiles*yTiles); |
| 930 | SkTDArray<SkSurface*> surfaces; |
| 931 | surfaces.setReserve(xTiles*yTiles); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 932 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 933 | SkImageInfo info = canvas->imageInfo().makeWH(fW, fH); |
| 934 | for (int j = 0; j < yTiles; j++) { |
| 935 | for (int i = 0; i < xTiles; i++) { |
| 936 | // This lets our ultimate Sink determine the best kind of surface. |
| 937 | // E.g., if it's a GpuSink, the surfaces and images are textures. |
| 938 | SkSurface* s = canvas->newSurface(info); |
| 939 | if (!s) { |
| 940 | s = SkSurface::NewRaster(info); // Some canvases can't create surfaces. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 941 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 942 | surfaces.push(s); |
| 943 | SkCanvas* c = s->getCanvas(); |
| 944 | c->translate(SkIntToScalar(-i * fW), |
| 945 | SkIntToScalar(-j * fH)); // Line up the canvas with this tile. |
| 946 | mpd.add(c, pic); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 947 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 948 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 949 | mpd.draw(); |
| 950 | for (int j = 0; j < yTiles; j++) { |
| 951 | for (int i = 0; i < xTiles; i++) { |
| 952 | SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot()); |
| 953 | canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH)); |
| 954 | } |
| 955 | } |
| 956 | surfaces.unrefAll(); |
| 957 | return ""; |
| 958 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 959 | } |
| 960 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 961 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 962 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 963 | // Draw the Src into two pictures, then draw the second picture into the wrapped Sink. |
| 964 | // This tests that any shortcuts we may take while recording that second picture are legal. |
| 965 | Error ViaSecondPicture::draw( |
| 966 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 967 | auto size = src.size(); |
| 968 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
| 969 | SkPictureRecorder recorder; |
| 970 | SkAutoTUnref<SkPicture> pic; |
| 971 | for (int i = 0; i < 2; i++) { |
| 972 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 973 | SkIntToScalar(size.height()))); |
| 974 | if (!err.isEmpty()) { |
| 975 | return err; |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 976 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 977 | pic.reset(recorder.endRecordingAsPicture()); |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 978 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 979 | canvas->drawPicture(pic); |
| 980 | return ""; |
| 981 | }); |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 982 | } |
| 983 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 984 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 985 | |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 986 | // Draw the Src twice. This can help exercise caching. |
| 987 | Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 988 | return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error { |
| 989 | for (int i = 0; i < 2; i++) { |
| 990 | SkAutoCanvasRestore acr(canvas, true/*save now*/); |
| 991 | canvas->clear(SK_ColorTRANSPARENT); |
| 992 | Error err = src.draw(canvas); |
| 993 | if (err.isEmpty()) { |
| 994 | return err; |
| 995 | } |
| 996 | } |
| 997 | return ""; |
| 998 | }); |
| 999 | } |
| 1000 | |
| 1001 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1002 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1003 | // This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas. |
| 1004 | // Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op. |
| 1005 | // This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures. |
| 1006 | struct DrawsAsSingletonPictures { |
| 1007 | SkCanvas* fCanvas; |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1008 | const SkDrawableList& fDrawables; |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1009 | |
| 1010 | SK_CREATE_MEMBER_DETECTOR(paint); |
| 1011 | |
| 1012 | template <typename T> |
| 1013 | void draw(const T& op, SkCanvas* canvas) { |
| 1014 | // We must pass SkMatrix::I() as our initial matrix. |
| 1015 | // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix, |
| 1016 | // which would have the funky effect of applying transforms over and over. |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1017 | SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I()); |
| 1018 | d(op); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | // Most things that have paints are Draw-type ops. Create sub-pictures for each. |
| 1022 | template <typename T> |
| 1023 | SK_WHEN(HasMember_paint<T>, void) operator()(const T& op) { |
| 1024 | SkPictureRecorder rec; |
| 1025 | this->draw(op, rec.beginRecording(SkRect::MakeLargest())); |
| 1026 | SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture()); |
| 1027 | fCanvas->drawPicture(pic); |
| 1028 | } |
| 1029 | |
| 1030 | // If you don't have a paint or are a SaveLayer, you're not a Draw-type op. |
| 1031 | // We cannot make subpictures out of these because they affect state. Draw them directly. |
| 1032 | template <typename T> |
| 1033 | SK_WHEN(!HasMember_paint<T>, void) operator()(const T& op) { this->draw(op, fCanvas); } |
| 1034 | void operator()(const SkRecords::SaveLayer& op) { this->draw(op, fCanvas); } |
| 1035 | }; |
| 1036 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1037 | // Record Src into a picture, then record it into a macro picture with a sub-picture for each draw. |
| 1038 | // Then play back that macro picture into our wrapped sink. |
| 1039 | Error ViaSingletonPictures::draw( |
| 1040 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1041 | auto size = src.size(); |
| 1042 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
| 1043 | // Use low-level (Skia-private) recording APIs so we can read the SkRecord. |
| 1044 | SkRecord skr; |
| 1045 | SkRecorder recorder(&skr, size.width(), size.height()); |
| 1046 | Error err = src.draw(&recorder); |
| 1047 | if (!err.isEmpty()) { |
| 1048 | return err; |
| 1049 | } |
| 1050 | |
| 1051 | // Record our macro-picture, with each draw op as its own sub-picture. |
| 1052 | SkPictureRecorder macroRec; |
| 1053 | SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()), |
| 1054 | SkIntToScalar(size.height())); |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1055 | |
| 1056 | SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList()); |
| 1057 | const SkDrawableList empty; |
| 1058 | |
| 1059 | DrawsAsSingletonPictures drawsAsSingletonPictures = { |
| 1060 | macroCanvas, |
| 1061 | drawables ? *drawables : empty, |
| 1062 | }; |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame^] | 1063 | for (int i = 0; i < skr.count(); i++) { |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1064 | skr.visit<void>(i, drawsAsSingletonPictures); |
| 1065 | } |
| 1066 | SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
| 1067 | |
| 1068 | canvas->drawPicture(macroPic); |
| 1069 | return ""; |
| 1070 | }); |
| 1071 | } |
| 1072 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1073 | } // namespace DM |