blob: 5b1a018ca9b527e5ff5382314998c861c970e194 [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);
Vishnu Nair670b3f72020-09-29 17:52:18 -070097 // Return true if we need to reject the buffer based on the scaling mode and the buffer size.
98 bool rejectBuffer(const BufferItem& item) const;
Robert Carr78c25dd2019-08-15 14:10:33 -070099
Vishnu Nairdab94092020-09-29 16:09:04 -0700100 std::string mName;
Valerie Haud3b90d22019-11-06 09:37:31 -0800101 sp<SurfaceControl> mSurfaceControl;
102
103 std::mutex mMutex;
104 std::condition_variable mCallbackCV;
Valerie Haud3b90d22019-11-06 09:37:31 -0800105
Valerie Hau65b8e872020-02-13 09:45:14 -0800106 // BufferQueue internally allows 1 more than
107 // the max to be acquired
108 static const int MAX_ACQUIRED_BUFFERS = 1;
Valerie Haua32c5522019-12-09 10:11:08 -0800109
110 int32_t mNumFrameAvailable GUARDED_BY(mMutex);
111 int32_t mNumAcquired GUARDED_BY(mMutex);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700112 bool mInitialCallbackReceived GUARDED_BY(mMutex) = false;
Valerie Haua32c5522019-12-09 10:11:08 -0800113 struct PendingReleaseItem {
114 BufferItem item;
115 sp<Fence> releaseFence;
Robert Carr78c25dd2019-08-15 14:10:33 -0700116 };
Robert Carr78c25dd2019-08-15 14:10:33 -0700117
Valerie Haua32c5522019-12-09 10:11:08 -0800118 std::queue<const BufferItem> mSubmitted GUARDED_BY(mMutex);
Vishnu Nairdab94092020-09-29 16:09:04 -0700119 // Keep a reference to the currently presented buffer so we can release it when the next buffer
120 // is ready to be presented.
Valerie Haua32c5522019-12-09 10:11:08 -0800121 PendingReleaseItem mPendingReleaseItem GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700122
Vishnu Nairdab94092020-09-29 16:09:04 -0700123 uint32_t mWidth GUARDED_BY(mMutex);
124 uint32_t mHeight GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700125
Valerie Haua32c5522019-12-09 10:11:08 -0800126 uint32_t mTransformHint GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700127
128 sp<IGraphicBufferConsumer> mConsumer;
129 sp<IGraphicBufferProducer> mProducer;
Valerie Hau871d6352020-01-29 08:44:02 -0800130 sp<BLASTBufferItemConsumer> mBufferItemConsumer;
Robert Carr78c25dd2019-08-15 14:10:33 -0700131
Valerie Haud3b90d22019-11-06 09:37:31 -0800132 SurfaceComposerClient::Transaction* mNextTransaction GUARDED_BY(mMutex);
Robert Carr78c25dd2019-08-15 14:10:33 -0700133};
134
135} // namespace android
136
137#endif // ANDROID_GUI_SURFACE_H