File: WaylandVsyncSource.h

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (110 lines) | stat: -rw-r--r-- 3,850 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef _WaylandVsyncSource_h_
#define _WaylandVsyncSource_h_

#include "base/thread.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Maybe.h"
#include "mozilla/Mutex.h"
#include "mozilla/Monitor.h"
#include "mozilla/layers/NativeLayerWayland.h"
#include "MozContainer.h"
#include "nsWaylandDisplay.h"
#include "VsyncSource.h"
#include "WaylandSurface.h"

namespace mozilla {

using layers::NativeLayerRootWayland;

/*
 * WaylandVsyncSource
 *
 * This class provides a per-widget VsyncSource under Wayland, emulated using
 * frame callbacks on the widget surface with empty surface commits.
 *
 * Wayland does not expose vsync/vblank, as it considers that an implementation
 * detail the clients should not concern themselves with. Instead, frame
 * callbacks are provided whenever the compositor believes it is a good time to
 * start drawing the next frame for a particular surface, giving us as much
 * time as possible to do so.
 *
 * Note that the compositor sends frame callbacks only when it sees fit, and
 * when that may be is entirely up to the compositor. One cannot expect a
 * certain rate of callbacks, or any callbacks at all. Examples of common
 * variations would be surfaces moved between outputs with different refresh
 * rates, and surfaces that are hidden and therefore do not receieve any
 * callbacks at all. Other hypothetical scenarios of variation could be
 * throttling to conserve power, or because a user has requested it.
 *
 */
class WaylandVsyncSource final : public gfx::VsyncSource {
 public:
  explicit WaylandVsyncSource(nsWindow* aWindow);
  virtual ~WaylandVsyncSource();

  static Maybe<TimeDuration> GetFastestVsyncRate();

  // Regular VSync callback. Runs for visible windows only.
  // aTime = 0 means emulated frame and use current time.
  void VisibleWindowCallback(uint32_t aTime = 0);

  // Idle callback for hidden windows.
  // Returns whether we should keep firing.
  bool HiddenWindowCallback();

  TimeDuration GetVsyncRate() override;

  // Called from gecko code. It generally enables/disables VSync state
  // regardless of actual VSync source.
  void EnableVsync() override;
  void DisableVsync() override;
  bool IsVsyncEnabled() override;
  void Shutdown() override;

  // Enable/Disable this particular VSync source. Called from widget code
  // if we know that nsWindow become visible/hidden.
  void EnableVSyncSource();
  void DisableVSyncSource();

  // We addref/unref this during init so we should not
  // call it from constructor.
  void Init();

 private:
  Maybe<TimeDuration> GetVsyncRateIfEnabled();

  void CalculateVsyncRateLocked(const MutexAutoLock& aProofOfLock,
                                TimeStamp aVsyncTimestamp);
  void* GetWindowForLogging() { return mWindow; };

  void SetHiddenWindowVSync();

  void SetVSyncEventsStateLocked(const MutexAutoLock& aProofOfLock,
                                 bool aEnabled);

  Mutex mMutex;

  // Main thread only, except for logging.
  RefPtr<nsWindow> mWindow;
  RefPtr<widget::WaylandSurface> mWaylandSurface MOZ_GUARDED_BY(mMutex);

  bool mIsShutdown MOZ_GUARDED_BY(mMutex) = false;
  bool mVsyncEnabled MOZ_GUARDED_BY(mMutex) = false;
  bool mVsyncSourceEnabled MOZ_GUARDED_BY(mMutex) = false;

  TimeDuration mVsyncRate MOZ_GUARDED_BY(mMutex);
  TimeStamp mLastVsyncTimeStamp MOZ_GUARDED_BY(mMutex);
  uint32_t mLastFrameTime MOZ_GUARDED_BY(mMutex) = 0;

  guint mHiddenWindowTimerID = 0;    // Main thread only.
  const guint mHiddenWindowTimeout;  // Main thread only.
};

}  // namespace mozilla

#endif  // _WaylandVsyncSource_h_