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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "dm/DMSrcSink.h" |
| 9 | #include "include/codec/SkAndroidCodec.h" |
| 10 | #include "include/codec/SkCodec.h" |
| 11 | #include "include/core/SkColorSpace.h" |
| 12 | #include "include/core/SkData.h" |
| 13 | #include "include/core/SkDeferredDisplayListRecorder.h" |
| 14 | #include "include/core/SkDocument.h" |
| 15 | #include "include/core/SkExecutor.h" |
| 16 | #include "include/core/SkImageGenerator.h" |
| 17 | #include "include/core/SkMallocPixelRef.h" |
| 18 | #include "include/core/SkMultiPictureDraw.h" |
| 19 | #include "include/core/SkPictureRecorder.h" |
| 20 | #include "include/core/SkStream.h" |
| 21 | #include "include/core/SkSurface.h" |
| 22 | #include "include/core/SkSurfaceCharacterization.h" |
| 23 | #include "include/docs/SkPDFDocument.h" |
| 24 | #include "include/gpu/GrBackendSurface.h" |
| 25 | #include "include/ports/SkImageGeneratorCG.h" |
| 26 | #include "include/ports/SkImageGeneratorWIC.h" |
| 27 | #include "include/private/SkImageInfoPriv.h" |
| 28 | #include "include/private/SkTLogic.h" |
Brian Osman | ea236bf | 2019-04-29 10:28:22 -0400 | [diff] [blame] | 29 | #include "include/third_party/skcms/skcms.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "include/utils/SkNullCanvas.h" |
| 31 | #include "include/utils/SkRandom.h" |
| 32 | #include "src/codec/SkCodecImageGenerator.h" |
| 33 | #include "src/codec/SkSwizzler.h" |
| 34 | #include "src/core/SkAutoMalloc.h" |
| 35 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/core/SkMakeUnique.h" |
| 37 | #include "src/core/SkOSFile.h" |
| 38 | #include "src/core/SkOpts.h" |
| 39 | #include "src/core/SkPictureCommon.h" |
| 40 | #include "src/core/SkPictureData.h" |
| 41 | #include "src/core/SkRecordDraw.h" |
| 42 | #include "src/core/SkRecorder.h" |
| 43 | #include "src/core/SkTaskGroup.h" |
| 44 | #include "src/gpu/GrContextPriv.h" |
| 45 | #include "src/gpu/GrGpu.h" |
| 46 | #include "src/utils/SkMultiPictureDocumentPriv.h" |
| 47 | #include "src/utils/SkOSPath.h" |
| 48 | #include "tools/DDLPromiseImageHelper.h" |
| 49 | #include "tools/DDLTileHelper.h" |
| 50 | #include "tools/Resources.h" |
| 51 | #include "tools/debugger/DebugCanvas.h" |
| 52 | #include "tools/gpu/MemoryCache.h" |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 53 | #if defined(SK_BUILD_FOR_WIN) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 54 | #include "include/docs/SkXPSDocument.h" |
| 55 | #include "src/utils/win/SkAutoCoInitialize.h" |
| 56 | #include "src/utils/win/SkHRESULT.h" |
| 57 | #include "src/utils/win/SkTScopedComPtr.h" |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 58 | #include <XpsObjectModel.h> |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
Florin Malita | 87ccf33 | 2018-05-04 12:23:24 -0400 | [diff] [blame] | 61 | #if defined(SK_ENABLE_SKOTTIE) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 62 | #include "modules/skottie/include/Skottie.h" |
| 63 | #include "modules/skottie/utils/SkottieUtils.h" |
Florin Malita | 124d5af | 2017-12-31 17:02:26 -0500 | [diff] [blame] | 64 | #endif |
| 65 | |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 66 | #if defined(SK_XML) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 67 | #include "experimental/svg/model/SkSVGDOM.h" |
| 68 | #include "include/svg/SkSVGCanvas.h" |
| 69 | #include "src/xml/SkXMLWriter.h" |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 70 | #endif |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 71 | #include "tests/TestUtils.h" |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 72 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 73 | #include <cmath> |
| 74 | #include <functional> |
| 75 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 76 | static DEFINE_bool(multiPage, false, |
| 77 | "For document-type backends, render the source into multiple pages"); |
| 78 | static DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?"); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 79 | |
Mike Klein | 4932a5e | 2019-03-22 11:08:02 -0500 | [diff] [blame] | 80 | DECLARE_int(gpuThreads); |
| 81 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 82 | using sk_gpu_test::GrContextFactory; |
| 83 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 84 | namespace DM { |
| 85 | |
Hal Canary | 972eba3 | 2018-07-30 17:07:07 -0400 | [diff] [blame] | 86 | GMSrc::GMSrc(skiagm::GMFactory factory) : fFactory(factory) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 87 | |
| 88 | Error GMSrc::draw(SkCanvas* canvas) const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 89 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 90 | SkString errorMsg; |
| 91 | skiagm::DrawResult drawResult = gm->draw(canvas, &errorMsg); |
| 92 | if (skiagm::DrawResult::kSkip == drawResult) { |
| 93 | return Error::Nonfatal(std::move(errorMsg)); // Cause this test to be skipped. |
| 94 | } |
Mike Klein | 16b1efb | 2019-04-02 10:01:11 -0400 | [diff] [blame] | 95 | return errorMsg; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | SkISize GMSrc::size() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 99 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 100 | return gm->getISize(); |
| 101 | } |
| 102 | |
| 103 | Name GMSrc::name() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 104 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 105 | return gm->getName(); |
| 106 | } |
| 107 | |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 108 | void GMSrc::modifyGrContextOptions(GrContextOptions* options) const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 109 | std::unique_ptr<skiagm::GM> gm(fFactory(nullptr)); |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 110 | gm->modifyGrContextOptions(options); |
| 111 | } |
| 112 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 113 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 114 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 115 | BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 116 | : fPath(path) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 117 | , fMode(mode) |
| 118 | , fDstColorType(dstColorType) |
| 119 | , fSampleSize(sampleSize) |
| 120 | {} |
| 121 | |
| 122 | bool BRDSrc::veto(SinkFlags flags) const { |
| 123 | // No need to test to non-raster or indirect backends. |
| 124 | return flags.type != SinkFlags::kRaster |
| 125 | || flags.approach != SinkFlags::kDirect; |
| 126 | } |
| 127 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 128 | static SkBitmapRegionDecoder* create_brd(Path path) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 129 | sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str())); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 130 | if (!encoded) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 131 | return nullptr; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 132 | } |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 133 | return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 136 | static inline void alpha8_to_gray8(SkBitmap* bitmap) { |
| 137 | // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert |
| 138 | // them back to kGray8 so our test framework can draw them correctly. |
| 139 | if (kAlpha_8_SkColorType == bitmap->info().colorType()) { |
| 140 | SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType) |
| 141 | .makeAlphaType(kOpaque_SkAlphaType); |
| 142 | *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo; |
| 143 | } |
| 144 | } |
| 145 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 146 | Error BRDSrc::draw(SkCanvas* canvas) const { |
| 147 | SkColorType colorType = canvas->imageInfo().colorType(); |
| 148 | if (kRGB_565_SkColorType == colorType && |
| 149 | CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) { |
| 150 | return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); |
| 151 | } |
| 152 | switch (fDstColorType) { |
| 153 | case CodecSrc::kGetFromCanvas_DstColorType: |
| 154 | break; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 155 | case CodecSrc::kGrayscale_Always_DstColorType: |
| 156 | colorType = kGray_8_SkColorType; |
| 157 | break; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 158 | default: |
| 159 | SkASSERT(false); |
| 160 | break; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 163 | std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 164 | if (nullptr == brd.get()) { |
| 165 | return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str())); |
| 166 | } |
| 167 | |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 168 | auto recommendedCT = brd->computeOutputColorType(colorType); |
| 169 | if (kRGB_565_SkColorType == colorType && recommendedCT != colorType) { |
| 170 | return Error::Nonfatal("Skip decoding non-opaque to 565."); |
Leon Scroggins III | 1dc8ecb | 2017-08-17 13:42:48 -0400 | [diff] [blame] | 171 | } |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 172 | colorType = recommendedCT; |
| 173 | |
| 174 | auto colorSpace = brd->computeOutputColorSpace(colorType, nullptr); |
Leon Scroggins III | 1dc8ecb | 2017-08-17 13:42:48 -0400 | [diff] [blame] | 175 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 176 | const uint32_t width = brd->width(); |
| 177 | const uint32_t height = brd->height(); |
| 178 | // Visually inspecting very small output images is not necessary. |
| 179 | if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) { |
| 180 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 181 | } |
| 182 | switch (fMode) { |
| 183 | case kFullImage_Mode: { |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 184 | SkBitmap bitmap; |
| 185 | if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height), |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 186 | fSampleSize, colorType, false, colorSpace)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 187 | return "Cannot decode (full) region."; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 188 | } |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 189 | alpha8_to_gray8(&bitmap); |
Matt Sarett | 8db74f1 | 2017-06-14 13:02:05 +0000 | [diff] [blame] | 190 | |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 191 | canvas->drawBitmap(bitmap, 0, 0); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 192 | return ""; |
| 193 | } |
| 194 | case kDivisor_Mode: { |
| 195 | const uint32_t divisor = 2; |
| 196 | if (width < divisor || height < divisor) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 197 | return Error::Nonfatal("Divisor is larger than image dimension."); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Use a border to test subsets that extend outside the image. |
| 201 | // We will not allow the border to be larger than the image dimensions. Allowing |
| 202 | // these large borders causes off by one errors that indicate a problem with the |
| 203 | // test suite, not a problem with the implementation. |
| 204 | const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * divisor); |
| 205 | const uint32_t scaledBorder = SkTMin(5u, maxBorder); |
| 206 | const uint32_t unscaledBorder = scaledBorder * fSampleSize; |
| 207 | |
| 208 | // We may need to clear the canvas to avoid uninitialized memory. |
| 209 | // Assume we are scaling a 780x780 image with sampleSize = 8. |
| 210 | // The output image should be 97x97. |
| 211 | // Each subset will be 390x390. |
| 212 | // Each scaled subset be 48x48. |
| 213 | // Four scaled subsets will only fill a 96x96 image. |
| 214 | // The bottom row and last column will not be touched. |
| 215 | // This is an unfortunate result of our rounding rules when scaling. |
| 216 | // Maybe we need to consider testing scaled subsets without trying to |
| 217 | // combine them to match the full scaled image? Or maybe this is the |
| 218 | // best we can do? |
| 219 | canvas->clear(0); |
| 220 | |
| 221 | for (uint32_t x = 0; x < divisor; x++) { |
| 222 | for (uint32_t y = 0; y < divisor; y++) { |
| 223 | // Calculate the subset dimensions |
| 224 | uint32_t subsetWidth = width / divisor; |
| 225 | uint32_t subsetHeight = height / divisor; |
| 226 | const int left = x * subsetWidth; |
| 227 | const int top = y * subsetHeight; |
| 228 | |
| 229 | // Increase the size of the last subset in each row or column, when the |
| 230 | // divisor does not divide evenly into the image dimensions |
| 231 | subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0; |
| 232 | subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0; |
| 233 | |
| 234 | // Increase the size of the subset in order to have a border on each side |
| 235 | const int decodeLeft = left - unscaledBorder; |
| 236 | const int decodeTop = top - unscaledBorder; |
| 237 | const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2; |
| 238 | const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 239 | SkBitmap bitmap; |
| 240 | if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft, |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 241 | decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false, |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 242 | colorSpace)) { |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 243 | return "Cannot decode region."; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 244 | } |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 245 | |
Matt Sarett | 334df3a | 2016-12-15 18:17:33 -0500 | [diff] [blame] | 246 | alpha8_to_gray8(&bitmap); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 247 | canvas->drawBitmapRect(bitmap, |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 248 | SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder, |
| 249 | (SkScalar) (subsetWidth / fSampleSize), |
| 250 | (SkScalar) (subsetHeight / fSampleSize)), |
| 251 | SkRect::MakeXYWH((SkScalar) (left / fSampleSize), |
| 252 | (SkScalar) (top / fSampleSize), |
| 253 | (SkScalar) (subsetWidth / fSampleSize), |
| 254 | (SkScalar) (subsetHeight / fSampleSize)), |
| 255 | nullptr); |
| 256 | } |
| 257 | } |
| 258 | return ""; |
| 259 | } |
| 260 | default: |
| 261 | SkASSERT(false); |
mtklein | 9b43915 | 2015-12-09 13:02:26 -0800 | [diff] [blame] | 262 | return "Error: Should not be reached."; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | SkISize BRDSrc::size() const { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 267 | std::unique_ptr<SkBitmapRegionDecoder> brd(create_brd(fPath)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 268 | if (brd) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 269 | return {SkTMax(1, brd->width() / (int)fSampleSize), |
| 270 | SkTMax(1, brd->height() / (int)fSampleSize)}; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 271 | } |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 272 | return {0, 0}; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static SkString get_scaled_name(const Path& path, float scale) { |
| 276 | return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale); |
| 277 | } |
| 278 | |
| 279 | Name BRDSrc::name() const { |
| 280 | // We will replicate the names used by CodecSrc so that images can |
| 281 | // be compared in Gold. |
| 282 | if (1 == fSampleSize) { |
| 283 | return SkOSPath::Basename(fPath.c_str()); |
| 284 | } |
msarett | 4b0778e | 2015-11-13 09:59:11 -0800 | [diff] [blame] | 285 | return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 289 | |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 290 | static bool serial_from_path_name(const SkString& path) { |
| 291 | if (!FLAGS_RAW_threading) { |
| 292 | static const char* const exts[] = { |
| 293 | "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw", |
| 294 | "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW", |
| 295 | }; |
| 296 | const char* actualExt = strrchr(path.c_str(), '.'); |
| 297 | if (actualExt) { |
| 298 | actualExt++; |
| 299 | for (auto* ext : exts) { |
| 300 | if (0 == strcmp(ext, actualExt)) { |
| 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 309 | CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType, |
| 310 | float scale) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 311 | : fPath(path) |
| 312 | , fMode(mode) |
| 313 | , fDstColorType(dstColorType) |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 314 | , fDstAlphaType(dstAlphaType) |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 315 | , fScale(scale) |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 316 | , fRunSerially(serial_from_path_name(path)) |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 317 | {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 318 | |
mtklein | 99cab4e | 2015-07-31 06:43:04 -0700 | [diff] [blame] | 319 | bool CodecSrc::veto(SinkFlags flags) const { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 320 | // Test to direct raster backends (8888 and 565). |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 321 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 322 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 323 | |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 324 | // Allows us to test decodes to non-native 8888. |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 325 | static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 326 | if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | for (int y = 0; y < bitmap.height(); y++) { |
| 331 | uint32_t* row = (uint32_t*) bitmap.getAddr(0, y); |
| 332 | SkOpts::RGBA_to_BGRA(row, row, bitmap.width()); |
| 333 | } |
| 334 | } |
| 335 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 336 | static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType, |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 337 | CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 338 | switch (dstColorType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 339 | case CodecSrc::kGrayscale_Always_DstColorType: |
scroggo | 1a36192 | 2016-05-20 14:27:16 -0700 | [diff] [blame] | 340 | if (kRGB_565_SkColorType == canvasColorType) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 341 | return false; |
| 342 | } |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 343 | *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 344 | break; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 345 | case CodecSrc::kNonNative8888_Always_DstColorType: |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 346 | if (kRGB_565_SkColorType == canvasColorType |
| 347 | || kRGBA_F16_SkColorType == canvasColorType) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | #ifdef SK_PMCOLOR_IS_RGBA |
| 351 | *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType); |
| 352 | #else |
| 353 | *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType); |
| 354 | #endif |
| 355 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 356 | default: |
msarett | 55f7bdd | 2016-02-16 13:24:54 -0800 | [diff] [blame] | 357 | if (kRGB_565_SkColorType == canvasColorType && |
| 358 | kOpaque_SkAlphaType != decodeInfo->alphaType()) { |
| 359 | return false; |
| 360 | } |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 361 | |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 362 | *decodeInfo = decodeInfo->makeColorType(canvasColorType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 366 | *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 370 | static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 371 | CodecSrc::DstColorType dstColorType, |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 372 | SkScalar left = 0, SkScalar top = 0) { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 373 | SkBitmap bitmap; |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 374 | bitmap.installPixels(info, pixels, rowBytes); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 375 | swap_rb_if_necessary(bitmap, dstColorType); |
| 376 | canvas->drawBitmap(bitmap, left, top); |
| 377 | } |
| 378 | |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 379 | // For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or |
| 380 | // color format conversions should be performed by the codec. Sometimes the output of the |
| 381 | // decode will be in an interesting color space. On our srgb and f16 backends, we need to |
| 382 | // "pretend" that the color space is standard sRGB to avoid triggering color conversion |
| 383 | // at draw time. |
| 384 | static void set_bitmap_color_space(SkImageInfo* info) { |
Brian Osman | 6b62296 | 2018-08-27 19:16:02 +0000 | [diff] [blame] | 385 | *info = info->makeColorSpace(SkColorSpace::MakeSRGB()); |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 386 | } |
| 387 | |
mtklein | e0effd6 | 2015-07-29 06:37:28 -0700 | [diff] [blame] | 388 | Error CodecSrc::draw(SkCanvas* canvas) const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 389 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 390 | if (!encoded) { |
| 391 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 392 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 393 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 394 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
msarett | 9e707a0 | 2015-09-01 14:57:57 -0700 | [diff] [blame] | 395 | if (nullptr == codec.get()) { |
| 396 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 397 | } |
| 398 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 399 | SkImageInfo decodeInfo = codec->getInfo(); |
| 400 | if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType, |
| 401 | fDstAlphaType)) { |
Matt Sarett | ea518f0 | 2017-02-03 11:58:59 -0800 | [diff] [blame] | 402 | return Error::Nonfatal("Skipping uninteresting test."); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 403 | } |
| 404 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 405 | // Try to scale the image if it is desired |
| 406 | SkISize size = codec->getScaledDimensions(fScale); |
| 407 | if (size == decodeInfo.dimensions() && 1.0f != fScale) { |
| 408 | return Error::Nonfatal("Test without scaling is uninteresting."); |
| 409 | } |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 410 | |
| 411 | // Visually inspecting very small output images is not necessary. We will |
| 412 | // cover these cases in unit testing. |
| 413 | if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) { |
| 414 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 415 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 416 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 417 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 418 | const int bpp = decodeInfo.bytesPerPixel(); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 419 | const size_t rowBytes = size.width() * bpp; |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 420 | const size_t safeSize = decodeInfo.computeByteSize(rowBytes); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 421 | SkAutoMalloc pixels(safeSize); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 422 | |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 423 | SkCodec::Options options; |
| 424 | if (kCodecZeroInit_Mode == fMode) { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 425 | memset(pixels.get(), 0, size.height() * rowBytes); |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 426 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 427 | } |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 428 | |
| 429 | SkImageInfo bitmapInfo = decodeInfo; |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 430 | set_bitmap_color_space(&bitmapInfo); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 431 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 432 | kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
| 433 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 434 | } |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 435 | |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 436 | switch (fMode) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 437 | case kAnimated_Mode: { |
| 438 | std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo(); |
| 439 | if (frameInfos.size() <= 1) { |
| 440 | return SkStringPrintf("%s is not an animated image.", fPath.c_str()); |
| 441 | } |
| 442 | |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 443 | // As in CodecSrc::size(), compute a roughly square grid to draw the frames |
| 444 | // into. "factor" is the number of frames to draw on one row. There will be |
| 445 | // up to "factor" rows as well. |
| 446 | const float root = sqrt((float) frameInfos.size()); |
| 447 | const int factor = sk_float_ceil2int(root); |
| 448 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 449 | // Used to cache a frame that future frames will depend on. |
| 450 | SkAutoMalloc priorFramePixels; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 451 | int cachedFrame = SkCodec::kNoFrame; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 452 | for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 453 | options.fFrameIndex = i; |
| 454 | // Check for a prior frame |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 455 | const int reqFrame = frameInfos[i].fRequiredFrame; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 456 | if (reqFrame != SkCodec::kNoFrame && reqFrame == cachedFrame |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 457 | && priorFramePixels.get()) { |
| 458 | // Copy into pixels |
| 459 | memcpy(pixels.get(), priorFramePixels.get(), safeSize); |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 460 | options.fPriorFrame = reqFrame; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 461 | } else { |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 462 | options.fPriorFrame = SkCodec::kNoFrame; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 463 | } |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 464 | SkCodec::Result result = codec->getPixels(decodeInfo, pixels.get(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 465 | rowBytes, &options); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 466 | if (SkCodec::kInvalidInput == result && i > 0) { |
| 467 | // Some of our test images have truncated later frames. Treat that |
| 468 | // the same as incomplete. |
| 469 | result = SkCodec::kIncompleteInput; |
| 470 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 471 | switch (result) { |
| 472 | case SkCodec::kSuccess: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 473 | case SkCodec::kErrorInInput: |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 474 | case SkCodec::kIncompleteInput: { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 475 | // If the next frame depends on this one, store it in priorFrame. |
| 476 | // It is possible that we may discard a frame that future frames depend on, |
| 477 | // but the codec will simply redecode the discarded frame. |
| 478 | // Do this before calling draw_to_canvas, which premultiplies in place. If |
| 479 | // we're decoding to unpremul, we want to pass the unmodified frame to the |
| 480 | // codec for decoding the next frame. |
| 481 | if (static_cast<size_t>(i+1) < frameInfos.size() |
| 482 | && frameInfos[i+1].fRequiredFrame == i) { |
| 483 | memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize); |
| 484 | cachedFrame = i; |
| 485 | } |
| 486 | |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 487 | SkAutoCanvasRestore acr(canvas, true); |
| 488 | const int xTranslate = (i % factor) * decodeInfo.width(); |
| 489 | const int yTranslate = (i / factor) * decodeInfo.height(); |
| 490 | canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate)); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 491 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType); |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 492 | if (result != SkCodec::kSuccess) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 493 | return ""; |
| 494 | } |
| 495 | break; |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 496 | } |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 497 | case SkCodec::kInvalidConversion: |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 498 | if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) { |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 499 | return Error::Nonfatal(SkStringPrintf( |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 500 | "Cannot decode frame %i to 565 (%s).", i, fPath.c_str())); |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 501 | } |
| 502 | // Fall through. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 503 | default: |
| 504 | return SkStringPrintf("Couldn't getPixels for frame %i in %s.", |
| 505 | i, fPath.c_str()); |
| 506 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 507 | } |
| 508 | break; |
| 509 | } |
msarett | bb25b53 | 2016-01-13 09:31:39 -0800 | [diff] [blame] | 510 | case kCodecZeroInit_Mode: |
msarett | 9e707a0 | 2015-09-01 14:57:57 -0700 | [diff] [blame] | 511 | case kCodec_Mode: { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 512 | switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 513 | case SkCodec::kSuccess: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 514 | // We consider these to be valid, since we should still decode what is |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 515 | // available. |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 516 | case SkCodec::kErrorInInput: |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 517 | case SkCodec::kIncompleteInput: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 518 | break; |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 519 | default: |
| 520 | // Everything else is considered a failure. |
| 521 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| 522 | } |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 523 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 524 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType); |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 525 | break; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 526 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 527 | case kScanline_Mode: { |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 528 | void* dst = pixels.get(); |
| 529 | uint32_t height = decodeInfo.height(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 530 | const bool useIncremental = [this]() { |
| 531 | auto exts = { "png", "PNG", "gif", "GIF" }; |
| 532 | for (auto ext : exts) { |
| 533 | if (fPath.endsWith(ext)) { |
| 534 | return true; |
| 535 | } |
| 536 | } |
| 537 | return false; |
| 538 | }(); |
| 539 | // ico may use the old scanline method or the new one, depending on whether it |
| 540 | // internally holds a bmp or a png. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 541 | const bool ico = fPath.endsWith("ico"); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 542 | bool useOldScanlineMethod = !useIncremental && !ico; |
| 543 | if (useIncremental || ico) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 544 | if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 545 | rowBytes, &options)) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 546 | int rowsDecoded; |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 547 | auto result = codec->incrementalDecode(&rowsDecoded); |
| 548 | if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 549 | codec->fillIncompleteImage(decodeInfo, dst, rowBytes, |
| 550 | SkCodec::kNo_ZeroInitialized, height, |
| 551 | rowsDecoded); |
scroggo | 6fb2391 | 2016-06-02 14:16:43 -0700 | [diff] [blame] | 552 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 553 | } else { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 554 | if (useIncremental) { |
| 555 | // Error: These should support incremental decode. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 556 | return "Could not start incremental decode"; |
| 557 | } |
| 558 | // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP, |
| 559 | // which should work via startScanlineDecode |
| 560 | useOldScanlineMethod = true; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | if (useOldScanlineMethod) { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 565 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 566 | return "Could not start scanline decoder"; |
| 567 | } |
| 568 | |
Brian Osman | ea176c6 | 2018-04-06 15:28:23 -0400 | [diff] [blame] | 569 | // We do not need to check the return value. On an incomplete |
| 570 | // image, memory will be filled with a default value. |
| 571 | codec->getScanlines(dst, height, rowBytes); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 574 | draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType); |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 575 | break; |
| 576 | } |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 577 | case kStripe_Mode: { |
| 578 | const int height = decodeInfo.height(); |
| 579 | // This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 580 | // does not align with image blocks. |
| 581 | const int stripeHeight = 37; |
| 582 | const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 583 | void* dst = pixels.get(); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 584 | |
| 585 | // Decode odd stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 586 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) { |
msarett | b65e604 | 2016-02-23 05:37:25 -0800 | [diff] [blame] | 587 | return "Could not start scanline decoder"; |
| 588 | } |
| 589 | |
| 590 | // This mode was designed to test the new skip scanlines API in libjpeg-turbo. |
| 591 | // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting |
| 592 | // 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] | 593 | // We only run this on Jpeg, which is always kTopDown. |
| 594 | SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 595 | |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 596 | for (int i = 0; i < numStripes; i += 2) { |
| 597 | // Skip a stripe |
| 598 | const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 599 | codec->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 600 | |
| 601 | // Read a stripe |
| 602 | const int startY = (i + 1) * stripeHeight; |
| 603 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 604 | if (linesToRead > 0) { |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 605 | codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead, |
| 606 | rowBytes); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | // Decode even stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 611 | const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 612 | if (SkCodec::kSuccess != startResult) { |
| 613 | return "Failed to restart scanline decoder with same parameters."; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 614 | } |
| 615 | for (int i = 0; i < numStripes; i += 2) { |
| 616 | // Read a stripe |
| 617 | const int startY = i * stripeHeight; |
| 618 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 619 | codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead, |
| 620 | rowBytes); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 621 | |
| 622 | // Skip a stripe |
msarett | f6db27e | 2015-06-12 09:34:04 -0700 | [diff] [blame] | 623 | const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); |
| 624 | if (linesToSkip > 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 625 | codec->skipScanlines(linesToSkip); |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 628 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 629 | draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 630 | break; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 631 | } |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 632 | case kCroppedScanline_Mode: { |
| 633 | const int width = decodeInfo.width(); |
| 634 | const int height = decodeInfo.height(); |
| 635 | // This value is chosen because, as we move across the image, it will sometimes |
| 636 | // align with the jpeg block sizes and it will sometimes not. This allows us |
| 637 | // to test interestingly different code paths in the implementation. |
| 638 | const int tileSize = 36; |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 639 | SkIRect subset; |
| 640 | for (int x = 0; x < width; x += tileSize) { |
| 641 | subset = SkIRect::MakeXYWH(x, 0, SkTMin(tileSize, width - x), height); |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 642 | options.fSubset = ⊂ |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 643 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) { |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 644 | return "Could not start scanline decoder."; |
| 645 | } |
| 646 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 647 | codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes); |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 650 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType); |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 651 | break; |
| 652 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 653 | case kSubset_Mode: { |
| 654 | // Arbitrarily choose a divisor. |
| 655 | int divisor = 2; |
| 656 | // Total width/height of the image. |
| 657 | const int W = codec->getInfo().width(); |
| 658 | const int H = codec->getInfo().height(); |
| 659 | if (divisor > W || divisor > H) { |
| 660 | return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divisor %d is too big " |
| 661 | "for %s with dimensions (%d x %d)", divisor, |
| 662 | fPath.c_str(), W, H)); |
| 663 | } |
| 664 | // subset dimensions |
| 665 | // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 666 | const int w = SkAlign2(W / divisor); |
| 667 | const int h = SkAlign2(H / divisor); |
| 668 | SkIRect subset; |
Matt Sarett | a40d9c8 | 2017-05-19 15:21:05 -0400 | [diff] [blame] | 669 | options.fSubset = ⊂ |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 670 | SkBitmap subsetBm; |
| 671 | // We will reuse pixel memory from bitmap. |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 672 | void* dst = pixels.get(); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 673 | // Keep track of left and top (for drawing subsetBm into canvas). We could use |
| 674 | // fScale * x and fScale * y, but we want integers such that the next subset will start |
| 675 | // where the last one ended. So we'll add decodeInfo.width() and height(). |
| 676 | int left = 0; |
| 677 | for (int x = 0; x < W; x += w) { |
| 678 | int top = 0; |
| 679 | for (int y = 0; y < H; y+= h) { |
| 680 | // Do not make the subset go off the edge of the image. |
| 681 | const int preScaleW = SkTMin(w, W - x); |
| 682 | const int preScaleH = SkTMin(h, H - y); |
| 683 | subset.setXYWH(x, y, preScaleW, preScaleH); |
| 684 | // And scale |
| 685 | // FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 686 | // into account? |
msarett | c7eb490 | 2016-04-25 07:04:58 -0700 | [diff] [blame] | 687 | const int scaledW = SkTMax(1, SkScalarRoundToInt(preScaleW * fScale)); |
| 688 | const int scaledH = SkTMax(1, SkScalarRoundToInt(preScaleH * fScale)); |
| 689 | decodeInfo = decodeInfo.makeWH(scaledW, scaledH); |
| 690 | SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 691 | size_t subsetRowBytes = subsetBitmapInfo.minRowBytes(); |
| 692 | const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 693 | &options); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 694 | switch (result) { |
| 695 | case SkCodec::kSuccess: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 696 | case SkCodec::kErrorInInput: |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 697 | case SkCodec::kIncompleteInput: |
| 698 | break; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 699 | default: |
| 700 | return SkStringPrintf("subset codec failed to decode (%d, %d, %d, %d) " |
| 701 | "from %s with dimensions (%d x %d)\t error %d", |
| 702 | x, y, decodeInfo.width(), decodeInfo.height(), |
| 703 | fPath.c_str(), W, H, result); |
| 704 | } |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 705 | draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType, |
| 706 | SkIntToScalar(left), SkIntToScalar(top)); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 707 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 708 | // translate by the scaled height. |
| 709 | top += decodeInfo.height(); |
| 710 | } |
| 711 | // translate by the scaled width. |
| 712 | left += decodeInfo.width(); |
| 713 | } |
| 714 | return ""; |
| 715 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 716 | default: |
| 717 | SkASSERT(false); |
| 718 | return "Invalid fMode"; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 719 | } |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 720 | return ""; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | SkISize CodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 724 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 725 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
scroggo | 7fac5af | 2015-09-30 11:33:12 -0700 | [diff] [blame] | 726 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 727 | return {0, 0}; |
scroggo | 7fac5af | 2015-09-30 11:33:12 -0700 | [diff] [blame] | 728 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 729 | |
| 730 | auto imageSize = codec->getScaledDimensions(fScale); |
| 731 | if (fMode == kAnimated_Mode) { |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 732 | // We'll draw one of each frame, so make it big enough to hold them all |
| 733 | // in a grid. The grid will be roughly square, with "factor" frames per |
| 734 | // row and up to "factor" rows. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 735 | const size_t count = codec->getFrameInfo().size(); |
scroggo | 90e971a | 2016-10-25 06:11:01 -0700 | [diff] [blame] | 736 | const float root = sqrt((float) count); |
| 737 | const int factor = sk_float_ceil2int(root); |
| 738 | imageSize.fWidth = imageSize.fWidth * factor; |
| 739 | imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 740 | } |
| 741 | return imageSize; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | Name CodecSrc::name() const { |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 745 | if (1.0f == fScale) { |
scroggo | 6e8c68e | 2016-10-24 13:48:49 -0700 | [diff] [blame] | 746 | Name name = SkOSPath::Basename(fPath.c_str()); |
| 747 | if (fMode == kAnimated_Mode) { |
| 748 | name.append("_animated"); |
| 749 | } |
| 750 | return name; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 751 | } |
scroggo | 6e8c68e | 2016-10-24 13:48:49 -0700 | [diff] [blame] | 752 | SkASSERT(fMode != kAnimated_Mode); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 753 | return get_scaled_name(fPath, fScale); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 757 | |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 758 | AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType, |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 759 | SkAlphaType dstAlphaType, int sampleSize) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 760 | : fPath(path) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 761 | , fDstColorType(dstColorType) |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 762 | , fDstAlphaType(dstAlphaType) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 763 | , fSampleSize(sampleSize) |
scroggo | 3ac66e9 | 2016-02-08 15:09:48 -0800 | [diff] [blame] | 764 | , fRunSerially(serial_from_path_name(path)) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 765 | {} |
| 766 | |
| 767 | bool AndroidCodecSrc::veto(SinkFlags flags) const { |
| 768 | // No need to test decoding to non-raster or indirect backend. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 769 | return flags.type != SinkFlags::kRaster |
| 770 | || flags.approach != SinkFlags::kDirect; |
| 771 | } |
| 772 | |
| 773 | Error AndroidCodecSrc::draw(SkCanvas* canvas) const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 774 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 775 | if (!encoded) { |
| 776 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 777 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 778 | std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded)); |
| 779 | if (nullptr == codec) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 780 | return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str()); |
| 781 | } |
| 782 | |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 783 | SkImageInfo decodeInfo = codec->getInfo(); |
| 784 | if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType, |
| 785 | fDstAlphaType)) { |
Matt Sarett | ea518f0 | 2017-02-03 11:58:59 -0800 | [diff] [blame] | 786 | return Error::Nonfatal("Skipping uninteresting test."); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // Scale the image if it is desired. |
| 790 | SkISize size = codec->getSampledDimensions(fSampleSize); |
| 791 | |
| 792 | // Visually inspecting very small output images is not necessary. We will |
| 793 | // cover these cases in unit testing. |
| 794 | if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) { |
| 795 | return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 796 | } |
| 797 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 798 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 799 | int bpp = decodeInfo.bytesPerPixel(); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 800 | size_t rowBytes = size.width() * bpp; |
| 801 | SkAutoMalloc pixels(size.height() * rowBytes); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 802 | |
| 803 | SkBitmap bitmap; |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 804 | SkImageInfo bitmapInfo = decodeInfo; |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 805 | set_bitmap_color_space(&bitmapInfo); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 806 | if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
| 807 | kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
| 808 | bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
| 809 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 810 | |
| 811 | // Create options for the codec. |
| 812 | SkAndroidCodec::AndroidOptions options; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 813 | options.fSampleSize = fSampleSize; |
| 814 | |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 815 | switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) { |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 816 | case SkCodec::kSuccess: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 817 | case SkCodec::kErrorInInput: |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 818 | case SkCodec::kIncompleteInput: |
| 819 | break; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 820 | default: |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 821 | return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 822 | } |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 823 | draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType); |
scroggo | f8dc9df | 2016-05-16 09:04:13 -0700 | [diff] [blame] | 824 | return ""; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | SkISize AndroidCodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 828 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 829 | std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded)); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 830 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 831 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 832 | } |
| 833 | return codec->getSampledDimensions(fSampleSize); |
| 834 | } |
| 835 | |
| 836 | Name AndroidCodecSrc::name() const { |
| 837 | // We will replicate the names used by CodecSrc so that images can |
| 838 | // be compared in Gold. |
| 839 | if (1 == fSampleSize) { |
| 840 | return SkOSPath::Basename(fPath.c_str()); |
| 841 | } |
msarett | 4b0778e | 2015-11-13 09:59:11 -0800 | [diff] [blame] | 842 | return get_scaled_name(fPath, 1.0f / (float) fSampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 846 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 847 | ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu) |
| 848 | : fPath(path) |
| 849 | , fMode(mode) |
| 850 | , fDstAlphaType(alphaType) |
| 851 | , fIsGpu(isGpu) |
| 852 | , fRunSerially(serial_from_path_name(path)) |
| 853 | {} |
| 854 | |
| 855 | bool ImageGenSrc::veto(SinkFlags flags) const { |
| 856 | if (fIsGpu) { |
Brian Salomon | bd7c551 | 2017-03-07 09:08:36 -0500 | [diff] [blame] | 857 | // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs. |
| 858 | return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect || |
| 859 | flags.multisampled == SinkFlags::kMultisampled; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
| 863 | } |
| 864 | |
| 865 | Error ImageGenSrc::draw(SkCanvas* canvas) const { |
| 866 | if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 867 | return Error::Nonfatal("Uninteresting to test image generator to 565."); |
| 868 | } |
| 869 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 870 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 871 | if (!encoded) { |
| 872 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 873 | } |
| 874 | |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 875 | #if defined(SK_BUILD_FOR_WIN) |
| 876 | // Initialize COM in order to test with WIC. |
| 877 | SkAutoCoInitialize com; |
| 878 | if (!com.succeeded()) { |
| 879 | return "Could not initialize COM."; |
| 880 | } |
| 881 | #endif |
| 882 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 883 | std::unique_ptr<SkImageGenerator> gen(nullptr); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 884 | switch (fMode) { |
| 885 | case kCodec_Mode: |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 886 | gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 887 | if (!gen) { |
| 888 | return "Could not create codec image generator."; |
| 889 | } |
| 890 | break; |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 891 | case kPlatform_Mode: { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 892 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame] | 893 | gen = SkImageGeneratorCG::MakeFromEncodedCG(encoded); |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 894 | #elif defined(SK_BUILD_FOR_WIN) |
Leon Scroggins III | a57488a | 2018-06-25 14:01:29 -0400 | [diff] [blame] | 895 | gen = SkImageGeneratorWIC::MakeFromEncodedWIC(encoded); |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 896 | #endif |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 897 | if (!gen) { |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 898 | return "Could not create platform image generator."; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 899 | } |
| 900 | break; |
msarett | fc0b6d1 | 2016-03-17 13:50:17 -0700 | [diff] [blame] | 901 | } |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 902 | default: |
| 903 | SkASSERT(false); |
| 904 | return "Invalid image generator mode"; |
| 905 | } |
| 906 | |
| 907 | // Test deferred decoding path on GPU |
| 908 | if (fIsGpu) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 909 | sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 910 | if (!image) { |
| 911 | return "Could not create image from codec image generator."; |
| 912 | } |
| 913 | canvas->drawImage(image, 0, 0); |
| 914 | return ""; |
| 915 | } |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 916 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 917 | // Test various color and alpha types on CPU |
| 918 | SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType); |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 919 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 920 | int bpp = decodeInfo.bytesPerPixel(); |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 921 | size_t rowBytes = decodeInfo.width() * bpp; |
| 922 | SkAutoMalloc pixels(decodeInfo.height() * rowBytes); |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 923 | if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes)) { |
Matt Sarett | 05cb4c3 | 2017-03-02 12:07:46 -0500 | [diff] [blame] | 924 | SkString err = |
| 925 | SkStringPrintf("Image generator could not getPixels() for %s\n", fPath.c_str()); |
| 926 | |
| 927 | #if defined(SK_BUILD_FOR_WIN) |
| 928 | if (kPlatform_Mode == fMode) { |
| 929 | // Do not issue a fatal error for WIC flakiness. |
| 930 | return Error::Nonfatal(err); |
| 931 | } |
| 932 | #endif |
| 933 | |
| 934 | return err; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 935 | } |
| 936 | |
Matt Sarett | f485cf9 | 2017-05-26 08:58:03 -0400 | [diff] [blame] | 937 | set_bitmap_color_space(&decodeInfo); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 938 | draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes, |
msarett | b1be46b | 2016-05-17 08:52:11 -0700 | [diff] [blame] | 939 | CodecSrc::kGetFromCanvas_DstColorType); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 940 | return ""; |
| 941 | } |
| 942 | |
| 943 | SkISize ImageGenSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 944 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 945 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 946 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 947 | return {0, 0}; |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 948 | } |
| 949 | return codec->getInfo().dimensions(); |
| 950 | } |
| 951 | |
| 952 | Name ImageGenSrc::name() const { |
| 953 | return SkOSPath::Basename(fPath.c_str()); |
| 954 | } |
| 955 | |
| 956 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 957 | |
Mike Klein | 0d5d142 | 2019-03-06 10:56:04 -0600 | [diff] [blame] | 958 | ColorCodecSrc::ColorCodecSrc(Path path, bool decode_to_dst) : fPath(path) |
| 959 | , fDecodeToDst(decode_to_dst) {} |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 960 | |
| 961 | bool ColorCodecSrc::veto(SinkFlags flags) const { |
| 962 | // Test to direct raster backends (8888 and 565). |
| 963 | return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; |
| 964 | } |
| 965 | |
| 966 | Error ColorCodecSrc::draw(SkCanvas* canvas) const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 967 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 968 | if (!encoded) { |
| 969 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 970 | } |
| 971 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 972 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
| 973 | if (nullptr == codec) { |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 974 | return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| 975 | } |
| 976 | |
Mike Klein | 0d5d142 | 2019-03-06 10:56:04 -0600 | [diff] [blame] | 977 | SkImageInfo info = codec->getInfo(); |
| 978 | if (fDecodeToDst) { |
| 979 | info = canvas->imageInfo().makeWH(info.width(), |
| 980 | info.height()); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | SkBitmap bitmap; |
Mike Klein | 0d5d142 | 2019-03-06 10:56:04 -0600 | [diff] [blame] | 984 | if (!bitmap.tryAllocPixels(info)) { |
| 985 | return SkStringPrintf("Image(%s) is too large (%d x %d)", |
| 986 | fPath.c_str(), info.width(), info.height()); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Mike Klein | 0d5d142 | 2019-03-06 10:56:04 -0600 | [diff] [blame] | 989 | switch (auto r = codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes())) { |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 990 | case SkCodec::kSuccess: |
| 991 | case SkCodec::kErrorInInput: |
| 992 | case SkCodec::kIncompleteInput: |
Mike Klein | 0d5d142 | 2019-03-06 10:56:04 -0600 | [diff] [blame] | 993 | canvas->drawBitmap(bitmap, 0,0); |
| 994 | return ""; |
| 995 | case SkCodec::kInvalidConversion: |
| 996 | // TODO(mtklein): why are there formats we can't decode to? |
| 997 | return Error::Nonfatal("SkCodec can't decode to this format."); |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 998 | default: |
| 999 | return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 1000 | } |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | SkISize ColorCodecSrc::size() const { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 1004 | sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 1005 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1006 | if (nullptr == codec) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1007 | return {0, 0}; |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1008 | } |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1009 | return {codec->getInfo().width(), codec->getInfo().height()}; |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | Name ColorCodecSrc::name() const { |
| 1013 | return SkOSPath::Basename(fPath.c_str()); |
| 1014 | } |
| 1015 | |
| 1016 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1017 | |
Mike Klein | 19fb397 | 2019-03-21 13:08:08 -0500 | [diff] [blame] | 1018 | static DEFINE_int(skpViewportSize, 1000, |
| 1019 | "Width & height of the viewport used to crop skp rendering."); |
| 1020 | |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1021 | SKPSrc::SKPSrc(Path path) : fPath(path) { } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1022 | |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1023 | Error SKPSrc::draw(SkCanvas* canvas) const { |
Hal Canary | 72b91cb | 2019-01-14 12:47:59 -0500 | [diff] [blame] | 1024 | std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str()); |
| 1025 | if (!stream) { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1026 | return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 1027 | } |
Hal Canary | 72b91cb | 2019-01-14 12:47:59 -0500 | [diff] [blame] | 1028 | sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get())); |
| 1029 | if (!pic) { |
| 1030 | return SkStringPrintf("Couldn't parse file %s.", fPath.c_str()); |
| 1031 | } |
| 1032 | stream = nullptr; // Might as well drop this when we're done with it. |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1033 | canvas->clipRect(SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1034 | canvas->drawPicture(pic); |
| 1035 | return ""; |
| 1036 | } |
| 1037 | |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1038 | static SkRect get_cull_rect_for_skp(const char* path) { |
| 1039 | std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path); |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1040 | if (!stream) { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1041 | return SkRect::MakeEmpty(); |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1042 | } |
| 1043 | SkPictInfo info; |
Mike Reed | e7a5832 | 2017-12-20 14:09:20 -0500 | [diff] [blame] | 1044 | if (!SkPicture_StreamIsSKP(stream.get(), &info)) { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1045 | return SkRect::MakeEmpty(); |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1046 | } |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 1047 | |
| 1048 | return info.fCullRect; |
| 1049 | } |
| 1050 | |
| 1051 | SkISize SKPSrc::size() const { |
| 1052 | SkRect viewport = get_cull_rect_for_skp(fPath.c_str()); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1053 | if (!viewport.intersect((SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize)))) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1054 | return {0, 0}; |
mtklein | ffa901a | 2015-03-16 10:38:07 -0700 | [diff] [blame] | 1055 | } |
| 1056 | return viewport.roundOut().size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 1060 | |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1061 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1062 | |
Chris Dalton | 184c37e | 2018-09-28 11:27:39 -0600 | [diff] [blame] | 1063 | BisectSrc::BisectSrc(Path path, const char* trail) : INHERITED(path), fTrail(trail) {} |
| 1064 | |
| 1065 | Error BisectSrc::draw(SkCanvas* canvas) const { |
| 1066 | struct FoundPath { |
| 1067 | SkPath fPath; |
| 1068 | SkPaint fPaint; |
| 1069 | SkMatrix fViewMatrix; |
| 1070 | }; |
| 1071 | |
| 1072 | // This subclass of SkCanvas just extracts all the SkPaths (drawn via drawPath) from an SKP. |
| 1073 | class PathFindingCanvas : public SkCanvas { |
| 1074 | public: |
| 1075 | PathFindingCanvas(int width, int height) : SkCanvas(width, height, nullptr) {} |
| 1076 | const SkTArray<FoundPath>& foundPaths() const { return fFoundPaths; } |
| 1077 | |
| 1078 | private: |
| 1079 | void onDrawPath(const SkPath& path, const SkPaint& paint) override { |
| 1080 | fFoundPaths.push_back() = {path, paint, this->getTotalMatrix()}; |
| 1081 | } |
| 1082 | |
| 1083 | SkTArray<FoundPath> fFoundPaths; |
| 1084 | }; |
| 1085 | |
| 1086 | PathFindingCanvas pathFinder(canvas->getBaseLayerSize().width(), |
| 1087 | canvas->getBaseLayerSize().height()); |
| 1088 | Error err = this->INHERITED::draw(&pathFinder); |
| 1089 | if (!err.isEmpty()) { |
| 1090 | return err; |
| 1091 | } |
| 1092 | |
| 1093 | int start = 0, end = pathFinder.foundPaths().count(); |
| 1094 | for (const char* ch = fTrail.c_str(); *ch; ++ch) { |
| 1095 | int midpt = (start + end) / 2; |
| 1096 | if ('l' == *ch) { |
| 1097 | start = midpt; |
| 1098 | } else if ('r' == *ch) { |
| 1099 | end = midpt; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | for (int i = start; i < end; ++i) { |
| 1104 | const FoundPath& path = pathFinder.foundPaths()[i]; |
| 1105 | SkAutoCanvasRestore acr(canvas, true); |
| 1106 | canvas->concat(path.fViewMatrix); |
| 1107 | canvas->drawPath(path.fPath, path.fPaint); |
| 1108 | } |
| 1109 | |
| 1110 | return ""; |
| 1111 | } |
| 1112 | |
| 1113 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1114 | |
Florin Malita | 87ccf33 | 2018-05-04 12:23:24 -0400 | [diff] [blame] | 1115 | #if defined(SK_ENABLE_SKOTTIE) |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1116 | SkottieSrc::SkottieSrc(Path path) : fPath(std::move(path)) {} |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1117 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 1118 | Error SkottieSrc::draw(SkCanvas* canvas) const { |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 1119 | auto animation = skottie::Animation::Builder() |
| 1120 | .setResourceProvider( |
| 1121 | skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()))) |
| 1122 | .makeFromFile(fPath.c_str()); |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1123 | if (!animation) { |
| 1124 | return SkStringPrintf("Unable to parse file: %s", fPath.c_str()); |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | canvas->drawColor(SK_ColorWHITE); |
| 1128 | |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 1129 | const auto t_rate = 1.0f / (kTileCount * kTileCount - 1); |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1130 | |
Kevin Lubick | c5fe15d | 2018-08-29 09:52:52 -0400 | [diff] [blame] | 1131 | // Draw the frames in a shuffled order to exercise non-linear |
| 1132 | // frame progression. The film strip will still be in order left-to-right, |
| 1133 | // top-down, just not drawn in that order. |
| 1134 | static constexpr int frameOrder[] = { 4, 0, 3, 1, 2 }; |
| 1135 | static_assert(SK_ARRAY_COUNT(frameOrder) == kTileCount, ""); |
Florin Malita | d3c1b84 | 2018-01-27 12:43:24 -0500 | [diff] [blame] | 1136 | |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1137 | for (int i = 0; i < kTileCount; ++i) { |
Kevin Lubick | c5fe15d | 2018-08-29 09:52:52 -0400 | [diff] [blame] | 1138 | const SkScalar y = frameOrder[i] * kTileSize; |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1139 | |
| 1140 | for (int j = 0; j < kTileCount; ++j) { |
Kevin Lubick | c5fe15d | 2018-08-29 09:52:52 -0400 | [diff] [blame] | 1141 | const SkScalar x = frameOrder[j] * kTileSize; |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1142 | SkRect dest = SkRect::MakeXYWH(x, y, kTileSize, kTileSize); |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1143 | |
Kevin Lubick | c5fe15d | 2018-08-29 09:52:52 -0400 | [diff] [blame] | 1144 | const auto t = t_rate * (frameOrder[i] * kTileCount + frameOrder[j]); |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 1145 | { |
| 1146 | SkAutoCanvasRestore acr(canvas, true); |
Florin Malita | d3c1b84 | 2018-01-27 12:43:24 -0500 | [diff] [blame] | 1147 | canvas->clipRect(dest, true); |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1148 | canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()), |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 1149 | dest, |
Florin Malita | 3fae0f3 | 2018-03-15 21:52:54 -0400 | [diff] [blame] | 1150 | SkMatrix::kCenter_ScaleToFit)); |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1151 | animation->seek(t); |
| 1152 | animation->render(canvas); |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 1153 | } |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | return ""; |
| 1158 | } |
| 1159 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 1160 | SkISize SkottieSrc::size() const { |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1161 | return SkISize::Make(kTargetSize, kTargetSize); |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1162 | } |
| 1163 | |
Florin Malita | 9f7d4cd | 2018-07-30 15:49:20 -0400 | [diff] [blame] | 1164 | Name SkottieSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1165 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 1166 | bool SkottieSrc::veto(SinkFlags flags) const { |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1167 | // No need to test to non-(raster||gpu||vector) or indirect backends. |
| 1168 | bool type_ok = flags.type == SinkFlags::kRaster |
| 1169 | || flags.type == SinkFlags::kGPU |
| 1170 | || flags.type == SinkFlags::kVector; |
| 1171 | |
| 1172 | return !type_ok || flags.approach != SinkFlags::kDirect; |
| 1173 | } |
Florin Malita | 124d5af | 2017-12-31 17:02:26 -0500 | [diff] [blame] | 1174 | #endif |
Florin Malita | fc043dc | 2017-12-31 11:08:42 -0500 | [diff] [blame] | 1175 | |
| 1176 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1177 | #if defined(SK_XML) |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1178 | // Used when the image doesn't have an intrinsic size. |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1179 | static const SkSize kDefaultSVGSize = {1000, 1000}; |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1180 | |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1181 | // Used to force-scale tiny fixed-size images. |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1182 | static const SkSize kMinimumSVGSize = {128, 128}; |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1183 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1184 | SVGSrc::SVGSrc(Path path) |
| 1185 | : fName(SkOSPath::Basename(path.c_str())) |
| 1186 | , fScale(1) { |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1187 | |
Brian Osman | 133823d | 2018-09-19 14:14:15 -0400 | [diff] [blame] | 1188 | sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str())); |
| 1189 | if (!data) { |
| 1190 | return; |
| 1191 | } |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1192 | |
Brian Osman | 133823d | 2018-09-19 14:14:15 -0400 | [diff] [blame] | 1193 | SkMemoryStream stream(std::move(data)); |
| 1194 | fDom = SkSVGDOM::MakeFromStream(stream); |
| 1195 | if (!fDom) { |
| 1196 | return; |
| 1197 | } |
| 1198 | |
| 1199 | const SkSize& sz = fDom->containerSize(); |
| 1200 | if (sz.isEmpty()) { |
| 1201 | // no intrinsic size |
| 1202 | fDom->setContainerSize(kDefaultSVGSize); |
| 1203 | } else { |
| 1204 | fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(), |
| 1205 | kMinimumSVGSize.height() / sz.height())); |
| 1206 | } |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | Error SVGSrc::draw(SkCanvas* canvas) const { |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1210 | if (!fDom) { |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1211 | return SkStringPrintf("Unable to parse file: %s", fName.c_str()); |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1214 | SkAutoCanvasRestore acr(canvas, true); |
| 1215 | canvas->scale(fScale, fScale); |
| 1216 | fDom->render(canvas); |
| 1217 | |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1218 | return ""; |
| 1219 | } |
| 1220 | |
| 1221 | SkISize SVGSrc::size() const { |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1222 | if (!fDom) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1223 | return {0, 0}; |
fmalita | bdf3e5c | 2016-09-17 07:26:26 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1226 | return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale} |
| 1227 | .toRound(); |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
fmalita | acd2f5c | 2016-11-08 07:13:45 -0800 | [diff] [blame] | 1230 | Name SVGSrc::name() const { return fName; } |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1231 | |
fmalita | 179d885 | 2016-08-16 14:23:29 -0700 | [diff] [blame] | 1232 | bool SVGSrc::veto(SinkFlags flags) const { |
Florin Malita | 93323eb | 2017-06-16 15:31:17 -0400 | [diff] [blame] | 1233 | // No need to test to non-(raster||gpu||vector) or indirect backends. |
fmalita | 179d885 | 2016-08-16 14:23:29 -0700 | [diff] [blame] | 1234 | bool type_ok = flags.type == SinkFlags::kRaster |
Florin Malita | 93323eb | 2017-06-16 15:31:17 -0400 | [diff] [blame] | 1235 | || flags.type == SinkFlags::kGPU |
| 1236 | || flags.type == SinkFlags::kVector; |
fmalita | 179d885 | 2016-08-16 14:23:29 -0700 | [diff] [blame] | 1237 | |
| 1238 | return !type_ok || flags.approach != SinkFlags::kDirect; |
| 1239 | } |
| 1240 | |
fmalita | a2b9fdf | 2016-08-03 19:53:36 -0700 | [diff] [blame] | 1241 | #endif // defined(SK_XML) |
| 1242 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1243 | |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1244 | MSKPSrc::MSKPSrc(Path path) : fPath(path) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1245 | std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str()); |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1246 | int count = SkMultiPictureDocumentReadPageCount(stream.get()); |
| 1247 | if (count > 0) { |
| 1248 | fPages.reset(count); |
| 1249 | (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count()); |
| 1250 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1253 | int MSKPSrc::pageCount() const { return fPages.count(); } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1254 | |
| 1255 | SkISize MSKPSrc::size() const { return this->size(0); } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1256 | SkISize MSKPSrc::size(int i) const { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1257 | return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0}; |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1258 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1259 | |
| 1260 | Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); } |
| 1261 | Error MSKPSrc::draw(int i, SkCanvas* canvas) const { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1262 | if (this->pageCount() == 0) { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1263 | return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str()); |
| 1264 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1265 | if (i >= fPages.count() || i < 0) { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1266 | return SkStringPrintf("MultiPictureDocument page number out of range: %d", i); |
| 1267 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1268 | SkPicture* page = fPages[i].fPicture.get(); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1269 | if (!page) { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 1270 | std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str()); |
| 1271 | if (!stream) { |
| 1272 | return SkStringPrintf("Unable to open file: %s", fPath.c_str()); |
| 1273 | } |
| 1274 | if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) { |
| 1275 | return SkStringPrintf("SkMultiPictureDocument reader failed on page %d: %s", i, |
| 1276 | fPath.c_str()); |
| 1277 | } |
| 1278 | page = fPages[i].fPicture.get(); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1279 | } |
| 1280 | canvas->drawPicture(page); |
| 1281 | return ""; |
| 1282 | } |
| 1283 | |
| 1284 | Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 1285 | |
| 1286 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1287 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 1288 | Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1289 | return src.draw(SkMakeNullCanvas().get()); |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1293 | |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1294 | static Error compare_bitmaps(const SkBitmap& reference, const SkBitmap& bitmap) { |
| 1295 | // The dimensions are a property of the Src only, and so should be identical. |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 1296 | SkASSERT(reference.computeByteSize() == bitmap.computeByteSize()); |
| 1297 | if (reference.computeByteSize() != bitmap.computeByteSize()) { |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1298 | return "Dimensions don't match reference"; |
| 1299 | } |
| 1300 | // All SkBitmaps in DM are tight, so this comparison is easy. |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 1301 | if (0 != memcmp(reference.getPixels(), bitmap.getPixels(), reference.computeByteSize())) { |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1302 | SkString encoded; |
| 1303 | SkString errString("Pixels don't match reference"); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 1304 | if (bitmap_to_base64_data_uri(reference, &encoded)) { |
| 1305 | errString.append("\nExpected: "); |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1306 | errString.append(encoded); |
| 1307 | } else { |
| 1308 | errString.append("\nExpected image failed to encode: "); |
| 1309 | errString.append(encoded); |
| 1310 | } |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 1311 | if (bitmap_to_base64_data_uri(bitmap, &encoded)) { |
| 1312 | errString.append("\nActual: "); |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1313 | errString.append(encoded); |
| 1314 | } else { |
| 1315 | errString.append("\nActual image failed to encode: "); |
| 1316 | errString.append(encoded); |
| 1317 | } |
| 1318 | return errString; |
| 1319 | } |
| 1320 | return ""; |
| 1321 | } |
| 1322 | |
| 1323 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1324 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 1325 | static DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?"); |
Mike Klein | 19fb397 | 2019-03-21 13:08:08 -0500 | [diff] [blame] | 1326 | static DEFINE_bool(preAbandonGpuContext, false, |
| 1327 | "Test abandoning the GrContext before running the test."); |
| 1328 | static DEFINE_bool(abandonGpuContext, false, |
| 1329 | "Test abandoning the GrContext after running each test."); |
| 1330 | static DEFINE_bool(releaseAndAbandonGpuContext, false, |
| 1331 | "Test releasing all gpu resources and abandoning the GrContext " |
| 1332 | "after running each test"); |
| 1333 | static DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing."); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1334 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 1335 | GPUSink::GPUSink(GrContextFactory::ContextType ct, |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 1336 | GrContextFactory::ContextOverrides overrides, |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1337 | SkCommandLineConfigGpu::SurfType surfType, |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 1338 | int samples, |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 1339 | bool diText, |
brianosman | d93c120 | 2016-03-10 07:49:08 -0800 | [diff] [blame] | 1340 | SkColorType colorType, |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1341 | SkAlphaType alphaType, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1342 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | f21aa04 | 2017-08-21 16:48:46 -0400 | [diff] [blame] | 1343 | bool threaded, |
| 1344 | const GrContextOptions& grCtxOptions) |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1345 | : fContextType(ct) |
| 1346 | , fContextOverrides(overrides) |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1347 | , fSurfType(surfType) |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1348 | , fSampleCount(samples) |
| 1349 | , fUseDIText(diText) |
| 1350 | , fColorType(colorType) |
| 1351 | , fAlphaType(alphaType) |
| 1352 | , fColorSpace(std::move(colorSpace)) |
Brian Osman | f21aa04 | 2017-08-21 16:48:46 -0400 | [diff] [blame] | 1353 | , fThreaded(threaded) |
| 1354 | , fBaseContextOptions(grCtxOptions) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1355 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1356 | Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const { |
| 1357 | return this->onDraw(src, dst, dstStream, log, fBaseContextOptions); |
| 1358 | } |
| 1359 | |
| 1360 | Error GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log, |
| 1361 | const GrContextOptions& baseOptions) const { |
| 1362 | GrContextOptions grOptions = baseOptions; |
kkinnunen | 64492c4 | 2015-12-08 01:24:40 -0800 | [diff] [blame] | 1363 | |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1364 | // We don't expect the src to mess with the persistent cache or the executor. |
| 1365 | SkDEBUGCODE(auto cache = grOptions.fPersistentCache); |
| 1366 | SkDEBUGCODE(auto exec = grOptions.fExecutor); |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1367 | src.modifyGrContextOptions(&grOptions); |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1368 | SkASSERT(cache == grOptions.fPersistentCache); |
| 1369 | SkASSERT(exec == grOptions.fExecutor); |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1370 | |
| 1371 | GrContextFactory factory(grOptions); |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1372 | const SkISize size = src.size(); |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1373 | SkImageInfo info = |
| 1374 | SkImageInfo::Make(size.width(), size.height(), fColorType, fAlphaType, fColorSpace); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1375 | sk_sp<SkSurface> surface; |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 1376 | GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 1377 | const int maxDimension = context->priv().caps()->maxTextureSize(); |
msarett | 13a036b | 2016-02-08 09:10:47 -0800 | [diff] [blame] | 1378 | if (maxDimension < SkTMax(size.width(), size.height())) { |
| 1379 | return Error::Nonfatal("Src too large to create a texture.\n"); |
| 1380 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1381 | uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0; |
| 1382 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 1383 | GrBackendTexture backendTexture; |
| 1384 | GrBackendRenderTarget backendRT; |
| 1385 | switch (fSurfType) { |
| 1386 | case SkCommandLineConfigGpu::SurfType::kDefault: |
| 1387 | surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount, |
| 1388 | &props); |
| 1389 | break; |
| 1390 | case SkCommandLineConfigGpu::SurfType::kBackendTexture: |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 1391 | backendTexture = context->createBackendTexture( |
Robert Phillips | 8062679 | 2019-06-04 07:16:10 -0400 | [diff] [blame] | 1392 | info.width(), info.height(), info.colorType(), SkColors::kTransparent, |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 1393 | GrMipMapped::kNo, GrRenderable::kYes); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1394 | surface = SkSurface::MakeFromBackendTexture(context, backendTexture, |
| 1395 | kTopLeft_GrSurfaceOrigin, fSampleCount, |
Brian Salomon | 5370677 | 2018-03-19 14:18:08 -0400 | [diff] [blame] | 1396 | fColorType, info.refColorSpace(), &props); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1397 | break; |
| 1398 | case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget: |
| 1399 | if (1 == fSampleCount) { |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1400 | auto colorType = SkColorTypeToGrColorType(info.colorType()); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 1401 | backendRT = context->priv().getGpu()->createTestingOnlyBackendRenderTarget( |
Brian Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame] | 1402 | info.width(), info.height(), colorType); |
Brian Salomon | 49edccd | 2018-03-23 15:31:32 -0400 | [diff] [blame] | 1403 | surface = SkSurface::MakeFromBackendRenderTarget( |
| 1404 | context, backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(), |
| 1405 | info.refColorSpace(), &props); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1406 | } |
| 1407 | break; |
| 1408 | } |
msarett | 13a036b | 2016-02-08 09:10:47 -0800 | [diff] [blame] | 1409 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1410 | if (!surface) { |
| 1411 | return "Could not create a surface."; |
| 1412 | } |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1413 | if (FLAGS_preAbandonGpuContext) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1414 | factory.abandonContexts(); |
| 1415 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1416 | SkCanvas* canvas = surface->getCanvas(); |
| 1417 | Error err = src.draw(canvas); |
| 1418 | if (!err.isEmpty()) { |
| 1419 | return err; |
| 1420 | } |
Robert Phillips | 9882dae | 2019-03-04 11:00:10 -0500 | [diff] [blame] | 1421 | surface->flush(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1422 | if (FLAGS_gpuStats) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 1423 | canvas->getGrContext()->priv().dumpCacheStats(log); |
| 1424 | canvas->getGrContext()->priv().dumpGpuStats(log); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1425 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 1426 | if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType || |
| 1427 | info.colorType() == kRGB_888x_SkColorType) { |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 1428 | // We don't currently support readbacks into these formats on the GPU backend. Convert to |
| 1429 | // 32 bit. |
| 1430 | info = SkImageInfo::Make(size.width(), size.height(), kRGBA_8888_SkColorType, |
| 1431 | kPremul_SkAlphaType, fColorSpace); |
| 1432 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1433 | dst->allocPixels(info); |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 1434 | canvas->readPixels(*dst, 0, 0); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 1435 | if (FLAGS_abandonGpuContext) { |
| 1436 | factory.abandonContexts(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 1437 | } else if (FLAGS_releaseAndAbandonGpuContext) { |
| 1438 | factory.releaseResourcesAndAbandonContexts(); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 1439 | } |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 1440 | if (!context->abandoned()) { |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1441 | surface.reset(); |
| 1442 | if (backendTexture.isValid()) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 1443 | context->deleteBackendTexture(backendTexture); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1444 | } |
| 1445 | if (backendRT.isValid()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 1446 | context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1447 | } |
| 1448 | } |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 1449 | if (grOptions.fPersistentCache) { |
| 1450 | context->storeVkPipelineCacheData(); |
| 1451 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1452 | return ""; |
| 1453 | } |
| 1454 | |
| 1455 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1456 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1457 | GPUThreadTestingSink::GPUThreadTestingSink(GrContextFactory::ContextType ct, |
| 1458 | GrContextFactory::ContextOverrides overrides, |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1459 | SkCommandLineConfigGpu::SurfType surfType, |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1460 | int samples, |
| 1461 | bool diText, |
| 1462 | SkColorType colorType, |
| 1463 | SkAlphaType alphaType, |
| 1464 | sk_sp<SkColorSpace> colorSpace, |
| 1465 | bool threaded, |
| 1466 | const GrContextOptions& grCtxOptions) |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1467 | : INHERITED(ct, overrides, surfType, samples, diText, colorType, alphaType, |
| 1468 | std::move(colorSpace), threaded, grCtxOptions) |
Mike Klein | 022cfa2 | 2017-09-01 11:53:16 -0400 | [diff] [blame] | 1469 | , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)) { |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1470 | SkASSERT(fExecutor); |
| 1471 | } |
| 1472 | |
| 1473 | Error GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream, |
| 1474 | SkString* log) const { |
| 1475 | // Draw twice, once with worker threads, and once without. Verify that we get the same result. |
| 1476 | // Also, force us to only use the software path renderer, so we really stress-test the threaded |
| 1477 | // version of that code. |
| 1478 | GrContextOptions contextOptions = this->baseContextOptions(); |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 1479 | contextOptions.fGpuPathRenderers = GpuPathRenderers::kNone; |
Greg Daniel | 024615e | 2018-05-10 17:25:52 +0000 | [diff] [blame] | 1480 | contextOptions.fExecutor = fExecutor.get(); |
Adrienne Walker | ab7181d | 2018-05-14 14:02:03 -0700 | [diff] [blame] | 1481 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1482 | Error err = this->onDraw(src, dst, wStream, log, contextOptions); |
| 1483 | if (!err.isEmpty() || !dst) { |
| 1484 | return err; |
| 1485 | } |
| 1486 | |
| 1487 | SkBitmap reference; |
| 1488 | SkString refLog; |
| 1489 | SkDynamicMemoryWStream refStream; |
| 1490 | contextOptions.fExecutor = nullptr; |
| 1491 | Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions); |
| 1492 | if (!refErr.isEmpty()) { |
| 1493 | return refErr; |
| 1494 | } |
| 1495 | |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1496 | return compare_bitmaps(reference, *dst); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1500 | |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1501 | GPUPersistentCacheTestingSink::GPUPersistentCacheTestingSink( |
| 1502 | GrContextFactory::ContextType ct, |
| 1503 | GrContextFactory::ContextOverrides overrides, |
| 1504 | SkCommandLineConfigGpu::SurfType surfType, |
| 1505 | int samples, |
| 1506 | bool diText, |
| 1507 | SkColorType colorType, |
| 1508 | SkAlphaType alphaType, |
| 1509 | sk_sp<SkColorSpace> colorSpace, |
| 1510 | bool threaded, |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 1511 | const GrContextOptions& grCtxOptions, |
| 1512 | int cacheType) |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1513 | : INHERITED(ct, overrides, surfType, samples, diText, colorType, alphaType, |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 1514 | std::move(colorSpace), threaded, grCtxOptions) |
| 1515 | , fCacheType(cacheType) {} |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1516 | |
| 1517 | Error GPUPersistentCacheTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream, |
| 1518 | SkString* log) const { |
| 1519 | // Draw twice, once with a cold cache, and again with a warm cache. Verify that we get the same |
| 1520 | // result. |
| 1521 | sk_gpu_test::MemoryCache memoryCache; |
| 1522 | GrContextOptions contextOptions = this->baseContextOptions(); |
| 1523 | contextOptions.fPersistentCache = &memoryCache; |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 1524 | contextOptions.fDisallowGLSLBinaryCaching = (fCacheType == 2); |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1525 | |
| 1526 | Error err = this->onDraw(src, dst, wStream, log, contextOptions); |
| 1527 | if (!err.isEmpty() || !dst) { |
| 1528 | return err; |
| 1529 | } |
| 1530 | |
| 1531 | SkBitmap reference; |
| 1532 | SkString refLog; |
| 1533 | SkDynamicMemoryWStream refStream; |
| 1534 | memoryCache.resetNumCacheMisses(); |
| 1535 | Error refErr = this->onDraw(src, &reference, &refStream, &refLog, contextOptions); |
| 1536 | if (!refErr.isEmpty()) { |
| 1537 | return refErr; |
| 1538 | } |
Brian Salomon | 7fc5299 | 2018-07-12 16:20:23 -0400 | [diff] [blame] | 1539 | SkASSERT(!memoryCache.numCacheMisses()); |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1540 | |
| 1541 | return compare_bitmaps(reference, *dst); |
| 1542 | } |
| 1543 | |
| 1544 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1545 | static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) { |
halcanary | 4ba051c | 2016-03-10 10:31:53 -0800 | [diff] [blame] | 1546 | if (src.size().isEmpty()) { |
| 1547 | return "Source has empty dimensions"; |
| 1548 | } |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1549 | SkASSERT(doc); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1550 | int pageCount = src.pageCount(); |
| 1551 | for (int i = 0; i < pageCount; ++i) { |
| 1552 | int width = src.size(i).width(), height = src.size(i).height(); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1553 | SkCanvas* canvas = |
| 1554 | doc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); |
| 1555 | if (!canvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1556 | return "SkDocument::beginPage(w,h) returned nullptr"; |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1557 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1558 | Error err = src.draw(i, canvas); |
halcanary | 7e79818 | 2015-04-14 14:06:18 -0700 | [diff] [blame] | 1559 | if (!err.isEmpty()) { |
| 1560 | return err; |
| 1561 | } |
| 1562 | doc->endPage(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1563 | } |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 1564 | doc->close(); |
halcanary | fd4a993 | 2015-01-28 11:45:58 -0800 | [diff] [blame] | 1565 | dst->flush(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1566 | return ""; |
| 1567 | } |
| 1568 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1569 | Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 1570 | SkPDF::Metadata metadata; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 1571 | metadata.fTitle = src.name(); |
| 1572 | metadata.fSubject = "rendering correctness test"; |
| 1573 | metadata.fCreator = "Skia/DM"; |
Mike Reed | a4daf19 | 2017-12-14 13:25:04 -0500 | [diff] [blame] | 1574 | metadata.fRasterDPI = fRasterDpi; |
| 1575 | metadata.fPDFA = fPDFA; |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 1576 | #if SK_PDF_TEST_EXECUTOR |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1577 | std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool(); |
| 1578 | metadata.fExecutor = executor.get(); |
| 1579 | #endif |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 1580 | auto doc = SkPDF::MakeDocument(dst, metadata); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1581 | if (!doc) { |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 1582 | return "SkPDF::MakeDocument() returned nullptr"; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1583 | } |
| 1584 | return draw_skdocument(src, doc.get(), dst); |
| 1585 | } |
| 1586 | |
| 1587 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1588 | |
| 1589 | XPSSink::XPSSink() {} |
| 1590 | |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 1591 | #ifdef SK_BUILD_FOR_WIN |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1592 | static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() { |
| 1593 | IXpsOMObjectFactory* factory; |
| 1594 | HRN(CoCreateInstance(CLSID_XpsOMObjectFactory, |
| 1595 | nullptr, |
| 1596 | CLSCTX_INPROC_SERVER, |
| 1597 | IID_PPV_ARGS(&factory))); |
| 1598 | return SkTScopedComPtr<IXpsOMObjectFactory>(factory); |
| 1599 | } |
| 1600 | |
| 1601 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 1602 | SkAutoCoInitialize com; |
| 1603 | if (!com.succeeded()) { |
| 1604 | return "Could not initialize COM."; |
| 1605 | } |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1606 | SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory(); |
| 1607 | if (!factory) { |
| 1608 | return "Failed to create XPS Factory."; |
| 1609 | } |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 1610 | auto doc = SkXPS::MakeDocument(dst, factory.get()); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1611 | if (!doc) { |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 1612 | return "SkXPS::MakeDocument() returned nullptr"; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1613 | } |
| 1614 | return draw_skdocument(src, doc.get(), dst); |
| 1615 | } |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 1616 | #else |
| 1617 | Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
| 1618 | return "XPS not supported on this platform."; |
| 1619 | } |
| 1620 | #endif |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 1621 | |
| 1622 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1623 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1624 | SKPSink::SKPSink() {} |
| 1625 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1626 | Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1627 | SkSize size; |
| 1628 | size = src.size(); |
| 1629 | SkPictureRecorder recorder; |
| 1630 | Error err = src.draw(recorder.beginRecording(size.width(), size.height())); |
| 1631 | if (!err.isEmpty()) { |
| 1632 | return err; |
| 1633 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1634 | recorder.finishRecordingAsPicture()->serialize(dst); |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 1635 | return ""; |
| 1636 | } |
| 1637 | |
| 1638 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1639 | |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 1640 | Error DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
Mike Klein | 8f4e224 | 2019-03-20 11:59:00 -0500 | [diff] [blame] | 1641 | DebugCanvas debugCanvas(src.size().width(), src.size().height()); |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 1642 | Error err = src.draw(&debugCanvas); |
| 1643 | if (!err.isEmpty()) { |
| 1644 | return err; |
| 1645 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1646 | std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas(); |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 1647 | UrlDataManager dataManager(SkString("data")); |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 1648 | SkJSONWriter writer(dst, SkJSONWriter::Mode::kPretty); |
| 1649 | writer.beginObject(); // root |
| 1650 | debugCanvas.toJSON(writer, dataManager, debugCanvas.getSize(), nullCanvas.get()); |
| 1651 | writer.endObject(); // root |
| 1652 | writer.flush(); |
| 1653 | return ""; |
Hal Canary | 85c7fe8 | 2016-10-25 10:33:27 -0400 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1657 | |
Bryce Thomas | 95a7b76 | 2018-03-02 13:54:21 -0800 | [diff] [blame] | 1658 | SVGSink::SVGSink(int pageIndex) : fPageIndex(pageIndex) {} |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 1659 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1660 | Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const { |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 1661 | #if defined(SK_XML) |
Bryce Thomas | 95a7b76 | 2018-03-02 13:54:21 -0800 | [diff] [blame] | 1662 | if (src.pageCount() > 1) { |
| 1663 | int pageCount = src.pageCount(); |
| 1664 | if (fPageIndex > pageCount - 1) { |
| 1665 | return Error(SkStringPrintf("Page index %d too high for document with only %d pages.", |
| 1666 | fPageIndex, pageCount)); |
| 1667 | } |
| 1668 | } |
Bryce Thomas | 95a7b76 | 2018-03-02 13:54:21 -0800 | [diff] [blame] | 1669 | return src.draw(fPageIndex, |
| 1670 | SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()), |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1671 | SkIntToScalar(src.size().height())), |
Florin Malita | 562017b | 2019-02-14 13:42:15 -0500 | [diff] [blame] | 1672 | dst) |
Bryce Thomas | 95a7b76 | 2018-03-02 13:54:21 -0800 | [diff] [blame] | 1673 | .get()); |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 1674 | #else |
Hal Canary | 327ef03 | 2018-03-22 13:10:51 -0400 | [diff] [blame] | 1675 | (void)fPageIndex; |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 1676 | return Error("SVG sink is disabled."); |
| 1677 | #endif // SK_XML |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1681 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1682 | RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace) |
mtklein | 27c3fdd | 2016-02-26 14:43:21 -0800 | [diff] [blame] | 1683 | : fColorType(colorType) |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 1684 | , fColorSpace(std::move(colorSpace)) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1685 | |
Mike Klein | 48b6490 | 2018-07-25 13:28:44 -0400 | [diff] [blame] | 1686 | Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 1687 | const SkISize size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1688 | // If there's an appropriate alpha type for this color type, use it, otherwise use premul. |
| 1689 | SkAlphaType alphaType = kPremul_SkAlphaType; |
| 1690 | (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType); |
| 1691 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 1692 | dst->allocPixelsFlags(SkImageInfo::Make(size.width(), size.height(), |
| 1693 | fColorType, alphaType, fColorSpace), |
| 1694 | SkBitmap::kZeroPixels_AllocFlag); |
Yuqian Li | b8b6253 | 2018-02-23 14:13:36 +0800 | [diff] [blame] | 1695 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1696 | SkCanvas canvas(*dst); |
| 1697 | return src.draw(&canvas); |
| 1698 | } |
| 1699 | |
| 1700 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1701 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1702 | // 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] | 1703 | // 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] | 1704 | // Several examples below. |
| 1705 | |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1706 | template <typename Fn> |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1707 | static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log, |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1708 | SkISize size, const Fn& draw) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1709 | class ProxySrc : public Src { |
| 1710 | public: |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1711 | ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {} |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1712 | Error draw(SkCanvas* canvas) const override { return fDraw(canvas); } |
halcanary | b4a7f14 | 2016-03-30 08:31:27 -0700 | [diff] [blame] | 1713 | Name name() const override { return "ProxySrc"; } |
| 1714 | SkISize size() const override { return fSize; } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1715 | private: |
mtklein | cbf8978 | 2016-02-19 14:27:14 -0800 | [diff] [blame] | 1716 | SkISize fSize; |
| 1717 | const Fn& fDraw; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1718 | }; |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1719 | return sink->draw(ProxySrc(size, draw), bitmap, stream, log); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1723 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 1724 | static DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output."); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1725 | |
| 1726 | // Is *bitmap identical to what you get drawing src into sink? |
| 1727 | static Error check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) { |
| 1728 | // We can only check raster outputs. |
| 1729 | // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.) |
| 1730 | if (FLAGS_check && bitmap) { |
| 1731 | SkBitmap reference; |
| 1732 | SkString log; |
halcanary | b4a7f14 | 2016-03-30 08:31:27 -0700 | [diff] [blame] | 1733 | SkDynamicMemoryWStream wStream; |
| 1734 | Error err = sink->draw(src, &reference, &wStream, &log); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1735 | // If we can draw into this Sink via some pipeline, we should be able to draw directly. |
| 1736 | SkASSERT(err.isEmpty()); |
| 1737 | if (!err.isEmpty()) { |
| 1738 | return err; |
| 1739 | } |
Brian Osman | e5756ec | 2017-09-06 17:08:30 -0400 | [diff] [blame] | 1740 | return compare_bitmaps(reference, *bitmap); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1741 | } |
| 1742 | return ""; |
| 1743 | } |
| 1744 | |
| 1745 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1746 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1747 | static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) { |
| 1748 | SkRect bounds = SkRect::MakeIWH(srcW, srcH); |
| 1749 | matrix->mapRect(&bounds); |
| 1750 | matrix->postTranslate(-bounds.x(), -bounds.y()); |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1751 | return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())}; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1752 | } |
| 1753 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1754 | ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1755 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1756 | Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1757 | SkMatrix matrix = fMatrix; |
| 1758 | SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height()); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1759 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1760 | canvas->concat(matrix); |
| 1761 | return src.draw(canvas); |
| 1762 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1765 | // Undoes any flip or 90 degree rotate without changing the scale of the bitmap. |
| 1766 | // This should be pixel-preserving. |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1767 | ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {} |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1768 | |
| 1769 | Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1770 | Error err = fSink->draw(src, bitmap, stream, log); |
| 1771 | if (!err.isEmpty()) { |
| 1772 | return err; |
| 1773 | } |
| 1774 | |
| 1775 | SkMatrix inverse; |
| 1776 | if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) { |
| 1777 | return "Cannot upright --matrix."; |
| 1778 | } |
| 1779 | SkMatrix upright = SkMatrix::I(); |
| 1780 | upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX())); |
| 1781 | upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY())); |
| 1782 | upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX())); |
| 1783 | upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY())); |
| 1784 | |
| 1785 | SkBitmap uprighted; |
| 1786 | SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height()); |
| 1787 | uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height())); |
| 1788 | |
| 1789 | SkCanvas canvas(uprighted); |
| 1790 | canvas.concat(upright); |
| 1791 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1792 | paint.setBlendMode(SkBlendMode::kSrc); |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1793 | canvas.drawBitmap(*bitmap, 0, 0, &paint); |
| 1794 | |
| 1795 | *bitmap = uprighted; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 1796 | return ""; |
| 1797 | } |
| 1798 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1799 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1800 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1801 | Error ViaSerialization::draw( |
| 1802 | const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1803 | // Record our Src into a picture. |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1804 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1805 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1806 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1807 | SkIntToScalar(size.height()))); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1808 | if (!err.isEmpty()) { |
| 1809 | return err; |
| 1810 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1811 | sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1812 | |
| 1813 | // Serialize it and then deserialize it. |
reed | 39eaf5f | 2016-09-15 07:19:35 -0700 | [diff] [blame] | 1814 | sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1815 | |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1816 | err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1817 | canvas->drawPicture(deserialized); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1818 | return ""; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1819 | }); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1820 | if (!err.isEmpty()) { |
| 1821 | return err; |
| 1822 | } |
| 1823 | |
| 1824 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1828 | |
msarett | 62d3b10 | 2015-12-10 15:14:27 -0800 | [diff] [blame] | 1829 | ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink) |
| 1830 | : Via(sink) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 1831 | , fW(w) |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1832 | , fH(h) |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 1833 | , fFactory(factory) {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1834 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 1835 | Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1836 | auto size = src.size(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1837 | SkPictureRecorder recorder; |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1838 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1839 | SkIntToScalar(size.height()), |
| 1840 | fFactory.get())); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1841 | if (!err.isEmpty()) { |
| 1842 | return err; |
| 1843 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1844 | sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1845 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1846 | return draw_to_canvas(fSink.get(), bitmap, stream, log, src.size(), [&](SkCanvas* canvas) { |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1847 | const int xTiles = (size.width() + fW - 1) / fW, |
| 1848 | yTiles = (size.height() + fH - 1) / fH; |
| 1849 | SkMultiPictureDraw mpd(xTiles*yTiles); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1850 | SkTArray<sk_sp<SkSurface>> surfaces; |
| 1851 | // surfaces.setReserve(xTiles*yTiles); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1852 | |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1853 | SkImageInfo info = canvas->imageInfo().makeWH(fW, fH); |
| 1854 | for (int j = 0; j < yTiles; j++) { |
| 1855 | for (int i = 0; i < xTiles; i++) { |
| 1856 | // This lets our ultimate Sink determine the best kind of surface. |
| 1857 | // E.g., if it's a GpuSink, the surfaces and images are textures. |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1858 | auto s = canvas->makeSurface(info); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 1859 | if (!s) { |
| 1860 | s = SkSurface::MakeRaster(info); // Some canvases can't create surfaces. |
| 1861 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1862 | surfaces.push_back(s); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1863 | SkCanvas* c = s->getCanvas(); |
| 1864 | c->translate(SkIntToScalar(-i * fW), |
| 1865 | SkIntToScalar(-j * fH)); // Line up the canvas with this tile. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1866 | mpd.add(c, pic.get()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1867 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1868 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1869 | mpd.draw(); |
| 1870 | for (int j = 0; j < yTiles; j++) { |
| 1871 | for (int i = 0; i < xTiles; i++) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1872 | sk_sp<SkImage> image(surfaces[i+xTiles*j]->makeImageSnapshot()); |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1873 | canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH)); |
| 1874 | } |
| 1875 | } |
mtklein | a16e69e | 2015-05-05 11:38:45 -0700 | [diff] [blame] | 1876 | return ""; |
| 1877 | }); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 1880 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1881 | |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1882 | ViaDDL::ViaDDL(int numReplays, int numDivisions, Sink* sink) |
| 1883 | : Via(sink), fNumReplays(numReplays), fNumDivisions(numDivisions) {} |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1884 | |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1885 | Error ViaDDL::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1886 | auto size = src.size(); |
| 1887 | SkPictureRecorder recorder; |
| 1888 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1889 | SkIntToScalar(size.height()))); |
| 1890 | if (!err.isEmpty()) { |
| 1891 | return err; |
| 1892 | } |
| 1893 | sk_sp<SkPicture> inputPicture(recorder.finishRecordingAsPicture()); |
| 1894 | |
| 1895 | // this is our ultimate final drawing area/rect |
| 1896 | SkIRect viewport = SkIRect::MakeWH(size.fWidth, size.fHeight); |
| 1897 | |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 1898 | DDLPromiseImageHelper promiseImageHelper; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 1899 | sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture.get()); |
| 1900 | if (!compressedPictureData) { |
| 1901 | return SkStringPrintf("ViaDDL: Couldn't deflate SkPicture"); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1902 | } |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1903 | auto draw = [&](SkCanvas* canvas) -> Error { |
| 1904 | GrContext* context = canvas->getGrContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 1905 | if (!context || !context->priv().getGpu()) { |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1906 | return SkStringPrintf("DDLs are GPU only"); |
| 1907 | } |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1908 | |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1909 | // This is here bc this is the first point where we have access to the context |
| 1910 | promiseImageHelper.uploadAllToGPU(context); |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 1911 | // We draw N times, with a clear between. |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1912 | for (int replay = 0; replay < fNumReplays; ++replay) { |
| 1913 | if (replay > 0) { |
| 1914 | // Clear the drawing of the previous replay |
Brian Salomon | 8d23a58 | 2018-12-19 11:35:20 -0500 | [diff] [blame] | 1915 | canvas->clear(SK_ColorTRANSPARENT); |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1916 | } |
| 1917 | // First, create all the tiles (including their individual dest surfaces) |
| 1918 | DDLTileHelper tiles(canvas, viewport, fNumDivisions); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1919 | |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1920 | // Second, reinflate the compressed picture individually for each thread |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 1921 | // This recreates the promise SkImages on each replay iteration. We are currently |
| 1922 | // relying on this to test using a SkPromiseImageTexture to fulfill different |
| 1923 | // SkImages. On each replay the promise SkImages are recreated in createSKPPerTile. |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1924 | tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1925 | |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1926 | // Third, create the DDLs in parallel |
| 1927 | tiles.createDDLsInParallel(); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1928 | |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1929 | if (replay == fNumReplays - 1) { |
| 1930 | // This drops the promiseImageHelper's refs on all the promise images if we're in |
| 1931 | // the last run. |
| 1932 | promiseImageHelper.reset(); |
| 1933 | } |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1934 | |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1935 | // Fourth, synchronously render the display lists into the dest tiles |
| 1936 | // TODO: it would be cool to not wait until all the tiles are drawn to begin |
| 1937 | // drawing to the GPU and composing to the final surface |
| 1938 | tiles.drawAllTilesAndFlush(context, false); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1939 | |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 1940 | // Finally, compose the drawn tiles into the result |
| 1941 | // Note: the separation between the tiles and the final composition better |
| 1942 | // matches Chrome but costs us a copy |
| 1943 | tiles.composeAllTiles(canvas); |
| 1944 | context->flush(); |
| 1945 | } |
| 1946 | return ""; |
| 1947 | }; |
| 1948 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, draw); |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1949 | } |
| 1950 | |
Robert Phillips | 33f02ed | 2018-03-27 08:06:57 -0400 | [diff] [blame] | 1951 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1952 | |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1953 | Error ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1954 | auto size = src.size(); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1955 | Error err = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) { |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1956 | SkPictureRecorder recorder; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1957 | sk_sp<SkPicture> pic; |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1958 | Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()), |
| 1959 | SkIntToScalar(size.height()))); |
| 1960 | if (!err.isEmpty()) { |
| 1961 | return err; |
| 1962 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1963 | pic = recorder.finishRecordingAsPicture(); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1964 | canvas->drawPicture(pic); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1965 | return err; |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1966 | }); |
Ben Wagner | 1861e88 | 2018-04-04 17:40:46 -0400 | [diff] [blame] | 1967 | if (!err.isEmpty()) { |
| 1968 | return err; |
| 1969 | } |
| 1970 | |
| 1971 | return check_against_reference(bitmap, src, fSink.get()); |
mtklein | 4a34ecb | 2016-01-08 10:19:35 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 1975 | |
Mike Reed | bae888e | 2017-02-18 16:50:45 -0500 | [diff] [blame] | 1976 | #ifdef TEST_VIA_SVG |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 1977 | #include "experimental/svg/model/SkSVGDOM.h" |
| 1978 | #include "include/svg/SkSVGCanvas.h" |
| 1979 | #include "src/xml/SkXMLWriter.h" |
Mike Reed | f67c459 | 2017-02-17 17:06:11 -0500 | [diff] [blame] | 1980 | |
| 1981 | Error ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const { |
| 1982 | auto size = src.size(); |
| 1983 | return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error { |
| 1984 | SkDynamicMemoryWStream wstream; |
| 1985 | SkXMLStreamWriter writer(&wstream); |
| 1986 | Error err = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get()); |
| 1987 | if (!err.isEmpty()) { |
| 1988 | return err; |
| 1989 | } |
| 1990 | std::unique_ptr<SkStream> rstream(wstream.detachAsStream()); |
| 1991 | auto dom = SkSVGDOM::MakeFromStream(*rstream); |
| 1992 | if (dom) { |
| 1993 | dom->setContainerSize(SkSize::Make(size)); |
| 1994 | dom->render(canvas); |
| 1995 | } |
| 1996 | return ""; |
| 1997 | }); |
| 1998 | } |
Mike Reed | bae888e | 2017-02-18 16:50:45 -0500 | [diff] [blame] | 1999 | #endif |
Mike Reed | f67c459 | 2017-02-17 17:06:11 -0500 | [diff] [blame] | 2000 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 2001 | } // namespace DM |