• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 namespace gl
16 {
17 class Overlay;
18 class OverlayState;
19 
20 namespace overlay_impl
21 {
22 class AppendWidgetDataHelper;
23 }
24 
25 enum class WidgetType
26 {
27     // Text types:
28 
29     // A total count of some event.
30     Count,
31     // A single line of ASCII text.  Retains content until changed.
32     Text,
33     // A per-second value.
34     PerSecond,
35 
36     // Graph types:
37 
38     // A graph of the last N values.
39     RunningGraph,
40     // A histogram of the last N values (values between 0 and 1).
41     RunningHistogram,
42 
43     InvalidEnum,
44     EnumCount = InvalidEnum,
45 };
46 
47 enum class WidgetId
48 {
49     // Front-end widgets:
50 
51     // Frames per second (PerSecond).
52     FPS,
53 
54     // Vulkan backend:
55 
56     // Last validation error (Text).
57     VulkanLastValidationMessage,
58     // Number of validation errors and warnings (Count).
59     VulkanValidationMessageCount,
60     // Number of RenderPasses in a frame (RunningGraph).
61     VulkanRenderPassCount,
62     // Secondary Command Buffer pool memory waste (RunningHistogram).
63     VulkanSecondaryCommandBufferPoolWaste,
64 
65     InvalidEnum,
66     EnumCount = InvalidEnum,
67 };
68 
69 namespace overlay
70 {
71 class Widget
72 {
73   public:
~Widget()74     virtual ~Widget() {}
75 
76   protected:
77     WidgetType type;
78     // Whether this item should be drawn.
79     bool enabled = false;
80 
81     // For text items, size of the font.  This is a value in [0, overlay::kFontCount) which
82     // determines the font size to use.
83     int fontSize;
84 
85     // The area covered by the item, predetermined by the overlay class.  Negative values
86     // indicate offset from the left/bottom of the image.
87     int32_t coords[4];
88     float color[4];
89 
90     friend class gl::Overlay;
91     friend class gl::OverlayState;
92     friend class overlay_impl::AppendWidgetDataHelper;
93 };
94 
95 class Count : public Widget
96 {
97   public:
~Count()98     ~Count() override {}
add(size_t n)99     void add(size_t n) { count += n; }
reset()100     void reset() { count = 0; }
101 
102   protected:
103     size_t count = 0;
104 
105     friend class gl::Overlay;
106     friend class overlay_impl::AppendWidgetDataHelper;
107 };
108 
109 class PerSecond : public Count
110 {
111   public:
~PerSecond()112     ~PerSecond() override {}
113 
114   protected:
115     size_t lastPerSecondCount = 0;
116 
117     friend class gl::Overlay;
118     friend class overlay_impl::AppendWidgetDataHelper;
119 };
120 
121 class Text : public Widget
122 {
123   public:
~Text()124     ~Text() override {}
set(std::string && str)125     void set(std::string &&str) { text = std::move(str); }
126 
127   protected:
128     std::string text;
129 
130     friend class overlay_impl::AppendWidgetDataHelper;
131 };
132 
133 class RunningGraph : public Widget
134 {
135   public:
136     // Out of line constructor to satisfy chromium-style.
137     RunningGraph(size_t n);
138     ~RunningGraph() override;
139 
add(size_t n)140     void add(size_t n)
141     {
142         if (!ignoreFirstValue)
143         {
144             runningValues[lastValueIndex] += n;
145         }
146     }
147 
next()148     void next()
149     {
150         if (ignoreFirstValue)
151         {
152             ignoreFirstValue = false;
153         }
154         else
155         {
156             lastValueIndex                = (lastValueIndex + 1) % runningValues.size();
157             runningValues[lastValueIndex] = 0;
158         }
159     }
160 
161   protected:
162     std::vector<size_t> runningValues;
163     size_t lastValueIndex = 0;
164     Text description;
165     bool ignoreFirstValue = true;
166 
167     friend class gl::Overlay;
168     friend class gl::OverlayState;
169     friend class overlay_impl::AppendWidgetDataHelper;
170 };
171 
172 class RunningHistogram : public RunningGraph
173 {
174   public:
RunningHistogram(size_t n)175     RunningHistogram(size_t n) : RunningGraph(n) {}
~RunningHistogram()176     ~RunningHistogram() override {}
set(float n)177     void set(float n)
178     {
179         ASSERT(n >= 0.0f && n <= 1.0f);
180         size_t rank =
181             n == 1.0f ? runningValues.size() - 1 : static_cast<size_t>(n * runningValues.size());
182 
183         runningValues[lastValueIndex] = rank;
184     }
185 };
186 
187 // If overlay is disabled, all the above classes would be replaced with Dummy, turning them into
188 // noop.
189 class Dummy
190 {
191   public:
reset()192     void reset() const {}
193     template <typename T>
set(T)194     void set(T) const
195     {}
196     template <typename T>
add(T)197     void add(T) const
198     {}
next()199     void next() const {}
200 };
201 
202 }  // namespace overlay
203 
204 }  // namespace gl
205 
206 #endif  // LIBANGLE_OVERLAYWIDGETS_H_
207