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