blob: dd8e714e2359b000c2b736f5051243fe3518eba8 [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,
41 int bufferCount, bool controlledByApp)
42 : BufferItemConsumer(consumer, consumerUsage, bufferCount, controlledByApp),
43 mCurrentlyConnected(false),
Hongguang Chen621ec582021-02-16 15:42:35 -080044 mPreviouslyConnected(false),
45 mBLASTBufferQueue(nullptr) {}
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 void setBlastBufferQueue(BLASTBufferQueue* blastbufferqueue) REQUIRES(mMutex);
57
58protected:
59 void onSidebandStreamChanged() override REQUIRES(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080060
61private:
62 uint64_t mCurrentFrameNumber = 0;
63
Hongguang Chen621ec582021-02-16 15:42:35 -080064 Mutex mMutex;
65 ConsumerFrameEventHistory mFrameEventHistory GUARDED_BY(mMutex);
66 std::queue<uint64_t> mDisconnectEvents GUARDED_BY(mMutex);
67 bool mCurrentlyConnected GUARDED_BY(mMutex);
68 bool mPreviouslyConnected GUARDED_BY(mMutex);
69 BLASTBufferQueue* mBLASTBufferQueue 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 Nairdab94092020-09-29 16:09:04 -070076 BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width,
chaviw565ee542021-01-14 10:21:23 -080077 int height, int32_t format, bool enableTripleBuffering = true);
Robert Carrcedef052020-04-22 15:58:23 -070078
Robert Carr78c25dd2019-08-15 14:10:33 -070079 sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
80 return mProducer;
81 }
Vishnu Nair992496b2020-10-22 17:27:21 -070082 sp<Surface> getSurface(bool includeSurfaceControlHandle);
Robert Carr78c25dd2019-08-15 14:10:33 -070083
84 void onBufferFreed(const wp<GraphicBuffer>&/* graphicBuffer*/) override { /* TODO */ }
Vishnu Nairaef1de92020-10-22 12:15:53 -070085 void onFrameReplaced(const BufferItem& item) override;
Robert Carr78c25dd2019-08-15 14:10:33 -070086 void onFrameAvailable(const BufferItem& item) override;
Vishnu Nairadf632b2021-01-07 14:05:08 -080087 void onFrameDequeued(const uint64_t) override;
88 void onFrameCancelled(const uint64_t) override;
Robert Carr78c25dd2019-08-15 14:10:33 -070089
90 void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
91 const std::vector<SurfaceControlStats>& stats);
92 void setNextTransaction(SurfaceComposerClient::Transaction *t);
Vishnu Nairc4a40c12020-12-23 09:14:32 -080093 void mergeWithNextTransaction(SurfaceComposerClient::Transaction* t, uint64_t frameNumber);
chaviw71c2cc42020-10-23 16:42:02 -070094 void setTransactionCompleteCallback(uint64_t frameNumber,
95 std::function<void(int64_t)>&& transactionCompleteCallback);
Robert Carr78c25dd2019-08-15 14:10:33 -070096
chaviw565ee542021-01-14 10:21:23 -080097 void update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height, int32_t format);
Vishnu Nair7eb670a2020-10-15 12:16:10 -070098 void flushShadowQueue() { mFlushShadowQueue = true; }
Robert Carr78c25dd2019-08-15 14:10:33 -070099
Marin Shalamanov46084422020-10-13 12:33:42 +0200100 status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless);
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000101 status_t setFrameTimelineInfo(const FrameTimelineInfo& info);
Robert Carr9c006e02020-10-14 13:41:57 -0700102
Hongguang Chen621ec582021-02-16 15:42:35 -0800103 void setSidebandStream(const sp<NativeHandle>& stream);
104
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800105 virtual ~BLASTBufferQueue();
Robert Carr78c25dd2019-08-15 14:10:33 -0700106
107private:
Valerie Hauc5011f92019-10-11 09:52:07 -0700108 friend class BLASTBufferQueueHelper;
109
Robert Carr78c25dd2019-08-15 14:10:33 -0700110 // can't be copied
111 BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs);
112 BLASTBufferQueue(const BLASTBufferQueue& rhs);
Vishnu Nair89496122020-12-14 17:14:53 -0800113 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
114 sp<IGraphicBufferConsumer>* outConsumer);
Robert Carr78c25dd2019-08-15 14:10:33 -0700115
Robert Carr255acdc2020-04-17 14:08:55 -0700116 void processNextBufferLocked(bool useNextTransaction) REQUIRES(mMutex);
Vishnu Nairbf255772020-10-16 10:54:41 -0700117 Rect computeCrop(const BufferItem& item) REQUIRES(mMutex);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700118 // 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 -0800119 bool rejectBuffer(const BufferItem& item) REQUIRES(mMutex);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800120 bool maxBuffersAcquired(bool includeExtraAcquire) const REQUIRES(mMutex);
chaviw497e81c2021-02-04 17:09:47 -0800121 static PixelFormat convertBufferFormat(PixelFormat& format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700122
Vishnu Nairdab94092020-09-29 16:09:04 -0700123 std::string mName;
Valerie Haud3b90d22019-11-06 09:37:31 -0800124 sp<SurfaceControl> mSurfaceControl;
125
126 std::mutex mMutex;
127 std::condition_variable mCallbackCV;
Valerie Haud3b90d22019-11-06 09:37:31 -0800128
Valerie Hau65b8e872020-02-13 09:45:14 -0800129 // BufferQueue internally allows 1 more than
130 // the max to be acquired
131 static const int MAX_ACQUIRED_BUFFERS = 1;
Valerie Haua32c5522019-12-09 10:11:08 -0800132
133 int32_t mNumFrameAvailable GUARDED_BY(mMutex);
134 int32_t mNumAcquired GUARDED_BY(mMutex);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700135 bool mInitialCallbackReceived GUARDED_BY(mMutex) = false;
Valerie Haua32c5522019-12-09 10:11:08 -0800136 struct PendingReleaseItem {
137 BufferItem item;
138 sp<Fence> releaseFence;
Robert Carr78c25dd2019-08-15 14:10:33 -0700139 };
Robert Carr78c25dd2019-08-15 14:10:33 -0700140
Valerie Haua32c5522019-12-09 10:11:08 -0800141 std::queue<const BufferItem> mSubmitted GUARDED_BY(mMutex);
Vishnu Nairdab94092020-09-29 16:09:04 -0700142 // Keep a reference to the currently presented buffer so we can release it when the next buffer
143 // is ready to be presented.
Valerie Haua32c5522019-12-09 10:11:08 -0800144 PendingReleaseItem mPendingReleaseItem GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700145
Vishnu Nairea0de002020-11-17 17:42:37 -0800146 ui::Size mSize GUARDED_BY(mMutex);
147 ui::Size mRequestedSize GUARDED_BY(mMutex);
chaviw565ee542021-01-14 10:21:23 -0800148 int32_t mFormat GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700149
Valerie Haua32c5522019-12-09 10:11:08 -0800150 uint32_t mTransformHint GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700151
152 sp<IGraphicBufferConsumer> mConsumer;
153 sp<IGraphicBufferProducer> mProducer;
Valerie Hau871d6352020-01-29 08:44:02 -0800154 sp<BLASTBufferItemConsumer> mBufferItemConsumer;
Robert Carr78c25dd2019-08-15 14:10:33 -0700155
Valerie Haud3b90d22019-11-06 09:37:31 -0800156 SurfaceComposerClient::Transaction* mNextTransaction GUARDED_BY(mMutex);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800157 std::vector<std::tuple<uint64_t /* framenumber */, SurfaceComposerClient::Transaction>>
158 mPendingTransactions GUARDED_BY(mMutex);
159
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700160 // If set to true, the next queue buffer will wait until the shadow queue has been processed by
161 // the adapter.
162 bool mFlushShadowQueue = false;
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800163 // Last requested auto refresh state set by the producer. The state indicates that the consumer
164 // should acquire the next frame as soon as it can and not wait for a frame to become available.
165 // This is only relevant for shared buffer mode.
166 bool mAutoRefresh GUARDED_BY(mMutex) = false;
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100167
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000168 std::queue<FrameTimelineInfo> mNextFrameTimelineInfoQueue GUARDED_BY(mMutex);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800169
170 // Last acquired buffer's scaling mode. This is used to check if we should update the blast
171 // layer size immediately or wait until we get the next buffer. This will support scenarios
172 // where the layer can change sizes and the buffer will scale to fit the new size.
173 uint32_t mLastBufferScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
chaviw71c2cc42020-10-23 16:42:02 -0700174
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800175 // Tracks the last acquired frame number
176 uint64_t mLastAcquiredFrameNumber GUARDED_BY(mMutex) = 0;
177
chaviw71c2cc42020-10-23 16:42:02 -0700178 std::function<void(int64_t)> mTransactionCompleteCallback GUARDED_BY(mMutex) = nullptr;
179 uint64_t mTransactionCompleteFrameNumber GUARDED_BY(mMutex){0};
Vishnu Nair277142c2021-01-05 18:35:29 -0800180
181 // Queues up transactions using this token in SurfaceFlinger. This prevents queued up
182 // transactions from other parts of the client from blocking this transaction.
Vishnu Nairadf632b2021-01-07 14:05:08 -0800183 const sp<IBinder> mApplyToken GUARDED_BY(mMutex) = new BBinder();
184
185 // Guards access to mDequeueTimestamps since we cannot hold to mMutex in onFrameDequeued or
186 // we will deadlock.
187 std::mutex mTimestampMutex;
188 // Tracks buffer dequeue times by the client. This info is sent to SurfaceFlinger which uses
189 // it for debugging purposes.
190 std::unordered_map<uint64_t /* bufferId */, nsecs_t> mDequeueTimestamps
191 GUARDED_BY(mTimestampMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700192};
193
194} // namespace android
195
196#endif // ANDROID_GUI_SURFACE_H