RegionContainBench.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2013 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/SkRegion.h"
  9. #include "include/core/SkString.h"
  10. #include "include/utils/SkRandom.h"
  11. static bool sect_proc(SkRegion& a, SkRegion& b) {
  12. SkRegion result;
  13. return result.op(a, b, SkRegion::kIntersect_Op);
  14. }
  15. class RegionContainBench : public Benchmark {
  16. public:
  17. typedef bool (*Proc)(SkRegion& a, SkRegion& b);
  18. SkRegion fA, fB;
  19. Proc fProc;
  20. SkString fName;
  21. enum {
  22. W = 200,
  23. H = 200,
  24. COUNT = 10,
  25. };
  26. SkIRect randrect(SkRandom& rand, int i) {
  27. int w = rand.nextU() % W;
  28. return SkIRect::MakeXYWH(0, i*H/COUNT, w, H/COUNT);
  29. }
  30. RegionContainBench(Proc proc, const char name[]) {
  31. fProc = proc;
  32. fName.printf("region_contains_%s", name);
  33. SkRandom rand;
  34. for (int i = 0; i < COUNT; i++) {
  35. fA.op(randrect(rand, i), SkRegion::kXOR_Op);
  36. }
  37. fB.setRect(0, 0, H, W);
  38. }
  39. bool isSuitableFor(Backend backend) override {
  40. return backend == kNonRendering_Backend;
  41. }
  42. protected:
  43. const char* onGetName() override { return fName.c_str(); }
  44. void onDraw(int loops, SkCanvas*) override {
  45. Proc proc = fProc;
  46. for (int i = 0; i < loops; ++i) {
  47. proc(fA, fB);
  48. }
  49. }
  50. private:
  51. typedef Benchmark INHERITED;
  52. };
  53. DEF_BENCH(return new RegionContainBench(sect_proc, "sect");)