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