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