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