GrMipMapBench.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2016 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/SkImage.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkSurface.h"
  12. class GrMipMapBench: public Benchmark {
  13. sk_sp<SkSurface> fSurface;
  14. SkString fName;
  15. const int fW, fH;
  16. public:
  17. GrMipMapBench(int w, int h) : fW(w), fH(h) {
  18. fName.printf("gr_mipmap_build_%dx%d", w, h);
  19. }
  20. protected:
  21. bool isSuitableFor(Backend backend) override {
  22. return kGPU_Backend == backend;
  23. }
  24. const char* onGetName() override { return fName.c_str(); }
  25. void onDraw(int loops, SkCanvas* canvas) override {
  26. if (!fSurface) {
  27. GrContext* context = canvas->getGrContext();
  28. if (nullptr == context) {
  29. return;
  30. }
  31. auto srgb = SkColorSpace::MakeSRGB();
  32. SkImageInfo info =
  33. SkImageInfo::Make(fW, fH, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb);
  34. // We're benching the regeneration of the mip levels not the need to allocate them every
  35. // frame. Thus we create the surface with mips to begin with.
  36. fSurface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
  37. kBottomLeft_GrSurfaceOrigin, nullptr, true);
  38. }
  39. // Clear surface once:
  40. fSurface->getCanvas()->clear(SK_ColorBLACK);
  41. SkPaint paint;
  42. paint.setFilterQuality(kMedium_SkFilterQuality);
  43. paint.setColor(SK_ColorWHITE);
  44. for (int i = 0; i < loops; i++) {
  45. // Touch surface so mips are dirtied
  46. fSurface->getCanvas()->drawPoint(0, 0, paint);
  47. // Draw reduced version of surface to original canvas, to trigger mip generation
  48. canvas->save();
  49. canvas->scale(0.1f, 0.1f);
  50. canvas->drawImage(fSurface->makeImageSnapshot(), 0, 0, &paint);
  51. canvas->restore();
  52. }
  53. }
  54. void onPerCanvasPostDraw(SkCanvas*) override {
  55. fSurface.reset(nullptr);
  56. }
  57. private:
  58. typedef Benchmark INHERITED;
  59. };
  60. // Build variants that exercise the width and heights being even or odd at each level, as the
  61. // impl specializes on each of these.
  62. //
  63. DEF_BENCH( return new GrMipMapBench(511, 511); )
  64. DEF_BENCH( return new GrMipMapBench(512, 511); )
  65. DEF_BENCH( return new GrMipMapBench(511, 512); )
  66. DEF_BENCH( return new GrMipMapBench(512, 512); )