blob: a137dbbf2891d140d45ae8dd264cbddfeaef812c [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002 * Copyright 2018 The Android Open Source Project
Pawin Vongmasa36653902018-11-15 00:10:25 -08003 *
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//#define LOG_NDEBUG 0
18#define LOG_TAG "Codec2Client"
Arun Johnson7ba67072023-11-06 22:23:04 +000019#define ATRACE_TAG ATRACE_TAG_VIDEO
Pawin Vongmasa1c75a232019-01-09 04:41:52 -080020#include <android-base/logging.h>
Arun Johnson7ba67072023-11-06 22:23:04 +000021#include <utils/Trace.h>
Sungtak Leeaa71b5c2023-11-04 09:21:15 +000022
23#include <codec2/aidl/GraphicBufferAllocator.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080024#include <codec2/hidl/client.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070025#include <C2Debug.h>
26#include <C2BufferPriv.h>
Lajos Molnar78aa7c92021-02-18 21:39:01 -080027#include <C2Config.h> // for C2StreamUsageTuning
Pawin Vongmasabf69de92019-10-29 06:21:27 -070028#include <C2PlatformSupport.h>
29
30#include <android/hardware/media/bufferpool/2.0/IClientManager.h>
31#include <android/hardware/media/c2/1.0/IComponent.h>
32#include <android/hardware/media/c2/1.0/IComponentInterface.h>
33#include <android/hardware/media/c2/1.0/IComponentListener.h>
34#include <android/hardware/media/c2/1.0/IComponentStore.h>
35#include <android/hardware/media/c2/1.0/IConfigurable.h>
36#include <android/hidl/manager/1.2/IServiceManager.h>
37
Wonsik Kime8e98152022-12-16 16:04:17 -080038#include <aidl/android/hardware/media/bufferpool2/IClientManager.h>
39#include <aidl/android/hardware/media/c2/BnComponentListener.h>
Wonsik Kimc8fc2c32022-12-12 14:43:00 -080040#include <aidl/android/hardware/media/c2/FieldSupportedValues.h>
41#include <aidl/android/hardware/media/c2/FieldSupportedValuesQuery.h>
42#include <aidl/android/hardware/media/c2/FieldSupportedValuesQueryResult.h>
43#include <aidl/android/hardware/media/c2/IComponent.h>
44#include <aidl/android/hardware/media/c2/IComponentInterface.h>
45#include <aidl/android/hardware/media/c2/IComponentStore.h>
46#include <aidl/android/hardware/media/c2/IConfigurable.h>
47#include <aidl/android/hardware/media/c2/ParamDescriptor.h>
Wonsik Kime8e98152022-12-16 16:04:17 -080048#include <aidl/android/hardware/media/c2/StructDescriptor.h>
Wonsik Kimc8fc2c32022-12-12 14:43:00 -080049
Wonsik Kime8e98152022-12-16 16:04:17 -080050#include <aidlcommonsupport/NativeHandle.h>
Wonsik Kim138db0d2023-11-02 16:02:01 -070051#include <android/api-level.h>
Wonsik Kime8e98152022-12-16 16:04:17 -080052#include <android/binder_auto_utils.h>
53#include <android/binder_ibinder.h>
54#include <android/binder_manager.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070055#include <android-base/properties.h>
Wonsik Kime8e98152022-12-16 16:04:17 -080056#include <android-base/stringprintf.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070057#include <bufferpool/ClientManager.h>
Wonsik Kime8e98152022-12-16 16:04:17 -080058#include <bufferpool2/ClientManager.h>
59#include <codec2/aidl/BufferTypes.h>
Wonsik Kim1caded02022-12-09 13:03:11 -080060#include <codec2/aidl/ParamTypes.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070061#include <codec2/hidl/1.0/types.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070062#include <codec2/hidl/1.1/types.h>
Sungtak Lee8577dab2021-03-12 02:25:50 -080063#include <codec2/hidl/1.2/types.h>
Sungtak Leea714f112021-03-16 05:40:03 -070064#include <codec2/hidl/output.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070065
66#include <cutils/native_handle.h>
67#include <gui/bufferqueue/2.0/B2HGraphicBufferProducer.h>
68#include <gui/bufferqueue/2.0/H2BGraphicBufferProducer.h>
Lajos Molnar78aa7c92021-02-18 21:39:01 -080069#include <hardware/gralloc.h> // for GRALLOC_USAGE_*
Pawin Vongmasabf69de92019-10-29 06:21:27 -070070#include <hidl/HidlSupport.h>
Lajos Molnar78aa7c92021-02-18 21:39:01 -080071#include <system/window.h> // for NATIVE_WINDOW_QUERY_*
72#include <media/stagefright/foundation/ADebug.h> // for asString(status_t)
Pawin Vongmasa36653902018-11-15 00:10:25 -080073
74#include <deque>
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070075#include <iterator>
Pawin Vongmasa36653902018-11-15 00:10:25 -080076#include <limits>
77#include <map>
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070078#include <mutex>
Sungtak Leeaa71b5c2023-11-04 09:21:15 +000079#include <optional>
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070080#include <sstream>
81#include <thread>
Pawin Vongmasa36653902018-11-15 00:10:25 -080082#include <type_traits>
83#include <vector>
84
Pawin Vongmasa36653902018-11-15 00:10:25 -080085namespace android {
86
87using ::android::hardware::hidl_vec;
88using ::android::hardware::hidl_string;
89using ::android::hardware::Return;
90using ::android::hardware::Void;
Pawin Vongmasa36653902018-11-15 00:10:25 -080091
Pawin Vongmasaef939bf2019-03-03 04:44:59 -080092using HGraphicBufferProducer1 = ::android::hardware::graphics::bufferqueue::
93 V1_0::IGraphicBufferProducer;
94using HGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
95 V2_0::IGraphicBufferProducer;
96using B2HGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
97 V2_0::utils::B2HGraphicBufferProducer;
98using H2BGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
99 V2_0::utils::H2BGraphicBufferProducer;
Sungtak Leea714f112021-03-16 05:40:03 -0700100using ::android::hardware::media::c2::V1_2::SurfaceSyncObj;
Pawin Vongmasaef939bf2019-03-03 04:44:59 -0800101
Sungtak Leeaa71b5c2023-11-04 09:21:15 +0000102using AidlGraphicBufferAllocator = ::aidl::android::hardware::media::c2::
103 implementation::GraphicBufferAllocator;
104
Wonsik Kime8e98152022-12-16 16:04:17 -0800105namespace bufferpool2_aidl = ::aidl::android::hardware::media::bufferpool2;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800106namespace bufferpool_hidl = ::android::hardware::media::bufferpool::V2_0;
107namespace c2_aidl = ::aidl::android::hardware::media::c2;
108namespace c2_hidl_base = ::android::hardware::media::c2;
109namespace c2_hidl = ::android::hardware::media::c2::V1_2;
110
111using c2_hidl::utils::operator<<;
112
Pawin Vongmasa36653902018-11-15 00:10:25 -0800113namespace /* unnamed */ {
114
115// c2_status_t value that corresponds to hwbinder transaction failure.
116constexpr c2_status_t C2_TRANSACTION_FAILED = C2_CORRUPTED;
117
Lajos Molnar78aa7c92021-02-18 21:39:01 -0800118// By default prepare buffer to be displayed on any of the common surfaces
119constexpr uint64_t kDefaultConsumerUsage =
120 (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER);
121
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700122// Searches for a name in GetServiceNames() and returns the index found. If the
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700123// name is not found, the returned index will be equal to
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700124// GetServiceNames().size().
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700125size_t getServiceIndex(char const* name) {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700126 std::vector<std::string> const& names = Codec2Client::GetServiceNames();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700127 size_t i = 0;
128 for (; i < names.size(); ++i) {
129 if (name == names[i]) {
130 break;
131 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800132 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700133 return i;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800134}
135
Pawin Vongmasa83d2c552020-03-05 04:36:08 -0800136class Client2Store : public C2ComponentStore {
137 std::shared_ptr<Codec2Client> mClient;
138
139public:
140 Client2Store(std::shared_ptr<Codec2Client> const& client)
141 : mClient(client) { }
142
143 virtual ~Client2Store() = default;
144
145 virtual c2_status_t config_sm(
146 std::vector<C2Param*> const &params,
147 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
148 return mClient->config(params, C2_MAY_BLOCK, failures);
149 };
150
151 virtual c2_status_t copyBuffer(
152 std::shared_ptr<C2GraphicBuffer>,
153 std::shared_ptr<C2GraphicBuffer>) {
154 return C2_OMITTED;
155 }
156
157 virtual c2_status_t createComponent(
158 C2String, std::shared_ptr<C2Component>* const component) {
159 component->reset();
160 return C2_OMITTED;
161 }
162
163 virtual c2_status_t createInterface(
164 C2String, std::shared_ptr<C2ComponentInterface>* const interface) {
165 interface->reset();
166 return C2_OMITTED;
167 }
168
169 virtual c2_status_t query_sm(
170 std::vector<C2Param*> const& stackParams,
171 std::vector<C2Param::Index> const& heapParamIndices,
172 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
173 return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
174 }
175
176 virtual c2_status_t querySupportedParams_nb(
177 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
178 return mClient->querySupportedParams(params);
179 }
180
181 virtual c2_status_t querySupportedValues_sm(
182 std::vector<C2FieldSupportedValuesQuery>& fields) const {
183 return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
184 }
185
186 virtual C2String getName() const {
187 return mClient->getName();
188 }
189
190 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
191 return mClient->getParamReflector();
192 }
193
194 virtual std::vector<std::shared_ptr<C2Component::Traits const>> listComponents() {
195 return std::vector<std::shared_ptr<C2Component::Traits const>>();
196 }
197};
198
Wonsik Kime8e98152022-12-16 16:04:17 -0800199c2_status_t GetC2Status(const ::ndk::ScopedAStatus &transStatus, const char *method) {
200 if (!transStatus.isOk()) {
201 if (transStatus.getExceptionCode() == EX_SERVICE_SPECIFIC) {
202 c2_status_t status = static_cast<c2_status_t>(transStatus.getServiceSpecificError());
203 LOG(DEBUG) << method << " -- call failed: " << status << ".";
204 return status;
205 } else {
206 LOG(ERROR) << method << " -- transaction failed.";
207 return C2_TRANSACTION_FAILED;
208 }
209 }
210 return C2_OK;
211}
212
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700213} // unnamed namespace
214
215// This class caches a Codec2Client object and its component traits. The client
216// will be created the first time it is needed, and it can be refreshed if the
217// service dies (by calling invalidate()). The first time listComponents() is
218// called from the client, the result will be cached.
219class Codec2Client::Cache {
220 // Cached client
221 std::shared_ptr<Codec2Client> mClient;
222 mutable std::mutex mClientMutex;
223
224 // Cached component traits
225 std::vector<C2Component::Traits> mTraits;
226 std::once_flag mTraitsInitializationFlag;
227
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700228 // The index of the service. This is based on GetServiceNames().
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700229 size_t mIndex;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700230 // Called by s() exactly once to initialize the cache. The index must be a
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700231 // valid index into the vector returned by GetServiceNames(). Calling
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700232 // init(index) will associate the cache to the service with name
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700233 // GetServiceNames()[index].
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700234 void init(size_t index) {
235 mIndex = index;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700236 }
237
238public:
239 Cache() = default;
240
241 // Initializes mClient if needed, then returns mClient.
242 // If the service is unavailable but listed in the manifest, this function
243 // will block indefinitely.
244 std::shared_ptr<Codec2Client> getClient() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700245 std::scoped_lock lock{mClientMutex};
246 if (!mClient) {
247 mClient = Codec2Client::_CreateFromIndex(mIndex);
248 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -0700249 CHECK(mClient) << "Failed to create Codec2Client to service \""
250 << GetServiceNames()[mIndex] << "\". (Index = "
251 << mIndex << ").";
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700252 return mClient;
253 }
254
255 // Causes a subsequent call to getClient() to create a new client. This
256 // function should be called after the service dies.
257 //
258 // Note: This function is called only by ForAllServices().
259 void invalidate() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700260 std::scoped_lock lock{mClientMutex};
261 mClient = nullptr;
262 }
263
264 // Returns a list of traits for components supported by the service. This
265 // list is cached.
266 std::vector<C2Component::Traits> const& getTraits() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700267 std::call_once(mTraitsInitializationFlag, [this]() {
268 bool success{false};
269 // Spin until _listComponents() is successful.
270 while (true) {
271 std::shared_ptr<Codec2Client> client = getClient();
272 mTraits = client->_listComponents(&success);
273 if (success) {
274 break;
275 }
Pawin Vongmasa21617db2020-04-24 06:16:42 -0700276 invalidate();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700277 using namespace std::chrono_literals;
278 static constexpr auto kServiceRetryPeriod = 5s;
279 LOG(INFO) << "Failed to retrieve component traits from service "
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700280 "\"" << GetServiceNames()[mIndex] << "\". "
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700281 "Retrying...";
282 std::this_thread::sleep_for(kServiceRetryPeriod);
283 }
284 });
285 return mTraits;
286 }
287
288 // List() returns the list of all caches.
289 static std::vector<Cache>& List() {
290 static std::vector<Cache> sCaches{[]() {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700291 size_t numServices = GetServiceNames().size();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700292 std::vector<Cache> caches(numServices);
293 for (size_t i = 0; i < numServices; ++i) {
294 caches[i].init(i);
295 }
296 return caches;
297 }()};
298 return sCaches;
299 }
300};
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800301// Codec2ConfigurableClient::HidlImpl
Pawin Vongmasa36653902018-11-15 00:10:25 -0800302
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800303struct Codec2ConfigurableClient::HidlImpl : public Codec2ConfigurableClient::ImplBase {
304 typedef c2_hidl::IConfigurable Base;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800305
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800306 // base cannot be null.
307 explicit HidlImpl(const sp<Base>& base);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800308
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800309 const C2String& getName() const override {
310 return mName;
311 }
312
313 c2_status_t query(
314 const std::vector<C2Param*>& stackParams,
315 const std::vector<C2Param::Index> &heapParamIndices,
316 c2_blocking_t mayBlock,
317 std::vector<std::unique_ptr<C2Param>>* const heapParams) const override;
318
319 c2_status_t config(
320 const std::vector<C2Param*> &params,
321 c2_blocking_t mayBlock,
322 std::vector<std::unique_ptr<C2SettingResult>>* const failures) override;
323
324 c2_status_t querySupportedParams(
325 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params
326 ) const override;
327
328 c2_status_t querySupportedValues(
329 std::vector<C2FieldSupportedValuesQuery>& fields,
330 c2_blocking_t mayBlock) const override;
331
332private:
333 sp<Base> mBase;
334 const C2String mName;
335};
336
337Codec2ConfigurableClient::HidlImpl::HidlImpl(const sp<Base>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800338 : mBase{base},
339 mName{[base]() -> C2String {
340 C2String outName;
341 Return<void> transStatus = base->getName(
342 [&outName](const hidl_string& name) {
343 outName = name.c_str();
344 });
345 return transStatus.isOk() ? outName : "";
346 }()} {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800347}
348
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800349c2_status_t Codec2ConfigurableClient::HidlImpl::query(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800350 const std::vector<C2Param*> &stackParams,
351 const std::vector<C2Param::Index> &heapParamIndices,
352 c2_blocking_t mayBlock,
353 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800354 hidl_vec<c2_hidl::ParamIndex> indices(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800355 stackParams.size() + heapParamIndices.size());
356 size_t numIndices = 0;
357 for (C2Param* const& stackParam : stackParams) {
358 if (!stackParam) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800359 LOG(WARNING) << "query -- null stack param encountered.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800360 continue;
361 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800362 indices[numIndices++] = static_cast<c2_hidl::ParamIndex>(stackParam->index());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800363 }
364 size_t numStackIndices = numIndices;
365 for (const C2Param::Index& index : heapParamIndices) {
366 indices[numIndices++] =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800367 static_cast<c2_hidl::ParamIndex>(static_cast<uint32_t>(index));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800368 }
369 indices.resize(numIndices);
370 if (heapParams) {
371 heapParams->reserve(heapParams->size() + numIndices);
372 }
373 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800374 Return<void> transStatus = mBase->query(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800375 indices,
376 mayBlock == C2_MAY_BLOCK,
377 [&status, &numStackIndices, &stackParams, heapParams](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800378 c2_hidl::Status s, const c2_hidl::Params& p) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800379 status = static_cast<c2_status_t>(s);
380 if (status != C2_OK && status != C2_BAD_INDEX) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800381 LOG(DEBUG) << "query -- call failed: "
382 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800383 return;
384 }
385 std::vector<C2Param*> paramPointers;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800386 if (!c2_hidl::utils::parseParamsBlob(&paramPointers, p)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800387 LOG(ERROR) << "query -- error while parsing params.";
388 status = C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800389 return;
390 }
391 size_t i = 0;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800392 for (auto it = paramPointers.begin();
393 it != paramPointers.end(); ) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800394 C2Param* paramPointer = *it;
395 if (numStackIndices > 0) {
396 --numStackIndices;
397 if (!paramPointer) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800398 LOG(WARNING) << "query -- null stack param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800399 ++it;
400 continue;
401 }
402 for (; i < stackParams.size() && !stackParams[i]; ) {
403 ++i;
404 }
405 if (i >= stackParams.size()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800406 LOG(ERROR) << "query -- unexpected error.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800407 status = C2_CORRUPTED;
408 return;
409 }
410 if (stackParams[i]->index() != paramPointer->index()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800411 LOG(WARNING) << "query -- param skipped: "
412 "index = "
413 << stackParams[i]->index() << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800414 stackParams[i++]->invalidate();
415 continue;
416 }
417 if (!stackParams[i++]->updateFrom(*paramPointer)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800418 LOG(WARNING) << "query -- param update failed: "
419 "index = "
420 << paramPointer->index() << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800421 }
422 } else {
423 if (!paramPointer) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800424 LOG(WARNING) << "query -- null heap param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800425 ++it;
426 continue;
427 }
428 if (!heapParams) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800429 LOG(WARNING) << "query -- "
430 "unexpected extra stack param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800431 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800432 heapParams->emplace_back(
433 C2Param::Copy(*paramPointer));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800434 }
435 }
436 ++it;
437 }
438 });
439 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800440 LOG(ERROR) << "query -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800441 return C2_TRANSACTION_FAILED;
442 }
443 return status;
444}
445
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800446c2_status_t Codec2ConfigurableClient::HidlImpl::config(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800447 const std::vector<C2Param*> &params,
448 c2_blocking_t mayBlock,
449 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800450 c2_hidl::Params hidlParams;
451 if (!c2_hidl::utils::createParamsBlob(&hidlParams, params)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800452 LOG(ERROR) << "config -- bad input.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800453 return C2_TRANSACTION_FAILED;
454 }
455 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800456 Return<void> transStatus = mBase->config(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800457 hidlParams,
458 mayBlock == C2_MAY_BLOCK,
459 [&status, &params, failures](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800460 c2_hidl::Status s,
461 const hidl_vec<c2_hidl::SettingResult> f,
462 const c2_hidl::Params& o) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800463 status = static_cast<c2_status_t>(s);
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800464 if (status != C2_OK && status != C2_BAD_INDEX) {
465 LOG(DEBUG) << "config -- call failed: "
466 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800467 }
468 size_t i = failures->size();
469 failures->resize(i + f.size());
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800470 for (const c2_hidl::SettingResult& sf : f) {
471 if (!c2_hidl::utils::objcpy(&(*failures)[i++], sf)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800472 LOG(ERROR) << "config -- "
473 << "invalid SettingResult returned.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800474 return;
475 }
476 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800477 if (!c2_hidl::utils::updateParamsFromBlob(params, o)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800478 LOG(ERROR) << "config -- "
479 << "failed to parse returned params.";
480 status = C2_CORRUPTED;
481 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800482 });
483 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800484 LOG(ERROR) << "config -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800485 return C2_TRANSACTION_FAILED;
486 }
487 return status;
488}
489
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800490c2_status_t Codec2ConfigurableClient::HidlImpl::querySupportedParams(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800491 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
492 // TODO: Cache and query properly!
493 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800494 Return<void> transStatus = mBase->querySupportedParams(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800495 std::numeric_limits<uint32_t>::min(),
496 std::numeric_limits<uint32_t>::max(),
497 [&status, params](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800498 c2_hidl::Status s,
499 const hidl_vec<c2_hidl::ParamDescriptor>& p) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800500 status = static_cast<c2_status_t>(s);
501 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800502 LOG(DEBUG) << "querySupportedParams -- call failed: "
503 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800504 return;
505 }
506 size_t i = params->size();
507 params->resize(i + p.size());
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800508 for (const c2_hidl::ParamDescriptor& sp : p) {
509 if (!c2_hidl::utils::objcpy(&(*params)[i++], sp)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800510 LOG(ERROR) << "querySupportedParams -- "
511 << "invalid returned ParamDescriptor.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800512 return;
513 }
514 }
515 });
516 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800517 LOG(ERROR) << "querySupportedParams -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800518 return C2_TRANSACTION_FAILED;
519 }
520 return status;
521}
522
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800523c2_status_t Codec2ConfigurableClient::HidlImpl::querySupportedValues(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800524 std::vector<C2FieldSupportedValuesQuery>& fields,
525 c2_blocking_t mayBlock) const {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800526 hidl_vec<c2_hidl::FieldSupportedValuesQuery> inFields(fields.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800527 for (size_t i = 0; i < fields.size(); ++i) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800528 if (!c2_hidl::utils::objcpy(&inFields[i], fields[i])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800529 LOG(ERROR) << "querySupportedValues -- bad input";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800530 return C2_TRANSACTION_FAILED;
531 }
532 }
533
534 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800535 Return<void> transStatus = mBase->querySupportedValues(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800536 inFields,
537 mayBlock == C2_MAY_BLOCK,
538 [&status, &inFields, &fields](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800539 c2_hidl::Status s,
540 const hidl_vec<c2_hidl::FieldSupportedValuesQueryResult>& r) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800541 status = static_cast<c2_status_t>(s);
542 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800543 LOG(DEBUG) << "querySupportedValues -- call failed: "
544 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800545 return;
546 }
547 if (r.size() != fields.size()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800548 LOG(ERROR) << "querySupportedValues -- "
549 "input and output lists "
550 "have different sizes.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800551 status = C2_CORRUPTED;
552 return;
553 }
554 for (size_t i = 0; i < fields.size(); ++i) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800555 if (!c2_hidl::utils::objcpy(&fields[i], inFields[i], r[i])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800556 LOG(ERROR) << "querySupportedValues -- "
557 "invalid returned value.";
558 status = C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800559 return;
560 }
561 }
562 });
563 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800564 LOG(ERROR) << "querySupportedValues -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800565 return C2_TRANSACTION_FAILED;
566 }
567 return status;
568}
569
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800570// Codec2ConfigurableClient::AidlImpl
571
572struct Codec2ConfigurableClient::AidlImpl : public Codec2ConfigurableClient::ImplBase {
573 typedef c2_aidl::IConfigurable Base;
574
575 // base cannot be null.
576 explicit AidlImpl(const std::shared_ptr<Base>& base);
577
578 const C2String& getName() const override {
579 return mName;
580 }
581
582 c2_status_t query(
583 const std::vector<C2Param*>& stackParams,
584 const std::vector<C2Param::Index> &heapParamIndices,
585 c2_blocking_t mayBlock,
586 std::vector<std::unique_ptr<C2Param>>* const heapParams) const override;
587
588 c2_status_t config(
589 const std::vector<C2Param*> &params,
590 c2_blocking_t mayBlock,
591 std::vector<std::unique_ptr<C2SettingResult>>* const failures) override;
592
593 c2_status_t querySupportedParams(
594 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params
595 ) const override;
596
597 c2_status_t querySupportedValues(
598 std::vector<C2FieldSupportedValuesQuery>& fields,
599 c2_blocking_t mayBlock) const override;
600
601private:
602 std::shared_ptr<Base> mBase;
603 const C2String mName;
604};
605
606Codec2ConfigurableClient::AidlImpl::AidlImpl(const std::shared_ptr<Base>& base)
607 : mBase{base},
608 mName{[base]() -> C2String {
609 std::string outName;
610 ndk::ScopedAStatus status = base->getName(&outName);
611 return status.isOk() ? outName : "";
612 }()} {
613}
614
615c2_status_t Codec2ConfigurableClient::AidlImpl::query(
616 const std::vector<C2Param*> &stackParams,
617 const std::vector<C2Param::Index> &heapParamIndices,
618 c2_blocking_t mayBlock,
619 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
Wonsik Kim1caded02022-12-09 13:03:11 -0800620 std::vector<int> indices(
621 stackParams.size() + heapParamIndices.size());
622 size_t numIndices = 0;
623 for (C2Param* const& stackParam : stackParams) {
624 if (!stackParam) {
625 LOG(WARNING) << "query -- null stack param encountered.";
626 continue;
627 }
628 indices[numIndices++] = int(stackParam->index());
629 }
630 size_t numStackIndices = numIndices;
631 for (const C2Param::Index& index : heapParamIndices) {
632 indices[numIndices++] = int(static_cast<uint32_t>(index));
633 }
634 indices.resize(numIndices);
635 if (heapParams) {
636 heapParams->reserve(heapParams->size() + numIndices);
637 }
Sungtak Lee932255f2024-01-03 10:57:45 +0000638 c2_aidl::IConfigurable::QueryResult result;
Wonsik Kim1caded02022-12-09 13:03:11 -0800639 ndk::ScopedAStatus transStatus = mBase->query(indices, (mayBlock == C2_MAY_BLOCK), &result);
Wonsik Kime8e98152022-12-16 16:04:17 -0800640 c2_status_t status = GetC2Status(transStatus, "query");
641 if (status != C2_OK) {
642 return status;
Wonsik Kim1caded02022-12-09 13:03:11 -0800643 }
Sungtak Lee932255f2024-01-03 10:57:45 +0000644 status = static_cast<c2_status_t>(result.status.status);
Wonsik Kim1caded02022-12-09 13:03:11 -0800645
Wonsik Kim1caded02022-12-09 13:03:11 -0800646 std::vector<C2Param*> paramPointers;
Sungtak Lee932255f2024-01-03 10:57:45 +0000647 if (!c2_aidl::utils::ParseParamsBlob(&paramPointers, result.params)) {
Wonsik Kim1caded02022-12-09 13:03:11 -0800648 LOG(ERROR) << "query -- error while parsing params.";
649 return C2_CORRUPTED;
650 }
651 size_t i = 0;
Wonsik Kim095e72e2024-02-27 11:33:21 -0800652 size_t numQueried = 0;
Wonsik Kim977176c2023-12-18 17:14:20 -0800653 for (auto it = paramPointers.begin(); it != paramPointers.end(); ) {
Wonsik Kim1caded02022-12-09 13:03:11 -0800654 C2Param* paramPointer = *it;
655 if (numStackIndices > 0) {
656 --numStackIndices;
657 if (!paramPointer) {
658 LOG(DEBUG) << "query -- null stack param.";
659 ++it;
660 continue;
661 }
662 for (; i < stackParams.size() && !stackParams[i]; ) {
663 ++i;
664 }
665 if (i >= stackParams.size()) {
666 LOG(ERROR) << "query -- unexpected error.";
667 status = C2_CORRUPTED;
668 break;
669 }
670 if (stackParams[i]->index() != paramPointer->index()) {
671 LOG(DEBUG) << "query -- param skipped: "
672 "index = "
673 << stackParams[i]->index() << ".";
674 stackParams[i++]->invalidate();
675 // this means that the param could not be queried.
676 // signalling C2_BAD_INDEX to the client.
677 status = C2_BAD_INDEX;
678 continue;
679 }
Wonsik Kim977176c2023-12-18 17:14:20 -0800680 if (stackParams[i++]->updateFrom(*paramPointer)) {
Wonsik Kim095e72e2024-02-27 11:33:21 -0800681 ++numQueried;
Wonsik Kim977176c2023-12-18 17:14:20 -0800682 } else {
Wonsik Kim1caded02022-12-09 13:03:11 -0800683 LOG(WARNING) << "query -- param update failed: "
684 "index = "
685 << paramPointer->index() << ".";
686 }
687 } else {
688 if (!paramPointer) {
689 LOG(DEBUG) << "query -- null heap param.";
690 ++it;
691 continue;
692 }
693 if (!heapParams) {
694 LOG(WARNING) << "query -- "
695 "unexpected extra stack param.";
696 } else {
697 heapParams->emplace_back(C2Param::Copy(*paramPointer));
Wonsik Kim095e72e2024-02-27 11:33:21 -0800698 ++numQueried;
Wonsik Kim1caded02022-12-09 13:03:11 -0800699 }
700 }
701 ++it;
702 }
Wonsik Kim977176c2023-12-18 17:14:20 -0800703 if (status == C2_OK && indices.size() != numQueried) {
704 status = C2_BAD_INDEX;
705 }
Wonsik Kim1caded02022-12-09 13:03:11 -0800706 return status;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800707}
708
709c2_status_t Codec2ConfigurableClient::AidlImpl::config(
710 const std::vector<C2Param*> &params,
711 c2_blocking_t mayBlock,
712 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
Wonsik Kim1caded02022-12-09 13:03:11 -0800713 c2_aidl::Params aidlParams;
714 if (!c2_aidl::utils::CreateParamsBlob(&aidlParams, params)) {
715 LOG(ERROR) << "config -- bad input.";
716 return C2_TRANSACTION_FAILED;
717 }
718 c2_aidl::IConfigurable::ConfigResult result;
719 ndk::ScopedAStatus transStatus = mBase->config(aidlParams, (mayBlock == C2_MAY_BLOCK), &result);
Wonsik Kime8e98152022-12-16 16:04:17 -0800720 c2_status_t status = GetC2Status(transStatus, "config");
721 if (status != C2_OK) {
722 return status;
Wonsik Kim1caded02022-12-09 13:03:11 -0800723 }
Sungtak Lee932255f2024-01-03 10:57:45 +0000724 status = static_cast<c2_status_t>(result.status.status);
Wonsik Kim1caded02022-12-09 13:03:11 -0800725 size_t i = failures->size();
726 failures->resize(i + result.failures.size());
727 for (const c2_aidl::SettingResult& sf : result.failures) {
728 if (!c2_aidl::utils::FromAidl(&(*failures)[i++], sf)) {
729 LOG(ERROR) << "config -- invalid SettingResult returned.";
730 return C2_CORRUPTED;
731 }
732 }
733 if (!c2_aidl::utils::UpdateParamsFromBlob(params, result.params)) {
734 LOG(ERROR) << "config -- "
735 << "failed to parse returned params.";
736 status = C2_CORRUPTED;
737 }
738 return status;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800739}
740
741c2_status_t Codec2ConfigurableClient::AidlImpl::querySupportedParams(
742 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
Wonsik Kim1caded02022-12-09 13:03:11 -0800743 // TODO: Cache and query properly!
744 std::vector<c2_aidl::ParamDescriptor> result;
745 ndk::ScopedAStatus transStatus = mBase->querySupportedParams(
746 std::numeric_limits<uint32_t>::min(),
747 std::numeric_limits<uint32_t>::max(),
748 &result);
Wonsik Kime8e98152022-12-16 16:04:17 -0800749 c2_status_t status = GetC2Status(transStatus, "querySupportedParams");
750 if (status != C2_OK) {
751 return status;
Wonsik Kim1caded02022-12-09 13:03:11 -0800752 }
Wonsik Kim1caded02022-12-09 13:03:11 -0800753 size_t i = params->size();
754 params->resize(i + result.size());
755 for (const c2_aidl::ParamDescriptor& sp : result) {
756 if (!c2_aidl::utils::FromAidl(&(*params)[i++], sp)) {
757 LOG(ERROR) << "querySupportedParams -- invalid returned ParamDescriptor.";
758 return C2_CORRUPTED;
759 }
760 }
761 return status;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800762}
763
764c2_status_t Codec2ConfigurableClient::AidlImpl::querySupportedValues(
765 std::vector<C2FieldSupportedValuesQuery>& fields,
766 c2_blocking_t mayBlock) const {
Wonsik Kim1caded02022-12-09 13:03:11 -0800767 std::vector<c2_aidl::FieldSupportedValuesQuery> inFields(fields.size());
768 for (size_t i = 0; i < fields.size(); ++i) {
769 if (!c2_aidl::utils::ToAidl(&inFields[i], fields[i])) {
770 LOG(ERROR) << "querySupportedValues -- bad input";
771 return C2_TRANSACTION_FAILED;
772 }
773 }
774
Sungtak Lee932255f2024-01-03 10:57:45 +0000775 c2_aidl::IConfigurable::QuerySupportedValuesResult result;
776
Wonsik Kim1caded02022-12-09 13:03:11 -0800777 ndk::ScopedAStatus transStatus = mBase->querySupportedValues(
778 inFields, (mayBlock == C2_MAY_BLOCK), &result);
Wonsik Kime8e98152022-12-16 16:04:17 -0800779 c2_status_t status = GetC2Status(transStatus, "querySupportedValues");
780 if (status != C2_OK) {
781 return status;
Wonsik Kim1caded02022-12-09 13:03:11 -0800782 }
Sungtak Lee932255f2024-01-03 10:57:45 +0000783 status = static_cast<c2_status_t>(result.status.status);
784 if (result.values.size() != fields.size()) {
Wonsik Kim1caded02022-12-09 13:03:11 -0800785 LOG(ERROR) << "querySupportedValues -- "
786 "input and output lists "
787 "have different sizes.";
788 return C2_CORRUPTED;
789 }
790 for (size_t i = 0; i < fields.size(); ++i) {
Sungtak Lee932255f2024-01-03 10:57:45 +0000791 if (!c2_aidl::utils::FromAidl(&fields[i], inFields[i], result.values[i])) {
Wonsik Kim1caded02022-12-09 13:03:11 -0800792 LOG(ERROR) << "querySupportedValues -- "
793 "invalid returned value.";
794 return C2_CORRUPTED;
795 }
796 }
797 return status;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800798}
799
800// Codec2ConfigurableClient
801
802Codec2ConfigurableClient::Codec2ConfigurableClient(const sp<HidlBase> &hidlBase)
803 : mImpl(new Codec2ConfigurableClient::HidlImpl(hidlBase)) {
804}
805
806Codec2ConfigurableClient::Codec2ConfigurableClient(
807 const std::shared_ptr<AidlBase> &aidlBase)
808 : mImpl(new Codec2ConfigurableClient::AidlImpl(aidlBase)) {
809}
810
811const C2String& Codec2ConfigurableClient::getName() const {
812 return mImpl->getName();
813}
814
815c2_status_t Codec2ConfigurableClient::query(
816 const std::vector<C2Param*>& stackParams,
817 const std::vector<C2Param::Index> &heapParamIndices,
818 c2_blocking_t mayBlock,
819 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
820 return mImpl->query(stackParams, heapParamIndices, mayBlock, heapParams);
821}
822
823c2_status_t Codec2ConfigurableClient::config(
824 const std::vector<C2Param*> &params,
825 c2_blocking_t mayBlock,
826 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
827 return mImpl->config(params, mayBlock, failures);
828}
829
830c2_status_t Codec2ConfigurableClient::querySupportedParams(
831 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
832 return mImpl->querySupportedParams(params);
833}
834
835c2_status_t Codec2ConfigurableClient::querySupportedValues(
836 std::vector<C2FieldSupportedValuesQuery>& fields,
837 c2_blocking_t mayBlock) const {
838 return mImpl->querySupportedValues(fields, mayBlock);
839}
840
841
Pawin Vongmasa36653902018-11-15 00:10:25 -0800842// Codec2Client::Component::HidlListener
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800843struct Codec2Client::Component::HidlListener : public c2_hidl::IComponentListener {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800844 std::weak_ptr<Component> component;
845 std::weak_ptr<Listener> base;
846
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800847 virtual Return<void> onWorkDone(const c2_hidl::WorkBundle& workBundle) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800848 std::list<std::unique_ptr<C2Work>> workItems;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800849 if (!c2_hidl::utils::objcpy(&workItems, workBundle)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800850 LOG(DEBUG) << "onWorkDone -- received corrupted WorkBundle.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800851 return Void();
852 }
853 // release input buffers potentially held by the component from queue
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800854 std::shared_ptr<Codec2Client::Component> strongComponent =
855 component.lock();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800856 if (strongComponent) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800857 strongComponent->handleOnWorkDone(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800858 }
859 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800860 listener->onWorkDone(component, workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800861 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800862 LOG(DEBUG) << "onWorkDone -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800863 }
864 return Void();
865 }
866
867 virtual Return<void> onTripped(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800868 const hidl_vec<c2_hidl::SettingResult>& settingResults) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800869 std::vector<std::shared_ptr<C2SettingResult>> c2SettingResults(
870 settingResults.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800871 for (size_t i = 0; i < settingResults.size(); ++i) {
872 std::unique_ptr<C2SettingResult> c2SettingResult;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800873 if (!c2_hidl::utils::objcpy(&c2SettingResult, settingResults[i])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800874 LOG(DEBUG) << "onTripped -- received corrupted SettingResult.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800875 return Void();
876 }
877 c2SettingResults[i] = std::move(c2SettingResult);
878 }
879 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
880 listener->onTripped(component, c2SettingResults);
881 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800882 LOG(DEBUG) << "onTripped -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800883 }
884 return Void();
885 }
886
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800887 virtual Return<void> onError(c2_hidl::Status s, uint32_t errorCode) override {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800888 LOG(DEBUG) << "onError --"
889 << " status = " << s
890 << ", errorCode = " << errorCode
891 << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800892 if (std::shared_ptr<Listener> listener = base.lock()) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800893 listener->onError(component, s == c2_hidl::Status::OK ?
Pawin Vongmasa36653902018-11-15 00:10:25 -0800894 errorCode : static_cast<c2_status_t>(s));
895 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800896 LOG(DEBUG) << "onError -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800897 }
898 return Void();
899 }
900
901 virtual Return<void> onFramesRendered(
902 const hidl_vec<RenderedFrame>& renderedFrames) override {
903 std::shared_ptr<Listener> listener = base.lock();
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800904 if (!listener) {
905 LOG(DEBUG) << "onFramesRendered -- listener died.";
906 return Void();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800907 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800908 for (const RenderedFrame& renderedFrame : renderedFrames) {
909 listener->onFrameRendered(
910 renderedFrame.bufferQueueId,
911 renderedFrame.slotId,
912 renderedFrame.timestampNs);
913 }
914 return Void();
915 }
916
917 virtual Return<void> onInputBuffersReleased(
918 const hidl_vec<InputBuffer>& inputBuffers) override {
919 std::shared_ptr<Listener> listener = base.lock();
920 if (!listener) {
921 LOG(DEBUG) << "onInputBuffersReleased -- listener died.";
922 return Void();
923 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800924 for (const InputBuffer& inputBuffer : inputBuffers) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800925 LOG(VERBOSE) << "onInputBuffersReleased --"
926 " received death notification of"
927 " input buffer:"
928 " frameIndex = " << inputBuffer.frameIndex
929 << ", bufferIndex = " << inputBuffer.arrayIndex
930 << ".";
Wonsik Kimab34ed62019-01-31 15:28:46 -0800931 listener->onInputBufferDone(
932 inputBuffer.frameIndex, inputBuffer.arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800933 }
934 return Void();
935 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800936
Pawin Vongmasa36653902018-11-15 00:10:25 -0800937};
938
Wonsik Kime8e98152022-12-16 16:04:17 -0800939// Codec2Client::Component::AidlListener
940struct Codec2Client::Component::AidlListener : public c2_aidl::BnComponentListener {
941 std::weak_ptr<Component> component;
942 std::weak_ptr<Listener> base;
943
944 virtual ::ndk::ScopedAStatus onWorkDone(const c2_aidl::WorkBundle& workBundle) override {
945 std::list<std::unique_ptr<C2Work>> workItems;
946 if (!c2_aidl::utils::FromAidl(&workItems, workBundle)) {
947 LOG(DEBUG) << "onWorkDone -- received corrupted WorkBundle.";
948 return ::ndk::ScopedAStatus::ok();
949 }
950 // release input buffers potentially held by the component from queue
951 std::shared_ptr<Codec2Client::Component> strongComponent =
952 component.lock();
953 if (strongComponent) {
954 strongComponent->handleOnWorkDone(workItems);
955 }
956 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
957 listener->onWorkDone(component, workItems);
958 } else {
959 LOG(DEBUG) << "onWorkDone -- listener died.";
960 }
961 return ::ndk::ScopedAStatus::ok();
962 }
963
964 virtual ::ndk::ScopedAStatus onTripped(
965 const std::vector<c2_aidl::SettingResult>& settingResults) override {
966 std::vector<std::shared_ptr<C2SettingResult>> c2SettingResults(
967 settingResults.size());
968 for (size_t i = 0; i < settingResults.size(); ++i) {
969 std::unique_ptr<C2SettingResult> c2SettingResult;
970 if (!c2_aidl::utils::FromAidl(&c2SettingResult, settingResults[i])) {
971 LOG(DEBUG) << "onTripped -- received corrupted SettingResult.";
972 return ::ndk::ScopedAStatus::ok();
973 }
974 c2SettingResults[i] = std::move(c2SettingResult);
975 }
976 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
977 listener->onTripped(component, c2SettingResults);
978 } else {
979 LOG(DEBUG) << "onTripped -- listener died.";
980 }
981 return ::ndk::ScopedAStatus::ok();
982 }
983
984 virtual ::ndk::ScopedAStatus onError(const c2_aidl::Status &s, int32_t errorCode) override {
985 LOG(DEBUG) << "onError --"
986 << " status = " << s.status
987 << ", errorCode = " << errorCode
988 << ".";
989 if (std::shared_ptr<Listener> listener = base.lock()) {
990 listener->onError(component, s.status == c2_aidl::Status::OK ?
991 errorCode : static_cast<c2_status_t>(s.status));
992 } else {
993 LOG(DEBUG) << "onError -- listener died.";
994 }
995 return ::ndk::ScopedAStatus::ok();
996 }
997
998 virtual ::ndk::ScopedAStatus onFramesRendered(
999 const std::vector<RenderedFrame>& renderedFrames) override {
1000 std::shared_ptr<Listener> listener = base.lock();
1001 if (!listener) {
1002 LOG(DEBUG) << "onFramesRendered -- listener died.";
1003 return ::ndk::ScopedAStatus::ok();
1004 }
1005 for (const RenderedFrame& renderedFrame : renderedFrames) {
1006 listener->onFrameRendered(
1007 renderedFrame.bufferQueueId,
1008 renderedFrame.slotId,
1009 renderedFrame.timestampNs);
1010 }
1011 return ::ndk::ScopedAStatus::ok();
1012 }
1013
1014 virtual ::ndk::ScopedAStatus onInputBuffersReleased(
1015 const std::vector<InputBuffer>& inputBuffers) override {
1016 std::shared_ptr<Listener> listener = base.lock();
1017 if (!listener) {
1018 LOG(DEBUG) << "onInputBuffersReleased -- listener died.";
1019 return ::ndk::ScopedAStatus::ok();
1020 }
1021 for (const InputBuffer& inputBuffer : inputBuffers) {
1022 LOG(VERBOSE) << "onInputBuffersReleased --"
1023 " received death notification of"
1024 " input buffer:"
1025 " frameIndex = " << inputBuffer.frameIndex
1026 << ", bufferIndex = " << inputBuffer.arrayIndex
1027 << ".";
1028 listener->onInputBufferDone(
1029 inputBuffer.frameIndex, inputBuffer.arrayIndex);
1030 }
1031 return ::ndk::ScopedAStatus::ok();
1032 }
1033
1034};
1035
1036// Codec2Client::Component::HidlBufferPoolSender
1037struct Codec2Client::Component::HidlBufferPoolSender :
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001038 hardware::media::c2::V1_1::utils::DefaultBufferPoolSender {
Wonsik Kime8e98152022-12-16 16:04:17 -08001039 HidlBufferPoolSender()
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001040 : hardware::media::c2::V1_1::utils::DefaultBufferPoolSender() {
1041 }
1042};
1043
Wonsik Kime8e98152022-12-16 16:04:17 -08001044// Codec2Client::Component::AidlBufferPoolSender
1045struct Codec2Client::Component::AidlBufferPoolSender :
1046 c2_aidl::utils::DefaultBufferPoolSender {
1047 AidlBufferPoolSender()
1048 : c2_aidl::utils::DefaultBufferPoolSender() {
1049 }
1050};
1051
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001052// Codec2Client::Component::OutputBufferQueue
1053struct Codec2Client::Component::OutputBufferQueue :
Sungtak Leea714f112021-03-16 05:40:03 -07001054 hardware::media::c2::OutputBufferQueue {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001055 OutputBufferQueue()
Sungtak Leea714f112021-03-16 05:40:03 -07001056 : hardware::media::c2::OutputBufferQueue() {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001057 }
1058};
1059
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00001060// The class holds GraphicBufferAllocator and the associated id of
1061// HAL side BlockPool.
1062// This is tightly coupled with BlockPool creation and destruction.
1063// The life cycle inside class will be as follows.
1064//
1065// On createBlockPool client request.
1066// 1. this::create() creates a GraphicBufferAllocator and set it as
1067// the current.
1068// 2. C2AIDL_HAL::createBlockPool() creates a C2BlockPool using
1069// the GraphicBufferAllocator created in #1.
1070// 3. this::setCurrentId() associates the id returned in #2 to the current
1071//
1072// On destroyBlockPool cliet request
1073// 1. C2AIDL_HAL::destroyBlockPool() destroys the block pool
1074// from HAL process.
1075// 2. this::remove() destroys GraphicBufferAllocator which is associatted
1076// with the C2BlockPool in #1.
1077//
1078struct Codec2Client::Component::GraphicBufferAllocators {
1079private:
1080 std::optional<C2BlockPool::local_id_t> mCurrentId;
1081 std::shared_ptr<AidlGraphicBufferAllocator> mCurrent;
1082
1083 // A new BlockPool is created before the old BlockPool is destroyed.
1084 // This holds the reference of the old BlockPool when a new BlockPool is
1085 // created until the old BlockPool is explicitly requested for destruction.
1086 std::map<C2BlockPool::local_id_t, std::shared_ptr<AidlGraphicBufferAllocator>> mOlds;
1087 std::mutex mMutex;
1088
1089public:
1090 // Creates a GraphicBufferAllocator which will be passed to HAL
1091 // for creating C2BlockPool. And the created GraphicBufferAllocator
1092 // will be used afterwards by current().
1093 std::shared_ptr<AidlGraphicBufferAllocator> create() {
1094 std::unique_lock<std::mutex> l(mMutex);
1095 if (mCurrent) {
1096 // If this is not stopped.
1097 mCurrent->reset();
1098 if (mCurrentId.has_value()) {
1099 mOlds.emplace(mCurrentId.value(), mCurrent);
1100 }
1101 mCurrentId.reset();
1102 mCurrent.reset();
1103 }
1104 // TODO: integrate initial value with CCodec/CCodecBufferChannel
1105 mCurrent =
1106 AidlGraphicBufferAllocator::CreateGraphicBufferAllocator(3 /* maxDequeueCount */);
1107 ALOGD("GraphicBufferAllocator created");
1108 return mCurrent;
1109 }
1110
1111 // Associates the blockpool Id returned from HAL to the
1112 // current GraphicBufferAllocator.
1113 void setCurrentId(C2BlockPool::local_id_t id) {
1114 std::unique_lock<std::mutex> l(mMutex);
1115 CHECK(!mCurrentId.has_value());
1116 mCurrentId = id;
1117 }
1118
1119 // Returns the current GraphicBufferAllocator.
1120 std::shared_ptr<AidlGraphicBufferAllocator> current() {
1121 std::unique_lock<std::mutex> l(mMutex);
1122 return mCurrent;
1123 }
1124
1125 // Removes the GraphicBufferAllocator associated with given \p id.
1126 void remove(C2BlockPool::local_id_t id) {
1127 std::unique_lock<std::mutex> l(mMutex);
1128 mOlds.erase(id);
1129 if (mCurrentId == id) {
1130 if (mCurrent) {
1131 mCurrent->reset();
1132 mCurrent.reset();
1133 }
1134 mCurrentId.reset();
1135 }
1136 }
1137};
1138
Pawin Vongmasa36653902018-11-15 00:10:25 -08001139// Codec2Client
Wonsik Kime8e98152022-12-16 16:04:17 -08001140Codec2Client::Codec2Client(sp<HidlBase> const& base,
Sungtak Leef4b39512023-05-10 07:17:22 +00001141 sp<c2_hidl::IConfigurable> const& configurable,
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001142 size_t serviceIndex)
Sungtak Leef4b39512023-05-10 07:17:22 +00001143 : Configurable{configurable},
Wonsik Kime8e98152022-12-16 16:04:17 -08001144 mHidlBase1_0{base},
1145 mHidlBase1_1{HidlBase1_1::castFrom(base)},
1146 mHidlBase1_2{HidlBase1_2::castFrom(base)},
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001147 mServiceIndex{serviceIndex} {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001148 Return<sp<bufferpool_hidl::IClientManager>> transResult = base->getPoolClientManager();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001149 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001150 LOG(ERROR) << "getPoolClientManager -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001151 } else {
Wonsik Kime8e98152022-12-16 16:04:17 -08001152 mHidlHostPoolManager = static_cast<sp<bufferpool_hidl::IClientManager>>(transResult);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001153 }
1154}
1155
Wonsik Kime8e98152022-12-16 16:04:17 -08001156Codec2Client::Codec2Client(std::shared_ptr<AidlBase> const& base,
1157 std::shared_ptr<c2_aidl::IConfigurable> const& configurable,
1158 size_t serviceIndex)
1159 : Configurable{configurable},
1160 mAidlBase{base},
1161 mServiceIndex{serviceIndex} {
1162 ::ndk::ScopedAStatus transStatus = base->getPoolClientManager(&mAidlHostPoolManager);
1163 if (!transStatus.isOk()) {
1164 LOG(ERROR) << "getPoolClientManager -- transaction failed.";
1165 mAidlHostPoolManager.reset();
1166 }
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001167}
1168
Wonsik Kime8e98152022-12-16 16:04:17 -08001169sp<Codec2Client::HidlBase> const& Codec2Client::getHidlBase() const {
1170 return mHidlBase1_0;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001171}
1172
Wonsik Kime8e98152022-12-16 16:04:17 -08001173sp<Codec2Client::HidlBase1_0> const& Codec2Client::getHidlBase1_0() const {
1174 return mHidlBase1_0;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001175}
1176
Wonsik Kime8e98152022-12-16 16:04:17 -08001177sp<Codec2Client::HidlBase1_1> const& Codec2Client::getHidlBase1_1() const {
1178 return mHidlBase1_1;
1179}
1180
1181sp<Codec2Client::HidlBase1_2> const& Codec2Client::getHidlBase1_2() const {
1182 return mHidlBase1_2;
1183}
1184
1185::ndk::SpAIBinder Codec2Client::getAidlBase() const {
1186 return mAidlBase ? mAidlBase->asBinder() : nullptr;
Sungtak Lee8577dab2021-03-12 02:25:50 -08001187}
1188
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001189std::string const& Codec2Client::getServiceName() const {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001190 return GetServiceNames()[mServiceIndex];
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001191}
1192
Pawin Vongmasa36653902018-11-15 00:10:25 -08001193c2_status_t Codec2Client::createComponent(
1194 const C2String& name,
1195 const std::shared_ptr<Codec2Client::Listener>& listener,
1196 std::shared_ptr<Codec2Client::Component>* const component) {
Wonsik Kime8e98152022-12-16 16:04:17 -08001197 if (mAidlBase) {
1198 std::shared_ptr<Component::AidlListener> aidlListener =
1199 Component::AidlListener::make<Component::AidlListener>();
1200 aidlListener->base = listener;
1201 std::shared_ptr<c2_aidl::IComponent> aidlComponent;
1202 ::ndk::ScopedAStatus transStatus = mAidlBase->createComponent(
1203 name,
1204 aidlListener,
1205 bufferpool2_aidl::implementation::ClientManager::getInstance(),
1206 &aidlComponent);
1207 c2_status_t status = GetC2Status(transStatus, "createComponent");
1208 if (status != C2_OK) {
1209 return status;
1210 } else if (!aidlComponent) {
1211 LOG(ERROR) << "createComponent(" << name.c_str()
1212 << ") -- null component.";
1213 return C2_CORRUPTED;
1214 }
1215 *component = std::make_shared<Codec2Client::Component>(aidlComponent);
1216 status = (*component)->setDeathListener((*component), listener);
1217 if (status != C2_OK) {
1218 LOG(ERROR) << "createComponent(" << name.c_str()
1219 << ") -- failed to set up death listener: "
1220 << status << ".";
1221 }
1222 (*component)->mAidlBufferPoolSender->setReceiver(mAidlHostPoolManager);
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00001223 aidlListener->component = *component;
Wonsik Kime8e98152022-12-16 16:04:17 -08001224 return status;
1225 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001226
Pawin Vongmasa36653902018-11-15 00:10:25 -08001227 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001228 sp<Component::HidlListener> hidlListener = new Component::HidlListener{};
Pawin Vongmasa36653902018-11-15 00:10:25 -08001229 hidlListener->base = listener;
Sungtak Lee8577dab2021-03-12 02:25:50 -08001230 Return<void> transStatus;
Wonsik Kime8e98152022-12-16 16:04:17 -08001231 if (mHidlBase1_2) {
1232 transStatus = mHidlBase1_2->createComponent_1_2(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001233 name,
1234 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001235 bufferpool_hidl::implementation::ClientManager::getInstance(),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001236 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001237 c2_hidl::Status s,
1238 const sp<c2_hidl::IComponent>& c) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001239 status = static_cast<c2_status_t>(s);
1240 if (status != C2_OK) {
1241 return;
1242 }
1243 *component = std::make_shared<Codec2Client::Component>(c);
1244 hidlListener->component = *component;
Sungtak Lee8577dab2021-03-12 02:25:50 -08001245 });
1246 }
Wonsik Kime8e98152022-12-16 16:04:17 -08001247 else if (mHidlBase1_1) {
1248 transStatus = mHidlBase1_1->createComponent_1_1(
Sungtak Lee8577dab2021-03-12 02:25:50 -08001249 name,
1250 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001251 bufferpool_hidl::implementation::ClientManager::getInstance(),
Sungtak Lee8577dab2021-03-12 02:25:50 -08001252 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001253 c2_hidl::Status s,
1254 const sp<c2_hidl_base::V1_1::IComponent>& c) {
Sungtak Lee8577dab2021-03-12 02:25:50 -08001255 status = static_cast<c2_status_t>(s);
1256 if (status != C2_OK) {
1257 return;
1258 }
1259 *component = std::make_shared<Codec2Client::Component>(c);
1260 hidlListener->component = *component;
1261 });
Wonsik Kime8e98152022-12-16 16:04:17 -08001262 } else if (mHidlBase1_0) { // ver1_0
1263 transStatus = mHidlBase1_0->createComponent(
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001264 name,
1265 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001266 bufferpool_hidl::implementation::ClientManager::getInstance(),
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001267 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001268 c2_hidl::Status s,
1269 const sp<c2_hidl_base::V1_0::IComponent>& c) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001270 status = static_cast<c2_status_t>(s);
1271 if (status != C2_OK) {
1272 return;
1273 }
1274 *component = std::make_shared<Codec2Client::Component>(c);
1275 hidlListener->component = *component;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001276 });
Sungtak Lee8577dab2021-03-12 02:25:50 -08001277 } else {
1278 status = C2_CORRUPTED;
1279 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001280 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001281 LOG(ERROR) << "createComponent(" << name.c_str()
1282 << ") -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001283 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001284 } else if (status != C2_OK) {
Pawin Vongmasa9aac3042020-03-05 02:30:32 -08001285 if (status == C2_NOT_FOUND) {
1286 LOG(VERBOSE) << "createComponent(" << name.c_str()
1287 << ") -- component not found.";
1288 } else {
1289 LOG(ERROR) << "createComponent(" << name.c_str()
1290 << ") -- call failed: " << status << ".";
1291 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001292 return status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001293 } else if (!*component) {
1294 LOG(ERROR) << "createComponent(" << name.c_str()
1295 << ") -- null component.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001296 return C2_CORRUPTED;
1297 }
1298
1299 status = (*component)->setDeathListener(*component, listener);
1300 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001301 LOG(ERROR) << "createComponent(" << name.c_str()
1302 << ") -- failed to set up death listener: "
1303 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001304 }
1305
Wonsik Kime8e98152022-12-16 16:04:17 -08001306 (*component)->mHidlBufferPoolSender->setReceiver(mHidlHostPoolManager);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001307 return status;
1308}
1309
1310c2_status_t Codec2Client::createInterface(
1311 const C2String& name,
1312 std::shared_ptr<Codec2Client::Interface>* const interface) {
Wonsik Kime8e98152022-12-16 16:04:17 -08001313 if (mAidlBase) {
1314 std::shared_ptr<c2_aidl::IComponentInterface> aidlInterface;
1315 ::ndk::ScopedAStatus transStatus = mAidlBase->createInterface(
1316 name,
1317 &aidlInterface);
1318 c2_status_t status = GetC2Status(transStatus, "createInterface");
1319 if (status != C2_OK) {
1320 return status;
1321 } else if (!aidlInterface) {
1322 LOG(ERROR) << "createInterface(" << name.c_str()
1323 << ") -- null interface.";
1324 return C2_CORRUPTED;
1325 }
1326 interface->reset(new Codec2Client::Interface(aidlInterface));
1327 return C2_OK;
1328 }
1329
Pawin Vongmasa36653902018-11-15 00:10:25 -08001330 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08001331 Return<void> transStatus = mHidlBase1_0->createInterface(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001332 name,
1333 [&status, interface](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001334 c2_hidl::Status s,
1335 const sp<c2_hidl::IComponentInterface>& i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001336 status = static_cast<c2_status_t>(s);
1337 if (status != C2_OK) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001338 return;
1339 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001340 *interface = std::make_shared<Interface>(i);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001341 });
1342 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001343 LOG(ERROR) << "createInterface(" << name.c_str()
1344 << ") -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001345 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001346 } else if (status != C2_OK) {
Pawin Vongmasa9aac3042020-03-05 02:30:32 -08001347 if (status == C2_NOT_FOUND) {
1348 LOG(VERBOSE) << "createInterface(" << name.c_str()
1349 << ") -- component not found.";
1350 } else {
1351 LOG(ERROR) << "createInterface(" << name.c_str()
1352 << ") -- call failed: " << status << ".";
1353 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001354 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001355 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001356
Pawin Vongmasa36653902018-11-15 00:10:25 -08001357 return status;
1358}
1359
1360c2_status_t Codec2Client::createInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001361 std::shared_ptr<InputSurface>* const inputSurface) {
Wonsik Kime8e98152022-12-16 16:04:17 -08001362 if (mAidlBase) {
1363 // FIXME
1364 return C2_OMITTED;
1365 }
1366
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001367 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08001368 Return<void> transStatus = mHidlBase1_0->createInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001369 [&status, inputSurface](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001370 c2_hidl::Status s,
1371 const sp<c2_hidl::IInputSurface>& i) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001372 status = static_cast<c2_status_t>(s);
1373 if (status != C2_OK) {
1374 return;
1375 }
1376 *inputSurface = std::make_shared<InputSurface>(i);
1377 });
1378 if (!transStatus.isOk()) {
1379 LOG(ERROR) << "createInputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001380 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001381 } else if (status != C2_OK) {
1382 LOG(DEBUG) << "createInputSurface -- call failed: "
1383 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001384 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001385 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001386}
1387
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001388std::vector<C2Component::Traits> const& Codec2Client::listComponents() const {
1389 return Cache::List()[mServiceIndex].getTraits();
1390}
1391
1392std::vector<C2Component::Traits> Codec2Client::_listComponents(
1393 bool* success) const {
1394 std::vector<C2Component::Traits> traits;
1395 std::string const& serviceName = getServiceName();
Wonsik Kime8e98152022-12-16 16:04:17 -08001396
1397 if (mAidlBase) {
1398 std::vector<c2_aidl::IComponentStore::ComponentTraits> aidlTraits;
1399 ::ndk::ScopedAStatus transStatus = mAidlBase->listComponents(&aidlTraits);
1400 if (!transStatus.isOk()) {
1401 LOG(ERROR) << "_listComponents -- transaction failed.";
1402 *success = false;
1403 } else {
1404 traits.resize(aidlTraits.size());
1405 *success = true;
1406 for (size_t i = 0; i < aidlTraits.size(); ++i) {
1407 if (!c2_aidl::utils::FromAidl(&traits[i], aidlTraits[i])) {
1408 LOG(ERROR) << "_listComponents -- corrupted output.";
1409 *success = false;
1410 traits.clear();
1411 break;
1412 }
1413 traits[i].owner = serviceName;
1414 }
1415 }
1416 return traits;
1417 }
1418 Return<void> transStatus = mHidlBase1_0->listComponents(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001419 [&traits, &serviceName](c2_hidl::Status s,
1420 const hidl_vec<c2_hidl::IComponentStore::ComponentTraits>& t) {
1421 if (s != c2_hidl::Status::OK) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001422 LOG(DEBUG) << "_listComponents -- call failed: "
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001423 << static_cast<c2_status_t>(s) << ".";
1424 return;
1425 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001426 traits.resize(t.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001427 for (size_t i = 0; i < t.size(); ++i) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001428 if (!c2_hidl::utils::objcpy(&traits[i], t[i])) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001429 LOG(ERROR) << "_listComponents -- corrupted output.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001430 return;
1431 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001432 traits[i].owner = serviceName;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001433 }
1434 });
1435 if (!transStatus.isOk()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001436 LOG(ERROR) << "_listComponents -- transaction failed.";
1437 *success = false;
1438 } else {
1439 *success = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001440 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001441 return traits;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001442}
1443
1444c2_status_t Codec2Client::copyBuffer(
1445 const std::shared_ptr<C2Buffer>& src,
1446 const std::shared_ptr<C2Buffer>& dst) {
1447 // TODO: Implement?
1448 (void)src;
1449 (void)dst;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001450 LOG(ERROR) << "copyBuffer not implemented";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001451 return C2_OMITTED;
1452}
1453
Wonsik Kime8e98152022-12-16 16:04:17 -08001454std::shared_ptr<C2ParamReflector> Codec2Client::getParamReflector() {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001455 // TODO: this is not meant to be exposed as C2ParamReflector on the client side; instead, it
1456 // should reflect the HAL API.
Wonsik Kime8e98152022-12-16 16:04:17 -08001457 struct HidlSimpleParamReflector : public C2ParamReflector {
1458 std::unique_ptr<C2StructDescriptor> describe(
1459 C2Param::CoreIndex coreIndex) const override {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001460 hidl_vec<c2_hidl::ParamIndex> indices(1);
1461 indices[0] = static_cast<c2_hidl::ParamIndex>(coreIndex.coreIndex());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001462 std::unique_ptr<C2StructDescriptor> descriptor;
1463 Return<void> transStatus = mBase->getStructDescriptors(
1464 indices,
1465 [&descriptor](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001466 c2_hidl::Status s,
1467 const hidl_vec<c2_hidl::StructDescriptor>& sd) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001468 c2_status_t status = static_cast<c2_status_t>(s);
1469 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001470 LOG(DEBUG) << "SimpleParamReflector -- "
1471 "getStructDescriptors() failed: "
1472 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001473 descriptor.reset();
1474 return;
1475 }
1476 if (sd.size() != 1) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001477 LOG(DEBUG) << "SimpleParamReflector -- "
1478 "getStructDescriptors() "
1479 "returned vector of size "
1480 << sd.size() << ". "
1481 "It should be 1.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001482 descriptor.reset();
1483 return;
1484 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001485 if (!c2_hidl::utils::objcpy(&descriptor, sd[0])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001486 LOG(DEBUG) << "SimpleParamReflector -- "
1487 "getStructDescriptors() returned "
1488 "corrupted data.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001489 descriptor.reset();
1490 return;
1491 }
1492 });
Wonsik Kim492fecd2020-11-19 11:14:11 -08001493 if (!transStatus.isOk()) {
1494 LOG(DEBUG) << "SimpleParamReflector -- transaction failed: "
1495 << transStatus.description();
1496 descriptor.reset();
1497 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001498 return descriptor;
1499 }
1500
Wonsik Kime8e98152022-12-16 16:04:17 -08001501 HidlSimpleParamReflector(sp<HidlBase> base)
Pawin Vongmasa36653902018-11-15 00:10:25 -08001502 : mBase(base) { }
1503
Wonsik Kime8e98152022-12-16 16:04:17 -08001504 sp<HidlBase> mBase;
1505 };
1506 struct AidlSimpleParamReflector : public C2ParamReflector {
1507 std::unique_ptr<C2StructDescriptor> describe(
1508 C2Param::CoreIndex coreIndex) const override {
1509 std::vector<c2_aidl::StructDescriptor> aidlDesc;
1510 std::unique_ptr<C2StructDescriptor> descriptor;
1511 ::ndk::ScopedAStatus transStatus = mBase->getStructDescriptors(
1512 {int32_t(coreIndex.coreIndex())},
1513 &aidlDesc);
1514 c2_status_t status = GetC2Status(transStatus, "describe");
1515 if (status != C2_OK) {
1516 descriptor.reset();
1517 } else if (!c2_aidl::utils::FromAidl(&descriptor, aidlDesc[0])) {
1518 LOG(ERROR) << "describe -- conversion failed.";
1519 descriptor.reset();
1520 }
1521 return descriptor;
1522 }
1523
1524 AidlSimpleParamReflector(const std::shared_ptr<AidlBase> &base)
1525 : mBase(base) { }
1526
1527 std::shared_ptr<AidlBase> mBase;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001528 };
1529
Wonsik Kime8e98152022-12-16 16:04:17 -08001530 if (mAidlBase) {
1531 return std::make_shared<AidlSimpleParamReflector>(mAidlBase);
1532 }
1533 return std::make_shared<HidlSimpleParamReflector>(mHidlBase1_0);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001534};
1535
Wonsik Kime8e98152022-12-16 16:04:17 -08001536std::vector<std::string> Codec2Client::CacheServiceNames() {
1537 std::vector<std::string> names;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001538
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001539 if (c2_aidl::utils::IsSelected()) {
Wonsik Kim138db0d2023-11-02 16:02:01 -07001540 if (__builtin_available(android __ANDROID_API_S__, *)) {
1541 // Get AIDL service names
1542 AServiceManager_forEachDeclaredInstance(
1543 AidlBase::descriptor, &names, [](const char *name, void *context) {
1544 std::vector<std::string> *names = (std::vector<std::string> *)context;
1545 names->emplace_back(name);
1546 });
1547 } else {
1548 LOG(FATAL) << "C2 AIDL cannot be selected on Android version older than 35";
1549 }
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001550 } else {
1551 // Get HIDL service names
1552 using ::android::hardware::media::c2::V1_0::IComponentStore;
1553 using ::android::hidl::manager::V1_2::IServiceManager;
1554 while (true) {
1555 sp<IServiceManager> serviceManager = IServiceManager::getService();
1556 CHECK(serviceManager) << "Hardware service manager is not running.";
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001557
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001558 Return<void> transResult;
1559 transResult = serviceManager->listManifestByInterface(
1560 IComponentStore::descriptor,
1561 [&names](
1562 hidl_vec<hidl_string> const& instanceNames) {
1563 names.insert(names.end(), instanceNames.begin(), instanceNames.end());
1564 });
1565 if (transResult.isOk()) {
1566 break;
1567 }
1568 LOG(ERROR) << "Could not retrieve the list of service instances of "
1569 << IComponentStore::descriptor
1570 << ". Retrying...";
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001571 }
Wonsik Kime8e98152022-12-16 16:04:17 -08001572 }
1573 // Sort service names in each category.
1574 std::stable_sort(
1575 names.begin(), names.end(),
1576 [](const std::string &a, const std::string &b) {
1577 // First compare by prefix: default -> vendor -> {everything else}
1578 constexpr int DEFAULT = 1;
1579 constexpr int VENDOR = 2;
1580 constexpr int OTHER = 3;
1581 int aPrefix = ((a.compare(0, 7, "default") == 0) ? DEFAULT :
1582 (a.compare(0, 6, "vendor") == 0) ? VENDOR :
1583 OTHER);
1584 int bPrefix = ((b.compare(0, 7, "default") == 0) ? DEFAULT :
1585 (b.compare(0, 6, "vendor") == 0) ? VENDOR :
1586 OTHER);
1587 if (aPrefix != bPrefix) {
1588 return aPrefix < bPrefix;
1589 }
1590 // If the prefix is the same, compare alphabetically
1591 return a < b;
1592 });
1593
1594 // Summarize to logcat.
1595 if (names.empty()) {
1596 LOG(INFO) << "No Codec2 services declared in the manifest.";
1597 } else {
1598 std::stringstream stringOutput;
1599 stringOutput << "Available Codec2 services:";
1600 for (std::string const& name : names) {
1601 stringOutput << " \"" << name << "\"";
1602 }
1603 LOG(INFO) << stringOutput.str();
1604 }
1605
1606 return names;
1607}
1608
1609std::vector<std::string> const& Codec2Client::GetServiceNames() {
1610 static std::vector<std::string> sServiceNames = CacheServiceNames();
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001611 return sServiceNames;
1612}
1613
Pawin Vongmasa36653902018-11-15 00:10:25 -08001614std::shared_ptr<Codec2Client> Codec2Client::CreateFromService(
Pawin Vongmasa83d2c552020-03-05 04:36:08 -08001615 const char* name,
1616 bool setAsPreferredCodec2ComponentStore) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001617 size_t index = getServiceIndex(name);
Pawin Vongmasa83d2c552020-03-05 04:36:08 -08001618 if (index == GetServiceNames().size()) {
1619 if (setAsPreferredCodec2ComponentStore) {
1620 LOG(WARNING) << "CreateFromService(" << name
1621 << ") -- preferred C2ComponentStore not set.";
1622 }
1623 return nullptr;
1624 }
1625 std::shared_ptr<Codec2Client> client = _CreateFromIndex(index);
1626 if (setAsPreferredCodec2ComponentStore) {
1627 SetPreferredCodec2ComponentStore(
1628 std::make_shared<Client2Store>(client));
1629 LOG(INFO) << "CreateFromService(" << name
1630 << ") -- service set as preferred C2ComponentStore.";
1631 }
1632 return client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001633}
1634
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001635std::vector<std::shared_ptr<Codec2Client>> Codec2Client::
1636 CreateFromAllServices() {
1637 std::vector<std::shared_ptr<Codec2Client>> clients(
1638 GetServiceNames().size());
1639 for (size_t i = GetServiceNames().size(); i > 0; ) {
1640 --i;
1641 clients[i] = _CreateFromIndex(i);
1642 }
1643 return clients;
1644}
1645
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001646std::shared_ptr<Codec2Client> Codec2Client::_CreateFromIndex(size_t index) {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001647 std::string const& name = GetServiceNames()[index];
Pawin Vongmasa9aac3042020-03-05 02:30:32 -08001648 LOG(VERBOSE) << "Creating a Codec2 client to service \"" << name << "\"";
Wonsik Kime8e98152022-12-16 16:04:17 -08001649
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001650 if (c2_aidl::utils::IsSelected()) {
Wonsik Kim138db0d2023-11-02 16:02:01 -07001651 if (__builtin_available(android __ANDROID_API_S__, *)) {
1652 std::string instanceName =
1653 ::android::base::StringPrintf("%s/%s", AidlBase::descriptor, name.c_str());
1654 if (AServiceManager_isDeclared(instanceName.c_str())) {
1655 std::shared_ptr<AidlBase> baseStore = AidlBase::fromBinder(
1656 ::ndk::SpAIBinder(AServiceManager_waitForService(instanceName.c_str())));
1657 CHECK(baseStore) << "Codec2 AIDL service \"" << name << "\""
1658 " inaccessible for unknown reasons.";
1659 LOG(VERBOSE) << "Client to Codec2 AIDL service \"" << name << "\" created";
1660 std::shared_ptr<c2_aidl::IConfigurable> configurable;
1661 ::ndk::ScopedAStatus transStatus = baseStore->getConfigurable(&configurable);
1662 CHECK(transStatus.isOk()) << "Codec2 AIDL service \"" << name << "\""
1663 "does not have IConfigurable.";
1664 return std::make_shared<Codec2Client>(baseStore, configurable, index);
1665 } else {
1666 LOG(ERROR) << "Codec2 AIDL service \"" << name << "\" is not declared";
1667 }
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001668 } else {
Wonsik Kim138db0d2023-11-02 16:02:01 -07001669 LOG(FATAL) << "C2 AIDL cannot be selected on Android version older than 35";
Wonsik Kime8e98152022-12-16 16:04:17 -08001670 }
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001671 } else {
1672 std::string instanceName = "android.hardware.media.c2/" + name;
1673 sp<HidlBase> baseStore = HidlBase::getService(name);
1674 CHECK(baseStore) << "Codec2 service \"" << name << "\""
1675 " inaccessible for unknown reasons.";
1676 LOG(VERBOSE) << "Client to Codec2 service \"" << name << "\" created";
1677 Return<sp<c2_hidl::IConfigurable>> transResult = baseStore->getConfigurable();
1678 CHECK(transResult.isOk()) << "Codec2 service \"" << name << "\""
1679 "does not have IConfigurable.";
1680 sp<c2_hidl::IConfigurable> configurable =
1681 static_cast<sp<c2_hidl::IConfigurable>>(transResult);
1682 return std::make_shared<Codec2Client>(baseStore, configurable, index);
Wonsik Kime8e98152022-12-16 16:04:17 -08001683 }
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001684 return nullptr;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001685}
1686
1687c2_status_t Codec2Client::ForAllServices(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001688 const std::string &key,
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001689 size_t numberOfAttempts,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001690 std::function<c2_status_t(const std::shared_ptr<Codec2Client>&)>
1691 predicate) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001692 c2_status_t status = C2_NO_INIT; // no IComponentStores present
1693
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001694 // Cache the mapping key -> index of Codec2Client in Cache::List().
Pawin Vongmasa36653902018-11-15 00:10:25 -08001695 static std::mutex key2IndexMutex;
1696 static std::map<std::string, size_t> key2Index;
1697
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001698 // By default try all stores. However, try the last known client first. If
1699 // the last known client fails, retry once. We do this by pushing the last
1700 // known client in front of the list of all clients.
Pawin Vongmasa36653902018-11-15 00:10:25 -08001701 std::deque<size_t> indices;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001702 for (size_t index = Cache::List().size(); index > 0; ) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001703 indices.push_front(--index);
1704 }
1705
1706 bool wasMapped = false;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001707 {
1708 std::scoped_lock lock{key2IndexMutex};
1709 auto it = key2Index.find(key);
1710 if (it != key2Index.end()) {
1711 indices.push_front(it->second);
1712 wasMapped = true;
1713 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001714 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001715
1716 for (size_t index : indices) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001717 Cache& cache = Cache::List()[index];
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001718 for (size_t tries = numberOfAttempts; tries > 0; --tries) {
1719 std::shared_ptr<Codec2Client> client{cache.getClient()};
Pawin Vongmasa36653902018-11-15 00:10:25 -08001720 status = predicate(client);
1721 if (status == C2_OK) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001722 std::scoped_lock lock{key2IndexMutex};
Pawin Vongmasa36653902018-11-15 00:10:25 -08001723 key2Index[key] = index; // update last known client index
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001724 return C2_OK;
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001725 } else if (status == C2_NO_MEMORY) {
1726 return C2_NO_MEMORY;
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001727 } else if (status == C2_TRANSACTION_FAILED) {
1728 LOG(WARNING) << "\"" << key << "\" failed for service \""
1729 << client->getName()
1730 << "\" due to transaction failure. "
1731 << "(Service may have crashed.)"
1732 << (tries > 1 ? " Retrying..." : "");
1733 cache.invalidate();
1734 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001735 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001736 if (wasMapped) {
1737 LOG(INFO) << "\"" << key << "\" became invalid in service \""
1738 << client->getName() << "\". Retrying...";
1739 wasMapped = false;
1740 }
1741 break;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001742 }
1743 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001744 return status; // return the last status from a valid client
Pawin Vongmasa36653902018-11-15 00:10:25 -08001745}
1746
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001747c2_status_t Codec2Client::CreateComponentByName(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001748 const char* componentName,
1749 const std::shared_ptr<Listener>& listener,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001750 std::shared_ptr<Component>* component,
Pawin Vongmasa23c90c82019-09-03 00:44:42 -07001751 std::shared_ptr<Codec2Client>* owner,
1752 size_t numberOfAttempts) {
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001753 std::string key{"create:"};
1754 key.append(componentName);
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001755 c2_status_t status = ForAllServices(
1756 key,
1757 numberOfAttempts,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001758 [owner, component, componentName, &listener](
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001759 const std::shared_ptr<Codec2Client> &client)
1760 -> c2_status_t {
1761 c2_status_t status = client->createComponent(componentName,
1762 listener,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001763 component);
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001764 if (status == C2_OK) {
1765 if (owner) {
1766 *owner = client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001767 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001768 } else if (status != C2_NOT_FOUND) {
1769 LOG(DEBUG) << "IComponentStore("
1770 << client->getServiceName()
1771 << ")::createComponent(\"" << componentName
1772 << "\") returned status = "
1773 << status << ".";
1774 }
1775 return status;
1776 });
1777 if (status != C2_OK) {
1778 LOG(DEBUG) << "Failed to create component \"" << componentName
1779 << "\" from all known services. "
1780 "Last returned status = " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001781 }
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001782 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001783}
1784
Wonsik Kime8e98152022-12-16 16:04:17 -08001785std::shared_ptr<Codec2Client::Interface> Codec2Client::CreateInterfaceByName(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001786 const char* interfaceName,
Pawin Vongmasa23c90c82019-09-03 00:44:42 -07001787 std::shared_ptr<Codec2Client>* owner,
1788 size_t numberOfAttempts) {
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001789 std::string key{"create:"};
1790 key.append(interfaceName);
1791 std::shared_ptr<Interface> interface;
1792 c2_status_t status = ForAllServices(
1793 key,
1794 numberOfAttempts,
1795 [owner, &interface, interfaceName](
1796 const std::shared_ptr<Codec2Client> &client)
1797 -> c2_status_t {
1798 c2_status_t status = client->createInterface(interfaceName,
1799 &interface);
1800 if (status == C2_OK) {
1801 if (owner) {
1802 *owner = client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001803 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001804 } else if (status != C2_NOT_FOUND) {
1805 LOG(DEBUG) << "IComponentStore("
1806 << client->getServiceName()
1807 << ")::createInterface(\"" << interfaceName
1808 << "\") returned status = "
1809 << status << ".";
1810 }
1811 return status;
1812 });
1813 if (status != C2_OK) {
1814 LOG(DEBUG) << "Failed to create interface \"" << interfaceName
1815 << "\" from all known services. "
1816 "Last returned status = " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001817 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001818 return interface;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001819}
1820
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001821std::vector<C2Component::Traits> const& Codec2Client::ListComponents() {
1822 static std::vector<C2Component::Traits> sList{[]() {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001823 std::vector<C2Component::Traits> list;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001824 for (Cache& cache : Cache::List()) {
1825 std::vector<C2Component::Traits> const& traits = cache.getTraits();
1826 list.insert(list.end(), traits.begin(), traits.end());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001827 }
1828 return list;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001829 }()};
1830 return sList;
1831}
Pawin Vongmasa36653902018-11-15 00:10:25 -08001832
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001833std::shared_ptr<Codec2Client::InputSurface> Codec2Client::CreateInputSurface(
1834 char const* serviceName) {
Pawin Vongmasa18588322019-05-18 01:52:13 -07001835 int32_t inputSurfaceSetting = ::android::base::GetIntProperty(
1836 "debug.stagefright.c2inputsurface", int32_t(0));
1837 if (inputSurfaceSetting <= 0) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001838 return nullptr;
1839 }
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001840 size_t index = GetServiceNames().size();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001841 if (serviceName) {
1842 index = getServiceIndex(serviceName);
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001843 if (index == GetServiceNames().size()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001844 LOG(DEBUG) << "CreateInputSurface -- invalid service name: \""
1845 << serviceName << "\"";
1846 }
1847 }
1848
1849 std::shared_ptr<Codec2Client::InputSurface> inputSurface;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001850 if (index != GetServiceNames().size()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001851 std::shared_ptr<Codec2Client> client = Cache::List()[index].getClient();
1852 if (client->createInputSurface(&inputSurface) == C2_OK) {
1853 return inputSurface;
1854 }
1855 }
1856 LOG(INFO) << "CreateInputSurface -- attempting to create an input surface "
1857 "from all services...";
1858 for (Cache& cache : Cache::List()) {
1859 std::shared_ptr<Codec2Client> client = cache.getClient();
1860 if (client->createInputSurface(&inputSurface) == C2_OK) {
1861 LOG(INFO) << "CreateInputSurface -- input surface obtained from "
1862 "service \"" << client->getServiceName() << "\"";
1863 return inputSurface;
1864 }
1865 }
1866 LOG(WARNING) << "CreateInputSurface -- failed to create an input surface "
1867 "from all services";
1868 return nullptr;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001869}
1870
Sungtak Lee92bca442024-03-12 09:10:31 +00001871bool Codec2Client::IsAidlSelected() {
1872 return c2_aidl::utils::IsSelected();
1873}
1874
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001875// Codec2Client::Interface
Wonsik Kime8e98152022-12-16 16:04:17 -08001876Codec2Client::Interface::Interface(const sp<HidlBase>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001877 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001878 [base]() -> sp<c2_hidl::IConfigurable> {
1879 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001880 base->getConfigurable();
1881 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001882 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001883 nullptr;
1884 }()
1885 },
Wonsik Kime8e98152022-12-16 16:04:17 -08001886 mHidlBase{base} {
1887}
1888
1889Codec2Client::Interface::Interface(const std::shared_ptr<AidlBase>& base)
1890 : Configurable{
1891 [base]() -> std::shared_ptr<c2_aidl::IConfigurable> {
1892 std::shared_ptr<c2_aidl::IConfigurable> aidlConfigurable;
1893 ::ndk::ScopedAStatus transStatus =
1894 base->getConfigurable(&aidlConfigurable);
1895 return transStatus.isOk() ? aidlConfigurable : nullptr;
1896 }()
1897 },
1898 mAidlBase{base} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001899}
1900
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001901// Codec2Client::Component
Wonsik Kime8e98152022-12-16 16:04:17 -08001902
1903class Codec2Client::Component::AidlDeathManager {
1904public:
1905 AidlDeathManager()
1906 : mSeq(0),
1907 mDeathRecipient(AIBinder_DeathRecipient_new(OnBinderDied)) {
1908 }
1909
1910 ~AidlDeathManager() = default;
1911
1912 bool linkToDeath(
1913 const std::shared_ptr<Component> &comp,
1914 const std::shared_ptr<Listener> &listener,
1915 size_t *seqPtr) {
1916 std::unique_lock lock(mMutex);
1917 size_t seq = mSeq++;
1918 if (!mMap.try_emplace(seq, comp, listener).second) {
1919 return false;
1920 }
1921 if (STATUS_OK != AIBinder_linkToDeath(
1922 comp->mAidlBase->asBinder().get(), mDeathRecipient.get(), (void *)seq)) {
1923 mMap.erase(seq);
1924 return false;
1925 }
1926 *seqPtr = seq;
1927 return true;
1928 }
1929
1930 void unlinkToDeath(size_t seq, const std::shared_ptr<AidlBase> &base) {
1931 std::unique_lock lock(mMutex);
1932 AIBinder_unlinkToDeath(base->asBinder().get(), mDeathRecipient.get(), (void *)seq);
1933 mMap.erase(seq);
1934 }
1935
1936private:
1937 std::mutex mMutex;
1938 size_t mSeq;
1939 typedef std::tuple<std::weak_ptr<Component>, std::weak_ptr<Listener>> Context;
1940 std::map<size_t, Context> mMap;
1941 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
1942
1943 bool extractContext(size_t seq, Context *context) {
1944 std::unique_lock lock(mMutex);
1945 auto node = mMap.extract(seq);
1946 if (!node) {
1947 return false;
1948 }
1949 *context = node.mapped();
1950 return true;
1951 }
1952
1953 static void OnBinderDied(void *cookie) {
1954 size_t seq = size_t(cookie);
1955 Context context;
1956 if (!Component::GetAidlDeathManager()->extractContext(seq, &context)) {
1957 return;
1958 }
1959 std::weak_ptr<Component> weakComponent;
1960 std::weak_ptr<Listener> weakListener;
1961 std::tie(weakComponent, weakListener) = context;
1962 if (std::shared_ptr<Listener> listener = weakListener.lock()) {
1963 listener->onDeath(weakComponent);
1964 } else {
1965 LOG(DEBUG) << "onDeath -- listener died.";
1966 }
1967 }
1968};
1969
1970Codec2Client::Component::Component(const sp<HidlBase>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001971 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001972 [base]() -> sp<c2_hidl::IConfigurable> {
1973 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001974 base->getInterface();
1975 if (!transResult1.isOk()) {
1976 return nullptr;
1977 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001978 Return<sp<c2_hidl::IConfigurable>> transResult2 =
1979 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001980 getConfigurable();
1981 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001982 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001983 nullptr;
1984 }()
1985 },
Wonsik Kime8e98152022-12-16 16:04:17 -08001986 mHidlBase1_0{base},
1987 mHidlBase1_1{HidlBase1_1::castFrom(base)},
1988 mHidlBase1_2{HidlBase1_2::castFrom(base)},
1989 mHidlBufferPoolSender{std::make_unique<HidlBufferPoolSender>()},
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001990 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
1991}
1992
Wonsik Kime8e98152022-12-16 16:04:17 -08001993Codec2Client::Component::Component(const sp<HidlBase1_1>& base)
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001994 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001995 [base]() -> sp<c2_hidl::IConfigurable> {
1996 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001997 base->getInterface();
1998 if (!transResult1.isOk()) {
1999 return nullptr;
2000 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002001 Return<sp<c2_hidl::IConfigurable>> transResult2 =
2002 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002003 getConfigurable();
2004 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002005 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002006 nullptr;
2007 }()
2008 },
Wonsik Kime8e98152022-12-16 16:04:17 -08002009 mHidlBase1_0{base},
2010 mHidlBase1_1{base},
2011 mHidlBase1_2{HidlBase1_2::castFrom(base)},
2012 mHidlBufferPoolSender{std::make_unique<HidlBufferPoolSender>()},
Sungtak Lee8577dab2021-03-12 02:25:50 -08002013 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
2014}
2015
Wonsik Kime8e98152022-12-16 16:04:17 -08002016Codec2Client::Component::Component(const sp<HidlBase1_2>& base)
Sungtak Lee8577dab2021-03-12 02:25:50 -08002017 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002018 [base]() -> sp<c2_hidl::IConfigurable> {
2019 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Sungtak Lee8577dab2021-03-12 02:25:50 -08002020 base->getInterface();
2021 if (!transResult1.isOk()) {
2022 return nullptr;
2023 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002024 Return<sp<c2_hidl::IConfigurable>> transResult2 =
2025 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Sungtak Lee8577dab2021-03-12 02:25:50 -08002026 getConfigurable();
2027 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002028 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Sungtak Lee8577dab2021-03-12 02:25:50 -08002029 nullptr;
2030 }()
2031 },
Wonsik Kime8e98152022-12-16 16:04:17 -08002032 mHidlBase1_0{base},
2033 mHidlBase1_1{base},
2034 mHidlBase1_2{base},
2035 mHidlBufferPoolSender{std::make_unique<HidlBufferPoolSender>()},
2036 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
2037}
2038
2039Codec2Client::Component::Component(const std::shared_ptr<AidlBase> &base)
2040 : Configurable{
2041 [base]() -> std::shared_ptr<c2_aidl::IConfigurable> {
2042 std::shared_ptr<c2_aidl::IComponentInterface> aidlIntf;
2043 ::ndk::ScopedAStatus transStatus = base->getInterface(&aidlIntf);
2044 if (!transStatus.isOk()) {
2045 return nullptr;
2046 }
2047 std::shared_ptr<c2_aidl::IConfigurable> aidlConfigurable;
2048 transStatus = aidlIntf->getConfigurable(&aidlConfigurable);
2049 return transStatus.isOk() ? aidlConfigurable : nullptr;
2050 }()
2051 },
2052 mAidlBase{base},
2053 mAidlBufferPoolSender{std::make_unique<AidlBufferPoolSender>()},
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002054 mGraphicBufferAllocators{std::make_unique<GraphicBufferAllocators>()} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002055}
2056
2057Codec2Client::Component::~Component() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002058 if (mAidlDeathSeq) {
2059 GetAidlDeathManager()->unlinkToDeath(*mAidlDeathSeq, mAidlBase);
2060 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002061}
2062
2063c2_status_t Codec2Client::Component::createBlockPool(
2064 C2Allocator::id_t id,
2065 C2BlockPool::local_id_t* blockPoolId,
2066 std::shared_ptr<Codec2Client::Configurable>* configurable) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002067 if (mAidlBase) {
2068 c2_aidl::IComponent::BlockPool aidlBlockPool;
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002069 c2_status_t status = C2_OK;
2070
2071 // TODO: Temporary mapping for the current CCodecBufferChannel.
2072 // Handle this properly and remove this temporary allocator mapping.
2073 id = id == C2PlatformAllocatorStore::BUFFERQUEUE ?
2074 C2PlatformAllocatorStore::IGBA : id;
2075
Sungtak Leefffb5692024-01-18 11:22:38 +00002076 c2_aidl::IComponent::BlockPoolAllocator allocator;
2077 allocator.allocatorId = id;
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002078 if (id == C2PlatformAllocatorStore::IGBA) {
2079 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2080 mGraphicBufferAllocators->create();
2081 ::ndk::ScopedFileDescriptor waitableFd;
2082 ::ndk::ScopedAStatus ret = gba->getWaitableFd(&waitableFd);
2083 status = GetC2Status(ret, "Gba::getWaitableFd");
2084 if (status != C2_OK) {
2085 return status;
2086 }
Sungtak Leefffb5692024-01-18 11:22:38 +00002087 c2_aidl::IComponent::GbAllocator gbAllocator;
2088 gbAllocator.waitableFd = std::move(waitableFd);
2089 gbAllocator.igba =
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002090 c2_aidl::IGraphicBufferAllocator::fromBinder(gba->asBinder());
Sungtak Leefffb5692024-01-18 11:22:38 +00002091 allocator.gbAllocator = std::move(gbAllocator);
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002092 ::ndk::ScopedAStatus transStatus = mAidlBase->createBlockPool(
2093 allocator, &aidlBlockPool);
2094 status = GetC2Status(transStatus, "createBlockPool");
2095 if (status != C2_OK) {
2096 return status;
2097 }
2098 mGraphicBufferAllocators->setCurrentId(aidlBlockPool.blockPoolId);
2099 } else {
2100 ::ndk::ScopedAStatus transStatus = mAidlBase->createBlockPool(
Sungtak Leefffb5692024-01-18 11:22:38 +00002101 allocator, &aidlBlockPool);
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002102 status = GetC2Status(transStatus, "createBlockPool");
2103 if (status != C2_OK) {
2104 return status;
2105 }
Wonsik Kime8e98152022-12-16 16:04:17 -08002106 }
2107 *blockPoolId = aidlBlockPool.blockPoolId;
2108 *configurable = std::make_shared<Configurable>(aidlBlockPool.configurable);
2109 return C2_OK;
2110 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002111 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08002112 Return<void> transStatus = mHidlBase1_0->createBlockPool(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002113 static_cast<uint32_t>(id),
2114 [&status, blockPoolId, configurable](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002115 c2_hidl::Status s,
Pawin Vongmasa36653902018-11-15 00:10:25 -08002116 uint64_t pId,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002117 const sp<c2_hidl::IConfigurable>& c) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002118 status = static_cast<c2_status_t>(s);
2119 configurable->reset();
2120 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002121 LOG(DEBUG) << "createBlockPool -- call failed: "
2122 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002123 return;
2124 }
2125 *blockPoolId = static_cast<C2BlockPool::local_id_t>(pId);
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002126 *configurable = std::make_shared<Configurable>(c);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002127 });
2128 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002129 LOG(ERROR) << "createBlockPool -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002130 return C2_TRANSACTION_FAILED;
2131 }
2132 return status;
2133}
2134
2135c2_status_t Codec2Client::Component::destroyBlockPool(
2136 C2BlockPool::local_id_t localId) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002137 if (mAidlBase) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002138 mGraphicBufferAllocators->remove(localId);
Wonsik Kime8e98152022-12-16 16:04:17 -08002139 ::ndk::ScopedAStatus transStatus = mAidlBase->destroyBlockPool(localId);
2140 return GetC2Status(transStatus, "destroyBlockPool");
2141 }
2142 Return<c2_hidl::Status> transResult = mHidlBase1_0->destroyBlockPool(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002143 static_cast<uint64_t>(localId));
2144 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002145 LOG(ERROR) << "destroyBlockPool -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002146 return C2_TRANSACTION_FAILED;
2147 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002148 return static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transResult));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002149}
2150
Wonsik Kimab34ed62019-01-31 15:28:46 -08002151void Codec2Client::Component::handleOnWorkDone(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002152 const std::list<std::unique_ptr<C2Work>> &workItems) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002153 if (mAidlBase) {
2154 holdIgbaBlocks(workItems);
2155 } else {
2156 // Output bufferqueue-based blocks' lifetime management
2157 mOutputBufferQueue->holdBufferQueueBlocks(workItems);
2158 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002159}
2160
2161c2_status_t Codec2Client::Component::queue(
2162 std::list<std::unique_ptr<C2Work>>* const items) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002163 if (mAidlBase) {
2164 c2_aidl::WorkBundle workBundle;
2165 if (!c2_aidl::utils::ToAidl(&workBundle, *items, mAidlBufferPoolSender.get())) {
2166 LOG(ERROR) << "queue -- bad input.";
2167 return C2_TRANSACTION_FAILED;
2168 }
2169 ::ndk::ScopedAStatus transStatus = mAidlBase->queue(workBundle);
2170 return GetC2Status(transStatus, "queue");
2171 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002172 c2_hidl::WorkBundle workBundle;
Wonsik Kime8e98152022-12-16 16:04:17 -08002173 if (!c2_hidl::utils::objcpy(&workBundle, *items, mHidlBufferPoolSender.get())) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002174 LOG(ERROR) << "queue -- bad input.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002175 return C2_TRANSACTION_FAILED;
2176 }
Wonsik Kime8e98152022-12-16 16:04:17 -08002177 Return<c2_hidl::Status> transStatus = mHidlBase1_0->queue(workBundle);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002178 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002179 LOG(ERROR) << "queue -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002180 return C2_TRANSACTION_FAILED;
2181 }
2182 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002183 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002184 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002185 LOG(DEBUG) << "queue -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002186 }
2187 return status;
2188}
2189
2190c2_status_t Codec2Client::Component::flush(
2191 C2Component::flush_mode_t mode,
2192 std::list<std::unique_ptr<C2Work>>* const flushedWork) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002193 (void)mode; // Flush mode isn't supported in HIDL/AIDL yet.
2194 c2_status_t status = C2_OK;
2195 if (mAidlBase) {
2196 c2_aidl::WorkBundle workBundle;
2197 ::ndk::ScopedAStatus transStatus = mAidlBase->flush(&workBundle);
2198 c2_status_t status = GetC2Status(transStatus, "flush");
2199 if (status != C2_OK) {
2200 return status;
2201 }
2202 if (!c2_aidl::utils::FromAidl(flushedWork, workBundle)) {
2203 LOG(DEBUG) << "flush -- flushedWork corrupted.";
2204 return C2_CORRUPTED;
2205 }
2206 } else {
2207 Return<void> transStatus = mHidlBase1_0->flush(
2208 [&status, flushedWork](
2209 c2_hidl::Status s, const c2_hidl::WorkBundle& wb) {
2210 status = static_cast<c2_status_t>(s);
2211 if (status != C2_OK) {
2212 LOG(DEBUG) << "flush -- call failed: " << status << ".";
2213 return;
2214 }
2215 if (!c2_hidl::utils::objcpy(flushedWork, wb)) {
2216 status = C2_CORRUPTED;
2217 } else {
2218 status = C2_OK;
2219 }
2220 });
2221 if (!transStatus.isOk()) {
2222 LOG(ERROR) << "flush -- transaction failed.";
2223 return C2_TRANSACTION_FAILED;
2224 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002225 }
2226
2227 // Indices of flushed work items.
2228 std::vector<uint64_t> flushedIndices;
2229 for (const std::unique_ptr<C2Work> &work : *flushedWork) {
2230 if (work) {
2231 if (work->worklets.empty()
2232 || !work->worklets.back()
2233 || (work->worklets.back()->output.flags &
2234 C2FrameData::FLAG_INCOMPLETE) == 0) {
2235 // input is complete
2236 flushedIndices.emplace_back(
2237 work->input.ordinal.frameIndex.peeku());
2238 }
2239 }
2240 }
2241
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002242 if (mAidlBase) {
2243 holdIgbaBlocks(*flushedWork);
2244 } else {
2245 // Output bufferqueue-based blocks' lifetime management
2246 mOutputBufferQueue->holdBufferQueueBlocks(*flushedWork);
2247 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002248
2249 return status;
2250}
2251
2252c2_status_t Codec2Client::Component::drain(C2Component::drain_mode_t mode) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002253 if (mAidlBase) {
2254 ::ndk::ScopedAStatus transStatus = mAidlBase->drain(
2255 mode == C2Component::DRAIN_COMPONENT_WITH_EOS);
2256 return GetC2Status(transStatus, "drain");
2257 }
2258 Return<c2_hidl::Status> transStatus = mHidlBase1_0->drain(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002259 mode == C2Component::DRAIN_COMPONENT_WITH_EOS);
2260 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002261 LOG(ERROR) << "drain -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002262 return C2_TRANSACTION_FAILED;
2263 }
2264 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002265 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002266 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002267 LOG(DEBUG) << "drain -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002268 }
2269 return status;
2270}
2271
2272c2_status_t Codec2Client::Component::start() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002273 if (mAidlBase) {
2274 ::ndk::ScopedAStatus transStatus = mAidlBase->start();
2275 return GetC2Status(transStatus, "start");
2276 }
2277 Return<c2_hidl::Status> transStatus = mHidlBase1_0->start();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002278 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002279 LOG(ERROR) << "start -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002280 return C2_TRANSACTION_FAILED;
2281 }
2282 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002283 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002284 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002285 LOG(DEBUG) << "start -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002286 }
2287 return status;
2288}
2289
2290c2_status_t Codec2Client::Component::stop() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002291 if (mAidlBase) {
2292 ::ndk::ScopedAStatus transStatus = mAidlBase->stop();
2293 return GetC2Status(transStatus, "stop");
2294 }
2295 Return<c2_hidl::Status> transStatus = mHidlBase1_0->stop();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002296 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002297 LOG(ERROR) << "stop -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002298 return C2_TRANSACTION_FAILED;
2299 }
2300 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002301 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002302 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002303 LOG(DEBUG) << "stop -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002304 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002305 return status;
2306}
2307
2308c2_status_t Codec2Client::Component::reset() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002309 if (mAidlBase) {
2310 ::ndk::ScopedAStatus transStatus = mAidlBase->reset();
2311 return GetC2Status(transStatus, "reset");
2312 }
2313 Return<c2_hidl::Status> transStatus = mHidlBase1_0->reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002314 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002315 LOG(ERROR) << "reset -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002316 return C2_TRANSACTION_FAILED;
2317 }
2318 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002319 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002320 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002321 LOG(DEBUG) << "reset -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002322 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002323 return status;
2324}
2325
2326c2_status_t Codec2Client::Component::release() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002327 if (mAidlBase) {
2328 ::ndk::ScopedAStatus transStatus = mAidlBase->release();
2329 return GetC2Status(transStatus, "release");
2330 }
2331 Return<c2_hidl::Status> transStatus = mHidlBase1_0->release();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002332 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002333 LOG(ERROR) << "release -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002334 return C2_TRANSACTION_FAILED;
2335 }
2336 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002337 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002338 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002339 LOG(DEBUG) << "release -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002340 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002341 return status;
2342}
2343
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002344c2_status_t Codec2Client::Component::configureVideoTunnel(
2345 uint32_t avSyncHwId,
2346 native_handle_t** sidebandHandle) {
2347 *sidebandHandle = nullptr;
Wonsik Kime8e98152022-12-16 16:04:17 -08002348 if (mAidlBase) {
2349 ::aidl::android::hardware::common::NativeHandle handle;
2350 ::ndk::ScopedAStatus transStatus = mAidlBase->configureVideoTunnel(avSyncHwId, &handle);
2351 c2_status_t status = GetC2Status(transStatus, "configureVideoTunnel");
2352 if (status != C2_OK) {
2353 return status;
2354 }
2355 if (isAidlNativeHandleEmpty(handle)) {
2356 LOG(DEBUG) << "configureVideoTunnel -- empty handle returned";
2357 } else {
2358 *sidebandHandle = dupFromAidl(handle);
2359 }
2360 return C2_OK;
2361 }
2362 if (!mHidlBase1_1) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002363 return C2_OMITTED;
2364 }
2365 c2_status_t status{};
Wonsik Kime8e98152022-12-16 16:04:17 -08002366 Return<void> transStatus = mHidlBase1_1->configureVideoTunnel(avSyncHwId,
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002367 [&status, sidebandHandle](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002368 c2_hidl::Status s, hardware::hidl_handle const& h) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002369 status = static_cast<c2_status_t>(s);
2370 if (h.getNativeHandle()) {
2371 *sidebandHandle = native_handle_clone(h.getNativeHandle());
2372 }
2373 });
2374 if (!transStatus.isOk()) {
2375 LOG(ERROR) << "configureVideoTunnel -- transaction failed.";
2376 return C2_TRANSACTION_FAILED;
2377 }
2378 return status;
2379}
2380
Pawin Vongmasa36653902018-11-15 00:10:25 -08002381c2_status_t Codec2Client::Component::setOutputSurface(
2382 C2BlockPool::local_id_t blockPoolId,
2383 const sp<IGraphicBufferProducer>& surface,
Sungtak Leedb14cba2021-04-10 00:50:23 -07002384 uint32_t generation,
2385 int maxDequeueCount) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002386 if (mAidlBase) {
2387 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2388 mGraphicBufferAllocators->current();
2389 if (!gba) {
2390 LOG(ERROR) << "setOutputSurface for AIDL -- "
2391 "GraphicBufferAllocator was not created.";
2392 return C2_CORRUPTED;
2393 }
2394 bool ret = gba->configure(surface, generation, maxDequeueCount);
2395 return ret ? C2_OK : C2_CORRUPTED;
2396 }
Sungtak Lee08515812019-06-05 11:16:32 -07002397 uint64_t bqId = 0;
2398 sp<IGraphicBufferProducer> nullIgbp;
2399 sp<HGraphicBufferProducer2> nullHgbp;
Pawin Vongmasa3866c7e2019-01-31 05:21:29 -08002400
Sungtak Lee08515812019-06-05 11:16:32 -07002401 sp<HGraphicBufferProducer2> igbp = surface ?
2402 surface->getHalInterface<HGraphicBufferProducer2>() : nullHgbp;
2403 if (surface && !igbp) {
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08002404 igbp = new B2HGraphicBufferProducer2(surface);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002405 }
2406
Sungtak Leefb579022022-05-10 06:36:15 +00002407 std::scoped_lock lock(mOutputMutex);
Sungtak Leea714f112021-03-16 05:40:03 -07002408 std::shared_ptr<SurfaceSyncObj> syncObj;
2409
Sungtak Lee08515812019-06-05 11:16:32 -07002410 if (!surface) {
Sungtak Leedb14cba2021-04-10 00:50:23 -07002411 mOutputBufferQueue->configure(nullIgbp, generation, 0, maxDequeueCount, nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07002412 } else if (surface->getUniqueId(&bqId) != OK) {
2413 LOG(ERROR) << "setOutputSurface -- "
2414 "cannot obtain bufferqueue id.";
2415 bqId = 0;
Sungtak Leedb14cba2021-04-10 00:50:23 -07002416 mOutputBufferQueue->configure(nullIgbp, generation, 0, maxDequeueCount, nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07002417 } else {
Wonsik Kime8e98152022-12-16 16:04:17 -08002418 mOutputBufferQueue->configure(surface, generation, bqId, maxDequeueCount,
2419 mHidlBase1_2 ? &syncObj : nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07002420 }
Lajos Molnar78aa7c92021-02-18 21:39:01 -08002421
2422 // set consumer bits
2423 // TODO: should this get incorporated into setOutputSurface method so that consumer bits
2424 // can be set atomically?
2425 uint64_t consumerUsage = kDefaultConsumerUsage;
2426 {
2427 if (surface) {
Sungtak Lee46a69d62023-08-12 07:24:24 +00002428 uint64_t usage = 0;
2429 status_t err = surface->getConsumerUsage(&usage);
Lajos Molnar78aa7c92021-02-18 21:39:01 -08002430 if (err != NO_ERROR) {
2431 ALOGD("setOutputSurface -- failed to get consumer usage bits (%d/%s). ignoring",
2432 err, asString(err));
2433 } else {
2434 // Note: we are adding the default usage because components must support
2435 // producing output frames that can be displayed an all output surfaces.
2436
2437 // TODO: do not set usage for tunneled scenario. It is unclear if consumer usage
2438 // is meaningful in a tunneled scenario; on one hand output buffers exist, but
2439 // they do not exist inside of C2 scope. Any buffer usage shall be communicated
2440 // through the sideband channel.
2441
Sungtak Lee46a69d62023-08-12 07:24:24 +00002442 consumerUsage = usage | kDefaultConsumerUsage;
Lajos Molnar78aa7c92021-02-18 21:39:01 -08002443 }
2444 }
2445
2446 C2StreamUsageTuning::output outputUsage{
2447 0u, C2AndroidMemoryUsage::FromGrallocUsage(consumerUsage).expected};
2448 std::vector<std::unique_ptr<C2SettingResult>> failures;
2449 c2_status_t err = config({&outputUsage}, C2_MAY_BLOCK, &failures);
2450 if (err != C2_OK) {
2451 ALOGD("setOutputSurface -- failed to set consumer usage (%d/%s)",
2452 err, asString(err));
2453 }
2454 }
2455 ALOGD("setOutputSurface -- generation=%u consumer usage=%#llx%s",
2456 generation, (long long)consumerUsage, syncObj ? " sync" : "");
Sungtak Lee08515812019-06-05 11:16:32 -07002457
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002458 Return<c2_hidl::Status> transStatus = syncObj ?
Wonsik Kime8e98152022-12-16 16:04:17 -08002459 mHidlBase1_2->setOutputSurfaceWithSyncObj(
Sungtak Leea714f112021-03-16 05:40:03 -07002460 static_cast<uint64_t>(blockPoolId),
2461 bqId == 0 ? nullHgbp : igbp, *syncObj) :
Wonsik Kime8e98152022-12-16 16:04:17 -08002462 mHidlBase1_0->setOutputSurface(
Sungtak Leea714f112021-03-16 05:40:03 -07002463 static_cast<uint64_t>(blockPoolId),
2464 bqId == 0 ? nullHgbp : igbp);
Lajos Molnar78aa7c92021-02-18 21:39:01 -08002465
Sungtak Lee8e2e2c62023-01-17 18:29:34 +00002466 mOutputBufferQueue->expireOldWaiters();
2467
Pawin Vongmasa36653902018-11-15 00:10:25 -08002468 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002469 LOG(ERROR) << "setOutputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002470 return C2_TRANSACTION_FAILED;
2471 }
2472 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002473 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002474 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002475 LOG(DEBUG) << "setOutputSurface -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002476 }
Sungtak Leea714f112021-03-16 05:40:03 -07002477 ALOGD("Surface configure completed");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002478 return status;
2479}
2480
2481status_t Codec2Client::Component::queueToOutputSurface(
2482 const C2ConstGraphicBlock& block,
2483 const QueueBufferInput& input,
2484 QueueBufferOutput* output) {
Arun Johnson7ba67072023-11-06 22:23:04 +00002485 ScopedTrace trace(ATRACE_TAG,"Codec2Client::Component::queueToOutputSurface");
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002486 if (mAidlBase) {
2487 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2488 mGraphicBufferAllocators->current();
2489 if (gba) {
2490 return gba->displayBuffer(block, input, output);
2491 } else {
2492 return C2_NOT_FOUND;
2493 }
2494 }
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002495 return mOutputBufferQueue->outputBuffer(block, input, output);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002496}
2497
Brian Lindahlc953b462023-01-27 16:21:43 -07002498void Codec2Client::Component::pollForRenderedFrames(FrameEventHistoryDelta* delta) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002499 if (mAidlBase) {
2500 // TODO b/311348680
2501 return;
2502 }
Brian Lindahlc953b462023-01-27 16:21:43 -07002503 mOutputBufferQueue->pollForRenderedFrames(delta);
2504}
2505
Sungtak Leea714f112021-03-16 05:40:03 -07002506void Codec2Client::Component::setOutputSurfaceMaxDequeueCount(
2507 int maxDequeueCount) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002508 if (mAidlBase) {
2509 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2510 mGraphicBufferAllocators->current();
2511 if (gba) {
2512 gba->updateMaxDequeueBufferCount(maxDequeueCount);
2513 }
2514 return;
2515 }
Sungtak Leea714f112021-03-16 05:40:03 -07002516 mOutputBufferQueue->updateMaxDequeueBufferCount(maxDequeueCount);
2517}
2518
Sungtak Leec7da7a02022-05-05 08:45:33 +00002519void Codec2Client::Component::stopUsingOutputSurface(
2520 C2BlockPool::local_id_t blockPoolId) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002521 if (mAidlBase) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002522 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2523 mGraphicBufferAllocators->current();
2524 if (gba) {
2525 gba->reset();
2526 }
Wonsik Kime8e98152022-12-16 16:04:17 -08002527 return;
2528 }
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002529 std::scoped_lock lock(mOutputMutex);
2530 mOutputBufferQueue->stop();
Wonsik Kime8e98152022-12-16 16:04:17 -08002531 Return<c2_hidl::Status> transStatus = mHidlBase1_0->setOutputSurface(
Sungtak Leec7da7a02022-05-05 08:45:33 +00002532 static_cast<uint64_t>(blockPoolId), nullptr);
2533 if (!transStatus.isOk()) {
2534 LOG(ERROR) << "setOutputSurface(stopUsingOutputSurface) -- transaction failed.";
2535 } else {
2536 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002537 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Sungtak Leec7da7a02022-05-05 08:45:33 +00002538 if (status != C2_OK) {
2539 LOG(DEBUG) << "setOutputSurface(stopUsingOutputSurface) -- call failed: "
2540 << status << ".";
2541 }
2542 }
Sungtak Lee8e2e2c62023-01-17 18:29:34 +00002543 mOutputBufferQueue->expireOldWaiters();
Sungtak Leec7da7a02022-05-05 08:45:33 +00002544}
2545
Sungtak Lee214ce612023-11-01 10:01:13 +00002546void Codec2Client::Component::onBufferReleasedFromOutputSurface(
2547 uint32_t generation) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002548 if (mAidlBase) {
2549 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2550 mGraphicBufferAllocators->current();
2551 if (gba) {
2552 gba->onBufferReleased(generation);
2553 }
2554 return;
2555 }
Sungtak Lee6700cc92023-11-22 18:04:05 +00002556 mOutputBufferQueue->onBufferReleased(generation);
Sungtak Lee214ce612023-11-01 10:01:13 +00002557}
2558
Sungtak Lee1720e4c2024-07-31 21:15:26 +00002559void Codec2Client::Component::onBufferAttachedToOutputSurface(
2560 uint32_t generation) {
2561 if (mAidlBase) {
2562 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2563 mGraphicBufferAllocators->current();
2564 if (gba) {
2565 gba->onBufferAttached(generation);
2566 }
2567 return;
2568 }
2569 mOutputBufferQueue->onBufferAttached(generation);
2570}
2571
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002572void Codec2Client::Component::holdIgbaBlocks(
2573 const std::list<std::unique_ptr<C2Work>>& workList) {
2574 if (!mAidlBase) {
2575 return;
2576 }
2577 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2578 mGraphicBufferAllocators->current();
2579 if (!gba) {
2580 return;
2581 }
2582 std::shared_ptr<c2_aidl::IGraphicBufferAllocator> igba =
2583 c2_aidl::IGraphicBufferAllocator::fromBinder(gba->asBinder());
2584 for (const std::unique_ptr<C2Work>& work : workList) {
2585 if (!work) [[unlikely]] {
2586 continue;
2587 }
2588 for (const std::unique_ptr<C2Worklet>& worklet : work->worklets) {
2589 if (!worklet) {
2590 continue;
2591 }
2592 for (const std::shared_ptr<C2Buffer>& buffer : worklet->output.buffers) {
2593 if (buffer) {
2594 for (const C2ConstGraphicBlock& block : buffer->data().graphicBlocks()) {
2595 std::shared_ptr<_C2BlockPoolData> poolData =
2596 _C2BlockFactory::GetGraphicBlockPoolData(block);
2597 _C2BlockFactory::RegisterIgba(poolData, igba);
2598 }
2599 }
2600 }
2601 }
2602 }
2603}
2604
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002605c2_status_t Codec2Client::Component::connectToInputSurface(
2606 const std::shared_ptr<InputSurface>& inputSurface,
2607 std::shared_ptr<InputSurfaceConnection>* connection) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002608 if (mAidlBase) {
2609 // FIXME
2610 return C2_OMITTED;
2611 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002612 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08002613 Return<void> transStatus = mHidlBase1_0->connectToInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002614 inputSurface->mBase,
2615 [&status, connection](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002616 c2_hidl::Status s, const sp<c2_hidl::IInputSurfaceConnection>& c) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002617 status = static_cast<c2_status_t>(s);
2618 if (status != C2_OK) {
2619 LOG(DEBUG) << "connectToInputSurface -- call failed: "
2620 << status << ".";
2621 return;
2622 }
2623 *connection = std::make_shared<InputSurfaceConnection>(c);
2624 });
Pawin Vongmasa36653902018-11-15 00:10:25 -08002625 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002626 LOG(ERROR) << "connectToInputSurface -- transaction failed";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002627 return C2_TRANSACTION_FAILED;
2628 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002629 return status;
2630}
2631
2632c2_status_t Codec2Client::Component::connectToOmxInputSurface(
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08002633 const sp<HGraphicBufferProducer1>& producer,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002634 const sp<HGraphicBufferSource>& source,
2635 std::shared_ptr<InputSurfaceConnection>* connection) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002636 if (mAidlBase) {
2637 LOG(WARNING) << "Connecting to OMX input surface is not supported for AIDL C2 HAL";
2638 return C2_OMITTED;
2639 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002640 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08002641 Return<void> transStatus = mHidlBase1_0->connectToOmxInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002642 producer, source,
2643 [&status, connection](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002644 c2_hidl::Status s, const sp<c2_hidl::IInputSurfaceConnection>& c) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002645 status = static_cast<c2_status_t>(s);
2646 if (status != C2_OK) {
2647 LOG(DEBUG) << "connectToOmxInputSurface -- call failed: "
2648 << status << ".";
2649 return;
2650 }
2651 *connection = std::make_shared<InputSurfaceConnection>(c);
2652 });
2653 if (!transStatus.isOk()) {
2654 LOG(ERROR) << "connectToOmxInputSurface -- transaction failed.";
2655 return C2_TRANSACTION_FAILED;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002656 }
2657 return status;
2658}
2659
2660c2_status_t Codec2Client::Component::disconnectFromInputSurface() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002661 if (mAidlBase) {
2662 // FIXME
2663 return C2_OMITTED;
2664 }
2665 Return<c2_hidl::Status> transStatus = mHidlBase1_0->disconnectFromInputSurface();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002666 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002667 LOG(ERROR) << "disconnectToInputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002668 return C2_TRANSACTION_FAILED;
2669 }
2670 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002671 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002672 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002673 LOG(DEBUG) << "disconnectFromInputSurface -- call failed: "
2674 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002675 }
2676 return status;
2677}
2678
Wonsik Kime8e98152022-12-16 16:04:17 -08002679Codec2Client::Component::AidlDeathManager *Codec2Client::Component::GetAidlDeathManager() {
2680 // This object never gets destructed
2681 static AidlDeathManager *sManager = new AidlDeathManager();
2682 return sManager;
2683}
2684
Pawin Vongmasa36653902018-11-15 00:10:25 -08002685c2_status_t Codec2Client::Component::setDeathListener(
2686 const std::shared_ptr<Component>& component,
2687 const std::shared_ptr<Listener>& listener) {
2688
2689 struct HidlDeathRecipient : public hardware::hidl_death_recipient {
2690 std::weak_ptr<Component> component;
2691 std::weak_ptr<Listener> base;
2692
2693 virtual void serviceDied(
2694 uint64_t /* cookie */,
2695 const wp<::android::hidl::base::V1_0::IBase>& /* who */
2696 ) override {
2697 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
2698 listener->onDeath(component);
2699 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002700 LOG(DEBUG) << "onDeath -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002701 }
2702 }
2703 };
2704
Wonsik Kime8e98152022-12-16 16:04:17 -08002705 if (component->mAidlBase) {
2706 size_t seq;
2707 if (GetAidlDeathManager()->linkToDeath(component, listener, &seq)) {
2708 component->mAidlDeathSeq = seq;
2709 }
2710 return C2_OK;
2711 }
2712
Pawin Vongmasa36653902018-11-15 00:10:25 -08002713 sp<HidlDeathRecipient> deathRecipient = new HidlDeathRecipient();
2714 deathRecipient->base = listener;
2715 deathRecipient->component = component;
2716
2717 component->mDeathRecipient = deathRecipient;
Wonsik Kime8e98152022-12-16 16:04:17 -08002718 Return<bool> transResult = component->mHidlBase1_0->linkToDeath(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002719 component->mDeathRecipient, 0);
2720 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002721 LOG(ERROR) << "setDeathListener -- linkToDeath() transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002722 return C2_TRANSACTION_FAILED;
2723 }
2724 if (!static_cast<bool>(transResult)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002725 LOG(DEBUG) << "setDeathListener -- linkToDeath() call failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002726 return C2_CORRUPTED;
2727 }
2728 return C2_OK;
2729}
2730
2731// Codec2Client::InputSurface
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002732Codec2Client::InputSurface::InputSurface(const sp<c2_hidl::IInputSurface>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002733 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002734 [base]() -> sp<c2_hidl::IConfigurable> {
2735 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002736 base->getConfigurable();
2737 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002738 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002739 nullptr;
2740 }()
2741 },
2742 mBase{base},
2743 mGraphicBufferProducer{new
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08002744 H2BGraphicBufferProducer2([base]() -> sp<HGraphicBufferProducer2> {
2745 Return<sp<HGraphicBufferProducer2>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002746 base->getGraphicBufferProducer();
2747 return transResult.isOk() ?
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08002748 static_cast<sp<HGraphicBufferProducer2>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002749 nullptr;
2750 }())} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002751}
2752
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002753sp<IGraphicBufferProducer>
Pawin Vongmasa36653902018-11-15 00:10:25 -08002754 Codec2Client::InputSurface::getGraphicBufferProducer() const {
2755 return mGraphicBufferProducer;
2756}
2757
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002758sp<c2_hidl::IInputSurface> Codec2Client::InputSurface::getHalInterface() const {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002759 return mBase;
2760}
2761
2762// Codec2Client::InputSurfaceConnection
Pawin Vongmasa36653902018-11-15 00:10:25 -08002763Codec2Client::InputSurfaceConnection::InputSurfaceConnection(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002764 const sp<c2_hidl::IInputSurfaceConnection>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002765 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002766 [base]() -> sp<c2_hidl::IConfigurable> {
2767 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002768 base->getConfigurable();
2769 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002770 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002771 nullptr;
2772 }()
2773 },
2774 mBase{base} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002775}
2776
2777c2_status_t Codec2Client::InputSurfaceConnection::disconnect() {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002778 Return<c2_hidl::Status> transResult = mBase->disconnect();
2779 return static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transResult));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002780}
2781
2782} // namespace android