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