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  * Type for validating and caching parameters when CachedConfigurable is
83  * initialized.
84  *
85  * This is meant to be created by the ComponentStore. The purpose of abstracting
86  * this is to allow different versions of ComponentStore to work with this
87  * CachedConfigurable.
88  */
89 struct ParameterCache {
90     virtual c2_status_t validate(
91             const std::vector<std::shared_ptr<C2ParamDescriptor>>&) = 0;
92     virtual ~ParameterCache() = default;
93 };
94 
95 /**
96  * Implementation of the IConfigurable interface that supports caching of
97  * supported parameters from a supplied ComponentStore.
98  *
99  * CachedConfigurable essentially converts a ConfigurableC2Intf into HIDL's
100  * IConfigurable. A Codec2 object generally implements ConfigurableC2Intf and
101  * passes the implementation to the constructor of CachedConfigurable.
102  *
103  * Note that caching happens
104  */
105 struct CachedConfigurable : public IConfigurable {
106     CachedConfigurable(std::unique_ptr<ConfigurableC2Intf>&& intf);
107 
108     // Populates mSupportedParams.
109     c2_status_t init(const std::shared_ptr<ParameterCache> &cache);
110 
111     // Methods from ::android::hardware::media::c2::V1_0::IConfigurable
112 
113     virtual Return<uint32_t> getId() override;
114 
115     virtual Return<void> getName(getName_cb _hidl_cb) override;
116 
117     virtual Return<void> query(
118             const hidl_vec<uint32_t>& indices,
119             bool mayBlock,
120             query_cb _hidl_cb) override;
121 
122     virtual Return<void> config(
123             const hidl_vec<uint8_t>& inParams,
124             bool mayBlock,
125             config_cb _hidl_cb) override;
126 
127     virtual Return<void> querySupportedParams(
128             uint32_t start,
129             uint32_t count,
130             querySupportedParams_cb _hidl_cb) override;
131 
132     virtual Return<void> querySupportedValues(
133             const hidl_vec<FieldSupportedValuesQuery>& inFields,
134             bool mayBlock,
135             querySupportedValues_cb _hidl_cb) override;
136 
137 protected:
138     // Common Codec2.0 interface wrapper
139     std::unique_ptr<ConfigurableC2Intf> mIntf;
140 
141     // Cached supported params
142     std::vector<std::shared_ptr<C2ParamDescriptor>> mSupportedParams;
143 };
144 
145 }  // namespace utils
146 }  // namespace V1_0
147 }  // namespace c2
148 }  // namespace media
149 }  // namespace hardware
150 }  // namespace android
151 
152 #endif  // CODEC2_HIDL_V1_0_UTILS_CONFIGURABLE_H
153 
154