blob: 74addeac4aa5cd8310f0b7145bfe1a3350302301 [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);
chaviw0acd33a2021-11-02 11:55:37 -050098 void setSyncTransaction(SurfaceComposerClient::Transaction* t, bool acquireSingleBuffer = true);
Vishnu Nairc4a40c12020-12-23 09:14:32 -080099 void mergeWithNextTransaction(SurfaceComposerClient::Transaction* t, uint64_t frameNumber);
chaviw6a195272021-09-03 16:14:25 -0500100 void applyPendingTransactions(uint64_t frameNumber);
chaviwd84085a2022-02-08 11:07:04 -0600101 SurfaceComposerClient::Transaction* gatherPendingTransactions(uint64_t frameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700102
Vishnu Naird2aaab12022-02-10 14:49:09 -0800103 void update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height, int32_t format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700104
Marin Shalamanov46084422020-10-13 12:33:42 +0200105 status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless);
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000106 status_t setFrameTimelineInfo(const FrameTimelineInfo& info);
Robert Carr9c006e02020-10-14 13:41:57 -0700107
Hongguang Chen621ec582021-02-16 15:42:35 -0800108 void setSidebandStream(const sp<NativeHandle>& stream);
109
Robert Carr82d07c92021-05-10 11:36:43 -0700110 uint32_t getLastTransformHint() const;
chaviw0b020f82021-08-20 12:00:47 -0500111 uint64_t getLastAcquiredFrameNum();
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800112 void abandon();
Robert Carr82d07c92021-05-10 11:36:43 -0700113
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800114 virtual ~BLASTBufferQueue();
Robert Carr78c25dd2019-08-15 14:10:33 -0700115
116private:
Valerie Hauc5011f92019-10-11 09:52:07 -0700117 friend class BLASTBufferQueueHelper;
118
Robert Carr78c25dd2019-08-15 14:10:33 -0700119 // can't be copied
120 BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs);
121 BLASTBufferQueue(const BLASTBufferQueue& rhs);
Vishnu Nair89496122020-12-14 17:14:53 -0800122 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
123 sp<IGraphicBufferConsumer>* outConsumer);
Robert Carr78c25dd2019-08-15 14:10:33 -0700124
chaviwd7deef72021-10-06 11:53:40 -0500125 void acquireNextBufferLocked(
126 const std::optional<SurfaceComposerClient::Transaction*> transaction) REQUIRES(mMutex);
Vishnu Nairbf255772020-10-16 10:54:41 -0700127 Rect computeCrop(const BufferItem& item) REQUIRES(mMutex);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700128 // 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 -0800129 bool rejectBuffer(const BufferItem& item) REQUIRES(mMutex);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800130 bool maxBuffersAcquired(bool includeExtraAcquire) const REQUIRES(mMutex);
chaviw497e81c2021-02-04 17:09:47 -0800131 static PixelFormat convertBufferFormat(PixelFormat& format);
chaviw6a195272021-09-03 16:14:25 -0500132 void mergePendingTransactions(SurfaceComposerClient::Transaction* t, uint64_t frameNumber)
133 REQUIRES(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700134
chaviwd7deef72021-10-06 11:53:40 -0500135 void flushShadowQueue() REQUIRES(mMutex);
136 void acquireAndReleaseBuffer() REQUIRES(mMutex);
chaviw0acd33a2021-11-02 11:55:37 -0500137 void releaseBuffer(const ReleaseCallbackId& callbackId, const sp<Fence>& releaseFence)
138 REQUIRES(mMutex);
139 void flushAndWaitForFreeBuffer(std::unique_lock<std::mutex>& lock);
chaviwd7deef72021-10-06 11:53:40 -0500140
Vishnu Nairdab94092020-09-29 16:09:04 -0700141 std::string mName;
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700142 // Represents the queued buffer count from buffer queue,
143 // pre-BLAST. This is mNumFrameAvailable (buffers that queued to blast) +
144 // mNumAcquired (buffers that queued to SF) mPendingRelease.size() (buffers that are held by
145 // blast). This counter is read by android studio profiler.
146 std::string mQueuedBufferTrace;
Valerie Haud3b90d22019-11-06 09:37:31 -0800147 sp<SurfaceControl> mSurfaceControl;
148
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800149 mutable std::mutex mMutex;
Valerie Haud3b90d22019-11-06 09:37:31 -0800150 std::condition_variable mCallbackCV;
Valerie Haud3b90d22019-11-06 09:37:31 -0800151
Valerie Hau65b8e872020-02-13 09:45:14 -0800152 // BufferQueue internally allows 1 more than
153 // the max to be acquired
Ady Abraham0bde6b52021-05-18 13:57:02 -0700154 int32_t mMaxAcquiredBuffers = 1;
Valerie Haua32c5522019-12-09 10:11:08 -0800155
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800156 int32_t mNumFrameAvailable GUARDED_BY(mMutex) = 0;
157 int32_t mNumAcquired GUARDED_BY(mMutex) = 0;
Robert Carr78c25dd2019-08-15 14:10:33 -0700158
Vishnu Nair1506b182021-02-22 14:35:15 -0800159 // Keep a reference to the submitted buffers so we can release when surfaceflinger drops the
160 // buffer or the buffer has been presented and a new buffer is ready to be presented.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700161 std::unordered_map<ReleaseCallbackId, BufferItem, ReleaseBufferCallbackIdHash> mSubmitted
162 GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700163
Ady Abraham899dcdb2021-06-15 16:56:21 -0700164 // Keep a queue of the released buffers instead of immediately releasing
165 // the buffers back to the buffer queue. This would be controlled by SF
166 // setting the max acquired buffer count.
167 struct ReleasedBuffer {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700168 ReleaseCallbackId callbackId;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700169 sp<Fence> releaseFence;
170 };
171 std::deque<ReleasedBuffer> mPendingRelease GUARDED_BY(mMutex);
172
Vishnu Nairea0de002020-11-17 17:42:37 -0800173 ui::Size mSize GUARDED_BY(mMutex);
174 ui::Size mRequestedSize GUARDED_BY(mMutex);
chaviw565ee542021-01-14 10:21:23 -0800175 int32_t mFormat GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700176
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000177 struct BufferInfo {
178 bool hasBuffer = false;
179 uint32_t width;
180 uint32_t height;
181 uint32_t transform;
182 // This is used to check if we should update the blast layer size immediately or wait until
183 // we get the next buffer. This will support scenarios where the layer can change sizes
184 // and the buffer will scale to fit the new size.
185 uint32_t scalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700186 Rect crop;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000187
188 void update(bool hasBuffer, uint32_t width, uint32_t height, uint32_t transform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700189 uint32_t scalingMode, const Rect& crop) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000190 this->hasBuffer = hasBuffer;
191 this->width = width;
192 this->height = height;
193 this->transform = transform;
194 this->scalingMode = scalingMode;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700195 if (!crop.isEmpty()) {
196 this->crop = crop;
197 } else {
198 this->crop = Rect(width, height);
199 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000200 }
201 };
202
203 // Last acquired buffer's info. This is used to calculate the correct scale when size change is
204 // requested. We need to use the old buffer's info to determine what scale we need to apply to
205 // ensure the correct size.
206 BufferInfo mLastBufferInfo GUARDED_BY(mMutex);
207 void setMatrix(SurfaceComposerClient::Transaction* t, const BufferInfo& bufferInfo)
208 REQUIRES(mMutex);
209
Valerie Haua32c5522019-12-09 10:11:08 -0800210 uint32_t mTransformHint GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700211
212 sp<IGraphicBufferConsumer> mConsumer;
213 sp<IGraphicBufferProducer> mProducer;
Valerie Hau871d6352020-01-29 08:44:02 -0800214 sp<BLASTBufferItemConsumer> mBufferItemConsumer;
Robert Carr78c25dd2019-08-15 14:10:33 -0700215
chaviwa1c4c822021-11-10 18:11:58 -0600216 SurfaceComposerClient::Transaction* mSyncTransaction GUARDED_BY(mMutex);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800217 std::vector<std::tuple<uint64_t /* framenumber */, SurfaceComposerClient::Transaction>>
218 mPendingTransactions GUARDED_BY(mMutex);
219
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000220 std::queue<FrameTimelineInfo> mNextFrameTimelineInfoQueue GUARDED_BY(mMutex);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800221
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800222 // Tracks the last acquired frame number
223 uint64_t mLastAcquiredFrameNumber GUARDED_BY(mMutex) = 0;
224
Vishnu Nair277142c2021-01-05 18:35:29 -0800225 // Queues up transactions using this token in SurfaceFlinger. This prevents queued up
226 // transactions from other parts of the client from blocking this transaction.
Vishnu Nairadf632b2021-01-07 14:05:08 -0800227 const sp<IBinder> mApplyToken GUARDED_BY(mMutex) = new BBinder();
228
229 // Guards access to mDequeueTimestamps since we cannot hold to mMutex in onFrameDequeued or
230 // we will deadlock.
231 std::mutex mTimestampMutex;
232 // Tracks buffer dequeue times by the client. This info is sent to SurfaceFlinger which uses
233 // it for debugging purposes.
234 std::unordered_map<uint64_t /* bufferId */, nsecs_t> mDequeueTimestamps
235 GUARDED_BY(mTimestampMutex);
chaviw42026162021-04-16 15:46:12 -0500236
237 // Keep track of SurfaceControls that have submitted a transaction and BBQ is waiting on a
238 // callback for them.
239 std::queue<sp<SurfaceControl>> mSurfaceControlsWithPendingCallback GUARDED_BY(mMutex);
chaviw69058fb2021-09-27 09:37:30 -0500240
241 uint32_t mCurrentMaxAcquiredBufferCount;
chaviw0acd33a2021-11-02 11:55:37 -0500242 bool mWaitForTransactionCallback GUARDED_BY(mMutex) = false;
243
244 // Flag to determine if syncTransaction should only acquire a single buffer and then clear or
245 // continue to acquire buffers until explicitly cleared
246 bool mAcquireSingleBuffer GUARDED_BY(mMutex) = true;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800247
248 // True if BBQ will update the destination frame used to scale the buffer to the requested size.
249 // If false, the caller is responsible for updating the destination frame on the BBQ
250 // surfacecontol. This is useful if the caller wants to synchronize the buffer scale with
251 // additional scales in the hierarchy.
252 bool mUpdateDestinationFrame GUARDED_BY(mMutex) = true;
Robert Carr78c25dd2019-08-15 14:10:33 -0700253};
254
255} // namespace android
256
257#endif // ANDROID_GUI_SURFACE_H