SampleRectanizer.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "include/core/SkCanvas.h"
  8. #include "include/core/SkFont.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/utils/SkRandom.h"
  11. #include "samplecode/Sample.h"
  12. #include "src/utils/SkUTF.h"
  13. #if SK_SUPPORT_GPU
  14. #include "src/gpu/GrRectanizer_pow2.h"
  15. #include "src/gpu/GrRectanizer_skyline.h"
  16. // This slide visualizes the various GrRectanizer-derived classes behavior
  17. // for various input sets
  18. // 'j' will cycle through the various rectanizers
  19. // Pow2 -> GrRectanizerPow2
  20. // Skyline -> GrRectanizerSkyline
  21. // 'h' will cycle through the various rect sets
  22. // Rand -> random rects from 2-256
  23. // Pow2Rand -> random power of 2 sized rects from 2-256
  24. // SmallPow2 -> 128x128 rects
  25. class RectanizerView : public Sample {
  26. public:
  27. RectanizerView()
  28. : fCurRandRect(0)
  29. , fCurRectanizer(0) {
  30. for (int i = 0; i < 3; ++i) {
  31. fRects[i].setReserve(kNumRandRects);
  32. }
  33. fRectLocations.setReserve(kNumRandRects);
  34. SkRandom random;
  35. for (int i = 0; i < kNumRandRects; ++i) {
  36. *fRects[0].append() = SkISize::Make(random.nextRangeU(kMinRectSize, kMaxRectSize),
  37. random.nextRangeU(kMinRectSize, kMaxRectSize));
  38. *fRects[1].append() = SkISize::Make(
  39. GrNextPow2(random.nextRangeU(kMinRectSize, kMaxRectSize)),
  40. GrNextPow2(random.nextRangeU(kMinRectSize, kMaxRectSize)));
  41. *fRects[2].append() = SkISize::Make(128, 128);
  42. *fRectLocations.append() = SkIPoint16::Make(0, 0);
  43. }
  44. fCurRects = &fRects[0];
  45. fRectanizers.push_back(
  46. std::unique_ptr<GrRectanizer>(new GrRectanizerPow2(kWidth, kHeight)));
  47. fRectanizers.push_back(
  48. std::unique_ptr<GrRectanizer>(new GrRectanizerSkyline(kWidth, kHeight)));
  49. }
  50. protected:
  51. SkString name() override { return SkString("Rectanizer"); }
  52. bool onChar(SkUnichar uni) override {
  53. char utf8[SkUTF::kMaxBytesInUTF8Sequence];
  54. size_t size = SkUTF::ToUTF8(uni, utf8);
  55. // Only consider events for single char keys
  56. if (1 == size) {
  57. switch (utf8[0]) {
  58. case kCycleRectanizerKey:
  59. this->cycleRectanizer();
  60. return true;
  61. case kCycleRectsKey:
  62. this->cycleRects();
  63. return true;
  64. default:
  65. break;
  66. }
  67. }
  68. return false;
  69. }
  70. void onDrawContent(SkCanvas* canvas) override {
  71. if (fCurRandRect < kNumRandRects) {
  72. if (fRectanizers[fCurRectanizer]->addRect((*fCurRects)[fCurRandRect].fWidth,
  73. (*fCurRects)[fCurRandRect].fHeight,
  74. &fRectLocations[fCurRandRect])) {
  75. ++fCurRandRect;
  76. }
  77. }
  78. SkFont blackBigFont;
  79. blackBigFont.setSize(20);
  80. SkPaint blackStroke;
  81. blackStroke.setStyle(SkPaint::kStroke_Style);
  82. SkPaint redFill;
  83. redFill.setColor(SK_ColorRED);
  84. SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
  85. canvas->clear(SK_ColorWHITE);
  86. canvas->drawRect(r, blackStroke);
  87. long totArea = 0;
  88. for (int i = 0; i < fCurRandRect; ++i) {
  89. r = SkRect::MakeXYWH(SkIntToScalar(fRectLocations[i].fX),
  90. SkIntToScalar(fRectLocations[i].fY),
  91. SkIntToScalar((*fCurRects)[i].fWidth),
  92. SkIntToScalar((*fCurRects)[i].fHeight));
  93. canvas->drawRect(r, redFill);
  94. canvas->drawRect(r, blackStroke);
  95. totArea += (*fCurRects)[i].fWidth * (*fCurRects)[i].fHeight;
  96. }
  97. SkString str;
  98. str.printf("%s-%s: tot Area: %ld %%full: %.2f (%.2f) numTextures: %d/%d",
  99. this->getRectanizerName(),
  100. this->getRectsName(),
  101. totArea,
  102. 100.0f * fRectanizers[fCurRectanizer]->percentFull(),
  103. 100.0f * totArea / ((float)kWidth*kHeight),
  104. fCurRandRect,
  105. kNumRandRects);
  106. canvas->drawString(str, 50, kHeight + 50, blackBigFont, SkPaint());
  107. str.printf("Press \'j\' to toggle rectanizer");
  108. canvas->drawString(str, 50, kHeight + 100, blackBigFont, SkPaint());
  109. str.printf("Press \'h\' to toggle rects");
  110. canvas->drawString(str, 50, kHeight + 150, blackBigFont, SkPaint());
  111. }
  112. private:
  113. static const int kWidth = 1024;
  114. static const int kHeight = 1024;
  115. static const int kNumRandRects = 200;
  116. static const char kCycleRectanizerKey = 'j';
  117. static const char kCycleRectsKey = 'h';
  118. static const int kMinRectSize = 2;
  119. static const int kMaxRectSize = 256;
  120. int fCurRandRect;
  121. SkTDArray<SkISize> fRects[3];
  122. SkTDArray<SkISize>* fCurRects;
  123. SkTDArray<SkIPoint16> fRectLocations;
  124. SkTArray<std::unique_ptr<GrRectanizer>> fRectanizers;
  125. int fCurRectanizer;
  126. const char* getRectanizerName() const {
  127. if (!fCurRectanizer) {
  128. return "Pow2";
  129. } else {
  130. return "Skyline";
  131. }
  132. }
  133. void cycleRectanizer() {
  134. fCurRectanizer = (fCurRectanizer + 1) % fRectanizers.count();
  135. fRectanizers[fCurRectanizer]->reset();
  136. fCurRandRect = 0;
  137. }
  138. const char* getRectsName() const {
  139. if (fCurRects == &fRects[0]) {
  140. return "Rand";
  141. } else if (fCurRects == &fRects[1]) {
  142. return "Pow2Rand";
  143. } else {
  144. return "SmallPow2";
  145. }
  146. }
  147. void cycleRects() {
  148. if (fCurRects == &fRects[0]) {
  149. fCurRects = &fRects[1];
  150. } else if (fCurRects == &fRects[1]) {
  151. fCurRects = &fRects[2];
  152. } else {
  153. fCurRects = &fRects[0];
  154. }
  155. fRectanizers[fCurRectanizer]->reset();
  156. fCurRandRect = 0;
  157. }
  158. typedef Sample INHERITED;
  159. };
  160. //////////////////////////////////////////////////////////////////////////////
  161. DEF_SAMPLE( return new RectanizerView(); )
  162. #endif