1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "VsyncParent.h"
9 #include "mozilla/Unused.h"
10 #include "nsThreadUtils.h"
11 #include "nsIThread.h"
13 namespace mozilla::dom
{
15 VsyncParent::VsyncParent()
16 : mObservingVsync(false),
18 mInitialThread(NS_GetCurrentThread()) {}
20 void VsyncParent::UpdateVsyncDispatcher(
21 const RefPtr
<VsyncDispatcher
>& aVsyncDispatcher
) {
22 if (aVsyncDispatcher
== mVsyncDispatcher
) {
26 if (mObservingVsync
&& mVsyncDispatcher
) {
27 mVsyncDispatcher
->RemoveVsyncObserver(this);
29 mVsyncDispatcher
= aVsyncDispatcher
;
30 if (mObservingVsync
) {
31 mVsyncDispatcher
->AddVsyncObserver(this);
35 void VsyncParent::NotifyVsync(const VsyncEvent
& aVsync
) {
36 if (IsOnInitialThread()) {
37 DispatchVsyncEvent(aVsync
);
41 // Called on hardware vsync thread. We should post to current ipc thread.
42 nsCOMPtr
<nsIRunnable
> vsyncEvent
= NewRunnableMethod
<VsyncEvent
>(
43 "dom::VsyncParent::DispatchVsyncEvent", this,
44 &VsyncParent::DispatchVsyncEvent
, aVsync
);
45 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToThreadQueue(
46 vsyncEvent
.forget(), mInitialThread
, EventQueuePriority::Vsync
));
49 void VsyncParent::DispatchVsyncEvent(const VsyncEvent
& aVsync
) {
50 AssertIsOnInitialThread();
52 // If we call NotifyVsync() when we handle ActorDestroy() message, we might
53 // still call DispatchVsyncEvent().
54 // Similarly, we might also receive RecvUnobserveVsync() when call
55 // NotifyVsync(). We use mObservingVsync and mDestroyed flags to skip this
57 if (mObservingVsync
&& !mDestroyed
) {
58 TimeDuration vsyncRate
= mVsyncDispatcher
->GetVsyncRate();
59 Unused
<< SendNotify(aVsync
, vsyncRate
.ToMilliseconds());
63 mozilla::ipc::IPCResult
VsyncParent::RecvObserve() {
64 AssertIsOnInitialThread();
65 if (!mObservingVsync
) {
66 if (mVsyncDispatcher
) {
67 mVsyncDispatcher
->AddVsyncObserver(this);
69 mObservingVsync
= true;
72 return IPC_FAIL_NO_REASON(this);
75 mozilla::ipc::IPCResult
VsyncParent::RecvUnobserve() {
76 AssertIsOnInitialThread();
77 if (mObservingVsync
) {
78 if (mVsyncDispatcher
) {
79 mVsyncDispatcher
->RemoveVsyncObserver(this);
81 mObservingVsync
= false;
84 return IPC_FAIL_NO_REASON(this);
87 void VsyncParent::ActorDestroy(ActorDestroyReason aActorDestroyReason
) {
88 MOZ_ASSERT(!mDestroyed
);
89 AssertIsOnInitialThread();
90 if (mObservingVsync
&& mVsyncDispatcher
) {
91 mVsyncDispatcher
->RemoveVsyncObserver(this);
93 mVsyncDispatcher
= nullptr;
97 bool VsyncParent::IsOnInitialThread() {
98 return NS_GetCurrentThread() == mInitialThread
;
101 void VsyncParent::AssertIsOnInitialThread() { MOZ_ASSERT(IsOnInitialThread()); }
103 } // namespace mozilla::dom