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