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