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