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     ComponentStore(const std::shared_ptr<C2ComponentStore>& store);
60     virtual ~ComponentStore();
61 
62     /**
63      * Returns the status of the construction of this object.
64      */
65     c2_status_t status() const;
66 
67     /**
68      * This function is called by CachedConfigurable::init() to validate
69      * supported parameters.
70      */
71     c2_status_t validateSupportedParams(
72             const std::vector<std::shared_ptr<C2ParamDescriptor>>& params);
73 
74     /**
75      * Returns the store's ParameterCache. This is used for validation by
76      * Configurable::init().
77      */
78     std::shared_ptr<ParameterCache> getParameterCache() const;
79 
80     static std::shared_ptr<FilterWrapper> GetFilterWrapper();
81 
82     std::shared_ptr<MultiAccessUnitInterface> tryCreateMultiAccessUnitInterface(
83             const std::shared_ptr<C2ComponentInterface> &c2interface);
84 
85     // Methods from ::android::hardware::media::c2::V1_0::IComponentStore.
86     virtual Return<void> createComponent(
87             const hidl_string& name,
88             const sp<IComponentListener>& listener,
89             const sp<IClientManager>& pool,
90             createComponent_cb _hidl_cb) override;
91     virtual Return<void> createInterface(
92             const hidl_string& name,
93             createInterface_cb _hidl_cb) override;
94     virtual Return<void> listComponents(listComponents_cb _hidl_cb) override;
95     virtual Return<void> createInputSurface(
96             createInputSurface_cb _hidl_cb) override;
97     virtual Return<void> getStructDescriptors(
98             const hidl_vec<uint32_t>& indices,
99             getStructDescriptors_cb _hidl_cb) override;
100     virtual Return<sp<IClientManager>> getPoolClientManager() override;
101     virtual Return<Status> copyBuffer(
102             const Buffer& src,
103             const Buffer& dst) override;
104     virtual Return<sp<IConfigurable>> getConfigurable() override;
105 
106     // Methods from ::android::hardware::media::c2::V1_1::IComponentStore.
107     virtual Return<void> createComponent_1_1(
108             const hidl_string& name,
109             const sp<IComponentListener>& listener,
110             const sp<IClientManager>& pool,
111             createComponent_1_1_cb _hidl_cb) override;
112 
113     // Methods from ::android::hardware::media::c2::V1_2::IComponentStore.
114     virtual Return<void> createComponent_1_2(
115             const hidl_string& name,
116             const sp<IComponentListener>& listener,
117             const sp<IClientManager>& pool,
118             createComponent_1_2_cb _hidl_cb) override;
119 
120     /**
121      * Dumps information when lshal is called.
122      */
123     virtual Return<void> debug(
124             const hidl_handle& handle,
125             const hidl_vec<hidl_string>& args) override;
126 
127 protected:
128     sp<CachedConfigurable> mConfigurable;
129     struct StoreParameterCache;
130     std::shared_ptr<StoreParameterCache> mParameterCache;
131 
132     // Does bookkeeping for an interface that has been loaded.
133     void onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf);
134 
135     // describe from mParamReflectors
136     std::shared_ptr<C2StructDescriptor> describe(const C2Param::CoreIndex &index);
137 
138     c2_status_t mInit;
139     std::shared_ptr<C2ComponentStore> mStore;
140     std::vector<std::shared_ptr<C2ParamReflector>> mParamReflectors;
141 
142     std::map<C2Param::CoreIndex, std::shared_ptr<C2StructDescriptor>> mStructDescriptors;
143     std::set<C2Param::CoreIndex> mUnsupportedStructDescriptors;
144     std::set<C2String> mLoadedInterfaces;
145     mutable std::mutex mStructDescriptorsMutex;
146 
147     // ComponentStore keeps track of live Components.
148 
149     struct ComponentStatus {
150         std::shared_ptr<C2Component> c2Component;
151         std::chrono::system_clock::time_point birthTime;
152     };
153 
154     mutable std::mutex mComponentRosterMutex;
155     std::map<Component*, ComponentStatus> mComponentRoster;
156 
157     // Called whenever Component is created.
158     void reportComponentBirth(Component* component);
159     // Called only from the destructor of Component.
160     void reportComponentDeath(Component* component);
161 
162     friend Component;
163 
164     // Helper functions for dumping.
165 
166     std::ostream& dump(
167             std::ostream& out,
168             const std::shared_ptr<const C2Component::Traits>& comp);
169 
170     std::ostream& dump(
171             std::ostream& out,
172             ComponentStatus& compStatus);
173 
174 };
175 
176 } // namespace utils
177 } // namespace V1_2
178 } // namespace c2
179 } // namespace media
180 } // namespace hardware
181 } // namespace android
182 
183 #endif // CODEC2_HIDL_V1_2_UTILS_COMPONENT_STORE_H
184