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