InterpBench.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2015 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/SkMatrix.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/core/SkString.h"
  11. #include "include/private/SkColorData.h"
  12. #include "include/private/SkFixed.h"
  13. #include "include/utils/SkRandom.h"
  14. #define TILE(x, width) (((x) & 0xFFFF) * width >> 16)
  15. class InterpBench : public Benchmark {
  16. enum {
  17. kBuffer = 128,
  18. kLoop = 20000
  19. };
  20. SkString fName;
  21. int16_t fDst[kBuffer];
  22. float fFx, fDx;
  23. public:
  24. InterpBench(const char name[]) {
  25. fName.printf("interp_%s", name);
  26. fFx = 3.3f;
  27. fDx = 0.1257f;
  28. }
  29. bool isSuitableFor(Backend backend) override {
  30. return backend == kNonRendering_Backend;
  31. }
  32. virtual void performTest(int16_t dst[], float x, float dx, int count) = 0;
  33. protected:
  34. virtual int mulLoopCount() const { return 1; }
  35. const char* onGetName() override {
  36. return fName.c_str();
  37. }
  38. void onDraw(int loops, SkCanvas*) override {
  39. int n = loops * this->mulLoopCount();
  40. for (int i = 0; i < n; i++) {
  41. this->performTest(fDst, fFx, fDx, kBuffer);
  42. }
  43. }
  44. private:
  45. typedef Benchmark INHERITED;
  46. };
  47. class Fixed16D16Interp : public InterpBench {
  48. public:
  49. Fixed16D16Interp() : INHERITED("16.16") {}
  50. protected:
  51. void performTest(int16_t dst[], float fx, float dx, int count) override {
  52. SkFixed curr = SkFloatToFixed(fx);
  53. SkFixed step = SkFloatToFixed(dx);
  54. for (int i = 0; i < count; i += 4) {
  55. dst[i + 0] = TILE(curr, count); curr += step;
  56. dst[i + 1] = TILE(curr, count); curr += step;
  57. dst[i + 2] = TILE(curr, count); curr += step;
  58. dst[i + 3] = TILE(curr, count); curr += step;
  59. }
  60. }
  61. private:
  62. typedef InterpBench INHERITED;
  63. };
  64. class Fixed32D32Interp : public InterpBench {
  65. public:
  66. Fixed32D32Interp() : INHERITED("32.32") {}
  67. protected:
  68. void performTest(int16_t dst[], float fx, float dx, int count) override {
  69. int64_t curr = (int64_t)(fx * 65536 * 655536);
  70. int64_t step = (int64_t)(dx * 65536 * 655536);
  71. SkFixed tmp;
  72. for (int i = 0; i < count; i += 4) {
  73. tmp = (SkFixed)(curr >> 16);
  74. dst[i + 0] = TILE(tmp, count);
  75. curr += step;
  76. tmp = (SkFixed)(curr >> 16);
  77. dst[i + 1] = TILE(tmp, count);
  78. curr += step;
  79. tmp = (SkFixed)(curr >> 16);
  80. dst[i + 2] = TILE(tmp, count);
  81. curr += step;
  82. tmp = (SkFixed)(curr >> 16);
  83. dst[i + 3] = TILE(tmp, count);
  84. curr += step;
  85. }
  86. }
  87. private:
  88. typedef InterpBench INHERITED;
  89. };
  90. class Fixed16D48Interp : public InterpBench {
  91. public:
  92. Fixed16D48Interp() : INHERITED("16.48") {}
  93. protected:
  94. void performTest(int16_t dst[], float fx, float dx, int count) override {
  95. int64_t curr = (int64_t)(fx * 65536 * 655536 * 65536);
  96. int64_t step = (int64_t)(dx * 65536 * 655536 * 65536);
  97. SkFixed tmp;
  98. for (int i = 0; i < count; i += 4) {
  99. tmp = (SkFixed) (curr >> 32); dst[i + 0] = TILE(tmp, count); curr += step;
  100. tmp = (SkFixed) (curr >> 32); dst[i + 1] = TILE(tmp, count); curr += step;
  101. tmp = (SkFixed) (curr >> 32); dst[i + 2] = TILE(tmp, count); curr += step;
  102. tmp = (SkFixed) (curr >> 32); dst[i + 3] = TILE(tmp, count); curr += step;
  103. }
  104. }
  105. private:
  106. typedef InterpBench INHERITED;
  107. };
  108. class FloatInterp : public InterpBench {
  109. public:
  110. FloatInterp() : INHERITED("float") {}
  111. protected:
  112. void performTest(int16_t dst[], float fx, float dx, int count) override {
  113. SkFixed tmp;
  114. for (int i = 0; i < count; i += 4) {
  115. tmp = SkFloatToFixed(fx); dst[i + 0] = TILE(tmp, count); fx += dx;
  116. tmp = SkFloatToFixed(fx); dst[i + 1] = TILE(tmp, count); fx += dx;
  117. tmp = SkFloatToFixed(fx); dst[i + 2] = TILE(tmp, count); fx += dx;
  118. tmp = SkFloatToFixed(fx); dst[i + 3] = TILE(tmp, count); fx += dx;
  119. }
  120. }
  121. private:
  122. typedef InterpBench INHERITED;
  123. };
  124. class DoubleInterp : public InterpBench {
  125. public:
  126. DoubleInterp() : INHERITED("double") {}
  127. protected:
  128. void performTest(int16_t dst[], float fx, float dx, int count) override {
  129. double ffx = fx;
  130. double ddx = dx;
  131. SkFixed tmp;
  132. for (int i = 0; i < count; i += 4) {
  133. tmp = SkDoubleToFixed(ffx); dst[i + 0] = TILE(tmp, count); ffx += ddx;
  134. tmp = SkDoubleToFixed(ffx); dst[i + 1] = TILE(tmp, count); ffx += ddx;
  135. tmp = SkDoubleToFixed(ffx); dst[i + 2] = TILE(tmp, count); ffx += ddx;
  136. tmp = SkDoubleToFixed(ffx); dst[i + 3] = TILE(tmp, count); ffx += ddx;
  137. }
  138. }
  139. private:
  140. typedef InterpBench INHERITED;
  141. };
  142. ///////////////////////////////////////////////////////////////////////////////
  143. DEF_BENCH( return new Fixed16D16Interp(); )
  144. DEF_BENCH( return new Fixed32D32Interp(); )
  145. DEF_BENCH( return new Fixed16D48Interp(); )
  146. DEF_BENCH( return new FloatInterp(); )
  147. DEF_BENCH( return new DoubleInterp(); )