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