blob: f2a9e97296f28c162414eee6a69688fda5982445 [file] [log] [blame]
robertphillips@google.com8e679fe2013-01-17 19:51:41 +00001/*
2 * Copyright 2013 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkShader.h"
12#include "include/core/SkString.h"
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000013
14static void create_gradient(SkBitmap* bm) {
15 SkASSERT(1 == bm->width());
16 const int height = bm->height();
17
18 float deltaB = 255.0f / height;
19 float blue = 255.0f;
20
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000021 for (int y = 0; y < height; y++) {
22 *bm->getAddr32(0, y) = SkColorSetRGB(0, 0, (U8CPU) blue);
23 blue -= deltaB;
24 }
25}
26
27// Test out the special case of a tiled 1xN texture. Test out opacity,
28// filtering and the different tiling modes
tfarinaf168b862014-06-19 12:32:29 -070029class ConstXTileBench : public Benchmark {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000030 SkPaint fPaint;
31 SkString fName;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000032 bool fDoTrans;
robertphillips@google.com1e305232013-01-22 20:29:16 +000033 bool fDoScale;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000034 static const int kWidth = 1;
35 static const int kHeight = 300;
36
37public:
Mike Reedfae8fce2019-04-03 10:27:45 -040038 ConstXTileBench(SkTileMode xTile,
39 SkTileMode yTile,
Mike Reed057fcbe2020-12-12 14:31:25 -050040 SkFilterMode fm,
robertphillips@google.com1e305232013-01-22 20:29:16 +000041 bool doTrans,
42 bool doScale)
Mike Reed057fcbe2020-12-12 14:31:25 -050043 : fDoTrans(doTrans)
robertphillips@google.com1e305232013-01-22 20:29:16 +000044 , fDoScale(doScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000045 SkBitmap bm;
46
reed6c225732014-06-09 19:52:07 -070047 bm.allocN32Pixels(kWidth, kHeight, true);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000048 bm.eraseColor(SK_ColorWHITE);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000049
50 create_gradient(&bm);
51
Mike Reed5ec22382021-01-14 21:59:01 -050052 fPaint.setShader(bm.makeShader(xTile, yTile, SkSamplingOptions(fm)));
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000053
54 fName.printf("constXTile_");
55
Mike Reedfae8fce2019-04-03 10:27:45 -040056 static const char* gTileModeStr[kSkTileModeCount] = { "C", "R", "M", "D" };
57 fName.append(gTileModeStr[(unsigned)xTile]);
58 fName.append(gTileModeStr[(unsigned)yTile]);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000059
Mike Reed057fcbe2020-12-12 14:31:25 -050060 if (fm != SkFilterMode::kNearest) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000061 fName.append("_filter");
62 }
63
64 if (doTrans) {
65 fName.append("_trans");
66 }
robertphillips@google.com1e305232013-01-22 20:29:16 +000067
68 if (doScale) {
69 fName.append("_scale");
70 }
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000071 }
72
73protected:
Brian Salomond0072812020-07-21 17:03:56 -040074 const char* onGetName() override {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000075 return fName.c_str();
76 }
77
Brian Salomond0072812020-07-21 17:03:56 -040078 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000079 SkPaint paint(fPaint);
80 this->setupPaint(&paint);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000081 if (fDoTrans) {
Cary Clark6cdb7d32018-04-24 14:47:16 -040082 paint.setColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000083 }
84
85 SkRect r;
86
robertphillips@google.com1e305232013-01-22 20:29:16 +000087 if (fDoScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000088 r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480));
89 canvas->scale(SK_ScalarHalf, SK_ScalarHalf);
90 } else {
91 r = SkRect::MakeWH(SkIntToScalar(640), SkIntToScalar(480));
92 }
93
94 SkPaint bgPaint;
95 bgPaint.setColor(SK_ColorWHITE);
96
commit-bot@chromium.org33614712013-12-03 18:17:16 +000097 for (int i = 0; i < loops; i++) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000098 if (fDoTrans) {
99 canvas->drawRect(r, bgPaint);
100 }
101
102 canvas->drawRect(r, paint);
103 }
104 }
105
106private:
John Stiles7571f9e2020-09-02 22:42:33 -0400107 using INHERITED = Benchmark;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000108};
109
mtklein474cef42014-06-24 15:19:25 -0700110// Scaled benches are trending towards free. Seems like caching.
111// TODO(mtklein, reed): fix and reenable
112
Mike Reed057fcbe2020-12-12 14:31:25 -0500113 constexpr SkFilterMode gNN = SkFilterMode::kNearest;
114 constexpr SkFilterMode gLI = SkFilterMode::kLinear;
115
Mike Reedfae8fce2019-04-03 10:27:45 -0400116//DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, false, false, true))
Mike Reed057fcbe2020-12-12 14:31:25 -0500117DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, gNN, false, false))
Mike Reedfae8fce2019-04-03 10:27:45 -0400118//DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, false, false, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000119
Mike Reed057fcbe2020-12-12 14:31:25 -0500120DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, gLI, false, false))
Mike Reedfae8fce2019-04-03 10:27:45 -0400121//DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, true, false, true))
Mike Reed057fcbe2020-12-12 14:31:25 -0500122DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, gLI, false, false))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000123
Mike Reedfae8fce2019-04-03 10:27:45 -0400124//DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, false, true, true))
Mike Reed057fcbe2020-12-12 14:31:25 -0500125DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, gNN, true, false))
Mike Reedfae8fce2019-04-03 10:27:45 -0400126//DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, false, true, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000127
Mike Reed057fcbe2020-12-12 14:31:25 -0500128DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, gLI, true, false))
Mike Reedfae8fce2019-04-03 10:27:45 -0400129//DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, true, true, true))
Mike Reed057fcbe2020-12-12 14:31:25 -0500130DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, gLI, true, false))