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