ShadowBench.cpp 3.1 KB

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