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