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