Flutter Engine
The Flutter Engine
RectanizerSlide.cpp
Go to the documentation of this file.
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
12#include "src/base/SkRandom.h"
13#include "src/base/SkUTF.h"
14#include "tools/viewer/Slide.h"
15#if defined(SK_GANESH) || defined(SK_GRAPHITE)
18
19using namespace skia_private;
20using namespace skgpu;
21
22// This slide visualizes the various Rectanizer-derived classes behavior
23// for various input sets
24// 'j' will cycle through the various rectanizers
25// Pow2 -> RectanizerPow2
26// Skyline -> RectanizerSkyline
27// 'h' will cycle through the various rect sets
28// Rand -> random rects from 2-256
29// Pow2Rand -> random power of 2 sized rects from 2-256
30// SmallPow2 -> 128x128 rects
31class RectanizerSlide : public Slide {
32public:
33 RectanizerSlide()
34 : fCurRandRect(0)
35 , fCurRectanizer(0) {
36 for (int i = 0; i < 3; ++i) {
37 fRects[i].reserve(kNumRandRects);
38 }
39 fRectLocations.reserve(kNumRandRects);
40
41 SkRandom random;
42 for (int i = 0; i < kNumRandRects; ++i) {
43 *fRects[0].append() = SkISize::Make(random.nextRangeU(kMinRectSize, kMaxRectSize),
44 random.nextRangeU(kMinRectSize, kMaxRectSize));
45 *fRects[1].append() = SkISize::Make(
46 GrNextPow2(random.nextRangeU(kMinRectSize, kMaxRectSize)),
47 GrNextPow2(random.nextRangeU(kMinRectSize, kMaxRectSize)));
48 *fRects[2].append() = SkISize::Make(128, 128);
49 *fRectLocations.append() = SkIPoint16::Make(0, 0);
50 }
51
52 fCurRects = &fRects[0];
53
54 fRectanizers.push_back(
55 std::unique_ptr<Rectanizer>(new RectanizerPow2(kWidth, kHeight)));
56 fRectanizers.push_back(
57 std::unique_ptr<Rectanizer>(new RectanizerSkyline(kWidth, kHeight)));
58 fName = "Rectanizer";
59 }
60
61 bool onChar(SkUnichar uni) override {
63 size_t size = SkUTF::ToUTF8(uni, utf8);
64 // Only consider events for single char keys
65 if (1 == size) {
66 switch (utf8[0]) {
67 case kCycleRectanizerKey:
68 this->cycleRectanizer();
69 return true;
70 case kCycleRectsKey:
71 this->cycleRects();
72 return true;
73 default:
74 break;
75 }
76 }
77 return false;
78 }
79
80 void draw(SkCanvas* canvas) override {
81 if (fCurRandRect < kNumRandRects) {
82 if (fRectanizers[fCurRectanizer]->addRect((*fCurRects)[fCurRandRect].fWidth,
83 (*fCurRects)[fCurRandRect].fHeight,
84 &fRectLocations[fCurRandRect])) {
85 ++fCurRandRect;
86 }
87 }
88
89 SkFont blackBigFont;
90 blackBigFont.setSize(20);
91 SkPaint blackStroke;
93 SkPaint redFill;
94 redFill.setColor(SK_ColorRED);
95
97
98 canvas->clear(SK_ColorWHITE);
99 canvas->drawRect(r, blackStroke);
100
101 long totArea = 0;
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;
110 }
111
112 SkString str;
113
114 str.printf("%s-%s: tot Area: %ld %%full: %.2f (%.2f) numTextures: %d/%d",
115 this->getRectanizerName(),
116 this->getRectsName(),
117 totArea,
118 100.0f * fRectanizers[fCurRectanizer]->percentFull(),
119 100.0f * totArea / ((float)kWidth*kHeight),
120 fCurRandRect,
121 kNumRandRects);
122 canvas->drawString(str, 50, kHeight + 50, blackBigFont, SkPaint());
123
124 str.printf("Press \'j\' to toggle rectanizer");
125 canvas->drawString(str, 50, kHeight + 100, blackBigFont, SkPaint());
126
127 str.printf("Press \'h\' to toggle rects");
128 canvas->drawString(str, 50, kHeight + 150, blackBigFont, SkPaint());
129 }
130
131private:
132 static const int kWidth = 1024;
133 static const int kHeight = 1024;
134 static const int kNumRandRects = 200;
135 static const char kCycleRectanizerKey = 'j';
136 static const char kCycleRectsKey = 'h';
137 static const int kMinRectSize = 2;
138 static const int kMaxRectSize = 256;
139
140 int fCurRandRect;
141 SkTDArray<SkISize> fRects[3];
142 SkTDArray<SkISize>* fCurRects;
143 SkTDArray<SkIPoint16> fRectLocations;
145 int fCurRectanizer;
146
147 const char* getRectanizerName() const {
148 if (!fCurRectanizer) {
149 return "Pow2";
150 } else {
151 return "Skyline";
152 }
153 }
154
155 void cycleRectanizer() {
156 fCurRectanizer = (fCurRectanizer + 1) % fRectanizers.size();
157
158 fRectanizers[fCurRectanizer]->reset();
159 fCurRandRect = 0;
160 }
161
162 const char* getRectsName() const {
163 if (fCurRects == &fRects[0]) {
164 return "Rand";
165 } else if (fCurRects == &fRects[1]) {
166 return "Pow2Rand";
167 } else {
168 return "SmallPow2";
169 }
170 }
171
172 void cycleRects() {
173 if (fCurRects == &fRects[0]) {
174 fCurRects = &fRects[1];
175 } else if (fCurRects == &fRects[1]) {
176 fCurRects = &fRects[2];
177 } else {
178 fCurRects = &fRects[0];
179 }
180
181 fRectanizers[fCurRectanizer]->reset();
182 fCurRandRect = 0;
183 }
184};
185
186//////////////////////////////////////////////////////////////////////////////
187
188DEF_SLIDE( return new RectanizerSlide(); )
189
190#endif
const char * fName
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
static uint32_t GrNextPow2(uint32_t n)
Definition: SkMathPriv.h:302
#define SkIntToScalar(x)
Definition: SkScalar.h:57
int32_t SkUnichar
Definition: SkTypes.h:175
#define DEF_SLIDE(code)
Definition: Slide.h:25
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
Definition: SkFont.h:35
void setSize(SkScalar textSize)
Definition: SkFont.cpp:129
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition: SkRandom.h:80
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
Definition: Slide.h:29
virtual bool onChar(SkUnichar c)
Definition: Slide.h:44
virtual void draw(SkCanvas *canvas)=0
void reset(int n)
Definition: SkTArray.h:144
int size() const
Definition: SkTArray.h:421
SK_SPI size_t ToUTF8(SkUnichar uni, char utf8[kMaxBytesInUTF8Sequence]=nullptr)
constexpr unsigned kMaxBytesInUTF8Sequence
Definition: SkUTF.h:59
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
Definition: GpuTools.h:21
static constexpr SkIPoint16 Make(int x, int y)
Definition: SkIPoint16.h:29
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609