AlternatingColorPatternBench.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2014 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/SkPaint.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/core/SkString.h"
  13. #include "include/effects/SkGradientShader.h"
  14. enum ColorPattern {
  15. kWhite_ColorPattern,
  16. kBlue_ColorPattern,
  17. kOpaqueBitmap_ColorPattern,
  18. kAlphaBitmap_ColorPattern,
  19. };
  20. static const struct ColorPatternData{
  21. SkColor fColor;
  22. bool fIsBitmap;
  23. const char* fName;
  24. } gColorPatterns[] = {
  25. // Keep this in same order as ColorPattern enum
  26. { SK_ColorWHITE, false, "white" }, // kWhite_ColorPattern
  27. { SK_ColorBLUE, false, "blue" }, // kBlue_ColorPattern
  28. { SK_ColorWHITE, true, "obaqueBitMap" }, // kOpaqueBitmap_ColorPattern
  29. { 0x10000000, true, "alphaBitmap" }, // kAlphaBitmap_ColorPattern
  30. };
  31. enum DrawType {
  32. kRect_DrawType,
  33. kPath_DrawType,
  34. };
  35. static void makebm(SkBitmap* bm, int w, int h) {
  36. bm->allocN32Pixels(w, h);
  37. bm->eraseColor(SK_ColorTRANSPARENT);
  38. SkCanvas canvas(*bm);
  39. SkScalar s = SkIntToScalar(SkMin32(w, h));
  40. static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
  41. static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
  42. static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
  43. static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
  44. static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
  45. SkPaint paint;
  46. paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, SK_ARRAY_COUNT(kColors0),
  47. SkTileMode::kClamp));
  48. canvas.drawPaint(paint);
  49. paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, SK_ARRAY_COUNT(kColors1),
  50. SkTileMode::kClamp));
  51. canvas.drawPaint(paint);
  52. }
  53. /**
  54. * This bench draws a grid of either rects or filled paths, with two alternating color patterns.
  55. * This color patterns are passed in as enums to the class. The options are:
  56. * 1) solid white color
  57. * 2) solid blue color
  58. * 3) opaque bitmap
  59. * 4) partial alpha bitmap
  60. * The same color pattern can be set for both arguments to create a uniform pattern on all draws.
  61. *
  62. * The bench is used to test a few things. First it can test any optimizations made for a specific
  63. * color pattern (for example drawing an opaque bitmap versus one with partial alpha). Also it can
  64. * be used to test the cost of program switching and/or GrDrawOp combining when alternating between
  65. * different patterns when on the gpu.
  66. */
  67. class AlternatingColorPatternBench : public Benchmark {
  68. public:
  69. enum {
  70. NX = 5,
  71. NY = 5,
  72. NUM_DRAWS = NX * NY,
  73. };
  74. sk_sp<SkShader> fBmShader;
  75. SkPath fPaths[NUM_DRAWS];
  76. SkRect fRects[NUM_DRAWS];
  77. SkColor fColors[NUM_DRAWS];
  78. sk_sp<SkShader> fShaders[NUM_DRAWS];
  79. SkString fName;
  80. ColorPatternData fPattern1;
  81. ColorPatternData fPattern2;
  82. DrawType fDrawType;
  83. SkBitmap fBmp;
  84. AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, DrawType drawType) {
  85. fPattern1 = gColorPatterns[pattern1];
  86. fPattern2 = gColorPatterns[pattern2];
  87. fName.printf("colorPattern_%s_%s_%s",
  88. fPattern1.fName, fPattern2.fName,
  89. kRect_DrawType == drawType ? "rect" : "path");
  90. fDrawType = drawType;
  91. }
  92. protected:
  93. const char* onGetName() override {
  94. return fName.c_str();
  95. }
  96. void onDelayedSetup() override {
  97. int w = 40;
  98. int h = 40;
  99. makebm(&fBmp, w, h);
  100. fBmShader = fBmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
  101. int offset = 2;
  102. int count = 0;
  103. for (int j = 0; j < NY; ++j) {
  104. for (int i = 0; i < NX; ++i) {
  105. int x = (w + offset) * i;
  106. int y = (h * offset) * j;
  107. if (kRect_DrawType == fDrawType) {
  108. fRects[count].set(SkIntToScalar(x), SkIntToScalar(y),
  109. SkIntToScalar(x + w), SkIntToScalar(y + h));
  110. } else {
  111. fPaths[count].moveTo(SkIntToScalar(x), SkIntToScalar(y));
  112. fPaths[count].rLineTo(SkIntToScalar(w), 0);
  113. fPaths[count].rLineTo(0, SkIntToScalar(h));
  114. fPaths[count].rLineTo(SkIntToScalar(-w + 1), 0);
  115. }
  116. if (0 == count % 2) {
  117. fColors[count] = fPattern1.fColor;
  118. fShaders[count] = fPattern1.fIsBitmap ? fBmShader : nullptr;
  119. } else {
  120. fColors[count] = fPattern2.fColor;
  121. fShaders[count] = fPattern2.fIsBitmap ? fBmShader : nullptr;
  122. }
  123. ++count;
  124. }
  125. }
  126. }
  127. void onDraw(int loops, SkCanvas* canvas) override {
  128. SkPaint paint;
  129. paint.setAntiAlias(false);
  130. paint.setFilterQuality(kLow_SkFilterQuality);
  131. for (int i = 0; i < loops; ++i) {
  132. for (int j = 0; j < NUM_DRAWS; ++j) {
  133. paint.setColor(fColors[j]);
  134. paint.setShader(fShaders[j]);
  135. if (kRect_DrawType == fDrawType) {
  136. canvas->drawRect(fRects[j], paint);
  137. } else {
  138. canvas->drawPath(fPaths[j], paint);
  139. }
  140. }
  141. }
  142. }
  143. private:
  144. typedef Benchmark INHERITED;
  145. };
  146. DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
  147. kWhite_ColorPattern,
  148. kPath_DrawType);)
  149. DEF_BENCH(return new AlternatingColorPatternBench(kBlue_ColorPattern,
  150. kBlue_ColorPattern,
  151. kPath_DrawType);)
  152. DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
  153. kBlue_ColorPattern,
  154. kPath_DrawType);)
  155. DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
  156. kOpaqueBitmap_ColorPattern,
  157. kPath_DrawType);)
  158. DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
  159. kAlphaBitmap_ColorPattern,
  160. kPath_DrawType);)
  161. DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
  162. kAlphaBitmap_ColorPattern,
  163. kPath_DrawType);)
  164. DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
  165. kOpaqueBitmap_ColorPattern,
  166. kRect_DrawType);)
  167. DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
  168. kAlphaBitmap_ColorPattern,
  169. kRect_DrawType);)
  170. DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
  171. kAlphaBitmap_ColorPattern,
  172. kRect_DrawType);)