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