RepeatTileBench.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2011 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/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColorPriv.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkShader.h"
  13. #include "include/core/SkString.h"
  14. #include "tools/ToolUtils.h"
  15. static void draw_into_bitmap(const SkBitmap& bm) {
  16. const int w = bm.width();
  17. const int h = bm.height();
  18. SkCanvas canvas(bm);
  19. SkPaint p;
  20. p.setAntiAlias(true);
  21. p.setColor(SK_ColorRED);
  22. canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
  23. SkIntToScalar(SkMin32(w, h))*3/8, p);
  24. SkRect r;
  25. r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
  26. p.setStyle(SkPaint::kStroke_Style);
  27. p.setStrokeWidth(SkIntToScalar(4));
  28. p.setColor(SK_ColorBLUE);
  29. canvas.drawRect(r, p);
  30. }
  31. class RepeatTileBench : public Benchmark {
  32. const SkAlphaType fAlphaType;
  33. SkPaint fPaint;
  34. SkString fName;
  35. SkBitmap fBitmap;
  36. public:
  37. RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType) : fAlphaType(at) {
  38. const int w = 50;
  39. const int h = 50;
  40. fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at));
  41. fName.printf("repeatTile_%s_%c",
  42. ToolUtils::colortype_name(ct),
  43. kOpaque_SkAlphaType == at ? 'X' : 'A');
  44. }
  45. protected:
  46. const char* onGetName() override {
  47. return fName.c_str();
  48. }
  49. void onDelayedSetup() override {
  50. fBitmap.allocPixels();
  51. fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0);
  52. draw_into_bitmap(fBitmap);
  53. fPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
  54. }
  55. void onDraw(int loops, SkCanvas* canvas) override {
  56. SkPaint paint(fPaint);
  57. this->setupPaint(&paint);
  58. for (int i = 0; i < loops; i++) {
  59. canvas->drawPaint(paint);
  60. }
  61. }
  62. private:
  63. typedef Benchmark INHERITED;
  64. };
  65. DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType))
  66. DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType))
  67. DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType))