DisplacementBench.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/SkCanvas.h"
  9. #include "include/core/SkFont.h"
  10. #include "include/core/SkSurface.h"
  11. #include "include/effects/SkDisplacementMapEffect.h"
  12. #include "include/effects/SkImageSource.h"
  13. #define FILTER_WIDTH_SMALL 32
  14. #define FILTER_HEIGHT_SMALL 32
  15. #define FILTER_WIDTH_LARGE 256
  16. #define FILTER_HEIGHT_LARGE 256
  17. class DisplacementBaseBench : public Benchmark {
  18. public:
  19. DisplacementBaseBench(bool small) : fInitialized(false), fIsSmall(small) { }
  20. protected:
  21. void onDelayedSetup() override {
  22. if (!fInitialized) {
  23. this->makeBitmap();
  24. this->makeCheckerboard();
  25. fInitialized = true;
  26. }
  27. }
  28. void makeBitmap() {
  29. const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
  30. const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
  31. fBitmap.allocN32Pixels(w, h);
  32. SkCanvas canvas(fBitmap);
  33. canvas.clear(0x00000000);
  34. SkPaint paint;
  35. paint.setColor(0xFF884422);
  36. SkFont font;
  37. font.setSize(SkIntToScalar(96));
  38. canvas.drawSimpleText("g", 1, SkTextEncoding::kUTF8, SkIntToScalar(15), SkIntToScalar(55), font, paint);
  39. }
  40. void makeCheckerboard() {
  41. const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
  42. const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
  43. auto surface(SkSurface::MakeRasterN32Premul(w, h));
  44. SkCanvas* canvas = surface->getCanvas();
  45. canvas->clear(0x00000000);
  46. SkPaint darkPaint;
  47. darkPaint.setColor(0xFF804020);
  48. SkPaint lightPaint;
  49. lightPaint.setColor(0xFF244484);
  50. for (int y = 0; y < h; y += 16) {
  51. for (int x = 0; x < w; x += 16) {
  52. canvas->save();
  53. canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
  54. canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
  55. canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
  56. canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
  57. canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
  58. canvas->restore();
  59. }
  60. }
  61. fCheckerboard = surface->makeImageSnapshot();
  62. }
  63. void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
  64. canvas->save();
  65. canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
  66. SkIntToScalar(fBitmap.width()),
  67. SkIntToScalar(fBitmap.height())));
  68. canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
  69. canvas->restore();
  70. }
  71. inline bool isSmall() const { return fIsSmall; }
  72. SkBitmap fBitmap;
  73. sk_sp<SkImage> fCheckerboard;
  74. private:
  75. bool fInitialized;
  76. bool fIsSmall;
  77. typedef Benchmark INHERITED;
  78. };
  79. class DisplacementZeroBench : public DisplacementBaseBench {
  80. public:
  81. DisplacementZeroBench(bool small) : INHERITED(small) { }
  82. protected:
  83. const char* onGetName() override {
  84. return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
  85. }
  86. void onDraw(int loops, SkCanvas* canvas) override {
  87. SkPaint paint;
  88. sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
  89. // No displacement effect
  90. paint.setImageFilter(SkDisplacementMapEffect::Make(
  91. SkDisplacementMapEffect::kR_ChannelSelectorType,
  92. SkDisplacementMapEffect::kG_ChannelSelectorType,
  93. 0.0f, std::move(displ), nullptr));
  94. for (int i = 0; i < loops; i++) {
  95. this->drawClippedBitmap(canvas, 0, 0, paint);
  96. }
  97. }
  98. private:
  99. typedef DisplacementBaseBench INHERITED;
  100. };
  101. class DisplacementAlphaBench : public DisplacementBaseBench {
  102. public:
  103. DisplacementAlphaBench(bool small) : INHERITED(small) { }
  104. protected:
  105. const char* onGetName() override {
  106. return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
  107. }
  108. void onDraw(int loops, SkCanvas* canvas) override {
  109. SkPaint paint;
  110. sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
  111. // Displacement, with 1 alpha component (which isn't pre-multiplied)
  112. paint.setImageFilter(SkDisplacementMapEffect::Make(
  113. SkDisplacementMapEffect::kB_ChannelSelectorType,
  114. SkDisplacementMapEffect::kA_ChannelSelectorType,
  115. 16.0f, std::move(displ), nullptr));
  116. for (int i = 0; i < loops; i++) {
  117. this->drawClippedBitmap(canvas, 100, 0, paint);
  118. }
  119. }
  120. private:
  121. typedef DisplacementBaseBench INHERITED;
  122. };
  123. class DisplacementFullBench : public DisplacementBaseBench {
  124. public:
  125. DisplacementFullBench(bool small) : INHERITED(small) { }
  126. protected:
  127. const char* onGetName() override {
  128. return isSmall() ? "displacement_full_small" : "displacement_full_large";
  129. }
  130. void onDraw(int loops, SkCanvas* canvas) override {
  131. SkPaint paint;
  132. sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
  133. // Displacement, with 2 non-alpha components
  134. paint.setImageFilter(SkDisplacementMapEffect::Make(
  135. SkDisplacementMapEffect::kR_ChannelSelectorType,
  136. SkDisplacementMapEffect::kB_ChannelSelectorType,
  137. 32.0f, std::move(displ), nullptr));
  138. for (int i = 0; i < loops; ++i) {
  139. this->drawClippedBitmap(canvas, 200, 0, paint);
  140. }
  141. }
  142. private:
  143. typedef DisplacementBaseBench INHERITED;
  144. };
  145. ///////////////////////////////////////////////////////////////////////////////
  146. DEF_BENCH( return new DisplacementZeroBench(true); )
  147. DEF_BENCH( return new DisplacementAlphaBench(true); )
  148. DEF_BENCH( return new DisplacementFullBench(true); )
  149. DEF_BENCH( return new DisplacementZeroBench(false); )
  150. DEF_BENCH( return new DisplacementAlphaBench(false); )
  151. DEF_BENCH( return new DisplacementFullBench(false); )