blob: 476d0152312ad23ca99b2c4c2a6cd9403b331f63 [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>
9#include "SkColor.h"
10#include "SkLinearBitmapPipeline.h"
herb42da45d2016-04-08 14:24:37 -070011#include "SkBitmapProcShader.h"
reeddd9ffea2016-02-18 12:39:14 -080012#include "SkPM4f.h"
herbfeec8782016-02-17 10:00:07 -080013#include "Benchmark.h"
14#include "SkShader.h"
15#include "SkImage.h"
16
17struct 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 {
herb134ad3e2016-02-17 11:57:24 -080052 fName.set("SkBitmapFP");
herbfeec8782016-02-17 10:00:07 -080053 if (fM.getType() & SkMatrix::kPerspective_Mask) {
herb134ad3e2016-02-17 11:57:24 -080054 fName.append("Perspective");
herbfeec8782016-02-17 10:00:07 -080055 } else if (fM.getType() & SkMatrix::kAffine_Mask) {
herb134ad3e2016-02-17 11:57:24 -080056 fName.append("Affine");
herbfeec8782016-02-17 10:00:07 -080057 } else if (fM.getType() & SkMatrix::kScale_Mask) {
herb134ad3e2016-02-17 11:57:24 -080058 fName.append("Scale");
herbfeec8782016-02-17 10:00:07 -080059 } else if (fM.getType() & SkMatrix::kTranslate_Mask) {
herb134ad3e2016-02-17 11:57:24 -080060 fName.append("Translate");
herbfeec8782016-02-17 10:00:07 -080061 } else {
herb134ad3e2016-02-17 11:57:24 -080062 fName.append("Identity");
herbfeec8782016-02-17 10:00:07 -080063 }
64
herb134ad3e2016-02-17 11:57:24 -080065 fName.append(tileName("X", fXTile));
66 fName.append(tileName("Y", fYTile));
herbfeec8782016-02-17 10:00:07 -080067
68 if (fUseBilerp) {
herb134ad3e2016-02-17 11:57:24 -080069 fName.append("Filter");
herbfeec8782016-02-17 10:00:07 -080070 } else {
herb134ad3e2016-02-17 11:57:24 -080071 fName.append("Nearest");
herbfeec8782016-02-17 10:00:07 -080072 }
73
herb134ad3e2016-02-17 11:57:24 -080074 fName.appendf("%s", BaseName().c_str());
herbfeec8782016-02-17 10:00:07 -080075
herb134ad3e2016-02-17 11:57:24 -080076 return fName.c_str();
herbfeec8782016-02-17 10:00:07 -080077 }
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
herb134ad3e2016-02-17 11:57:24 -0800101 SkString fName;
herbfeec8782016-02-17 10:00:07 -0800102 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
113struct 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) { }
herb134ad3e2016-02-17 11:57:24 -0800122
herbfeec8782016-02-17 10:00:07 -0800123 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
mtkleinb65fcd42016-02-17 13:50:46 -0800137 SkAutoTMalloc<SkPM4f> FPbuffer(width*height);
herbfeec8782016-02-17 10:00:07 -0800138
herbc5eddd72016-02-17 19:50:05 -0800139 SkFilterQuality filterQuality;
140 if (fUseBilerp) {
141 filterQuality = SkFilterQuality::kLow_SkFilterQuality;
142 } else {
143 filterQuality = SkFilterQuality::kNone_SkFilterQuality;
144 }
145
herbed545042016-02-18 13:55:02 -0800146 SkPixmap srcPixmap{fInfo, fBitmap.get(), static_cast<size_t>(4 * width)};
147
herbc5eddd72016-02-17 19:50:05 -0800148 SkLinearBitmapPipeline pipeline{
herb670f01f2016-05-13 10:04:46 -0700149 fInvert, filterQuality, fXTile, fYTile, SK_ColorBLACK, srcPixmap};
herbfeec8782016-02-17 10:00:07 -0800150
151 int count = 100;
152
153 for (int n = 0; n < 1000*loops; n++) {
154 pipeline.shadeSpan4f(3, 6, FPbuffer, count);
155 }
herbfeec8782016-02-17 10:00:07 -0800156 }
157};
158
159struct 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) { }
herb134ad3e2016-02-17 11:57:24 -0800168
herbfeec8782016-02-17 10:00:07 -0800169 SkString BaseName() override {
170 SkString name{"Orig"};
171 return name;
172 }
173
174 void onPreDraw(SkCanvas* c) override {
175 CommonBitmapFPBenchmark::onPreDraw(c);
176
reed9ce9d672016-03-17 10:51:11 -0700177 fImage = SkImage::MakeRasterCopy(
178 SkPixmap(fInfo, fBitmap.get(), sizeof(SkPMColor) * fSrcSize.fWidth));
reed5671c5b2016-03-09 14:47:34 -0800179 fPaint.setShader(fImage->makeShader(fXTile, fYTile));
herbfeec8782016-02-17 10:00:07 -0800180 if (fUseBilerp) {
181 fPaint.setFilterQuality(SkFilterQuality::kLow_SkFilterQuality);
182 } else {
183 fPaint.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
184 }
herbfeec8782016-02-17 10:00:07 -0800185 }
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
mtkleinb65fcd42016-02-17 13:50:46 -0800195 SkAutoTMalloc<SkPMColor> buffer4b(width*height);
herbfeec8782016-02-17 10:00:07 -0800196
herb42da45d2016-04-08 14:24:37 -0700197 uint32_t storage[kSkBlitterContextSize];
fmalitad0c4e092016-02-22 17:19:04 -0800198 const SkShader::ContextRec rec(fPaint, fM, nullptr,
199 SkShader::ContextRec::kPMColor_DstType);
fmalita8d9f2e42016-02-22 10:39:41 -0800200 SkASSERT(fPaint.getShader()->contextSize(rec) <= sizeof(storage));
201 SkShader::Context* ctx = fPaint.getShader()->createContext(rec, storage);
herbfeec8782016-02-17 10:00:07 -0800202
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();
herbfeec8782016-02-17 10:00:07 -0800210 }
211 SkPaint fPaint;
reed9ce9d672016-03-17 10:51:11 -0700212 sk_sp<SkImage> fImage;
herbfeec8782016-02-17 10:00:07 -0800213};
214
215static SkISize srcSize = SkISize::Make(120, 100);
216static SkMatrix mI = SkMatrix::I();
217DEF_BENCH(return new SkBitmapFPGeneral(
218 srcSize, kSRGB_SkColorProfileType, mI, false,
219 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
220
221DEF_BENCH(return new SkBitmapFPGeneral(
222 srcSize, kLinear_SkColorProfileType, mI, false,
223 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
224
225DEF_BENCH(return new SkBitmapFPOrigShader(
226 srcSize, kLinear_SkColorProfileType, mI, false,
227 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
228
229DEF_BENCH(return new SkBitmapFPGeneral(
230 srcSize, kSRGB_SkColorProfileType, mI, true,
231 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
232
233DEF_BENCH(return new SkBitmapFPGeneral(
234 srcSize, kLinear_SkColorProfileType, mI, true,
235 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
236
237DEF_BENCH(return new SkBitmapFPOrigShader(
238 srcSize, kLinear_SkColorProfileType, mI, true,
239 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
240
241static SkMatrix mS = SkMatrix::MakeScale(2.7f, 2.7f);
242DEF_BENCH(return new SkBitmapFPGeneral(
243 srcSize, kSRGB_SkColorProfileType, mS, false,
244 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
245
246DEF_BENCH(return new SkBitmapFPGeneral(
247 srcSize, kLinear_SkColorProfileType, mS, false,
248 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
249
250DEF_BENCH(return new SkBitmapFPOrigShader(
251 srcSize, kLinear_SkColorProfileType, mS, false,
252 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
253
254DEF_BENCH(return new SkBitmapFPGeneral(
255 srcSize, kSRGB_SkColorProfileType, mS, true,
256 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
257
258DEF_BENCH(return new SkBitmapFPGeneral(
259 srcSize, kLinear_SkColorProfileType, mS, true,
260 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
261
262DEF_BENCH(return new SkBitmapFPOrigShader(
263 srcSize, kLinear_SkColorProfileType, mS, true,
264 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
265
herb6eff52a2016-03-23 09:00:33 -0700266// Repeat
267DEF_BENCH(return new SkBitmapFPGeneral(
268 srcSize, kSRGB_SkColorProfileType, mS, false,
269 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
270
271DEF_BENCH(return new SkBitmapFPGeneral(
272 srcSize, kLinear_SkColorProfileType, mS, false,
273 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
274
275DEF_BENCH(return new SkBitmapFPOrigShader(
276 srcSize, kLinear_SkColorProfileType, mS, false,
277 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
278
279DEF_BENCH(return new SkBitmapFPGeneral(
280 srcSize, kSRGB_SkColorProfileType, mS, true,
281 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
282
283DEF_BENCH(return new SkBitmapFPGeneral(
284 srcSize, kLinear_SkColorProfileType, mS, true,
285 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
286
287DEF_BENCH(return new SkBitmapFPOrigShader(
288 srcSize, kLinear_SkColorProfileType, mS, true,
289 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
290
herbfeec8782016-02-17 10:00:07 -0800291static SkMatrix rotate(SkScalar r) {
292 SkMatrix m;
293 m.setRotate(30);
294 return m;
295}
296
297static SkMatrix mR = rotate(30);
298DEF_BENCH(return new SkBitmapFPGeneral(
299 srcSize, kSRGB_SkColorProfileType, mR, false,
300 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
301
302DEF_BENCH(return new SkBitmapFPGeneral(
303 srcSize, kLinear_SkColorProfileType, mR, false,
304 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
305
306DEF_BENCH(return new SkBitmapFPOrigShader(
307 srcSize, kLinear_SkColorProfileType, mR, false,
308 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
309
310DEF_BENCH(return new SkBitmapFPGeneral(
311 srcSize, kSRGB_SkColorProfileType, mR, true,
312 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
313
314DEF_BENCH(return new SkBitmapFPGeneral(
315 srcSize, kLinear_SkColorProfileType, mR, true,
316 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
317
318DEF_BENCH(return new SkBitmapFPOrigShader(
319 srcSize, kLinear_SkColorProfileType, mR, true,
320 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);)
321
herb6eff52a2016-03-23 09:00:33 -0700322// Repeat
323DEF_BENCH(return new SkBitmapFPGeneral(
324 srcSize, kSRGB_SkColorProfileType, mR, false,
325 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
326
327DEF_BENCH(return new SkBitmapFPGeneral(
328 srcSize, kLinear_SkColorProfileType, mR, false,
329 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
330
331DEF_BENCH(return new SkBitmapFPOrigShader(
332 srcSize, kLinear_SkColorProfileType, mR, false,
333 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
334
335DEF_BENCH(return new SkBitmapFPGeneral(
336 srcSize, kSRGB_SkColorProfileType, mR, true,
337 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
338
339DEF_BENCH(return new SkBitmapFPGeneral(
340 srcSize, kLinear_SkColorProfileType, mR, true,
341 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)
342
343DEF_BENCH(return new SkBitmapFPOrigShader(
344 srcSize, kLinear_SkColorProfileType, mR, true,
345 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);)