1 /*
2  * Copyright (C) 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 HARDWARE_GOOGLE_MEDIA_C2_V1_0_UTILS_COMPONENTSTORE_H
18 #define HARDWARE_GOOGLE_MEDIA_C2_V1_0_UTILS_COMPONENTSTORE_H
19 
20 #include <codec2/hidl/1.0/Component.h>
21 #include <codec2/hidl/1.0/Configurable.h>
22 #include <hardware/google/media/c2/1.0/IComponentStore.h>
23 #include <android/hardware/media/bufferpool/1.0/IClientManager.h>
24 #include <hidl/Status.h>
25 
26 #include <C2Component.h>
27 #include <C2Param.h>
28 #include <C2.h>
29 
30 #include <map>
31 #include <memory>
32 #include <mutex>
33 #include <set>
34 #include <vector>
35 
36 namespace hardware {
37 namespace google {
38 namespace media {
39 namespace c2 {
40 namespace V1_0 {
41 namespace utils {
42 
43 using ::android::hardware::media::bufferpool::V1_0::IClientManager;
44 
45 using ::android::hardware::hidl_array;
46 using ::android::hardware::hidl_handle;
47 using ::android::hardware::hidl_memory;
48 using ::android::hardware::hidl_string;
49 using ::android::hardware::hidl_vec;
50 using ::android::hardware::Return;
51 using ::android::hardware::Void;
52 using ::android::sp;
53 using ::android::wp;
54 
55 struct ComponentStore : public Configurable<IComponentStore> {
56     ComponentStore(const std::shared_ptr<C2ComponentStore>& store);
57     virtual ~ComponentStore() = default;
58 
statusComponentStore59     c2_status_t status() const {
60         return mInit;
61     }
62 
63     c2_status_t validateSupportedParams(
64             const std::vector<std::shared_ptr<C2ParamDescriptor>>& params);
65 
66     // Methods from ::android::hardware::media::c2::V1_0::IComponentStore
67     Return<void> createComponent(
68             const hidl_string& name,
69             const sp<IComponentListener>& listener,
70             const sp<IClientManager>& pool,
71             createComponent_cb _hidl_cb) override;
72     Return<void> createInterface(
73             const hidl_string& name,
74             createInterface_cb _hidl_cb) override;
75     Return<void> listComponents(listComponents_cb _hidl_cb) override;
76     Return<sp<IInputSurface>> createInputSurface() override;
77     Return<void> getStructDescriptors(
78             const hidl_vec<uint32_t>& indices,
79             getStructDescriptors_cb _hidl_cb) override;
80     Return<sp<IClientManager>> getPoolClientManager() override;
81     Return<Status> copyBuffer(
82             const Buffer& src,
83             const Buffer& dst) override;
84 
85     // Debug dump
86     Return<void> debug(
87             const hidl_handle& handle,
88             const hidl_vec<hidl_string>& args) override;
89 
90 protected:
91     // does bookkeeping for an interface that has been loaded
92     void onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf);
93 
94     c2_status_t mInit;
95     std::shared_ptr<C2ComponentStore> mStore;
96     std::shared_ptr<C2ParamReflector> mParamReflector;
97 
98     std::map<C2Param::CoreIndex, std::shared_ptr<C2StructDescriptor>> mStructDescriptors;
99     std::set<C2Param::CoreIndex> mUnsupportedStructDescriptors;
100     std::set<C2String> mLoadedInterfaces;
101     mutable std::mutex mStructDescriptorsMutex;
102 
103     // Component lifetime management
104     Component::Roster mComponentRoster;
105     mutable std::mutex mComponentRosterMutex;
106     void reportComponentDeath(const Component::LocalId& componentLocalId);
107 
108     friend Component;
109 
110     // C2Component lookup
111     std::shared_ptr<C2Component> findC2Component(
112             const sp<IComponent>& component) const;
113 
114     friend struct InputSurface;
115 };
116 
117 }  // namespace utils
118 }  // namespace V1_0
119 }  // namespace c2
120 }  // namespace media
121 }  // namespace google
122 }  // namespace hardware
123 
124 #endif  // HARDWARE_GOOGLE_MEDIA_C2_V1_0_UTILS_COMPONENTSTORE_H
125