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