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/. */
6 
7 #include "VsyncChild.h"
8 
9 #include "mozilla/SchedulerGroup.h"
10 #include "mozilla/VsyncDispatcher.h"
11 #include "nsThreadUtils.h"
12 
13 namespace mozilla::dom {
14 
VsyncChild()15 VsyncChild::VsyncChild()
16     : mIsShutdown(false), mVsyncRate(TimeDuration::Forever()) {
17   MOZ_ASSERT(NS_IsMainThread());
18 }
19 
~VsyncChild()20 VsyncChild::~VsyncChild() { MOZ_ASSERT(NS_IsMainThread()); }
21 
AddChildRefreshTimer(VsyncObserver * aVsyncObserver)22 void VsyncChild::AddChildRefreshTimer(VsyncObserver* aVsyncObserver) {
23   MOZ_ASSERT(NS_IsMainThread());
24   MOZ_ASSERT(!mObservers.Contains(aVsyncObserver));
25 
26   if (mIsShutdown) {
27     return;
28   }
29 
30   if (mObservers.IsEmpty()) {
31     Unused << PVsyncChild::SendObserve();
32   }
33   mObservers.AppendElement(std::move(aVsyncObserver));
34 }
35 
RemoveChildRefreshTimer(VsyncObserver * aVsyncObserver)36 void VsyncChild::RemoveChildRefreshTimer(VsyncObserver* aVsyncObserver) {
37   MOZ_ASSERT(NS_IsMainThread());
38   if (mIsShutdown) {
39     return;
40   }
41 
42   if (mObservers.RemoveElement(aVsyncObserver) && mObservers.IsEmpty()) {
43     Unused << PVsyncChild::SendUnobserve();
44   }
45 }
46 
ActorDestroy(ActorDestroyReason aActorDestroyReason)47 void VsyncChild::ActorDestroy(ActorDestroyReason aActorDestroyReason) {
48   MOZ_ASSERT(NS_IsMainThread());
49   MOZ_ASSERT(!mIsShutdown);
50   mIsShutdown = true;
51 
52   if (!mObservers.IsEmpty()) {
53     Unused << PVsyncChild::SendUnobserve();
54   }
55   mObservers.Clear();
56 }
57 
RecvNotify(const VsyncEvent & aVsync,const float & aVsyncRate)58 mozilla::ipc::IPCResult VsyncChild::RecvNotify(const VsyncEvent& aVsync,
59                                                const float& aVsyncRate) {
60   MOZ_ASSERT(NS_IsMainThread());
61   MOZ_ASSERT(!mIsShutdown);
62 
63   SchedulerGroup::MarkVsyncRan();
64 
65   mVsyncRate = TimeDuration::FromMilliseconds(aVsyncRate);
66 
67   for (VsyncObserver* observer : mObservers.ForwardRange()) {
68     observer->NotifyVsync(aVsync);
69   }
70   return IPC_OK();
71 }
72 
GetVsyncRate()73 TimeDuration VsyncChild::GetVsyncRate() { return mVsyncRate; }
74 
75 }  // namespace mozilla::dom
76