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     // Methods from ::android::hardware::media::c2::V1_0::IComponentStore.
83     virtual Return<void> createComponent(
84             const hidl_string& name,
85             const sp<IComponentListener>& listener,
86             const sp<IClientManager>& pool,
87             createComponent_cb _hidl_cb) override;
88     virtual Return<void> createInterface(
89             const hidl_string& name,
90             createInterface_cb _hidl_cb) override;
91     virtual Return<void> listComponents(listComponents_cb _hidl_cb) override;
92     virtual Return<void> createInputSurface(
93             createInputSurface_cb _hidl_cb) override;
94     virtual Return<void> getStructDescriptors(
95             const hidl_vec<uint32_t>& indices,
96             getStructDescriptors_cb _hidl_cb) override;
97     virtual Return<sp<IClientManager>> getPoolClientManager() override;
98     virtual Return<Status> copyBuffer(
99             const Buffer& src,
100             const Buffer& dst) override;
101     virtual Return<sp<IConfigurable>> getConfigurable() override;
102 
103     // Methods from ::android::hardware::media::c2::V1_1::IComponentStore.
104     virtual Return<void> createComponent_1_1(
105             const hidl_string& name,
106             const sp<IComponentListener>& listener,
107             const sp<IClientManager>& pool,
108             createComponent_1_1_cb _hidl_cb) override;
109 
110     // Methods from ::android::hardware::media::c2::V1_2::IComponentStore.
111     virtual Return<void> createComponent_1_2(
112             const hidl_string& name,
113             const sp<IComponentListener>& listener,
114             const sp<IClientManager>& pool,
115             createComponent_1_2_cb _hidl_cb) override;
116 
117     /**
118      * Dumps information when lshal is called.
119      */
120     virtual Return<void> debug(
121             const hidl_handle& handle,
122             const hidl_vec<hidl_string>& args) override;
123 
124 protected:
125     sp<CachedConfigurable> mConfigurable;
126     struct StoreParameterCache;
127     std::shared_ptr<StoreParameterCache> mParameterCache;
128 
129     // Does bookkeeping for an interface that has been loaded.
130     void onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf);
131 
132     c2_status_t mInit;
133     std::shared_ptr<C2ComponentStore> mStore;
134     std::shared_ptr<C2ParamReflector> mParamReflector;
135 
136     std::map<C2Param::CoreIndex, std::shared_ptr<C2StructDescriptor>> mStructDescriptors;
137     std::set<C2Param::CoreIndex> mUnsupportedStructDescriptors;
138     std::set<C2String> mLoadedInterfaces;
139     mutable std::mutex mStructDescriptorsMutex;
140 
141     // ComponentStore keeps track of live Components.
142 
143     struct ComponentStatus {
144         std::shared_ptr<C2Component> c2Component;
145         std::chrono::system_clock::time_point birthTime;
146     };
147 
148     mutable std::mutex mComponentRosterMutex;
149     std::map<Component*, ComponentStatus> mComponentRoster;
150 
151     // Called whenever Component is created.
152     void reportComponentBirth(Component* component);
153     // Called only from the destructor of Component.
154     void reportComponentDeath(Component* component);
155 
156     friend Component;
157 
158     // Helper functions for dumping.
159 
160     std::ostream& dump(
161             std::ostream& out,
162             const std::shared_ptr<const C2Component::Traits>& comp);
163 
164     std::ostream& dump(
165             std::ostream& out,
166             ComponentStatus& compStatus);
167 
168 };
169 
170 } // namespace utils
171 } // namespace V1_2
172 } // namespace c2
173 } // namespace media
174 } // namespace hardware
175 } // namespace android
176 
177 #endif // CODEC2_HIDL_V1_2_UTILS_COMPONENT_STORE_H
178