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