tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Overlay.h (4496B)


      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 
     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 
     67    overlay::Text *getTextWidget(WidgetId id) const
     68    {
     69        return getWidgetAs<overlay::Text, WidgetType::Text>(id);
     70    }
     71    overlay::Count *getCountWidget(WidgetId id) const
     72    {
     73        return getWidgetAs<overlay::Count, WidgetType::Count>(id);
     74    }
     75    overlay::PerSecond *getPerSecondWidget(WidgetId id) const
     76    {
     77        return getWidgetAs<overlay::PerSecond, WidgetType::PerSecond>(id);
     78    }
     79    overlay::RunningGraph *getRunningGraphWidget(WidgetId id) const
     80    {
     81        return getWidgetAs<overlay::RunningGraph, WidgetType::RunningGraph>(id);
     82    }
     83    overlay::RunningHistogram *getRunningHistogramWidget(WidgetId id) const
     84    {
     85        return getWidgetAs<overlay::RunningHistogram, WidgetType::RunningHistogram>(id);
     86    }
     87 
     88    rx::OverlayImpl *getImplementation() const { return mImplementation.get(); }
     89 
     90    bool isEnabled() const
     91    {
     92        return mImplementation != nullptr && mState.getEnabledWidgetCount() > 0;
     93    }
     94 
     95  private:
     96    template <typename Widget, WidgetType Type>
     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 
    119    void init() {}
    120    void destroy(const Context *context) {}
    121 
    122    void onSwap() const {}
    123 
    124    const overlay::Mock *getTextWidget(WidgetId id) const { return &mMock; }
    125    const overlay::Mock *getCountWidget(WidgetId id) const { return &mMock; }
    126    const overlay::Mock *getPerSecondWidget(WidgetId id) const { return &mMock; }
    127    const overlay::Mock *getRunningGraphWidget(WidgetId id) const { return &mMock; }
    128    const overlay::Mock *getRunningHistogramWidget(WidgetId id) const { return &mMock; }
    129 
    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_