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