1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Codec2-types"
19 #include <android-base/logging.h>
20
21 #include <codec2/hidl/1.0/types.h>
22 #include <media/stagefright/foundation/AUtils.h>
23
24 #include <C2AllocatorIon.h>
25 #include <C2AllocatorGralloc.h>
26 #include <C2BlockInternal.h>
27 #include <C2Buffer.h>
28 #include <C2Component.h>
29 #include <C2Param.h>
30 #include <C2ParamInternal.h>
31 #include <C2PlatformSupport.h>
32 #include <C2Work.h>
33 #include <util/C2ParamUtils.h>
34
35 #include <algorithm>
36 #include <functional>
37 #include <iomanip>
38 #include <unordered_map>
39
40 namespace android {
41 namespace hardware {
42 namespace media {
43 namespace c2 {
44 namespace V1_0 {
45 namespace utils {
46
47 using ::android::hardware::Return;
48 using ::android::hardware::media::bufferpool::BufferPoolData;
49 using ::android::hardware::media::bufferpool::V2_0::BufferStatusMessage;
50 using ::android::hardware::media::bufferpool::V2_0::ResultStatus;
51 using ::android::hardware::media::bufferpool::V2_0::implementation::
52 ClientManager;
53 using ::android::hardware::media::bufferpool::V2_0::implementation::
54 TransactionId;
55
asString(Status status,const char * def)56 const char* asString(Status status, const char* def) {
57 return asString(static_cast<c2_status_t>(status), def);
58 }
59
60 namespace /* unnamed */ {
61
62 template <typename EnumClass>
underlying_value(EnumClass x)63 typename std::underlying_type<EnumClass>::type underlying_value(
64 EnumClass x) {
65 return static_cast<typename std::underlying_type<EnumClass>::type>(x);
66 }
67
68 template <typename Common, typename DstVector, typename SrcVector>
copyVector(DstVector * d,const SrcVector & s)69 void copyVector(DstVector* d, const SrcVector& s) {
70 static_assert(sizeof(Common) == sizeof(decltype((*d)[0])),
71 "DstVector's component size does not match Common");
72 static_assert(sizeof(Common) == sizeof(decltype(s[0])),
73 "SrcVector's component size does not match Common");
74 d->resize(s.size());
75 std::copy(
76 reinterpret_cast<const Common*>(&s[0]),
77 reinterpret_cast<const Common*>(&s[0] + s.size()),
78 reinterpret_cast<Common*>(&(*d)[0]));
79 }
80
81 // C2ParamField -> ParamField
objcpy(ParamField * d,const C2ParamField & s)82 bool objcpy(ParamField *d, const C2ParamField &s) {
83 d->index = static_cast<ParamIndex>(_C2ParamInspector::GetIndex(s));
84 d->fieldId.offset = static_cast<uint32_t>(_C2ParamInspector::GetOffset(s));
85 d->fieldId.size = static_cast<uint32_t>(_C2ParamInspector::GetSize(s));
86 return true;
87 }
88
89 struct C2ParamFieldBuilder : public C2ParamField {
C2ParamFieldBuilderandroid::hardware::media::c2::V1_0::utils::__anon4abb1d570111::C2ParamFieldBuilder90 C2ParamFieldBuilder() : C2ParamField(
91 static_cast<C2Param::Index>(static_cast<uint32_t>(0)), 0, 0) {
92 }
93 // ParamField -> C2ParamField
C2ParamFieldBuilderandroid::hardware::media::c2::V1_0::utils::__anon4abb1d570111::C2ParamFieldBuilder94 C2ParamFieldBuilder(const ParamField& s) : C2ParamField(
95 static_cast<C2Param::Index>(static_cast<uint32_t>(s.index)),
96 static_cast<uint32_t>(s.fieldId.offset),
97 static_cast<uint32_t>(s.fieldId.size)) {
98 }
99 };
100
101 // C2WorkOrdinalStruct -> WorkOrdinal
objcpy(WorkOrdinal * d,const C2WorkOrdinalStruct & s)102 bool objcpy(WorkOrdinal *d, const C2WorkOrdinalStruct &s) {
103 d->frameIndex = static_cast<uint64_t>(s.frameIndex.peeku());
104 d->timestampUs = static_cast<uint64_t>(s.timestamp.peeku());
105 d->customOrdinal = static_cast<uint64_t>(s.customOrdinal.peeku());
106 return true;
107 }
108
109 // WorkOrdinal -> C2WorkOrdinalStruct
objcpy(C2WorkOrdinalStruct * d,const WorkOrdinal & s)110 bool objcpy(C2WorkOrdinalStruct *d, const WorkOrdinal &s) {
111 d->frameIndex = c2_cntr64_t(s.frameIndex);
112 d->timestamp = c2_cntr64_t(s.timestampUs);
113 d->customOrdinal = c2_cntr64_t(s.customOrdinal);
114 return true;
115 }
116
117 // C2FieldSupportedValues::range's type -> ValueRange
objcpy(ValueRange * d,const decltype(C2FieldSupportedValues::range) & s)118 bool objcpy(
119 ValueRange* d,
120 const decltype(C2FieldSupportedValues::range)& s) {
121 d->min = static_cast<PrimitiveValue>(s.min.u64);
122 d->max = static_cast<PrimitiveValue>(s.max.u64);
123 d->step = static_cast<PrimitiveValue>(s.step.u64);
124 d->num = static_cast<PrimitiveValue>(s.num.u64);
125 d->denom = static_cast<PrimitiveValue>(s.denom.u64);
126 return true;
127 }
128
129 // C2FieldSupportedValues -> FieldSupportedValues
objcpy(FieldSupportedValues * d,const C2FieldSupportedValues & s)130 bool objcpy(FieldSupportedValues *d, const C2FieldSupportedValues &s) {
131 switch (s.type) {
132 case C2FieldSupportedValues::EMPTY: {
133 d->empty(::android::hidl::safe_union::V1_0::Monostate{});
134 break;
135 }
136 case C2FieldSupportedValues::RANGE: {
137 ValueRange range{};
138 if (!objcpy(&range, s.range)) {
139 LOG(ERROR) << "Invalid C2FieldSupportedValues::range.";
140 d->range(range);
141 return false;
142 }
143 d->range(range);
144 break;
145 }
146 case C2FieldSupportedValues::VALUES: {
147 hidl_vec<PrimitiveValue> values;
148 copyVector<uint64_t>(&values, s.values);
149 d->values(values);
150 break;
151 }
152 case C2FieldSupportedValues::FLAGS: {
153 hidl_vec<PrimitiveValue> flags;
154 copyVector<uint64_t>(&flags, s.values);
155 d->flags(flags);
156 break;
157 }
158 default:
159 LOG(DEBUG) << "Unrecognized C2FieldSupportedValues::type_t "
160 << "with underlying value " << underlying_value(s.type)
161 << ".";
162 return false;
163 }
164 return true;
165 }
166
167 // ValueRange -> C2FieldSupportedValues::range's type
objcpy(decltype(C2FieldSupportedValues::range) * d,const ValueRange & s)168 bool objcpy(
169 decltype(C2FieldSupportedValues::range)* d,
170 const ValueRange& s) {
171 d->min.u64 = static_cast<uint64_t>(s.min);
172 d->max.u64 = static_cast<uint64_t>(s.max);
173 d->step.u64 = static_cast<uint64_t>(s.step);
174 d->num.u64 = static_cast<uint64_t>(s.num);
175 d->denom.u64 = static_cast<uint64_t>(s.denom);
176 return true;
177 }
178
179 // FieldSupportedValues -> C2FieldSupportedValues
objcpy(C2FieldSupportedValues * d,const FieldSupportedValues & s)180 bool objcpy(C2FieldSupportedValues *d, const FieldSupportedValues &s) {
181 switch (s.getDiscriminator()) {
182 case FieldSupportedValues::hidl_discriminator::empty: {
183 d->type = C2FieldSupportedValues::EMPTY;
184 break;
185 }
186 case FieldSupportedValues::hidl_discriminator::range: {
187 d->type = C2FieldSupportedValues::RANGE;
188 if (!objcpy(&d->range, s.range())) {
189 LOG(ERROR) << "Invalid FieldSupportedValues::range.";
190 return false;
191 }
192 d->values.resize(0);
193 break;
194 }
195 case FieldSupportedValues::hidl_discriminator::values: {
196 d->type = C2FieldSupportedValues::VALUES;
197 copyVector<uint64_t>(&d->values, s.values());
198 break;
199 }
200 case FieldSupportedValues::hidl_discriminator::flags: {
201 d->type = C2FieldSupportedValues::FLAGS;
202 copyVector<uint64_t>(&d->values, s.flags());
203 break;
204 }
205 default:
206 LOG(WARNING) << "Unrecognized FieldSupportedValues::getDiscriminator()";
207 return false;
208 }
209 return true;
210 }
211
212 } // unnamed namespace
213
214 // C2FieldSupportedValuesQuery -> FieldSupportedValuesQuery
objcpy(FieldSupportedValuesQuery * d,const C2FieldSupportedValuesQuery & s)215 bool objcpy(
216 FieldSupportedValuesQuery* d,
217 const C2FieldSupportedValuesQuery& s) {
218 if (!objcpy(&d->field, s.field())) {
219 LOG(ERROR) << "Invalid C2FieldSupportedValuesQuery::field.";
220 return false;
221 }
222 switch (s.type()) {
223 case C2FieldSupportedValuesQuery::POSSIBLE:
224 d->type = FieldSupportedValuesQuery::Type::POSSIBLE;
225 break;
226 case C2FieldSupportedValuesQuery::CURRENT:
227 d->type = FieldSupportedValuesQuery::Type::CURRENT;
228 break;
229 default:
230 LOG(DEBUG) << "Unrecognized C2FieldSupportedValuesQuery::type_t "
231 << "with underlying value " << underlying_value(s.type())
232 << ".";
233 d->type = static_cast<FieldSupportedValuesQuery::Type>(s.type());
234 }
235 return true;
236 }
237
238 // FieldSupportedValuesQuery -> C2FieldSupportedValuesQuery
objcpy(C2FieldSupportedValuesQuery * d,const FieldSupportedValuesQuery & s)239 bool objcpy(
240 C2FieldSupportedValuesQuery* d,
241 const FieldSupportedValuesQuery& s) {
242 C2FieldSupportedValuesQuery::type_t dType;
243 switch (s.type) {
244 case FieldSupportedValuesQuery::Type::POSSIBLE:
245 dType = C2FieldSupportedValuesQuery::POSSIBLE;
246 break;
247 case FieldSupportedValuesQuery::Type::CURRENT:
248 dType = C2FieldSupportedValuesQuery::CURRENT;
249 break;
250 default:
251 LOG(DEBUG) << "Unrecognized FieldSupportedValuesQuery::Type "
252 << "with underlying value " << underlying_value(s.type)
253 << ".";
254 dType = static_cast<C2FieldSupportedValuesQuery::type_t>(s.type);
255 }
256 *d = C2FieldSupportedValuesQuery(C2ParamFieldBuilder(s.field), dType);
257 return true;
258 }
259
260 // C2FieldSupportedValuesQuery -> FieldSupportedValuesQueryResult
objcpy(FieldSupportedValuesQueryResult * d,const C2FieldSupportedValuesQuery & s)261 bool objcpy(
262 FieldSupportedValuesQueryResult* d,
263 const C2FieldSupportedValuesQuery& s) {
264 d->status = static_cast<Status>(s.status);
265 return objcpy(&d->values, s.values);
266 }
267
268 // FieldSupportedValuesQuery, FieldSupportedValuesQueryResult ->
269 // C2FieldSupportedValuesQuery
objcpy(C2FieldSupportedValuesQuery * d,const FieldSupportedValuesQuery & sq,const FieldSupportedValuesQueryResult & sr)270 bool objcpy(
271 C2FieldSupportedValuesQuery* d,
272 const FieldSupportedValuesQuery& sq,
273 const FieldSupportedValuesQueryResult& sr) {
274 if (!objcpy(d, sq)) {
275 LOG(ERROR) << "Invalid FieldSupportedValuesQuery.";
276 return false;
277 }
278 d->status = static_cast<c2_status_t>(sr.status);
279 if (!objcpy(&d->values, sr.values)) {
280 LOG(ERROR) << "Invalid FieldSupportedValuesQueryResult::values.";
281 return false;
282 }
283 return true;
284 }
285
286 // C2Component::Traits -> IComponentStore::ComponentTraits
objcpy(IComponentStore::ComponentTraits * d,const C2Component::Traits & s)287 bool objcpy(
288 IComponentStore::ComponentTraits *d,
289 const C2Component::Traits &s) {
290 d->name = s.name;
291
292 switch (s.domain) {
293 case C2Component::DOMAIN_VIDEO:
294 d->domain = IComponentStore::ComponentTraits::Domain::VIDEO;
295 break;
296 case C2Component::DOMAIN_AUDIO:
297 d->domain = IComponentStore::ComponentTraits::Domain::AUDIO;
298 break;
299 case C2Component::DOMAIN_IMAGE:
300 d->domain = IComponentStore::ComponentTraits::Domain::IMAGE;
301 break;
302 case C2Component::DOMAIN_OTHER:
303 d->domain = IComponentStore::ComponentTraits::Domain::OTHER;
304 break;
305 default:
306 LOG(DEBUG) << "Unrecognized C2Component::domain_t "
307 << "with underlying value " << underlying_value(s.domain)
308 << ".";
309 d->domain = static_cast<IComponentStore::ComponentTraits::Domain>(
310 s.domain);
311 }
312
313 switch (s.kind) {
314 case C2Component::KIND_DECODER:
315 d->kind = IComponentStore::ComponentTraits::Kind::DECODER;
316 break;
317 case C2Component::KIND_ENCODER:
318 d->kind = IComponentStore::ComponentTraits::Kind::ENCODER;
319 break;
320 case C2Component::KIND_OTHER:
321 d->kind = IComponentStore::ComponentTraits::Kind::OTHER;
322 break;
323 default:
324 LOG(DEBUG) << "Unrecognized C2Component::kind_t "
325 << "with underlying value " << underlying_value(s.kind)
326 << ".";
327 d->kind = static_cast<IComponentStore::ComponentTraits::Kind>(
328 s.kind);
329 }
330
331 d->rank = static_cast<uint32_t>(s.rank);
332
333 d->mediaType = s.mediaType;
334
335 d->aliases.resize(s.aliases.size());
336 for (size_t ix = s.aliases.size(); ix > 0; ) {
337 --ix;
338 d->aliases[ix] = s.aliases[ix];
339 }
340 return true;
341 }
342
343 // ComponentTraits -> C2Component::Traits, std::unique_ptr<std::vector<std::string>>
objcpy(C2Component::Traits * d,const IComponentStore::ComponentTraits & s)344 bool objcpy(
345 C2Component::Traits* d,
346 const IComponentStore::ComponentTraits& s) {
347 d->name = s.name.c_str();
348
349 switch (s.domain) {
350 case IComponentStore::ComponentTraits::Domain::VIDEO:
351 d->domain = C2Component::DOMAIN_VIDEO;
352 break;
353 case IComponentStore::ComponentTraits::Domain::AUDIO:
354 d->domain = C2Component::DOMAIN_AUDIO;
355 break;
356 case IComponentStore::ComponentTraits::Domain::IMAGE:
357 d->domain = C2Component::DOMAIN_IMAGE;
358 break;
359 case IComponentStore::ComponentTraits::Domain::OTHER:
360 d->domain = C2Component::DOMAIN_OTHER;
361 break;
362 default:
363 LOG(DEBUG) << "Unrecognized ComponentTraits::Domain "
364 << "with underlying value " << underlying_value(s.domain)
365 << ".";
366 d->domain = static_cast<C2Component::domain_t>(s.domain);
367 }
368
369 switch (s.kind) {
370 case IComponentStore::ComponentTraits::Kind::DECODER:
371 d->kind = C2Component::KIND_DECODER;
372 break;
373 case IComponentStore::ComponentTraits::Kind::ENCODER:
374 d->kind = C2Component::KIND_ENCODER;
375 break;
376 case IComponentStore::ComponentTraits::Kind::OTHER:
377 d->kind = C2Component::KIND_OTHER;
378 break;
379 default:
380 LOG(DEBUG) << "Unrecognized ComponentTraits::Kind "
381 << "with underlying value " << underlying_value(s.kind)
382 << ".";
383 d->kind = static_cast<C2Component::kind_t>(s.kind);
384 }
385
386 d->rank = static_cast<C2Component::rank_t>(s.rank);
387 d->mediaType = s.mediaType.c_str();
388 d->aliases.resize(s.aliases.size());
389 for (size_t i = 0; i < s.aliases.size(); ++i) {
390 d->aliases[i] = s.aliases[i];
391 }
392 return true;
393 }
394
395 namespace /* unnamed */ {
396
397 // C2ParamFieldValues -> ParamFieldValues
objcpy(ParamFieldValues * d,const C2ParamFieldValues & s)398 bool objcpy(ParamFieldValues *d, const C2ParamFieldValues &s) {
399 if (!objcpy(&d->paramOrField, s.paramOrField)) {
400 LOG(ERROR) << "Invalid C2ParamFieldValues::paramOrField.";
401 return false;
402 }
403 if (s.values) {
404 d->values.resize(1);
405 if (!objcpy(&d->values[0], *s.values)) {
406 LOG(ERROR) << "Invalid C2ParamFieldValues::values.";
407 return false;
408 }
409 return true;
410 }
411 d->values.resize(0);
412 return true;
413 }
414
415 // ParamFieldValues -> C2ParamFieldValues
objcpy(C2ParamFieldValues * d,const ParamFieldValues & s)416 bool objcpy(C2ParamFieldValues *d, const ParamFieldValues &s) {
417 d->paramOrField = C2ParamFieldBuilder(s.paramOrField);
418 if (s.values.size() == 1) {
419 d->values = std::make_unique<C2FieldSupportedValues>();
420 if (!objcpy(d->values.get(), s.values[0])) {
421 LOG(ERROR) << "Invalid ParamFieldValues::values.";
422 return false;
423 }
424 return true;
425 } else if (s.values.size() == 0) {
426 d->values.reset();
427 return true;
428 }
429 LOG(ERROR) << "Invalid ParamFieldValues: "
430 "Two or more FieldSupportedValues objects exist in "
431 "ParamFieldValues. "
432 "Only zero or one is allowed.";
433 return false;
434 }
435
436 } // unnamed namespace
437
438 // C2SettingResult -> SettingResult
objcpy(SettingResult * d,const C2SettingResult & s)439 bool objcpy(SettingResult *d, const C2SettingResult &s) {
440 switch (s.failure) {
441 case C2SettingResult::BAD_TYPE:
442 d->failure = SettingResult::Failure::BAD_TYPE;
443 break;
444 case C2SettingResult::BAD_PORT:
445 d->failure = SettingResult::Failure::BAD_PORT;
446 break;
447 case C2SettingResult::BAD_INDEX:
448 d->failure = SettingResult::Failure::BAD_INDEX;
449 break;
450 case C2SettingResult::READ_ONLY:
451 d->failure = SettingResult::Failure::READ_ONLY;
452 break;
453 case C2SettingResult::MISMATCH:
454 d->failure = SettingResult::Failure::MISMATCH;
455 break;
456 case C2SettingResult::BAD_VALUE:
457 d->failure = SettingResult::Failure::BAD_VALUE;
458 break;
459 case C2SettingResult::CONFLICT:
460 d->failure = SettingResult::Failure::CONFLICT;
461 break;
462 case C2SettingResult::UNSUPPORTED:
463 d->failure = SettingResult::Failure::UNSUPPORTED;
464 break;
465 case C2SettingResult::INFO_BAD_VALUE:
466 d->failure = SettingResult::Failure::INFO_BAD_VALUE;
467 break;
468 case C2SettingResult::INFO_CONFLICT:
469 d->failure = SettingResult::Failure::INFO_CONFLICT;
470 break;
471 default:
472 LOG(DEBUG) << "Unrecognized C2SettingResult::Failure "
473 << "with underlying value " << underlying_value(s.failure)
474 << ".";
475 d->failure = static_cast<SettingResult::Failure>(s.failure);
476 }
477 if (!objcpy(&d->field, s.field)) {
478 LOG(ERROR) << "Invalid C2SettingResult::field.";
479 return false;
480 }
481 d->conflicts.resize(s.conflicts.size());
482 size_t i = 0;
483 for (const C2ParamFieldValues& sConflict : s.conflicts) {
484 ParamFieldValues &dConflict = d->conflicts[i++];
485 if (!objcpy(&dConflict, sConflict)) {
486 LOG(ERROR) << "Invalid C2SettingResult::conflicts["
487 << i - 1 << "].";
488 return false;
489 }
490 }
491 return true;
492 }
493
494 // SettingResult -> std::unique_ptr<C2SettingResult>
objcpy(std::unique_ptr<C2SettingResult> * d,const SettingResult & s)495 bool objcpy(std::unique_ptr<C2SettingResult> *d, const SettingResult &s) {
496 *d = std::unique_ptr<C2SettingResult>(new C2SettingResult {
497 .field = C2ParamFieldValues(C2ParamFieldBuilder()) });
498 if (!*d) {
499 LOG(ERROR) << "No memory for C2SettingResult.";
500 return false;
501 }
502
503 // failure
504 switch (s.failure) {
505 case SettingResult::Failure::BAD_TYPE:
506 (*d)->failure = C2SettingResult::BAD_TYPE;
507 break;
508 case SettingResult::Failure::BAD_PORT:
509 (*d)->failure = C2SettingResult::BAD_PORT;
510 break;
511 case SettingResult::Failure::BAD_INDEX:
512 (*d)->failure = C2SettingResult::BAD_INDEX;
513 break;
514 case SettingResult::Failure::READ_ONLY:
515 (*d)->failure = C2SettingResult::READ_ONLY;
516 break;
517 case SettingResult::Failure::MISMATCH:
518 (*d)->failure = C2SettingResult::MISMATCH;
519 break;
520 case SettingResult::Failure::BAD_VALUE:
521 (*d)->failure = C2SettingResult::BAD_VALUE;
522 break;
523 case SettingResult::Failure::CONFLICT:
524 (*d)->failure = C2SettingResult::CONFLICT;
525 break;
526 case SettingResult::Failure::UNSUPPORTED:
527 (*d)->failure = C2SettingResult::UNSUPPORTED;
528 break;
529 case SettingResult::Failure::INFO_BAD_VALUE:
530 (*d)->failure = C2SettingResult::INFO_BAD_VALUE;
531 break;
532 case SettingResult::Failure::INFO_CONFLICT:
533 (*d)->failure = C2SettingResult::INFO_CONFLICT;
534 break;
535 default:
536 LOG(DEBUG) << "Unrecognized SettingResult::Failure "
537 << "with underlying value " << underlying_value(s.failure)
538 << ".";
539 (*d)->failure = static_cast<C2SettingResult::Failure>(s.failure);
540 }
541
542 // field
543 if (!objcpy(&(*d)->field, s.field)) {
544 LOG(ERROR) << "Invalid SettingResult::field.";
545 return false;
546 }
547
548 // conflicts
549 (*d)->conflicts.clear();
550 (*d)->conflicts.reserve(s.conflicts.size());
551 for (const ParamFieldValues& sConflict : s.conflicts) {
552 (*d)->conflicts.emplace_back(
553 C2ParamFieldValues{ C2ParamFieldBuilder(), nullptr });
554 if (!objcpy(&(*d)->conflicts.back(), sConflict)) {
555 LOG(ERROR) << "Invalid SettingResult::conflicts.";
556 return false;
557 }
558 }
559 return true;
560 }
561
562 // C2ParamDescriptor -> ParamDescriptor
objcpy(ParamDescriptor * d,const C2ParamDescriptor & s)563 bool objcpy(ParamDescriptor *d, const C2ParamDescriptor &s) {
564 d->index = static_cast<ParamIndex>(s.index());
565 d->attrib = static_cast<hidl_bitfield<ParamDescriptor::Attrib>>(
566 _C2ParamInspector::GetAttrib(s));
567 d->name = s.name();
568 copyVector<uint32_t>(&d->dependencies, s.dependencies());
569 return true;
570 }
571
572 // ParamDescriptor -> C2ParamDescriptor
objcpy(std::shared_ptr<C2ParamDescriptor> * d,const ParamDescriptor & s)573 bool objcpy(std::shared_ptr<C2ParamDescriptor> *d, const ParamDescriptor &s) {
574 std::vector<C2Param::Index> dDependencies;
575 dDependencies.reserve(s.dependencies.size());
576 for (const ParamIndex& sDependency : s.dependencies) {
577 dDependencies.emplace_back(static_cast<uint32_t>(sDependency));
578 }
579 *d = std::make_shared<C2ParamDescriptor>(
580 C2Param::Index(static_cast<uint32_t>(s.index)),
581 static_cast<C2ParamDescriptor::attrib_t>(s.attrib),
582 C2String(s.name.c_str()),
583 std::move(dDependencies));
584 return true;
585 }
586
587 // C2StructDescriptor -> StructDescriptor
objcpy(StructDescriptor * d,const C2StructDescriptor & s)588 bool objcpy(StructDescriptor *d, const C2StructDescriptor &s) {
589 d->type = static_cast<ParamIndex>(s.coreIndex().coreIndex());
590 d->fields.resize(s.numFields());
591 size_t i = 0;
592 for (const auto& sField : s) {
593 FieldDescriptor& dField = d->fields[i++];
594 dField.fieldId.offset = static_cast<uint32_t>(
595 _C2ParamInspector::GetOffset(sField));
596 dField.fieldId.size = static_cast<uint32_t>(
597 _C2ParamInspector::GetSize(sField));
598 dField.type = static_cast<hidl_bitfield<FieldDescriptor::Type>>(
599 sField.type());
600 dField.extent = static_cast<uint32_t>(sField.extent());
601 dField.name = static_cast<hidl_string>(sField.name());
602 const auto& sNamedValues = sField.namedValues();
603 dField.namedValues.resize(sNamedValues.size());
604 size_t j = 0;
605 for (const auto& sNamedValue : sNamedValues) {
606 FieldDescriptor::NamedValue& dNamedValue = dField.namedValues[j++];
607 dNamedValue.name = static_cast<hidl_string>(sNamedValue.first);
608 dNamedValue.value = static_cast<PrimitiveValue>(
609 sNamedValue.second.u64);
610 }
611 }
612 return true;
613 }
614
615 // StructDescriptor -> C2StructDescriptor
objcpy(std::unique_ptr<C2StructDescriptor> * d,const StructDescriptor & s)616 bool objcpy(std::unique_ptr<C2StructDescriptor> *d, const StructDescriptor &s) {
617 C2Param::CoreIndex dIndex = C2Param::CoreIndex(static_cast<uint32_t>(s.type));
618 std::vector<C2FieldDescriptor> dFields;
619 dFields.reserve(s.fields.size());
620 for (const auto &sField : s.fields) {
621 C2FieldDescriptor dField = {
622 static_cast<uint32_t>(sField.type),
623 sField.extent,
624 sField.name,
625 sField.fieldId.offset,
626 sField.fieldId.size };
627 C2FieldDescriptor::NamedValuesType namedValues;
628 namedValues.reserve(sField.namedValues.size());
629 for (const auto& sNamedValue : sField.namedValues) {
630 namedValues.emplace_back(
631 sNamedValue.name,
632 C2Value::Primitive(static_cast<uint64_t>(sNamedValue.value)));
633 }
634 _C2ParamInspector::AddNamedValues(dField, std::move(namedValues));
635 dFields.emplace_back(dField);
636 }
637 *d = std::make_unique<C2StructDescriptor>(
638 _C2ParamInspector::CreateStructDescriptor(dIndex, std::move(dFields)));
639 return true;
640 }
641
642 namespace /* unnamed */ {
643
644 // Find or add a hidl BaseBlock object from a given C2Handle* to a list and an
645 // associated map.
646 // Note: The handle is not cloned.
_addBaseBlock(uint32_t * index,const C2Handle * handle,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)647 bool _addBaseBlock(
648 uint32_t* index,
649 const C2Handle* handle,
650 std::list<BaseBlock>* baseBlocks,
651 std::map<const void*, uint32_t>* baseBlockIndices) {
652 if (!handle) {
653 LOG(ERROR) << "addBaseBlock called on a null C2Handle.";
654 return false;
655 }
656 auto it = baseBlockIndices->find(handle);
657 if (it != baseBlockIndices->end()) {
658 *index = it->second;
659 } else {
660 *index = baseBlocks->size();
661 baseBlockIndices->emplace(handle, *index);
662 baseBlocks->emplace_back();
663
664 BaseBlock &dBaseBlock = baseBlocks->back();
665 // This does not clone the handle.
666 dBaseBlock.nativeBlock(
667 reinterpret_cast<const native_handle_t*>(handle));
668
669 }
670 return true;
671 }
672
673 // Find or add a hidl BaseBlock object from a given BufferPoolData to a list and
674 // an associated map.
_addBaseBlock(uint32_t * index,const std::shared_ptr<BufferPoolData> bpData,BufferPoolSender * bufferPoolSender,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)675 bool _addBaseBlock(
676 uint32_t* index,
677 const std::shared_ptr<BufferPoolData> bpData,
678 BufferPoolSender* bufferPoolSender,
679 std::list<BaseBlock>* baseBlocks,
680 std::map<const void*, uint32_t>* baseBlockIndices) {
681 if (!bpData) {
682 LOG(ERROR) << "addBaseBlock called on a null BufferPoolData.";
683 return false;
684 }
685 auto it = baseBlockIndices->find(bpData.get());
686 if (it != baseBlockIndices->end()) {
687 *index = it->second;
688 } else {
689 *index = baseBlocks->size();
690 baseBlockIndices->emplace(bpData.get(), *index);
691 baseBlocks->emplace_back();
692
693 BaseBlock &dBaseBlock = baseBlocks->back();
694
695 if (bufferPoolSender) {
696 BufferStatusMessage pooledBlock;
697 ResultStatus bpStatus = bufferPoolSender->send(
698 bpData,
699 &pooledBlock);
700
701 if (bpStatus != ResultStatus::OK) {
702 LOG(ERROR) << "Failed to send buffer with BufferPool. Error: "
703 << static_cast<int32_t>(bpStatus)
704 << ".";
705 return false;
706 }
707 dBaseBlock.pooledBlock(pooledBlock);
708 }
709 }
710 return true;
711 }
712
addBaseBlock(uint32_t * index,const C2Handle * handle,const std::shared_ptr<const _C2BlockPoolData> & blockPoolData,BufferPoolSender * bufferPoolSender,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)713 bool addBaseBlock(
714 uint32_t* index,
715 const C2Handle* handle,
716 const std::shared_ptr<const _C2BlockPoolData>& blockPoolData,
717 BufferPoolSender* bufferPoolSender,
718 std::list<BaseBlock>* baseBlocks,
719 std::map<const void*, uint32_t>* baseBlockIndices) {
720 if (!blockPoolData) {
721 // No BufferPoolData ==> NATIVE block.
722 return _addBaseBlock(
723 index, handle,
724 baseBlocks, baseBlockIndices);
725 }
726 switch (blockPoolData->getType()) {
727 case _C2BlockPoolData::TYPE_BUFFERPOOL: {
728 // BufferPoolData
729 std::shared_ptr<BufferPoolData> bpData;
730 if (!_C2BlockFactory::GetBufferPoolData(blockPoolData, &bpData)
731 || !bpData) {
732 LOG(ERROR) << "BufferPoolData unavailable in a block.";
733 return false;
734 }
735 return _addBaseBlock(
736 index, bpData,
737 bufferPoolSender, baseBlocks, baseBlockIndices);
738 }
739 case _C2BlockPoolData::TYPE_BUFFERQUEUE:
740 uint32_t gen;
741 uint64_t bqId;
742 int32_t bqSlot;
743 // Update handle if migration happened.
744 if (_C2BlockFactory::GetBufferQueueData(
745 blockPoolData, &gen, &bqId, &bqSlot)) {
746 android::MigrateNativeCodec2GrallocHandle(
747 const_cast<native_handle_t*>(handle), gen, bqId, bqSlot);
748 }
749 return _addBaseBlock(
750 index, handle,
751 baseBlocks, baseBlockIndices);
752 default:
753 LOG(ERROR) << "Unknown C2BlockPoolData type.";
754 return false;
755 }
756 }
757
758 // C2Fence -> hidl_handle
759 // Note: File descriptors are not duplicated. The original file descriptor must
760 // not be closed before the transaction is complete.
objcpy(hidl_handle * d,const C2Fence & s)761 bool objcpy(hidl_handle* d, const C2Fence& s) {
762 (void)s; // TODO: implement s.fd()
763 int fenceFd = -1;
764 d->setTo(nullptr);
765 if (fenceFd >= 0) {
766 native_handle_t *handle = native_handle_create(1, 0);
767 if (!handle) {
768 LOG(ERROR) << "Failed to create a native handle.";
769 return false;
770 }
771 handle->data[0] = fenceFd;
772 d->setTo(handle, true /* owns */);
773 }
774 return true;
775 }
776
777 // C2ConstLinearBlock -> Block
778 // Note: Native handles are not duplicated. The original handles must not be
779 // closed before the transaction is complete.
objcpy(Block * d,const C2ConstLinearBlock & s,BufferPoolSender * bufferPoolSender,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)780 bool objcpy(Block* d, const C2ConstLinearBlock& s,
781 BufferPoolSender* bufferPoolSender,
782 std::list<BaseBlock>* baseBlocks,
783 std::map<const void*, uint32_t>* baseBlockIndices) {
784 std::shared_ptr<const _C2BlockPoolData> bpData =
785 _C2BlockFactory::GetLinearBlockPoolData(s);
786 if (!addBaseBlock(&d->index, s.handle(), bpData,
787 bufferPoolSender, baseBlocks, baseBlockIndices)) {
788 LOG(ERROR) << "Invalid block data in C2ConstLinearBlock.";
789 return false;
790 }
791
792 // Create the metadata.
793 C2Hidl_RangeInfo dRangeInfo;
794 dRangeInfo.offset = static_cast<uint32_t>(s.offset());
795 dRangeInfo.length = static_cast<uint32_t>(s.size());
796 if (!createParamsBlob(&d->meta, std::vector<C2Param*>{ &dRangeInfo })) {
797 LOG(ERROR) << "Invalid range info in C2ConstLinearBlock.";
798 return false;
799 }
800
801 // Copy the fence
802 if (!objcpy(&d->fence, s.fence())) {
803 LOG(ERROR) << "Invalid C2ConstLinearBlock::fence.";
804 return false;
805 }
806 return true;
807 }
808
809 // C2ConstGraphicBlock -> Block
810 // Note: Native handles are not duplicated. The original handles must not be
811 // closed before the transaction is complete.
objcpy(Block * d,const C2ConstGraphicBlock & s,BufferPoolSender * bufferPoolSender,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)812 bool objcpy(Block* d, const C2ConstGraphicBlock& s,
813 BufferPoolSender* bufferPoolSender,
814 std::list<BaseBlock>* baseBlocks,
815 std::map<const void*, uint32_t>* baseBlockIndices) {
816 std::shared_ptr<const _C2BlockPoolData> bpData =
817 _C2BlockFactory::GetGraphicBlockPoolData(s);
818 if (!addBaseBlock(&d->index, s.handle(), bpData,
819 bufferPoolSender, baseBlocks, baseBlockIndices)) {
820 LOG(ERROR) << "Invalid block data in C2ConstGraphicBlock.";
821 return false;
822 }
823
824 // Create the metadata.
825 C2Hidl_RectInfo dRectInfo;
826 C2Rect sRect = s.crop();
827 dRectInfo.left = static_cast<uint32_t>(sRect.left);
828 dRectInfo.top = static_cast<uint32_t>(sRect.top);
829 dRectInfo.width = static_cast<uint32_t>(sRect.width);
830 dRectInfo.height = static_cast<uint32_t>(sRect.height);
831 if (!createParamsBlob(&d->meta, std::vector<C2Param*>{ &dRectInfo })) {
832 LOG(ERROR) << "Invalid rect info in C2ConstGraphicBlock.";
833 return false;
834 }
835
836 // Copy the fence
837 if (!objcpy(&d->fence, s.fence())) {
838 LOG(ERROR) << "Invalid C2ConstGraphicBlock::fence.";
839 return false;
840 }
841 return true;
842 }
843
844 // C2BufferData -> Buffer
845 // This function only fills in d->blocks.
objcpy(Buffer * d,const C2BufferData & s,BufferPoolSender * bufferPoolSender,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)846 bool objcpy(Buffer* d, const C2BufferData& s,
847 BufferPoolSender* bufferPoolSender,
848 std::list<BaseBlock>* baseBlocks,
849 std::map<const void*, uint32_t>* baseBlockIndices) {
850 d->blocks.resize(
851 s.linearBlocks().size() +
852 s.graphicBlocks().size());
853 size_t i = 0;
854 for (const C2ConstLinearBlock& linearBlock : s.linearBlocks()) {
855 Block& dBlock = d->blocks[i++];
856 if (!objcpy(
857 &dBlock, linearBlock,
858 bufferPoolSender, baseBlocks, baseBlockIndices)) {
859 LOG(ERROR) << "Invalid C2BufferData::linearBlocks. "
860 << "(Destination index = " << i - 1 << ".)";
861 return false;
862 }
863 }
864 for (const C2ConstGraphicBlock& graphicBlock : s.graphicBlocks()) {
865 Block& dBlock = d->blocks[i++];
866 if (!objcpy(
867 &dBlock, graphicBlock,
868 bufferPoolSender, baseBlocks, baseBlockIndices)) {
869 LOG(ERROR) << "Invalid C2BufferData::graphicBlocks. "
870 << "(Destination index = " << i - 1 << ".)";
871 return false;
872 }
873 }
874 return true;
875 }
876
877 // C2Buffer -> Buffer
objcpy(Buffer * d,const C2Buffer & s,BufferPoolSender * bufferPoolSender,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)878 bool objcpy(Buffer* d, const C2Buffer& s,
879 BufferPoolSender* bufferPoolSender,
880 std::list<BaseBlock>* baseBlocks,
881 std::map<const void*, uint32_t>* baseBlockIndices) {
882 if (!createParamsBlob(&d->info, s.info())) {
883 LOG(ERROR) << "Invalid C2Buffer::info.";
884 return false;
885 }
886 if (!objcpy(d, s.data(), bufferPoolSender, baseBlocks, baseBlockIndices)) {
887 LOG(ERROR) << "Invalid C2Buffer::data.";
888 return false;
889 }
890 return true;
891 }
892
893 // C2InfoBuffer -> InfoBuffer
objcpy(InfoBuffer * d,const C2InfoBuffer & s,BufferPoolSender * bufferPoolSender,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)894 bool objcpy(InfoBuffer* d, const C2InfoBuffer& s,
895 BufferPoolSender* bufferPoolSender,
896 std::list<BaseBlock>* baseBlocks,
897 std::map<const void*, uint32_t>* baseBlockIndices) {
898 // TODO: C2InfoBuffer is not implemented.
899 (void)d;
900 (void)s;
901 (void)bufferPoolSender;
902 (void)baseBlocks;
903 (void)baseBlockIndices;
904 LOG(INFO) << "InfoBuffer not implemented.";
905 return true;
906 }
907
908 // C2FrameData -> FrameData
objcpy(FrameData * d,const C2FrameData & s,BufferPoolSender * bufferPoolSender,std::list<BaseBlock> * baseBlocks,std::map<const void *,uint32_t> * baseBlockIndices)909 bool objcpy(FrameData* d, const C2FrameData& s,
910 BufferPoolSender* bufferPoolSender,
911 std::list<BaseBlock>* baseBlocks,
912 std::map<const void*, uint32_t>* baseBlockIndices) {
913 d->flags = static_cast<hidl_bitfield<FrameData::Flags>>(s.flags);
914 if (!objcpy(&d->ordinal, s.ordinal)) {
915 LOG(ERROR) << "Invalid C2FrameData::ordinal.";
916 return false;
917 }
918
919 d->buffers.resize(s.buffers.size());
920 size_t i = 0;
921 for (const std::shared_ptr<C2Buffer>& sBuffer : s.buffers) {
922 Buffer& dBuffer = d->buffers[i++];
923 if (!sBuffer) {
924 // A null (pointer to) C2Buffer corresponds to a Buffer with empty
925 // info and blocks.
926 dBuffer.info.resize(0);
927 dBuffer.blocks.resize(0);
928 continue;
929 }
930 if (!objcpy(
931 &dBuffer, *sBuffer,
932 bufferPoolSender, baseBlocks, baseBlockIndices)) {
933 LOG(ERROR) << "Invalid C2FrameData::buffers["
934 << i - 1 << "].";
935 return false;
936 }
937 }
938
939 if (!createParamsBlob(&d->configUpdate, s.configUpdate)) {
940 LOG(ERROR) << "Invalid C2FrameData::configUpdate.";
941 return false;
942 }
943
944 d->infoBuffers.resize(s.infoBuffers.size());
945 i = 0;
946 for (const std::shared_ptr<C2InfoBuffer>& sInfoBuffer : s.infoBuffers) {
947 InfoBuffer& dInfoBuffer = d->infoBuffers[i++];
948 if (!sInfoBuffer) {
949 LOG(ERROR) << "Null C2FrameData::infoBuffers["
950 << i - 1 << "].";
951 return false;
952 }
953 if (!objcpy(&dInfoBuffer, *sInfoBuffer,
954 bufferPoolSender, baseBlocks, baseBlockIndices)) {
955 LOG(ERROR) << "Invalid C2FrameData::infoBuffers["
956 << i - 1 << "].";
957 return false;
958 }
959 }
960
961 return true;
962 }
963
964 } // unnamed namespace
965
966 // DefaultBufferPoolSender's implementation
967
DefaultBufferPoolSender(const sp<IClientManager> & receiverManager,std::chrono::steady_clock::duration refreshInterval)968 DefaultBufferPoolSender::DefaultBufferPoolSender(
969 const sp<IClientManager>& receiverManager,
970 std::chrono::steady_clock::duration refreshInterval)
971 : mReceiverManager(receiverManager),
972 mRefreshInterval(refreshInterval) {
973 }
974
setReceiver(const sp<IClientManager> & receiverManager,std::chrono::steady_clock::duration refreshInterval)975 void DefaultBufferPoolSender::setReceiver(
976 const sp<IClientManager>& receiverManager,
977 std::chrono::steady_clock::duration refreshInterval) {
978 std::lock_guard<std::mutex> lock(mMutex);
979 if (mReceiverManager != receiverManager) {
980 mReceiverManager = receiverManager;
981 mConnections.clear();
982 }
983 mRefreshInterval = refreshInterval;
984 }
985
send(const std::shared_ptr<BufferPoolData> & bpData,BufferStatusMessage * bpMessage)986 ResultStatus DefaultBufferPoolSender::send(
987 const std::shared_ptr<BufferPoolData>& bpData,
988 BufferStatusMessage* bpMessage) {
989 int64_t connectionId = bpData->mConnectionId;
990 if (connectionId == 0) {
991 LOG(WARNING) << "registerSender -- invalid sender connection id (0).";
992 return ResultStatus::CRITICAL_ERROR;
993 }
994 std::lock_guard<std::mutex> lock(mMutex);
995 if (!mReceiverManager) {
996 LOG(ERROR) << "No access to receiver's BufferPool.";
997 return ResultStatus::NOT_FOUND;
998 }
999 if (!mSenderManager) {
1000 mSenderManager = ClientManager::getInstance();
1001 if (!mSenderManager) {
1002 LOG(ERROR) << "Failed to retrieve local BufferPool ClientManager.";
1003 return ResultStatus::CRITICAL_ERROR;
1004 }
1005 }
1006
1007 int64_t receiverConnectionId{0};
1008 auto foundConnection = mConnections.find(connectionId);
1009 bool isNewConnection = foundConnection == mConnections.end();
1010 std::chrono::steady_clock::time_point now =
1011 std::chrono::steady_clock::now();
1012 if (isNewConnection ||
1013 (now - foundConnection->second.lastSent > mRefreshInterval)) {
1014 // Initialize the bufferpool connection.
1015 ResultStatus rs =
1016 mSenderManager->registerSender(mReceiverManager,
1017 connectionId,
1018 &receiverConnectionId);
1019 if ((rs != ResultStatus::OK) && (rs != ResultStatus::ALREADY_EXISTS)) {
1020 LOG(WARNING) << "registerSender -- returned error: "
1021 << static_cast<int32_t>(rs)
1022 << ".";
1023 return rs;
1024 } else if (receiverConnectionId == 0) {
1025 LOG(WARNING) << "registerSender -- "
1026 "invalid receiver connection id (0).";
1027 return ResultStatus::CRITICAL_ERROR;
1028 } else {
1029 if (isNewConnection) {
1030 foundConnection = mConnections.try_emplace(
1031 connectionId, receiverConnectionId, now).first;
1032 } else {
1033 foundConnection->second.receiverConnectionId = receiverConnectionId;
1034 }
1035 }
1036 } else {
1037 receiverConnectionId = foundConnection->second.receiverConnectionId;
1038 }
1039
1040 uint64_t transactionId;
1041 int64_t timestampUs;
1042 ResultStatus rs = mSenderManager->postSend(
1043 receiverConnectionId, bpData, &transactionId, ×tampUs);
1044 if (rs != ResultStatus::OK) {
1045 LOG(ERROR) << "ClientManager::postSend -- returned error: "
1046 << static_cast<int32_t>(rs)
1047 << ".";
1048 mConnections.erase(foundConnection);
1049 return rs;
1050 }
1051 if (!bpMessage) {
1052 LOG(ERROR) << "Null output parameter for BufferStatusMessage.";
1053 mConnections.erase(foundConnection);
1054 return ResultStatus::CRITICAL_ERROR;
1055 }
1056 bpMessage->connectionId = receiverConnectionId;
1057 bpMessage->bufferId = bpData->mId;
1058 bpMessage->transactionId = transactionId;
1059 bpMessage->timestampUs = timestampUs;
1060 foundConnection->second.lastSent = now;
1061 return rs;
1062 }
1063
1064 // std::list<std::unique_ptr<C2Work>> -> WorkBundle
objcpy(WorkBundle * d,const std::list<std::unique_ptr<C2Work>> & s,BufferPoolSender * bufferPoolSender)1065 bool objcpy(
1066 WorkBundle* d,
1067 const std::list<std::unique_ptr<C2Work>>& s,
1068 BufferPoolSender* bufferPoolSender) {
1069 // baseBlocks holds a list of BaseBlock objects that Blocks can refer to.
1070 std::list<BaseBlock> baseBlocks;
1071
1072 // baseBlockIndices maps a raw pointer to native_handle_t or BufferPoolData
1073 // inside baseBlocks to the corresponding index into baseBlocks. The keys
1074 // (pointers) are used to identify blocks that have the same "base block" in
1075 // s, a list of C2Work objects. Because baseBlocks will be copied into a
1076 // hidl_vec eventually, the values of baseBlockIndices are zero-based
1077 // integer indices instead of list iterators.
1078 //
1079 // Note that the pointers can be raw because baseBlockIndices has a shorter
1080 // lifespan than all of base blocks.
1081 std::map<const void*, uint32_t> baseBlockIndices;
1082
1083 d->works.resize(s.size());
1084 size_t i = 0;
1085 for (const std::unique_ptr<C2Work>& sWork : s) {
1086 Work &dWork = d->works[i++];
1087 if (!sWork) {
1088 LOG(WARNING) << "Null C2Work encountered.";
1089 continue;
1090 }
1091
1092 // chain info is not in use currently.
1093
1094 // input
1095 if (!objcpy(&dWork.input, sWork->input,
1096 bufferPoolSender, &baseBlocks, &baseBlockIndices)) {
1097 LOG(ERROR) << "Invalid C2Work::input.";
1098 return false;
1099 }
1100
1101 // worklets
1102 if (sWork->worklets.size() == 0) {
1103 LOG(DEBUG) << "Work with no worklets.";
1104 } else {
1105 // Parcel the worklets.
1106 hidl_vec<Worklet> &dWorklets = dWork.worklets;
1107 dWorklets.resize(sWork->worklets.size());
1108 size_t j = 0;
1109 for (const std::unique_ptr<C2Worklet>& sWorklet : sWork->worklets)
1110 {
1111 if (!sWorklet) {
1112 LOG(WARNING) << "Null C2Work::worklets["
1113 << j << "].";
1114 continue;
1115 }
1116 Worklet &dWorklet = dWorklets[j++];
1117
1118 // component id
1119 dWorklet.componentId = static_cast<uint32_t>(
1120 sWorklet->component);
1121
1122 // tunings
1123 if (!createParamsBlob(&dWorklet.tunings, sWorklet->tunings)) {
1124 LOG(ERROR) << "Invalid C2Work::worklets["
1125 << j - 1 << "]->tunings.";
1126 return false;
1127 }
1128
1129 // failures
1130 dWorklet.failures.resize(sWorklet->failures.size());
1131 size_t k = 0;
1132 for (const std::unique_ptr<C2SettingResult>& sFailure :
1133 sWorklet->failures) {
1134 if (!sFailure) {
1135 LOG(WARNING) << "Null C2Work::worklets["
1136 << j - 1 << "]->failures["
1137 << k << "].";
1138 continue;
1139 }
1140 if (!objcpy(&dWorklet.failures[k++], *sFailure)) {
1141 LOG(ERROR) << "Invalid C2Work::worklets["
1142 << j - 1 << "]->failures["
1143 << k - 1 << "].";
1144 return false;
1145 }
1146 }
1147
1148 // output
1149 if (!objcpy(&dWorklet.output, sWorklet->output,
1150 bufferPoolSender, &baseBlocks, &baseBlockIndices)) {
1151 LOG(ERROR) << "Invalid C2Work::worklets["
1152 << j - 1 << "]->output.";
1153 return false;
1154 }
1155 }
1156 }
1157
1158 // worklets processed
1159 dWork.workletsProcessed = sWork->workletsProcessed;
1160
1161 // result
1162 dWork.result = static_cast<Status>(sWork->result);
1163 }
1164
1165 // Copy std::list<BaseBlock> to hidl_vec<BaseBlock>.
1166 {
1167 d->baseBlocks.resize(baseBlocks.size());
1168 size_t i = 0;
1169 for (const BaseBlock& baseBlock : baseBlocks) {
1170 d->baseBlocks[i++] = baseBlock;
1171 }
1172 }
1173
1174 return true;
1175 }
1176
1177 namespace /* unnamed */ {
1178
1179 struct C2BaseBlock {
1180 enum type_t {
1181 LINEAR,
1182 GRAPHIC,
1183 };
1184 type_t type;
1185 std::shared_ptr<C2LinearBlock> linear;
1186 std::shared_ptr<C2GraphicBlock> graphic;
1187 };
1188
1189 // hidl_handle -> C2Fence
1190 // Note: File descriptors are not duplicated. The original file descriptor must
1191 // not be closed before the transaction is complete.
objcpy(C2Fence * d,const hidl_handle & s)1192 bool objcpy(C2Fence* d, const hidl_handle& s) {
1193 // TODO: Implement.
1194 (void)s;
1195 *d = C2Fence();
1196 return true;
1197 }
1198
1199 // C2LinearBlock, vector<C2Param*>, C2Fence -> C2Buffer
createLinearBuffer(std::shared_ptr<C2Buffer> * buffer,const std::shared_ptr<C2LinearBlock> & block,const std::vector<C2Param * > & meta,const C2Fence & fence)1200 bool createLinearBuffer(
1201 std::shared_ptr<C2Buffer>* buffer,
1202 const std::shared_ptr<C2LinearBlock>& block,
1203 const std::vector<C2Param*>& meta,
1204 const C2Fence& fence) {
1205 // Check the block meta. It should have exactly 1 C2Info:
1206 // C2Hidl_RangeInfo.
1207 if ((meta.size() != 1) || !meta[0]) {
1208 LOG(ERROR) << "Invalid C2LinearBlock::meta.";
1209 return false;
1210 }
1211 if (meta[0]->size() != sizeof(C2Hidl_RangeInfo)) {
1212 LOG(ERROR) << "Invalid range info in C2LinearBlock.";
1213 return false;
1214 }
1215 C2Hidl_RangeInfo *rangeInfo =
1216 reinterpret_cast<C2Hidl_RangeInfo*>(meta[0]);
1217
1218 // Create C2Buffer from C2LinearBlock.
1219 *buffer = C2Buffer::CreateLinearBuffer(block->share(
1220 rangeInfo->offset, rangeInfo->length,
1221 fence));
1222 if (!(*buffer)) {
1223 LOG(ERROR) << "CreateLinearBuffer failed.";
1224 return false;
1225 }
1226 return true;
1227 }
1228
1229 // C2GraphicBlock, vector<C2Param*>, C2Fence -> C2Buffer
createGraphicBuffer(std::shared_ptr<C2Buffer> * buffer,const std::shared_ptr<C2GraphicBlock> & block,const std::vector<C2Param * > & meta,const C2Fence & fence)1230 bool createGraphicBuffer(
1231 std::shared_ptr<C2Buffer>* buffer,
1232 const std::shared_ptr<C2GraphicBlock>& block,
1233 const std::vector<C2Param*>& meta,
1234 const C2Fence& fence) {
1235 // Check the block meta. It should have exactly 1 C2Info:
1236 // C2Hidl_RectInfo.
1237 if ((meta.size() != 1) || !meta[0]) {
1238 LOG(ERROR) << "Invalid C2GraphicBlock::meta.";
1239 return false;
1240 }
1241 if (meta[0]->size() != sizeof(C2Hidl_RectInfo)) {
1242 LOG(ERROR) << "Invalid rect info in C2GraphicBlock.";
1243 return false;
1244 }
1245 C2Hidl_RectInfo *rectInfo =
1246 reinterpret_cast<C2Hidl_RectInfo*>(meta[0]);
1247
1248 // Create C2Buffer from C2GraphicBlock.
1249 *buffer = C2Buffer::CreateGraphicBuffer(block->share(
1250 C2Rect(rectInfo->width, rectInfo->height).
1251 at(rectInfo->left, rectInfo->top),
1252 fence));
1253 if (!(*buffer)) {
1254 LOG(ERROR) << "CreateGraphicBuffer failed.";
1255 return false;
1256 }
1257 return true;
1258 }
1259
1260 // Buffer -> C2Buffer
1261 // Note: The native handles will be cloned.
objcpy(std::shared_ptr<C2Buffer> * d,const Buffer & s,const std::vector<C2BaseBlock> & baseBlocks)1262 bool objcpy(std::shared_ptr<C2Buffer>* d, const Buffer& s,
1263 const std::vector<C2BaseBlock>& baseBlocks) {
1264 *d = nullptr;
1265
1266 // Currently, a non-null C2Buffer must contain exactly 1 block.
1267 if (s.blocks.size() == 0) {
1268 return true;
1269 } else if (s.blocks.size() != 1) {
1270 LOG(ERROR) << "Invalid Buffer: "
1271 "Currently, a C2Buffer must contain exactly 1 block.";
1272 return false;
1273 }
1274
1275 const Block &sBlock = s.blocks[0];
1276 if (sBlock.index >= baseBlocks.size()) {
1277 LOG(ERROR) << "Invalid Buffer::blocks[0].index: "
1278 "Array index out of range.";
1279 return false;
1280 }
1281 const C2BaseBlock &baseBlock = baseBlocks[sBlock.index];
1282
1283 // Parse meta.
1284 std::vector<C2Param*> sBlockMeta;
1285 if (!parseParamsBlob(&sBlockMeta, sBlock.meta)) {
1286 LOG(ERROR) << "Invalid Buffer::blocks[0].meta.";
1287 return false;
1288 }
1289
1290 // Copy fence.
1291 C2Fence dFence;
1292 if (!objcpy(&dFence, sBlock.fence)) {
1293 LOG(ERROR) << "Invalid Buffer::blocks[0].fence.";
1294 return false;
1295 }
1296
1297 // Construct a block.
1298 switch (baseBlock.type) {
1299 case C2BaseBlock::LINEAR:
1300 if (!createLinearBuffer(d, baseBlock.linear, sBlockMeta, dFence)) {
1301 LOG(ERROR) << "Invalid C2BaseBlock::linear.";
1302 return false;
1303 }
1304 break;
1305 case C2BaseBlock::GRAPHIC:
1306 if (!createGraphicBuffer(d, baseBlock.graphic, sBlockMeta, dFence)) {
1307 LOG(ERROR) << "Invalid C2BaseBlock::graphic.";
1308 return false;
1309 }
1310 break;
1311 default:
1312 LOG(ERROR) << "Invalid C2BaseBlock::type.";
1313 return false;
1314 }
1315
1316 // Parse info
1317 std::vector<C2Param*> params;
1318 if (!parseParamsBlob(¶ms, s.info)) {
1319 LOG(ERROR) << "Invalid Buffer::info.";
1320 return false;
1321 }
1322 for (C2Param* param : params) {
1323 if (param == nullptr) {
1324 LOG(ERROR) << "Null param in Buffer::info.";
1325 return false;
1326 }
1327 std::shared_ptr<C2Param> c2param{
1328 C2Param::Copy(*param).release()};
1329 if (!c2param) {
1330 LOG(ERROR) << "Invalid param in Buffer::info.";
1331 return false;
1332 }
1333 c2_status_t status =
1334 (*d)->setInfo(std::static_pointer_cast<C2Info>(c2param));
1335 if (status != C2_OK) {
1336 LOG(ERROR) << "C2Buffer::setInfo failed.";
1337 return false;
1338 }
1339 }
1340
1341 return true;
1342 }
1343
1344 // FrameData -> C2FrameData
objcpy(C2FrameData * d,const FrameData & s,const std::vector<C2BaseBlock> & baseBlocks)1345 bool objcpy(C2FrameData* d, const FrameData& s,
1346 const std::vector<C2BaseBlock>& baseBlocks) {
1347 d->flags = static_cast<C2FrameData::flags_t>(s.flags);
1348 if (!objcpy(&d->ordinal, s.ordinal)) {
1349 LOG(ERROR) << "Invalid FrameData::ordinal.";
1350 return false;
1351 }
1352 d->buffers.clear();
1353 d->buffers.reserve(s.buffers.size());
1354 for (const Buffer& sBuffer : s.buffers) {
1355 std::shared_ptr<C2Buffer> dBuffer;
1356 if (!objcpy(&dBuffer, sBuffer, baseBlocks)) {
1357 LOG(ERROR) << "Invalid FrameData::buffers.";
1358 return false;
1359 }
1360 d->buffers.emplace_back(dBuffer);
1361 }
1362
1363 std::vector<C2Param*> params;
1364 if (!parseParamsBlob(¶ms, s.configUpdate)) {
1365 LOG(ERROR) << "Invalid FrameData::configUpdate.";
1366 return false;
1367 }
1368 d->configUpdate.clear();
1369 for (C2Param* param : params) {
1370 d->configUpdate.emplace_back(C2Param::Copy(*param));
1371 if (!d->configUpdate.back()) {
1372 LOG(ERROR) << "Unexpected error while parsing "
1373 "FrameData::configUpdate.";
1374 return false;
1375 }
1376 }
1377
1378 // TODO: Implement this once C2InfoBuffer has constructors.
1379 d->infoBuffers.clear();
1380 return true;
1381 }
1382
1383 // BaseBlock -> C2BaseBlock
objcpy(C2BaseBlock * d,const BaseBlock & s)1384 bool objcpy(C2BaseBlock* d, const BaseBlock& s) {
1385 switch (s.getDiscriminator()) {
1386 case BaseBlock::hidl_discriminator::nativeBlock: {
1387 native_handle_t* sHandle =
1388 native_handle_clone(s.nativeBlock());
1389 if (sHandle == nullptr) {
1390 LOG(ERROR) << "Null BaseBlock::nativeBlock.";
1391 return false;
1392 }
1393 const C2Handle *sC2Handle =
1394 reinterpret_cast<const C2Handle*>(sHandle);
1395
1396 d->linear = _C2BlockFactory::CreateLinearBlock(sC2Handle);
1397 if (d->linear) {
1398 d->type = C2BaseBlock::LINEAR;
1399 return true;
1400 }
1401
1402 d->graphic = _C2BlockFactory::CreateGraphicBlock(sC2Handle);
1403 if (d->graphic) {
1404 d->type = C2BaseBlock::GRAPHIC;
1405 return true;
1406 }
1407
1408 LOG(ERROR) << "Unknown handle type in BaseBlock::nativeBlock.";
1409 if (sHandle) {
1410 native_handle_close(sHandle);
1411 native_handle_delete(sHandle);
1412 }
1413 return false;
1414 }
1415 case BaseBlock::hidl_discriminator::pooledBlock: {
1416 const BufferStatusMessage &bpMessage =
1417 s.pooledBlock();
1418 sp<ClientManager> bp = ClientManager::getInstance();
1419 std::shared_ptr<BufferPoolData> bpData;
1420 native_handle_t *cHandle;
1421 ResultStatus bpStatus = bp->receive(
1422 bpMessage.connectionId,
1423 bpMessage.transactionId,
1424 bpMessage.bufferId,
1425 bpMessage.timestampUs,
1426 &cHandle,
1427 &bpData);
1428 if (bpStatus != ResultStatus::OK) {
1429 LOG(ERROR) << "Failed to receive buffer from bufferpool -- "
1430 << "resultStatus = " << underlying_value(bpStatus)
1431 << ".";
1432 return false;
1433 } else if (!bpData) {
1434 LOG(ERROR) << "No data in bufferpool transaction.";
1435 return false;
1436 }
1437
1438 d->linear = _C2BlockFactory::CreateLinearBlock(cHandle, bpData);
1439 if (d->linear) {
1440 d->type = C2BaseBlock::LINEAR;
1441 return true;
1442 }
1443
1444 d->graphic = _C2BlockFactory::CreateGraphicBlock(cHandle, bpData);
1445 if (d->graphic) {
1446 d->type = C2BaseBlock::GRAPHIC;
1447 return true;
1448 }
1449 if (cHandle) {
1450 // Though we got cloned handle, creating block failed.
1451 native_handle_close(cHandle);
1452 native_handle_delete(cHandle);
1453 }
1454
1455 LOG(ERROR) << "Unknown handle type in BaseBlock::pooledBlock.";
1456 return false;
1457 }
1458 default:
1459 LOG(ERROR) << "Unrecognized BaseBlock's discriminator with "
1460 << "underlying value "
1461 << underlying_value(s.getDiscriminator()) << ".";
1462 return false;
1463 }
1464 }
1465
1466 } // unnamed namespace
1467
1468 // WorkBundle -> std::list<std::unique_ptr<C2Work>>
objcpy(std::list<std::unique_ptr<C2Work>> * d,const WorkBundle & s)1469 bool objcpy(std::list<std::unique_ptr<C2Work>>* d, const WorkBundle& s) {
1470 // Convert BaseBlocks to C2BaseBlocks.
1471 std::vector<C2BaseBlock> dBaseBlocks(s.baseBlocks.size());
1472 for (size_t i = 0; i < s.baseBlocks.size(); ++i) {
1473 if (!objcpy(&dBaseBlocks[i], s.baseBlocks[i])) {
1474 LOG(ERROR) << "Invalid WorkBundle::baseBlocks["
1475 << i << "].";
1476 return false;
1477 }
1478 }
1479
1480 d->clear();
1481 for (const Work& sWork : s.works) {
1482 d->emplace_back(std::make_unique<C2Work>());
1483 C2Work& dWork = *d->back();
1484
1485 // chain info is not in use currently.
1486
1487 // input
1488 if (!objcpy(&dWork.input, sWork.input, dBaseBlocks)) {
1489 LOG(ERROR) << "Invalid Work::input.";
1490 return false;
1491 }
1492
1493 // worklet(s)
1494 dWork.worklets.clear();
1495 for (const Worklet& sWorklet : sWork.worklets) {
1496 std::unique_ptr<C2Worklet> dWorklet = std::make_unique<C2Worklet>();
1497
1498 // component id
1499 dWorklet->component = static_cast<c2_node_id_t>(
1500 sWorklet.componentId);
1501
1502 // tunings
1503 if (!copyParamsFromBlob(&dWorklet->tunings, sWorklet.tunings)) {
1504 LOG(ERROR) << "Invalid Worklet::tunings";
1505 return false;
1506 }
1507
1508 // failures
1509 dWorklet->failures.clear();
1510 dWorklet->failures.reserve(sWorklet.failures.size());
1511 for (const SettingResult& sFailure : sWorklet.failures) {
1512 std::unique_ptr<C2SettingResult> dFailure;
1513 if (!objcpy(&dFailure, sFailure)) {
1514 LOG(ERROR) << "Invalid Worklet::failures.";
1515 return false;
1516 }
1517 dWorklet->failures.emplace_back(std::move(dFailure));
1518 }
1519
1520 // output
1521 if (!objcpy(&dWorklet->output, sWorklet.output, dBaseBlocks)) {
1522 LOG(ERROR) << "Invalid Worklet::output.";
1523 return false;
1524 }
1525
1526 dWork.worklets.emplace_back(std::move(dWorklet));
1527 }
1528
1529 // workletsProcessed
1530 dWork.workletsProcessed = sWork.workletsProcessed;
1531
1532 // result
1533 dWork.result = static_cast<c2_status_t>(sWork.result);
1534 }
1535
1536 return true;
1537 }
1538
1539 constexpr size_t PARAMS_ALIGNMENT = 8; // 64-bit alignment
1540 static_assert(PARAMS_ALIGNMENT % alignof(C2Param) == 0, "C2Param alignment mismatch");
1541 static_assert(PARAMS_ALIGNMENT % alignof(C2Info) == 0, "C2Param alignment mismatch");
1542 static_assert(PARAMS_ALIGNMENT % alignof(C2Tuning) == 0, "C2Param alignment mismatch");
1543
1544 // Params -> std::vector<C2Param*>
parseParamsBlob(std::vector<C2Param * > * params,const hidl_vec<uint8_t> & blob)1545 bool parseParamsBlob(std::vector<C2Param*> *params, const hidl_vec<uint8_t> &blob) {
1546 // assuming blob is const here
1547 size_t size = blob.size();
1548 size_t ix = 0;
1549 const uint8_t *data = blob.data();
1550 C2Param *p = nullptr;
1551
1552 do {
1553 p = C2ParamUtils::ParseFirst(data + ix, size - ix);
1554 if (p) {
1555 params->emplace_back(p);
1556 ix += p->size();
1557 ix = align(ix, PARAMS_ALIGNMENT);
1558 }
1559 } while (p);
1560
1561 if (ix != size) {
1562 LOG(ERROR) << "parseParamsBlob -- inconsistent sizes.";
1563 return false;
1564 }
1565 return true;
1566 }
1567
1568 namespace /* unnamed */ {
1569
1570 /**
1571 * Concatenates a list of C2Params into a params blob. T is a container type
1572 * whose member type is compatible with C2Param*.
1573 *
1574 * \param[out] blob target blob
1575 * \param[in] params parameters to concatenate
1576 * \retval C2_OK if the blob was successfully created
1577 * \retval C2_BAD_VALUE if the blob was not successful created (this only
1578 * happens if the parameters were not const)
1579 */
1580 template <typename T>
_createParamsBlob(hidl_vec<uint8_t> * blob,const T & params)1581 bool _createParamsBlob(hidl_vec<uint8_t> *blob, const T ¶ms) {
1582 // assuming the parameter values are const
1583 size_t size = 0;
1584 for (const auto &p : params) {
1585 if (!p) {
1586 continue;
1587 }
1588 size += p->size();
1589 size = align(size, PARAMS_ALIGNMENT);
1590 }
1591 blob->resize(size);
1592 size_t ix = 0;
1593 for (const auto &p : params) {
1594 if (!p) {
1595 continue;
1596 }
1597 // NEVER overwrite even if param values (e.g. size) changed
1598 size_t paramSize = std::min(p->size(), size - ix);
1599 std::copy(
1600 reinterpret_cast<const uint8_t*>(&*p),
1601 reinterpret_cast<const uint8_t*>(&*p) + paramSize,
1602 &(*blob)[ix]);
1603 ix += paramSize;
1604 ix = align(ix, PARAMS_ALIGNMENT);
1605 }
1606 blob->resize(ix);
1607 if (ix != size) {
1608 LOG(ERROR) << "createParamsBlob -- inconsistent sizes.";
1609 return false;
1610 }
1611 return true;
1612 }
1613
1614 /**
1615 * Parses a params blob and create a vector of new T objects that contain copies
1616 * of the params in the blob. T is C2Param or its compatible derived class.
1617 *
1618 * \param[out] params the resulting vector
1619 * \param[in] blob parameter blob to parse
1620 * \retval C2_OK if the full blob was parsed and params was constructed
1621 * \retval C2_BAD_VALUE otherwise
1622 */
1623 template <typename T>
_copyParamsFromBlob(std::vector<std::unique_ptr<T>> * params,Params blob)1624 bool _copyParamsFromBlob(
1625 std::vector<std::unique_ptr<T>>* params,
1626 Params blob) {
1627
1628 std::vector<C2Param*> paramPointers;
1629 if (!parseParamsBlob(¶mPointers, blob)) {
1630 LOG(ERROR) << "copyParamsFromBlob -- failed to parse.";
1631 return false;
1632 }
1633
1634 params->resize(paramPointers.size());
1635 size_t i = 0;
1636 for (C2Param* const& paramPointer : paramPointers) {
1637 if (!paramPointer) {
1638 LOG(ERROR) << "copyParamsFromBlob -- null paramPointer.";
1639 return false;
1640 }
1641 (*params)[i++].reset(reinterpret_cast<T*>(
1642 C2Param::Copy(*paramPointer).release()));
1643 }
1644 return true;
1645 }
1646
1647 } // unnamed namespace
1648
1649 // std::vector<const C2Param*> -> Params
createParamsBlob(hidl_vec<uint8_t> * blob,const std::vector<const C2Param * > & params)1650 bool createParamsBlob(
1651 hidl_vec<uint8_t> *blob,
1652 const std::vector<const C2Param*> ¶ms) {
1653 return _createParamsBlob(blob, params);
1654 }
1655
1656 // std::vector<C2Param*> -> Params
createParamsBlob(hidl_vec<uint8_t> * blob,const std::vector<C2Param * > & params)1657 bool createParamsBlob(
1658 hidl_vec<uint8_t> *blob,
1659 const std::vector<C2Param*> ¶ms) {
1660 return _createParamsBlob(blob, params);
1661 }
1662
1663 // std::vector<std::unique_ptr<C2Param>> -> Params
createParamsBlob(hidl_vec<uint8_t> * blob,const std::vector<std::unique_ptr<C2Param>> & params)1664 bool createParamsBlob(
1665 hidl_vec<uint8_t> *blob,
1666 const std::vector<std::unique_ptr<C2Param>> ¶ms) {
1667 return _createParamsBlob(blob, params);
1668 }
1669
1670 // std::vector<std::unique_ptr<C2Tuning>> -> Params
createParamsBlob(hidl_vec<uint8_t> * blob,const std::vector<std::unique_ptr<C2Tuning>> & params)1671 bool createParamsBlob(
1672 hidl_vec<uint8_t> *blob,
1673 const std::vector<std::unique_ptr<C2Tuning>> ¶ms) {
1674 return _createParamsBlob(blob, params);
1675 }
1676
1677 // std::vector<std::shared_ptr<const C2Info>> -> Params
createParamsBlob(hidl_vec<uint8_t> * blob,const std::vector<std::shared_ptr<const C2Info>> & params)1678 bool createParamsBlob(
1679 hidl_vec<uint8_t> *blob,
1680 const std::vector<std::shared_ptr<const C2Info>> ¶ms) {
1681 return _createParamsBlob(blob, params);
1682 }
1683
1684 // Params -> std::vector<std::unique_ptr<C2Param>>
copyParamsFromBlob(std::vector<std::unique_ptr<C2Param>> * params,Params blob)1685 bool copyParamsFromBlob(
1686 std::vector<std::unique_ptr<C2Param>>* params,
1687 Params blob) {
1688 return _copyParamsFromBlob(params, blob);
1689 }
1690
1691 // Params -> std::vector<std::unique_ptr<C2Tuning>>
copyParamsFromBlob(std::vector<std::unique_ptr<C2Tuning>> * params,Params blob)1692 bool copyParamsFromBlob(
1693 std::vector<std::unique_ptr<C2Tuning>>* params,
1694 Params blob) {
1695 return _copyParamsFromBlob(params, blob);
1696 }
1697
1698 // Params -> update std::vector<std::unique_ptr<C2Param>>
updateParamsFromBlob(const std::vector<C2Param * > & params,const Params & blob)1699 bool updateParamsFromBlob(
1700 const std::vector<C2Param*>& params,
1701 const Params& blob) {
1702 std::unordered_map<uint32_t, C2Param*> index2param;
1703 for (C2Param* const& param : params) {
1704 if (!param) {
1705 LOG(ERROR) << "updateParamsFromBlob -- null output param.";
1706 return false;
1707 }
1708 if (index2param.find(param->index()) == index2param.end()) {
1709 index2param.emplace(param->index(), param);
1710 }
1711 }
1712
1713 std::vector<C2Param*> paramPointers;
1714 if (!parseParamsBlob(¶mPointers, blob)) {
1715 LOG(ERROR) << "updateParamsFromBlob -- failed to parse.";
1716 return false;
1717 }
1718
1719 for (C2Param* const& paramPointer : paramPointers) {
1720 if (!paramPointer) {
1721 LOG(ERROR) << "updateParamsFromBlob -- null input param.";
1722 return false;
1723 }
1724 decltype(index2param)::iterator i = index2param.find(
1725 paramPointer->index());
1726 if (i == index2param.end()) {
1727 LOG(DEBUG) << "updateParamsFromBlob -- index "
1728 << paramPointer->index() << " not found. Skipping...";
1729 continue;
1730 }
1731 if (!i->second->updateFrom(*paramPointer)) {
1732 LOG(ERROR) << "updateParamsFromBlob -- size mismatch: "
1733 << params.size() << " vs " << paramPointer->size()
1734 << " (index = " << i->first << ").";
1735 return false;
1736 }
1737 }
1738 return true;
1739 }
1740
1741 // Convert BufferPool ResultStatus to c2_status_t.
toC2Status(ResultStatus rs)1742 c2_status_t toC2Status(ResultStatus rs) {
1743 switch (rs) {
1744 case ResultStatus::OK:
1745 return C2_OK;
1746 case ResultStatus::NO_MEMORY:
1747 return C2_NO_MEMORY;
1748 case ResultStatus::ALREADY_EXISTS:
1749 return C2_DUPLICATE;
1750 case ResultStatus::NOT_FOUND:
1751 return C2_NOT_FOUND;
1752 case ResultStatus::CRITICAL_ERROR:
1753 return C2_CORRUPTED;
1754 default:
1755 LOG(WARNING) << "Unrecognized BufferPool ResultStatus: "
1756 << static_cast<int32_t>(rs) << ".";
1757 return C2_CORRUPTED;
1758 }
1759 }
1760
1761 namespace /* unnamed */ {
1762
1763 template <typename BlockProcessor>
forEachBlock(C2FrameData & frameData,BlockProcessor process)1764 void forEachBlock(C2FrameData& frameData,
1765 BlockProcessor process) {
1766 for (const std::shared_ptr<C2Buffer>& buffer : frameData.buffers) {
1767 if (buffer) {
1768 for (const C2ConstGraphicBlock& block :
1769 buffer->data().graphicBlocks()) {
1770 process(block);
1771 }
1772 }
1773 }
1774 }
1775
1776 template <typename BlockProcessor>
forEachBlock(const std::list<std::unique_ptr<C2Work>> & workList,BlockProcessor process,bool processInput,bool processOutput)1777 void forEachBlock(const std::list<std::unique_ptr<C2Work>>& workList,
1778 BlockProcessor process,
1779 bool processInput, bool processOutput) {
1780 for (const std::unique_ptr<C2Work>& work : workList) {
1781 if (!work) {
1782 continue;
1783 }
1784 if (processInput) {
1785 forEachBlock(work->input, process);
1786 }
1787 if (processOutput) {
1788 for (const std::unique_ptr<C2Worklet>& worklet : work->worklets) {
1789 if (worklet) {
1790 forEachBlock(worklet->output,
1791 process);
1792 }
1793 }
1794 }
1795 }
1796 }
1797
1798 } // unnamed namespace
1799
beginTransferBufferQueueBlock(const C2ConstGraphicBlock & block)1800 bool beginTransferBufferQueueBlock(const C2ConstGraphicBlock& block) {
1801 std::shared_ptr<_C2BlockPoolData> data =
1802 _C2BlockFactory::GetGraphicBlockPoolData(block);
1803 if (data && _C2BlockFactory::GetBufferQueueData(data)) {
1804 _C2BlockFactory::BeginTransferBlockToClient(data);
1805 return true;
1806 }
1807 return false;
1808 }
1809
beginTransferBufferQueueBlocks(const std::list<std::unique_ptr<C2Work>> & workList,bool processInput,bool processOutput)1810 void beginTransferBufferQueueBlocks(
1811 const std::list<std::unique_ptr<C2Work>>& workList,
1812 bool processInput, bool processOutput) {
1813 forEachBlock(workList, beginTransferBufferQueueBlock,
1814 processInput, processOutput);
1815 }
1816
endTransferBufferQueueBlock(const C2ConstGraphicBlock & block,bool transfer)1817 bool endTransferBufferQueueBlock(
1818 const C2ConstGraphicBlock& block,
1819 bool transfer) {
1820 std::shared_ptr<_C2BlockPoolData> data =
1821 _C2BlockFactory::GetGraphicBlockPoolData(block);
1822 if (data && _C2BlockFactory::GetBufferQueueData(data)) {
1823 _C2BlockFactory::EndTransferBlockToClient(data, transfer);
1824 return true;
1825 }
1826 return false;
1827 }
1828
endTransferBufferQueueBlocks(const std::list<std::unique_ptr<C2Work>> & workList,bool transfer,bool processInput,bool processOutput)1829 void endTransferBufferQueueBlocks(
1830 const std::list<std::unique_ptr<C2Work>>& workList,
1831 bool transfer,
1832 bool processInput, bool processOutput) {
1833 forEachBlock(workList,
1834 std::bind(endTransferBufferQueueBlock,
1835 std::placeholders::_1, transfer),
1836 processInput, processOutput);
1837 }
1838
displayBufferQueueBlock(const C2ConstGraphicBlock & block)1839 bool displayBufferQueueBlock(const C2ConstGraphicBlock& block) {
1840 std::shared_ptr<_C2BlockPoolData> data =
1841 _C2BlockFactory::GetGraphicBlockPoolData(block);
1842 if (data && _C2BlockFactory::GetBufferQueueData(data)) {
1843 _C2BlockFactory::DisplayBlockToBufferQueue(data);
1844 return true;
1845 }
1846 return false;
1847 }
1848
1849 } // namespace utils
1850 } // namespace V1_0
1851 } // namespace c2
1852 } // namespace media
1853 } // namespace hardware
1854 } // namespace android
1855
1856