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