blob: b872cea4b0953b89794e9456ef97899178bcae2a [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;
652 for (auto it = paramPointers.begin();
653 it != paramPointers.end(); ) {
654 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 }
680 if (!stackParams[i++]->updateFrom(*paramPointer)) {
681 LOG(WARNING) << "query -- param update failed: "
682 "index = "
683 << paramPointer->index() << ".";
684 }
685 } else {
686 if (!paramPointer) {
687 LOG(DEBUG) << "query -- null heap param.";
688 ++it;
689 continue;
690 }
691 if (!heapParams) {
692 LOG(WARNING) << "query -- "
693 "unexpected extra stack param.";
694 } else {
695 heapParams->emplace_back(C2Param::Copy(*paramPointer));
696 }
697 }
698 ++it;
699 }
700 return status;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800701}
702
703c2_status_t Codec2ConfigurableClient::AidlImpl::config(
704 const std::vector<C2Param*> &params,
705 c2_blocking_t mayBlock,
706 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
Wonsik Kim1caded02022-12-09 13:03:11 -0800707 c2_aidl::Params aidlParams;
708 if (!c2_aidl::utils::CreateParamsBlob(&aidlParams, params)) {
709 LOG(ERROR) << "config -- bad input.";
710 return C2_TRANSACTION_FAILED;
711 }
712 c2_aidl::IConfigurable::ConfigResult result;
713 ndk::ScopedAStatus transStatus = mBase->config(aidlParams, (mayBlock == C2_MAY_BLOCK), &result);
Wonsik Kime8e98152022-12-16 16:04:17 -0800714 c2_status_t status = GetC2Status(transStatus, "config");
715 if (status != C2_OK) {
716 return status;
Wonsik Kim1caded02022-12-09 13:03:11 -0800717 }
Sungtak Lee932255f2024-01-03 10:57:45 +0000718 status = static_cast<c2_status_t>(result.status.status);
Wonsik Kim1caded02022-12-09 13:03:11 -0800719 size_t i = failures->size();
720 failures->resize(i + result.failures.size());
721 for (const c2_aidl::SettingResult& sf : result.failures) {
722 if (!c2_aidl::utils::FromAidl(&(*failures)[i++], sf)) {
723 LOG(ERROR) << "config -- invalid SettingResult returned.";
724 return C2_CORRUPTED;
725 }
726 }
727 if (!c2_aidl::utils::UpdateParamsFromBlob(params, result.params)) {
728 LOG(ERROR) << "config -- "
729 << "failed to parse returned params.";
730 status = C2_CORRUPTED;
731 }
732 return status;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800733}
734
735c2_status_t Codec2ConfigurableClient::AidlImpl::querySupportedParams(
736 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
Wonsik Kim1caded02022-12-09 13:03:11 -0800737 // TODO: Cache and query properly!
738 std::vector<c2_aidl::ParamDescriptor> result;
739 ndk::ScopedAStatus transStatus = mBase->querySupportedParams(
740 std::numeric_limits<uint32_t>::min(),
741 std::numeric_limits<uint32_t>::max(),
742 &result);
Wonsik Kime8e98152022-12-16 16:04:17 -0800743 c2_status_t status = GetC2Status(transStatus, "querySupportedParams");
744 if (status != C2_OK) {
745 return status;
Wonsik Kim1caded02022-12-09 13:03:11 -0800746 }
Wonsik Kim1caded02022-12-09 13:03:11 -0800747 size_t i = params->size();
748 params->resize(i + result.size());
749 for (const c2_aidl::ParamDescriptor& sp : result) {
750 if (!c2_aidl::utils::FromAidl(&(*params)[i++], sp)) {
751 LOG(ERROR) << "querySupportedParams -- invalid returned ParamDescriptor.";
752 return C2_CORRUPTED;
753 }
754 }
755 return status;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800756}
757
758c2_status_t Codec2ConfigurableClient::AidlImpl::querySupportedValues(
759 std::vector<C2FieldSupportedValuesQuery>& fields,
760 c2_blocking_t mayBlock) const {
Wonsik Kim1caded02022-12-09 13:03:11 -0800761 std::vector<c2_aidl::FieldSupportedValuesQuery> inFields(fields.size());
762 for (size_t i = 0; i < fields.size(); ++i) {
763 if (!c2_aidl::utils::ToAidl(&inFields[i], fields[i])) {
764 LOG(ERROR) << "querySupportedValues -- bad input";
765 return C2_TRANSACTION_FAILED;
766 }
767 }
768
Sungtak Lee932255f2024-01-03 10:57:45 +0000769 c2_aidl::IConfigurable::QuerySupportedValuesResult result;
770
Wonsik Kim1caded02022-12-09 13:03:11 -0800771 ndk::ScopedAStatus transStatus = mBase->querySupportedValues(
772 inFields, (mayBlock == C2_MAY_BLOCK), &result);
Wonsik Kime8e98152022-12-16 16:04:17 -0800773 c2_status_t status = GetC2Status(transStatus, "querySupportedValues");
774 if (status != C2_OK) {
775 return status;
Wonsik Kim1caded02022-12-09 13:03:11 -0800776 }
Sungtak Lee932255f2024-01-03 10:57:45 +0000777 status = static_cast<c2_status_t>(result.status.status);
778 if (result.values.size() != fields.size()) {
Wonsik Kim1caded02022-12-09 13:03:11 -0800779 LOG(ERROR) << "querySupportedValues -- "
780 "input and output lists "
781 "have different sizes.";
782 return C2_CORRUPTED;
783 }
784 for (size_t i = 0; i < fields.size(); ++i) {
Sungtak Lee932255f2024-01-03 10:57:45 +0000785 if (!c2_aidl::utils::FromAidl(&fields[i], inFields[i], result.values[i])) {
Wonsik Kim1caded02022-12-09 13:03:11 -0800786 LOG(ERROR) << "querySupportedValues -- "
787 "invalid returned value.";
788 return C2_CORRUPTED;
789 }
790 }
791 return status;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800792}
793
794// Codec2ConfigurableClient
795
796Codec2ConfigurableClient::Codec2ConfigurableClient(const sp<HidlBase> &hidlBase)
797 : mImpl(new Codec2ConfigurableClient::HidlImpl(hidlBase)) {
798}
799
800Codec2ConfigurableClient::Codec2ConfigurableClient(
801 const std::shared_ptr<AidlBase> &aidlBase)
802 : mImpl(new Codec2ConfigurableClient::AidlImpl(aidlBase)) {
803}
804
805const C2String& Codec2ConfigurableClient::getName() const {
806 return mImpl->getName();
807}
808
809c2_status_t Codec2ConfigurableClient::query(
810 const std::vector<C2Param*>& stackParams,
811 const std::vector<C2Param::Index> &heapParamIndices,
812 c2_blocking_t mayBlock,
813 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
814 return mImpl->query(stackParams, heapParamIndices, mayBlock, heapParams);
815}
816
817c2_status_t Codec2ConfigurableClient::config(
818 const std::vector<C2Param*> &params,
819 c2_blocking_t mayBlock,
820 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
821 return mImpl->config(params, mayBlock, failures);
822}
823
824c2_status_t Codec2ConfigurableClient::querySupportedParams(
825 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
826 return mImpl->querySupportedParams(params);
827}
828
829c2_status_t Codec2ConfigurableClient::querySupportedValues(
830 std::vector<C2FieldSupportedValuesQuery>& fields,
831 c2_blocking_t mayBlock) const {
832 return mImpl->querySupportedValues(fields, mayBlock);
833}
834
835
Pawin Vongmasa36653902018-11-15 00:10:25 -0800836// Codec2Client::Component::HidlListener
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800837struct Codec2Client::Component::HidlListener : public c2_hidl::IComponentListener {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800838 std::weak_ptr<Component> component;
839 std::weak_ptr<Listener> base;
840
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800841 virtual Return<void> onWorkDone(const c2_hidl::WorkBundle& workBundle) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800842 std::list<std::unique_ptr<C2Work>> workItems;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800843 if (!c2_hidl::utils::objcpy(&workItems, workBundle)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800844 LOG(DEBUG) << "onWorkDone -- received corrupted WorkBundle.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800845 return Void();
846 }
847 // release input buffers potentially held by the component from queue
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800848 std::shared_ptr<Codec2Client::Component> strongComponent =
849 component.lock();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800850 if (strongComponent) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800851 strongComponent->handleOnWorkDone(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800852 }
853 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800854 listener->onWorkDone(component, workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800855 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800856 LOG(DEBUG) << "onWorkDone -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800857 }
858 return Void();
859 }
860
861 virtual Return<void> onTripped(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800862 const hidl_vec<c2_hidl::SettingResult>& settingResults) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800863 std::vector<std::shared_ptr<C2SettingResult>> c2SettingResults(
864 settingResults.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800865 for (size_t i = 0; i < settingResults.size(); ++i) {
866 std::unique_ptr<C2SettingResult> c2SettingResult;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800867 if (!c2_hidl::utils::objcpy(&c2SettingResult, settingResults[i])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800868 LOG(DEBUG) << "onTripped -- received corrupted SettingResult.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800869 return Void();
870 }
871 c2SettingResults[i] = std::move(c2SettingResult);
872 }
873 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
874 listener->onTripped(component, c2SettingResults);
875 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800876 LOG(DEBUG) << "onTripped -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800877 }
878 return Void();
879 }
880
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800881 virtual Return<void> onError(c2_hidl::Status s, uint32_t errorCode) override {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800882 LOG(DEBUG) << "onError --"
883 << " status = " << s
884 << ", errorCode = " << errorCode
885 << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800886 if (std::shared_ptr<Listener> listener = base.lock()) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800887 listener->onError(component, s == c2_hidl::Status::OK ?
Pawin Vongmasa36653902018-11-15 00:10:25 -0800888 errorCode : static_cast<c2_status_t>(s));
889 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800890 LOG(DEBUG) << "onError -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800891 }
892 return Void();
893 }
894
895 virtual Return<void> onFramesRendered(
896 const hidl_vec<RenderedFrame>& renderedFrames) override {
897 std::shared_ptr<Listener> listener = base.lock();
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800898 if (!listener) {
899 LOG(DEBUG) << "onFramesRendered -- listener died.";
900 return Void();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800901 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800902 for (const RenderedFrame& renderedFrame : renderedFrames) {
903 listener->onFrameRendered(
904 renderedFrame.bufferQueueId,
905 renderedFrame.slotId,
906 renderedFrame.timestampNs);
907 }
908 return Void();
909 }
910
911 virtual Return<void> onInputBuffersReleased(
912 const hidl_vec<InputBuffer>& inputBuffers) override {
913 std::shared_ptr<Listener> listener = base.lock();
914 if (!listener) {
915 LOG(DEBUG) << "onInputBuffersReleased -- listener died.";
916 return Void();
917 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800918 for (const InputBuffer& inputBuffer : inputBuffers) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800919 LOG(VERBOSE) << "onInputBuffersReleased --"
920 " received death notification of"
921 " input buffer:"
922 " frameIndex = " << inputBuffer.frameIndex
923 << ", bufferIndex = " << inputBuffer.arrayIndex
924 << ".";
Wonsik Kimab34ed62019-01-31 15:28:46 -0800925 listener->onInputBufferDone(
926 inputBuffer.frameIndex, inputBuffer.arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800927 }
928 return Void();
929 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800930
Pawin Vongmasa36653902018-11-15 00:10:25 -0800931};
932
Wonsik Kime8e98152022-12-16 16:04:17 -0800933// Codec2Client::Component::AidlListener
934struct Codec2Client::Component::AidlListener : public c2_aidl::BnComponentListener {
935 std::weak_ptr<Component> component;
936 std::weak_ptr<Listener> base;
937
938 virtual ::ndk::ScopedAStatus onWorkDone(const c2_aidl::WorkBundle& workBundle) override {
939 std::list<std::unique_ptr<C2Work>> workItems;
940 if (!c2_aidl::utils::FromAidl(&workItems, workBundle)) {
941 LOG(DEBUG) << "onWorkDone -- received corrupted WorkBundle.";
942 return ::ndk::ScopedAStatus::ok();
943 }
944 // release input buffers potentially held by the component from queue
945 std::shared_ptr<Codec2Client::Component> strongComponent =
946 component.lock();
947 if (strongComponent) {
948 strongComponent->handleOnWorkDone(workItems);
949 }
950 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
951 listener->onWorkDone(component, workItems);
952 } else {
953 LOG(DEBUG) << "onWorkDone -- listener died.";
954 }
955 return ::ndk::ScopedAStatus::ok();
956 }
957
958 virtual ::ndk::ScopedAStatus onTripped(
959 const std::vector<c2_aidl::SettingResult>& settingResults) override {
960 std::vector<std::shared_ptr<C2SettingResult>> c2SettingResults(
961 settingResults.size());
962 for (size_t i = 0; i < settingResults.size(); ++i) {
963 std::unique_ptr<C2SettingResult> c2SettingResult;
964 if (!c2_aidl::utils::FromAidl(&c2SettingResult, settingResults[i])) {
965 LOG(DEBUG) << "onTripped -- received corrupted SettingResult.";
966 return ::ndk::ScopedAStatus::ok();
967 }
968 c2SettingResults[i] = std::move(c2SettingResult);
969 }
970 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
971 listener->onTripped(component, c2SettingResults);
972 } else {
973 LOG(DEBUG) << "onTripped -- listener died.";
974 }
975 return ::ndk::ScopedAStatus::ok();
976 }
977
978 virtual ::ndk::ScopedAStatus onError(const c2_aidl::Status &s, int32_t errorCode) override {
979 LOG(DEBUG) << "onError --"
980 << " status = " << s.status
981 << ", errorCode = " << errorCode
982 << ".";
983 if (std::shared_ptr<Listener> listener = base.lock()) {
984 listener->onError(component, s.status == c2_aidl::Status::OK ?
985 errorCode : static_cast<c2_status_t>(s.status));
986 } else {
987 LOG(DEBUG) << "onError -- listener died.";
988 }
989 return ::ndk::ScopedAStatus::ok();
990 }
991
992 virtual ::ndk::ScopedAStatus onFramesRendered(
993 const std::vector<RenderedFrame>& renderedFrames) override {
994 std::shared_ptr<Listener> listener = base.lock();
995 if (!listener) {
996 LOG(DEBUG) << "onFramesRendered -- listener died.";
997 return ::ndk::ScopedAStatus::ok();
998 }
999 for (const RenderedFrame& renderedFrame : renderedFrames) {
1000 listener->onFrameRendered(
1001 renderedFrame.bufferQueueId,
1002 renderedFrame.slotId,
1003 renderedFrame.timestampNs);
1004 }
1005 return ::ndk::ScopedAStatus::ok();
1006 }
1007
1008 virtual ::ndk::ScopedAStatus onInputBuffersReleased(
1009 const std::vector<InputBuffer>& inputBuffers) override {
1010 std::shared_ptr<Listener> listener = base.lock();
1011 if (!listener) {
1012 LOG(DEBUG) << "onInputBuffersReleased -- listener died.";
1013 return ::ndk::ScopedAStatus::ok();
1014 }
1015 for (const InputBuffer& inputBuffer : inputBuffers) {
1016 LOG(VERBOSE) << "onInputBuffersReleased --"
1017 " received death notification of"
1018 " input buffer:"
1019 " frameIndex = " << inputBuffer.frameIndex
1020 << ", bufferIndex = " << inputBuffer.arrayIndex
1021 << ".";
1022 listener->onInputBufferDone(
1023 inputBuffer.frameIndex, inputBuffer.arrayIndex);
1024 }
1025 return ::ndk::ScopedAStatus::ok();
1026 }
1027
1028};
1029
1030// Codec2Client::Component::HidlBufferPoolSender
1031struct Codec2Client::Component::HidlBufferPoolSender :
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001032 hardware::media::c2::V1_1::utils::DefaultBufferPoolSender {
Wonsik Kime8e98152022-12-16 16:04:17 -08001033 HidlBufferPoolSender()
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001034 : hardware::media::c2::V1_1::utils::DefaultBufferPoolSender() {
1035 }
1036};
1037
Wonsik Kime8e98152022-12-16 16:04:17 -08001038// Codec2Client::Component::AidlBufferPoolSender
1039struct Codec2Client::Component::AidlBufferPoolSender :
1040 c2_aidl::utils::DefaultBufferPoolSender {
1041 AidlBufferPoolSender()
1042 : c2_aidl::utils::DefaultBufferPoolSender() {
1043 }
1044};
1045
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001046// Codec2Client::Component::OutputBufferQueue
1047struct Codec2Client::Component::OutputBufferQueue :
Sungtak Leea714f112021-03-16 05:40:03 -07001048 hardware::media::c2::OutputBufferQueue {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001049 OutputBufferQueue()
Sungtak Leea714f112021-03-16 05:40:03 -07001050 : hardware::media::c2::OutputBufferQueue() {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001051 }
1052};
1053
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00001054// The class holds GraphicBufferAllocator and the associated id of
1055// HAL side BlockPool.
1056// This is tightly coupled with BlockPool creation and destruction.
1057// The life cycle inside class will be as follows.
1058//
1059// On createBlockPool client request.
1060// 1. this::create() creates a GraphicBufferAllocator and set it as
1061// the current.
1062// 2. C2AIDL_HAL::createBlockPool() creates a C2BlockPool using
1063// the GraphicBufferAllocator created in #1.
1064// 3. this::setCurrentId() associates the id returned in #2 to the current
1065//
1066// On destroyBlockPool cliet request
1067// 1. C2AIDL_HAL::destroyBlockPool() destroys the block pool
1068// from HAL process.
1069// 2. this::remove() destroys GraphicBufferAllocator which is associatted
1070// with the C2BlockPool in #1.
1071//
1072struct Codec2Client::Component::GraphicBufferAllocators {
1073private:
1074 std::optional<C2BlockPool::local_id_t> mCurrentId;
1075 std::shared_ptr<AidlGraphicBufferAllocator> mCurrent;
1076
1077 // A new BlockPool is created before the old BlockPool is destroyed.
1078 // This holds the reference of the old BlockPool when a new BlockPool is
1079 // created until the old BlockPool is explicitly requested for destruction.
1080 std::map<C2BlockPool::local_id_t, std::shared_ptr<AidlGraphicBufferAllocator>> mOlds;
1081 std::mutex mMutex;
1082
1083public:
1084 // Creates a GraphicBufferAllocator which will be passed to HAL
1085 // for creating C2BlockPool. And the created GraphicBufferAllocator
1086 // will be used afterwards by current().
1087 std::shared_ptr<AidlGraphicBufferAllocator> create() {
1088 std::unique_lock<std::mutex> l(mMutex);
1089 if (mCurrent) {
1090 // If this is not stopped.
1091 mCurrent->reset();
1092 if (mCurrentId.has_value()) {
1093 mOlds.emplace(mCurrentId.value(), mCurrent);
1094 }
1095 mCurrentId.reset();
1096 mCurrent.reset();
1097 }
1098 // TODO: integrate initial value with CCodec/CCodecBufferChannel
1099 mCurrent =
1100 AidlGraphicBufferAllocator::CreateGraphicBufferAllocator(3 /* maxDequeueCount */);
1101 ALOGD("GraphicBufferAllocator created");
1102 return mCurrent;
1103 }
1104
1105 // Associates the blockpool Id returned from HAL to the
1106 // current GraphicBufferAllocator.
1107 void setCurrentId(C2BlockPool::local_id_t id) {
1108 std::unique_lock<std::mutex> l(mMutex);
1109 CHECK(!mCurrentId.has_value());
1110 mCurrentId = id;
1111 }
1112
1113 // Returns the current GraphicBufferAllocator.
1114 std::shared_ptr<AidlGraphicBufferAllocator> current() {
1115 std::unique_lock<std::mutex> l(mMutex);
1116 return mCurrent;
1117 }
1118
1119 // Removes the GraphicBufferAllocator associated with given \p id.
1120 void remove(C2BlockPool::local_id_t id) {
1121 std::unique_lock<std::mutex> l(mMutex);
1122 mOlds.erase(id);
1123 if (mCurrentId == id) {
1124 if (mCurrent) {
1125 mCurrent->reset();
1126 mCurrent.reset();
1127 }
1128 mCurrentId.reset();
1129 }
1130 }
1131};
1132
Pawin Vongmasa36653902018-11-15 00:10:25 -08001133// Codec2Client
Wonsik Kime8e98152022-12-16 16:04:17 -08001134Codec2Client::Codec2Client(sp<HidlBase> const& base,
Sungtak Leef4b39512023-05-10 07:17:22 +00001135 sp<c2_hidl::IConfigurable> const& configurable,
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001136 size_t serviceIndex)
Sungtak Leef4b39512023-05-10 07:17:22 +00001137 : Configurable{configurable},
Wonsik Kime8e98152022-12-16 16:04:17 -08001138 mHidlBase1_0{base},
1139 mHidlBase1_1{HidlBase1_1::castFrom(base)},
1140 mHidlBase1_2{HidlBase1_2::castFrom(base)},
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001141 mServiceIndex{serviceIndex} {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001142 Return<sp<bufferpool_hidl::IClientManager>> transResult = base->getPoolClientManager();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001143 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001144 LOG(ERROR) << "getPoolClientManager -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001145 } else {
Wonsik Kime8e98152022-12-16 16:04:17 -08001146 mHidlHostPoolManager = static_cast<sp<bufferpool_hidl::IClientManager>>(transResult);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001147 }
1148}
1149
Wonsik Kime8e98152022-12-16 16:04:17 -08001150Codec2Client::Codec2Client(std::shared_ptr<AidlBase> const& base,
1151 std::shared_ptr<c2_aidl::IConfigurable> const& configurable,
1152 size_t serviceIndex)
1153 : Configurable{configurable},
1154 mAidlBase{base},
1155 mServiceIndex{serviceIndex} {
1156 ::ndk::ScopedAStatus transStatus = base->getPoolClientManager(&mAidlHostPoolManager);
1157 if (!transStatus.isOk()) {
1158 LOG(ERROR) << "getPoolClientManager -- transaction failed.";
1159 mAidlHostPoolManager.reset();
1160 }
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001161}
1162
Wonsik Kime8e98152022-12-16 16:04:17 -08001163sp<Codec2Client::HidlBase> const& Codec2Client::getHidlBase() const {
1164 return mHidlBase1_0;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001165}
1166
Wonsik Kime8e98152022-12-16 16:04:17 -08001167sp<Codec2Client::HidlBase1_0> const& Codec2Client::getHidlBase1_0() const {
1168 return mHidlBase1_0;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001169}
1170
Wonsik Kime8e98152022-12-16 16:04:17 -08001171sp<Codec2Client::HidlBase1_1> const& Codec2Client::getHidlBase1_1() const {
1172 return mHidlBase1_1;
1173}
1174
1175sp<Codec2Client::HidlBase1_2> const& Codec2Client::getHidlBase1_2() const {
1176 return mHidlBase1_2;
1177}
1178
1179::ndk::SpAIBinder Codec2Client::getAidlBase() const {
1180 return mAidlBase ? mAidlBase->asBinder() : nullptr;
Sungtak Lee8577dab2021-03-12 02:25:50 -08001181}
1182
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001183std::string const& Codec2Client::getServiceName() const {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001184 return GetServiceNames()[mServiceIndex];
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001185}
1186
Pawin Vongmasa36653902018-11-15 00:10:25 -08001187c2_status_t Codec2Client::createComponent(
1188 const C2String& name,
1189 const std::shared_ptr<Codec2Client::Listener>& listener,
1190 std::shared_ptr<Codec2Client::Component>* const component) {
Wonsik Kime8e98152022-12-16 16:04:17 -08001191 if (mAidlBase) {
1192 std::shared_ptr<Component::AidlListener> aidlListener =
1193 Component::AidlListener::make<Component::AidlListener>();
1194 aidlListener->base = listener;
1195 std::shared_ptr<c2_aidl::IComponent> aidlComponent;
1196 ::ndk::ScopedAStatus transStatus = mAidlBase->createComponent(
1197 name,
1198 aidlListener,
1199 bufferpool2_aidl::implementation::ClientManager::getInstance(),
1200 &aidlComponent);
1201 c2_status_t status = GetC2Status(transStatus, "createComponent");
1202 if (status != C2_OK) {
1203 return status;
1204 } else if (!aidlComponent) {
1205 LOG(ERROR) << "createComponent(" << name.c_str()
1206 << ") -- null component.";
1207 return C2_CORRUPTED;
1208 }
1209 *component = std::make_shared<Codec2Client::Component>(aidlComponent);
1210 status = (*component)->setDeathListener((*component), listener);
1211 if (status != C2_OK) {
1212 LOG(ERROR) << "createComponent(" << name.c_str()
1213 << ") -- failed to set up death listener: "
1214 << status << ".";
1215 }
1216 (*component)->mAidlBufferPoolSender->setReceiver(mAidlHostPoolManager);
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00001217 aidlListener->component = *component;
Wonsik Kime8e98152022-12-16 16:04:17 -08001218 return status;
1219 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001220
Pawin Vongmasa36653902018-11-15 00:10:25 -08001221 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001222 sp<Component::HidlListener> hidlListener = new Component::HidlListener{};
Pawin Vongmasa36653902018-11-15 00:10:25 -08001223 hidlListener->base = listener;
Sungtak Lee8577dab2021-03-12 02:25:50 -08001224 Return<void> transStatus;
Wonsik Kime8e98152022-12-16 16:04:17 -08001225 if (mHidlBase1_2) {
1226 transStatus = mHidlBase1_2->createComponent_1_2(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001227 name,
1228 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001229 bufferpool_hidl::implementation::ClientManager::getInstance(),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001230 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001231 c2_hidl::Status s,
1232 const sp<c2_hidl::IComponent>& c) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001233 status = static_cast<c2_status_t>(s);
1234 if (status != C2_OK) {
1235 return;
1236 }
1237 *component = std::make_shared<Codec2Client::Component>(c);
1238 hidlListener->component = *component;
Sungtak Lee8577dab2021-03-12 02:25:50 -08001239 });
1240 }
Wonsik Kime8e98152022-12-16 16:04:17 -08001241 else if (mHidlBase1_1) {
1242 transStatus = mHidlBase1_1->createComponent_1_1(
Sungtak Lee8577dab2021-03-12 02:25:50 -08001243 name,
1244 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001245 bufferpool_hidl::implementation::ClientManager::getInstance(),
Sungtak Lee8577dab2021-03-12 02:25:50 -08001246 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001247 c2_hidl::Status s,
1248 const sp<c2_hidl_base::V1_1::IComponent>& c) {
Sungtak Lee8577dab2021-03-12 02:25:50 -08001249 status = static_cast<c2_status_t>(s);
1250 if (status != C2_OK) {
1251 return;
1252 }
1253 *component = std::make_shared<Codec2Client::Component>(c);
1254 hidlListener->component = *component;
1255 });
Wonsik Kime8e98152022-12-16 16:04:17 -08001256 } else if (mHidlBase1_0) { // ver1_0
1257 transStatus = mHidlBase1_0->createComponent(
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001258 name,
1259 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001260 bufferpool_hidl::implementation::ClientManager::getInstance(),
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001261 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001262 c2_hidl::Status s,
1263 const sp<c2_hidl_base::V1_0::IComponent>& c) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001264 status = static_cast<c2_status_t>(s);
1265 if (status != C2_OK) {
1266 return;
1267 }
1268 *component = std::make_shared<Codec2Client::Component>(c);
1269 hidlListener->component = *component;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001270 });
Sungtak Lee8577dab2021-03-12 02:25:50 -08001271 } else {
1272 status = C2_CORRUPTED;
1273 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001274 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001275 LOG(ERROR) << "createComponent(" << name.c_str()
1276 << ") -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001277 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001278 } else if (status != C2_OK) {
Pawin Vongmasa9aac3042020-03-05 02:30:32 -08001279 if (status == C2_NOT_FOUND) {
1280 LOG(VERBOSE) << "createComponent(" << name.c_str()
1281 << ") -- component not found.";
1282 } else {
1283 LOG(ERROR) << "createComponent(" << name.c_str()
1284 << ") -- call failed: " << status << ".";
1285 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001286 return status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001287 } else if (!*component) {
1288 LOG(ERROR) << "createComponent(" << name.c_str()
1289 << ") -- null component.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001290 return C2_CORRUPTED;
1291 }
1292
1293 status = (*component)->setDeathListener(*component, listener);
1294 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001295 LOG(ERROR) << "createComponent(" << name.c_str()
1296 << ") -- failed to set up death listener: "
1297 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001298 }
1299
Wonsik Kime8e98152022-12-16 16:04:17 -08001300 (*component)->mHidlBufferPoolSender->setReceiver(mHidlHostPoolManager);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001301 return status;
1302}
1303
1304c2_status_t Codec2Client::createInterface(
1305 const C2String& name,
1306 std::shared_ptr<Codec2Client::Interface>* const interface) {
Wonsik Kime8e98152022-12-16 16:04:17 -08001307 if (mAidlBase) {
1308 std::shared_ptr<c2_aidl::IComponentInterface> aidlInterface;
1309 ::ndk::ScopedAStatus transStatus = mAidlBase->createInterface(
1310 name,
1311 &aidlInterface);
1312 c2_status_t status = GetC2Status(transStatus, "createInterface");
1313 if (status != C2_OK) {
1314 return status;
1315 } else if (!aidlInterface) {
1316 LOG(ERROR) << "createInterface(" << name.c_str()
1317 << ") -- null interface.";
1318 return C2_CORRUPTED;
1319 }
1320 interface->reset(new Codec2Client::Interface(aidlInterface));
1321 return C2_OK;
1322 }
1323
Pawin Vongmasa36653902018-11-15 00:10:25 -08001324 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08001325 Return<void> transStatus = mHidlBase1_0->createInterface(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001326 name,
1327 [&status, interface](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001328 c2_hidl::Status s,
1329 const sp<c2_hidl::IComponentInterface>& i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001330 status = static_cast<c2_status_t>(s);
1331 if (status != C2_OK) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001332 return;
1333 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001334 *interface = std::make_shared<Interface>(i);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001335 });
1336 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001337 LOG(ERROR) << "createInterface(" << name.c_str()
1338 << ") -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001339 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001340 } else if (status != C2_OK) {
Pawin Vongmasa9aac3042020-03-05 02:30:32 -08001341 if (status == C2_NOT_FOUND) {
1342 LOG(VERBOSE) << "createInterface(" << name.c_str()
1343 << ") -- component not found.";
1344 } else {
1345 LOG(ERROR) << "createInterface(" << name.c_str()
1346 << ") -- call failed: " << status << ".";
1347 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001348 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001349 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001350
Pawin Vongmasa36653902018-11-15 00:10:25 -08001351 return status;
1352}
1353
1354c2_status_t Codec2Client::createInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001355 std::shared_ptr<InputSurface>* const inputSurface) {
Wonsik Kime8e98152022-12-16 16:04:17 -08001356 if (mAidlBase) {
1357 // FIXME
1358 return C2_OMITTED;
1359 }
1360
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001361 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08001362 Return<void> transStatus = mHidlBase1_0->createInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001363 [&status, inputSurface](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001364 c2_hidl::Status s,
1365 const sp<c2_hidl::IInputSurface>& i) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001366 status = static_cast<c2_status_t>(s);
1367 if (status != C2_OK) {
1368 return;
1369 }
1370 *inputSurface = std::make_shared<InputSurface>(i);
1371 });
1372 if (!transStatus.isOk()) {
1373 LOG(ERROR) << "createInputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001374 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001375 } else if (status != C2_OK) {
1376 LOG(DEBUG) << "createInputSurface -- call failed: "
1377 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001378 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001379 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001380}
1381
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001382std::vector<C2Component::Traits> const& Codec2Client::listComponents() const {
1383 return Cache::List()[mServiceIndex].getTraits();
1384}
1385
1386std::vector<C2Component::Traits> Codec2Client::_listComponents(
1387 bool* success) const {
1388 std::vector<C2Component::Traits> traits;
1389 std::string const& serviceName = getServiceName();
Wonsik Kime8e98152022-12-16 16:04:17 -08001390
1391 if (mAidlBase) {
1392 std::vector<c2_aidl::IComponentStore::ComponentTraits> aidlTraits;
1393 ::ndk::ScopedAStatus transStatus = mAidlBase->listComponents(&aidlTraits);
1394 if (!transStatus.isOk()) {
1395 LOG(ERROR) << "_listComponents -- transaction failed.";
1396 *success = false;
1397 } else {
1398 traits.resize(aidlTraits.size());
1399 *success = true;
1400 for (size_t i = 0; i < aidlTraits.size(); ++i) {
1401 if (!c2_aidl::utils::FromAidl(&traits[i], aidlTraits[i])) {
1402 LOG(ERROR) << "_listComponents -- corrupted output.";
1403 *success = false;
1404 traits.clear();
1405 break;
1406 }
1407 traits[i].owner = serviceName;
1408 }
1409 }
1410 return traits;
1411 }
1412 Return<void> transStatus = mHidlBase1_0->listComponents(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001413 [&traits, &serviceName](c2_hidl::Status s,
1414 const hidl_vec<c2_hidl::IComponentStore::ComponentTraits>& t) {
1415 if (s != c2_hidl::Status::OK) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001416 LOG(DEBUG) << "_listComponents -- call failed: "
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001417 << static_cast<c2_status_t>(s) << ".";
1418 return;
1419 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001420 traits.resize(t.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001421 for (size_t i = 0; i < t.size(); ++i) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001422 if (!c2_hidl::utils::objcpy(&traits[i], t[i])) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001423 LOG(ERROR) << "_listComponents -- corrupted output.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001424 return;
1425 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001426 traits[i].owner = serviceName;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001427 }
1428 });
1429 if (!transStatus.isOk()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001430 LOG(ERROR) << "_listComponents -- transaction failed.";
1431 *success = false;
1432 } else {
1433 *success = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001434 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001435 return traits;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001436}
1437
1438c2_status_t Codec2Client::copyBuffer(
1439 const std::shared_ptr<C2Buffer>& src,
1440 const std::shared_ptr<C2Buffer>& dst) {
1441 // TODO: Implement?
1442 (void)src;
1443 (void)dst;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001444 LOG(ERROR) << "copyBuffer not implemented";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001445 return C2_OMITTED;
1446}
1447
Wonsik Kime8e98152022-12-16 16:04:17 -08001448std::shared_ptr<C2ParamReflector> Codec2Client::getParamReflector() {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001449 // TODO: this is not meant to be exposed as C2ParamReflector on the client side; instead, it
1450 // should reflect the HAL API.
Wonsik Kime8e98152022-12-16 16:04:17 -08001451 struct HidlSimpleParamReflector : public C2ParamReflector {
1452 std::unique_ptr<C2StructDescriptor> describe(
1453 C2Param::CoreIndex coreIndex) const override {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001454 hidl_vec<c2_hidl::ParamIndex> indices(1);
1455 indices[0] = static_cast<c2_hidl::ParamIndex>(coreIndex.coreIndex());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001456 std::unique_ptr<C2StructDescriptor> descriptor;
1457 Return<void> transStatus = mBase->getStructDescriptors(
1458 indices,
1459 [&descriptor](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001460 c2_hidl::Status s,
1461 const hidl_vec<c2_hidl::StructDescriptor>& sd) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001462 c2_status_t status = static_cast<c2_status_t>(s);
1463 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001464 LOG(DEBUG) << "SimpleParamReflector -- "
1465 "getStructDescriptors() failed: "
1466 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001467 descriptor.reset();
1468 return;
1469 }
1470 if (sd.size() != 1) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001471 LOG(DEBUG) << "SimpleParamReflector -- "
1472 "getStructDescriptors() "
1473 "returned vector of size "
1474 << sd.size() << ". "
1475 "It should be 1.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001476 descriptor.reset();
1477 return;
1478 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001479 if (!c2_hidl::utils::objcpy(&descriptor, sd[0])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001480 LOG(DEBUG) << "SimpleParamReflector -- "
1481 "getStructDescriptors() returned "
1482 "corrupted data.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001483 descriptor.reset();
1484 return;
1485 }
1486 });
Wonsik Kim492fecd2020-11-19 11:14:11 -08001487 if (!transStatus.isOk()) {
1488 LOG(DEBUG) << "SimpleParamReflector -- transaction failed: "
1489 << transStatus.description();
1490 descriptor.reset();
1491 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001492 return descriptor;
1493 }
1494
Wonsik Kime8e98152022-12-16 16:04:17 -08001495 HidlSimpleParamReflector(sp<HidlBase> base)
Pawin Vongmasa36653902018-11-15 00:10:25 -08001496 : mBase(base) { }
1497
Wonsik Kime8e98152022-12-16 16:04:17 -08001498 sp<HidlBase> mBase;
1499 };
1500 struct AidlSimpleParamReflector : public C2ParamReflector {
1501 std::unique_ptr<C2StructDescriptor> describe(
1502 C2Param::CoreIndex coreIndex) const override {
1503 std::vector<c2_aidl::StructDescriptor> aidlDesc;
1504 std::unique_ptr<C2StructDescriptor> descriptor;
1505 ::ndk::ScopedAStatus transStatus = mBase->getStructDescriptors(
1506 {int32_t(coreIndex.coreIndex())},
1507 &aidlDesc);
1508 c2_status_t status = GetC2Status(transStatus, "describe");
1509 if (status != C2_OK) {
1510 descriptor.reset();
1511 } else if (!c2_aidl::utils::FromAidl(&descriptor, aidlDesc[0])) {
1512 LOG(ERROR) << "describe -- conversion failed.";
1513 descriptor.reset();
1514 }
1515 return descriptor;
1516 }
1517
1518 AidlSimpleParamReflector(const std::shared_ptr<AidlBase> &base)
1519 : mBase(base) { }
1520
1521 std::shared_ptr<AidlBase> mBase;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001522 };
1523
Wonsik Kime8e98152022-12-16 16:04:17 -08001524 if (mAidlBase) {
1525 return std::make_shared<AidlSimpleParamReflector>(mAidlBase);
1526 }
1527 return std::make_shared<HidlSimpleParamReflector>(mHidlBase1_0);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001528};
1529
Wonsik Kime8e98152022-12-16 16:04:17 -08001530std::vector<std::string> Codec2Client::CacheServiceNames() {
1531 std::vector<std::string> names;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001532
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001533 if (c2_aidl::utils::IsSelected()) {
Wonsik Kim138db0d2023-11-02 16:02:01 -07001534 if (__builtin_available(android __ANDROID_API_S__, *)) {
1535 // Get AIDL service names
1536 AServiceManager_forEachDeclaredInstance(
1537 AidlBase::descriptor, &names, [](const char *name, void *context) {
1538 std::vector<std::string> *names = (std::vector<std::string> *)context;
1539 names->emplace_back(name);
1540 });
1541 } else {
1542 LOG(FATAL) << "C2 AIDL cannot be selected on Android version older than 35";
1543 }
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001544 } else {
1545 // Get HIDL service names
1546 using ::android::hardware::media::c2::V1_0::IComponentStore;
1547 using ::android::hidl::manager::V1_2::IServiceManager;
1548 while (true) {
1549 sp<IServiceManager> serviceManager = IServiceManager::getService();
1550 CHECK(serviceManager) << "Hardware service manager is not running.";
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001551
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001552 Return<void> transResult;
1553 transResult = serviceManager->listManifestByInterface(
1554 IComponentStore::descriptor,
1555 [&names](
1556 hidl_vec<hidl_string> const& instanceNames) {
1557 names.insert(names.end(), instanceNames.begin(), instanceNames.end());
1558 });
1559 if (transResult.isOk()) {
1560 break;
1561 }
1562 LOG(ERROR) << "Could not retrieve the list of service instances of "
1563 << IComponentStore::descriptor
1564 << ". Retrying...";
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001565 }
Wonsik Kime8e98152022-12-16 16:04:17 -08001566 }
1567 // Sort service names in each category.
1568 std::stable_sort(
1569 names.begin(), names.end(),
1570 [](const std::string &a, const std::string &b) {
1571 // First compare by prefix: default -> vendor -> {everything else}
1572 constexpr int DEFAULT = 1;
1573 constexpr int VENDOR = 2;
1574 constexpr int OTHER = 3;
1575 int aPrefix = ((a.compare(0, 7, "default") == 0) ? DEFAULT :
1576 (a.compare(0, 6, "vendor") == 0) ? VENDOR :
1577 OTHER);
1578 int bPrefix = ((b.compare(0, 7, "default") == 0) ? DEFAULT :
1579 (b.compare(0, 6, "vendor") == 0) ? VENDOR :
1580 OTHER);
1581 if (aPrefix != bPrefix) {
1582 return aPrefix < bPrefix;
1583 }
1584 // If the prefix is the same, compare alphabetically
1585 return a < b;
1586 });
1587
1588 // Summarize to logcat.
1589 if (names.empty()) {
1590 LOG(INFO) << "No Codec2 services declared in the manifest.";
1591 } else {
1592 std::stringstream stringOutput;
1593 stringOutput << "Available Codec2 services:";
1594 for (std::string const& name : names) {
1595 stringOutput << " \"" << name << "\"";
1596 }
1597 LOG(INFO) << stringOutput.str();
1598 }
1599
1600 return names;
1601}
1602
1603std::vector<std::string> const& Codec2Client::GetServiceNames() {
1604 static std::vector<std::string> sServiceNames = CacheServiceNames();
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001605 return sServiceNames;
1606}
1607
Pawin Vongmasa36653902018-11-15 00:10:25 -08001608std::shared_ptr<Codec2Client> Codec2Client::CreateFromService(
Pawin Vongmasa83d2c552020-03-05 04:36:08 -08001609 const char* name,
1610 bool setAsPreferredCodec2ComponentStore) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001611 size_t index = getServiceIndex(name);
Pawin Vongmasa83d2c552020-03-05 04:36:08 -08001612 if (index == GetServiceNames().size()) {
1613 if (setAsPreferredCodec2ComponentStore) {
1614 LOG(WARNING) << "CreateFromService(" << name
1615 << ") -- preferred C2ComponentStore not set.";
1616 }
1617 return nullptr;
1618 }
1619 std::shared_ptr<Codec2Client> client = _CreateFromIndex(index);
1620 if (setAsPreferredCodec2ComponentStore) {
1621 SetPreferredCodec2ComponentStore(
1622 std::make_shared<Client2Store>(client));
1623 LOG(INFO) << "CreateFromService(" << name
1624 << ") -- service set as preferred C2ComponentStore.";
1625 }
1626 return client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001627}
1628
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001629std::vector<std::shared_ptr<Codec2Client>> Codec2Client::
1630 CreateFromAllServices() {
1631 std::vector<std::shared_ptr<Codec2Client>> clients(
1632 GetServiceNames().size());
1633 for (size_t i = GetServiceNames().size(); i > 0; ) {
1634 --i;
1635 clients[i] = _CreateFromIndex(i);
1636 }
1637 return clients;
1638}
1639
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001640std::shared_ptr<Codec2Client> Codec2Client::_CreateFromIndex(size_t index) {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001641 std::string const& name = GetServiceNames()[index];
Pawin Vongmasa9aac3042020-03-05 02:30:32 -08001642 LOG(VERBOSE) << "Creating a Codec2 client to service \"" << name << "\"";
Wonsik Kime8e98152022-12-16 16:04:17 -08001643
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001644 if (c2_aidl::utils::IsSelected()) {
Wonsik Kim138db0d2023-11-02 16:02:01 -07001645 if (__builtin_available(android __ANDROID_API_S__, *)) {
1646 std::string instanceName =
1647 ::android::base::StringPrintf("%s/%s", AidlBase::descriptor, name.c_str());
1648 if (AServiceManager_isDeclared(instanceName.c_str())) {
1649 std::shared_ptr<AidlBase> baseStore = AidlBase::fromBinder(
1650 ::ndk::SpAIBinder(AServiceManager_waitForService(instanceName.c_str())));
1651 CHECK(baseStore) << "Codec2 AIDL service \"" << name << "\""
1652 " inaccessible for unknown reasons.";
1653 LOG(VERBOSE) << "Client to Codec2 AIDL service \"" << name << "\" created";
1654 std::shared_ptr<c2_aidl::IConfigurable> configurable;
1655 ::ndk::ScopedAStatus transStatus = baseStore->getConfigurable(&configurable);
1656 CHECK(transStatus.isOk()) << "Codec2 AIDL service \"" << name << "\""
1657 "does not have IConfigurable.";
1658 return std::make_shared<Codec2Client>(baseStore, configurable, index);
1659 } else {
1660 LOG(ERROR) << "Codec2 AIDL service \"" << name << "\" is not declared";
1661 }
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001662 } else {
Wonsik Kim138db0d2023-11-02 16:02:01 -07001663 LOG(FATAL) << "C2 AIDL cannot be selected on Android version older than 35";
Wonsik Kime8e98152022-12-16 16:04:17 -08001664 }
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001665 } else {
1666 std::string instanceName = "android.hardware.media.c2/" + name;
1667 sp<HidlBase> baseStore = HidlBase::getService(name);
1668 CHECK(baseStore) << "Codec2 service \"" << name << "\""
1669 " inaccessible for unknown reasons.";
1670 LOG(VERBOSE) << "Client to Codec2 service \"" << name << "\" created";
1671 Return<sp<c2_hidl::IConfigurable>> transResult = baseStore->getConfigurable();
1672 CHECK(transResult.isOk()) << "Codec2 service \"" << name << "\""
1673 "does not have IConfigurable.";
1674 sp<c2_hidl::IConfigurable> configurable =
1675 static_cast<sp<c2_hidl::IConfigurable>>(transResult);
1676 return std::make_shared<Codec2Client>(baseStore, configurable, index);
Wonsik Kime8e98152022-12-16 16:04:17 -08001677 }
Wonsik Kimb613d4b2023-09-01 14:01:36 -07001678 return nullptr;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001679}
1680
1681c2_status_t Codec2Client::ForAllServices(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001682 const std::string &key,
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001683 size_t numberOfAttempts,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001684 std::function<c2_status_t(const std::shared_ptr<Codec2Client>&)>
1685 predicate) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001686 c2_status_t status = C2_NO_INIT; // no IComponentStores present
1687
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001688 // Cache the mapping key -> index of Codec2Client in Cache::List().
Pawin Vongmasa36653902018-11-15 00:10:25 -08001689 static std::mutex key2IndexMutex;
1690 static std::map<std::string, size_t> key2Index;
1691
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001692 // By default try all stores. However, try the last known client first. If
1693 // the last known client fails, retry once. We do this by pushing the last
1694 // known client in front of the list of all clients.
Pawin Vongmasa36653902018-11-15 00:10:25 -08001695 std::deque<size_t> indices;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001696 for (size_t index = Cache::List().size(); index > 0; ) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001697 indices.push_front(--index);
1698 }
1699
1700 bool wasMapped = false;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001701 {
1702 std::scoped_lock lock{key2IndexMutex};
1703 auto it = key2Index.find(key);
1704 if (it != key2Index.end()) {
1705 indices.push_front(it->second);
1706 wasMapped = true;
1707 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001708 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001709
1710 for (size_t index : indices) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001711 Cache& cache = Cache::List()[index];
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001712 for (size_t tries = numberOfAttempts; tries > 0; --tries) {
1713 std::shared_ptr<Codec2Client> client{cache.getClient()};
Pawin Vongmasa36653902018-11-15 00:10:25 -08001714 status = predicate(client);
1715 if (status == C2_OK) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001716 std::scoped_lock lock{key2IndexMutex};
Pawin Vongmasa36653902018-11-15 00:10:25 -08001717 key2Index[key] = index; // update last known client index
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001718 return C2_OK;
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001719 } else if (status == C2_NO_MEMORY) {
1720 return C2_NO_MEMORY;
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001721 } else if (status == C2_TRANSACTION_FAILED) {
1722 LOG(WARNING) << "\"" << key << "\" failed for service \""
1723 << client->getName()
1724 << "\" due to transaction failure. "
1725 << "(Service may have crashed.)"
1726 << (tries > 1 ? " Retrying..." : "");
1727 cache.invalidate();
1728 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001729 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001730 if (wasMapped) {
1731 LOG(INFO) << "\"" << key << "\" became invalid in service \""
1732 << client->getName() << "\". Retrying...";
1733 wasMapped = false;
1734 }
1735 break;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001736 }
1737 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001738 return status; // return the last status from a valid client
Pawin Vongmasa36653902018-11-15 00:10:25 -08001739}
1740
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001741c2_status_t Codec2Client::CreateComponentByName(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001742 const char* componentName,
1743 const std::shared_ptr<Listener>& listener,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001744 std::shared_ptr<Component>* component,
Pawin Vongmasa23c90c82019-09-03 00:44:42 -07001745 std::shared_ptr<Codec2Client>* owner,
1746 size_t numberOfAttempts) {
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001747 std::string key{"create:"};
1748 key.append(componentName);
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001749 c2_status_t status = ForAllServices(
1750 key,
1751 numberOfAttempts,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001752 [owner, component, componentName, &listener](
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001753 const std::shared_ptr<Codec2Client> &client)
1754 -> c2_status_t {
1755 c2_status_t status = client->createComponent(componentName,
1756 listener,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001757 component);
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001758 if (status == C2_OK) {
1759 if (owner) {
1760 *owner = client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001761 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001762 } else if (status != C2_NOT_FOUND) {
1763 LOG(DEBUG) << "IComponentStore("
1764 << client->getServiceName()
1765 << ")::createComponent(\"" << componentName
1766 << "\") returned status = "
1767 << status << ".";
1768 }
1769 return status;
1770 });
1771 if (status != C2_OK) {
1772 LOG(DEBUG) << "Failed to create component \"" << componentName
1773 << "\" from all known services. "
1774 "Last returned status = " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001775 }
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001776 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001777}
1778
Wonsik Kime8e98152022-12-16 16:04:17 -08001779std::shared_ptr<Codec2Client::Interface> Codec2Client::CreateInterfaceByName(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001780 const char* interfaceName,
Pawin Vongmasa23c90c82019-09-03 00:44:42 -07001781 std::shared_ptr<Codec2Client>* owner,
1782 size_t numberOfAttempts) {
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001783 std::string key{"create:"};
1784 key.append(interfaceName);
1785 std::shared_ptr<Interface> interface;
1786 c2_status_t status = ForAllServices(
1787 key,
1788 numberOfAttempts,
1789 [owner, &interface, interfaceName](
1790 const std::shared_ptr<Codec2Client> &client)
1791 -> c2_status_t {
1792 c2_status_t status = client->createInterface(interfaceName,
1793 &interface);
1794 if (status == C2_OK) {
1795 if (owner) {
1796 *owner = client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001797 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001798 } else if (status != C2_NOT_FOUND) {
1799 LOG(DEBUG) << "IComponentStore("
1800 << client->getServiceName()
1801 << ")::createInterface(\"" << interfaceName
1802 << "\") returned status = "
1803 << status << ".";
1804 }
1805 return status;
1806 });
1807 if (status != C2_OK) {
1808 LOG(DEBUG) << "Failed to create interface \"" << interfaceName
1809 << "\" from all known services. "
1810 "Last returned status = " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001811 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001812 return interface;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001813}
1814
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001815std::vector<C2Component::Traits> const& Codec2Client::ListComponents() {
1816 static std::vector<C2Component::Traits> sList{[]() {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001817 std::vector<C2Component::Traits> list;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001818 for (Cache& cache : Cache::List()) {
1819 std::vector<C2Component::Traits> const& traits = cache.getTraits();
1820 list.insert(list.end(), traits.begin(), traits.end());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001821 }
1822 return list;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001823 }()};
1824 return sList;
1825}
Pawin Vongmasa36653902018-11-15 00:10:25 -08001826
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001827std::shared_ptr<Codec2Client::InputSurface> Codec2Client::CreateInputSurface(
1828 char const* serviceName) {
Pawin Vongmasa18588322019-05-18 01:52:13 -07001829 int32_t inputSurfaceSetting = ::android::base::GetIntProperty(
1830 "debug.stagefright.c2inputsurface", int32_t(0));
1831 if (inputSurfaceSetting <= 0) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001832 return nullptr;
1833 }
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001834 size_t index = GetServiceNames().size();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001835 if (serviceName) {
1836 index = getServiceIndex(serviceName);
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001837 if (index == GetServiceNames().size()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001838 LOG(DEBUG) << "CreateInputSurface -- invalid service name: \""
1839 << serviceName << "\"";
1840 }
1841 }
1842
1843 std::shared_ptr<Codec2Client::InputSurface> inputSurface;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001844 if (index != GetServiceNames().size()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001845 std::shared_ptr<Codec2Client> client = Cache::List()[index].getClient();
1846 if (client->createInputSurface(&inputSurface) == C2_OK) {
1847 return inputSurface;
1848 }
1849 }
1850 LOG(INFO) << "CreateInputSurface -- attempting to create an input surface "
1851 "from all services...";
1852 for (Cache& cache : Cache::List()) {
1853 std::shared_ptr<Codec2Client> client = cache.getClient();
1854 if (client->createInputSurface(&inputSurface) == C2_OK) {
1855 LOG(INFO) << "CreateInputSurface -- input surface obtained from "
1856 "service \"" << client->getServiceName() << "\"";
1857 return inputSurface;
1858 }
1859 }
1860 LOG(WARNING) << "CreateInputSurface -- failed to create an input surface "
1861 "from all services";
1862 return nullptr;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001863}
1864
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001865// Codec2Client::Interface
Wonsik Kime8e98152022-12-16 16:04:17 -08001866Codec2Client::Interface::Interface(const sp<HidlBase>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001867 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001868 [base]() -> sp<c2_hidl::IConfigurable> {
1869 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001870 base->getConfigurable();
1871 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001872 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001873 nullptr;
1874 }()
1875 },
Wonsik Kime8e98152022-12-16 16:04:17 -08001876 mHidlBase{base} {
1877}
1878
1879Codec2Client::Interface::Interface(const std::shared_ptr<AidlBase>& base)
1880 : Configurable{
1881 [base]() -> std::shared_ptr<c2_aidl::IConfigurable> {
1882 std::shared_ptr<c2_aidl::IConfigurable> aidlConfigurable;
1883 ::ndk::ScopedAStatus transStatus =
1884 base->getConfigurable(&aidlConfigurable);
1885 return transStatus.isOk() ? aidlConfigurable : nullptr;
1886 }()
1887 },
1888 mAidlBase{base} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001889}
1890
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001891// Codec2Client::Component
Wonsik Kime8e98152022-12-16 16:04:17 -08001892
1893class Codec2Client::Component::AidlDeathManager {
1894public:
1895 AidlDeathManager()
1896 : mSeq(0),
1897 mDeathRecipient(AIBinder_DeathRecipient_new(OnBinderDied)) {
1898 }
1899
1900 ~AidlDeathManager() = default;
1901
1902 bool linkToDeath(
1903 const std::shared_ptr<Component> &comp,
1904 const std::shared_ptr<Listener> &listener,
1905 size_t *seqPtr) {
1906 std::unique_lock lock(mMutex);
1907 size_t seq = mSeq++;
1908 if (!mMap.try_emplace(seq, comp, listener).second) {
1909 return false;
1910 }
1911 if (STATUS_OK != AIBinder_linkToDeath(
1912 comp->mAidlBase->asBinder().get(), mDeathRecipient.get(), (void *)seq)) {
1913 mMap.erase(seq);
1914 return false;
1915 }
1916 *seqPtr = seq;
1917 return true;
1918 }
1919
1920 void unlinkToDeath(size_t seq, const std::shared_ptr<AidlBase> &base) {
1921 std::unique_lock lock(mMutex);
1922 AIBinder_unlinkToDeath(base->asBinder().get(), mDeathRecipient.get(), (void *)seq);
1923 mMap.erase(seq);
1924 }
1925
1926private:
1927 std::mutex mMutex;
1928 size_t mSeq;
1929 typedef std::tuple<std::weak_ptr<Component>, std::weak_ptr<Listener>> Context;
1930 std::map<size_t, Context> mMap;
1931 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
1932
1933 bool extractContext(size_t seq, Context *context) {
1934 std::unique_lock lock(mMutex);
1935 auto node = mMap.extract(seq);
1936 if (!node) {
1937 return false;
1938 }
1939 *context = node.mapped();
1940 return true;
1941 }
1942
1943 static void OnBinderDied(void *cookie) {
1944 size_t seq = size_t(cookie);
1945 Context context;
1946 if (!Component::GetAidlDeathManager()->extractContext(seq, &context)) {
1947 return;
1948 }
1949 std::weak_ptr<Component> weakComponent;
1950 std::weak_ptr<Listener> weakListener;
1951 std::tie(weakComponent, weakListener) = context;
1952 if (std::shared_ptr<Listener> listener = weakListener.lock()) {
1953 listener->onDeath(weakComponent);
1954 } else {
1955 LOG(DEBUG) << "onDeath -- listener died.";
1956 }
1957 }
1958};
1959
1960Codec2Client::Component::Component(const sp<HidlBase>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001961 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001962 [base]() -> sp<c2_hidl::IConfigurable> {
1963 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001964 base->getInterface();
1965 if (!transResult1.isOk()) {
1966 return nullptr;
1967 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001968 Return<sp<c2_hidl::IConfigurable>> transResult2 =
1969 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001970 getConfigurable();
1971 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001972 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001973 nullptr;
1974 }()
1975 },
Wonsik Kime8e98152022-12-16 16:04:17 -08001976 mHidlBase1_0{base},
1977 mHidlBase1_1{HidlBase1_1::castFrom(base)},
1978 mHidlBase1_2{HidlBase1_2::castFrom(base)},
1979 mHidlBufferPoolSender{std::make_unique<HidlBufferPoolSender>()},
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001980 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
1981}
1982
Wonsik Kime8e98152022-12-16 16:04:17 -08001983Codec2Client::Component::Component(const sp<HidlBase1_1>& base)
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001984 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001985 [base]() -> sp<c2_hidl::IConfigurable> {
1986 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001987 base->getInterface();
1988 if (!transResult1.isOk()) {
1989 return nullptr;
1990 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001991 Return<sp<c2_hidl::IConfigurable>> transResult2 =
1992 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001993 getConfigurable();
1994 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001995 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001996 nullptr;
1997 }()
1998 },
Wonsik Kime8e98152022-12-16 16:04:17 -08001999 mHidlBase1_0{base},
2000 mHidlBase1_1{base},
2001 mHidlBase1_2{HidlBase1_2::castFrom(base)},
2002 mHidlBufferPoolSender{std::make_unique<HidlBufferPoolSender>()},
Sungtak Lee8577dab2021-03-12 02:25:50 -08002003 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
2004}
2005
Wonsik Kime8e98152022-12-16 16:04:17 -08002006Codec2Client::Component::Component(const sp<HidlBase1_2>& base)
Sungtak Lee8577dab2021-03-12 02:25:50 -08002007 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002008 [base]() -> sp<c2_hidl::IConfigurable> {
2009 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Sungtak Lee8577dab2021-03-12 02:25:50 -08002010 base->getInterface();
2011 if (!transResult1.isOk()) {
2012 return nullptr;
2013 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002014 Return<sp<c2_hidl::IConfigurable>> transResult2 =
2015 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Sungtak Lee8577dab2021-03-12 02:25:50 -08002016 getConfigurable();
2017 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002018 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Sungtak Lee8577dab2021-03-12 02:25:50 -08002019 nullptr;
2020 }()
2021 },
Wonsik Kime8e98152022-12-16 16:04:17 -08002022 mHidlBase1_0{base},
2023 mHidlBase1_1{base},
2024 mHidlBase1_2{base},
2025 mHidlBufferPoolSender{std::make_unique<HidlBufferPoolSender>()},
2026 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
2027}
2028
2029Codec2Client::Component::Component(const std::shared_ptr<AidlBase> &base)
2030 : Configurable{
2031 [base]() -> std::shared_ptr<c2_aidl::IConfigurable> {
2032 std::shared_ptr<c2_aidl::IComponentInterface> aidlIntf;
2033 ::ndk::ScopedAStatus transStatus = base->getInterface(&aidlIntf);
2034 if (!transStatus.isOk()) {
2035 return nullptr;
2036 }
2037 std::shared_ptr<c2_aidl::IConfigurable> aidlConfigurable;
2038 transStatus = aidlIntf->getConfigurable(&aidlConfigurable);
2039 return transStatus.isOk() ? aidlConfigurable : nullptr;
2040 }()
2041 },
2042 mAidlBase{base},
2043 mAidlBufferPoolSender{std::make_unique<AidlBufferPoolSender>()},
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002044 mGraphicBufferAllocators{std::make_unique<GraphicBufferAllocators>()} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002045}
2046
2047Codec2Client::Component::~Component() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002048 if (mAidlDeathSeq) {
2049 GetAidlDeathManager()->unlinkToDeath(*mAidlDeathSeq, mAidlBase);
2050 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002051}
2052
2053c2_status_t Codec2Client::Component::createBlockPool(
2054 C2Allocator::id_t id,
2055 C2BlockPool::local_id_t* blockPoolId,
2056 std::shared_ptr<Codec2Client::Configurable>* configurable) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002057 if (mAidlBase) {
2058 c2_aidl::IComponent::BlockPool aidlBlockPool;
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002059 c2_status_t status = C2_OK;
2060
2061 // TODO: Temporary mapping for the current CCodecBufferChannel.
2062 // Handle this properly and remove this temporary allocator mapping.
2063 id = id == C2PlatformAllocatorStore::BUFFERQUEUE ?
2064 C2PlatformAllocatorStore::IGBA : id;
2065
2066 if (id == C2PlatformAllocatorStore::IGBA) {
2067 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2068 mGraphicBufferAllocators->create();
2069 ::ndk::ScopedFileDescriptor waitableFd;
2070 ::ndk::ScopedAStatus ret = gba->getWaitableFd(&waitableFd);
2071 status = GetC2Status(ret, "Gba::getWaitableFd");
2072 if (status != C2_OK) {
2073 return status;
2074 }
2075 c2_aidl::IComponent::BlockPoolAllocator allocator;
2076 allocator.set<c2_aidl::IComponent::BlockPoolAllocator::allocator>();
2077 allocator.get<c2_aidl::IComponent::BlockPoolAllocator::allocator>().igba =
2078 c2_aidl::IGraphicBufferAllocator::fromBinder(gba->asBinder());
2079 allocator.get<c2_aidl::IComponent::BlockPoolAllocator::allocator>().waitableFd =
2080 std::move(waitableFd);
2081 ::ndk::ScopedAStatus transStatus = mAidlBase->createBlockPool(
2082 allocator, &aidlBlockPool);
2083 status = GetC2Status(transStatus, "createBlockPool");
2084 if (status != C2_OK) {
2085 return status;
2086 }
2087 mGraphicBufferAllocators->setCurrentId(aidlBlockPool.blockPoolId);
2088 } else {
2089 ::ndk::ScopedAStatus transStatus = mAidlBase->createBlockPool(
2090 static_cast<int32_t>(id), &aidlBlockPool);
2091 status = GetC2Status(transStatus, "createBlockPool");
2092 if (status != C2_OK) {
2093 return status;
2094 }
Wonsik Kime8e98152022-12-16 16:04:17 -08002095 }
2096 *blockPoolId = aidlBlockPool.blockPoolId;
2097 *configurable = std::make_shared<Configurable>(aidlBlockPool.configurable);
2098 return C2_OK;
2099 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002100 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08002101 Return<void> transStatus = mHidlBase1_0->createBlockPool(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002102 static_cast<uint32_t>(id),
2103 [&status, blockPoolId, configurable](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002104 c2_hidl::Status s,
Pawin Vongmasa36653902018-11-15 00:10:25 -08002105 uint64_t pId,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002106 const sp<c2_hidl::IConfigurable>& c) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002107 status = static_cast<c2_status_t>(s);
2108 configurable->reset();
2109 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002110 LOG(DEBUG) << "createBlockPool -- call failed: "
2111 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002112 return;
2113 }
2114 *blockPoolId = static_cast<C2BlockPool::local_id_t>(pId);
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002115 *configurable = std::make_shared<Configurable>(c);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002116 });
2117 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002118 LOG(ERROR) << "createBlockPool -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002119 return C2_TRANSACTION_FAILED;
2120 }
2121 return status;
2122}
2123
2124c2_status_t Codec2Client::Component::destroyBlockPool(
2125 C2BlockPool::local_id_t localId) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002126 if (mAidlBase) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002127 mGraphicBufferAllocators->remove(localId);
Wonsik Kime8e98152022-12-16 16:04:17 -08002128 ::ndk::ScopedAStatus transStatus = mAidlBase->destroyBlockPool(localId);
2129 return GetC2Status(transStatus, "destroyBlockPool");
2130 }
2131 Return<c2_hidl::Status> transResult = mHidlBase1_0->destroyBlockPool(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002132 static_cast<uint64_t>(localId));
2133 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002134 LOG(ERROR) << "destroyBlockPool -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002135 return C2_TRANSACTION_FAILED;
2136 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002137 return static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transResult));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002138}
2139
Wonsik Kimab34ed62019-01-31 15:28:46 -08002140void Codec2Client::Component::handleOnWorkDone(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002141 const std::list<std::unique_ptr<C2Work>> &workItems) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002142 if (mAidlBase) {
2143 holdIgbaBlocks(workItems);
2144 } else {
2145 // Output bufferqueue-based blocks' lifetime management
2146 mOutputBufferQueue->holdBufferQueueBlocks(workItems);
2147 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002148}
2149
2150c2_status_t Codec2Client::Component::queue(
2151 std::list<std::unique_ptr<C2Work>>* const items) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002152 if (mAidlBase) {
2153 c2_aidl::WorkBundle workBundle;
2154 if (!c2_aidl::utils::ToAidl(&workBundle, *items, mAidlBufferPoolSender.get())) {
2155 LOG(ERROR) << "queue -- bad input.";
2156 return C2_TRANSACTION_FAILED;
2157 }
2158 ::ndk::ScopedAStatus transStatus = mAidlBase->queue(workBundle);
2159 return GetC2Status(transStatus, "queue");
2160 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002161 c2_hidl::WorkBundle workBundle;
Wonsik Kime8e98152022-12-16 16:04:17 -08002162 if (!c2_hidl::utils::objcpy(&workBundle, *items, mHidlBufferPoolSender.get())) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002163 LOG(ERROR) << "queue -- bad input.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002164 return C2_TRANSACTION_FAILED;
2165 }
Wonsik Kime8e98152022-12-16 16:04:17 -08002166 Return<c2_hidl::Status> transStatus = mHidlBase1_0->queue(workBundle);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002167 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002168 LOG(ERROR) << "queue -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002169 return C2_TRANSACTION_FAILED;
2170 }
2171 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002172 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002173 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002174 LOG(DEBUG) << "queue -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002175 }
2176 return status;
2177}
2178
2179c2_status_t Codec2Client::Component::flush(
2180 C2Component::flush_mode_t mode,
2181 std::list<std::unique_ptr<C2Work>>* const flushedWork) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002182 (void)mode; // Flush mode isn't supported in HIDL/AIDL yet.
2183 c2_status_t status = C2_OK;
2184 if (mAidlBase) {
2185 c2_aidl::WorkBundle workBundle;
2186 ::ndk::ScopedAStatus transStatus = mAidlBase->flush(&workBundle);
2187 c2_status_t status = GetC2Status(transStatus, "flush");
2188 if (status != C2_OK) {
2189 return status;
2190 }
2191 if (!c2_aidl::utils::FromAidl(flushedWork, workBundle)) {
2192 LOG(DEBUG) << "flush -- flushedWork corrupted.";
2193 return C2_CORRUPTED;
2194 }
2195 } else {
2196 Return<void> transStatus = mHidlBase1_0->flush(
2197 [&status, flushedWork](
2198 c2_hidl::Status s, const c2_hidl::WorkBundle& wb) {
2199 status = static_cast<c2_status_t>(s);
2200 if (status != C2_OK) {
2201 LOG(DEBUG) << "flush -- call failed: " << status << ".";
2202 return;
2203 }
2204 if (!c2_hidl::utils::objcpy(flushedWork, wb)) {
2205 status = C2_CORRUPTED;
2206 } else {
2207 status = C2_OK;
2208 }
2209 });
2210 if (!transStatus.isOk()) {
2211 LOG(ERROR) << "flush -- transaction failed.";
2212 return C2_TRANSACTION_FAILED;
2213 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002214 }
2215
2216 // Indices of flushed work items.
2217 std::vector<uint64_t> flushedIndices;
2218 for (const std::unique_ptr<C2Work> &work : *flushedWork) {
2219 if (work) {
2220 if (work->worklets.empty()
2221 || !work->worklets.back()
2222 || (work->worklets.back()->output.flags &
2223 C2FrameData::FLAG_INCOMPLETE) == 0) {
2224 // input is complete
2225 flushedIndices.emplace_back(
2226 work->input.ordinal.frameIndex.peeku());
2227 }
2228 }
2229 }
2230
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002231 if (mAidlBase) {
2232 holdIgbaBlocks(*flushedWork);
2233 } else {
2234 // Output bufferqueue-based blocks' lifetime management
2235 mOutputBufferQueue->holdBufferQueueBlocks(*flushedWork);
2236 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002237
2238 return status;
2239}
2240
2241c2_status_t Codec2Client::Component::drain(C2Component::drain_mode_t mode) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002242 if (mAidlBase) {
2243 ::ndk::ScopedAStatus transStatus = mAidlBase->drain(
2244 mode == C2Component::DRAIN_COMPONENT_WITH_EOS);
2245 return GetC2Status(transStatus, "drain");
2246 }
2247 Return<c2_hidl::Status> transStatus = mHidlBase1_0->drain(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002248 mode == C2Component::DRAIN_COMPONENT_WITH_EOS);
2249 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002250 LOG(ERROR) << "drain -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002251 return C2_TRANSACTION_FAILED;
2252 }
2253 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002254 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002255 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002256 LOG(DEBUG) << "drain -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002257 }
2258 return status;
2259}
2260
2261c2_status_t Codec2Client::Component::start() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002262 if (mAidlBase) {
2263 ::ndk::ScopedAStatus transStatus = mAidlBase->start();
2264 return GetC2Status(transStatus, "start");
2265 }
2266 Return<c2_hidl::Status> transStatus = mHidlBase1_0->start();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002267 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002268 LOG(ERROR) << "start -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002269 return C2_TRANSACTION_FAILED;
2270 }
2271 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002272 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002273 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002274 LOG(DEBUG) << "start -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002275 }
2276 return status;
2277}
2278
2279c2_status_t Codec2Client::Component::stop() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002280 if (mAidlBase) {
2281 ::ndk::ScopedAStatus transStatus = mAidlBase->stop();
2282 return GetC2Status(transStatus, "stop");
2283 }
2284 Return<c2_hidl::Status> transStatus = mHidlBase1_0->stop();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002285 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002286 LOG(ERROR) << "stop -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002287 return C2_TRANSACTION_FAILED;
2288 }
2289 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002290 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002291 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002292 LOG(DEBUG) << "stop -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002293 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002294 return status;
2295}
2296
2297c2_status_t Codec2Client::Component::reset() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002298 if (mAidlBase) {
2299 ::ndk::ScopedAStatus transStatus = mAidlBase->reset();
2300 return GetC2Status(transStatus, "reset");
2301 }
2302 Return<c2_hidl::Status> transStatus = mHidlBase1_0->reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002303 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002304 LOG(ERROR) << "reset -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002305 return C2_TRANSACTION_FAILED;
2306 }
2307 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002308 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002309 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002310 LOG(DEBUG) << "reset -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002311 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002312 return status;
2313}
2314
2315c2_status_t Codec2Client::Component::release() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002316 if (mAidlBase) {
2317 ::ndk::ScopedAStatus transStatus = mAidlBase->release();
2318 return GetC2Status(transStatus, "release");
2319 }
2320 Return<c2_hidl::Status> transStatus = mHidlBase1_0->release();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002321 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002322 LOG(ERROR) << "release -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002323 return C2_TRANSACTION_FAILED;
2324 }
2325 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002326 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002327 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002328 LOG(DEBUG) << "release -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002329 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002330 return status;
2331}
2332
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002333c2_status_t Codec2Client::Component::configureVideoTunnel(
2334 uint32_t avSyncHwId,
2335 native_handle_t** sidebandHandle) {
2336 *sidebandHandle = nullptr;
Wonsik Kime8e98152022-12-16 16:04:17 -08002337 if (mAidlBase) {
2338 ::aidl::android::hardware::common::NativeHandle handle;
2339 ::ndk::ScopedAStatus transStatus = mAidlBase->configureVideoTunnel(avSyncHwId, &handle);
2340 c2_status_t status = GetC2Status(transStatus, "configureVideoTunnel");
2341 if (status != C2_OK) {
2342 return status;
2343 }
2344 if (isAidlNativeHandleEmpty(handle)) {
2345 LOG(DEBUG) << "configureVideoTunnel -- empty handle returned";
2346 } else {
2347 *sidebandHandle = dupFromAidl(handle);
2348 }
2349 return C2_OK;
2350 }
2351 if (!mHidlBase1_1) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002352 return C2_OMITTED;
2353 }
2354 c2_status_t status{};
Wonsik Kime8e98152022-12-16 16:04:17 -08002355 Return<void> transStatus = mHidlBase1_1->configureVideoTunnel(avSyncHwId,
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002356 [&status, sidebandHandle](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002357 c2_hidl::Status s, hardware::hidl_handle const& h) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002358 status = static_cast<c2_status_t>(s);
2359 if (h.getNativeHandle()) {
2360 *sidebandHandle = native_handle_clone(h.getNativeHandle());
2361 }
2362 });
2363 if (!transStatus.isOk()) {
2364 LOG(ERROR) << "configureVideoTunnel -- transaction failed.";
2365 return C2_TRANSACTION_FAILED;
2366 }
2367 return status;
2368}
2369
Pawin Vongmasa36653902018-11-15 00:10:25 -08002370c2_status_t Codec2Client::Component::setOutputSurface(
2371 C2BlockPool::local_id_t blockPoolId,
2372 const sp<IGraphicBufferProducer>& surface,
Sungtak Leedb14cba2021-04-10 00:50:23 -07002373 uint32_t generation,
2374 int maxDequeueCount) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002375 if (mAidlBase) {
2376 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2377 mGraphicBufferAllocators->current();
2378 if (!gba) {
2379 LOG(ERROR) << "setOutputSurface for AIDL -- "
2380 "GraphicBufferAllocator was not created.";
2381 return C2_CORRUPTED;
2382 }
2383 bool ret = gba->configure(surface, generation, maxDequeueCount);
2384 return ret ? C2_OK : C2_CORRUPTED;
2385 }
Sungtak Lee08515812019-06-05 11:16:32 -07002386 uint64_t bqId = 0;
2387 sp<IGraphicBufferProducer> nullIgbp;
2388 sp<HGraphicBufferProducer2> nullHgbp;
Pawin Vongmasa3866c7e2019-01-31 05:21:29 -08002389
Sungtak Lee08515812019-06-05 11:16:32 -07002390 sp<HGraphicBufferProducer2> igbp = surface ?
2391 surface->getHalInterface<HGraphicBufferProducer2>() : nullHgbp;
2392 if (surface && !igbp) {
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08002393 igbp = new B2HGraphicBufferProducer2(surface);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002394 }
2395
Sungtak Leefb579022022-05-10 06:36:15 +00002396 std::scoped_lock lock(mOutputMutex);
Sungtak Leea714f112021-03-16 05:40:03 -07002397 std::shared_ptr<SurfaceSyncObj> syncObj;
2398
Sungtak Lee08515812019-06-05 11:16:32 -07002399 if (!surface) {
Sungtak Leedb14cba2021-04-10 00:50:23 -07002400 mOutputBufferQueue->configure(nullIgbp, generation, 0, maxDequeueCount, nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07002401 } else if (surface->getUniqueId(&bqId) != OK) {
2402 LOG(ERROR) << "setOutputSurface -- "
2403 "cannot obtain bufferqueue id.";
2404 bqId = 0;
Sungtak Leedb14cba2021-04-10 00:50:23 -07002405 mOutputBufferQueue->configure(nullIgbp, generation, 0, maxDequeueCount, nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07002406 } else {
Wonsik Kime8e98152022-12-16 16:04:17 -08002407 mOutputBufferQueue->configure(surface, generation, bqId, maxDequeueCount,
2408 mHidlBase1_2 ? &syncObj : nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07002409 }
Lajos Molnar78aa7c92021-02-18 21:39:01 -08002410
2411 // set consumer bits
2412 // TODO: should this get incorporated into setOutputSurface method so that consumer bits
2413 // can be set atomically?
2414 uint64_t consumerUsage = kDefaultConsumerUsage;
2415 {
2416 if (surface) {
Sungtak Lee46a69d62023-08-12 07:24:24 +00002417 uint64_t usage = 0;
2418 status_t err = surface->getConsumerUsage(&usage);
Lajos Molnar78aa7c92021-02-18 21:39:01 -08002419 if (err != NO_ERROR) {
2420 ALOGD("setOutputSurface -- failed to get consumer usage bits (%d/%s). ignoring",
2421 err, asString(err));
2422 } else {
2423 // Note: we are adding the default usage because components must support
2424 // producing output frames that can be displayed an all output surfaces.
2425
2426 // TODO: do not set usage for tunneled scenario. It is unclear if consumer usage
2427 // is meaningful in a tunneled scenario; on one hand output buffers exist, but
2428 // they do not exist inside of C2 scope. Any buffer usage shall be communicated
2429 // through the sideband channel.
2430
Sungtak Lee46a69d62023-08-12 07:24:24 +00002431 consumerUsage = usage | kDefaultConsumerUsage;
Lajos Molnar78aa7c92021-02-18 21:39:01 -08002432 }
2433 }
2434
2435 C2StreamUsageTuning::output outputUsage{
2436 0u, C2AndroidMemoryUsage::FromGrallocUsage(consumerUsage).expected};
2437 std::vector<std::unique_ptr<C2SettingResult>> failures;
2438 c2_status_t err = config({&outputUsage}, C2_MAY_BLOCK, &failures);
2439 if (err != C2_OK) {
2440 ALOGD("setOutputSurface -- failed to set consumer usage (%d/%s)",
2441 err, asString(err));
2442 }
2443 }
2444 ALOGD("setOutputSurface -- generation=%u consumer usage=%#llx%s",
2445 generation, (long long)consumerUsage, syncObj ? " sync" : "");
Sungtak Lee08515812019-06-05 11:16:32 -07002446
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002447 Return<c2_hidl::Status> transStatus = syncObj ?
Wonsik Kime8e98152022-12-16 16:04:17 -08002448 mHidlBase1_2->setOutputSurfaceWithSyncObj(
Sungtak Leea714f112021-03-16 05:40:03 -07002449 static_cast<uint64_t>(blockPoolId),
2450 bqId == 0 ? nullHgbp : igbp, *syncObj) :
Wonsik Kime8e98152022-12-16 16:04:17 -08002451 mHidlBase1_0->setOutputSurface(
Sungtak Leea714f112021-03-16 05:40:03 -07002452 static_cast<uint64_t>(blockPoolId),
2453 bqId == 0 ? nullHgbp : igbp);
Lajos Molnar78aa7c92021-02-18 21:39:01 -08002454
Sungtak Lee8e2e2c62023-01-17 18:29:34 +00002455 mOutputBufferQueue->expireOldWaiters();
2456
Pawin Vongmasa36653902018-11-15 00:10:25 -08002457 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002458 LOG(ERROR) << "setOutputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002459 return C2_TRANSACTION_FAILED;
2460 }
2461 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002462 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002463 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002464 LOG(DEBUG) << "setOutputSurface -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002465 }
Sungtak Leea714f112021-03-16 05:40:03 -07002466 ALOGD("Surface configure completed");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002467 return status;
2468}
2469
2470status_t Codec2Client::Component::queueToOutputSurface(
2471 const C2ConstGraphicBlock& block,
2472 const QueueBufferInput& input,
2473 QueueBufferOutput* output) {
Arun Johnson7ba67072023-11-06 22:23:04 +00002474 ScopedTrace trace(ATRACE_TAG,"Codec2Client::Component::queueToOutputSurface");
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002475 if (mAidlBase) {
2476 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2477 mGraphicBufferAllocators->current();
2478 if (gba) {
2479 return gba->displayBuffer(block, input, output);
2480 } else {
2481 return C2_NOT_FOUND;
2482 }
2483 }
Pawin Vongmasabf69de92019-10-29 06:21:27 -07002484 return mOutputBufferQueue->outputBuffer(block, input, output);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002485}
2486
Brian Lindahlc953b462023-01-27 16:21:43 -07002487void Codec2Client::Component::pollForRenderedFrames(FrameEventHistoryDelta* delta) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002488 if (mAidlBase) {
2489 // TODO b/311348680
2490 return;
2491 }
Brian Lindahlc953b462023-01-27 16:21:43 -07002492 mOutputBufferQueue->pollForRenderedFrames(delta);
2493}
2494
Sungtak Leea714f112021-03-16 05:40:03 -07002495void Codec2Client::Component::setOutputSurfaceMaxDequeueCount(
2496 int maxDequeueCount) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002497 if (mAidlBase) {
2498 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2499 mGraphicBufferAllocators->current();
2500 if (gba) {
2501 gba->updateMaxDequeueBufferCount(maxDequeueCount);
2502 }
2503 return;
2504 }
Sungtak Leea714f112021-03-16 05:40:03 -07002505 mOutputBufferQueue->updateMaxDequeueBufferCount(maxDequeueCount);
2506}
2507
Sungtak Leec7da7a02022-05-05 08:45:33 +00002508void Codec2Client::Component::stopUsingOutputSurface(
2509 C2BlockPool::local_id_t blockPoolId) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002510 if (mAidlBase) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002511 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2512 mGraphicBufferAllocators->current();
2513 if (gba) {
2514 gba->reset();
2515 }
Wonsik Kime8e98152022-12-16 16:04:17 -08002516 return;
2517 }
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002518 std::scoped_lock lock(mOutputMutex);
2519 mOutputBufferQueue->stop();
Wonsik Kime8e98152022-12-16 16:04:17 -08002520 Return<c2_hidl::Status> transStatus = mHidlBase1_0->setOutputSurface(
Sungtak Leec7da7a02022-05-05 08:45:33 +00002521 static_cast<uint64_t>(blockPoolId), nullptr);
2522 if (!transStatus.isOk()) {
2523 LOG(ERROR) << "setOutputSurface(stopUsingOutputSurface) -- transaction failed.";
2524 } else {
2525 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002526 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Sungtak Leec7da7a02022-05-05 08:45:33 +00002527 if (status != C2_OK) {
2528 LOG(DEBUG) << "setOutputSurface(stopUsingOutputSurface) -- call failed: "
2529 << status << ".";
2530 }
2531 }
Sungtak Lee8e2e2c62023-01-17 18:29:34 +00002532 mOutputBufferQueue->expireOldWaiters();
Sungtak Leec7da7a02022-05-05 08:45:33 +00002533}
2534
Sungtak Lee214ce612023-11-01 10:01:13 +00002535void Codec2Client::Component::onBufferReleasedFromOutputSurface(
2536 uint32_t generation) {
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002537 if (mAidlBase) {
2538 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2539 mGraphicBufferAllocators->current();
2540 if (gba) {
2541 gba->onBufferReleased(generation);
2542 }
2543 return;
2544 }
Sungtak Lee6700cc92023-11-22 18:04:05 +00002545 mOutputBufferQueue->onBufferReleased(generation);
Sungtak Lee214ce612023-11-01 10:01:13 +00002546}
2547
Sungtak Leeaa71b5c2023-11-04 09:21:15 +00002548void Codec2Client::Component::holdIgbaBlocks(
2549 const std::list<std::unique_ptr<C2Work>>& workList) {
2550 if (!mAidlBase) {
2551 return;
2552 }
2553 std::shared_ptr<AidlGraphicBufferAllocator> gba =
2554 mGraphicBufferAllocators->current();
2555 if (!gba) {
2556 return;
2557 }
2558 std::shared_ptr<c2_aidl::IGraphicBufferAllocator> igba =
2559 c2_aidl::IGraphicBufferAllocator::fromBinder(gba->asBinder());
2560 for (const std::unique_ptr<C2Work>& work : workList) {
2561 if (!work) [[unlikely]] {
2562 continue;
2563 }
2564 for (const std::unique_ptr<C2Worklet>& worklet : work->worklets) {
2565 if (!worklet) {
2566 continue;
2567 }
2568 for (const std::shared_ptr<C2Buffer>& buffer : worklet->output.buffers) {
2569 if (buffer) {
2570 for (const C2ConstGraphicBlock& block : buffer->data().graphicBlocks()) {
2571 std::shared_ptr<_C2BlockPoolData> poolData =
2572 _C2BlockFactory::GetGraphicBlockPoolData(block);
2573 _C2BlockFactory::RegisterIgba(poolData, igba);
2574 }
2575 }
2576 }
2577 }
2578 }
2579}
2580
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002581c2_status_t Codec2Client::Component::connectToInputSurface(
2582 const std::shared_ptr<InputSurface>& inputSurface,
2583 std::shared_ptr<InputSurfaceConnection>* connection) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002584 if (mAidlBase) {
2585 // FIXME
2586 return C2_OMITTED;
2587 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002588 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08002589 Return<void> transStatus = mHidlBase1_0->connectToInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002590 inputSurface->mBase,
2591 [&status, connection](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002592 c2_hidl::Status s, const sp<c2_hidl::IInputSurfaceConnection>& c) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002593 status = static_cast<c2_status_t>(s);
2594 if (status != C2_OK) {
2595 LOG(DEBUG) << "connectToInputSurface -- call failed: "
2596 << status << ".";
2597 return;
2598 }
2599 *connection = std::make_shared<InputSurfaceConnection>(c);
2600 });
Pawin Vongmasa36653902018-11-15 00:10:25 -08002601 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002602 LOG(ERROR) << "connectToInputSurface -- transaction failed";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002603 return C2_TRANSACTION_FAILED;
2604 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002605 return status;
2606}
2607
2608c2_status_t Codec2Client::Component::connectToOmxInputSurface(
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08002609 const sp<HGraphicBufferProducer1>& producer,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002610 const sp<HGraphicBufferSource>& source,
2611 std::shared_ptr<InputSurfaceConnection>* connection) {
Wonsik Kime8e98152022-12-16 16:04:17 -08002612 if (mAidlBase) {
2613 LOG(WARNING) << "Connecting to OMX input surface is not supported for AIDL C2 HAL";
2614 return C2_OMITTED;
2615 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002616 c2_status_t status;
Wonsik Kime8e98152022-12-16 16:04:17 -08002617 Return<void> transStatus = mHidlBase1_0->connectToOmxInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002618 producer, source,
2619 [&status, connection](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002620 c2_hidl::Status s, const sp<c2_hidl::IInputSurfaceConnection>& c) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002621 status = static_cast<c2_status_t>(s);
2622 if (status != C2_OK) {
2623 LOG(DEBUG) << "connectToOmxInputSurface -- call failed: "
2624 << status << ".";
2625 return;
2626 }
2627 *connection = std::make_shared<InputSurfaceConnection>(c);
2628 });
2629 if (!transStatus.isOk()) {
2630 LOG(ERROR) << "connectToOmxInputSurface -- transaction failed.";
2631 return C2_TRANSACTION_FAILED;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002632 }
2633 return status;
2634}
2635
2636c2_status_t Codec2Client::Component::disconnectFromInputSurface() {
Wonsik Kime8e98152022-12-16 16:04:17 -08002637 if (mAidlBase) {
2638 // FIXME
2639 return C2_OMITTED;
2640 }
2641 Return<c2_hidl::Status> transStatus = mHidlBase1_0->disconnectFromInputSurface();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002642 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002643 LOG(ERROR) << "disconnectToInputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002644 return C2_TRANSACTION_FAILED;
2645 }
2646 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002647 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002648 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002649 LOG(DEBUG) << "disconnectFromInputSurface -- call failed: "
2650 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002651 }
2652 return status;
2653}
2654
Wonsik Kime8e98152022-12-16 16:04:17 -08002655Codec2Client::Component::AidlDeathManager *Codec2Client::Component::GetAidlDeathManager() {
2656 // This object never gets destructed
2657 static AidlDeathManager *sManager = new AidlDeathManager();
2658 return sManager;
2659}
2660
Pawin Vongmasa36653902018-11-15 00:10:25 -08002661c2_status_t Codec2Client::Component::setDeathListener(
2662 const std::shared_ptr<Component>& component,
2663 const std::shared_ptr<Listener>& listener) {
2664
2665 struct HidlDeathRecipient : public hardware::hidl_death_recipient {
2666 std::weak_ptr<Component> component;
2667 std::weak_ptr<Listener> base;
2668
2669 virtual void serviceDied(
2670 uint64_t /* cookie */,
2671 const wp<::android::hidl::base::V1_0::IBase>& /* who */
2672 ) override {
2673 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
2674 listener->onDeath(component);
2675 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002676 LOG(DEBUG) << "onDeath -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002677 }
2678 }
2679 };
2680
Wonsik Kime8e98152022-12-16 16:04:17 -08002681 if (component->mAidlBase) {
2682 size_t seq;
2683 if (GetAidlDeathManager()->linkToDeath(component, listener, &seq)) {
2684 component->mAidlDeathSeq = seq;
2685 }
2686 return C2_OK;
2687 }
2688
Pawin Vongmasa36653902018-11-15 00:10:25 -08002689 sp<HidlDeathRecipient> deathRecipient = new HidlDeathRecipient();
2690 deathRecipient->base = listener;
2691 deathRecipient->component = component;
2692
2693 component->mDeathRecipient = deathRecipient;
Wonsik Kime8e98152022-12-16 16:04:17 -08002694 Return<bool> transResult = component->mHidlBase1_0->linkToDeath(
Pawin Vongmasa36653902018-11-15 00:10:25 -08002695 component->mDeathRecipient, 0);
2696 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002697 LOG(ERROR) << "setDeathListener -- linkToDeath() transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002698 return C2_TRANSACTION_FAILED;
2699 }
2700 if (!static_cast<bool>(transResult)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002701 LOG(DEBUG) << "setDeathListener -- linkToDeath() call failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08002702 return C2_CORRUPTED;
2703 }
2704 return C2_OK;
2705}
2706
2707// Codec2Client::InputSurface
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002708Codec2Client::InputSurface::InputSurface(const sp<c2_hidl::IInputSurface>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002709 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002710 [base]() -> sp<c2_hidl::IConfigurable> {
2711 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002712 base->getConfigurable();
2713 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002714 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002715 nullptr;
2716 }()
2717 },
2718 mBase{base},
2719 mGraphicBufferProducer{new
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08002720 H2BGraphicBufferProducer2([base]() -> sp<HGraphicBufferProducer2> {
2721 Return<sp<HGraphicBufferProducer2>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002722 base->getGraphicBufferProducer();
2723 return transResult.isOk() ?
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08002724 static_cast<sp<HGraphicBufferProducer2>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002725 nullptr;
2726 }())} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002727}
2728
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002729sp<IGraphicBufferProducer>
Pawin Vongmasa36653902018-11-15 00:10:25 -08002730 Codec2Client::InputSurface::getGraphicBufferProducer() const {
2731 return mGraphicBufferProducer;
2732}
2733
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002734sp<c2_hidl::IInputSurface> Codec2Client::InputSurface::getHalInterface() const {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002735 return mBase;
2736}
2737
2738// Codec2Client::InputSurfaceConnection
Pawin Vongmasa36653902018-11-15 00:10:25 -08002739Codec2Client::InputSurfaceConnection::InputSurfaceConnection(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002740 const sp<c2_hidl::IInputSurfaceConnection>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002741 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002742 [base]() -> sp<c2_hidl::IConfigurable> {
2743 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002744 base->getConfigurable();
2745 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002746 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002747 nullptr;
2748 }()
2749 },
2750 mBase{base} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002751}
2752
2753c2_status_t Codec2Client::InputSurfaceConnection::disconnect() {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08002754 Return<c2_hidl::Status> transResult = mBase->disconnect();
2755 return static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transResult));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002756}
2757
2758} // namespace android