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 ANDROID_SIMPLE_C2_INTERFACE_H_
18 #define ANDROID_SIMPLE_C2_INTERFACE_H_
19
20 #include <C2Component.h>
21 #include <C2Config.h>
22 #include <util/C2InterfaceHelper.h>
23
24 namespace android {
25
26 /**
27 * Wrap a common interface object (such as Codec2Client::Interface, or C2InterfaceHelper into
28 * a C2ComponentInterface.
29 *
30 * \param T common interface type
31 */
32 template <typename T>
33 class SimpleC2Interface : public C2ComponentInterface {
34 public:
SimpleC2Interface(const char * name,c2_node_id_t id,const std::shared_ptr<T> & impl)35 SimpleC2Interface(const char *name, c2_node_id_t id, const std::shared_ptr<T> &impl)
36 : mName(name),
37 mId(id),
38 mImpl(impl) {
39 }
40
41 ~SimpleC2Interface() override = default;
42
43 // From C2ComponentInterface
getName()44 C2String getName() const override { return mName; }
getId()45 c2_node_id_t getId() const override { return mId; }
query_vb(const std::vector<C2Param * > & stackParams,const std::vector<C2Param::Index> & heapParamIndices,c2_blocking_t mayBlock,std::vector<std::unique_ptr<C2Param>> * const heapParams)46 c2_status_t query_vb(
47 const std::vector<C2Param*> &stackParams,
48 const std::vector<C2Param::Index> &heapParamIndices,
49 c2_blocking_t mayBlock,
50 std::vector<std::unique_ptr<C2Param>>* const heapParams) const override {
51 return mImpl->query(stackParams, heapParamIndices, mayBlock, heapParams);
52 }
config_vb(const std::vector<C2Param * > & params,c2_blocking_t mayBlock,std::vector<std::unique_ptr<C2SettingResult>> * const failures)53 c2_status_t config_vb(
54 const std::vector<C2Param*> ¶ms,
55 c2_blocking_t mayBlock,
56 std::vector<std::unique_ptr<C2SettingResult>>* const failures) override {
57 return mImpl->config(params, mayBlock, failures);
58 }
createTunnel_sm(c2_node_id_t)59 c2_status_t createTunnel_sm(c2_node_id_t) override { return C2_OMITTED; }
releaseTunnel_sm(c2_node_id_t)60 c2_status_t releaseTunnel_sm(c2_node_id_t) override { return C2_OMITTED; }
querySupportedParams_nb(std::vector<std::shared_ptr<C2ParamDescriptor>> * const params)61 c2_status_t querySupportedParams_nb(
62 std::vector<std::shared_ptr<C2ParamDescriptor>> * const params) const override {
63 return mImpl->querySupportedParams(params);
64 }
querySupportedValues_vb(std::vector<C2FieldSupportedValuesQuery> & fields,c2_blocking_t mayBlock)65 c2_status_t querySupportedValues_vb(
66 std::vector<C2FieldSupportedValuesQuery> &fields,
67 c2_blocking_t mayBlock) const override {
68 return mImpl->querySupportedValues(fields, mayBlock);
69 }
70
71 private:
72 C2String mName;
73 const c2_node_id_t mId;
74 const std::shared_ptr<T> mImpl;
75 };
76
77 /**
78 * Utility classes for common interfaces.
79 */
80 template<>
81 class SimpleC2Interface<void> {
82 public:
83 /**
84 * Base Codec 2.0 parameters required for all components.
85 */
86 struct BaseParams : C2InterfaceHelper {
87 explicit BaseParams(
88 const std::shared_ptr<C2ReflectorHelper> &helper,
89 C2String name,
90 C2Component::kind_t kind,
91 C2Component::domain_t domain,
92 C2String mediaType,
93 std::vector<C2String> aliases = std::vector<C2String>());
94
95 /// Marks that this component has no input latency. Otherwise, component must
96 /// add support for C2PortRequestedDelayTuning::input and C2PortActualDelayTuning::input.
97 void noInputLatency();
98
99 /// Marks that this component has no output latency. Otherwise, component must
100 /// add support for C2PortRequestedDelayTuning::output and C2PortActualDelayTuning::output.
101 void noOutputLatency();
102
103 /// Marks that this component has no pipeline latency. Otherwise, component must
104 /// add support for C2RequestedPipelineDelayTuning and C2ActualPipelineDelayTuning.
105 void noPipelineLatency();
106
107 /// Marks that this component has no need for private buffers. Otherwise, component must
108 /// add support for C2MaxPrivateBufferCountTuning, C2PrivateAllocatorsTuning and
109 /// C2PrivateBlockPoolsTuning.
110 void noPrivateBuffers();
111
112 /// Marks that this component holds no references to input buffers. Otherwise, component
113 /// must add support for C2StreamMaxReferenceAgeTuning::input and
114 /// C2StreamMaxReferenceCountTuning::input.
115 void noInputReferences();
116
117 /// Marks that this component holds no references to output buffers. Otherwise, component
118 /// must add support for C2StreamMaxReferenceAgeTuning::output and
119 /// C2StreamMaxReferenceCountTuning::output.
120 void noOutputReferences();
121
122 /// Marks that this component does not stretch time. Otherwise, component
123 /// must add support for C2ComponentTimeStretchTuning.
124 void noTimeStretch();
125
126 std::shared_ptr<C2ApiLevelSetting> mApiLevel;
127 std::shared_ptr<C2ApiFeaturesSetting> mApiFeatures;
128
129 std::shared_ptr<C2PlatformLevelSetting> mPlatformLevel;
130 std::shared_ptr<C2PlatformFeaturesSetting> mPlatformFeatures;
131
132 std::shared_ptr<C2ComponentNameSetting> mName;
133 std::shared_ptr<C2ComponentAliasesSetting> mAliases;
134 std::shared_ptr<C2ComponentKindSetting> mKind;
135 std::shared_ptr<C2ComponentDomainSetting> mDomain;
136 std::shared_ptr<C2ComponentAttributesSetting> mAttrib;
137 std::shared_ptr<C2ComponentTimeStretchTuning> mTimeStretch;
138
139 std::shared_ptr<C2PortMediaTypeSetting::input> mInputMediaType;
140 std::shared_ptr<C2PortMediaTypeSetting::output> mOutputMediaType;
141 std::shared_ptr<C2StreamBufferTypeSetting::input> mInputFormat;
142 std::shared_ptr<C2StreamBufferTypeSetting::output> mOutputFormat;
143
144 std::shared_ptr<C2PortRequestedDelayTuning::input> mRequestedInputDelay;
145 std::shared_ptr<C2PortRequestedDelayTuning::output> mRequestedOutputDelay;
146 std::shared_ptr<C2RequestedPipelineDelayTuning> mRequestedPipelineDelay;
147
148 std::shared_ptr<C2PortActualDelayTuning::input> mActualInputDelay;
149 std::shared_ptr<C2PortActualDelayTuning::output> mActualOutputDelay;
150 std::shared_ptr<C2ActualPipelineDelayTuning> mActualPipelineDelay;
151
152 std::shared_ptr<C2StreamMaxReferenceAgeTuning::input> mMaxInputReferenceAge;
153 std::shared_ptr<C2StreamMaxReferenceCountTuning::input> mMaxInputReferenceCount;
154 std::shared_ptr<C2StreamMaxReferenceAgeTuning::output> mMaxOutputReferenceAge;
155 std::shared_ptr<C2StreamMaxReferenceCountTuning::output> mMaxOutputReferenceCount;
156 std::shared_ptr<C2MaxPrivateBufferCountTuning> mMaxPrivateBufferCount;
157
158 std::shared_ptr<C2PortStreamCountTuning::input> mInputStreamCount;
159 std::shared_ptr<C2PortStreamCountTuning::output> mOutputStreamCount;
160
161 std::shared_ptr<C2SubscribedParamIndicesTuning> mSubscribedParamIndices;
162 std::shared_ptr<C2PortSuggestedBufferCountTuning::input> mSuggestedInputBufferCount;
163 std::shared_ptr<C2PortSuggestedBufferCountTuning::output> mSuggestedOutputBufferCount;
164
165 std::shared_ptr<C2CurrentWorkTuning> mCurrentWorkOrdinal;
166 std::shared_ptr<C2LastWorkQueuedTuning::input> mLastInputQueuedWorkOrdinal;
167 std::shared_ptr<C2LastWorkQueuedTuning::output> mLastOutputQueuedWorkOrdinal;
168
169 std::shared_ptr<C2PortAllocatorsTuning::input> mInputAllocators;
170 std::shared_ptr<C2PortAllocatorsTuning::output> mOutputAllocators;
171 std::shared_ptr<C2PrivateAllocatorsTuning> mPrivateAllocators;
172 std::shared_ptr<C2PortBlockPoolsTuning::output> mOutputPoolIds;
173 std::shared_ptr<C2PrivateBlockPoolsTuning> mPrivatePoolIds;
174
175 std::shared_ptr<C2TrippedTuning> mTripped;
176 std::shared_ptr<C2OutOfMemoryTuning> mOutOfMemory;
177
178 std::shared_ptr<C2PortConfigCounterTuning::input> mInputConfigCounter;
179 std::shared_ptr<C2PortConfigCounterTuning::output> mOutputConfigCounter;
180 std::shared_ptr<C2ConfigCounterTuning> mDirectConfigCounter;
181 };
182 };
183
184 template<typename T>
185 using SimpleInterface = SimpleC2Interface<T>;
186
187 template<typename T, typename ...Args>
AllocSharedString(const Args (&...args),const char * str)188 std::shared_ptr<T> AllocSharedString(const Args(&... args), const char *str) {
189 size_t len = strlen(str) + 1;
190 std::shared_ptr<T> ret = T::AllocShared(len, args...);
191 strcpy(ret->m.value, str);
192 return ret;
193 }
194
195 template<typename T, typename ...Args>
AllocSharedString(const Args (&...args),const std::string & str)196 std::shared_ptr<T> AllocSharedString(const Args(&... args), const std::string &str) {
197 std::shared_ptr<T> ret = T::AllocShared(str.length() + 1, args...);
198 strcpy(ret->m.value, str.c_str());
199 return ret;
200 }
201
202 template <typename T>
203 struct Setter {
204 typedef typename std::remove_reference<T>::type type;
205
NonStrictValueWithNoDepsSetter206 static C2R NonStrictValueWithNoDeps(
207 bool mayBlock, C2InterfaceHelper::C2P<type> &me) {
208 (void)mayBlock;
209 return me.F(me.v.value).validatePossible(me.v.value);
210 }
211
212 // TODO(b/230146771): fix crash
NonStrictValuesWithNoDepsSetter213 static C2R NonStrictValuesWithNoDeps(
214 bool mayBlock, C2InterfaceHelper::C2P<type> &me) {
215 (void)mayBlock;
216 C2R res = C2R::Ok();
217 for (size_t ix = 0; ix < me.v.flexCount(); ++ix) {
218 res.plus(me.F(me.v.m.values[ix]).validatePossible(me.v.m.values[ix]));
219 }
220 return res;
221 }
222
StrictValueWithNoDepsSetter223 static C2R StrictValueWithNoDeps(
224 bool mayBlock,
225 const C2InterfaceHelper::C2P<type> &old,
226 C2InterfaceHelper::C2P<type> &me) {
227 (void)mayBlock;
228 if (!me.F(me.v.value).supportsNow(me.v.value)) {
229 me.set().value = old.v.value;
230 }
231 return me.F(me.v.value).validatePossible(me.v.value);
232 }
233 };
234
235 } // namespace android
236
237 #endif // ANDROID_SIMPLE_C2_INTERFACE_H_
238