xref: /aosp_15_r20/frameworks/av/media/codec2/hal/client/include/codec2/hidl/output.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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     // Nofify a buffer is attached to the output surface.
73     void onBufferAttached(uint32_t generation);
74 
75     // Retrieve frame event history from the output surface.
76     void pollForRenderedFrames(FrameEventHistoryDelta* delta);
77 
78     // Call holdBufferQueueBlock() on output blocks in the given workList.
79     // The OutputBufferQueue will take the ownership of output blocks.
80     //
81     // Note: This function should be called after WorkBundle has been received
82     // from another process.
83     void holdBufferQueueBlocks(
84             const std::list<std::unique_ptr<C2Work>>& workList);
85 
86     // Update # of max dequeue buffer from BQ. If # of max dequeued buffer is shared
87     // via shared memory between HAL and framework, Update # of max dequeued buffer
88     // and synchronize.
89     void updateMaxDequeueBufferCount(int maxDequeueBufferCount);
90 
91 private:
92 
93     std::mutex mMutex;
94     sp<IGraphicBufferProducer> mIgbp;
95     uint32_t mGeneration;
96     uint64_t mBqId;
97     int32_t mMaxDequeueBufferCount;
98     std::shared_ptr<int> mOwner;
99     std::shared_ptr<int> mConsumerAttachCount;
100     // To migrate existing buffers
101     sp<GraphicBuffer> mBuffers[BufferQueueDefs::NUM_BUFFER_SLOTS]; // find a better way
102     std::weak_ptr<_C2BlockPoolData> mPoolDatas[BufferQueueDefs::NUM_BUFFER_SLOTS];
103     std::shared_ptr<C2SurfaceSyncMemory> mSyncMem;
104     bool mStopped;
105     std::mutex mOldMutex;
106     std::shared_ptr<C2SurfaceSyncMemory> mOldMem;
107 
108     bool registerBuffer(const C2ConstGraphicBlock& block);
109 };
110 
111 }  // namespace c2
112 }  // namespace media
113 }  // namespace hardware
114 }  // namespace android
115 
116 #endif  // CODEC2_HIDL_V1_0_UTILS_OUTPUT_BUFFER_QUEUE
117