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