blob: 100dbfa8aa83f1ae2f7f6abb13c6bf77dd495562 [file] [log] [blame]
Marissa Walle2ffb422018-10-12 11:33:52 -07001/*
2 * Copyright 2018 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#pragma once
18
19#include <condition_variable>
20#include <deque>
Sally Qi59a9f502021-10-12 18:53:23 +000021#include <future>
Vladimir Marko21092702021-10-12 12:43:47 +000022#include <mutex>
Sally Qi59a9f502021-10-12 18:53:23 +000023#include <queue>
Marissa Walle2ffb422018-10-12 11:33:52 -070024#include <thread>
25#include <unordered_map>
Marissa Wallefb71af2019-06-27 14:45:53 -070026#include <unordered_set>
Marissa Walle2ffb422018-10-12 11:33:52 -070027
28#include <android-base/thread_annotations.h>
29
30#include <binder/IBinder.h>
31#include <gui/ITransactionCompletedListener.h>
Sally Qi59a9f502021-10-12 18:53:23 +000032#include <renderengine/RenderEngine.h>
Marissa Wallfda30bb2018-10-12 11:34:28 -070033#include <ui/Fence.h>
Marissa Walle2ffb422018-10-12 11:33:52 -070034
35namespace android {
36
37class CallbackHandle : public RefBase {
38public:
Valerie Hau5de3ad22019-08-20 07:47:43 -070039 CallbackHandle(const sp<IBinder>& transactionListener, const std::vector<CallbackId>& ids,
40 const sp<IBinder>& sc);
Marissa Walle2ffb422018-10-12 11:33:52 -070041
Valerie Hau5de3ad22019-08-20 07:47:43 -070042 sp<IBinder> listener;
Marissa Walle2ffb422018-10-12 11:33:52 -070043 std::vector<CallbackId> callbackIds;
Marissa Wall0e24a832019-07-10 15:32:50 -070044 wp<IBinder> surfaceControl;
Marissa Wallfda30bb2018-10-12 11:34:28 -070045
46 bool releasePreviousBuffer = false;
Sally Qi59a9f502021-10-12 18:53:23 +000047 std::string name;
Marissa Wall5a68a772018-12-22 17:43:42 -080048 sp<Fence> previousReleaseFence;
Sally Qi59a9f502021-10-12 18:53:23 +000049 std::vector<std::shared_future<renderengine::RenderEngineResult>> previousReleaseFences;
Marissa Wallfda30bb2018-10-12 11:34:28 -070050 nsecs_t acquireTime = -1;
Marissa Wall5a68a772018-12-22 17:43:42 -080051 nsecs_t latchTime = -1;
Valerie Hau32cdc1f2019-10-21 14:45:54 -070052 uint32_t transformHint = 0;
Ady Abraham899dcdb2021-06-15 16:56:21 -070053 uint32_t currentMaxAcquiredBufferCount = 0;
Valerie Hau871d6352020-01-29 08:44:02 -080054 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
55 CompositorTiming compositorTiming;
56 nsecs_t refreshStartTime = 0;
57 nsecs_t dequeueReadyTime = 0;
58 uint64_t frameNumber = 0;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -070059 ReleaseCallbackId previousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
Marissa Walle2ffb422018-10-12 11:33:52 -070060};
61
Robert Carr9a803c32021-01-14 16:57:58 -080062class TransactionCallbackInvoker {
Marissa Walle2ffb422018-10-12 11:33:52 -070063public:
Robert Carr3ec2b852021-09-28 17:46:57 -070064 TransactionCallbackInvoker();
65 ~TransactionCallbackInvoker();
66
Robert Carr3d1047b2021-09-20 18:22:32 -070067 status_t addCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
Robert Carr3ec2b852021-09-28 17:46:57 -070068 const std::vector<JankData>& jankData);
Robert Carr3d1047b2021-09-20 18:22:32 -070069 status_t addOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070070 std::deque<sp<CallbackHandle>>& outRemainingHandles);
Marissa Walle2ffb422018-10-12 11:33:52 -070071
72 // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and
73 // presented this frame.
Marissa Wallefb71af2019-06-27 14:45:53 -070074 status_t registerUnpresentedCallbackHandle(const sp<CallbackHandle>& handle);
Robert Carr3d1047b2021-09-20 18:22:32 -070075 void addEmptyTransaction(const ListenerCallbacks& listenerCallbacks);
Marissa Wall3dad52d2019-03-22 14:03:19 -070076
Marissa Wallfda30bb2018-10-12 11:34:28 -070077 void addPresentFence(const sp<Fence>& presentFence);
78
Marissa Walle2ffb422018-10-12 11:33:52 -070079 void sendCallbacks();
Robert Carr3d1047b2021-09-20 18:22:32 -070080 void clearCompletedTransactions() {
81 mCompletedTransactions.clear();
82 }
Marissa Walld600d572019-03-26 15:38:50 -070083
Jorim Jaggi9c03b502020-11-24 23:51:31 +010084 status_t addCallbackHandle(const sp<CallbackHandle>& handle,
Robert Carr3d1047b2021-09-20 18:22:32 -070085 const std::vector<JankData>& jankData);
Marissa Walle2ffb422018-10-12 11:33:52 -070086
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070087
Robert Carr3d1047b2021-09-20 18:22:32 -070088private:
89 status_t findTransactionStats(const sp<IBinder>& listener,
90 const std::vector<CallbackId>& callbackIds,
91 TransactionStats** outTransactionStats);
Marissa Walle2ffb422018-10-12 11:33:52 -070092
Marissa Walle2ffb422018-10-12 11:33:52 -070093
Marissa Wallefb71af2019-06-27 14:45:53 -070094
Valerie Hau5de3ad22019-08-20 07:47:43 -070095 std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash>
Robert Carr3d1047b2021-09-20 18:22:32 -070096 mCompletedTransactions;
Marissa Walle2ffb422018-10-12 11:33:52 -070097
Robert Carr3d1047b2021-09-20 18:22:32 -070098 sp<Fence> mPresentFence;
Robert Carr3ec2b852021-09-28 17:46:57 -070099
100 std::mutex mCallbackThreadMutex;
101 std::condition_variable mCallbackConditionVariable;
102 std::thread mThread;
103 bool mKeepRunning = true;
104 std::queue<std::function<void()>> mCallbackThreadWork;
Marissa Walle2ffb422018-10-12 11:33:52 -0700105};
106
107} // namespace android