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