blob: f329664fb2489e8a2f17418b7ee9930331764039 [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 */
7#include "Benchmark.h"
8#include "SkCanvas.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -04009#include "SkDrawShadowInfo.h"
Jim Van Verth72f48912017-05-18 14:31:19 -040010#include "SkPaint.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040011#include "SkPath.h"
Jim Van Verth72f48912017-05-18 14:31:19 -040012#include "SkShadowUtils.h"
13
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
25 bool isVisual() override { return true; }
26
27protected:
28 enum {
29 kWidth = 640,
30 kHeight = 480,
31 kRRSize = 50,
32 kRRRadius = 6,
33 kRRSpace = 8,
34 kRRStep = kRRSize + kRRSpace,
35 kElevation = 16,
36 kNumRRects = ((kWidth - kRRSpace) / kRRStep)*((kHeight - kRRSpace) / kRRStep)
37 };
38
39 void computeName(const char root[]) {
40 static const char kTransChars[2] = {
41 'o', 't'
42 };
43 static const char kGeomChars[2] = {
44 'a', 'g'
45 };
46
47 fBaseName.printf("%s_%c_%c", root, kTransChars[fTransparent], kGeomChars[fForceGeometric]);
48 }
49
50 void genRRects() {
51 int i = 0;
52 for (int x = kRRSpace; x < kWidth - kRRStep; x += kRRStep) {
53 for (int y = kRRSpace; y < kHeight - kRRStep; y += kRRStep) {
54 SkRect rect = SkRect::MakeXYWH(x, y, kRRSize, kRRSize);
55 fRRects[i].addRRect(SkRRect::MakeRectXY(rect, kRRRadius, kRRRadius));
56 ++i;
57 }
58 }
59 SkASSERT(i == kNumRRects);
60 }
61
62 const char* onGetName() override { return fBaseName.c_str(); }
63
64 void onDelayedSetup() override {
65 fRec.fZPlaneParams = SkPoint3::Make(0, 0, kElevation);
66 fRec.fLightPos = SkPoint3::Make(270, 0, 600);
67 fRec.fLightRadius = 800;
68 fRec.fAmbientAlpha = 0.1f;
69 fRec.fSpotAlpha = 0.25f;
70 fRec.fColor = SK_ColorBLACK;
71 fRec.fFlags = 0;
72 if (fTransparent) {
73 fRec.fFlags |= SkShadowFlags::kTransparentOccluder_ShadowFlag;
74 }
75 if (fForceGeometric) {
76 fRec.fFlags |= SkShadowFlags::kGeometricOnly_ShadowFlag;
77 }
78
79 this->genRRects();
80 }
81
82 void onDraw(int loops, SkCanvas* canvas) override {
83 SkPaint paint;
84 paint.setColor(SK_ColorWHITE);
85 this->setupPaint(&paint);
86
87 for (int i = 0; i < loops; ++i) {
88 // use the private canvas call so we don't include the time to stuff data in the Rec
89 canvas->private_draw_shadow_rec(fRRects[i % kNumRRects], fRec);
90 }
91 }
92
93private:
94 SkString fBaseName;
95
96 SkPath fRRects[kNumRRects];
97 SkDrawShadowRec fRec;
98 int fTransparent;
99 int fForceGeometric;
100
101 typedef Benchmark INHERITED;
102};
103
104DEF_BENCH(return new ShadowBench(false, false);)
105DEF_BENCH(return new ShadowBench(false, true);)
106DEF_BENCH(return new ShadowBench(true, false);)
107DEF_BENCH(return new ShadowBench(true, true);)
108