tor-browser

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

OverlayWidgets.h (4434B)


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