blob: b4e9adb1b2f4ea908cdb11c8c7e063d1dc1d1fad [file] [log] [blame]
Jim Van Verth72f48912017-05-18 14:31:19 -04001/*
2 * Copyright 2017 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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "bench/Benchmark.h"
8#include "include/core/SkCanvas.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkPath.h"
11#include "include/utils/SkShadowUtils.h"
12#include "src/core/SkDrawShadowInfo.h"
Jim Van Verth72f48912017-05-18 14:31:19 -040013
14class ShadowBench : public Benchmark {
15// Draws a set of shadowed rrects filling the canvas, in various modes:
16// * opaque or transparent
17// * use analytic fast path or geometric tessellation
18public:
19 ShadowBench(bool transparent, bool forceGeometric)
20 : fTransparent(transparent)
21 , fForceGeometric(forceGeometric) {
22 computeName("shadows");
23 }
24
Jim Van Verth72f48912017-05-18 14:31:19 -040025protected:
26 enum {
27 kWidth = 640,
28 kHeight = 480,
29 kRRSize = 50,
30 kRRRadius = 6,
31 kRRSpace = 8,
32 kRRStep = kRRSize + kRRSpace,
33 kElevation = 16,
34 kNumRRects = ((kWidth - kRRSpace) / kRRStep)*((kHeight - kRRSpace) / kRRStep)
35 };
36
37 void computeName(const char root[]) {
38 static const char kTransChars[2] = {
39 'o', 't'
40 };
41 static const char kGeomChars[2] = {
42 'a', 'g'
43 };
44
45 fBaseName.printf("%s_%c_%c", root, kTransChars[fTransparent], kGeomChars[fForceGeometric]);
46 }
47
48 void genRRects() {
49 int i = 0;
50 for (int x = kRRSpace; x < kWidth - kRRStep; x += kRRStep) {
51 for (int y = kRRSpace; y < kHeight - kRRStep; y += kRRStep) {
52 SkRect rect = SkRect::MakeXYWH(x, y, kRRSize, kRRSize);
53 fRRects[i].addRRect(SkRRect::MakeRectXY(rect, kRRRadius, kRRRadius));
54 ++i;
55 }
56 }
57 SkASSERT(i == kNumRRects);
58 }
59
60 const char* onGetName() override { return fBaseName.c_str(); }
61
62 void onDelayedSetup() override {
63 fRec.fZPlaneParams = SkPoint3::Make(0, 0, kElevation);
64 fRec.fLightPos = SkPoint3::Make(270, 0, 600);
65 fRec.fLightRadius = 800;
Jim Van Verthb1b80f72018-01-18 15:19:13 -050066 fRec.fAmbientColor = 0x19000000;
67 fRec.fSpotColor = 0x40000000;
Jim Van Verth72f48912017-05-18 14:31:19 -040068 fRec.fFlags = 0;
69 if (fTransparent) {
70 fRec.fFlags |= SkShadowFlags::kTransparentOccluder_ShadowFlag;
71 }
72 if (fForceGeometric) {
73 fRec.fFlags |= SkShadowFlags::kGeometricOnly_ShadowFlag;
74 }
75
76 this->genRRects();
77 }
78
79 void onDraw(int loops, SkCanvas* canvas) override {
80 SkPaint paint;
81 paint.setColor(SK_ColorWHITE);
82 this->setupPaint(&paint);
83
84 for (int i = 0; i < loops; ++i) {
85 // use the private canvas call so we don't include the time to stuff data in the Rec
86 canvas->private_draw_shadow_rec(fRRects[i % kNumRRects], fRec);
87 }
88 }
89
90private:
91 SkString fBaseName;
92
93 SkPath fRRects[kNumRRects];
94 SkDrawShadowRec fRec;
95 int fTransparent;
96 int fForceGeometric;
97
John Stiles7571f9e2020-09-02 22:42:33 -040098 using INHERITED = Benchmark;
Jim Van Verth72f48912017-05-18 14:31:19 -040099};
100
101DEF_BENCH(return new ShadowBench(false, false);)
102DEF_BENCH(return new ShadowBench(false, true);)
103DEF_BENCH(return new ShadowBench(true, false);)
104DEF_BENCH(return new ShadowBench(true, true);)
105