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::layout {
14 
VsyncChild()15 VsyncChild::VsyncChild()
16     : mObservingVsync(false),
17       mIsShutdown(false),
18       mVsyncRate(TimeDuration::Forever()) {
19   MOZ_ASSERT(NS_IsMainThread());
20 }
21 
~VsyncChild()22 VsyncChild::~VsyncChild() { MOZ_ASSERT(NS_IsMainThread()); }
23 
SendObserve()24 bool VsyncChild::SendObserve() {
25   MOZ_ASSERT(NS_IsMainThread());
26   if (!mObservingVsync && !mIsShutdown) {
27     mObservingVsync = true;
28     PVsyncChild::SendObserve();
29   }
30   return true;
31 }
32 
SendUnobserve()33 bool VsyncChild::SendUnobserve() {
34   MOZ_ASSERT(NS_IsMainThread());
35   if (mObservingVsync && !mIsShutdown) {
36     mObservingVsync = false;
37     PVsyncChild::SendUnobserve();
38   }
39   return true;
40 }
41 
ActorDestroy(ActorDestroyReason aActorDestroyReason)42 void VsyncChild::ActorDestroy(ActorDestroyReason aActorDestroyReason) {
43   MOZ_ASSERT(NS_IsMainThread());
44   MOZ_ASSERT(!mIsShutdown);
45   mIsShutdown = true;
46   mObserver = nullptr;
47 }
48 
RecvNotify(const VsyncEvent & aVsync)49 mozilla::ipc::IPCResult VsyncChild::RecvNotify(const VsyncEvent& aVsync) {
50   MOZ_ASSERT(NS_IsMainThread());
51   MOZ_ASSERT(!mIsShutdown);
52 
53   SchedulerGroup::MarkVsyncRan();
54   if (mObservingVsync && mObserver) {
55     mObserver->NotifyVsync(aVsync);
56   }
57   return IPC_OK();
58 }
59 
SetVsyncObserver(VsyncObserver * aVsyncObserver)60 void VsyncChild::SetVsyncObserver(VsyncObserver* aVsyncObserver) {
61   MOZ_ASSERT(NS_IsMainThread());
62   mObserver = aVsyncObserver;
63 }
64 
GetVsyncRate()65 TimeDuration VsyncChild::GetVsyncRate() {
66   if (mVsyncRate == TimeDuration::Forever()) {
67     PVsyncChild::SendRequestVsyncRate();
68   }
69 
70   return mVsyncRate;
71 }
72 
VsyncRate()73 TimeDuration VsyncChild::VsyncRate() { return mVsyncRate; }
74 
RecvVsyncRate(const float & aVsyncRate)75 mozilla::ipc::IPCResult VsyncChild::RecvVsyncRate(const float& aVsyncRate) {
76   mVsyncRate = TimeDuration::FromMilliseconds(aVsyncRate);
77   return IPC_OK();
78 }
79 
80 }  // namespace mozilla::layout
81