VertBench.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/SkCanvas.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/core/SkShader.h"
  11. #include "include/core/SkString.h"
  12. #include "include/core/SkVertices.h"
  13. #include "include/utils/SkRandom.h"
  14. enum VertFlags {
  15. kColors_VertFlag,
  16. kTexture_VertFlag,
  17. };
  18. class VertBench : public Benchmark {
  19. SkString fName;
  20. enum {
  21. W = 640,
  22. H = 480,
  23. ROW = 20,
  24. COL = 20,
  25. PTS = (ROW + 1) * (COL + 1),
  26. IDX = ROW * COL * 6,
  27. };
  28. SkPoint fPts[PTS];
  29. SkColor fColors[PTS];
  30. uint16_t fIdx[IDX];
  31. static void load_2_tris(uint16_t idx[], int x, int y, int rb) {
  32. int n = y * rb + x;
  33. idx[0] = n; idx[1] = n + 1; idx[2] = rb + n + 1;
  34. idx[3] = n; idx[4] = rb + n + 1; idx[5] = n + rb;
  35. }
  36. public:
  37. VertBench() {
  38. const SkScalar dx = SkIntToScalar(W) / COL;
  39. const SkScalar dy = SkIntToScalar(H) / COL;
  40. SkPoint* pts = fPts;
  41. uint16_t* idx = fIdx;
  42. SkScalar yy = 0;
  43. for (int y = 0; y <= ROW; y++) {
  44. SkScalar xx = 0;
  45. for (int x = 0; x <= COL; ++x) {
  46. pts->set(xx, yy);
  47. pts += 1;
  48. xx += dx;
  49. if (x < COL && y < ROW) {
  50. load_2_tris(idx, x, y, COL + 1);
  51. for (int i = 0; i < 6; i++) {
  52. SkASSERT(idx[i] < PTS);
  53. }
  54. idx += 6;
  55. }
  56. }
  57. yy += dy;
  58. }
  59. SkASSERT(PTS == pts - fPts);
  60. SkASSERT(IDX == idx - fIdx);
  61. SkRandom rand;
  62. for (int i = 0; i < PTS; ++i) {
  63. fColors[i] = rand.nextU() | (0xFF << 24);
  64. }
  65. fName.set("verts");
  66. }
  67. protected:
  68. const char* onGetName() override { return fName.c_str(); }
  69. void onDraw(int loops, SkCanvas* canvas) override {
  70. SkPaint paint;
  71. this->setupPaint(&paint);
  72. auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, PTS,
  73. fPts, nullptr, fColors, IDX, fIdx);
  74. for (int i = 0; i < loops; i++) {
  75. canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
  76. }
  77. }
  78. private:
  79. typedef Benchmark INHERITED;
  80. };
  81. DEF_BENCH(return new VertBench();)
  82. /////////////////////////////////////////////////////////////////////////////////////////////////
  83. #include "include/core/SkRSXform.h"
  84. #include "include/utils/SkRandom.h"
  85. #include "tools/Resources.h"
  86. enum AtlasFlags {
  87. kColors_Flag = 1 << 0,
  88. kVerts_Flag = 1 << 1,
  89. };
  90. class AtlasBench : public Benchmark {
  91. unsigned fFlags;
  92. SkString fName;
  93. enum {
  94. W = 640,
  95. H = 480,
  96. N = 10*1000,
  97. };
  98. sk_sp<SkImage> fAtlas;
  99. SkRSXform fXforms[N];
  100. SkRect fRects[N];
  101. SkColor fColors[N];
  102. public:
  103. AtlasBench(unsigned flags) : fFlags(flags) {
  104. fName.printf("drawAtlas_%d", flags);
  105. }
  106. ~AtlasBench() override {}
  107. protected:
  108. const char* onGetName() override { return fName.c_str(); }
  109. void onDelayedSetup() override {
  110. fAtlas = GetResourceAsImage("images/mandrill_256.png");
  111. if (fAtlas) {
  112. fAtlas = fAtlas->makeRasterImage();
  113. }
  114. const SkScalar imageW = fAtlas->width();
  115. const SkScalar imageH = fAtlas->height();
  116. SkRandom rand;
  117. for (int i = 0; i < N; ++i) {
  118. fRects[i] = SkRect::MakeXYWH(rand.nextF() * (imageW - 8),
  119. rand.nextF() * (imageH - 8), 8, 8);
  120. fColors[i] = rand.nextU();
  121. fXforms[i] = SkRSXform::Make(1, 0, rand.nextF() * W, rand.nextF() * H);
  122. }
  123. }
  124. void onDraw(int loops, SkCanvas* canvas) override {
  125. const SkRect* cullRect = nullptr;
  126. const SkPaint* paintPtr = nullptr;
  127. const SkColor* colors = nullptr;
  128. const SkImage* atlas = nullptr;
  129. if (fFlags & kColors_Flag) {
  130. colors = fColors;
  131. }
  132. if (fFlags & kVerts_Flag) {
  133. atlas = fAtlas.get();
  134. }
  135. for (int i = 0; i < loops; i++) {
  136. canvas->drawAtlas(atlas, fXforms, fRects, colors, N, SkBlendMode::kSrcOver,
  137. cullRect, paintPtr);
  138. }
  139. }
  140. private:
  141. typedef Benchmark INHERITED;
  142. };
  143. //DEF_BENCH(return new AtlasBench(0);)
  144. //DEF_BENCH(return new AtlasBench(kColors_Flag);)
  145. DEF_BENCH(return new AtlasBench(kVerts_Flag);)
  146. DEF_BENCH(return new AtlasBench(kVerts_Flag | kColors_Flag);)