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