xref: /aosp_15_r20/frameworks/av/media/codec2/hal/client/include/codec2/aidl/GraphicBufferAllocator.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 #include <aidl/android/hardware/media/c2/BnGraphicBufferAllocator.h>
20 
21 #include <android-base/unique_fd.h>
22 #include <gui/IGraphicBufferProducer.h>
23 
24 #include <memory>
25 
26 #include <C2Buffer.h>
27 
28 namespace aidl::android::hardware::media::c2::implementation {
29 
30 // forward declarations
31 class GraphicsTracker;
32 
33 struct GraphicBufferAllocator : public BnGraphicBufferAllocator {
34 public:
35     // HAL interfaces
36     ::ndk::ScopedAStatus allocate(const IGraphicBufferAllocator::Description& in_desc,
37                                   IGraphicBufferAllocator::Allocation* _aidl_return) override;
38 
39     ::ndk::ScopedAStatus deallocate(int64_t in_id, bool* _aidl_return) override;
40 
41     ::ndk::ScopedAStatus getWaitableFd(
42             ::ndk::ScopedFileDescriptor* _aidl_return) override;
43 
44     /**
45      * Configuring Surface/BufferQueue for the interface.
46      *
47      * Configure Surface, generation # and max dequeueBuffer() count for
48      * allocate interface.
49      *
50      * @param   igbp              Surface where to allocate.
51      * @param   generation        Generation # for allocations.
52      * @param   maxDequeueBufferCount
53      *                            Maximum # of pending allocations.
54      */
55     bool configure(const ::android::sp<::android::IGraphicBufferProducer>& igbp,
56                    uint32_t generation,
57                    int maxDequeueBufferCount);
58 
59     /**
60      * Update max dequeue buffer count of BufferQueue.
61      *
62      * BufferQueue does not update this value if count is smaller
63      * than the currently dequeued count.
64      * TODO: better to update the value inside this interface.
65      * for return value inspection from BQ, also for delayed updates.
66      *
67      * @param   count             the new value to update
68      */
69     void updateMaxDequeueBufferCount(int count);
70 
71     void reset();
72 
73     /**
74      * Notifies a buffer being released.
75      *
76      * @param   generation        generation # for the BufferQueue.
77      */
78     void onBufferReleased(uint32_t generation);
79 
80     /**
81      * Notifies a buffer being attached to the consumer.
82      *
83      * @param   generation        generation # for the BufferQueue.
84      */
85     void onBufferAttached(uint32_t generation);
86 
87     /**
88      * Retrieve frame event history from the crurrent surface if any.
89      */
90     void pollForRenderedFrames(::android::FrameEventHistoryDelta* delta);
91 
92     /**
93      * Allocates a buffer.
94      *
95      * @param   width             width of the requested buffer.
96      * @param   height            height of the requested buffer.
97      * @param   format            format of the requested buffer.
98      * @param   usage             usage of the requested buffer.
99      * @param   buf               out param for created buffer.
100      * @param   fence             out param for a pending fence.
101      *
102      * @return  OK                When an allocation was created.
103      *          C2_BAD_STATE      Client is not in the state for allocating
104      *          C2_BLOCKING       operation is blocked. Waitable fds can be
105      *                            used to know when it unblocks.
106      *          C2_CORRUPTED      Failed with a serious reason.
107      */
108     c2_status_t allocate(uint32_t width, uint32_t height,
109                          ::android::PixelFormat format, uint64_t usage,
110                          AHardwareBuffer **buf, ::android::sp<::android::Fence> *fence);
111 
112     /**
113      * De-allocate a buffer.
114      *
115      * @param   id                unique id for a buffer.
116      * @param   fence             write fence if it's deallocated due to
117      *                            cancellation of displaying
118      */
119     bool deallocate(const uint64_t id, const ::android::sp<::android::Fence> &fence);
120 
121     /**
122      * Display a graphic buffer to BufferQueue.
123      *
124      * @param   block             block to display to Surface.
125      * @param   input             input parameter for displaying.
126      * @param   output            out parameter from Surface.
127      */
128     c2_status_t displayBuffer(
129             const C2ConstGraphicBlock& block,
130             const ::android::IGraphicBufferProducer::QueueBufferInput& input,
131             ::android::IGraphicBufferProducer::QueueBufferOutput *output);
132 
133     /**
134      * Notify stop()/release() is in progress.
135      */
136     void onRequestStop();
137 
138     ~GraphicBufferAllocator();
139 
140     /**
141      * Create the interface.
142      *
143      * The interface and codec instance's relationship is 1 to 1.
144      * The interface will be cretaed in the beginning of Codec createion. And
145      * lives until the instance destroyed.
146      *
147      * @param   maxDequeueCount   Initial max allocatable count
148      */
149     static std::shared_ptr<GraphicBufferAllocator> CreateGraphicBufferAllocator(
150             int maxDequeueCount);
151 private:
152     GraphicBufferAllocator(int maxDequeueCount);
153 
154     std::shared_ptr<GraphicsTracker> mGraphicsTracker;
155 
156     friend class ::ndk::SharedRefBase;
157 };
158 
159 } // namespace aidl::android::hardware::media::c2::implementation
160