xref: /aosp_15_r20/external/angle/src/libANGLE/Overlay.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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.h:
7 //    Defines the Overlay class that handles overlay widgets.
8 //
9 
10 #ifndef LIBANGLE_OVERLAY_H_
11 #define LIBANGLE_OVERLAY_H_
12 
13 #include "common/PackedEnums.h"
14 #include "common/angleutils.h"
15 #include "libANGLE/Error.h"
16 #include "libANGLE/OverlayWidgets.h"
17 #include "libANGLE/angletypes.h"
18 
19 namespace rx
20 {
21 class OverlayImpl;
22 class GLImplFactory;
23 }  // namespace rx
24 
25 namespace gl
26 {
27 class Context;
28 
29 class OverlayState : angle::NonCopyable
30 {
31   public:
32     OverlayState();
33     ~OverlayState();
34 
35     size_t getWidgetCoordinatesBufferSize() const;
36     size_t getTextWidgetsBufferSize() const;
37     size_t getGraphWidgetsBufferSize() const;
38 
39     const uint8_t *getFontData() const;
40     void fillWidgetData(const gl::Extents &imageExtents,
41                         uint8_t *textData,
42                         uint8_t *graphData,
43                         uint32_t *activeTextWidgetCountOut,
44                         uint32_t *activeGraphWidgetCountOut) const;
45 
getEnabledWidgetCount()46     uint32_t getEnabledWidgetCount() const { return mEnabledWidgetCount; }
47 
48   private:
49     friend class Overlay;
50 
51     uint32_t mEnabledWidgetCount;
52 
53     angle::PackedEnumMap<WidgetId, std::unique_ptr<overlay::Widget>> mOverlayWidgets;
54 };
55 
56 class Overlay : angle::NonCopyable
57 {
58   public:
59     Overlay(rx::GLImplFactory *implFactory);
60     ~Overlay();
61 
62     void init();
63     void destroy(const gl::Context *context);
64 
65     void onSwap() const;
66 
getTextWidget(WidgetId id)67     overlay::Text *getTextWidget(WidgetId id) const
68     {
69         return getWidgetAs<overlay::Text, WidgetType::Text>(id);
70     }
getCountWidget(WidgetId id)71     overlay::Count *getCountWidget(WidgetId id) const
72     {
73         return getWidgetAs<overlay::Count, WidgetType::Count>(id);
74     }
getPerSecondWidget(WidgetId id)75     overlay::PerSecond *getPerSecondWidget(WidgetId id) const
76     {
77         return getWidgetAs<overlay::PerSecond, WidgetType::PerSecond>(id);
78     }
getRunningGraphWidget(WidgetId id)79     overlay::RunningGraph *getRunningGraphWidget(WidgetId id) const
80     {
81         return getWidgetAs<overlay::RunningGraph, WidgetType::RunningGraph>(id);
82     }
getRunningHistogramWidget(WidgetId id)83     overlay::RunningHistogram *getRunningHistogramWidget(WidgetId id) const
84     {
85         return getWidgetAs<overlay::RunningHistogram, WidgetType::RunningHistogram>(id);
86     }
87 
getImplementation()88     rx::OverlayImpl *getImplementation() const { return mImplementation.get(); }
89 
isEnabled()90     bool isEnabled() const
91     {
92         return mImplementation != nullptr && mState.getEnabledWidgetCount() > 0;
93     }
94 
95   private:
96     template <typename Widget, WidgetType Type>
getWidgetAs(WidgetId id)97     Widget *getWidgetAs(WidgetId id) const
98     {
99         ASSERT(mState.mOverlayWidgets[id] != nullptr);
100         ASSERT(mState.mOverlayWidgets[id]->type == Type);
101         return rx::GetAs<Widget>(mState.mOverlayWidgets[id].get());
102     }
103     void initOverlayWidgets();
104     void enableOverlayWidgetsFromEnvironment();
105 
106     // Time tracking for PerSecond items.
107     mutable double mLastPerSecondUpdate;
108 
109     OverlayState mState;
110     std::unique_ptr<rx::OverlayImpl> mImplementation;
111 };
112 
113 class MockOverlay
114 {
115   public:
116     MockOverlay(rx::GLImplFactory *implFactory);
117     ~MockOverlay();
118 
init()119     void init() {}
destroy(const Context * context)120     void destroy(const Context *context) {}
121 
onSwap()122     void onSwap() const {}
123 
getTextWidget(WidgetId id)124     const overlay::Mock *getTextWidget(WidgetId id) const { return &mMock; }
getCountWidget(WidgetId id)125     const overlay::Mock *getCountWidget(WidgetId id) const { return &mMock; }
getPerSecondWidget(WidgetId id)126     const overlay::Mock *getPerSecondWidget(WidgetId id) const { return &mMock; }
getRunningGraphWidget(WidgetId id)127     const overlay::Mock *getRunningGraphWidget(WidgetId id) const { return &mMock; }
getRunningHistogramWidget(WidgetId id)128     const overlay::Mock *getRunningHistogramWidget(WidgetId id) const { return &mMock; }
129 
isEnabled()130     bool isEnabled() const { return false; }
131 
132   private:
133     overlay::Mock mMock;
134 };
135 
136 #if ANGLE_ENABLE_OVERLAY
137 using OverlayType            = Overlay;
138 using CountWidget            = overlay::Count;
139 using PerSecondWidget        = overlay::PerSecond;
140 using RunningGraphWidget     = overlay::RunningGraph;
141 using RunningHistogramWidget = overlay::RunningHistogram;
142 using TextWidget             = overlay::Text;
143 #else   // !ANGLE_ENABLE_OVERLAY
144 using OverlayType            = MockOverlay;
145 using CountWidget            = const overlay::Mock;
146 using PerSecondWidget        = const overlay::Mock;
147 using RunningGraphWidget     = const overlay::Mock;
148 using RunningHistogramWidget = const overlay::Mock;
149 using TextWidget             = const overlay::Mock;
150 #endif  // ANGLE_ENABLE_OVERLAY
151 
152 }  // namespace gl
153 
154 #endif  // LIBANGLE_OVERLAY_H_
155