1 /*
2  * Copyright 2018 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 CODEC2_HIDL_V1_0_UTILS_OUTPUT_BUFFER_QUEUE
18 #define CODEC2_HIDL_V1_0_UTILS_OUTPUT_BUFFER_QUEUE
19 
20 #include <gui/FrameTimestamps.h>
21 #include <gui/IGraphicBufferProducer.h>
22 #include <codec2/hidl/1.0/types.h>
23 #include <codec2/hidl/1.2/types.h>
24 #include <C2Work.h>
25 
26 struct C2_HIDE _C2BlockPoolData;
27 class C2SurfaceSyncMemory;
28 
29 namespace android {
30 namespace hardware {
31 namespace media {
32 namespace c2 {
33 
34 
35 // BufferQueue-Based Block Operations
36 // ==================================
37 
38 // Manage BufferQueue and graphic blocks for both component and codec.
39 // Manage graphic blocks ownership consistently during surface change.
40 struct OutputBufferQueue {
41 
42     OutputBufferQueue();
43 
44     ~OutputBufferQueue();
45 
46     // Configure a new surface to render graphic blocks.
47     // Graphic blocks from older surface will be migrated to new surface.
48     bool configure(const sp<IGraphicBufferProducer>& igbp,
49                    uint32_t generation,
50                    uint64_t bqId,
51                    int maxDequeueBufferCount,
52                    std::shared_ptr<V1_2::SurfaceSyncObj> *syncObj);
53 
54     // If there are waiters to allocate from the old surface, wake up and expire
55     // them.
56     void expireOldWaiters();
57 
58     // Stop using the current output surface. Pending buffer opeations will not
59     // perform anymore.
60     void stop();
61 
62     // Render a graphic block to current surface.
63     status_t outputBuffer(
64             const C2ConstGraphicBlock& block,
65             const BnGraphicBufferProducer::QueueBufferInput& input,
66             BnGraphicBufferProducer::QueueBufferOutput* output);
67 
68     // Nofify a buffer is released from the output surface. If HAL ver is 1.2
69     // update the number of dequeueable/allocatable buffers.
70     void onBufferReleased(uint32_t generation);
71 
72     // Retrieve frame event history from the output surface.
73     void pollForRenderedFrames(FrameEventHistoryDelta* delta);
74 
75     // Call holdBufferQueueBlock() on output blocks in the given workList.
76     // The OutputBufferQueue will take the ownership of output blocks.
77     //
78     // Note: This function should be called after WorkBundle has been received
79     // from another process.
80     void holdBufferQueueBlocks(
81             const std::list<std::unique_ptr<C2Work>>& workList);
82 
83     // Update # of max dequeue buffer from BQ. If # of max dequeued buffer is shared
84     // via shared memory between HAL and framework, Update # of max dequeued buffer
85     // and synchronize.
86     void updateMaxDequeueBufferCount(int maxDequeueBufferCount);
87 
88 private:
89 
90     std::mutex mMutex;
91     sp<IGraphicBufferProducer> mIgbp;
92     uint32_t mGeneration;
93     uint64_t mBqId;
94     int32_t mMaxDequeueBufferCount;
95     std::shared_ptr<int> mOwner;
96     // To migrate existing buffers
97     sp<GraphicBuffer> mBuffers[BufferQueueDefs::NUM_BUFFER_SLOTS]; // find a better way
98     std::weak_ptr<_C2BlockPoolData> mPoolDatas[BufferQueueDefs::NUM_BUFFER_SLOTS];
99     std::shared_ptr<C2SurfaceSyncMemory> mSyncMem;
100     bool mStopped;
101     std::mutex mOldMutex;
102     std::shared_ptr<C2SurfaceSyncMemory> mOldMem;
103 
104     bool registerBuffer(const C2ConstGraphicBlock& block);
105 };
106 
107 }  // namespace c2
108 }  // namespace media
109 }  // namespace hardware
110 }  // namespace android
111 
112 #endif  // CODEC2_HIDL_V1_0_UTILS_OUTPUT_BUFFER_QUEUE
113