summaryrefslogtreecommitdiffstats
path: root/widget/android/AndroidVsync.cpp
blob: b1681548104fb3fe39caf63b2d1c9244907551c5 (plain)
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
/* -*- 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 "AndroidVsync.h"

#include "mozilla/java/GeckoAppShellWrappers.h"
#include "nsTArray.h"

/**
 * Implementation for the AndroidVsync class.
 */

namespace mozilla {
namespace widget {

StaticDataMutex<ThreadSafeWeakPtr<AndroidVsync>> AndroidVsync::sInstance(
    "AndroidVsync::sInstance");

/* static */ RefPtr<AndroidVsync> AndroidVsync::GetInstance() {
  auto weakInstance = sInstance.Lock();
  RefPtr<AndroidVsync> instance(*weakInstance);
  if (!instance) {
    instance = new AndroidVsync();
    *weakInstance = instance;
  }
  return instance;
}

/**
 * Owned by the Java AndroidVsync instance.
 */
class AndroidVsyncSupport final
    : public java::AndroidVsync::Natives<AndroidVsyncSupport> {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AndroidVsyncSupport)

  using Base = java::AndroidVsync::Natives<AndroidVsyncSupport>;
  using Base::AttachNative;
  using Base::DisposeNative;

  explicit AndroidVsyncSupport(AndroidVsync* aAndroidVsync)
      : mAndroidVsync(std::move(aAndroidVsync),
                      "AndroidVsyncSupport::mAndroidVsync") {}

  // Called by Java
  void NotifyVsync(const java::AndroidVsync::LocalRef& aInstance,
                   int64_t aFrameTimeNanos) {
    auto androidVsync = mAndroidVsync.Lock();
    if (*androidVsync) {
      (*androidVsync)->NotifyVsync(aFrameTimeNanos);
    }
  }

  // Called by the AndroidVsync destructor
  void Unlink() {
    auto androidVsync = mAndroidVsync.Lock();
    *androidVsync = nullptr;
  }

 protected:
  ~AndroidVsyncSupport() = default;

  DataMutex<AndroidVsync*> mAndroidVsync;
};

AndroidVsync::AndroidVsync() : mImpl("AndroidVsync.mImpl") {
  AndroidVsyncSupport::Init();

  auto impl = mImpl.Lock();
  impl->mSupport = new AndroidVsyncSupport(this);
  impl->mSupportJava = java::AndroidVsync::New();
  AndroidVsyncSupport::AttachNative(impl->mSupportJava, impl->mSupport);
  float fps = java::GeckoAppShell::GetScreenRefreshRate();
  impl->mVsyncDuration = TimeDuration::FromMilliseconds(1000.0 / fps);
}

AndroidVsync::~AndroidVsync() {
  auto impl = mImpl.Lock();
  impl->mInputObservers.Clear();
  impl->mRenderObservers.Clear();
  impl->UpdateObservingVsync();
  impl->mSupport->Unlink();
}

TimeDuration AndroidVsync::GetVsyncRate() {
  auto impl = mImpl.Lock();
  return impl->mVsyncDuration;
}

void AndroidVsync::RegisterObserver(Observer* aObserver, ObserverType aType) {
  auto impl = mImpl.Lock();
  if (aType == AndroidVsync::INPUT) {
    impl->mInputObservers.AppendElement(aObserver);
  } else {
    impl->mRenderObservers.AppendElement(aObserver);
  }
  impl->UpdateObservingVsync();
}

void AndroidVsync::UnregisterObserver(Observer* aObserver, ObserverType aType) {
  auto impl = mImpl.Lock();
  if (aType == AndroidVsync::INPUT) {
    impl->mInputObservers.RemoveElement(aObserver);
  } else {
    impl->mRenderObservers.RemoveElement(aObserver);
  }
  aObserver->Dispose();
  impl->UpdateObservingVsync();
}

void AndroidVsync::Impl::UpdateObservingVsync() {
  bool shouldObserve =
      !mInputObservers.IsEmpty() || !mRenderObservers.IsEmpty();
  if (shouldObserve != mObservingVsync) {
    mObservingVsync = mSupportJava->ObserveVsync(shouldObserve);
  }
}

// Always called on the Java UI thread.
void AndroidVsync::NotifyVsync(int64_t aFrameTimeNanos) {
  // Convert aFrameTimeNanos to a TimeStamp. The value converts trivially to
  // the internal ticks representation of TimeStamp_posix; both use the
  // monotonic clock and are in nanoseconds.
  TimeStamp timeStamp = TimeStamp::FromSystemTime(aFrameTimeNanos);

  // Do not keep the lock held while calling OnVsync.
  nsTArray<Observer*> observers;
  {
    auto impl = mImpl.Lock();
    observers.AppendElements(impl->mInputObservers);
    observers.AppendElements(impl->mRenderObservers);
  }
  for (Observer* observer : observers) {
    observer->OnVsync(timeStamp);
  }
}

}  // namespace widget
}  // namespace mozilla