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