blob: 9d287910a52fe9f23fa52def9361236956535336 [file] [log] [blame]
Robert Carr78c25dd2019-08-15 14:10:33 -07001/*
2 * Copyright (C) 2019 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#ifndef ANDROID_GUI_BLAST_BUFFER_QUEUE_H
18#define ANDROID_GUI_BLAST_BUFFER_QUEUE_H
19
20#include <gui/IGraphicBufferProducer.h>
21#include <gui/BufferItemConsumer.h>
22#include <gui/BufferItem.h>
23#include <gui/SurfaceComposerClient.h>
24
25#include <utils/Condition.h>
26#include <utils/Mutex.h>
27#include <utils/RefBase.h>
28
29#include <system/window.h>
Valerie Haud3b90d22019-11-06 09:37:31 -080030#include <thread>
Jorim Jaggia3fe67b2020-12-01 00:24:33 +010031#include <queue>
Robert Carr78c25dd2019-08-15 14:10:33 -070032
33namespace android {
34
Hongguang Chen621ec582021-02-16 15:42:35 -080035class BLASTBufferQueue;
Robert Carr78c25dd2019-08-15 14:10:33 -070036class BufferItemConsumer;
37
Valerie Hau871d6352020-01-29 08:44:02 -080038class BLASTBufferItemConsumer : public BufferItemConsumer {
39public:
40 BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
Ady Abrahamdbca1352021-12-15 11:58:56 -080041 int bufferCount, bool controlledByApp, wp<BLASTBufferQueue> bbq)
Valerie Hau871d6352020-01-29 08:44:02 -080042 : BufferItemConsumer(consumer, consumerUsage, bufferCount, controlledByApp),
Ady Abrahamdbca1352021-12-15 11:58:56 -080043 mBLASTBufferQueue(std::move(bbq)),
Valerie Hau871d6352020-01-29 08:44:02 -080044 mCurrentlyConnected(false),
Ady Abrahamdbca1352021-12-15 11:58:56 -080045 mPreviouslyConnected(false) {}
Valerie Hau871d6352020-01-29 08:44:02 -080046
47 void onDisconnect() override;
48 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
Hongguang Chen621ec582021-02-16 15:42:35 -080049 FrameEventHistoryDelta* outDelta) override REQUIRES(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080050 void updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
51 const sp<Fence>& gpuCompositionDoneFence,
52 const sp<Fence>& presentFence, const sp<Fence>& prevReleaseFence,
53 CompositorTiming compositorTiming, nsecs_t latchTime,
Hongguang Chen621ec582021-02-16 15:42:35 -080054 nsecs_t dequeueReadyTime) REQUIRES(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080055 void getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect);
Hongguang Chen621ec582021-02-16 15:42:35 -080056
57protected:
58 void onSidebandStreamChanged() override REQUIRES(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080059
60private:
Ady Abrahamdbca1352021-12-15 11:58:56 -080061 const wp<BLASTBufferQueue> mBLASTBufferQueue;
62
Valerie Hau871d6352020-01-29 08:44:02 -080063 uint64_t mCurrentFrameNumber = 0;
64
Hongguang Chen621ec582021-02-16 15:42:35 -080065 Mutex mMutex;
66 ConsumerFrameEventHistory mFrameEventHistory GUARDED_BY(mMutex);
67 std::queue<uint64_t> mDisconnectEvents GUARDED_BY(mMutex);
68 bool mCurrentlyConnected GUARDED_BY(mMutex);
69 bool mPreviouslyConnected GUARDED_BY(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080070};
71
Robert Carr78c25dd2019-08-15 14:10:33 -070072class BLASTBufferQueue
73 : public ConsumerBase::FrameAvailableListener, public BufferItemConsumer::BufferFreedListener
74{
75public:
Vishnu Naird2aaab12022-02-10 14:49:09 -080076 BLASTBufferQueue(const std::string& name, bool updateDestinationFrame = true);
Vishnu Nairdab94092020-09-29 16:09:04 -070077 BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width,
Vishnu Nairdebd1cb2021-03-16 10:06:01 -070078 int height, int32_t format);
Robert Carrcedef052020-04-22 15:58:23 -070079
Robert Carr78c25dd2019-08-15 14:10:33 -070080 sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
81 return mProducer;
82 }
Vishnu Nair992496b2020-10-22 17:27:21 -070083 sp<Surface> getSurface(bool includeSurfaceControlHandle);
Vishnu Nair1e8bf102021-12-28 14:36:59 -080084 bool isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const;
Robert Carr78c25dd2019-08-15 14:10:33 -070085
86 void onBufferFreed(const wp<GraphicBuffer>&/* graphicBuffer*/) override { /* TODO */ }
Vishnu Nairaef1de92020-10-22 12:15:53 -070087 void onFrameReplaced(const BufferItem& item) override;
Robert Carr78c25dd2019-08-15 14:10:33 -070088 void onFrameAvailable(const BufferItem& item) override;
Vishnu Nairadf632b2021-01-07 14:05:08 -080089 void onFrameDequeued(const uint64_t) override;
90 void onFrameCancelled(const uint64_t) override;
Robert Carr78c25dd2019-08-15 14:10:33 -070091
chaviw6b9ffea2021-11-08 09:25:48 -060092 void transactionCommittedCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
93 const std::vector<SurfaceControlStats>& stats);
94 virtual void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
95 const std::vector<SurfaceControlStats>& stats);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -070096 void releaseBufferCallback(const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
chaviw69058fb2021-09-27 09:37:30 -050097 std::optional<uint32_t> currentMaxAcquiredBufferCount);
Tianhao Yao4861b102022-02-03 20:18:35 +000098 void syncNextTransaction(std::function<void(SurfaceComposerClient::Transaction*)> callback,
99 bool acquireSingleBuffer = true);
100 void stopContinuousSyncTransaction();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800101 void mergeWithNextTransaction(SurfaceComposerClient::Transaction* t, uint64_t frameNumber);
chaviw6a195272021-09-03 16:14:25 -0500102 void applyPendingTransactions(uint64_t frameNumber);
chaviwd84085a2022-02-08 11:07:04 -0600103 SurfaceComposerClient::Transaction* gatherPendingTransactions(uint64_t frameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700104
Vishnu Naird2aaab12022-02-10 14:49:09 -0800105 void update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height, int32_t format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700106
Marin Shalamanov46084422020-10-13 12:33:42 +0200107 status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless);
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000108 status_t setFrameTimelineInfo(const FrameTimelineInfo& info);
Robert Carr9c006e02020-10-14 13:41:57 -0700109
Hongguang Chen621ec582021-02-16 15:42:35 -0800110 void setSidebandStream(const sp<NativeHandle>& stream);
111
Robert Carr82d07c92021-05-10 11:36:43 -0700112 uint32_t getLastTransformHint() const;
chaviw0b020f82021-08-20 12:00:47 -0500113 uint64_t getLastAcquiredFrameNum();
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800114 void abandon();
Robert Carr82d07c92021-05-10 11:36:43 -0700115
Robert Carr4c1b6462021-12-21 10:30:50 -0800116 /**
117 * Set a callback to be invoked when we are hung. The boolean parameter
118 * indicates whether the hang is due to an unfired fence.
119 * TODO: The boolean is always true atm, unfired fence is
120 * the only case we detect.
121 */
122 void setTransactionHangCallback(std::function<void(bool)> callback);
123
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800124 virtual ~BLASTBufferQueue();
Robert Carr78c25dd2019-08-15 14:10:33 -0700125
126private:
Valerie Hauc5011f92019-10-11 09:52:07 -0700127 friend class BLASTBufferQueueHelper;
128
Robert Carr78c25dd2019-08-15 14:10:33 -0700129 // can't be copied
130 BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs);
131 BLASTBufferQueue(const BLASTBufferQueue& rhs);
Vishnu Nair89496122020-12-14 17:14:53 -0800132 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
133 sp<IGraphicBufferConsumer>* outConsumer);
Robert Carr78c25dd2019-08-15 14:10:33 -0700134
chaviwd7deef72021-10-06 11:53:40 -0500135 void acquireNextBufferLocked(
136 const std::optional<SurfaceComposerClient::Transaction*> transaction) REQUIRES(mMutex);
Vishnu Nairbf255772020-10-16 10:54:41 -0700137 Rect computeCrop(const BufferItem& item) REQUIRES(mMutex);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700138 // Return true if we need to reject the buffer based on the scaling mode and the buffer size.
Vishnu Nairea0de002020-11-17 17:42:37 -0800139 bool rejectBuffer(const BufferItem& item) REQUIRES(mMutex);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800140 bool maxBuffersAcquired(bool includeExtraAcquire) const REQUIRES(mMutex);
chaviw497e81c2021-02-04 17:09:47 -0800141 static PixelFormat convertBufferFormat(PixelFormat& format);
chaviw6a195272021-09-03 16:14:25 -0500142 void mergePendingTransactions(SurfaceComposerClient::Transaction* t, uint64_t frameNumber)
143 REQUIRES(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700144
chaviwd7deef72021-10-06 11:53:40 -0500145 void flushShadowQueue() REQUIRES(mMutex);
146 void acquireAndReleaseBuffer() REQUIRES(mMutex);
chaviw0acd33a2021-11-02 11:55:37 -0500147 void releaseBuffer(const ReleaseCallbackId& callbackId, const sp<Fence>& releaseFence)
148 REQUIRES(mMutex);
149 void flushAndWaitForFreeBuffer(std::unique_lock<std::mutex>& lock);
chaviwd7deef72021-10-06 11:53:40 -0500150
Vishnu Nairdab94092020-09-29 16:09:04 -0700151 std::string mName;
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700152 // Represents the queued buffer count from buffer queue,
153 // pre-BLAST. This is mNumFrameAvailable (buffers that queued to blast) +
154 // mNumAcquired (buffers that queued to SF) mPendingRelease.size() (buffers that are held by
155 // blast). This counter is read by android studio profiler.
156 std::string mQueuedBufferTrace;
Valerie Haud3b90d22019-11-06 09:37:31 -0800157 sp<SurfaceControl> mSurfaceControl;
158
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800159 mutable std::mutex mMutex;
Valerie Haud3b90d22019-11-06 09:37:31 -0800160 std::condition_variable mCallbackCV;
Valerie Haud3b90d22019-11-06 09:37:31 -0800161
Valerie Hau65b8e872020-02-13 09:45:14 -0800162 // BufferQueue internally allows 1 more than
163 // the max to be acquired
Ady Abraham0bde6b52021-05-18 13:57:02 -0700164 int32_t mMaxAcquiredBuffers = 1;
Valerie Haua32c5522019-12-09 10:11:08 -0800165
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800166 int32_t mNumFrameAvailable GUARDED_BY(mMutex) = 0;
167 int32_t mNumAcquired GUARDED_BY(mMutex) = 0;
Robert Carr78c25dd2019-08-15 14:10:33 -0700168
Vishnu Nair1506b182021-02-22 14:35:15 -0800169 // Keep a reference to the submitted buffers so we can release when surfaceflinger drops the
170 // buffer or the buffer has been presented and a new buffer is ready to be presented.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700171 std::unordered_map<ReleaseCallbackId, BufferItem, ReleaseBufferCallbackIdHash> mSubmitted
172 GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700173
Ady Abraham899dcdb2021-06-15 16:56:21 -0700174 // Keep a queue of the released buffers instead of immediately releasing
175 // the buffers back to the buffer queue. This would be controlled by SF
176 // setting the max acquired buffer count.
177 struct ReleasedBuffer {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700178 ReleaseCallbackId callbackId;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700179 sp<Fence> releaseFence;
180 };
181 std::deque<ReleasedBuffer> mPendingRelease GUARDED_BY(mMutex);
182
Vishnu Nairea0de002020-11-17 17:42:37 -0800183 ui::Size mSize GUARDED_BY(mMutex);
184 ui::Size mRequestedSize GUARDED_BY(mMutex);
chaviw565ee542021-01-14 10:21:23 -0800185 int32_t mFormat GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700186
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000187 struct BufferInfo {
188 bool hasBuffer = false;
189 uint32_t width;
190 uint32_t height;
191 uint32_t transform;
192 // This is used to check if we should update the blast layer size immediately or wait until
193 // we get the next buffer. This will support scenarios where the layer can change sizes
194 // and the buffer will scale to fit the new size.
195 uint32_t scalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700196 Rect crop;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000197
198 void update(bool hasBuffer, uint32_t width, uint32_t height, uint32_t transform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700199 uint32_t scalingMode, const Rect& crop) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000200 this->hasBuffer = hasBuffer;
201 this->width = width;
202 this->height = height;
203 this->transform = transform;
204 this->scalingMode = scalingMode;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700205 if (!crop.isEmpty()) {
206 this->crop = crop;
207 } else {
208 this->crop = Rect(width, height);
209 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000210 }
211 };
212
213 // Last acquired buffer's info. This is used to calculate the correct scale when size change is
214 // requested. We need to use the old buffer's info to determine what scale we need to apply to
215 // ensure the correct size.
216 BufferInfo mLastBufferInfo GUARDED_BY(mMutex);
217 void setMatrix(SurfaceComposerClient::Transaction* t, const BufferInfo& bufferInfo)
218 REQUIRES(mMutex);
219
Valerie Haua32c5522019-12-09 10:11:08 -0800220 uint32_t mTransformHint GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700221
222 sp<IGraphicBufferConsumer> mConsumer;
223 sp<IGraphicBufferProducer> mProducer;
Valerie Hau871d6352020-01-29 08:44:02 -0800224 sp<BLASTBufferItemConsumer> mBufferItemConsumer;
Robert Carr78c25dd2019-08-15 14:10:33 -0700225
Tianhao Yao4861b102022-02-03 20:18:35 +0000226 std::function<void(SurfaceComposerClient::Transaction*)> mTransactionReadyCallback
227 GUARDED_BY(mMutex);
chaviwa1c4c822021-11-10 18:11:58 -0600228 SurfaceComposerClient::Transaction* mSyncTransaction GUARDED_BY(mMutex);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800229 std::vector<std::tuple<uint64_t /* framenumber */, SurfaceComposerClient::Transaction>>
230 mPendingTransactions GUARDED_BY(mMutex);
231
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000232 std::queue<FrameTimelineInfo> mNextFrameTimelineInfoQueue GUARDED_BY(mMutex);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800233
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800234 // Tracks the last acquired frame number
235 uint64_t mLastAcquiredFrameNumber GUARDED_BY(mMutex) = 0;
236
Vishnu Nair277142c2021-01-05 18:35:29 -0800237 // Queues up transactions using this token in SurfaceFlinger. This prevents queued up
238 // transactions from other parts of the client from blocking this transaction.
Vishnu Nairadf632b2021-01-07 14:05:08 -0800239 const sp<IBinder> mApplyToken GUARDED_BY(mMutex) = new BBinder();
240
241 // Guards access to mDequeueTimestamps since we cannot hold to mMutex in onFrameDequeued or
242 // we will deadlock.
243 std::mutex mTimestampMutex;
244 // Tracks buffer dequeue times by the client. This info is sent to SurfaceFlinger which uses
245 // it for debugging purposes.
246 std::unordered_map<uint64_t /* bufferId */, nsecs_t> mDequeueTimestamps
247 GUARDED_BY(mTimestampMutex);
chaviw42026162021-04-16 15:46:12 -0500248
249 // Keep track of SurfaceControls that have submitted a transaction and BBQ is waiting on a
250 // callback for them.
251 std::queue<sp<SurfaceControl>> mSurfaceControlsWithPendingCallback GUARDED_BY(mMutex);
chaviw69058fb2021-09-27 09:37:30 -0500252
253 uint32_t mCurrentMaxAcquiredBufferCount;
chaviw0acd33a2021-11-02 11:55:37 -0500254
255 // Flag to determine if syncTransaction should only acquire a single buffer and then clear or
256 // continue to acquire buffers until explicitly cleared
257 bool mAcquireSingleBuffer GUARDED_BY(mMutex) = true;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800258
259 // True if BBQ will update the destination frame used to scale the buffer to the requested size.
260 // If false, the caller is responsible for updating the destination frame on the BBQ
261 // surfacecontol. This is useful if the caller wants to synchronize the buffer scale with
262 // additional scales in the hierarchy.
263 bool mUpdateDestinationFrame GUARDED_BY(mMutex) = true;
Robert Carr79dc06a2022-02-22 15:28:59 -0800264
265 // We send all transactions on our apply token over one-way binder calls to avoid blocking
266 // client threads. All of our transactions remain in order, since they are one-way binder calls
267 // from a single process, to a single interface. However once we give up a Transaction for sync
268 // we can start to have ordering issues. When we return from sync to normal frame production,
269 // we wait on the commit callback of sync frames ensuring ordering, however we don't want to
270 // wait on the commit callback for every normal frame (since even emitting them has a
271 // performance cost) this means we need a method to ensure frames are in order when switching
272 // from one-way application on our apply token, to application on some other apply token. We
273 // make use of setBufferHasBarrier to declare this ordering. This boolean simply tracks when we
274 // need to set this flag, notably only in the case where we are transitioning from a previous
275 // transaction applied by us (one way, may not yet have reached server) and an upcoming
276 // transaction that will be applied by some sync consumer.
277 bool mAppliedLastTransaction = false;
278 uint64_t mLastAppliedFrameNumber = 0;
Robert Carr4c1b6462021-12-21 10:30:50 -0800279
280 std::function<void(bool)> mTransactionHangCallback;
chaviwc1cf4022022-06-03 13:32:33 -0500281
282 std::unordered_set<uint64_t> mSyncedFrameNumbers GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700283};
284
285} // namespace android
286
287#endif // ANDROID_GUI_SURFACE_H