blob: 2db575bf5a134403c723cc012a3f50be31451bd8 [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;
Michael Wright63c168a2015-12-04 17:59:42 +000073
74 void scheduleCallbacks();
75
76 // Protected by mLock
77 std::priority_queue<FrameCallback> mCallbacks;
78
79 mutable Mutex mLock;
80
81 const sp<Looper> mLooper;
82 const std::thread::id mThreadId;
83};
84
85
Michael Wrightc0d3d3f2016-01-26 16:03:25 -080086static thread_local Choreographer* gChoreographer;
Michael Wright63c168a2015-12-04 17:59:42 +000087Choreographer* Choreographer::getForThread() {
88 if (gChoreographer == nullptr) {
89 sp<Looper> looper = Looper::getForThread();
90 if (!looper.get()) {
91 ALOGW("No looper prepared for thread");
92 return nullptr;
93 }
94 gChoreographer = new Choreographer(looper);
95 status_t result = gChoreographer->initialize();
96 if (result != OK) {
97 ALOGW("Failed to initialize");
98 return nullptr;
99 }
100 }
101 return gChoreographer;
102}
103
104Choreographer::Choreographer(const sp<Looper>& looper) :
105 DisplayEventDispatcher(looper), mLooper(looper), mThreadId(std::this_thread::get_id()) {
106}
107
108void Choreographer::postFrameCallback(AChoreographer_frameCallback cb, void* data) {
109 postFrameCallbackDelayed(cb, data, 0);
110}
111
112void Choreographer::postFrameCallbackDelayed(
113 AChoreographer_frameCallback cb, void* data, nsecs_t delay) {
114 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
115 FrameCallback callback{cb, data, now + delay};
116 {
117 AutoMutex _l{mLock};
118 mCallbacks.push(callback);
119 }
120 if (callback.dueTime <= now) {
121 if (std::this_thread::get_id() != mThreadId) {
122 Message m{MSG_SCHEDULE_VSYNC};
123 mLooper->sendMessage(this, m);
124 } else {
125 scheduleVsync();
126 }
127 } else {
128 Message m{MSG_SCHEDULE_CALLBACKS};
129 mLooper->sendMessageDelayed(delay, this, m);
130 }
131}
132
133void Choreographer::scheduleCallbacks() {
134 AutoMutex _{mLock};
135 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
136 if (mCallbacks.top().dueTime <= now) {
137 ALOGV("choreographer %p ~ scheduling vsync", this);
138 scheduleVsync();
139 return;
140 }
141}
142
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800143// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
144// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
145// the internal display implicitly.
146void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) {
Michael Wright63c168a2015-12-04 17:59:42 +0000147 std::vector<FrameCallback> callbacks{};
148 {
149 AutoMutex _l{mLock};
150 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
151 while (!mCallbacks.empty() && mCallbacks.top().dueTime < now) {
152 callbacks.push_back(mCallbacks.top());
153 mCallbacks.pop();
154 }
155 }
156 for (const auto& cb : callbacks) {
157 cb.callback(timestamp, cb.data);
158 }
159}
160
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800161void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
162 ALOGV("choreographer %p ~ received hotplug event (displayId=%"
163 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", connected=%s), ignoring.",
164 this, displayId, toString(connected));
Michael Wright63c168a2015-12-04 17:59:42 +0000165}
166
167void Choreographer::handleMessage(const Message& message) {
168 switch (message.what) {
169 case MSG_SCHEDULE_CALLBACKS:
170 scheduleCallbacks();
171 break;
172 case MSG_SCHEDULE_VSYNC:
173 scheduleVsync();
174 break;
175 }
176}
177
178}
179
180/* Glue for the NDK interface */
181
182using android::Choreographer;
183
184static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
185 return reinterpret_cast<Choreographer*>(choreographer);
186}
187
188static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
189 return reinterpret_cast<AChoreographer*>(choreographer);
190}
191
192AChoreographer* AChoreographer_getInstance() {
193 return Choreographer_to_AChoreographer(Choreographer::getForThread());
194}
195
196void AChoreographer_postFrameCallback(AChoreographer* choreographer,
197 AChoreographer_frameCallback callback, void* data) {
198 AChoreographer_to_Choreographer(choreographer)->postFrameCallback(callback, data);
199}
200void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
201 AChoreographer_frameCallback callback, void* data, long delayMillis) {
202 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
203 callback, data, ms2ns(delayMillis));
204}