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