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_CONFIGURABLE_H
18 #define CODEC2_HIDL_V1_0_UTILS_CONFIGURABLE_H
19 
20 #include <android/hardware/media/c2/1.0/IConfigurable.h>
21 #include <hidl/Status.h>
22 
23 #include <C2Component.h>
24 #include <C2Param.h>
25 #include <C2.h>
26 
27 #include <memory>
28 
29 namespace android {
30 namespace hardware {
31 namespace media {
32 namespace c2 {
33 namespace V1_0 {
34 namespace utils {
35 
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::Return;
38 using ::android::hardware::Void;
39 using ::android::sp;
40 
41 struct ComponentStore;
42 
43 /**
44  * Codec2 objects of different types may have different querying and configuring
45  * functions, but across the Treble boundary, they share the same HIDL
46  * interface, IConfigurable.
47  *
48  * ConfigurableC2Intf is an abstract class that a Codec2 object can implement to
49  * easily expose an IConfigurable instance. See CachedConfigurable below.
50  */
51 struct ConfigurableC2Intf {
getNameConfigurableC2Intf52     C2String getName() const { return mName; }
getIdConfigurableC2Intf53     uint32_t getId() const { return mId; }
54     /** C2ComponentInterface::query_vb sans stack params */
55     virtual c2_status_t query(
56             const std::vector<C2Param::Index> &indices,
57             c2_blocking_t mayBlock,
58             std::vector<std::unique_ptr<C2Param>>* const params) const = 0;
59     /** C2ComponentInterface::config_vb */
60     virtual c2_status_t config(
61             const std::vector<C2Param*> &params,
62             c2_blocking_t mayBlock,
63             std::vector<std::unique_ptr<C2SettingResult>>* const failures) = 0;
64     /** C2ComponentInterface::querySupportedParams_nb */
65     virtual c2_status_t querySupportedParams(
66             std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const = 0;
67     /** C2ComponentInterface::querySupportedParams_nb */
68     virtual c2_status_t querySupportedValues(
69             std::vector<C2FieldSupportedValuesQuery>& fields, c2_blocking_t mayBlock) const = 0;
70 
71     virtual ~ConfigurableC2Intf() = default;
72 
ConfigurableC2IntfConfigurableC2Intf73     ConfigurableC2Intf(const C2String& name, uint32_t id)
74           : mName{name}, mId{id} {}
75 
76 protected:
77     C2String mName; /* cached component name */
78     uint32_t mId;
79 };
80 
81 /**
82  * Implementation of the IConfigurable interface that supports caching of
83  * supported parameters from a supplied ComponentStore.
84  *
85  * CachedConfigurable essentially converts a ConfigurableC2Intf into HIDL's
86  * IConfigurable. A Codec2 object generally implements ConfigurableC2Intf and
87  * passes the implementation to the constructor of CachedConfigurable.
88  *
89  * Note that caching happens
90  */
91 struct CachedConfigurable : public IConfigurable {
92     CachedConfigurable(std::unique_ptr<ConfigurableC2Intf>&& intf);
93 
94     c2_status_t init(ComponentStore* store);
95 
96     // Methods from ::android::hardware::media::c2::V1_0::IConfigurable
97 
98     virtual Return<uint32_t> getId() override;
99 
100     virtual Return<void> getName(getName_cb _hidl_cb) override;
101 
102     virtual Return<void> query(
103             const hidl_vec<uint32_t>& indices,
104             bool mayBlock,
105             query_cb _hidl_cb) override;
106 
107     virtual Return<void> config(
108             const hidl_vec<uint8_t>& inParams,
109             bool mayBlock,
110             config_cb _hidl_cb) override;
111 
112     virtual Return<void> querySupportedParams(
113             uint32_t start,
114             uint32_t count,
115             querySupportedParams_cb _hidl_cb) override;
116 
117     virtual Return<void> querySupportedValues(
118             const hidl_vec<FieldSupportedValuesQuery>& inFields,
119             bool mayBlock,
120             querySupportedValues_cb _hidl_cb) override;
121 
122 protected:
123     // Common Codec2.0 interface wrapper
124     std::unique_ptr<ConfigurableC2Intf> mIntf;
125 
126     // Cached supported params
127     std::vector<std::shared_ptr<C2ParamDescriptor>> mSupportedParams;
128 };
129 
130 }  // namespace utils
131 }  // namespace V1_0
132 }  // namespace c2
133 }  // namespace media
134 }  // namespace hardware
135 }  // namespace android
136 
137 #endif  // CODEC2_HIDL_V1_0_UTILS_CONFIGURABLE_H
138 
139