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