xref: /aosp_15_r20/frameworks/av/media/codec2/hal/hidl/1.2/utils/include/codec2/hidl/1.2/ComponentStore.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright 2021 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_2_UTILS_COMPONENT_STORE_H
18 #define CODEC2_HIDL_V1_2_UTILS_COMPONENT_STORE_H
19 
20 #include <codec2/hidl/1.2/Component.h>
21 #include <codec2/hidl/1.2/ComponentInterface.h>
22 #include <codec2/hidl/1.2/Configurable.h>
23 #include <codec2/hidl/1.2/types.h>
24 
25 #include <android/hardware/media/bufferpool/2.0/IClientManager.h>
26 #include <android/hardware/media/c2/1.2/IComponentStore.h>
27 #include <hidl/Status.h>
28 
29 #include <C2Component.h>
30 #include <C2Param.h>
31 #include <C2.h>
32 
33 #include <chrono>
34 #include <map>
35 #include <memory>
36 #include <mutex>
37 #include <set>
38 #include <vector>
39 
40 namespace android {
41 class FilterWrapper;
42 
43 namespace hardware {
44 namespace media {
45 namespace c2 {
46 namespace V1_2 {
47 namespace utils {
48 
49 using ::android::hardware::media::bufferpool::V2_0::IClientManager;
50 
51 using ::android::hardware::hidl_handle;
52 using ::android::hardware::hidl_string;
53 using ::android::hardware::hidl_vec;
54 using ::android::hardware::Return;
55 using ::android::hardware::Void;
56 using ::android::sp;
57 
58 struct ComponentStore : public IComponentStore {
59     /**
60      * Constructor for ComponentStore.
61      *
62      * IMPORTANT: SetPreferredCodec2ComponentStore() is called in the constructor.
63      * Be careful about the order of SetPreferredCodec2ComponentStore() and
64      * ComponentStore() in the code.
65      */
66     ComponentStore(const std::shared_ptr<C2ComponentStore>& store);
67     virtual ~ComponentStore();
68 
69     /**
70      * Returns the status of the construction of this object.
71      */
72     c2_status_t status() const;
73 
74     /**
75      * This function is called by CachedConfigurable::init() to validate
76      * supported parameters.
77      */
78     c2_status_t validateSupportedParams(
79             const std::vector<std::shared_ptr<C2ParamDescriptor>>& params);
80 
81     /**
82      * Returns the store's ParameterCache. This is used for validation by
83      * Configurable::init().
84      */
85     std::shared_ptr<ParameterCache> getParameterCache() const;
86 
87     static std::shared_ptr<FilterWrapper> GetFilterWrapper();
88 
89     std::shared_ptr<MultiAccessUnitInterface> tryCreateMultiAccessUnitInterface(
90             const std::shared_ptr<C2ComponentInterface> &c2interface);
91 
92     // Methods from ::android::hardware::media::c2::V1_0::IComponentStore.
93     virtual Return<void> createComponent(
94             const hidl_string& name,
95             const sp<IComponentListener>& listener,
96             const sp<IClientManager>& pool,
97             createComponent_cb _hidl_cb) override;
98     virtual Return<void> createInterface(
99             const hidl_string& name,
100             createInterface_cb _hidl_cb) override;
101     virtual Return<void> listComponents(listComponents_cb _hidl_cb) override;
102     virtual Return<void> createInputSurface(
103             createInputSurface_cb _hidl_cb) override;
104     virtual Return<void> getStructDescriptors(
105             const hidl_vec<uint32_t>& indices,
106             getStructDescriptors_cb _hidl_cb) override;
107     virtual Return<sp<IClientManager>> getPoolClientManager() override;
108     virtual Return<Status> copyBuffer(
109             const Buffer& src,
110             const Buffer& dst) override;
111     virtual Return<sp<IConfigurable>> getConfigurable() override;
112 
113     // Methods from ::android::hardware::media::c2::V1_1::IComponentStore.
114     virtual Return<void> createComponent_1_1(
115             const hidl_string& name,
116             const sp<IComponentListener>& listener,
117             const sp<IClientManager>& pool,
118             createComponent_1_1_cb _hidl_cb) override;
119 
120     // Methods from ::android::hardware::media::c2::V1_2::IComponentStore.
121     virtual Return<void> createComponent_1_2(
122             const hidl_string& name,
123             const sp<IComponentListener>& listener,
124             const sp<IClientManager>& pool,
125             createComponent_1_2_cb _hidl_cb) override;
126 
127     /**
128      * Dumps information when lshal is called.
129      */
130     virtual Return<void> debug(
131             const hidl_handle& handle,
132             const hidl_vec<hidl_string>& args) override;
133 
134 protected:
135     sp<CachedConfigurable> mConfigurable;
136     struct StoreParameterCache;
137     std::shared_ptr<StoreParameterCache> mParameterCache;
138 
139     // Does bookkeeping for an interface that has been loaded.
140     void onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf);
141 
142     // describe from mParamReflectors
143     std::shared_ptr<C2StructDescriptor> describe(const C2Param::CoreIndex &index);
144 
145     c2_status_t mInit;
146     std::shared_ptr<C2ComponentStore> mStore;
147     std::vector<std::shared_ptr<C2ParamReflector>> mParamReflectors;
148 
149     // Reflector helper for MultiAccessUnitHelper
150     std::shared_ptr<C2ReflectorHelper> mMultiAccessUnitReflector;
151 
152     std::map<C2Param::CoreIndex, std::shared_ptr<C2StructDescriptor>> mStructDescriptors;
153     std::set<C2Param::CoreIndex> mUnsupportedStructDescriptors;
154     std::set<C2String> mLoadedInterfaces;
155     mutable std::mutex mStructDescriptorsMutex;
156 
157     // ComponentStore keeps track of live Components.
158 
159     struct ComponentStatus {
160         std::shared_ptr<C2Component> c2Component;
161         std::chrono::system_clock::time_point birthTime;
162     };
163 
164     mutable std::mutex mComponentRosterMutex;
165     std::map<Component*, ComponentStatus> mComponentRoster;
166 
167     // Called whenever Component is created.
168     void reportComponentBirth(Component* component);
169     // Called only from the destructor of Component.
170     void reportComponentDeath(Component* component);
171 
172     friend Component;
173 
174     // Helper functions for dumping.
175 
176     std::ostream& dump(
177             std::ostream& out,
178             const std::shared_ptr<const C2Component::Traits>& comp);
179 
180     std::ostream& dump(
181             std::ostream& out,
182             ComponentStatus& compStatus);
183 
184 };
185 
186 } // namespace utils
187 } // namespace V1_2
188 } // namespace c2
189 } // namespace media
190 } // namespace hardware
191 } // namespace android
192 
193 #endif // CODEC2_HIDL_V1_2_UTILS_COMPONENT_STORE_H
194