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" |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 11 | #include "SkCodecImageGenerator.h" |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 12 | #include "SkCommonFlags.h" |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 13 | #include "SkData.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 14 | #include "SkDocument.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 15 | #include "SkError.h" |
mtklein | b3e5e4d | 2015-03-25 13:13:43 -0700 | [diff] [blame] | 16 | #include "SkImageGenerator.h" |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 17 | #include "SkImageGeneratorCG.h" |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 18 | #include "SkImageGeneratorWIC.h" |
mtklein | c8be09a | 2016-01-04 18:56:57 -0800 | [diff] [blame] | 19 | #include "SkMallocPixelRef.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 20 | #include "SkMultiPictureDraw.h" |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 21 | #include "SkNullCanvas.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 22 | #include "SkOSFile.h" |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 23 | #include "SkOpts.h" |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 24 | #include "SkPictureData.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 25 | #include "SkPictureRecorder.h" |
| 26 | #include "SkRandom.h" |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 27 | #include "SkRecordDraw.h" |
| 28 | #include "SkRecorder.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 29 | #include "SkSVGCanvas.h" |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 30 | #include "SkStream.h" |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 31 | #include "SkTLogic.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 32 | #include "SkXMLWriter.h" |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 33 | #include "SkSwizzler.h" |
mtklein | 6459352 | 2015-11-12 10:41:05 -0800 | [diff] [blame] | 34 | #include <functional> |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 35 | |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 36 | #if defined(SK_BUILD_FOR_WIN) |
| 37 | #include "SkAutoCoInitialize.h" |
| 38 | #endif |
| 39 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 40 | DEFINE_bool(multiPage, false, "For document-type backends, render the source" |
| 41 | " into multiple pages"); |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 42 | DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?"); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 43 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 44 | using sk_gpu_test::GrContextFactory; |
| 45 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 46 | namespace DM { |
| 47 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 48 | GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
| 49 | |
| 50 | Error GMSrc::draw(SkCanvas* canvas) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 52 | canvas->concat(gm->getInitialTransform()); |
| 53 | gm->draw(canvas); |
| 54 | return ""; |
| 55 | } |
| 56 | |
| 57 | SkISize GMSrc::size() 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->getISize(); |
| 60 | } |
| 61 | |
| 62 | Name GMSrc::name() const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 64 | return gm->getName(); |
| 65 | } |
| 66 | |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 67 | void GMSrc::modifyGrContextOptions(GrContextOptions* options) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr)); |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 69 | gm->modifyGrContextOptions(options); |
| 70 | } |
| 71 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 72 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 73 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 74 | BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode, |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 75 | CodecSrc::DstColorType dstColorType, uint32_t sampleSize) |
| 76 | : fPath(path) |
| 77 | , fStrategy(strategy) |
| 78 | , fMode(mode) |
| 79 | , fDstColorType(dstColorType) |
| 80 | , fSampleSize(sampleSize) |
| 81 | {} |
| 82 | |
| 83 | bool BRDSrc::veto(SinkFlags flags) const { |
| 84 | // No need to test to non-raster or indirect backends. |
| 85 | return flags.type != SinkFlags::kRaster |
| 86 | || flags.approach != SinkFlags::kDirect; |
| 87 | } |
| 88 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 89 | static SkBitmapRegionDecoder* create_brd(Path path, |
| 90 | SkBitmapRegionDecoder::Strategy strategy) { |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 91 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); |
| 92 | if (!encoded) { |
| 93 | return NULL; |
| 94 | } |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 95 | return SkBitmapRegionDecoder::Create(encoded, strategy); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | Error BRDSrc::draw(SkCanvas* canvas) const { |
| 99 | SkColorType colorType = canvas->imageInfo().colorType(); |
| 100 | if (kRGB_565_SkColorType == colorType && |
| 101 | CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) { |
| 102 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 103 | } |
| 104 | switch (fDstColorType) { |
| 105 | case CodecSrc::kGetFromCanvas_DstColorType: |
| 106 | break; |
| 107 | case CodecSrc::kIndex8_Always_DstColorType: |
| 108 | colorType = kIndex_8_SkColorType; |
| 109 | break; |
| 110 | case CodecSrc::kGrayscale_Always_DstColorType: |
| 111 | colorType = kGray_8_SkColorType; |
| 112 | break; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 113 | default: |
| 114 | SkASSERT(false); |
| 115 | break; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 116 | } |
| 117 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 118 | SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 119 | if (nullptr == brd.get()) { |
| 120 | return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str())); |
| 121 | } |
| 122 | |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 123 | if (!brd->conversionSupported(colorType)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 124 | return Error::Nonfatal("Cannot convert to color type."); |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 125 | } |
| 126 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 127 | const uint32_t width = brd->width(); |
| 128 | const uint32_t height = brd->height(); |
| 129 | // Visually inspecting very small output images is not necessary. |
| 130 | if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) { |
| 131 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 132 | } |
| 133 | switch (fMode) { |
| 134 | case kFullImage_Mode: { |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 135 | SkBitmap bitmap; |
| 136 | if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height), |
| 137 | fSampleSize, colorType, false)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 138 | return "Cannot decode (full) region."; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 139 | } |
| 140 | if (colorType != bitmap.colorType()) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 141 | return Error::Nonfatal("Cannot convert to color type."); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 142 | } |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 143 | canvas->drawBitmap(bitmap, 0, 0); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 144 | return ""; |
| 145 | } |
| 146 | case kDivisor_Mode: { |
| 147 | const uint32_t divisor = 2; |
| 148 | if (width < divisor || height < divisor) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 149 | return Error::Nonfatal("Divisor is larger than image dimension."); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // Use a border to test subsets that extend outside the image. |
| 153 | // We will not allow the border to be larger than the image dimensions. Allowing |
| 154 | // these large borders causes off by one errors that indicate a problem with the |
| 155 | // test suite, not a problem with the implementation. |
| 156 | const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor); |
| 157 | const uint32_t scaledBorder = SkTMin(5u, maxBorder); |
| 158 | const uint32_t unscaledBorder = scaledBorder * fSampleSize; |
| 159 | |
| 160 | // We may need to clear the canvas to avoid uninitialized memory. |
| 161 | // Assume we are scaling a 780x780 image with sampleSize = 8. |
| 162 | // The output image should be 97x97. |
| 163 | // Each subset will be 390x390. |
| 164 | // Each scaled subset be 48x48. |
| 165 | // Four scaled subsets will only fill a 96x96 image. |
| 166 | // The bottom row and last column will not be touched. |
| 167 | // This is an unfortunate result of our rounding rules when scaling. |
| 168 | // Maybe we need to consider testing scaled subsets without trying to |
| 169 | // combine them to match the full scaled image? Or maybe this is the |
| 170 | // best we can do? |
| 171 | canvas->clear(0); |
| 172 | |
| 173 | for (uint32_t x = 0; x < divisor; x++) { |
| 174 | for (uint32_t y = 0; y < divisor; y++) { |
| 175 | // Calculate the subset dimensions |
| 176 | uint32_t subsetWidth = width / divisor; |
| 177 | uint32_t subsetHeight = height / divisor; |
| 178 | const int left = x * subsetWidth; |
| 179 | const int top = y * subsetHeight; |
| 180 | |
| 181 | // Increase the size of the last subset in each row or column, when the |
| 182 | // divisor does not divide evenly into the image dimensions |
| 183 | subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0; |
| 184 | subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0; |
| 185 | |
| 186 | // Increase the size of the subset in order to have a border on each side |
| 187 | const int decodeLeft = left - unscaledBorder; |
| 188 | const int decodeTop = top - unscaledBorder; |
| 189 | const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2; |
| 190 | const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 191 | SkBitmap bitmap; |
| 192 | if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft, |
| 193 | decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 194 | return "Cannot decode region."; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 195 | } |
| 196 | if (colorType != bitmap.colorType()) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 197 | return Error::Nonfatal("Cannot convert to color type."); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 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); |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 215 | return "Error: Should not be reached."; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | SkISize BRDSrc::size() const { |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 220 | SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 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 | 4b0778e | 2015-11-13 09:59:11 -0800 | [diff] [blame] | 238 | return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 242 | |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 243 | static bool serial_from_path_name(const SkString& path) { |
| 244 | if (!FLAGS_RAW_threading) { |
| 245 | static const char* const exts[] = { |
| 246 | "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw", |
| 247 | "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW", |
| 248 | }; |
| 249 | const char* actualExt = strrchr(path.c_str(), '.'); |
| 250 | if (actualExt) { |
| 251 | actualExt++; |
| 252 | for (auto* ext : exts) { |
| 253 | if (0 == strcmp(ext, actualExt)) { |
| 254 | return true; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 262 | CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType, |
| 263 | float scale) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 264 | : fPath(path) |
| 265 | , fMode(mode) |
| 266 | , fDstColorType(dstColorType) |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 267 | , fDstAlphaType(dstAlphaType) |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 268 | , fScale(scale) |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 269 | , fRunSerially(serial_from_path_name(path)) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 270 | {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 271 | |
mtklein | 99cab4e | 2015-07-31 06:43:04 -0700 | [diff] [blame] | 272 | bool CodecSrc::veto(SinkFlags flags) const { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 273 | // Test to direct raster backends (8888 and 565). |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 274 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 275 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 276 | |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 277 | // Allows us to test decodes to non-native 8888. |
| 278 | void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) { |
| 279 | if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | for (int y = 0; y < bitmap.height(); y++) { |
| 284 | uint32_t* row = (uint32_t*) bitmap.getAddr(0, y); |
| 285 | SkOpts::RGBA_to_BGRA(row, row, bitmap.width()); |
| 286 | } |
| 287 | } |
| 288 | |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 289 | // FIXME: Currently we cannot draw unpremultiplied sources. skbug.com/3338 and skbug.com/3339. |
| 290 | // This allows us to still test unpremultiplied decodes. |
| 291 | void premultiply_if_necessary(SkBitmap& bitmap) { |
| 292 | if (kUnpremul_SkAlphaType != bitmap.alphaType()) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | switch (bitmap.colorType()) { |
| 297 | case kN32_SkColorType: |
| 298 | for (int y = 0; y < bitmap.height(); y++) { |
| 299 | uint32_t* row = (uint32_t*) bitmap.getAddr(0, y); |
| 300 | SkOpts::RGBA_to_rgbA(row, row, bitmap.width()); |
| 301 | } |
| 302 | break; |
| 303 | case kIndex_8_SkColorType: { |
| 304 | SkColorTable* colorTable = bitmap.getColorTable(); |
| 305 | SkPMColor* colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 306 | SkOpts::RGBA_to_rgbA(colorPtr, colorPtr, colorTable->count()); |
| 307 | break; |
| 308 | } |
| 309 | default: |
| 310 | // No need to premultiply kGray or k565 outputs. |
| 311 | break; |
| 312 | } |
msarett | e1daa48 | 2016-02-03 15:31:18 -0800 | [diff] [blame] | 313 | |
| 314 | // In the kIndex_8 case, the canvas won't even try to draw unless we mark the |
| 315 | // bitmap as kPremul. |
| 316 | bitmap.setAlphaType(kPremul_SkAlphaType); |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 317 | } |
| 318 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 319 | bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType, |
| 320 | CodecSrc::DstColorType dstColorType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 321 | switch (dstColorType) { |
| 322 | case CodecSrc::kIndex8_Always_DstColorType: |
| 323 | if (kRGB_565_SkColorType == canvasColorType) { |
| 324 | return false; |
| 325 | } |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 326 | *decodeInfo = decodeInfo->makeColorType(kIndex_8_SkColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 327 | break; |
| 328 | case CodecSrc::kGrayscale_Always_DstColorType: |
msarett | 55f7bdd | 2016-02-16 13:24:54 -0800 | [diff] [blame] | 329 | if (kRGB_565_SkColorType == canvasColorType || |
| 330 | kOpaque_SkAlphaType != decodeInfo->alphaType()) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 331 | return false; |
| 332 | } |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 333 | *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 334 | break; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 335 | case CodecSrc::kNonNative8888_Always_DstColorType: |
| 336 | if (kRGB_565_SkColorType == canvasColorType) { |
| 337 | return false; |
| 338 | } |
| 339 | #ifdef SK_PMCOLOR_IS_RGBA |
| 340 | *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType); |
| 341 | #else |
| 342 | *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType); |
| 343 | #endif |
| 344 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 345 | default: |
msarett | 55f7bdd | 2016-02-16 13:24:54 -0800 | [diff] [blame] | 346 | if (kRGB_565_SkColorType == canvasColorType && |
| 347 | kOpaque_SkAlphaType != decodeInfo->alphaType()) { |
| 348 | return false; |
| 349 | } |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 350 | *decodeInfo = decodeInfo->makeColorType(canvasColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 354 | return true; |
| 355 | } |
| 356 | |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 357 | Error CodecSrc::draw(SkCanvas* canvas) const { |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 358 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 359 | if (!encoded) { |
| 360 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 361 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 362 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 363 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
msarett | 9e707a0 | 2015-09-01 14:57:57 -0700 | [diff] [blame] | 364 | if (nullptr == codec.get()) { |
| 365 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 366 | } |
| 367 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 368 | SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType); |
| 369 | if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 370 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 371 | } |
| 372 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 373 | // Try to scale the image if it is desired |
| 374 | SkISize size = codec->getScaledDimensions(fScale); |
| 375 | if (size == decodeInfo.dimensions() && 1.0f != fScale) { |
| 376 | return Error::Nonfatal("Test without scaling is uninteresting."); |
| 377 | } |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 378 | |
| 379 | // Visually inspecting very small output images is not necessary. We will |
| 380 | // cover these cases in unit testing. |
| 381 | if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) { |
| 382 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 383 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 384 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 385 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 386 | // Construct a color table for the decode if necessary |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 387 | SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 388 | SkPMColor* colorPtr = nullptr; |
| 389 | int* colorCountPtr = nullptr; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 390 | int maxColors = 256; |
| 391 | if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 392 | SkPMColor colors[256]; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 393 | colorTable.reset(new SkColorTable(colors, maxColors)); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 394 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 395 | colorCountPtr = &maxColors; |
| 396 | } |
| 397 | |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 398 | SkBitmap bitmap; |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 399 | SkPixelRefFactory* factory = nullptr; |
| 400 | SkMallocPixelRef::ZeroedPRFactory zeroFactory; |
| 401 | SkCodec::Options options; |
| 402 | if (kCodecZeroInit_Mode == fMode) { |
| 403 | factory = &zeroFactory; |
| 404 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 405 | } |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 406 | |
| 407 | SkImageInfo bitmapInfo = decodeInfo; |
| 408 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 409 | kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
| 410 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 411 | } |
| 412 | if (!bitmap.tryAllocPixels(bitmapInfo, factory, colorTable.get())) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 413 | return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 414 | decodeInfo.width(), decodeInfo.height()); |
| 415 | } |
| 416 | |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 417 | switch (fMode) { |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 418 | case kCodecZeroInit_Mode: |
msarett | 9e707a0 | 2015-09-01 14:57:57 -0700 | [diff] [blame] | 419 | case kCodec_Mode: { |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 420 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options, |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 421 | colorPtr, colorCountPtr)) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 422 | case SkCodec::kSuccess: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 423 | // We consider incomplete to be valid, since we should still decode what is |
| 424 | // available. |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 425 | case SkCodec::kIncompleteInput: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 426 | break; |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 427 | default: |
| 428 | // Everything else is considered a failure. |
| 429 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 430 | } |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 431 | premultiply_if_necessary(bitmap); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 432 | swap_rb_if_necessary(bitmap, fDstColorType); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 433 | canvas->drawBitmap(bitmap, 0, 0); |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 434 | break; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 435 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 436 | case kScanline_Mode: { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 437 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr, |
| 438 | colorCountPtr)) { |
msarett | b65e604 | 2016-02-23 05:37:25 -0800 | [diff] [blame] | 439 | return "Could not start scanline decoder"; |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 440 | } |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 441 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 442 | void* dst = bitmap.getAddr(0, 0); |
| 443 | size_t rowBytes = bitmap.rowBytes(); |
| 444 | uint32_t height = decodeInfo.height(); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 445 | switch (codec->getScanlineOrder()) { |
| 446 | case SkCodec::kTopDown_SkScanlineOrder: |
| 447 | case SkCodec::kBottomUp_SkScanlineOrder: |
| 448 | case SkCodec::kNone_SkScanlineOrder: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 449 | // We do not need to check the return value. On an incomplete |
| 450 | // image, memory will be filled with a default value. |
| 451 | codec->getScanlines(dst, height, rowBytes); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 452 | break; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 453 | case SkCodec::kOutOfOrder_SkScanlineOrder: { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 454 | for (int y = 0; y < decodeInfo.height(); y++) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 455 | int dstY = codec->outputScanline(y); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 456 | void* dstPtr = bitmap.getAddr(0, dstY); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 457 | // We complete the loop, even if this call begins to fail |
| 458 | // due to an incomplete image. This ensures any uninitialized |
| 459 | // memory will be filled with the proper value. |
| 460 | codec->getScanlines(dstPtr, 1, bitmap.rowBytes()); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 461 | } |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 466 | premultiply_if_necessary(bitmap); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 467 | swap_rb_if_necessary(bitmap, fDstColorType); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 468 | canvas->drawBitmap(bitmap, 0, 0); |
| 469 | break; |
| 470 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 471 | case kStripe_Mode: { |
| 472 | const int height = decodeInfo.height(); |
| 473 | // This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 474 | // does not align with image blocks. |
| 475 | const int stripeHeight = 37; |
| 476 | const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
| 477 | |
| 478 | // Decode odd stripes |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 479 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr, |
msarett | b65e604 | 2016-02-23 05:37:25 -0800 | [diff] [blame] | 480 | colorCountPtr)) { |
| 481 | return "Could not start scanline decoder"; |
| 482 | } |
| 483 | |
| 484 | // This mode was designed to test the new skip scanlines API in libjpeg-turbo. |
| 485 | // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting |
| 486 | // to run this test for image types that do not have this scanline ordering. |
| 487 | if (SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) { |
| 488 | return Error::Nonfatal("kStripe test is only interesting for kTopDown codecs."); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 489 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 490 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 491 | for (int i = 0; i < numStripes; i += 2) { |
| 492 | // Skip a stripe |
| 493 | const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 494 | codec->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 495 | |
| 496 | // Read a stripe |
| 497 | const int startY = (i + 1) * stripeHeight; |
| 498 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 499 | if (linesToRead > 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 500 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | // Decode even stripes |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 505 | const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr, |
| 506 | colorPtr, colorCountPtr); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 507 | if (SkCodec::kSuccess != startResult) { |
| 508 | return "Failed to restart scanline decoder with same parameters."; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 509 | } |
| 510 | for (int i = 0; i < numStripes; i += 2) { |
| 511 | // Read a stripe |
| 512 | const int startY = i * stripeHeight; |
| 513 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 514 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 515 | |
| 516 | // Skip a stripe |
msarett | f6db27e | 2015-06-12 09:34:04 -0700 | [diff] [blame] | 517 | const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); |
| 518 | if (linesToSkip > 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 519 | codec->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 520 | } |
| 521 | } |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 522 | premultiply_if_necessary(bitmap); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 523 | swap_rb_if_necessary(bitmap, fDstColorType); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 524 | canvas->drawBitmap(bitmap, 0, 0); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 525 | break; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 526 | } |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 527 | case kCroppedScanline_Mode: { |
| 528 | const int width = decodeInfo.width(); |
| 529 | const int height = decodeInfo.height(); |
| 530 | // This value is chosen because, as we move across the image, it will sometimes |
| 531 | // align with the jpeg block sizes and it will sometimes not. This allows us |
| 532 | // to test interestingly different code paths in the implementation. |
| 533 | const int tileSize = 36; |
| 534 | |
| 535 | SkCodec::Options opts; |
| 536 | SkIRect subset; |
| 537 | for (int x = 0; x < width; x += tileSize) { |
| 538 | subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height); |
| 539 | opts.fSubset = ⊂ |
| 540 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &opts, |
| 541 | colorPtr, colorCountPtr)) { |
| 542 | return "Could not start scanline decoder."; |
| 543 | } |
| 544 | |
| 545 | codec->getScanlines(bitmap.getAddr(x, 0), height, bitmap.rowBytes()); |
| 546 | } |
| 547 | |
| 548 | premultiply_if_necessary(bitmap); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 549 | swap_rb_if_necessary(bitmap, fDstColorType); |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 550 | canvas->drawBitmap(bitmap, 0, 0); |
| 551 | break; |
| 552 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 553 | case kSubset_Mode: { |
| 554 | // Arbitrarily choose a divisor. |
| 555 | int divisor = 2; |
| 556 | // Total width/height of the image. |
| 557 | const int W = codec->getInfo().width(); |
| 558 | const int H = codec->getInfo().height(); |
| 559 | if (divisor > W || divisor > H) { |
| 560 | return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big " |
| 561 | "for %s with dimensions (%d x %d)", divisor, |
| 562 | fPath.c_str(), W, H)); |
| 563 | } |
| 564 | // subset dimensions |
| 565 | // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 566 | const int w = SkAlign2(W / divisor); |
| 567 | const int h = SkAlign2(H / divisor); |
| 568 | SkIRect subset; |
| 569 | SkCodec::Options opts; |
| 570 | opts.fSubset = ⊂ |
| 571 | SkBitmap subsetBm; |
| 572 | // We will reuse pixel memory from bitmap. |
| 573 | void* pixels = bitmap.getPixels(); |
| 574 | // Keep track of left and top (for drawing subsetBm into canvas). We could use |
| 575 | // fScale * x and fScale * y, but we want integers such that the next subset will start |
| 576 | // where the last one ended. So we'll add decodeInfo.width() and height(). |
| 577 | int left = 0; |
| 578 | for (int x = 0; x < W; x += w) { |
| 579 | int top = 0; |
| 580 | for (int y = 0; y < H; y+= h) { |
| 581 | // Do not make the subset go off the edge of the image. |
| 582 | const int preScaleW = SkTMin(w, W - x); |
| 583 | const int preScaleH = SkTMin(h, H - y); |
| 584 | subset.setXYWH(x, y, preScaleW, preScaleH); |
| 585 | // And scale |
| 586 | // FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 587 | // into account? |
msarett | c7eb490 | 2016-04-25 07:04:58 -0700 | [diff] [blame] | 588 | const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)); |
| 589 | const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)); |
| 590 | decodeInfo = decodeInfo.makeWH(scaledW, scaledH); |
| 591 | SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH); |
| 592 | size_t rowBytes = subsetBitmapInfo.minRowBytes(); |
| 593 | if (!subsetBm.installPixels(subsetBitmapInfo, pixels, rowBytes, colorTable.get(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 594 | nullptr, nullptr)) { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 595 | return SkStringPrintf("could not install pixels for %s.", fPath.c_str()); |
| 596 | } |
| 597 | const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes, |
| 598 | &opts, colorPtr, colorCountPtr); |
| 599 | switch (result) { |
| 600 | case SkCodec::kSuccess: |
| 601 | case SkCodec::kIncompleteInput: |
| 602 | break; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 603 | default: |
| 604 | return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) " |
| 605 | "from %s with dimensions (%d x %d)\t error %d", |
| 606 | x, y, decodeInfo.width(), decodeInfo.height(), |
| 607 | fPath.c_str(), W, H, result); |
| 608 | } |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 609 | premultiply_if_necessary(subsetBm); |
msarett | c7eb490 | 2016-04-25 07:04:58 -0700 | [diff] [blame] | 610 | swap_rb_if_necessary(subsetBm, fDstColorType); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 611 | canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToScalar(top)); |
| 612 | // translate by the scaled height. |
| 613 | top += decodeInfo.height(); |
| 614 | } |
| 615 | // translate by the scaled width. |
| 616 | left += decodeInfo.width(); |
| 617 | } |
| 618 | return ""; |
| 619 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 620 | default: |
| 621 | SkASSERT(false); |
| 622 | return "Invalid fMode"; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 623 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 624 | return ""; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | SkISize CodecSrc::size() const { |
| 628 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 629 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
scroggo | 7fac5af | 2015-09-30 11:33:12 -0700 | [diff] [blame] | 630 | if (nullptr == codec) { |
| 631 | return SkISize::Make(0, 0); |
| 632 | } |
| 633 | return codec->getScaledDimensions(fScale); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | Name CodecSrc::name() const { |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 637 | if (1.0f == fScale) { |
| 638 | return SkOSPath::Basename(fPath.c_str()); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 639 | } |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 640 | return get_scaled_name(fPath, fScale); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 644 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 645 | AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 646 | SkAlphaType dstAlphaType, int sampleSize) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 647 | : fPath(path) |
| 648 | , fMode(mode) |
| 649 | , fDstColorType(dstColorType) |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 650 | , fDstAlphaType(dstAlphaType) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 651 | , fSampleSize(sampleSize) |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 652 | , fRunSerially(serial_from_path_name(path)) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 653 | {} |
| 654 | |
| 655 | bool AndroidCodecSrc::veto(SinkFlags flags) const { |
| 656 | // No need to test decoding to non-raster or indirect backend. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 657 | return flags.type != SinkFlags::kRaster |
| 658 | || flags.approach != SinkFlags::kDirect; |
| 659 | } |
| 660 | |
| 661 | Error AndroidCodecSrc::draw(SkCanvas* canvas) const { |
| 662 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 663 | if (!encoded) { |
| 664 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 665 | } |
| 666 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); |
| 667 | if (nullptr == codec.get()) { |
| 668 | return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str()); |
| 669 | } |
| 670 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 671 | SkImageInfo decodeInfo = codec->getInfo().makeAlphaType(fDstAlphaType); |
| 672 | if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType)) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 673 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 674 | } |
| 675 | |
| 676 | // Scale the image if it is desired. |
| 677 | SkISize size = codec->getSampledDimensions(fSampleSize); |
| 678 | |
| 679 | // Visually inspecting very small output images is not necessary. We will |
| 680 | // cover these cases in unit testing. |
| 681 | if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) { |
| 682 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 683 | } |
| 684 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 685 | |
| 686 | // Construct a color table for the decode if necessary |
| 687 | SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 688 | SkPMColor* colorPtr = nullptr; |
| 689 | int* colorCountPtr = nullptr; |
| 690 | int maxColors = 256; |
| 691 | if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 692 | SkPMColor colors[256]; |
| 693 | colorTable.reset(new SkColorTable(colors, maxColors)); |
| 694 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 695 | colorCountPtr = &maxColors; |
| 696 | } |
| 697 | |
| 698 | SkBitmap bitmap; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 699 | SkImageInfo bitmapInfo = decodeInfo; |
| 700 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 701 | kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
| 702 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 703 | } |
| 704 | if (!bitmap.tryAllocPixels(bitmapInfo, nullptr, colorTable.get())) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 705 | return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 706 | decodeInfo.width(), decodeInfo.height()); |
| 707 | } |
| 708 | |
| 709 | // Create options for the codec. |
| 710 | SkAndroidCodec::AndroidOptions options; |
| 711 | options.fColorPtr = colorPtr; |
| 712 | options.fColorCount = colorCountPtr; |
| 713 | options.fSampleSize = fSampleSize; |
| 714 | |
| 715 | switch (fMode) { |
| 716 | case kFullImage_Mode: { |
| 717 | switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), |
| 718 | &options)) { |
| 719 | case SkCodec::kSuccess: |
| 720 | case SkCodec::kIncompleteInput: |
| 721 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 722 | default: |
| 723 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 724 | } |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 725 | premultiply_if_necessary(bitmap); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 726 | swap_rb_if_necessary(bitmap, fDstColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 727 | canvas->drawBitmap(bitmap, 0, 0); |
| 728 | return ""; |
| 729 | } |
| 730 | case kDivisor_Mode: { |
| 731 | const int width = codec->getInfo().width(); |
| 732 | const int height = codec->getInfo().height(); |
| 733 | const int divisor = 2; |
| 734 | if (width < divisor || height < divisor) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 735 | return Error::Nonfatal("Divisor is larger than image dimension."); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 736 | } |
| 737 | |
msarett | fa23a9e | 2015-10-21 13:26:59 -0700 | [diff] [blame] | 738 | // Keep track of the final decoded dimensions. |
| 739 | int finalScaledWidth = 0; |
| 740 | int finalScaledHeight = 0; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 741 | for (int x = 0; x < divisor; x++) { |
| 742 | for (int y = 0; y < divisor; y++) { |
| 743 | // Calculate the subset dimensions |
| 744 | int subsetWidth = width / divisor; |
| 745 | int subsetHeight = height / divisor; |
| 746 | const int left = x * subsetWidth; |
| 747 | const int top = y * subsetHeight; |
| 748 | |
| 749 | // Increase the size of the last subset in each row or column, when the |
| 750 | // divisor does not divide evenly into the image dimensions |
| 751 | subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0; |
| 752 | subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0; |
| 753 | SkIRect subset = SkIRect::MakeXYWH(left, top, subsetWidth, subsetHeight); |
| 754 | if (!codec->getSupportedSubset(&subset)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 755 | return "Could not get supported subset to decode."; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 756 | } |
| 757 | options.fSubset = ⊂ |
msarett | fa23a9e | 2015-10-21 13:26:59 -0700 | [diff] [blame] | 758 | const int scaledWidthOffset = subset.left() / fSampleSize; |
| 759 | const int scaledHeightOffset = subset.top() / fSampleSize; |
| 760 | void* pixels = bitmap.getAddr(scaledWidthOffset, scaledHeightOffset); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 761 | SkISize scaledSubsetSize = codec->getSampledSubsetDimensions(fSampleSize, |
| 762 | subset); |
| 763 | SkImageInfo subsetDecodeInfo = decodeInfo.makeWH(scaledSubsetSize.width(), |
| 764 | scaledSubsetSize.height()); |
| 765 | |
msarett | fa23a9e | 2015-10-21 13:26:59 -0700 | [diff] [blame] | 766 | if (x + 1 == divisor && y + 1 == divisor) { |
| 767 | finalScaledWidth = scaledWidthOffset + scaledSubsetSize.width(); |
| 768 | finalScaledHeight = scaledHeightOffset + scaledSubsetSize.height(); |
| 769 | } |
| 770 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 771 | switch (codec->getAndroidPixels(subsetDecodeInfo, pixels, bitmap.rowBytes(), |
| 772 | &options)) { |
| 773 | case SkCodec::kSuccess: |
| 774 | case SkCodec::kIncompleteInput: |
| 775 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 776 | default: |
| 777 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 778 | } |
| 779 | } |
| 780 | } |
msarett | fa23a9e | 2015-10-21 13:26:59 -0700 | [diff] [blame] | 781 | |
| 782 | SkRect rect = SkRect::MakeXYWH(0, 0, (SkScalar) finalScaledWidth, |
| 783 | (SkScalar) finalScaledHeight); |
msarett | 9e9444c | 2016-02-03 12:39:10 -0800 | [diff] [blame] | 784 | premultiply_if_necessary(bitmap); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 785 | swap_rb_if_necessary(bitmap, fDstColorType); |
msarett | fa23a9e | 2015-10-21 13:26:59 -0700 | [diff] [blame] | 786 | canvas->drawBitmapRect(bitmap, rect, rect, nullptr); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 787 | return ""; |
| 788 | } |
| 789 | default: |
| 790 | SkASSERT(false); |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 791 | return "Error: Should not be reached."; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | SkISize AndroidCodecSrc::size() const { |
| 796 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 797 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); |
| 798 | if (nullptr == codec) { |
| 799 | return SkISize::Make(0, 0); |
| 800 | } |
| 801 | return codec->getSampledDimensions(fSampleSize); |
| 802 | } |
| 803 | |
| 804 | Name AndroidCodecSrc::name() const { |
| 805 | // We will replicate the names used by CodecSrc so that images can |
| 806 | // be compared in Gold. |
| 807 | if (1 == fSampleSize) { |
| 808 | return SkOSPath::Basename(fPath.c_str()); |
| 809 | } |
msarett | 4b0778e | 2015-11-13 09:59:11 -0800 | [diff] [blame] | 810 | return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 814 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 815 | ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu) |
| 816 | : fPath(path) |
| 817 | , fMode(mode) |
| 818 | , fDstAlphaType(alphaType) |
| 819 | , fIsGpu(isGpu) |
| 820 | , fRunSerially(serial_from_path_name(path)) |
| 821 | {} |
| 822 | |
| 823 | bool ImageGenSrc::veto(SinkFlags flags) const { |
| 824 | if (fIsGpu) { |
| 825 | return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect; |
| 826 | } |
| 827 | |
| 828 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
| 829 | } |
| 830 | |
| 831 | Error ImageGenSrc::draw(SkCanvas* canvas) const { |
| 832 | if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 833 | return Error::Nonfatal("Uninteresting to test image generator to 565."); |
| 834 | } |
| 835 | |
| 836 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 837 | if (!encoded) { |
| 838 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 839 | } |
| 840 | |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 841 | #if defined(SK_BUILD_FOR_WIN) |
| 842 | // Initialize COM in order to test with WIC. |
| 843 | SkAutoCoInitialize com; |
| 844 | if (!com.succeeded()) { |
| 845 | return "Could not initialize COM."; |
| 846 | } |
| 847 | #endif |
| 848 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 849 | SkAutoTDelete<SkImageGenerator> gen(nullptr); |
| 850 | switch (fMode) { |
| 851 | case kCodec_Mode: |
| 852 | gen.reset(SkCodecImageGenerator::NewFromEncodedCodec(encoded)); |
| 853 | if (!gen) { |
| 854 | return "Could not create codec image generator."; |
| 855 | } |
| 856 | break; |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 857 | case kPlatform_Mode: { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 858 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 859 | gen.reset(SkImageGeneratorCG::NewFromEncodedCG(encoded)); |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 860 | #elif defined(SK_BUILD_FOR_WIN) |
| 861 | gen.reset(SkImageGeneratorWIC::NewFromEncodedWIC(encoded)); |
| 862 | #endif |
| 863 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 864 | if (!gen) { |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 865 | return "Could not create platform image generator."; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 866 | } |
| 867 | break; |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 868 | } |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 869 | default: |
| 870 | SkASSERT(false); |
| 871 | return "Invalid image generator mode"; |
| 872 | } |
| 873 | |
| 874 | // Test deferred decoding path on GPU |
| 875 | if (fIsGpu) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 876 | sk_sp<SkImage> image(SkImage::MakeFromGenerator(gen.release(), nullptr)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 877 | if (!image) { |
| 878 | return "Could not create image from codec image generator."; |
| 879 | } |
| 880 | canvas->drawImage(image, 0, 0); |
| 881 | return ""; |
| 882 | } |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 883 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 884 | // Test various color and alpha types on CPU |
| 885 | SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType); |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 886 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 887 | if (kGray_8_SkColorType == decodeInfo.colorType() && |
| 888 | kOpaque_SkAlphaType != decodeInfo.alphaType()) { |
| 889 | return Error::Nonfatal("Avoid requesting non-opaque kGray8 decodes."); |
| 890 | } |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 891 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 892 | SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 893 | SkPMColor* colorPtr = nullptr; |
| 894 | int* colorCountPtr = nullptr; |
| 895 | int maxColors = 256; |
| 896 | if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 897 | SkPMColor colors[256]; |
| 898 | colorTable.reset(new SkColorTable(colors, maxColors)); |
| 899 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 900 | colorCountPtr = &maxColors; |
| 901 | } |
| 902 | |
| 903 | SkBitmap bitmap; |
| 904 | if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) { |
| 905 | return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
| 906 | decodeInfo.width(), decodeInfo.height()); |
| 907 | } |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 908 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 909 | if (!gen->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), colorPtr, |
| 910 | colorCountPtr)) |
| 911 | { |
| 912 | return SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str()); |
| 913 | } |
| 914 | |
| 915 | premultiply_if_necessary(bitmap); |
| 916 | canvas->drawBitmap(bitmap, 0, 0); |
| 917 | return ""; |
| 918 | } |
| 919 | |
| 920 | SkISize ImageGenSrc::size() const { |
| 921 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 922 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 923 | if (nullptr == codec) { |
| 924 | return SkISize::Make(0, 0); |
| 925 | } |
| 926 | return codec->getInfo().dimensions(); |
| 927 | } |
| 928 | |
| 929 | Name ImageGenSrc::name() const { |
| 930 | return SkOSPath::Basename(fPath.c_str()); |
| 931 | } |
| 932 | |
| 933 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 934 | |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 935 | ColorCodecSrc::ColorCodecSrc(Path path, Mode mode) |
| 936 | : fPath(path) |
| 937 | , fMode(mode) |
| 938 | {} |
| 939 | |
| 940 | bool ColorCodecSrc::veto(SinkFlags flags) const { |
| 941 | // Test to direct raster backends (8888 and 565). |
| 942 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
| 943 | } |
| 944 | |
| 945 | Error ColorCodecSrc::draw(SkCanvas* canvas) const { |
| 946 | if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 947 | return Error::Nonfatal("No need to test color correction to 565 backend."); |
| 948 | } |
| 949 | |
| 950 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 951 | if (!encoded) { |
| 952 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 953 | } |
| 954 | |
| 955 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 956 | if (nullptr == codec.get()) { |
| 957 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| 958 | } |
| 959 | |
| 960 | SkImageInfo decodeInfo = codec->getInfo().makeColorType(kN32_SkColorType); |
| 961 | SkBitmap bitmap; |
| 962 | if (!bitmap.tryAllocPixels(decodeInfo)) { |
| 963 | return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
| 964 | decodeInfo.width(), decodeInfo.height()); |
| 965 | } |
| 966 | |
| 967 | switch (fMode) { |
| 968 | case kBaseline_Mode: |
| 969 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes())) { |
| 970 | case SkCodec::kSuccess: |
| 971 | break; |
| 972 | default: |
| 973 | // Everything else is considered a failure. |
| 974 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 975 | } |
| 976 | canvas->drawBitmap(bitmap, 0, 0); |
| 977 | break; |
| 978 | default: |
| 979 | SkASSERT(false); |
| 980 | return "Invalid fMode"; |
| 981 | } |
| 982 | return ""; |
| 983 | } |
| 984 | |
| 985 | SkISize ColorCodecSrc::size() const { |
| 986 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 987 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 988 | if (nullptr == codec) { |
| 989 | return SkISize::Make(0, 0); |
| 990 | } |
| 991 | return SkISize::Make(codec->getInfo().width(), codec->getInfo().height()); |
| 992 | } |
| 993 | |
| 994 | Name ColorCodecSrc::name() const { |
| 995 | return SkOSPath::Basename(fPath.c_str()); |
| 996 | } |
| 997 | |
| 998 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 999 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1000 | static const SkRect kSKPViewport = {0,0, 1000,1000}; |
| 1001 | |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 1002 | SKPSrc::SKPSrc(Path path) : fPath(path) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1003 | |
| 1004 | Error SKPSrc::draw(SkCanvas* canvas) const { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 1005 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 1006 | if (!stream) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1007 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 1008 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1009 | sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream)); |
mtklein | 75d98fd | 2015-01-18 07:05:01 -0800 | [diff] [blame] | 1010 | if (!pic) { |
| 1011 | return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str()); |
| 1012 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1013 | 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] | 1014 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1015 | canvas->clipRect(kSKPViewport); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1016 | canvas->drawPicture(pic); |
| 1017 | return ""; |
| 1018 | } |
| 1019 | |
| 1020 | SkISize SKPSrc::size() const { |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1021 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
| 1022 | if (!stream) { |
| 1023 | return SkISize::Make(0,0); |
| 1024 | } |
| 1025 | SkPictInfo info; |
| 1026 | if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) { |
| 1027 | return SkISize::Make(0,0); |
| 1028 | } |
| 1029 | SkRect viewport = kSKPViewport; |
| 1030 | if (!viewport.intersect(info.fCullRect)) { |
| 1031 | return SkISize::Make(0,0); |
| 1032 | } |
| 1033 | return viewport.roundOut().size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 1037 | |
| 1038 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1039 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 1040 | Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const { |
| 1041 | SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas()); |
| 1042 | return src.draw(canvas); |
| 1043 | } |
| 1044 | |
| 1045 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1046 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1047 | DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?"); |
| 1048 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 1049 | GPUSink::GPUSink(GrContextFactory::ContextType ct, |
| 1050 | GrContextFactory::ContextOptions options, |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 1051 | int samples, |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 1052 | bool diText, |
brianosman | d93c120 | 2016-03-10 07:49:08 -0800 | [diff] [blame] | 1053 | SkColorType colorType, |
| 1054 | SkColorProfileType profileType, |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 1055 | bool threaded) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1056 | : fContextType(ct) |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1057 | , fContextOptions(options) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1058 | , fSampleCount(samples) |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 1059 | , fUseDIText(diText) |
brianosman | d93c120 | 2016-03-10 07:49:08 -0800 | [diff] [blame] | 1060 | , fColorType(colorType) |
| 1061 | , fProfileType(profileType) |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 1062 | , fThreaded(threaded) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1063 | |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1064 | void PreAbandonGpuContextErrorHandler(SkError, void*) {} |
| 1065 | |
bsalomon | 648c696 | 2015-10-23 09:06:59 -0700 | [diff] [blame] | 1066 | DEFINE_bool(imm, false, "Run gpu configs in immediate mode."); |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 1067 | DEFINE_bool(batchClip, false, "Clip each GrBatch to its device bounds for testing."); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 1068 | DEFINE_bool(batchBounds, false, "Draw a wireframe bounds of each GrBatch."); |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 1069 | DEFINE_int32(batchLookback, -1, "Maximum GrBatch lookback for combining, negative means default."); |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 1070 | DEFINE_int32(batchLookahead, -1, "Maximum GrBatch lookahead for combining, negative means " |
| 1071 | "default."); |
bsalomon | 648c696 | 2015-10-23 09:06:59 -0700 | [diff] [blame] | 1072 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1073 | Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const { |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1074 | GrContextOptions grOptions; |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 1075 | grOptions.fImmediateMode = FLAGS_imm; |
| 1076 | grOptions.fClipBatchToBounds = FLAGS_batchClip; |
| 1077 | grOptions.fDrawBatchBounds = FLAGS_batchBounds; |
| 1078 | grOptions.fMaxBatchLookback = FLAGS_batchLookback; |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 1079 | grOptions.fMaxBatchLookahead = FLAGS_batchLookahead; |
kkinnunen | 64492c4 | 2015-12-08 01:24:40 -0800 | [diff] [blame] | 1080 | |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1081 | src.modifyGrContextOptions(&grOptions); |
| 1082 | |
| 1083 | GrContextFactory factory(grOptions); |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1084 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1085 | const SkImageInfo info = |
brianosman | d93c120 | 2016-03-10 07:49:08 -0800 | [diff] [blame] | 1086 | SkImageInfo::Make(size.width(), size.height(), fColorType, |
| 1087 | kPremul_SkAlphaType, fProfileType); |
msarett | 13a036b | 2016-02-08 09:10:47 -0800 | [diff] [blame] | 1088 | #if SK_SUPPORT_GPU |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame^] | 1089 | GrContext* context = factory.getContextInfo(fContextType, fContextOptions).grContext(); |
| 1090 | const int maxDimension = context->caps()->maxTextureSize(); |
msarett | 13a036b | 2016-02-08 09:10:47 -0800 | [diff] [blame] | 1091 | if (maxDimension < SkTMax(size.width(), size.height())) { |
| 1092 | return Error::Nonfatal("Src too large to create a texture.\n"); |
| 1093 | } |
| 1094 | #endif |
| 1095 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1096 | auto surface( |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 1097 | NewGpuSurface(&factory, fContextType, fContextOptions, info, fSampleCount, fUseDIText)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1098 | if (!surface) { |
| 1099 | return "Could not create a surface."; |
| 1100 | } |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1101 | if (FLAGS_preAbandonGpuContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1102 | SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, nullptr); |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1103 | factory.abandonContexts(); |
| 1104 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1105 | SkCanvas* canvas = surface->getCanvas(); |
| 1106 | Error err = src.draw(canvas); |
| 1107 | if (!err.isEmpty()) { |
| 1108 | return err; |
| 1109 | } |
| 1110 | canvas->flush(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1111 | if (FLAGS_gpuStats) { |
| 1112 | canvas->getGrContext()->dumpCacheStats(log); |
| 1113 | canvas->getGrContext()->dumpGpuStats(log); |
| 1114 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1115 | dst->allocPixels(info); |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1116 | canvas->readPixels(dst, 0, 0); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 1117 | if (FLAGS_abandonGpuContext) { |
| 1118 | factory.abandonContexts(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 1119 | } else if (FLAGS_releaseAndAbandonGpuContext) { |
| 1120 | factory.releaseResourcesAndAbandonContexts(); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 1121 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1122 | return ""; |
| 1123 | } |
| 1124 | |
| 1125 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1126 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1127 | static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) { |
halcanary | 4ba051c | 2016-03-10 10:31:53 -0800 | [diff] [blame] | 1128 | if (src.size().isEmpty()) { |
| 1129 | return "Source has empty dimensions"; |
| 1130 | } |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1131 | SkASSERT(doc); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 1132 | int width = src.size().width(), |
| 1133 | height = src.size().height(); |
| 1134 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1135 | if (FLAGS_multiPage) { |
halcanary | 4ba051c | 2016-03-10 10:31:53 -0800 | [diff] [blame] | 1136 | // Print the given DM:Src to a document, breaking on 8.5x11 pages. |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1137 | const int kLetterWidth = 612, // 8.5 * 72 |
| 1138 | kLetterHeight = 792; // 11 * 72 |
| 1139 | const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth), |
| 1140 | SkIntToScalar(kLetterHeight)); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 1141 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1142 | int xPages = ((width - 1) / kLetterWidth) + 1; |
| 1143 | int yPages = ((height - 1) / kLetterHeight) + 1; |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 1144 | |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1145 | for (int y = 0; y < yPages; ++y) { |
| 1146 | for (int x = 0; x < xPages; ++x) { |
| 1147 | int w = SkTMin(kLetterWidth, width - (x * kLetterWidth)); |
| 1148 | int h = SkTMin(kLetterHeight, height - (y * kLetterHeight)); |
| 1149 | SkCanvas* canvas = |
| 1150 | doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)); |
| 1151 | if (!canvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1152 | return "SkDocument::beginPage(w,h) returned nullptr"; |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1153 | } |
| 1154 | canvas->clipRect(letter); |
| 1155 | canvas->translate(-letter.width() * x, -letter.height() * y); |
| 1156 | Error err = src.draw(canvas); |
| 1157 | if (!err.isEmpty()) { |
| 1158 | return err; |
| 1159 | } |
| 1160 | doc->endPage(); |
djsollen | 2ab9000 | 2015-04-03 06:38:31 -0700 | [diff] [blame] | 1161 | } |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 1162 | } |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1163 | } else { |
| 1164 | SkCanvas* canvas = |
| 1165 | doc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); |
| 1166 | if (!canvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1167 | return "SkDocument::beginPage(w,h) returned nullptr"; |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1168 | } |
| 1169 | Error err = src.draw(canvas); |
| 1170 | if (!err.isEmpty()) { |
| 1171 | return err; |
| 1172 | } |
| 1173 | doc->endPage(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1174 | } |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1175 | if (!doc->close()) { |
| 1176 | return "SkDocument::close() returned false"; |
| 1177 | } |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 1178 | dst->flush(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1179 | return ""; |
| 1180 | } |
| 1181 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1182 | Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1183 | SkDocument::PDFMetadata metadata; |
| 1184 | metadata.fTitle = src.name(); |
| 1185 | metadata.fSubject = "rendering correctness test"; |
| 1186 | metadata.fCreator = "Skia/DM"; |
| 1187 | sk_sp<SkDocument> doc = SkDocument::MakePDF(dst, SK_ScalarDefaultRasterDPI, |
| 1188 | metadata, nullptr, fPDFA); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1189 | if (!doc) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1190 | return "SkDocument::MakePDF() returned nullptr"; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1191 | } |
| 1192 | return draw_skdocument(src, doc.get(), dst); |
| 1193 | } |
| 1194 | |
| 1195 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1196 | |
| 1197 | XPSSink::XPSSink() {} |
| 1198 | |
| 1199 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1200 | sk_sp<SkDocument> doc(SkDocument::MakeXPS(dst)); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1201 | if (!doc) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1202 | return "SkDocument::MakeXPS() returned nullptr"; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1203 | } |
| 1204 | return draw_skdocument(src, doc.get(), dst); |
| 1205 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1206 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1207 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1208 | SKPSink::SKPSink() {} |
| 1209 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1210 | Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1211 | SkSize size; |
| 1212 | size = src.size(); |
| 1213 | SkPictureRecorder recorder; |
| 1214 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 1215 | if (!err.isEmpty()) { |
| 1216 | return err; |
| 1217 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1218 | recorder.finishRecordingAsPicture()->serialize(dst); |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1219 | return ""; |
| 1220 | } |
| 1221 | |
| 1222 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1223 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 1224 | SVGSink::SVGSink() {} |
| 1225 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1226 | Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1227 | SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(dst)); |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 1228 | SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create( |
| 1229 | SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())), |
| 1230 | xmlWriter)); |
| 1231 | return src.draw(canvas); |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1235 | |
mtklein | 27c3fdd | 2016-02-26 14:43:21 -0800 | [diff] [blame] | 1236 | RasterSink::RasterSink(SkColorType colorType, SkColorProfileType profileType) |
| 1237 | : fColorType(colorType) |
| 1238 | , fProfileType(profileType) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1239 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1240 | Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1241 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1242 | // If there's an appropriate alpha type for this color type, use it, otherwise use premul. |
| 1243 | SkAlphaType alphaType = kPremul_SkAlphaType; |
| 1244 | (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType); |
| 1245 | |
mtklein | c8be09a | 2016-01-04 18:56:57 -0800 | [diff] [blame] | 1246 | SkMallocPixelRef::ZeroedPRFactory factory; |
mtklein | 27c3fdd | 2016-02-26 14:43:21 -0800 | [diff] [blame] | 1247 | dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), |
| 1248 | fColorType, alphaType, fProfileType), |
mtklein | c8be09a | 2016-01-04 18:56:57 -0800 | [diff] [blame] | 1249 | &factory, |
| 1250 | nullptr/*colortable*/); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1251 | SkCanvas canvas(*dst); |
| 1252 | return src.draw(&canvas); |
| 1253 | } |
| 1254 | |
| 1255 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1256 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1257 | // 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] | 1258 | // 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] | 1259 | // Several examples below. |
| 1260 | |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1261 | template <typename Fn> |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1262 | static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log, |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1263 | SkISize size, const Fn& draw) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1264 | class ProxySrc : public Src { |
| 1265 | public: |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1266 | ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {} |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1267 | Error draw(SkCanvas* canvas) const override { return fDraw(canvas); } |
halcanary | b4a7f14 | 2016-03-30 08:31:27 -0700 | [diff] [blame] | 1268 | Name name() const override { return "ProxySrc"; } |
| 1269 | SkISize size() const override { return fSize; } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1270 | private: |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1271 | SkISize fSize; |
| 1272 | const Fn& fDraw; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1273 | }; |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1274 | return sink->draw(ProxySrc(size, draw), bitmap, stream, log); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1278 | |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1279 | DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output."); |
| 1280 | |
| 1281 | // Is *bitmap identical to what you get drawing src into sink? |
| 1282 | static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) { |
| 1283 | // We can only check raster outputs. |
| 1284 | // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.) |
| 1285 | if (FLAGS_check && bitmap) { |
| 1286 | SkBitmap reference; |
| 1287 | SkString log; |
halcanary | b4a7f14 | 2016-03-30 08:31:27 -0700 | [diff] [blame] | 1288 | SkDynamicMemoryWStream wStream; |
| 1289 | Error err = sink->draw(src, &reference, &wStream, &log); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1290 | // If we can draw into this Sink via some pipeline, we should be able to draw directly. |
| 1291 | SkASSERT(err.isEmpty()); |
| 1292 | if (!err.isEmpty()) { |
| 1293 | return err; |
| 1294 | } |
| 1295 | // The dimensions are a property of the Src only, and so should be identical. |
| 1296 | SkASSERT(reference.getSize() == bitmap->getSize()); |
| 1297 | if (reference.getSize() != bitmap->getSize()) { |
| 1298 | return "Dimensions don't match reference"; |
| 1299 | } |
| 1300 | // All SkBitmaps in DM are pre-locked and tight, so this comparison is easy. |
| 1301 | if (0 != memcmp(reference.getPixels(), bitmap->getPixels(), reference.getSize())) { |
| 1302 | return "Pixels don't match reference"; |
| 1303 | } |
| 1304 | } |
| 1305 | return ""; |
| 1306 | } |
| 1307 | |
| 1308 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1309 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1310 | static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) { |
| 1311 | SkRect bounds = SkRect::MakeIWH(srcW, srcH); |
| 1312 | matrix->mapRect(&bounds); |
| 1313 | matrix->postTranslate(-bounds.x(), -bounds.y()); |
| 1314 | return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())); |
| 1315 | } |
| 1316 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1317 | ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1318 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1319 | Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1320 | SkMatrix matrix = fMatrix; |
| 1321 | SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height()); |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1322 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1323 | canvas->concat(matrix); |
| 1324 | return src.draw(canvas); |
| 1325 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1328 | // Undoes any flip or 90 degree rotate without changing the scale of the bitmap. |
| 1329 | // This should be pixel-preserving. |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1330 | ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1331 | |
| 1332 | Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1333 | Error err = fSink->draw(src, bitmap, stream, log); |
| 1334 | if (!err.isEmpty()) { |
| 1335 | return err; |
| 1336 | } |
| 1337 | |
| 1338 | SkMatrix inverse; |
| 1339 | if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) { |
| 1340 | return "Cannot upright --matrix."; |
| 1341 | } |
| 1342 | SkMatrix upright = SkMatrix::I(); |
| 1343 | upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX())); |
| 1344 | upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY())); |
| 1345 | upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX())); |
| 1346 | upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY())); |
| 1347 | |
| 1348 | SkBitmap uprighted; |
| 1349 | SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height()); |
| 1350 | uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height())); |
| 1351 | |
| 1352 | SkCanvas canvas(uprighted); |
| 1353 | canvas.concat(upright); |
| 1354 | SkPaint paint; |
| 1355 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 1356 | canvas.drawBitmap(*bitmap, 0, 0, &paint); |
| 1357 | |
| 1358 | *bitmap = uprighted; |
| 1359 | bitmap->lockPixels(); |
| 1360 | return ""; |
| 1361 | } |
| 1362 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1363 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1364 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1365 | Error ViaSerialization::draw( |
| 1366 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1367 | // Record our Src into a picture. |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1368 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1369 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1370 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1371 | SkIntToScalar(size.height()))); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1372 | if (!err.isEmpty()) { |
| 1373 | return err; |
| 1374 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1375 | sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1376 | |
| 1377 | // Serialize it and then deserialize it. |
| 1378 | SkDynamicMemoryWStream wStream; |
| 1379 | pic->serialize(&wStream); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 1380 | SkAutoTDelete<SkStream> rStream(wStream.detachAsStream()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1381 | sk_sp<SkPicture> deserialized(SkPicture::MakeFromStream(rStream)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1382 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1383 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1384 | canvas->drawPicture(deserialized); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1385 | return check_against_reference(bitmap, src, fSink); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1386 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1390 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1391 | ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink) |
| 1392 | : Via(sink) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 1393 | , fW(w) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1394 | , fH(h) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 1395 | , fFactory(factory) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1396 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1397 | Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1398 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1399 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1400 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1401 | SkIntToScalar(size.height()), |
| 1402 | fFactory.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1403 | if (!err.isEmpty()) { |
| 1404 | return err; |
| 1405 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1406 | sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1407 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1408 | return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1409 | const int xTiles = (size.width() + fW - 1) / fW, |
| 1410 | yTiles = (size.height() + fH - 1) / fH; |
| 1411 | SkMultiPictureDraw mpd(xTiles*yTiles); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1412 | SkTArray<sk_sp<SkSurface>> surfaces; |
| 1413 | // surfaces.setReserve(xTiles*yTiles); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1414 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1415 | SkImageInfo info = canvas->imageInfo().makeWH(fW, fH); |
| 1416 | for (int j = 0; j < yTiles; j++) { |
| 1417 | for (int i = 0; i < xTiles; i++) { |
| 1418 | // This lets our ultimate Sink determine the best kind of surface. |
| 1419 | // E.g., if it's a GpuSink, the surfaces and images are textures. |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1420 | auto s = canvas->makeSurface(info); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1421 | if (!s) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1422 | s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1423 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1424 | surfaces.push_back(s); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1425 | SkCanvas* c = s->getCanvas(); |
| 1426 | c->translate(SkIntToScalar(-i * fW), |
| 1427 | SkIntToScalar(-j * fH)); // Line up the canvas with this tile. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1428 | mpd.add(c, pic.get()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1429 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1430 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1431 | mpd.draw(); |
| 1432 | for (int j = 0; j < yTiles; j++) { |
| 1433 | for (int i = 0; i < xTiles; i++) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1434 | sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot()); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1435 | canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH)); |
| 1436 | } |
| 1437 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1438 | return ""; |
| 1439 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1442 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1443 | |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1444 | Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1445 | auto size = src.size(); |
| 1446 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
| 1447 | SkPictureRecorder recorder; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1448 | sk_sp<SkPicture> pic; |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1449 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1450 | SkIntToScalar(size.height()))); |
| 1451 | if (!err.isEmpty()) { |
| 1452 | return err; |
| 1453 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1454 | pic = recorder.finishRecordingAsPicture(); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1455 | canvas->drawPicture(pic); |
| 1456 | return check_against_reference(bitmap, src, fSink); |
| 1457 | }); |
| 1458 | } |
| 1459 | |
| 1460 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1461 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1462 | // Draw the Src into two pictures, then draw the second picture into the wrapped Sink. |
| 1463 | // This tests that any shortcuts we may take while recording that second picture are legal. |
| 1464 | Error ViaSecondPicture::draw( |
| 1465 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1466 | auto size = src.size(); |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1467 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1468 | SkPictureRecorder recorder; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1469 | sk_sp<SkPicture> pic; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1470 | for (int i = 0; i < 2; i++) { |
| 1471 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1472 | SkIntToScalar(size.height()))); |
| 1473 | if (!err.isEmpty()) { |
| 1474 | return err; |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1475 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1476 | pic = recorder.finishRecordingAsPicture(); |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1477 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1478 | canvas->drawPicture(pic); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1479 | return check_against_reference(bitmap, src, fSink); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1480 | }); |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1483 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1484 | |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 1485 | // Draw the Src twice. This can help exercise caching. |
| 1486 | Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1487 | return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error { |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 1488 | for (int i = 0; i < 2; i++) { |
| 1489 | SkAutoCanvasRestore acr(canvas, true/*save now*/); |
| 1490 | canvas->clear(SK_ColorTRANSPARENT); |
| 1491 | Error err = src.draw(canvas); |
| 1492 | if (err.isEmpty()) { |
| 1493 | return err; |
| 1494 | } |
| 1495 | } |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1496 | return check_against_reference(bitmap, src, fSink); |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 1497 | }); |
| 1498 | } |
| 1499 | |
| 1500 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1501 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1502 | // This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas. |
| 1503 | // Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op. |
| 1504 | // This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures. |
| 1505 | struct DrawsAsSingletonPictures { |
| 1506 | SkCanvas* fCanvas; |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1507 | const SkDrawableList& fDrawables; |
mtklein | 11064df | 2016-04-06 15:01:57 -0700 | [diff] [blame] | 1508 | SkRect fBounds; |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1509 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1510 | template <typename T> |
| 1511 | void draw(const T& op, SkCanvas* canvas) { |
| 1512 | // We must pass SkMatrix::I() as our initial matrix. |
| 1513 | // By default SkRecords::Draw() uses the canvas' matrix as its initial matrix, |
| 1514 | // which would have the funky effect of applying transforms over and over. |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1515 | SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I()); |
| 1516 | d(op); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 1519 | // Draws get their own picture. |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1520 | template <typename T> |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 1521 | SK_WHEN(T::kTags & SkRecords::kDraw_Tag, void) operator()(const T& op) { |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1522 | SkPictureRecorder rec; |
mtklein | 11064df | 2016-04-06 15:01:57 -0700 | [diff] [blame] | 1523 | this->draw(op, rec.beginRecording(fBounds)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1524 | sk_sp<SkPicture> pic(rec.finishRecordingAsPicture()); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1525 | fCanvas->drawPicture(pic); |
| 1526 | } |
| 1527 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 1528 | // We'll just issue non-draws directly. |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1529 | template <typename T> |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 1530 | skstd::enable_if_t<!(T::kTags & SkRecords::kDraw_Tag), void> operator()(const T& op) { |
| 1531 | this->draw(op, fCanvas); |
| 1532 | } |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1533 | }; |
| 1534 | |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1535 | // Record Src into a picture, then record it into a macro picture with a sub-picture for each draw. |
| 1536 | // Then play back that macro picture into our wrapped sink. |
| 1537 | Error ViaSingletonPictures::draw( |
| 1538 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1539 | auto size = src.size(); |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1540 | return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1541 | // Use low-level (Skia-private) recording APIs so we can read the SkRecord. |
| 1542 | SkRecord skr; |
| 1543 | SkRecorder recorder(&skr, size.width(), size.height()); |
| 1544 | Error err = src.draw(&recorder); |
| 1545 | if (!err.isEmpty()) { |
| 1546 | return err; |
| 1547 | } |
| 1548 | |
| 1549 | // Record our macro-picture, with each draw op as its own sub-picture. |
| 1550 | SkPictureRecorder macroRec; |
| 1551 | SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()), |
| 1552 | SkIntToScalar(size.height())); |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1553 | |
| 1554 | SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList()); |
| 1555 | const SkDrawableList empty; |
| 1556 | |
| 1557 | DrawsAsSingletonPictures drawsAsSingletonPictures = { |
| 1558 | macroCanvas, |
| 1559 | drawables ? *drawables : empty, |
mtklein | 11064df | 2016-04-06 15:01:57 -0700 | [diff] [blame] | 1560 | SkRect::MakeWH((SkScalar)size.width(), (SkScalar)size.height()), |
mtklein | d2baa90 | 2015-07-07 09:43:28 -0700 | [diff] [blame] | 1561 | }; |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 1562 | for (int i = 0; i < skr.count(); i++) { |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 1563 | skr.visit(i, drawsAsSingletonPictures); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1564 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1565 | sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture()); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1566 | |
| 1567 | canvas->drawPicture(macroPic); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1568 | return check_against_reference(bitmap, src, fSink); |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 1569 | }); |
| 1570 | } |
| 1571 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1572 | } // namespace DM |