skinning.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2018 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 "gm/gm.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRefCnt.h"
  14. #include "include/core/SkSize.h"
  15. #include "include/core/SkString.h"
  16. #include "include/core/SkVertices.h"
  17. #include <stdint.h>
  18. using namespace skiagm;
  19. static const int kCellSize = 60;
  20. static const int kColumnSize = 36;
  21. static const int kBoneCount = 7;
  22. static const SkVertices::Bone kBones[] = {
  23. {{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }}, // SkMatrix::I()
  24. {{ 1.0f, 0.0f, 0.0f, 1.0f, 10.0f, 0.0f }}, // SkMatrix::MakeTrans(10, 0)
  25. {{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 10.0f }}, // SkMatrix::MakeTrans(0, 10)
  26. {{ 1.0f, 0.0f, 0.0f, 1.0f, -10.0f, 0.0f }}, // SkMatrix::MakeTrans(-10, 0)
  27. {{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, -10.0f }}, // SkMatrix::MakeTrans(0, -10)
  28. {{ 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f }}, // SkMatrix::MakeScale(0.5)
  29. {{ 1.5f, 0.0f, 0.0f, 1.5f, 0.0f, 0.0f }}, // SkMatrix::MakeScale(1.5)
  30. };
  31. static const int kVertexCount = 4;
  32. static const SkPoint kPositions[] = {
  33. { 0, 0 },
  34. { 0, 30 },
  35. { 30, 30 },
  36. { 30, 0 },
  37. };
  38. static const SkColor kColors[] = {
  39. 0xFFFF0000,
  40. 0xFF00FF00,
  41. 0xFF0000FF,
  42. 0xFFFFFF00,
  43. };
  44. static const SkVertices::BoneIndices kBoneIndices[] = {
  45. {{ 1, 0, 0, 0 }},
  46. {{ 2, 1, 0, 0 }},
  47. {{ 3, 2, 1, 0 }},
  48. {{ 4, 3, 2, 1 }},
  49. };
  50. static const SkVertices::BoneWeights kBoneWeights[] = {
  51. {{ 1.0f, 0.0f, 0.0f, 0.0f }},
  52. {{ 0.5f, 0.5f, 0.0f, 0.0f }},
  53. {{ 0.34f, 0.33f, 0.33f, 0.0f }},
  54. {{ 0.25f, 0.25f, 0.25f, 0.25f }},
  55. };
  56. static const int kIndexCount = 6;
  57. static const uint16_t kIndices[] = {
  58. 0, 1, 2,
  59. 2, 3, 0,
  60. };
  61. // Swap two SkVertices::Bone pointers in place.
  62. static void swap(const SkVertices::Bone** x, const SkVertices::Bone** y) {
  63. const SkVertices::Bone* temp = *x;
  64. *x = *y;
  65. *y = temp;
  66. }
  67. class SkinningGM : public GM {
  68. public:
  69. SkinningGM(bool deformUsingCPU, bool cache)
  70. : fPaint()
  71. , fVertices(nullptr)
  72. , fDeformUsingCPU(deformUsingCPU)
  73. , fCache(cache)
  74. {}
  75. protected:
  76. bool runAsBench() const override {
  77. return true;
  78. }
  79. SkString onShortName() override {
  80. SkString name("skinning");
  81. if (fDeformUsingCPU) {
  82. name.append("_cpu");
  83. }
  84. if (fCache) {
  85. name.append("_cached");
  86. }
  87. return name;
  88. }
  89. SkISize onISize() override {
  90. return SkISize::Make(2400, 2400);
  91. }
  92. void onOnceBeforeDraw() override {
  93. fVertices = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
  94. kVertexCount,
  95. kPositions,
  96. nullptr,
  97. kColors,
  98. kBoneIndices,
  99. kBoneWeights,
  100. kIndexCount,
  101. kIndices,
  102. !fCache);
  103. }
  104. void onDraw(SkCanvas* canvas) override {
  105. // Set the initial position.
  106. int xpos = kCellSize;
  107. int ypos = kCellSize;
  108. // Create the mutable set of bones.
  109. const SkVertices::Bone* bones[kBoneCount];
  110. for (int i = 0; i < kBoneCount; i ++) {
  111. bones[i] = &kBones[i];
  112. }
  113. // Draw the vertices.
  114. drawPermutations(canvas, xpos, ypos, bones, 1);
  115. }
  116. private:
  117. void drawPermutations(SkCanvas* canvas,
  118. int& xpos,
  119. int& ypos,
  120. const SkVertices::Bone** bones,
  121. int start) {
  122. if (start == kBoneCount) {
  123. // Reached the end of the permutations, so draw.
  124. canvas->save();
  125. // Copy the bones.
  126. SkVertices::Bone copiedBones[kBoneCount];
  127. for (int i = 0; i < kBoneCount; i ++) {
  128. copiedBones[i] = *bones[i];
  129. }
  130. // Set the position.
  131. canvas->translate(xpos, ypos);
  132. // Draw the vertices.
  133. if (fDeformUsingCPU) {
  134. // Apply the bones.
  135. sk_sp<SkVertices> vertices = fVertices->applyBones(copiedBones,
  136. kBoneCount);
  137. // Deform with CPU.
  138. canvas->drawVertices(vertices.get(),
  139. SkBlendMode::kSrc,
  140. fPaint);
  141. } else {
  142. // Deform with GPU.
  143. canvas->drawVertices(fVertices.get(),
  144. copiedBones,
  145. kBoneCount,
  146. SkBlendMode::kSrc,
  147. fPaint);
  148. }
  149. canvas->restore();
  150. // Get a new position to draw the vertices.
  151. xpos += kCellSize;
  152. if (xpos > kCellSize * kColumnSize) {
  153. xpos = kCellSize;
  154. ypos += kCellSize;
  155. }
  156. return;
  157. }
  158. // Find all possible permutations within the given range.
  159. for (int i = start; i < kBoneCount; i ++) {
  160. // Swap the start and i-th elements.
  161. swap(bones + start, bones + i);
  162. // Find permutations of the sub array.
  163. drawPermutations(canvas, xpos, ypos, bones, start + 1);
  164. // Swap the elements back.
  165. swap(bones + i, bones + start);
  166. }
  167. }
  168. private:
  169. SkPaint fPaint;
  170. sk_sp<SkVertices> fVertices;
  171. bool fDeformUsingCPU;
  172. bool fCache;
  173. typedef GM INHERITED;
  174. };
  175. /////////////////////////////////////////////////////////////////////////////////////
  176. DEF_GM(return new SkinningGM(true, true);)
  177. DEF_GM(return new SkinningGM(false, true);)
  178. DEF_GM(return new SkinningGM(true, false);)
  179. DEF_GM(return new SkinningGM(false, false);)