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 // Overlay.cpp:
7 // Implements the Overlay class.
8 //
9
10 #include "libANGLE/Overlay.h"
11
12 #include "common/string_utils.h"
13 #include "common/system_utils.h"
14 #include "libANGLE/Context.h"
15 #include "libANGLE/Overlay_font_autogen.h"
16 #include "libANGLE/renderer/GLImplFactory.h"
17 #include "libANGLE/renderer/OverlayImpl.h"
18
19 #include <numeric>
20
21 namespace gl
22 {
23 namespace
24 {
25 constexpr std::pair<const char *, WidgetId> kWidgetNames[] = {
26 {"FPS", WidgetId::FPS},
27 {"VulkanLastValidationMessage", WidgetId::VulkanLastValidationMessage},
28 {"VulkanValidationMessageCount", WidgetId::VulkanValidationMessageCount},
29 {"VulkanRenderPassCount", WidgetId::VulkanRenderPassCount},
30 {"VulkanSecondaryCommandBufferPoolWaste", WidgetId::VulkanSecondaryCommandBufferPoolWaste},
31 };
32 } // namespace
33
OverlayState()34 OverlayState::OverlayState() : mEnabledWidgetCount(0), mOverlayWidgets{} {}
35 OverlayState::~OverlayState() = default;
36
Overlay(rx::GLImplFactory * factory)37 Overlay::Overlay(rx::GLImplFactory *factory)
38 : mLastPerSecondUpdate(0), mImplementation(factory->createOverlay(mState))
39 {}
40 Overlay::~Overlay() = default;
41
init(const Context * context)42 angle::Result Overlay::init(const Context *context)
43 {
44 initOverlayWidgets();
45 mLastPerSecondUpdate = angle::GetCurrentTime();
46
47 ASSERT(std::all_of(
48 mState.mOverlayWidgets.begin(), mState.mOverlayWidgets.end(),
49 [](const std::unique_ptr<overlay::Widget> &widget) { return widget.get() != nullptr; }));
50
51 enableOverlayWidgetsFromEnvironment();
52
53 return mImplementation->init(context);
54 }
55
destroy(const gl::Context * context)56 void Overlay::destroy(const gl::Context *context)
57 {
58 ASSERT(mImplementation);
59 mImplementation->onDestroy(context);
60 }
61
enableOverlayWidgetsFromEnvironment()62 void Overlay::enableOverlayWidgetsFromEnvironment()
63 {
64 std::vector<std::string> enabledWidgets =
65 angle::GetStringsFromEnvironmentVar("ANGLE_OVERLAY", ":");
66
67 for (const std::pair<const char *, WidgetId> &widgetName : kWidgetNames)
68 {
69 if (std::find(enabledWidgets.begin(), enabledWidgets.end(), widgetName.first) !=
70 enabledWidgets.end())
71 {
72 mState.mOverlayWidgets[widgetName.second]->enabled = true;
73 ++mState.mEnabledWidgetCount;
74 }
75 }
76 }
77
onSwap() const78 void Overlay::onSwap() const
79 {
80 // Increment FPS counter.
81 getPerSecondWidget(WidgetId::FPS)->add(1);
82
83 // Update per second values every second.
84 double currentTime = angle::GetCurrentTime();
85 double timeDiff = currentTime - mLastPerSecondUpdate;
86 if (timeDiff >= 1.0)
87 {
88 for (const std::unique_ptr<overlay::Widget> &widget : mState.mOverlayWidgets)
89 {
90 if (widget->type == WidgetType::PerSecond)
91 {
92 overlay::PerSecond *perSecond =
93 reinterpret_cast<overlay::PerSecond *>(widget.get());
94 perSecond->lastPerSecondCount = static_cast<size_t>(perSecond->count / timeDiff);
95 perSecond->count = 0;
96 }
97 }
98 mLastPerSecondUpdate += 1.0;
99 }
100 }
101
DummyOverlay(rx::GLImplFactory * implFactory)102 DummyOverlay::DummyOverlay(rx::GLImplFactory *implFactory) {}
103 DummyOverlay::~DummyOverlay() = default;
104
105 } // namespace gl
106