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