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