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; add(size_t n)139 void add(size_t n) { runningValues[lastValueIndex] += n; } next()140 void next() 141 { 142 lastValueIndex = (lastValueIndex + 1) % runningValues.size(); 143 runningValues[lastValueIndex] = 0; 144 } 145 146 protected: 147 std::vector<size_t> runningValues; 148 size_t lastValueIndex = 0; 149 Text description; 150 151 friend class gl::Overlay; 152 friend class gl::OverlayState; 153 friend class overlay_impl::AppendWidgetDataHelper; 154 }; 155 156 class RunningHistogram : public RunningGraph 157 { 158 public: RunningHistogram(size_t n)159 RunningHistogram(size_t n) : RunningGraph(n) {} ~RunningHistogram()160 ~RunningHistogram() override {} set(float n)161 void set(float n) 162 { 163 ASSERT(n >= 0.0f && n <= 1.0f); 164 size_t rank = 165 n == 1.0f ? runningValues.size() - 1 : static_cast<size_t>(n * runningValues.size()); 166 167 runningValues[lastValueIndex] = rank; 168 } 169 }; 170 171 // If overlay is disabled, all the above classes would be replaced with Dummy, turning them into 172 // noop. 173 class Dummy 174 { 175 public: reset()176 void reset() const {} 177 template <typename T> set(T)178 void set(T) const 179 {} 180 template <typename T> add(T)181 void add(T) const 182 {} next()183 void next() const {} 184 }; 185 186 } // namespace overlay 187 188 } // namespace gl 189 190 #endif // LIBANGLE_OVERLAYWIDGETS_H_ 191