Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / gtk / WaylandVsyncSource.h
blob78fe72e4451942c9f5863103c280f89aae576931
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef _WaylandVsyncSource_h_
7 #define _WaylandVsyncSource_h_
9 #include "base/thread.h"
10 #include "mozilla/RefPtr.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/Mutex.h"
13 #include "mozilla/Monitor.h"
14 #include "mozilla/layers/NativeLayerWayland.h"
15 #include "MozContainer.h"
16 #include "nsWaylandDisplay.h"
17 #include "VsyncSource.h"
19 namespace mozilla {
21 using layers::NativeLayerRootWayland;
24 * WaylandVsyncSource
26 * This class provides a per-widget VsyncSource under Wayland, emulated using
27 * frame callbacks on the widget surface with empty surface commits.
29 * Wayland does not expose vsync/vblank, as it considers that an implementation
30 * detail the clients should not concern themselves with. Instead, frame
31 * callbacks are provided whenever the compositor believes it is a good time to
32 * start drawing the next frame for a particular surface, giving us as much
33 * time as possible to do so.
35 * Note that the compositor sends frame callbacks only when it sees fit, and
36 * when that may be is entirely up to the compositor. One cannot expect a
37 * certain rate of callbacks, or any callbacks at all. Examples of common
38 * variations would be surfaces moved between outputs with different refresh
39 * rates, and surfaces that are hidden and therefore do not receieve any
40 * callbacks at all. Other hypothetical scenarios of variation could be
41 * throttling to conserve power, or because a user has requested it.
44 class WaylandVsyncSource final : public gfx::VsyncSource {
45 public:
46 explicit WaylandVsyncSource(nsWindow* aWindow);
47 virtual ~WaylandVsyncSource();
49 static Maybe<TimeDuration> GetFastestVsyncRate();
51 void MaybeUpdateSource(MozContainer* aContainer);
52 void MaybeUpdateSource(
53 const RefPtr<NativeLayerRootWayland>& aNativeLayerRoot);
55 void EnableMonitor();
56 void DisableMonitor();
58 void FrameCallback(wl_callback* aCallback, uint32_t aTime);
59 // Returns whether we should keep firing.
60 bool IdleCallback();
62 TimeDuration GetVsyncRate() override;
64 void EnableVsync() override;
66 void DisableVsync() override;
68 bool IsVsyncEnabled() override;
70 void Shutdown() override;
72 private:
73 Maybe<TimeDuration> GetVsyncRateIfEnabled();
75 void Refresh(const MutexAutoLock& aProofOfLock);
76 void SetupFrameCallback(const MutexAutoLock& aProofOfLock);
77 void CalculateVsyncRate(const MutexAutoLock& aProofOfLock,
78 TimeStamp aVsyncTimestamp);
79 void* GetWindowForLogging() { return mWindow; };
81 Mutex mMutex;
82 bool mIsShutdown MOZ_GUARDED_BY(mMutex) = false;
83 bool mVsyncEnabled MOZ_GUARDED_BY(mMutex) = false;
84 bool mMonitorEnabled MOZ_GUARDED_BY(mMutex) = false;
85 bool mCallbackRequested MOZ_GUARDED_BY(mMutex) = false;
86 MozContainer* mContainer MOZ_GUARDED_BY(mMutex) = nullptr;
87 RefPtr<NativeLayerRootWayland> mNativeLayerRoot MOZ_GUARDED_BY(mMutex);
88 TimeDuration mVsyncRate MOZ_GUARDED_BY(mMutex);
89 TimeStamp mLastVsyncTimeStamp MOZ_GUARDED_BY(mMutex);
90 wl_callback* mCallback MOZ_GUARDED_BY(mMutex) = nullptr;
92 guint mIdleTimerID = 0; // Main thread only.
93 nsWindow* const mWindow; // Main thread only, except for logging.
94 const guint mIdleTimeout;
97 } // namespace mozilla
99 #endif // _WaylandVsyncSource_h_