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