blob: 3fecd53b43e214d9d9d3937506c944d0d8957658 [file] [log] [blame]
Michael Wright63c168a2015-12-04 17:59:42 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Choreographer"
18//#define LOG_NDEBUG 0
19
20#include <cinttypes>
21#include <queue>
22#include <thread>
23
24#include <android/choreographer.h>
25#include <androidfw/DisplayEventDispatcher.h>
26#include <gui/ISurfaceComposer.h>
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080027#include <gui/SurfaceComposerClient.h>
Michael Wright63c168a2015-12-04 17:59:42 +000028#include <utils/Looper.h>
29#include <utils/Mutex.h>
30#include <utils/Timers.h>
31
32namespace android {
33
34static inline const char* toString(bool value) {
35 return value ? "true" : "false";
36}
37
38struct FrameCallback {
39 AChoreographer_frameCallback callback;
40 void* data;
41 nsecs_t dueTime;
42
43 inline bool operator<(const FrameCallback& rhs) const {
44 // Note that this is intentionally flipped because we want callbacks due sooner to be at
45 // the head of the queue
46 return dueTime > rhs.dueTime;
47 }
48};
49
50
51class Choreographer : public DisplayEventDispatcher, public MessageHandler {
52public:
53 void postFrameCallback(AChoreographer_frameCallback cb, void* data);
54 void postFrameCallbackDelayed(AChoreographer_frameCallback cb, void* data, nsecs_t delay);
55
56 enum {
57 MSG_SCHEDULE_CALLBACKS = 0,
58 MSG_SCHEDULE_VSYNC = 1
59 };
60 virtual void handleMessage(const Message& message) override;
61
62 static Choreographer* getForThread();
63
64protected:
65 virtual ~Choreographer() = default;
66
67private:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -070068 explicit Choreographer(const sp<Looper>& looper);
Michael Wright63c168a2015-12-04 17:59:42 +000069 Choreographer(const Choreographer&) = delete;
70
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080071 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count) override;
72 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
Ady Abrahama5a21f72019-02-13 16:41:59 -080073 void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId,
74 int32_t configId) override;
Michael Wright63c168a2015-12-04 17:59:42 +000075
76 void scheduleCallbacks();
77
78 // Protected by mLock
79 std::priority_queue<FrameCallback> mCallbacks;
80
81 mutable Mutex mLock;
82
83 const sp<Looper> mLooper;
84 const std::thread::id mThreadId;
85};
86
87
Michael Wrightc0d3d3f2016-01-26 16:03:25 -080088static thread_local Choreographer* gChoreographer;
Michael Wright63c168a2015-12-04 17:59:42 +000089Choreographer* Choreographer::getForThread() {
90 if (gChoreographer == nullptr) {
91 sp<Looper> looper = Looper::getForThread();
92 if (!looper.get()) {
93 ALOGW("No looper prepared for thread");
94 return nullptr;
95 }
96 gChoreographer = new Choreographer(looper);
97 status_t result = gChoreographer->initialize();
98 if (result != OK) {
99 ALOGW("Failed to initialize");
100 return nullptr;
101 }
102 }
103 return gChoreographer;
104}
105
106Choreographer::Choreographer(const sp<Looper>& looper) :
107 DisplayEventDispatcher(looper), mLooper(looper), mThreadId(std::this_thread::get_id()) {
108}
109
110void Choreographer::postFrameCallback(AChoreographer_frameCallback cb, void* data) {
111 postFrameCallbackDelayed(cb, data, 0);
112}
113
114void Choreographer::postFrameCallbackDelayed(
115 AChoreographer_frameCallback cb, void* data, nsecs_t delay) {
116 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
117 FrameCallback callback{cb, data, now + delay};
118 {
119 AutoMutex _l{mLock};
120 mCallbacks.push(callback);
121 }
122 if (callback.dueTime <= now) {
123 if (std::this_thread::get_id() != mThreadId) {
124 Message m{MSG_SCHEDULE_VSYNC};
125 mLooper->sendMessage(this, m);
126 } else {
127 scheduleVsync();
128 }
129 } else {
130 Message m{MSG_SCHEDULE_CALLBACKS};
131 mLooper->sendMessageDelayed(delay, this, m);
132 }
133}
134
135void Choreographer::scheduleCallbacks() {
136 AutoMutex _{mLock};
137 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
138 if (mCallbacks.top().dueTime <= now) {
139 ALOGV("choreographer %p ~ scheduling vsync", this);
140 scheduleVsync();
141 return;
142 }
143}
144
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800145// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
146// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
147// the internal display implicitly.
148void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) {
Michael Wright63c168a2015-12-04 17:59:42 +0000149 std::vector<FrameCallback> callbacks{};
150 {
151 AutoMutex _l{mLock};
152 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
153 while (!mCallbacks.empty() && mCallbacks.top().dueTime < now) {
154 callbacks.push_back(mCallbacks.top());
155 mCallbacks.pop();
156 }
157 }
158 for (const auto& cb : callbacks) {
159 cb.callback(timestamp, cb.data);
160 }
161}
162
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800163void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
164 ALOGV("choreographer %p ~ received hotplug event (displayId=%"
165 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", connected=%s), ignoring.",
166 this, displayId, toString(connected));
Michael Wright63c168a2015-12-04 17:59:42 +0000167}
168
Ady Abrahama5a21f72019-02-13 16:41:59 -0800169void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId displayId,
170 int32_t configId) {
171 ALOGV("choreographer %p ~ received config changed event (displayId=%"
172 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", configId=%s), ignoring.",
173 this, displayId, toString(configId));
174}
175
Michael Wright63c168a2015-12-04 17:59:42 +0000176void Choreographer::handleMessage(const Message& message) {
177 switch (message.what) {
178 case MSG_SCHEDULE_CALLBACKS:
179 scheduleCallbacks();
180 break;
181 case MSG_SCHEDULE_VSYNC:
182 scheduleVsync();
183 break;
184 }
185}
186
187}
188
189/* Glue for the NDK interface */
190
191using android::Choreographer;
192
193static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
194 return reinterpret_cast<Choreographer*>(choreographer);
195}
196
197static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
198 return reinterpret_cast<AChoreographer*>(choreographer);
199}
200
201AChoreographer* AChoreographer_getInstance() {
202 return Choreographer_to_AChoreographer(Choreographer::getForThread());
203}
204
205void AChoreographer_postFrameCallback(AChoreographer* choreographer,
206 AChoreographer_frameCallback callback, void* data) {
207 AChoreographer_to_Choreographer(choreographer)->postFrameCallback(callback, data);
208}
209void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
210 AChoreographer_frameCallback callback, void* data, long delayMillis) {
211 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
212 callback, data, ms2ns(delayMillis));
213}