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