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