summaryrefslogtreecommitdiffstats
path: root/widget/gtk/WaylandVsyncSource.cpp
blob: 02f9d9c38b796d1f30bcb9f689f4210952da27ec (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#ifdef MOZ_WAYLAND

#  include "WaylandVsyncSource.h"
#  include "nsThreadUtils.h"
#  include "nsISupportsImpl.h"
#  include "MainThreadUtils.h"

#  include <gdk/gdkwayland.h>

using namespace mozilla::widget;

namespace mozilla {

static void WaylandVsyncSourceCallbackHandler(void* data,
                                              struct wl_callback* callback,
                                              uint32_t time) {
  WaylandVsyncSource::WaylandDisplay* context =
      (WaylandVsyncSource::WaylandDisplay*)data;
  wl_callback_destroy(callback);
  context->FrameCallback(time);
}

static const struct wl_callback_listener WaylandVsyncSourceCallbackListener = {
    WaylandVsyncSourceCallbackHandler};

WaylandVsyncSource::WaylandDisplay::WaylandDisplay(MozContainer* container)
    : mEnabledLock("WaylandVsyncEnabledLock"),
      mIsShutdown(false),
      mVsyncEnabled(false),
      mMonitorEnabled(false),
      mCallback(nullptr),
      mContainer(container),
      mLastVsyncTimeStamp(TimeStamp::Now()) {
  MOZ_ASSERT(NS_IsMainThread());

  // We store the display here so all the frame callbacks won't have to look it
  // up all the time.
  mDisplay = WaylandDisplayGetWLDisplay();

  mVsyncRate = TimeDuration::FromMilliseconds(1000.0 / 60.0);
}

void WaylandVsyncSource::WaylandDisplay::ClearFrameCallback() {
  if (mCallback) {
    wl_callback_destroy(mCallback);
    mCallback = nullptr;
  }
}

void WaylandVsyncSource::WaylandDisplay::Refresh() {
  TimeStamp outputTimestamp;
  {
    MutexAutoLock lock(mEnabledLock);
    if (!mMonitorEnabled || !mVsyncEnabled || mCallback) {
      // We don't need to do anything because:
      // * We are unwanted by our widget or monitor, or
      // * The last frame callback hasn't yet run to see that it had been shut
      //   down, so we can reuse it after having set mVsyncEnabled to true.
      return;
    }

    struct wl_surface* surface = moz_container_wayland_surface_lock(mContainer);
    if (!surface) {
      // The surface hasn't been created yet. Try again when the surface is
      // ready.
      RefPtr<WaylandVsyncSource::WaylandDisplay> self(this);
      moz_container_wayland_add_initial_draw_callback(
          mContainer, [self]() -> void { self->Refresh(); });
      return;
    }
    moz_container_wayland_surface_unlock(mContainer, &surface);

    // Vsync is enabled, but we don't have a callback configured. Set one up so
    // we can get to work.
    SetupFrameCallback();
    mLastVsyncTimeStamp = TimeStamp::Now();
    outputTimestamp = mLastVsyncTimeStamp + GetVsyncRate();
  }
  NotifyVsync(mLastVsyncTimeStamp, outputTimestamp);
}

void WaylandVsyncSource::WaylandDisplay::EnableMonitor() {
  {
    MutexAutoLock lock(mEnabledLock);
    if (mMonitorEnabled) {
      return;
    }
    mMonitorEnabled = true;
  }
  Refresh();
}

void WaylandVsyncSource::WaylandDisplay::DisableMonitor() {
  MutexAutoLock lock(mEnabledLock);
  if (!mMonitorEnabled) {
    return;
  }
  mMonitorEnabled = false;
  ClearFrameCallback();
}

void WaylandVsyncSource::WaylandDisplay::SetupFrameCallback() {
  MOZ_ASSERT(mCallback == nullptr);
  struct wl_surface* surface = moz_container_wayland_surface_lock(mContainer);
  if (!surface) {
    // We don't have a surface, either due to being called before it was made
    // available in the mozcontainer, or after it was destroyed. We're all done
    // regardless.
    ClearFrameCallback();
    return;
  }

  mCallback = wl_surface_frame(surface);
  wl_callback_add_listener(mCallback, &WaylandVsyncSourceCallbackListener,
                           this);
  wl_surface_commit(surface);
  wl_display_flush(mDisplay);
  moz_container_wayland_surface_unlock(mContainer, &surface);
}

void WaylandVsyncSource::WaylandDisplay::FrameCallback(uint32_t timestampTime) {
  TimeStamp outputTimestamp;
  {
    MutexAutoLock lock(mEnabledLock);
    mCallback = nullptr;

    if (!mVsyncEnabled || !mMonitorEnabled) {
      // We are unwanted by either our creator or our consumer, so we just stop
      // here without setting up a new frame callback.
      return;
    }

    // Configure our next frame callback.
    SetupFrameCallback();

    int64_t tick =
        BaseTimeDurationPlatformUtils::TicksFromMilliseconds(timestampTime);
    TimeStamp callbackTimeStamp = TimeStamp::FromSystemTime(tick);
    double duration = (TimeStamp::Now() - callbackTimeStamp).ToMilliseconds();

    TimeStamp vsyncTimestamp;
    if (duration < 50 && duration > -50) {
      vsyncTimestamp = callbackTimeStamp;
    } else {
      vsyncTimestamp = TimeStamp::Now();
    }

    CalculateVsyncRate(vsyncTimestamp);
    mLastVsyncTimeStamp = vsyncTimestamp;
    outputTimestamp = vsyncTimestamp + GetVsyncRate();
  }
  NotifyVsync(mLastVsyncTimeStamp, outputTimestamp);
}

TimeDuration WaylandVsyncSource::WaylandDisplay::GetVsyncRate() {
  return mVsyncRate;
}

void WaylandVsyncSource::WaylandDisplay::CalculateVsyncRate(
    TimeStamp vsyncTimestamp) {
  double duration = (vsyncTimestamp - mLastVsyncTimeStamp).ToMilliseconds();
  double curVsyncRate = mVsyncRate.ToMilliseconds();
  double correction;

  if (duration > curVsyncRate) {
    correction = fmin(curVsyncRate, (duration - curVsyncRate) / 10);
    mVsyncRate += TimeDuration::FromMilliseconds(correction);
  } else {
    correction = fmin(curVsyncRate / 2, (curVsyncRate - duration) / 10);
    mVsyncRate -= TimeDuration::FromMilliseconds(correction);
  }
}

void WaylandVsyncSource::WaylandDisplay::EnableVsync() {
  MOZ_ASSERT(NS_IsMainThread());
  {
    MutexAutoLock lock(mEnabledLock);
    if (mVsyncEnabled || mIsShutdown) {
      return;
    }
    mVsyncEnabled = true;
  }
  Refresh();
}

void WaylandVsyncSource::WaylandDisplay::DisableVsync() {
  MutexAutoLock lock(mEnabledLock);
  mVsyncEnabled = false;
  ClearFrameCallback();
}

bool WaylandVsyncSource::WaylandDisplay::IsVsyncEnabled() {
  MutexAutoLock lock(mEnabledLock);
  return mVsyncEnabled;
}

void WaylandVsyncSource::WaylandDisplay::Shutdown() {
  MOZ_ASSERT(NS_IsMainThread());
  MutexAutoLock lock(mEnabledLock);
  mIsShutdown = true;
  mVsyncEnabled = false;
  ClearFrameCallback();
  wl_display_roundtrip(mDisplay);
}

}  // namespace mozilla

#endif  // MOZ_WAYLAND