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