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