robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkFont.h" |
| 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/utils/SkRandom.h" |
| 12 | #include "samplecode/Sample.h" |
| 13 | #include "src/utils/SkUTF.h" |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 15 | #include "src/gpu/GrRectanizerPow2.h" |
Herb Derby | 73c7587 | 2020-01-22 18:09:16 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrRectanizerSkyline.h" |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 17 | |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 18 | // This slide visualizes the various GrRectanizer-derived classes behavior |
| 19 | // for various input sets |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 20 | // 'j' will cycle through the various rectanizers |
| 21 | // Pow2 -> GrRectanizerPow2 |
| 22 | // Skyline -> GrRectanizerSkyline |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 23 | // 'h' will cycle through the various rect sets |
| 24 | // Rand -> random rects from 2-256 |
| 25 | // Pow2Rand -> random power of 2 sized rects from 2-256 |
| 26 | // SmallPow2 -> 128x128 rects |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 27 | class RectanizerView : public Sample { |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 28 | public: |
| 29 | RectanizerView() |
Florin Malita | e5ae84b | 2017-12-24 11:45:38 -0500 | [diff] [blame] | 30 | : fCurRandRect(0) |
| 31 | , fCurRectanizer(0) { |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 32 | for (int i = 0; i < 3; ++i) { |
| 33 | fRects[i].setReserve(kNumRandRects); |
| 34 | } |
| 35 | fRectLocations.setReserve(kNumRandRects); |
| 36 | |
| 37 | SkRandom random; |
| 38 | for (int i = 0; i < kNumRandRects; ++i) { |
| 39 | *fRects[0].append() = SkISize::Make(random.nextRangeU(kMinRectSize, kMaxRectSize), |
| 40 | random.nextRangeU(kMinRectSize, kMaxRectSize)); |
| 41 | *fRects[1].append() = SkISize::Make( |
| 42 | GrNextPow2(random.nextRangeU(kMinRectSize, kMaxRectSize)), |
| 43 | GrNextPow2(random.nextRangeU(kMinRectSize, kMaxRectSize))); |
| 44 | *fRects[2].append() = SkISize::Make(128, 128); |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 45 | *fRectLocations.append() = SkIPoint16::Make(0, 0); |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | fCurRects = &fRects[0]; |
| 49 | |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 50 | fRectanizers.push_back( |
| 51 | std::unique_ptr<GrRectanizer>(new GrRectanizerPow2(kWidth, kHeight))); |
| 52 | fRectanizers.push_back( |
| 53 | std::unique_ptr<GrRectanizer>(new GrRectanizerSkyline(kWidth, kHeight))); |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 57 | SkString name() override { return SkString("Rectanizer"); } |
| 58 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 59 | bool onChar(SkUnichar uni) override { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 60 | char utf8[SkUTF::kMaxBytesInUTF8Sequence]; |
| 61 | size_t size = SkUTF::ToUTF8(uni, utf8); |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 62 | // Only consider events for single char keys |
| 63 | if (1 == size) { |
| 64 | switch (utf8[0]) { |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 65 | case kCycleRectanizerKey: |
| 66 | this->cycleRectanizer(); |
| 67 | return true; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 68 | case kCycleRectsKey: |
| 69 | this->cycleRects(); |
| 70 | return true; |
| 71 | default: |
| 72 | break; |
| 73 | } |
| 74 | } |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 75 | return false; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 76 | } |
| 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | void onDrawContent(SkCanvas* canvas) override { |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 79 | if (fCurRandRect < kNumRandRects) { |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 80 | if (fRectanizers[fCurRectanizer]->addRect((*fCurRects)[fCurRandRect].fWidth, |
| 81 | (*fCurRects)[fCurRandRect].fHeight, |
| 82 | &fRectLocations[fCurRandRect])) { |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 83 | ++fCurRandRect; |
| 84 | } |
| 85 | } |
| 86 | |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 87 | SkFont blackBigFont; |
| 88 | blackBigFont.setSize(20); |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 89 | SkPaint blackStroke; |
| 90 | blackStroke.setStyle(SkPaint::kStroke_Style); |
| 91 | SkPaint redFill; |
| 92 | redFill.setColor(SK_ColorRED); |
| 93 | |
| 94 | SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 95 | |
| 96 | canvas->clear(SK_ColorWHITE); |
| 97 | canvas->drawRect(r, blackStroke); |
| 98 | |
| 99 | long totArea = 0; |
| 100 | for (int i = 0; i < fCurRandRect; ++i) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 101 | r = SkRect::MakeXYWH(SkIntToScalar(fRectLocations[i].fX), |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 102 | SkIntToScalar(fRectLocations[i].fY), |
| 103 | SkIntToScalar((*fCurRects)[i].fWidth), |
| 104 | SkIntToScalar((*fCurRects)[i].fHeight)); |
| 105 | canvas->drawRect(r, redFill); |
| 106 | canvas->drawRect(r, blackStroke); |
| 107 | totArea += (*fCurRects)[i].fWidth * (*fCurRects)[i].fHeight; |
| 108 | } |
| 109 | |
| 110 | SkString str; |
| 111 | |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 112 | str.printf("%s-%s: tot Area: %ld %%full: %.2f (%.2f) numTextures: %d/%d", |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 113 | this->getRectanizerName(), |
| 114 | this->getRectsName(), |
| 115 | totArea, |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 116 | 100.0f * fRectanizers[fCurRectanizer]->percentFull(), |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 117 | 100.0f * totArea / ((float)kWidth*kHeight), |
| 118 | fCurRandRect, |
| 119 | kNumRandRects); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 120 | canvas->drawString(str, 50, kHeight + 50, blackBigFont, SkPaint()); |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 121 | |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 122 | str.printf("Press \'j\' to toggle rectanizer"); |
| 123 | canvas->drawString(str, 50, kHeight + 100, blackBigFont, SkPaint()); |
| 124 | |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 125 | str.printf("Press \'h\' to toggle rects"); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 126 | canvas->drawString(str, 50, kHeight + 150, blackBigFont, SkPaint()); |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private: |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 130 | static const int kWidth = 1024; |
| 131 | static const int kHeight = 1024; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 132 | static const int kNumRandRects = 200; |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 133 | static const char kCycleRectanizerKey = 'j'; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 134 | static const char kCycleRectsKey = 'h'; |
| 135 | static const int kMinRectSize = 2; |
| 136 | static const int kMaxRectSize = 256; |
| 137 | |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 138 | int fCurRandRect; |
| 139 | SkTDArray<SkISize> fRects[3]; |
| 140 | SkTDArray<SkISize>* fCurRects; |
| 141 | SkTDArray<SkIPoint16> fRectLocations; |
| 142 | SkTArray<std::unique_ptr<GrRectanizer>> fRectanizers; |
| 143 | int fCurRectanizer; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 144 | |
| 145 | const char* getRectanizerName() const { |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 146 | if (!fCurRectanizer) { |
| 147 | return "Pow2"; |
| 148 | } else { |
| 149 | return "Skyline"; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void cycleRectanizer() { |
| 154 | fCurRectanizer = (fCurRectanizer + 1) % fRectanizers.count(); |
| 155 | |
| 156 | fRectanizers[fCurRectanizer]->reset(); |
| 157 | fCurRandRect = 0; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | const char* getRectsName() const { |
| 161 | if (fCurRects == &fRects[0]) { |
| 162 | return "Rand"; |
| 163 | } else if (fCurRects == &fRects[1]) { |
| 164 | return "Pow2Rand"; |
| 165 | } else { |
| 166 | return "SmallPow2"; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void cycleRects() { |
| 171 | if (fCurRects == &fRects[0]) { |
| 172 | fCurRects = &fRects[1]; |
| 173 | } else if (fCurRects == &fRects[1]) { |
| 174 | fCurRects = &fRects[2]; |
| 175 | } else { |
| 176 | fCurRects = &fRects[0]; |
| 177 | } |
| 178 | |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 179 | fRectanizers[fCurRectanizer]->reset(); |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 180 | fCurRandRect = 0; |
| 181 | } |
| 182 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 183 | using INHERITED = Sample; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | ////////////////////////////////////////////////////////////////////////////// |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 187 | |
| 188 | DEF_SAMPLE( return new RectanizerView(); ) |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame] | 189 | |
| 190 | #endif |