• 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_AIDL_UTILS_COMPONENTSTORE_H
18 #define CODEC2_AIDL_UTILS_COMPONENTSTORE_H
19 
20 #include <android/binder_auto_utils.h>
21 #include <codec2/aidl/ComponentInterface.h>
22 #include <codec2/aidl/Configurable.h>
23 
24 #include <aidl/android/hardware/media/bufferpool2/IClientManager.h>
25 #include <aidl/android/hardware/media/c2/BnComponentStore.h>
26 #include <aidl/android/hardware/media/c2/IInputSurface.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 }  // namespace android
42 
43 namespace aidl {
44 namespace android {
45 namespace hardware {
46 namespace media {
47 namespace c2 {
48 namespace utils {
49 
50 struct Component;
51 
52 using ::aidl::android::hardware::media::bufferpool2::IClientManager;
53 
54 struct ComponentStore : public BnComponentStore {
55     ComponentStore(const std::shared_ptr<C2ComponentStore>& store);
56     virtual ~ComponentStore();
57 
58     /**
59      * Returns the status of the construction of this object.
60      */
61     c2_status_t status() const;
62 
63     /**
64      * This function is called by CachedConfigurable::init() to validate
65      * supported parameters.
66      */
67     c2_status_t validateSupportedParams(
68             const std::vector<std::shared_ptr<C2ParamDescriptor>>& params);
69 
70     /**
71      * Returns the store's ParameterCache. This is used for validation by
72      * Configurable::init().
73      */
74     std::shared_ptr<ParameterCache> getParameterCache() const;
75 
76     static std::shared_ptr<::android::FilterWrapper> GetFilterWrapper();
77 
78     std::shared_ptr<MultiAccessUnitInterface> tryCreateMultiAccessUnitInterface(
79             const std::shared_ptr<C2ComponentInterface> &c2interface);
80 
81     // Methods from ::aidl::android::hardware::media::c2::IComponentStore.
82     virtual ::ndk::ScopedAStatus createComponent(
83             const std::string& name,
84             const std::shared_ptr<IComponentListener>& listener,
85             const std::shared_ptr<IClientManager>& pool,
86             std::shared_ptr<IComponent> *component) override;
87     virtual ::ndk::ScopedAStatus createInterface(
88             const std::string& name,
89             std::shared_ptr<IComponentInterface> *intf) override;
90     virtual ::ndk::ScopedAStatus listComponents(
91             std::vector<IComponentStore::ComponentTraits>* traits) override;
92     virtual ::ndk::ScopedAStatus createInputSurface(
93             std::shared_ptr<IInputSurface> *inputSurface) override;
94     virtual ::ndk::ScopedAStatus getStructDescriptors(
95             const std::vector<int32_t>& indices,
96             std::vector<StructDescriptor> *descs) override;
97     virtual ::ndk::ScopedAStatus getPoolClientManager(
98             std::shared_ptr<IClientManager> *manager) override;
99     virtual ::ndk::ScopedAStatus copyBuffer(
100             const Buffer& src,
101             const Buffer& dst) override;
102     virtual ::ndk::ScopedAStatus getConfigurable(
103             std::shared_ptr<IConfigurable> *configurable) override;
104 
105     /**
106      * Dumps information when lshal is called.
107      */
108     virtual binder_status_t dump(
109             int fd, const char** args, uint32_t numArgs) override;
110 
111 protected:
112     std::shared_ptr<CachedConfigurable> mConfigurable;
113     struct StoreParameterCache;
114     std::shared_ptr<StoreParameterCache> mParameterCache;
115 
116     // Does bookkeeping for an interface that has been loaded.
117     void onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf);
118 
119     c2_status_t mInit;
120     std::shared_ptr<C2ComponentStore> mStore;
121     std::vector<std::shared_ptr<C2ParamReflector>> mParamReflectors;
122 
123     std::map<C2Param::CoreIndex, std::shared_ptr<C2StructDescriptor>> mStructDescriptors;
124     std::set<C2Param::CoreIndex> mUnsupportedStructDescriptors;
125     std::set<C2String> mLoadedInterfaces;
126     mutable std::mutex mStructDescriptorsMutex;
127 
128     // ComponentStore keeps track of live Components.
129 
130     struct ComponentStatus {
131         std::shared_ptr<C2Component> c2Component;
132         std::chrono::system_clock::time_point birthTime;
133     };
134 
135     mutable std::mutex mComponentRosterMutex;
136     std::map<Component*, ComponentStatus> mComponentRoster;
137 
138     // describe from mParamReflectors
139     std::shared_ptr<C2StructDescriptor> describe(const C2Param::CoreIndex &index);
140 
141     // Called whenever Component is created.
142     void reportComponentBirth(Component* component);
143     // Called only from the destructor of Component.
144     void reportComponentDeath(Component* component);
145 
146     friend Component;
147 
148     // Helper functions for dumping.
149 
150     std::ostream& dump(
151             std::ostream& out,
152             const std::shared_ptr<const C2Component::Traits>& comp);
153 
154     std::ostream& dump(
155             std::ostream& out,
156             ComponentStatus& compStatus);
157 
158 };
159 
160 }  // namespace utils
161 }  // namespace c2
162 }  // namespace media
163 }  // namespace hardware
164 }  // namespace android
165 }  // namespace aidl
166 
167 #endif  // CODEC2_AIDL_UTILS_COMPONENTSTORE_H
168