blob: 35c656ab1548afb823ccbda73b9741c21d7b49ea [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 }
75
76 void onBufferFreed(const wp<GraphicBuffer>&/* graphicBuffer*/) override { /* TODO */ }
77 void onFrameReplaced(const BufferItem& item) override {onFrameAvailable(item);}
78 void onFrameAvailable(const BufferItem& item) override;
79
80 void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
81 const std::vector<SurfaceControlStats>& stats);
82 void setNextTransaction(SurfaceComposerClient::Transaction *t);
83
Vishnu Nairdab94092020-09-29 16:09:04 -070084 void update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height);
Robert Carr78c25dd2019-08-15 14:10:33 -070085
86 virtual ~BLASTBufferQueue() = default;
87
88private:
Valerie Hauc5011f92019-10-11 09:52:07 -070089 friend class BLASTBufferQueueHelper;
90
Robert Carr78c25dd2019-08-15 14:10:33 -070091 // can't be copied
92 BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs);
93 BLASTBufferQueue(const BLASTBufferQueue& rhs);
94
Robert Carr255acdc2020-04-17 14:08:55 -070095 void processNextBufferLocked(bool useNextTransaction) REQUIRES(mMutex);
Valerie Hau45e4b3b2019-12-03 10:49:17 -080096 Rect computeCrop(const BufferItem& item);
Robert Carr78c25dd2019-08-15 14:10:33 -070097
Vishnu Nairdab94092020-09-29 16:09:04 -070098 std::string mName;
Valerie Haud3b90d22019-11-06 09:37:31 -080099 sp<SurfaceControl> mSurfaceControl;
100
101 std::mutex mMutex;
102 std::condition_variable mCallbackCV;
Valerie Haud3b90d22019-11-06 09:37:31 -0800103
Valerie Hau65b8e872020-02-13 09:45:14 -0800104 // BufferQueue internally allows 1 more than
105 // the max to be acquired
106 static const int MAX_ACQUIRED_BUFFERS = 1;
Valerie Haua32c5522019-12-09 10:11:08 -0800107
108 int32_t mNumFrameAvailable GUARDED_BY(mMutex);
109 int32_t mNumAcquired GUARDED_BY(mMutex);
Valerie Haua32c5522019-12-09 10:11:08 -0800110 struct PendingReleaseItem {
111 BufferItem item;
112 sp<Fence> releaseFence;
Robert Carr78c25dd2019-08-15 14:10:33 -0700113 };
Robert Carr78c25dd2019-08-15 14:10:33 -0700114
Valerie Haua32c5522019-12-09 10:11:08 -0800115 std::queue<const BufferItem> mSubmitted GUARDED_BY(mMutex);
Vishnu Nairdab94092020-09-29 16:09:04 -0700116 // Keep a reference to the currently presented buffer so we can release it when the next buffer
117 // is ready to be presented.
Valerie Haua32c5522019-12-09 10:11:08 -0800118 PendingReleaseItem mPendingReleaseItem GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700119
Vishnu Nairdab94092020-09-29 16:09:04 -0700120 uint32_t mWidth GUARDED_BY(mMutex);
121 uint32_t mHeight GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700122
Valerie Haua32c5522019-12-09 10:11:08 -0800123 uint32_t mTransformHint GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700124
125 sp<IGraphicBufferConsumer> mConsumer;
126 sp<IGraphicBufferProducer> mProducer;
Valerie Hau871d6352020-01-29 08:44:02 -0800127 sp<BLASTBufferItemConsumer> mBufferItemConsumer;
Robert Carr78c25dd2019-08-15 14:10:33 -0700128
Valerie Haud3b90d22019-11-06 09:37:31 -0800129 SurfaceComposerClient::Transaction* mNextTransaction GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700130};
131
132} // namespace android
133
134#endif // ANDROID_GUI_SURFACE_H