1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // OverlayWidgets.h:
7 //    Defines the Overlay* widget classes and corresponding enums.
8 //
9 
10 #ifndef LIBANGLE_OVERLAYWIDGETS_H_
11 #define LIBANGLE_OVERLAYWIDGETS_H_
12 
13 #include "common/angleutils.h"
14 #include "libANGLE/Overlay_autogen.h"
15 
16 namespace gl
17 {
18 class Overlay;
19 class OverlayState;
20 
21 namespace overlay_impl
22 {
23 class AppendWidgetDataHelper;
24 }  // namespace overlay_impl
25 
26 enum class WidgetType
27 {
28     // Text types:
29 
30     // A total count of some event.
31     Count,
32     // A single line of ASCII text.  Retains content until changed.
33     Text,
34     // A per-second value.
35     PerSecond,
36 
37     // Graph types:
38 
39     // A graph of the last N values.
40     RunningGraph,
41     // A histogram of the last N values (values between 0 and 1).
42     RunningHistogram,
43 
44     InvalidEnum,
45     EnumCount = InvalidEnum,
46 };
47 
48 namespace overlay
49 {
50 class Widget
51 {
52   public:
~Widget()53     virtual ~Widget() {}
54 
55   protected:
56     WidgetType type;
57     // Whether this item should be drawn.
58     bool enabled = false;
59 
60     // For text items, size of the font.  This is a value in [0, overlay::kFontCount) which
61     // determines the font size to use.
62     int fontSize;
63 
64     // The area covered by the item, predetermined by the overlay class.  Negative values
65     // indicate offset from the left/bottom of the image.
66     int32_t coords[4];
67     float color[4];
68 
69     friend class gl::Overlay;
70     friend class gl::OverlayState;
71     friend class overlay_impl::AppendWidgetDataHelper;
72 };
73 
74 class Count : public Widget
75 {
76   public:
~Count()77     ~Count() override {}
add(size_t n)78     void add(size_t n) { count += n; }
reset()79     void reset() { count = 0; }
80 
81   protected:
82     size_t count = 0;
83 
84     friend class gl::Overlay;
85     friend class overlay_impl::AppendWidgetDataHelper;
86 };
87 
88 class PerSecond : public Count
89 {
90   public:
~PerSecond()91     ~PerSecond() override {}
92 
93   protected:
94     size_t lastPerSecondCount = 0;
95 
96     friend class gl::Overlay;
97     friend class overlay_impl::AppendWidgetDataHelper;
98 };
99 
100 class Text : public Widget
101 {
102   public:
~Text()103     ~Text() override {}
set(std::string && str)104     void set(std::string &&str) { text = std::move(str); }
105 
106   protected:
107     std::string text;
108 
109     friend class overlay_impl::AppendWidgetDataHelper;
110 };
111 
112 class RunningGraph : public Widget
113 {
114   public:
115     // Out of line constructor to satisfy chromium-style.
116     RunningGraph(size_t n);
117     ~RunningGraph() override;
118 
add(size_t n)119     void add(size_t n)
120     {
121         if (!ignoreFirstValue)
122         {
123             runningValues[lastValueIndex] += n;
124         }
125     }
126 
next()127     void next()
128     {
129         if (ignoreFirstValue)
130         {
131             ignoreFirstValue = false;
132         }
133         else
134         {
135             lastValueIndex                = (lastValueIndex + 1) % runningValues.size();
136             runningValues[lastValueIndex] = 0;
137         }
138     }
139 
140   protected:
141     std::vector<size_t> runningValues;
142     size_t lastValueIndex = 0;
143     Text description;
144     bool ignoreFirstValue = true;
145 
146     friend class gl::Overlay;
147     friend class gl::OverlayState;
148     friend class overlay_impl::AppendWidgetDataHelper;
149 };
150 
151 class RunningHistogram : public RunningGraph
152 {
153   public:
RunningHistogram(size_t n)154     RunningHistogram(size_t n) : RunningGraph(n) {}
~RunningHistogram()155     ~RunningHistogram() override {}
set(float n)156     void set(float n)
157     {
158         ASSERT(n >= 0.0f && n <= 1.0f);
159         size_t rank =
160             n == 1.0f ? runningValues.size() - 1 : static_cast<size_t>(n * runningValues.size());
161 
162         runningValues[lastValueIndex] = rank;
163     }
164 };
165 
166 // If overlay is disabled, all the above classes would be replaced with Mock, turning them into
167 // noop.
168 class Mock
169 {
170   public:
reset()171     void reset() const {}
172     template <typename T>
set(T)173     void set(T) const
174     {}
175     template <typename T>
add(T)176     void add(T) const
177     {}
next()178     void next() const {}
179 };
180 
181 }  // namespace overlay
182 
183 }  // namespace gl
184 
185 #endif  // LIBANGLE_OVERLAYWIDGETS_H_
186