blob: 2300e81aa7234c40e829b32053f5e08684d6d324 [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>
Robert Carr78c25dd2019-08-15 14:10:33 -070031
32namespace android {
33
34class BufferItemConsumer;
35
Valerie Hau871d6352020-01-29 08:44:02 -080036class BLASTBufferItemConsumer : public BufferItemConsumer {
37public:
38 BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
39 int bufferCount, bool controlledByApp)
40 : BufferItemConsumer(consumer, consumerUsage, bufferCount, controlledByApp),
41 mCurrentlyConnected(false),
42 mPreviouslyConnected(false) {}
43
44 void onDisconnect() override;
45 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
46 FrameEventHistoryDelta* outDelta) override
47 REQUIRES(mFrameEventHistoryMutex);
48 void updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
49 const sp<Fence>& gpuCompositionDoneFence,
50 const sp<Fence>& presentFence, const sp<Fence>& prevReleaseFence,
51 CompositorTiming compositorTiming, nsecs_t latchTime,
52 nsecs_t dequeueReadyTime) REQUIRES(mFrameEventHistoryMutex);
53 void getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect);
54
55private:
56 uint64_t mCurrentFrameNumber = 0;
57
58 Mutex mFrameEventHistoryMutex;
59 ConsumerFrameEventHistory mFrameEventHistory GUARDED_BY(mFrameEventHistoryMutex);
60 std::queue<uint64_t> mDisconnectEvents GUARDED_BY(mFrameEventHistoryMutex);
61 bool mCurrentlyConnected GUARDED_BY(mFrameEventHistoryMutex);
62 bool mPreviouslyConnected GUARDED_BY(mFrameEventHistoryMutex);
63};
64
Robert Carr78c25dd2019-08-15 14:10:33 -070065class BLASTBufferQueue
66 : public ConsumerBase::FrameAvailableListener, public BufferItemConsumer::BufferFreedListener
67{
68public:
Vishnu Nairdab94092020-09-29 16:09:04 -070069 BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width,
70 int height, bool enableTripleBuffering = true);
Robert Carrcedef052020-04-22 15:58:23 -070071
Robert Carr78c25dd2019-08-15 14:10:33 -070072 sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
73 return mProducer;
74 }
Vishnu Nair992496b2020-10-22 17:27:21 -070075 sp<Surface> getSurface(bool includeSurfaceControlHandle);
Robert Carr78c25dd2019-08-15 14:10:33 -070076
77 void onBufferFreed(const wp<GraphicBuffer>&/* graphicBuffer*/) override { /* TODO */ }
Vishnu Nairaef1de92020-10-22 12:15:53 -070078 void onFrameReplaced(const BufferItem& item) override;
Robert Carr78c25dd2019-08-15 14:10:33 -070079 void onFrameAvailable(const BufferItem& item) override;
80
81 void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
82 const std::vector<SurfaceControlStats>& stats);
83 void setNextTransaction(SurfaceComposerClient::Transaction *t);
84
Vishnu Nairdab94092020-09-29 16:09:04 -070085 void update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height);
Vishnu Nair7eb670a2020-10-15 12:16:10 -070086 void flushShadowQueue() { mFlushShadowQueue = true; }
Robert Carr78c25dd2019-08-15 14:10:33 -070087
Robert Carr9c006e02020-10-14 13:41:57 -070088 status_t setFrameRate(float frameRate, int8_t compatibility);
Robert Carr9b611b72020-10-19 12:00:23 -070089 status_t setFrameTimelineVsync(int64_t frameTimelineVsyncId);
Robert Carr9c006e02020-10-14 13:41:57 -070090
Robert Carr78c25dd2019-08-15 14:10:33 -070091 virtual ~BLASTBufferQueue() = default;
92
93private:
Valerie Hauc5011f92019-10-11 09:52:07 -070094 friend class BLASTBufferQueueHelper;
95
Robert Carr78c25dd2019-08-15 14:10:33 -070096 // can't be copied
97 BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs);
98 BLASTBufferQueue(const BLASTBufferQueue& rhs);
99
Robert Carr255acdc2020-04-17 14:08:55 -0700100 void processNextBufferLocked(bool useNextTransaction) REQUIRES(mMutex);
Vishnu Nairbf255772020-10-16 10:54:41 -0700101 Rect computeCrop(const BufferItem& item) REQUIRES(mMutex);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700102 // Return true if we need to reject the buffer based on the scaling mode and the buffer size.
Vishnu Nairbf255772020-10-16 10:54:41 -0700103 bool rejectBuffer(const BufferItem& item) const REQUIRES(mMutex);
104 bool maxBuffersAcquired() const REQUIRES(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700105
Vishnu Nairdab94092020-09-29 16:09:04 -0700106 std::string mName;
Valerie Haud3b90d22019-11-06 09:37:31 -0800107 sp<SurfaceControl> mSurfaceControl;
108
109 std::mutex mMutex;
110 std::condition_variable mCallbackCV;
Valerie Haud3b90d22019-11-06 09:37:31 -0800111
Valerie Hau65b8e872020-02-13 09:45:14 -0800112 // BufferQueue internally allows 1 more than
113 // the max to be acquired
114 static const int MAX_ACQUIRED_BUFFERS = 1;
Valerie Haua32c5522019-12-09 10:11:08 -0800115
116 int32_t mNumFrameAvailable GUARDED_BY(mMutex);
117 int32_t mNumAcquired GUARDED_BY(mMutex);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700118 bool mInitialCallbackReceived GUARDED_BY(mMutex) = false;
Valerie Haua32c5522019-12-09 10:11:08 -0800119 struct PendingReleaseItem {
120 BufferItem item;
121 sp<Fence> releaseFence;
Robert Carr78c25dd2019-08-15 14:10:33 -0700122 };
Robert Carr78c25dd2019-08-15 14:10:33 -0700123
Valerie Haua32c5522019-12-09 10:11:08 -0800124 std::queue<const BufferItem> mSubmitted GUARDED_BY(mMutex);
Vishnu Nairdab94092020-09-29 16:09:04 -0700125 // Keep a reference to the currently presented buffer so we can release it when the next buffer
126 // is ready to be presented.
Valerie Haua32c5522019-12-09 10:11:08 -0800127 PendingReleaseItem mPendingReleaseItem GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700128
Vishnu Nairdab94092020-09-29 16:09:04 -0700129 uint32_t mWidth GUARDED_BY(mMutex);
130 uint32_t mHeight GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700131
Valerie Haua32c5522019-12-09 10:11:08 -0800132 uint32_t mTransformHint GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700133
134 sp<IGraphicBufferConsumer> mConsumer;
135 sp<IGraphicBufferProducer> mProducer;
Valerie Hau871d6352020-01-29 08:44:02 -0800136 sp<BLASTBufferItemConsumer> mBufferItemConsumer;
Robert Carr78c25dd2019-08-15 14:10:33 -0700137
Valerie Haud3b90d22019-11-06 09:37:31 -0800138 SurfaceComposerClient::Transaction* mNextTransaction GUARDED_BY(mMutex);
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700139 // If set to true, the next queue buffer will wait until the shadow queue has been processed by
140 // the adapter.
141 bool mFlushShadowQueue = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700142};
143
144} // namespace android
145
146#endif // ANDROID_GUI_SURFACE_H