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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/* -*- 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/. */
#include "mozilla/layers/CompositorVsyncScheduler.h"
#include <stdio.h> // for fprintf, stdout
#include <stdint.h> // for uint64_t
#include "base/task.h" // for CancelableTask, etc
#include "base/thread.h" // for Thread
#include "gfxPlatform.h" // for gfxPlatform
#ifdef MOZ_WIDGET_GTK
# include "gfxPlatformGtk.h" // for gfxPlatform
#endif
#include "mozilla/AutoRestore.h" // for AutoRestore
#include "mozilla/DebugOnly.h" // for DebugOnly
#include "mozilla/StaticPrefs_gfx.h"
#include "mozilla/StaticPrefs_layers.h"
#include "mozilla/gfx/2D.h" // for DrawTarget
#include "mozilla/gfx/Point.h" // for IntSize
#include "mozilla/gfx/Rect.h" // for IntSize
#include "mozilla/layers/CompositorThread.h"
#include "mozilla/layers/CompositorVsyncSchedulerOwner.h"
#include "mozilla/mozalloc.h" // for operator new, etc
#include "nsCOMPtr.h" // for already_AddRefed
#include "nsDebug.h" // for NS_ASSERTION, etc
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
#include "nsIWidget.h" // for nsIWidget
#include "nsThreadUtils.h" // for NS_IsMainThread
#include "mozilla/glean/GfxMetrics.h"
#include "mozilla/VsyncDispatcher.h"
#if defined(XP_WIN) || defined(MOZ_WIDGET_GTK)
# include "VsyncSource.h"
#endif
#include "mozilla/widget/CompositorWidget.h"
#include "VRManager.h"
namespace mozilla {
namespace layers {
using namespace mozilla::gfx;
CompositorVsyncScheduler::Observer::Observer(CompositorVsyncScheduler* aOwner)
: mMutex("CompositorVsyncScheduler.Observer.Mutex"), mOwner(aOwner) {}
CompositorVsyncScheduler::Observer::~Observer() { MOZ_ASSERT(!mOwner); }
void CompositorVsyncScheduler::Observer::NotifyVsync(const VsyncEvent& aVsync) {
MutexAutoLock lock(mMutex);
if (!mOwner) {
return;
}
mOwner->NotifyVsync(aVsync);
}
void CompositorVsyncScheduler::Observer::Destroy() {
MutexAutoLock lock(mMutex);
mOwner = nullptr;
}
CompositorVsyncScheduler::CompositorVsyncScheduler(
CompositorVsyncSchedulerOwner* aVsyncSchedulerOwner,
widget::CompositorWidget* aWidget)
: mVsyncSchedulerOwner(aVsyncSchedulerOwner),
mLastComposeTime(SampleTime::FromNow()),
mLastVsyncTime(TimeStamp::Now()),
mLastVsyncOutputTime(TimeStamp::Now()),
mIsObservingVsync(false),
mRendersDelayedByVsyncReasons(wr::RenderReasons::NONE),
mVsyncNotificationsSkipped(0),
mWidget(aWidget),
mCurrentCompositeTaskMonitor("CurrentCompositeTaskMonitor"),
mCurrentCompositeTask(nullptr),
mCurrentCompositeTaskReasons(wr::RenderReasons::NONE),
mCurrentVRTaskMonitor("CurrentVRTaskMonitor"),
mCurrentVRTask(nullptr) {
mVsyncObserver = new Observer(this);
// mAsapScheduling is set on the main thread during init,
// but is only accessed after on the compositor thread.
mAsapScheduling =
StaticPrefs::layers_offmainthreadcomposition_frame_rate() == 0 ||
gfxPlatform::IsInLayoutAsapMode();
}
CompositorVsyncScheduler::~CompositorVsyncScheduler() {
MOZ_ASSERT(!mIsObservingVsync);
MOZ_ASSERT(!mVsyncObserver);
// The CompositorVsyncDispatcher is cleaned up before this in the
// nsIWidget, which stops vsync listeners
mVsyncSchedulerOwner = nullptr;
}
void CompositorVsyncScheduler::Destroy() {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
if (!mVsyncObserver) {
// Destroy was already called on this object.
return;
}
UnobserveVsync();
mVsyncObserver->Destroy();
mVsyncObserver = nullptr;
mCompositeRequestedAt = TimeStamp();
CancelCurrentCompositeTask();
CancelCurrentVRTask();
}
void CompositorVsyncScheduler::PostCompositeTask(const VsyncEvent& aVsyncEvent,
wr::RenderReasons aReasons) {
MonitorAutoLock lock(mCurrentCompositeTaskMonitor);
mCurrentCompositeTaskReasons = mCurrentCompositeTaskReasons | aReasons;
if (mCurrentCompositeTask == nullptr && CompositorThread()) {
RefPtr<CancelableRunnable> task =
NewCancelableRunnableMethod<VsyncEvent, wr::RenderReasons>(
"layers::CompositorVsyncScheduler::Composite", this,
&CompositorVsyncScheduler::Composite, aVsyncEvent, aReasons);
mCurrentCompositeTask = task;
CompositorThread()->Dispatch(task.forget());
}
}
void CompositorVsyncScheduler::PostVRTask(TimeStamp aTimestamp) {
MonitorAutoLock lockVR(mCurrentVRTaskMonitor);
if (mCurrentVRTask == nullptr && CompositorThread()) {
RefPtr<CancelableRunnable> task = NewCancelableRunnableMethod<TimeStamp>(
"layers::CompositorVsyncScheduler::DispatchVREvents", this,
&CompositorVsyncScheduler::DispatchVREvents, aTimestamp);
mCurrentVRTask = task;
CompositorThread()->Dispatch(task.forget());
}
}
void CompositorVsyncScheduler::ScheduleComposition(wr::RenderReasons aReasons) {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
if (!mVsyncObserver) {
// Destroy was already called on this object.
return;
}
// Make a synthetic vsync event for the calls to PostCompositeTask below.
TimeStamp vsyncTime = TimeStamp::Now();
TimeStamp outputTime = vsyncTime + mVsyncSchedulerOwner->GetVsyncInterval();
VsyncEvent vsyncEvent(VsyncId(), vsyncTime, outputTime);
if (mAsapScheduling) {
// Used only for performance testing purposes, and when recording/replaying
// to ensure that graphics are up to date.
PostCompositeTask(vsyncEvent, aReasons);
} else {
if (!mCompositeRequestedAt) {
mCompositeRequestedAt = TimeStamp::Now();
}
if (!mIsObservingVsync && mCompositeRequestedAt) {
ObserveVsync();
// Starting to observe vsync is an async operation that goes
// through the main thread of the UI process. It's possible that
// we're blocking there waiting on a composite, so schedule an initial
// one now to get things started.
PostCompositeTask(vsyncEvent,
aReasons | wr::RenderReasons::START_OBSERVING_VSYNC);
} else {
mRendersDelayedByVsyncReasons = aReasons;
}
}
}
void CompositorVsyncScheduler::NotifyVsync(const VsyncEvent& aVsync) {
// Called from the vsync dispatch thread. When in the GPU Process, that's
// the same as the compositor thread.
#ifdef DEBUG
# ifdef MOZ_WAYLAND
// On Wayland, we dispatch vsync from the main thread, without a GPU process.
// To allow this, we skip the following asserts if we're currently utilizing
// the Wayland backend. The IsParentProcess guard is needed to ensure that
// we don't accidentally attempt to initialize the gfxPlatform in the GPU
// process on X11.
if (!XRE_IsParentProcess() ||
!gfxPlatformGtk::GetPlatform()->IsWaylandDisplay())
# endif // MOZ_WAYLAND
{
MOZ_ASSERT_IF(XRE_IsParentProcess(),
!CompositorThreadHolder::IsInCompositorThread());
MOZ_ASSERT(!NS_IsMainThread());
}
MOZ_ASSERT_IF(XRE_GetProcessType() == GeckoProcessType_GPU,
CompositorThreadHolder::IsInCompositorThread());
#endif // DEBUG
#if defined(MOZ_WIDGET_ANDROID)
gfx::VRManager* vm = gfx::VRManager::Get();
if (!vm->IsPresenting()) {
PostCompositeTask(aVsync, wr::RenderReasons::VSYNC);
}
#else
PostCompositeTask(aVsync, wr::RenderReasons::VSYNC);
#endif // defined(MOZ_WIDGET_ANDROID)
PostVRTask(aVsync.mTime);
}
void CompositorVsyncScheduler::CancelCurrentVRTask() {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread() ||
NS_IsMainThread());
MonitorAutoLock lock(mCurrentVRTaskMonitor);
if (mCurrentVRTask) {
mCurrentVRTask->Cancel();
mCurrentVRTask = nullptr;
}
}
wr::RenderReasons CompositorVsyncScheduler::CancelCurrentCompositeTask() {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread() ||
NS_IsMainThread());
MonitorAutoLock lock(mCurrentCompositeTaskMonitor);
wr::RenderReasons canceledTaskRenderReasons = mCurrentCompositeTaskReasons;
mCurrentCompositeTaskReasons = wr::RenderReasons::NONE;
if (mCurrentCompositeTask) {
mCurrentCompositeTask->Cancel();
mCurrentCompositeTask = nullptr;
}
return canceledTaskRenderReasons;
}
void CompositorVsyncScheduler::Composite(const VsyncEvent& aVsyncEvent,
wr::RenderReasons aReasons) {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
MOZ_ASSERT(mVsyncSchedulerOwner);
{ // scope lock
MonitorAutoLock lock(mCurrentCompositeTaskMonitor);
aReasons =
aReasons | mCurrentCompositeTaskReasons | mRendersDelayedByVsyncReasons;
mCurrentCompositeTaskReasons = wr::RenderReasons::NONE;
mRendersDelayedByVsyncReasons = wr::RenderReasons::NONE;
mCurrentCompositeTask = nullptr;
}
mLastVsyncTime = aVsyncEvent.mTime;
mLastVsyncOutputTime = aVsyncEvent.mOutputTime;
mLastVsyncId = aVsyncEvent.mId;
if (!mAsapScheduling) {
// Some early exit conditions if we're not in ASAP mode
if (aVsyncEvent.mTime < mLastComposeTime.Time()) {
// We can sometimes get vsync timestamps that are in the past
// compared to the last compose with force composites.
// In those cases, wait until the next vsync;
return;
}
if (mVsyncSchedulerOwner->IsPendingComposite()) {
// If previous composite is still on going, finish it and wait for the
// next vsync.
mVsyncSchedulerOwner->FinishPendingComposite();
return;
}
}
if (mCompositeRequestedAt || mAsapScheduling) {
mCompositeRequestedAt = TimeStamp();
mLastComposeTime = SampleTime::FromVsync(aVsyncEvent.mTime);
// Tell the owner to do a composite
mVsyncSchedulerOwner->CompositeToTarget(aVsyncEvent.mId, aReasons, nullptr,
nullptr);
mVsyncNotificationsSkipped = 0;
TimeDuration compositeFrameTotal = TimeStamp::Now() - aVsyncEvent.mTime;
mozilla::glean::gfx::composite_frame_roundtrip_time.AccumulateRawDuration(
compositeFrameTotal);
} else if (mVsyncNotificationsSkipped++ >
StaticPrefs::gfx_vsync_compositor_unobserve_count_AtStartup()) {
UnobserveVsync();
}
}
void CompositorVsyncScheduler::ForceComposeToTarget(wr::RenderReasons aReasons,
gfx::DrawTarget* aTarget,
const IntRect* aRect) {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
/**
* bug 1138502 - There are cases such as during long-running window resizing
* events where we receive many force-composites. We also continue to get
* vsync notifications. Because the force-composites trigger compositing and
* clear the mCompositeRequestedAt timestamp, the vsync notifications will not
* need to do anything and so will increment the mVsyncNotificationsSkipped
* counter to indicate the vsync was ignored. If this happens enough times, we
* will disable listening for vsync entirely. On the next force-composite we
* will enable listening for vsync again, and continued force-composites and
* vsyncs will cause oscillation between observing vsync and not. On some
* platforms, enabling/disabling vsync is not free and this oscillating
* behavior causes a performance hit. In order to avoid this problem, we reset
* the mVsyncNotificationsSkipped counter to keep vsync enabled.
*/
mVsyncNotificationsSkipped = 0;
mLastComposeTime = SampleTime::FromNow();
MOZ_ASSERT(mVsyncSchedulerOwner);
mVsyncSchedulerOwner->CompositeToTarget(VsyncId(), aReasons, aTarget, aRect);
}
bool CompositorVsyncScheduler::NeedsComposite() {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
return (bool)mCompositeRequestedAt;
}
bool CompositorVsyncScheduler::FlushPendingComposite() {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
if (mCompositeRequestedAt) {
wr::RenderReasons reasons = CancelCurrentCompositeTask();
ForceComposeToTarget(reasons, nullptr, nullptr);
return true;
}
return false;
}
void CompositorVsyncScheduler::ObserveVsync() {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
mWidget->ObserveVsync(mVsyncObserver);
mIsObservingVsync = true;
}
void CompositorVsyncScheduler::UnobserveVsync() {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
mWidget->ObserveVsync(nullptr);
mIsObservingVsync = false;
}
void CompositorVsyncScheduler::DispatchVREvents(TimeStamp aVsyncTimestamp) {
{
MonitorAutoLock lock(mCurrentVRTaskMonitor);
mCurrentVRTask = nullptr;
}
// This only allows to be called by CompositorVsyncScheduler::PostVRTask()
// When the process is going to shutdown, the runnable has chance to be
// executed by other threads, we only want it to be run in the compositor
// thread.
if (!CompositorThreadHolder::IsInCompositorThread()) {
return;
}
VRManager* vm = VRManager::Get();
vm->NotifyVsync(aVsyncTimestamp);
}
const SampleTime& CompositorVsyncScheduler::GetLastComposeTime() const {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
return mLastComposeTime;
}
const TimeStamp& CompositorVsyncScheduler::GetLastVsyncTime() const {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
return mLastVsyncTime;
}
const TimeStamp& CompositorVsyncScheduler::GetLastVsyncOutputTime() const {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
return mLastVsyncOutputTime;
}
const VsyncId& CompositorVsyncScheduler::GetLastVsyncId() const {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
return mLastVsyncId;
}
void CompositorVsyncScheduler::UpdateLastComposeTime() {
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
mLastComposeTime = SampleTime::FromNow();
}
} // namespace layers
} // namespace mozilla
|