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