1 /*
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>
30 #include <thread>
31 
32 namespace android {
33 
34 class BufferItemConsumer;
35 
36 class BLASTBufferItemConsumer : public BufferItemConsumer {
37 public:
BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer> & consumer,uint64_t consumerUsage,int bufferCount,bool controlledByApp)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 
55 private:
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 
65 class BLASTBufferQueue
66     : public ConsumerBase::FrameAvailableListener, public BufferItemConsumer::BufferFreedListener
67 {
68 public:
69     BLASTBufferQueue(const sp<SurfaceControl>& surface, int width, int height,
70                      bool enableTripleBuffering = true);
71 
getIGraphicBufferProducer()72     sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
73         return mProducer;
74     }
75 
onBufferFreed(const wp<GraphicBuffer> &)76     void onBufferFreed(const wp<GraphicBuffer>&/* graphicBuffer*/) override { /* TODO */ }
onFrameReplaced(const BufferItem & item)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 
84     void update(const sp<SurfaceControl>& surface, int width, int height);
85 
86     virtual ~BLASTBufferQueue() = default;
87 
88 private:
89     friend class BLASTBufferQueueHelper;
90 
91     // can't be copied
92     BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs);
93     BLASTBufferQueue(const BLASTBufferQueue& rhs);
94 
95     void processNextBufferLocked(bool useNextTransaction) REQUIRES(mMutex);
96     Rect computeCrop(const BufferItem& item);
97 
98     sp<SurfaceControl> mSurfaceControl;
99 
100     std::mutex mMutex;
101     std::condition_variable mCallbackCV;
102 
103     // BufferQueue internally allows 1 more than
104     // the max to be acquired
105     static const int MAX_ACQUIRED_BUFFERS = 1;
106 
107     int32_t mNumFrameAvailable GUARDED_BY(mMutex);
108     int32_t mNumAcquired GUARDED_BY(mMutex);
109 
110     struct PendingReleaseItem {
111         BufferItem item;
112         sp<Fence> releaseFence;
113     };
114 
115     std::queue<const BufferItem> mSubmitted GUARDED_BY(mMutex);
116     PendingReleaseItem mPendingReleaseItem GUARDED_BY(mMutex);
117 
118     int mWidth GUARDED_BY(mMutex);
119     int mHeight GUARDED_BY(mMutex);
120 
121     uint32_t mTransformHint GUARDED_BY(mMutex);
122 
123     sp<IGraphicBufferConsumer> mConsumer;
124     sp<IGraphicBufferProducer> mProducer;
125     sp<BLASTBufferItemConsumer> mBufferItemConsumer;
126 
127     SurfaceComposerClient::Transaction* mNextTransaction GUARDED_BY(mMutex);
128 };
129 
130 } // namespace android
131 
132 #endif  // ANDROID_GUI_SURFACE_H
133