2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SampleCode.h"
13 #include "GrRectanizer_pow2.h"
14 #include "GrRectanizer_skyline.h"
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
26 class RectanizerView : public SampleView {
30 for (int i = 0; i < 3; ++i) {
31 fRects[i].setReserve(kNumRandRects);
33 fRectLocations.setReserve(kNumRandRects);
36 for (int i = 0; i < kNumRandRects; ++i) {
37 *fRects[0].append() = SkISize::Make(random.nextRangeU(kMinRectSize, kMaxRectSize),
38 random.nextRangeU(kMinRectSize, kMaxRectSize));
39 *fRects[1].append() = SkISize::Make(
40 GrNextPow2(random.nextRangeU(kMinRectSize, kMaxRectSize)),
41 GrNextPow2(random.nextRangeU(kMinRectSize, kMaxRectSize)));
42 *fRects[2].append() = SkISize::Make(128, 128);
43 *fRectLocations.append() = SkIPoint16::Make(0, 0);
46 fCurRects = &fRects[0];
48 fRectanizers[0] = new GrRectanizerPow2(kWidth, kHeight);
49 fRectanizers[1] = new GrRectanizerSkyline(kWidth, kHeight);
50 fCurRectanizer = fRectanizers[0];
54 bool onQuery(SkEvent* evt) SK_OVERRIDE {
55 if (SampleCode::TitleQ(*evt)) {
56 SampleCode::TitleR(evt, "Rectanizer");
60 if (SampleCode::CharQ(*evt, &uni)) {
61 char utf8[kMaxBytesInUTF8Sequence];
62 size_t size = SkUTF8_FromUnichar(uni, utf8);
63 // Only consider events for single char keys
66 case kCycleRectanizerKey:
67 this->cycleRectanizer();
77 return this->INHERITED::onQuery(evt);
80 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
81 if (fCurRandRect < kNumRandRects) {
82 if (fCurRectanizer->addRect((*fCurRects)[fCurRandRect].fWidth,
83 (*fCurRects)[fCurRandRect].fHeight,
84 &fRectLocations[fCurRandRect])) {
90 blackBigFont.setTextSize(20);
92 blackStroke.setStyle(SkPaint::kStroke_Style);
94 redFill.setColor(SK_ColorRED);
96 SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
98 canvas->clear(SK_ColorWHITE);
99 canvas->drawRect(r, blackStroke);
102 for (int i = 0; i < fCurRandRect; ++i) {
103 r = SkRect::MakeXYWH(SkIntToScalar(fRectLocations[i].fX),
104 SkIntToScalar(fRectLocations[i].fY),
105 SkIntToScalar((*fCurRects)[i].fWidth),
106 SkIntToScalar((*fCurRects)[i].fHeight));
107 canvas->drawRect(r, redFill);
108 canvas->drawRect(r, blackStroke);
109 totArea += (*fCurRects)[i].fWidth * (*fCurRects)[i].fHeight;
114 str.printf("%s-%s: tot Area: %ld %%full: %.2f (%.2f) numTextures: %d/%d",
115 this->getRectanizerName(),
116 this->getRectsName(),
118 100.0f * fCurRectanizer->percentFull(),
119 100.0f * totArea / ((float)kWidth*kHeight),
122 canvas->drawText(str.c_str(), str.size(), 50, kHeight + 50, blackBigFont);
124 str.printf("Press \'j\' to toggle rectanizer");
125 canvas->drawText(str.c_str(), str.size(), 50, kHeight + 100, blackBigFont);
127 str.printf("Press \'h\' to toggle rects");
128 canvas->drawText(str.c_str(), str.size(), 50, kHeight + 150, blackBigFont);
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;
143 SkTDArray<SkISize> fRects[3];
144 SkTDArray<SkISize>* fCurRects;
145 SkTDArray<SkIPoint16> fRectLocations;
146 GrRectanizer* fRectanizers[2];
147 GrRectanizer* fCurRectanizer;
149 const char* getRectanizerName() const {
150 if (fCurRectanizer == fRectanizers[0]) {
157 void cycleRectanizer() {
158 if (fCurRectanizer == fRectanizers[0]) {
159 fCurRectanizer = fRectanizers[1];
161 fCurRectanizer = fRectanizers[0];
164 fCurRectanizer->reset();
168 const char* getRectsName() const {
169 if (fCurRects == &fRects[0]) {
171 } else if (fCurRects == &fRects[1]) {
179 if (fCurRects == &fRects[0]) {
180 fCurRects = &fRects[1];
181 } else if (fCurRects == &fRects[1]) {
182 fCurRects = &fRects[2];
184 fCurRects = &fRects[0];
187 fCurRectanizer->reset();
191 typedef SampleView INHERITED;
194 //////////////////////////////////////////////////////////////////////////////
195 static SkView* MyFactory() { return new RectanizerView; }
196 static SkViewRegister reg(MyFactory);