herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #include <memory> |
| 9 | #include "SkColor.h" |
| 10 | #include "SkLinearBitmapPipeline.h" |
herb | 42da45d | 2016-04-08 14:24:37 -0700 | [diff] [blame] | 11 | #include "SkBitmapProcShader.h" |
reed | dd9ffea | 2016-02-18 12:39:14 -0800 | [diff] [blame] | 12 | #include "SkPM4f.h" |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 13 | #include "Benchmark.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkImage.h" |
| 16 | |
| 17 | struct CommonBitmapFPBenchmark : public Benchmark { |
| 18 | CommonBitmapFPBenchmark( |
| 19 | SkISize srcSize, |
| 20 | SkColorProfileType colorProfile, |
| 21 | SkMatrix m, |
| 22 | bool useBilerp, |
| 23 | SkShader::TileMode xTile, |
| 24 | SkShader::TileMode yTile) |
| 25 | : fColorProfile(colorProfile) |
| 26 | , fM{m} |
| 27 | , fUseBilerp{useBilerp} |
| 28 | , fXTile{xTile} |
| 29 | , fYTile{yTile} { |
| 30 | fSrcSize = srcSize; |
| 31 | } |
| 32 | |
| 33 | static SkString tileName(const char* pre, SkShader::TileMode mode) { |
| 34 | SkString name{pre}; |
| 35 | switch (mode) { |
| 36 | case SkShader::kClamp_TileMode: |
| 37 | name.append("Clamp"); |
| 38 | return name; |
| 39 | case SkShader::kRepeat_TileMode: |
| 40 | name.append("Repeat"); |
| 41 | return name; |
| 42 | case SkShader::kMirror_TileMode: |
| 43 | name.append("Mirror"); |
| 44 | return name; |
| 45 | default: |
| 46 | name.append("Unknown"); |
| 47 | return name; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const char* onGetName() override { |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 52 | fName.set("SkBitmapFP"); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 53 | if (fM.getType() & SkMatrix::kPerspective_Mask) { |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 54 | fName.append("Perspective"); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 55 | } else if (fM.getType() & SkMatrix::kAffine_Mask) { |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 56 | fName.append("Affine"); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 57 | } else if (fM.getType() & SkMatrix::kScale_Mask) { |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 58 | fName.append("Scale"); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 59 | } else if (fM.getType() & SkMatrix::kTranslate_Mask) { |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 60 | fName.append("Translate"); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 61 | } else { |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 62 | fName.append("Identity"); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 63 | } |
| 64 | |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 65 | fName.append(tileName("X", fXTile)); |
| 66 | fName.append(tileName("Y", fYTile)); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 67 | |
| 68 | if (fUseBilerp) { |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 69 | fName.append("Filter"); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 70 | } else { |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 71 | fName.append("Nearest"); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 72 | } |
| 73 | |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 74 | fName.appendf("%s", BaseName().c_str()); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 75 | |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 76 | return fName.c_str(); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void onPreDraw(SkCanvas*) override { |
| 80 | int width = fSrcSize.fWidth; |
| 81 | int height = fSrcSize.fHeight; |
| 82 | fBitmap.reset(new uint32_t[width * height]); |
| 83 | for (int y = 0; y < height; y++) { |
| 84 | for (int x = 0; x < width; x++) { |
| 85 | fBitmap[y * width + x] = (y << 8) + x + (128<<24); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | bool trash = fM.invert(&fInvert); |
| 90 | sk_ignore_unused_variable(trash); |
| 91 | |
| 92 | fInfo = SkImageInfo::MakeN32Premul(width, height, fColorProfile); |
| 93 | } |
| 94 | |
| 95 | bool isSuitableFor(Backend backend) override { |
| 96 | return backend == kNonRendering_Backend; |
| 97 | } |
| 98 | |
| 99 | virtual SkString BaseName() = 0; |
| 100 | |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 101 | SkString fName; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 102 | SkISize fSrcSize; |
| 103 | SkColorProfileType fColorProfile; |
| 104 | SkMatrix fM; |
| 105 | SkMatrix fInvert; |
| 106 | bool fUseBilerp; |
| 107 | SkShader::TileMode fXTile; |
| 108 | SkShader::TileMode fYTile; |
| 109 | SkImageInfo fInfo; |
| 110 | std::unique_ptr<uint32_t[]> fBitmap; |
| 111 | }; |
| 112 | |
| 113 | struct SkBitmapFPGeneral final : public CommonBitmapFPBenchmark { |
| 114 | SkBitmapFPGeneral( |
| 115 | SkISize srcSize, |
| 116 | SkColorProfileType colorProfile, |
| 117 | SkMatrix m, |
| 118 | bool useBilerp, |
| 119 | SkShader::TileMode xTile, |
| 120 | SkShader::TileMode yTile) |
| 121 | : CommonBitmapFPBenchmark(srcSize, colorProfile, m, useBilerp, xTile, yTile) { } |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 122 | |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 123 | SkString BaseName() override { |
| 124 | SkString name; |
| 125 | if (fInfo.isSRGB()) { |
| 126 | name.set("sRGB"); |
| 127 | } else { |
| 128 | name.set("Linr"); |
| 129 | } |
| 130 | return name; |
| 131 | } |
| 132 | |
| 133 | void onDraw(int loops, SkCanvas*) override { |
| 134 | int width = fSrcSize.fWidth; |
| 135 | int height = fSrcSize.fHeight; |
| 136 | |
mtklein | b65fcd4 | 2016-02-17 13:50:46 -0800 | [diff] [blame] | 137 | SkAutoTMalloc<SkPM4f> FPbuffer(width*height); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 138 | |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 139 | SkFilterQuality filterQuality; |
| 140 | if (fUseBilerp) { |
| 141 | filterQuality = SkFilterQuality::kLow_SkFilterQuality; |
| 142 | } else { |
| 143 | filterQuality = SkFilterQuality::kNone_SkFilterQuality; |
| 144 | } |
| 145 | |
herb | ed54504 | 2016-02-18 13:55:02 -0800 | [diff] [blame] | 146 | SkPixmap srcPixmap{fInfo, fBitmap.get(), static_cast<size_t>(4 * width)}; |
| 147 | |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 148 | SkLinearBitmapPipeline pipeline{ |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 149 | fInvert, filterQuality, fXTile, fYTile, SK_ColorBLACK, srcPixmap}; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 150 | |
| 151 | int count = 100; |
| 152 | |
| 153 | for (int n = 0; n < 1000*loops; n++) { |
| 154 | pipeline.shadeSpan4f(3, 6, FPbuffer, count); |
| 155 | } |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 156 | } |
| 157 | }; |
| 158 | |
| 159 | struct SkBitmapFPOrigShader : public CommonBitmapFPBenchmark { |
| 160 | SkBitmapFPOrigShader( |
| 161 | SkISize srcSize, |
| 162 | SkColorProfileType colorProfile, |
| 163 | SkMatrix m, |
| 164 | bool useBilerp, |
| 165 | SkShader::TileMode xTile, |
| 166 | SkShader::TileMode yTile) |
| 167 | : CommonBitmapFPBenchmark(srcSize, colorProfile, m, useBilerp, xTile, yTile) { } |
herb | 134ad3e | 2016-02-17 11:57:24 -0800 | [diff] [blame] | 168 | |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 169 | SkString BaseName() override { |
| 170 | SkString name{"Orig"}; |
| 171 | return name; |
| 172 | } |
| 173 | |
| 174 | void onPreDraw(SkCanvas* c) override { |
| 175 | CommonBitmapFPBenchmark::onPreDraw(c); |
| 176 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 177 | fImage = SkImage::MakeRasterCopy( |
| 178 | SkPixmap(fInfo, fBitmap.get(), sizeof(SkPMColor) * fSrcSize.fWidth)); |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 179 | fPaint.setShader(fImage->makeShader(fXTile, fYTile)); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 180 | if (fUseBilerp) { |
| 181 | fPaint.setFilterQuality(SkFilterQuality::kLow_SkFilterQuality); |
| 182 | } else { |
| 183 | fPaint.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality); |
| 184 | } |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void onPostDraw(SkCanvas*) override { |
| 188 | |
| 189 | } |
| 190 | |
| 191 | void onDraw(int loops, SkCanvas*) override { |
| 192 | int width = fSrcSize.fWidth; |
| 193 | int height = fSrcSize.fHeight; |
| 194 | |
mtklein | b65fcd4 | 2016-02-17 13:50:46 -0800 | [diff] [blame] | 195 | SkAutoTMalloc<SkPMColor> buffer4b(width*height); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 196 | |
herb | 42da45d | 2016-04-08 14:24:37 -0700 | [diff] [blame] | 197 | uint32_t storage[kSkBlitterContextSize]; |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 198 | const SkShader::ContextRec rec(fPaint, fM, nullptr, |
| 199 | SkShader::ContextRec::kPMColor_DstType); |
fmalita | 8d9f2e4 | 2016-02-22 10:39:41 -0800 | [diff] [blame] | 200 | SkASSERT(fPaint.getShader()->contextSize(rec) <= sizeof(storage)); |
| 201 | SkShader::Context* ctx = fPaint.getShader()->createContext(rec, storage); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 202 | |
| 203 | int count = 100; |
| 204 | |
| 205 | for (int n = 0; n < 1000*loops; n++) { |
| 206 | ctx->shadeSpan(3, 6, buffer4b, count); |
| 207 | } |
| 208 | |
| 209 | ctx->~Context(); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 210 | } |
| 211 | SkPaint fPaint; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 212 | sk_sp<SkImage> fImage; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | static SkISize srcSize = SkISize::Make(120, 100); |
| 216 | static SkMatrix mI = SkMatrix::I(); |
| 217 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 218 | srcSize, kSRGB_SkColorProfileType, mI, false, |
| 219 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 220 | |
| 221 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 222 | srcSize, kLinear_SkColorProfileType, mI, false, |
| 223 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 224 | |
| 225 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 226 | srcSize, kLinear_SkColorProfileType, mI, false, |
| 227 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 228 | |
| 229 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 230 | srcSize, kSRGB_SkColorProfileType, mI, true, |
| 231 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 232 | |
| 233 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 234 | srcSize, kLinear_SkColorProfileType, mI, true, |
| 235 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 236 | |
| 237 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 238 | srcSize, kLinear_SkColorProfileType, mI, true, |
| 239 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 240 | |
| 241 | static SkMatrix mS = SkMatrix::MakeScale(2.7f, 2.7f); |
| 242 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 243 | srcSize, kSRGB_SkColorProfileType, mS, false, |
| 244 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 245 | |
| 246 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 247 | srcSize, kLinear_SkColorProfileType, mS, false, |
| 248 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 249 | |
| 250 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 251 | srcSize, kLinear_SkColorProfileType, mS, false, |
| 252 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 253 | |
| 254 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 255 | srcSize, kSRGB_SkColorProfileType, mS, true, |
| 256 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 257 | |
| 258 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 259 | srcSize, kLinear_SkColorProfileType, mS, true, |
| 260 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 261 | |
| 262 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 263 | srcSize, kLinear_SkColorProfileType, mS, true, |
| 264 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 265 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 266 | // Repeat |
| 267 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 268 | srcSize, kSRGB_SkColorProfileType, mS, false, |
| 269 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 270 | |
| 271 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 272 | srcSize, kLinear_SkColorProfileType, mS, false, |
| 273 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 274 | |
| 275 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 276 | srcSize, kLinear_SkColorProfileType, mS, false, |
| 277 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 278 | |
| 279 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 280 | srcSize, kSRGB_SkColorProfileType, mS, true, |
| 281 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 282 | |
| 283 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 284 | srcSize, kLinear_SkColorProfileType, mS, true, |
| 285 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 286 | |
| 287 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 288 | srcSize, kLinear_SkColorProfileType, mS, true, |
| 289 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 290 | |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 291 | static SkMatrix rotate(SkScalar r) { |
| 292 | SkMatrix m; |
| 293 | m.setRotate(30); |
| 294 | return m; |
| 295 | } |
| 296 | |
| 297 | static SkMatrix mR = rotate(30); |
| 298 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 299 | srcSize, kSRGB_SkColorProfileType, mR, false, |
| 300 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 301 | |
| 302 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 303 | srcSize, kLinear_SkColorProfileType, mR, false, |
| 304 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 305 | |
| 306 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 307 | srcSize, kLinear_SkColorProfileType, mR, false, |
| 308 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 309 | |
| 310 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 311 | srcSize, kSRGB_SkColorProfileType, mR, true, |
| 312 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 313 | |
| 314 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 315 | srcSize, kLinear_SkColorProfileType, mR, true, |
| 316 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 317 | |
| 318 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 319 | srcSize, kLinear_SkColorProfileType, mR, true, |
| 320 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);) |
| 321 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 322 | // Repeat |
| 323 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 324 | srcSize, kSRGB_SkColorProfileType, mR, false, |
| 325 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 326 | |
| 327 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 328 | srcSize, kLinear_SkColorProfileType, mR, false, |
| 329 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 330 | |
| 331 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 332 | srcSize, kLinear_SkColorProfileType, mR, false, |
| 333 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 334 | |
| 335 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 336 | srcSize, kSRGB_SkColorProfileType, mR, true, |
| 337 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 338 | |
| 339 | DEF_BENCH(return new SkBitmapFPGeneral( |
| 340 | srcSize, kLinear_SkColorProfileType, mR, true, |
| 341 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |
| 342 | |
| 343 | DEF_BENCH(return new SkBitmapFPOrigShader( |
| 344 | srcSize, kLinear_SkColorProfileType, mR, true, |
| 345 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);) |