Jim Van Verth | 72f4891 | 2017-05-18 14:31:19 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #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 Verth | 72f4891 | 2017-05-18 14:31:19 -0400 | [diff] [blame] | 13 | |
| 14 | class 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 |
| 18 | public: |
| 19 | ShadowBench(bool transparent, bool forceGeometric) |
| 20 | : fTransparent(transparent) |
| 21 | , fForceGeometric(forceGeometric) { |
| 22 | computeName("shadows"); |
| 23 | } |
| 24 | |
Jim Van Verth | 72f4891 | 2017-05-18 14:31:19 -0400 | [diff] [blame] | 25 | protected: |
| 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 Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 66 | fRec.fAmbientColor = 0x19000000; |
| 67 | fRec.fSpotColor = 0x40000000; |
Jim Van Verth | 72f4891 | 2017-05-18 14:31:19 -0400 | [diff] [blame] | 68 | 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 | |
| 90 | private: |
| 91 | SkString fBaseName; |
| 92 | |
| 93 | SkPath fRRects[kNumRRects]; |
| 94 | SkDrawShadowRec fRec; |
| 95 | int fTransparent; |
| 96 | int fForceGeometric; |
| 97 | |
| 98 | typedef Benchmark INHERITED; |
| 99 | }; |
| 100 | |
| 101 | DEF_BENCH(return new ShadowBench(false, false);) |
| 102 | DEF_BENCH(return new ShadowBench(false, true);) |
| 103 | DEF_BENCH(return new ShadowBench(true, false);) |
| 104 | DEF_BENCH(return new ShadowBench(true, true);) |
| 105 | |