blob: bc1b5dfacdb9be5686d4b24ca80b6f8436b9ec62 [file] [log] [blame]
herbfeec8782016-02-17 10:00:07 -08001/*
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>
herbfeec8782016-02-17 10:00:07 -08009#include "Benchmark.h"
Herb Derby83e939b2017-02-07 14:25:11 -050010
11#include "SkArenaAlloc.h"
Herb Derbydfa492e2016-11-17 21:12:36 -050012#include "SkBitmapProcShader.h"
13#include "SkColor.h"
Herb Derbyac04fef2017-01-13 17:34:33 -050014#include "SkArenaAlloc.h"
herbfeec8782016-02-17 10:00:07 -080015#include "SkImage.h"
Herb Derbydfa492e2016-11-17 21:12:36 -050016#include "SkLinearBitmapPipeline.h"
17#include "SkPM4f.h"
18#include "SkShader.h"
herbfeec8782016-02-17 10:00:07 -080019
20struct CommonBitmapFPBenchmark : public Benchmark {
21 CommonBitmapFPBenchmark(
22 SkISize srcSize,
reeddabe5d32016-06-21 10:28:14 -070023 bool isSRGB,
herbfeec8782016-02-17 10:00:07 -080024 SkMatrix m,
25 bool useBilerp,
26 SkShader::TileMode xTile,
27 SkShader::TileMode yTile)
reeddabe5d32016-06-21 10:28:14 -070028 : fIsSRGB(isSRGB)
herbfeec8782016-02-17 10:00:07 -080029 , fM{m}
30 , fUseBilerp{useBilerp}
31 , fXTile{xTile}
32 , fYTile{yTile} {
33 fSrcSize = srcSize;
34 }
35
36 static SkString tileName(const char* pre, SkShader::TileMode mode) {
37 SkString name{pre};
38 switch (mode) {
39 case SkShader::kClamp_TileMode:
40 name.append("Clamp");
41 return name;
42 case SkShader::kRepeat_TileMode:
43 name.append("Repeat");
44 return name;
45 case SkShader::kMirror_TileMode:
46 name.append("Mirror");
47 return name;
48 default:
49 name.append("Unknown");
50 return name;
51 }
52 }
53
54 const char* onGetName() override {
herb134ad3e2016-02-17 11:57:24 -080055 fName.set("SkBitmapFP");
herbfeec8782016-02-17 10:00:07 -080056 if (fM.getType() & SkMatrix::kPerspective_Mask) {
herb134ad3e2016-02-17 11:57:24 -080057 fName.append("Perspective");
herbfeec8782016-02-17 10:00:07 -080058 } else if (fM.getType() & SkMatrix::kAffine_Mask) {
herb134ad3e2016-02-17 11:57:24 -080059 fName.append("Affine");
herbfeec8782016-02-17 10:00:07 -080060 } else if (fM.getType() & SkMatrix::kScale_Mask) {
herb134ad3e2016-02-17 11:57:24 -080061 fName.append("Scale");
herbfeec8782016-02-17 10:00:07 -080062 } else if (fM.getType() & SkMatrix::kTranslate_Mask) {
herb134ad3e2016-02-17 11:57:24 -080063 fName.append("Translate");
herbfeec8782016-02-17 10:00:07 -080064 } else {
herb134ad3e2016-02-17 11:57:24 -080065 fName.append("Identity");
herbfeec8782016-02-17 10:00:07 -080066 }
67
herb134ad3e2016-02-17 11:57:24 -080068 fName.append(tileName("X", fXTile));
69 fName.append(tileName("Y", fYTile));
herbfeec8782016-02-17 10:00:07 -080070
71 if (fUseBilerp) {
herb134ad3e2016-02-17 11:57:24 -080072 fName.append("Filter");
herbfeec8782016-02-17 10:00:07 -080073 } else {
herb134ad3e2016-02-17 11:57:24 -080074 fName.append("Nearest");
herbfeec8782016-02-17 10:00:07 -080075 }
76
herb134ad3e2016-02-17 11:57:24 -080077 fName.appendf("%s", BaseName().c_str());
herbfeec8782016-02-17 10:00:07 -080078
herb134ad3e2016-02-17 11:57:24 -080079 return fName.c_str();
herbfeec8782016-02-17 10:00:07 -080080 }
81
82 void onPreDraw(SkCanvas*) override {
83 int width = fSrcSize.fWidth;
84 int height = fSrcSize.fHeight;
85 fBitmap.reset(new uint32_t[width * height]);
86 for (int y = 0; y < height; y++) {
87 for (int x = 0; x < width; x++) {
88 fBitmap[y * width + x] = (y << 8) + x + (128<<24);
89 }
90 }
91
92 bool trash = fM.invert(&fInvert);
93 sk_ignore_unused_variable(trash);
94
reeddabe5d32016-06-21 10:28:14 -070095 fInfo = SkImageInfo::MakeN32Premul(width, height, fIsSRGB ?
Matt Sarett77a7a1b2017-02-07 13:56:11 -050096 SkColorSpace::MakeSRGB() : nullptr);
herbfeec8782016-02-17 10:00:07 -080097 }
98
99 bool isSuitableFor(Backend backend) override {
100 return backend == kNonRendering_Backend;
101 }
102
103 virtual SkString BaseName() = 0;
104
herb134ad3e2016-02-17 11:57:24 -0800105 SkString fName;
herbfeec8782016-02-17 10:00:07 -0800106 SkISize fSrcSize;
reeddabe5d32016-06-21 10:28:14 -0700107 bool fIsSRGB;
herbfeec8782016-02-17 10:00:07 -0800108 SkMatrix fM;
109 SkMatrix fInvert;
110 bool fUseBilerp;
111 SkShader::TileMode fXTile;
112 SkShader::TileMode fYTile;
113 SkImageInfo fInfo;
114 std::unique_ptr<uint32_t[]> fBitmap;
115};
116
117struct SkBitmapFPGeneral final : public CommonBitmapFPBenchmark {
118 SkBitmapFPGeneral(
119 SkISize srcSize,
reeddabe5d32016-06-21 10:28:14 -0700120 bool isSRGB,
herbfeec8782016-02-17 10:00:07 -0800121 SkMatrix m,
122 bool useBilerp,
123 SkShader::TileMode xTile,
124 SkShader::TileMode yTile)
reeddabe5d32016-06-21 10:28:14 -0700125 : CommonBitmapFPBenchmark(srcSize, isSRGB, m, useBilerp, xTile, yTile) { }
herb134ad3e2016-02-17 11:57:24 -0800126
herbfeec8782016-02-17 10:00:07 -0800127 SkString BaseName() override {
128 SkString name;
reeddabe5d32016-06-21 10:28:14 -0700129 if (fInfo.gammaCloseToSRGB()) {
herbfeec8782016-02-17 10:00:07 -0800130 name.set("sRGB");
131 } else {
132 name.set("Linr");
133 }
134 return name;
135 }
136
137 void onDraw(int loops, SkCanvas*) override {
138 int width = fSrcSize.fWidth;
139 int height = fSrcSize.fHeight;
140
mtkleinb65fcd42016-02-17 13:50:46 -0800141 SkAutoTMalloc<SkPM4f> FPbuffer(width*height);
herbfeec8782016-02-17 10:00:07 -0800142
herbc5eddd72016-02-17 19:50:05 -0800143 SkFilterQuality filterQuality;
144 if (fUseBilerp) {
145 filterQuality = SkFilterQuality::kLow_SkFilterQuality;
146 } else {
147 filterQuality = SkFilterQuality::kNone_SkFilterQuality;
148 }
149
herbed545042016-02-18 13:55:02 -0800150 SkPixmap srcPixmap{fInfo, fBitmap.get(), static_cast<size_t>(4 * width)};
151
Herb Derbydfa492e2016-11-17 21:12:36 -0500152
153 char storage[600];
Herb Derby0497f082017-01-13 11:30:44 -0500154 SkArenaAlloc allocator{storage, sizeof(storage), 512};
herbc5eddd72016-02-17 19:50:05 -0800155 SkLinearBitmapPipeline pipeline{
Herb Derbydfa492e2016-11-17 21:12:36 -0500156 fInvert, filterQuality, fXTile, fYTile, SK_ColorBLACK, srcPixmap, &allocator};
herbfeec8782016-02-17 10:00:07 -0800157
158 int count = 100;
159
160 for (int n = 0; n < 1000*loops; n++) {
161 pipeline.shadeSpan4f(3, 6, FPbuffer, count);
162 }
herbfeec8782016-02-17 10:00:07 -0800163 }
164};
165
166struct SkBitmapFPOrigShader : public CommonBitmapFPBenchmark {
167 SkBitmapFPOrigShader(
168 SkISize srcSize,
reeddabe5d32016-06-21 10:28:14 -0700169 bool isSRGB,
herbfeec8782016-02-17 10:00:07 -0800170 SkMatrix m,
171 bool useBilerp,
172 SkShader::TileMode xTile,
173 SkShader::TileMode yTile)
reeddabe5d32016-06-21 10:28:14 -0700174 : CommonBitmapFPBenchmark(srcSize, isSRGB, m, useBilerp, xTile, yTile) { }
herb134ad3e2016-02-17 11:57:24 -0800175
herbfeec8782016-02-17 10:00:07 -0800176 SkString BaseName() override {
177 SkString name{"Orig"};
178 return name;
179 }
180
181 void onPreDraw(SkCanvas* c) override {
182 CommonBitmapFPBenchmark::onPreDraw(c);
183
reed9ce9d672016-03-17 10:51:11 -0700184 fImage = SkImage::MakeRasterCopy(
185 SkPixmap(fInfo, fBitmap.get(), sizeof(SkPMColor) * fSrcSize.fWidth));
reed5671c5b2016-03-09 14:47:34 -0800186 fPaint.setShader(fImage->makeShader(fXTile, fYTile));
herbfeec8782016-02-17 10:00:07 -0800187 if (fUseBilerp) {
188 fPaint.setFilterQuality(SkFilterQuality::kLow_SkFilterQuality);
189 } else {
190 fPaint.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
191 }
herbfeec8782016-02-17 10:00:07 -0800192 }
193
194 void onPostDraw(SkCanvas*) override {
195
196 }
197
198 void onDraw(int loops, SkCanvas*) override {
199 int width = fSrcSize.fWidth;
200 int height = fSrcSize.fHeight;
201
mtkleinb65fcd42016-02-17 13:50:46 -0800202 SkAutoTMalloc<SkPMColor> buffer4b(width*height);
herbfeec8782016-02-17 10:00:07 -0800203
Herb Derby9852dc02017-02-07 17:57:47 -0500204 SkArenaAlloc alloc{0};
fmalitad0c4e092016-02-22 17:19:04 -0800205 const SkShader::ContextRec rec(fPaint, fM, nullptr,
Brian Osman11970e52016-12-05 15:26:50 -0500206 SkShader::ContextRec::kPMColor_DstType,
207 nullptr);
Herb Derby83e939b2017-02-07 14:25:11 -0500208 SkShader::Context* ctx = fPaint.getShader()->makeContext(rec, &alloc);
herbfeec8782016-02-17 10:00:07 -0800209
210 int count = 100;
211
212 for (int n = 0; n < 1000*loops; n++) {
213 ctx->shadeSpan(3, 6, buffer4b, count);
214 }
herbfeec8782016-02-17 10:00:07 -0800215 }
216 SkPaint fPaint;
reed9ce9d672016-03-17 10:51:11 -0700217 sk_sp<SkImage> fImage;
herbfeec8782016-02-17 10:00:07 -0800218};
219
reeddabe5d32016-06-21 10:28:14 -0700220const bool gSRGB = true;
221const bool gLinearRGB = false;
herbfeec8782016-02-17 10:00:07 -0800222static SkISize srcSize = SkISize::Make(120, 100);
223static SkMatrix mI = SkMatrix::I();
224DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700225 srcSize, gSRGB, mI, false,
herbfeec8782016-02-17 10:00:07 -0800226 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
227
228DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700229 srcSize, gLinearRGB, mI, false,
herbfeec8782016-02-17 10:00:07 -0800230 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
231
232DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700233 srcSize, gLinearRGB, mI, false,
herbfeec8782016-02-17 10:00:07 -0800234 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
235
236DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700237 srcSize, gSRGB, mI, true,
herbfeec8782016-02-17 10:00:07 -0800238 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
239
240DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700241 srcSize, gLinearRGB, mI, true,
herbfeec8782016-02-17 10:00:07 -0800242 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
243
244DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700245 srcSize, gLinearRGB, mI, true,
herbfeec8782016-02-17 10:00:07 -0800246 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
247
248static SkMatrix mS = SkMatrix::MakeScale(2.7f, 2.7f);
249DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700250 srcSize, gSRGB, mS, false,
herbfeec8782016-02-17 10:00:07 -0800251 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
252
253DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700254 srcSize, gLinearRGB, mS, false,
herbfeec8782016-02-17 10:00:07 -0800255 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
256
257DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700258 srcSize, gLinearRGB, mS, false,
herbfeec8782016-02-17 10:00:07 -0800259 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
260
261DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700262 srcSize, gSRGB, mS, true,
herbfeec8782016-02-17 10:00:07 -0800263 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
264
265DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700266 srcSize, gLinearRGB, mS, true,
herbfeec8782016-02-17 10:00:07 -0800267 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
268
269DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700270 srcSize, gLinearRGB, mS, true,
herbfeec8782016-02-17 10:00:07 -0800271 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
272
herb6eff52a2016-03-23 09:00:33 -0700273// Repeat
274DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700275 srcSize, gSRGB, mS, false,
herb6eff52a2016-03-23 09:00:33 -0700276 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
277
278DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700279 srcSize, gLinearRGB, mS, false,
herb6eff52a2016-03-23 09:00:33 -0700280 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
281
282DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700283 srcSize, gLinearRGB, mS, false,
herb6eff52a2016-03-23 09:00:33 -0700284 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
285
286DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700287 srcSize, gSRGB, mS, true,
herb6eff52a2016-03-23 09:00:33 -0700288 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
289
290DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700291 srcSize, gLinearRGB, mS, true,
herb6eff52a2016-03-23 09:00:33 -0700292 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
293
294DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700295 srcSize, gLinearRGB, mS, true,
herb6eff52a2016-03-23 09:00:33 -0700296 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
297
herbfeec8782016-02-17 10:00:07 -0800298static SkMatrix rotate(SkScalar r) {
299 SkMatrix m;
300 m.setRotate(30);
301 return m;
302}
303
304static SkMatrix mR = rotate(30);
305DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700306 srcSize, gSRGB, mR, false,
herbfeec8782016-02-17 10:00:07 -0800307 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
308
309DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700310 srcSize, gLinearRGB, mR, false,
herbfeec8782016-02-17 10:00:07 -0800311 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
312
313DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700314 srcSize, gLinearRGB, mR, false,
herbfeec8782016-02-17 10:00:07 -0800315 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
316
317DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700318 srcSize, gSRGB, mR, true,
herbfeec8782016-02-17 10:00:07 -0800319 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
320
321DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700322 srcSize, gLinearRGB, mR, true,
herbfeec8782016-02-17 10:00:07 -0800323 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
324
325DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700326 srcSize, gLinearRGB, mR, true,
herbfeec8782016-02-17 10:00:07 -0800327 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
328
herb6eff52a2016-03-23 09:00:33 -0700329// Repeat
330DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700331 srcSize, gSRGB, mR, false,
herb6eff52a2016-03-23 09:00:33 -0700332 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
333
334DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700335 srcSize, gLinearRGB, mR, false,
herb6eff52a2016-03-23 09:00:33 -0700336 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
337
338DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700339 srcSize, gLinearRGB, mR, false,
herb6eff52a2016-03-23 09:00:33 -0700340 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
341
342DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700343 srcSize, gSRGB, mR, true,
herb6eff52a2016-03-23 09:00:33 -0700344 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
345
346DEF_BENCH(return new SkBitmapFPGeneral(
reeddabe5d32016-06-21 10:28:14 -0700347 srcSize, gLinearRGB, mR, true,
herb6eff52a2016-03-23 09:00:33 -0700348 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
349
350DEF_BENCH(return new SkBitmapFPOrigShader(
reeddabe5d32016-06-21 10:28:14 -0700351 srcSize, gLinearRGB, mR, true,
herb6eff52a2016-03-23 09:00:33 -0700352 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)