1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef C2PARAM_H_
18 #define C2PARAM_H_
19
20 #include <C2.h>
21
22 #include <stdbool.h>
23 #include <stdint.h>
24
25 #include <algorithm>
26 #include <string>
27 #include <type_traits>
28 #include <utility>
29 #include <vector>
30
31 /// \addtogroup Parameters
32 /// @{
33
34 /// \defgroup internal Internal helpers.
35
36 /*!
37 * \file
38 * PARAMETERS: SETTINGs, TUNINGs, and INFOs
39 * ===
40 *
41 * These represent miscellaneous control and metadata information and are likely copied into
42 * kernel space. Therefore, these are C-like structures designed to carry just a small amount of
43 * information. We are using C++ to be able to add constructors, as well as non-virtual and class
44 * methods.
45 *
46 * ==Specification details:
47 *
48 * Restrictions:
49 * - must be POD struct, e.g. no vtable (no virtual destructor)
50 * - must have the same size in 64-bit and 32-bit mode (no size_t)
51 * - as such, no pointer members
52 * - some common member field names are reserved as they are defined as methods for all
53 * parameters:
54 * they are: size, type, kind, index and stream
55 *
56 * Behavior:
57 * - Params can be global (not related to input or output), related to input or output,
58 * or related to an input/output stream.
59 * - All params are queried/set using a unique param index, which incorporates a potential stream
60 * index and/or port.
61 * - Querying (supported) params MUST never fail.
62 * - All params MUST have default values.
63 * - If some fields have "unsupported" or "invalid" values during setting, this SHOULD be
64 * communicated to the app.
65 * a) Ideally, this should be avoided. When setting parameters, in general, component should do
66 * "best effort" to apply all settings. It should change "invalid/unsupported" values to the
67 * nearest supported values.
68 * - This is communicated to the client by changing the source values in tune()/
69 * configure().
70 * b) If falling back to a supported value is absolutely impossible, the component SHALL return
71 * an error for the specific setting, but should continue to apply other settings.
72 * TODO: this currently may result in unintended results.
73 *
74 * **NOTE:** unlike OMX, params are not versioned. Instead, a new struct with new param index
75 * SHALL be added as new versions are required.
76 *
77 * The proper subtype (Setting, Info or Param) is incorporated into the class type. Define structs
78 * to define multiple subtyped versions of related parameters.
79 *
80 * ==Implementation details:
81 *
82 * - Use macros to define parameters
83 * - All parameters must have a default constructor
84 * - This is only used for instantiating the class in source (e.g. will not be used
85 * when building a parameter by the framework from key/value pairs.)
86 */
87
88 /// \ingroup internal
89
90 /**
91 * Parameter base class.
92 */
93 struct C2Param {
94 // param index encompasses the following:
95 //
96 // - kind (setting, tuning, info, struct)
97 // - scope
98 // - direction (global, input, output)
99 // - stream flag
100 // - stream ID (usually 0)
101 // - and the parameter's type (core index)
102 // - flexible parameter flag
103 // - vendor extension flag
104 // - type index (this includes the vendor extension flag)
105 //
106 // layout:
107 //
108 // kind : <------- scope -------> : <----- core index ----->
109 // +------+-----+---+------+--------+----|------+--------------+
110 // | kind | dir | - |stream|streamID|flex|vendor| type index |
111 // +------+-----+---+------+--------+----+------+--------------+
112 // bit: 31..30 29.28 25 24 .. 17 16 15 14 .. 0
113 //
114 public:
115 /**
116 * C2Param kinds, usable as bitmaps.
117 */
118 enum kind_t : uint32_t {
119 NONE = 0,
120 STRUCT = (1 << 0),
121 INFO = (1 << 1),
122 SETTING = (1 << 2),
123 TUNING = (1 << 3) | SETTING, // tunings are settings
124 };
125
126 /**
127 * The parameter type index specifies the underlying parameter type of a parameter as
128 * an integer value.
129 *
130 * Parameter types are divided into two groups: platform types and vendor types.
131 *
132 * Platform types are defined by the platform and are common for all implementations.
133 *
134 * Vendor types are defined by each vendors, so they may differ between implementations.
135 * It is recommended that vendor types be the same for all implementations by a specific
136 * vendor.
137 */
138 typedef uint32_t type_index_t;
139 enum : uint32_t {
140 TYPE_INDEX_VENDOR_START = 0x00008000, ///< vendor indices SHALL start after this
141 };
142
143 /**
144 * Core index is the underlying parameter type for a parameter. It is used to describe the
145 * layout of the parameter structure regardless of the component or parameter kind/scope.
146 *
147 * It is used to identify and distinguish global parameters, and also parameters on a given
148 * port or stream. They must be unique for the set of global parameters, as well as for the
149 * set of parameters on each port or each stream, but the same core index can be used for
150 * parameters on different streams or ports, as well as for global parameters and port/stream
151 * parameters.
152 *
153 * Multiple parameter types can share the same layout.
154 *
155 * \note The layout for all parameters with the same core index across all components must
156 * be identical.
157 */
158 struct CoreIndex {
159 //public:
160 enum : uint32_t {
161 IS_FLEX_FLAG = 0x00010000,
162 IS_REQUEST_FLAG = 0x00020000,
163 };
164
165 protected:
166 enum : uint32_t {
167 KIND_MASK = 0xC0000000,
168 KIND_STRUCT = 0x00000000,
169 KIND_TUNING = 0x40000000,
170 KIND_SETTING = 0x80000000,
171 KIND_INFO = 0xC0000000,
172
173 DIR_MASK = 0x30000000,
174 DIR_GLOBAL = 0x20000000,
175 DIR_UNDEFINED = DIR_MASK, // MUST have all bits set
176 DIR_INPUT = 0x00000000,
177 DIR_OUTPUT = 0x10000000,
178
179 IS_STREAM_FLAG = 0x02000000,
180 STREAM_ID_MASK = 0x01F00000,
181 STREAM_ID_SHIFT = 20,
182 MAX_STREAM_ID = STREAM_ID_MASK >> STREAM_ID_SHIFT,
183 STREAM_MASK = IS_STREAM_FLAG | STREAM_ID_MASK,
184
185 IS_VENDOR_FLAG = 0x00008000,
186 TYPE_INDEX_MASK = 0x0000FFFF,
187 CORE_MASK = TYPE_INDEX_MASK | IS_FLEX_FLAG,
188 };
189
190 public:
191 /// constructor/conversion from uint32_t
CoreIndexC2Param::CoreIndex192 inline CoreIndex(uint32_t index) : mIndex(index) { }
193
194 // no conversion from uint64_t
195 inline CoreIndex(uint64_t index) = delete;
196
197 /// returns true iff this is a vendor extension parameter
isVendorC2Param::CoreIndex198 inline bool isVendor() const { return mIndex & IS_VENDOR_FLAG; }
199
200 /// returns true iff this is a flexible parameter (with variable size)
isFlexibleC2Param::CoreIndex201 inline bool isFlexible() const { return mIndex & IS_FLEX_FLAG; }
202
203 /// returns the core index
204 /// This is the combination of the parameter type index and the flexible flag.
coreIndexC2Param::CoreIndex205 inline uint32_t coreIndex() const { return mIndex & CORE_MASK; }
206
207 /// returns the parameter type index
typeIndexC2Param::CoreIndex208 inline type_index_t typeIndex() const { return mIndex & TYPE_INDEX_MASK; }
209
210 DEFINE_FIELD_AND_MASK_BASED_COMPARISON_OPERATORS(CoreIndex, mIndex, CORE_MASK)
211
212 protected:
213 uint32_t mIndex;
214 };
215
216 /**
217 * Type encompasses the parameter's kind (tuning, setting, info), its scope (whether the
218 * parameter is global, input or output, and whether it is for a stream) and the its base
219 * index (which also determines its layout).
220 */
221 struct Type : public CoreIndex {
222 //public:
223 /// returns true iff this is a global parameter (not for input nor output)
isGlobalC2Param::Type224 inline bool isGlobal() const { return (mIndex & DIR_MASK) == DIR_GLOBAL; }
225 /// returns true iff this is an input or input stream parameter
forInputC2Param::Type226 inline bool forInput() const { return (mIndex & DIR_MASK) == DIR_INPUT; }
227 /// returns true iff this is an output or output stream parameter
forOutputC2Param::Type228 inline bool forOutput() const { return (mIndex & DIR_MASK) == DIR_OUTPUT; }
229
230 /// returns true iff this is a stream parameter
forStreamC2Param::Type231 inline bool forStream() const { return mIndex & IS_STREAM_FLAG; }
232 /// returns true iff this is a port (input or output) parameter
forPortC2Param::Type233 inline bool forPort() const { return !forStream() && !isGlobal(); }
234
235 /// returns the parameter type: the parameter index without the stream ID
typeC2Param::Type236 inline uint32_t type() const { return mIndex & (~STREAM_ID_MASK); }
237
238 /// return the kind (struct, info, setting or tuning) of this param
kindC2Param::Type239 inline kind_t kind() const {
240 switch (mIndex & KIND_MASK) {
241 case KIND_STRUCT: return STRUCT;
242 case KIND_INFO: return INFO;
243 case KIND_SETTING: return SETTING;
244 case KIND_TUNING: return TUNING;
245 default: return NONE; // should not happen
246 }
247 }
248
249 /// constructor/conversion from uint32_t
TypeC2Param::Type250 inline Type(uint32_t index) : CoreIndex(index) { }
251
252 // no conversion from uint64_t
253 inline Type(uint64_t index) = delete;
254
255 DEFINE_FIELD_AND_MASK_BASED_COMPARISON_OPERATORS(Type, mIndex, ~STREAM_ID_MASK)
256
257 private:
258 friend struct C2Param; // for setPort()
259 friend struct C2Tuning; // for KIND_TUNING
260 friend struct C2Setting; // for KIND_SETTING
261 friend struct C2Info; // for KIND_INFO
262 // for DIR_GLOBAL
263 template<typename T, typename S, int I, class F> friend struct C2GlobalParam;
264 template<typename T, typename S, int I, class F> friend struct C2PortParam; // for kDir*
265 template<typename T, typename S, int I, class F> friend struct C2StreamParam; // for kDir*
266 friend struct _C2ParamInspector; // for testing
267
268 /**
269 * Sets the port/stream direction.
270 * @return true on success, false if could not set direction (e.g. it is global param).
271 */
setPortC2Param::Type272 inline bool setPort(bool output) {
273 if (isGlobal()) {
274 return false;
275 } else {
276 mIndex = (mIndex & ~DIR_MASK) | (output ? DIR_OUTPUT : DIR_INPUT);
277 return true;
278 }
279 }
280 };
281
282 /**
283 * index encompasses all remaining information: basically the stream ID.
284 */
285 struct Index : public Type {
286 /// returns the index as uint32_t
uint32_tC2Param::Index287 inline operator uint32_t() const { return mIndex; }
288
289 /// constructor/conversion from uint32_t
IndexC2Param::Index290 inline Index(uint32_t index) : Type(index) { }
291
292 /// copy constructor
293 inline Index(const Index &index) = default;
294
295 // no conversion from uint64_t
296 inline Index(uint64_t index) = delete;
297
298 /// returns the stream ID or ~0 if not a stream
streamC2Param::Index299 inline unsigned stream() const {
300 return forStream() ? rawStream() : ~0U;
301 }
302
303 /// Returns an index with stream field set to given stream.
withStreamC2Param::Index304 inline Index withStream(unsigned stream) const {
305 Index ix = mIndex;
306 (void)ix.setStream(stream);
307 return ix;
308 }
309
310 /// sets the port (direction). Returns true iff successful.
withPortC2Param::Index311 inline Index withPort(bool output) const {
312 Index ix = mIndex;
313 (void)ix.setPort(output);
314 return ix;
315 }
316
317 DEFINE_FIELD_BASED_COMPARISON_OPERATORS(Index, mIndex)
318
319 private:
320 friend class C2InfoBuffer; // for convertTo*
321 friend struct C2Param; // for setStream, MakeStreamId, isValid, convertTo*
322 friend struct _C2ParamInspector; // for testing
323
324 /**
325 * @return true if the type is valid, e.g. direction is not undefined AND
326 * stream is 0 if not a stream param.
327 */
isValidC2Param::Index328 inline bool isValid() const {
329 // there is no Type::isValid (even though some of this check could be
330 // performed on types) as this is only used on index...
331 return (forStream() ? rawStream() < MAX_STREAM_ID : rawStream() == 0)
332 && (mIndex & DIR_MASK) != DIR_UNDEFINED;
333 }
334
335 /// returns the raw stream ID field
rawStreamC2Param::Index336 inline unsigned rawStream() const {
337 return (mIndex & STREAM_ID_MASK) >> STREAM_ID_SHIFT;
338 }
339
340 /// returns the streamId bitfield for a given |stream|. If stream is invalid,
341 /// returns an invalid bitfield.
MakeStreamIdC2Param::Index342 inline static uint32_t MakeStreamId(unsigned stream) {
343 // saturate stream ID (max value is invalid)
344 if (stream > MAX_STREAM_ID) {
345 stream = MAX_STREAM_ID;
346 }
347 return (stream << STREAM_ID_SHIFT) & STREAM_ID_MASK;
348 }
349
convertToStreamC2Param::Index350 inline bool convertToStream(bool output, unsigned stream) {
351 mIndex = (mIndex & ~DIR_MASK) | IS_STREAM_FLAG;
352 (void)setPort(output);
353 return setStream(stream);
354 }
355
convertToPortC2Param::Index356 inline void convertToPort(bool output) {
357 mIndex = (mIndex & ~(DIR_MASK | IS_STREAM_FLAG));
358 (void)setPort(output);
359 }
360
convertToGlobalC2Param::Index361 inline void convertToGlobal() {
362 mIndex = (mIndex & ~(DIR_MASK | IS_STREAM_FLAG)) | DIR_GLOBAL;
363 }
364
convertToRequestC2Param::Index365 inline void convertToRequest() {
366 mIndex = mIndex | IS_REQUEST_FLAG;
367 }
368
369 /**
370 * Sets the stream index.
371 * \return true on success, false if could not set index (e.g. not a stream param).
372 */
setStreamC2Param::Index373 inline bool setStream(unsigned stream) {
374 if (forStream()) {
375 mIndex = (mIndex & ~STREAM_ID_MASK) | MakeStreamId(stream);
376 return this->stream() < MAX_STREAM_ID;
377 }
378 return false;
379 }
380 };
381
382 public:
383 // public getters for Index methods
384
385 /// returns true iff this is a vendor extension parameter
isVendorC2Param386 inline bool isVendor() const { return _mIndex.isVendor(); }
387 /// returns true iff this is a flexible parameter
isFlexibleC2Param388 inline bool isFlexible() const { return _mIndex.isFlexible(); }
389 /// returns true iff this is a global parameter (not for input nor output)
isGlobalC2Param390 inline bool isGlobal() const { return _mIndex.isGlobal(); }
391 /// returns true iff this is an input or input stream parameter
forInputC2Param392 inline bool forInput() const { return _mIndex.forInput(); }
393 /// returns true iff this is an output or output stream parameter
forOutputC2Param394 inline bool forOutput() const { return _mIndex.forOutput(); }
395
396 /// returns true iff this is a stream parameter
forStreamC2Param397 inline bool forStream() const { return _mIndex.forStream(); }
398 /// returns true iff this is a port (input or output) parameter
forPortC2Param399 inline bool forPort() const { return _mIndex.forPort(); }
400
401 /// returns the stream ID or ~0 if not a stream
streamC2Param402 inline unsigned stream() const { return _mIndex.stream(); }
403
404 /// returns the parameter type: the parameter index without the stream ID
typeC2Param405 inline Type type() const { return _mIndex.type(); }
406
407 /// returns the index of this parameter
408 /// \todo: should we restrict this to C2ParamField?
indexC2Param409 inline uint32_t index() const { return (uint32_t)_mIndex; }
410
411 /// returns the core index of this parameter
coreIndexC2Param412 inline CoreIndex coreIndex() const { return _mIndex.coreIndex(); }
413
414 /// returns the kind of this parameter
kindC2Param415 inline kind_t kind() const { return _mIndex.kind(); }
416
417 /// returns the size of the parameter or 0 if the parameter is invalid
sizeC2Param418 inline size_t size() const { return _mSize; }
419
420 /// returns true iff the parameter is valid
421 inline operator bool() const { return _mIndex.isValid() && _mSize > 0; }
422
423 /// returns true iff the parameter is invalid
424 inline bool operator!() const { return !operator bool(); }
425
426 // equality is done by memcmp (use equals() to prevent any overread)
427 inline bool operator==(const C2Param &o) const {
428 return equals(o) && memcmp(this, &o, _mSize) == 0;
429 }
430 inline bool operator!=(const C2Param &o) const { return !operator==(o); }
431
432 /// safe(r) type cast from pointer and size
FromC2Param433 inline static C2Param* From(void *addr, size_t len) {
434 // _mSize must fit into size, but really C2Param must also to be a valid param
435 if (len < sizeof(C2Param)) {
436 return nullptr;
437 }
438 // _mSize must match length
439 C2Param *param = (C2Param*)addr;
440 if (param->_mSize != len) {
441 return nullptr;
442 }
443 return param;
444 }
445
446 /// Returns managed clone of |orig| at heap.
CopyC2Param447 inline static std::unique_ptr<C2Param> Copy(const C2Param &orig) {
448 if (orig.size() == 0) {
449 return nullptr;
450 }
451 void *mem = ::operator new (orig.size());
452 C2Param *param = new (mem) C2Param(orig.size(), orig._mIndex);
453 param->updateFrom(orig);
454 return std::unique_ptr<C2Param>(param);
455 }
456
457 /// Returns managed clone of |orig| as a stream parameter at heap.
CopyAsStreamC2Param458 inline static std::unique_ptr<C2Param> CopyAsStream(
459 const C2Param &orig, bool output, unsigned stream) {
460 std::unique_ptr<C2Param> copy = Copy(orig);
461 if (copy) {
462 copy->_mIndex.convertToStream(output, stream);
463 }
464 return copy;
465 }
466
467 /// Returns managed clone of |orig| as a port parameter at heap.
CopyAsPortC2Param468 inline static std::unique_ptr<C2Param> CopyAsPort(const C2Param &orig, bool output) {
469 std::unique_ptr<C2Param> copy = Copy(orig);
470 if (copy) {
471 copy->_mIndex.convertToPort(output);
472 }
473 return copy;
474 }
475
476 /// Returns managed clone of |orig| as a global parameter at heap.
CopyAsGlobalC2Param477 inline static std::unique_ptr<C2Param> CopyAsGlobal(const C2Param &orig) {
478 std::unique_ptr<C2Param> copy = Copy(orig);
479 if (copy) {
480 copy->_mIndex.convertToGlobal();
481 }
482 return copy;
483 }
484
485 /// Returns managed clone of |orig| as a stream parameter at heap.
CopyAsRequestC2Param486 inline static std::unique_ptr<C2Param> CopyAsRequest(const C2Param &orig) {
487 std::unique_ptr<C2Param> copy = Copy(orig);
488 if (copy) {
489 copy->_mIndex.convertToRequest();
490 }
491 return copy;
492 }
493
494 #if 0
495 template<typename P, class=decltype(C2Param(P()))>
AsC2Param496 P *As() { return P::From(this); }
497 template<typename P>
AsC2Param498 const P *As() const { return const_cast<const P*>(P::From(const_cast<C2Param*>(this))); }
499 #endif
500
501 protected:
502 /// sets the stream field. Returns true iff successful.
setStreamC2Param503 inline bool setStream(unsigned stream) {
504 return _mIndex.setStream(stream);
505 }
506
507 /// sets the port (direction). Returns true iff successful.
setPortC2Param508 inline bool setPort(bool output) {
509 return _mIndex.setPort(output);
510 }
511
512 /// sets the size of this parameter.
setSizeC2Param513 inline void setSize(size_t size) {
514 if (size < sizeof(C2Param)) {
515 size = 0;
516 }
517 _mSize = c2_min(size, _mSize);
518 }
519
520 public:
521 /// invalidate this parameter. There is no recovery from this call; e.g. parameter
522 /// cannot be 'corrected' to be valid.
invalidateC2Param523 inline void invalidate() { _mSize = 0; }
524
525 // if other is the same kind of (valid) param as this, copy it into this and return true.
526 // otherwise, do not copy anything, and return false.
updateFromC2Param527 inline bool updateFrom(const C2Param &other) {
528 if (other._mSize <= _mSize && other._mIndex == _mIndex && _mSize > 0) {
529 memcpy(this, &other, other._mSize);
530 return true;
531 }
532 return false;
533 }
534
535 protected:
536 // returns |o| if it is a null ptr, or if can suitably be a param of given |type| (e.g. has
537 // same type (ignoring stream ID), and size). Otherwise, returns null. If |checkDir| is false,
538 // allow undefined or different direction (e.g. as constructed from C2PortParam() vs.
539 // C2PortParam::input), but still require equivalent type (stream, port or global); otherwise,
540 // return null.
541 inline static const C2Param* IfSuitable(
542 const C2Param* o, size_t size, Type type, size_t flexSize = 0, bool checkDir = true) {
543 if (o == nullptr || o->_mSize < size || (flexSize && ((o->_mSize - size) % flexSize))) {
544 return nullptr;
545 } else if (checkDir) {
546 return o->_mIndex.type() == type.mIndex ? o : nullptr;
547 } else if (o->_mIndex.isGlobal()) {
548 return nullptr;
549 } else {
550 return ((o->_mIndex.type() ^ type.mIndex) & ~Type::DIR_MASK) ? nullptr : o;
551 }
552 }
553
554 /// base constructor
C2ParamC2Param555 inline C2Param(uint32_t paramSize, Index paramIndex)
556 : _mSize(paramSize),
557 _mIndex(paramIndex) {
558 if (paramSize > sizeof(C2Param)) {
559 memset(this + 1, 0, paramSize - sizeof(C2Param));
560 }
561 }
562
563 /// base constructor with stream set
C2ParamC2Param564 inline C2Param(uint32_t paramSize, Index paramIndex, unsigned stream)
565 : _mSize(paramSize),
566 _mIndex(paramIndex | Index::MakeStreamId(stream)) {
567 if (paramSize > sizeof(C2Param)) {
568 memset(this + 1, 0, paramSize - sizeof(C2Param));
569 }
570 if (!forStream()) {
571 invalidate();
572 }
573 }
574
575 private:
576 friend struct _C2ParamInspector; // for testing
577
578 /// returns true iff |o| has the same size and index as this. This performs the
579 /// basic check for equality.
equalsC2Param580 inline bool equals(const C2Param &o) const {
581 return _mSize == o._mSize && _mIndex == o._mIndex;
582 }
583
584 uint32_t _mSize;
585 Index _mIndex;
586 };
587
588 /// \ingroup internal
589 /// allow C2Params access to private methods, e.g. constructors
590 #define C2PARAM_MAKE_FRIENDS \
591 template<typename U, typename S, int I, class F> friend struct C2GlobalParam; \
592 template<typename U, typename S, int I, class F> friend struct C2PortParam; \
593 template<typename U, typename S, int I, class F> friend struct C2StreamParam; \
594
595 /**
596 * Setting base structure for component method signatures. Wrap constructors.
597 */
598 struct C2Setting : public C2Param {
599 protected:
600 template<typename ...Args>
C2SettingC2Setting601 inline C2Setting(const Args(&... args)) : C2Param(args...) { }
602 public: // TODO
603 enum : uint32_t { PARAM_KIND = Type::KIND_SETTING };
604 };
605
606 /**
607 * Tuning base structure for component method signatures. Wrap constructors.
608 */
609 struct C2Tuning : public C2Setting {
610 protected:
611 template<typename ...Args>
C2TuningC2Tuning612 inline C2Tuning(const Args(&... args)) : C2Setting(args...) { }
613 public: // TODO
614 enum : uint32_t { PARAM_KIND = Type::KIND_TUNING };
615 };
616
617 /**
618 * Info base structure for component method signatures. Wrap constructors.
619 */
620 struct C2Info : public C2Param {
621 protected:
622 template<typename ...Args>
C2InfoC2Info623 inline C2Info(const Args(&... args)) : C2Param(args...) { }
624 public: // TODO
625 enum : uint32_t { PARAM_KIND = Type::KIND_INFO };
626 };
627
628 /**
629 * Structure uniquely specifying a field in an arbitrary structure.
630 *
631 * \note This structure is used differently in C2FieldDescriptor to
632 * identify array fields, such that _mSize is the size of each element. This is
633 * because the field descriptor contains the array-length, and we want to keep
634 * a relevant element size for variable length arrays.
635 */
636 struct _C2FieldId {
637 //public:
638 /**
639 * Constructor used for C2FieldDescriptor that removes the array extent.
640 *
641 * \param[in] offset pointer to the field in an object at address 0.
642 */
643 template<typename T, class B=typename std::remove_extent<T>::type>
_C2FieldId_C2FieldId644 inline _C2FieldId(T* offset)
645 : // offset is from "0" so will fit on 32-bits
646 _mOffset((uint32_t)(uintptr_t)(offset)),
647 _mSize(sizeof(B)) { }
648
649 /**
650 * Direct constructor from offset and size.
651 *
652 * \param[in] offset offset of the field.
653 * \param[in] size size of the field.
654 */
_C2FieldId_C2FieldId655 inline _C2FieldId(size_t offset, size_t size)
656 : _mOffset(offset), _mSize(size) {}
657
658 /**
659 * Constructor used to identify a field in an object.
660 *
661 * \param U[type] pointer to the object that contains this field. This is needed in case the
662 * field is in an (inherited) base class, in which case T will be that base class.
663 * \param pm[im] member pointer to the field
664 */
665 template<typename R, typename T, typename U, typename B=typename std::remove_extent<R>::type>
_C2FieldId_C2FieldId666 inline _C2FieldId(U *, R T::* pm)
667 : _mOffset((uint32_t)(uintptr_t)(&(((U*)256)->*pm)) - 256u),
668 _mSize(sizeof(B)) { }
669
670 /**
671 * Constructor used to identify a field in an object.
672 *
673 * \param pm[im] member pointer to the field
674 */
675 template<typename R, typename T, typename B=typename std::remove_extent<R>::type>
_C2FieldId_C2FieldId676 inline _C2FieldId(R T::* pm)
677 : _mOffset((uint32_t)(uintptr_t)(&(((T*)0)->*pm))),
678 _mSize(sizeof(B)) { }
679
680 inline bool operator==(const _C2FieldId &other) const {
681 return _mOffset == other._mOffset && _mSize == other._mSize;
682 }
683
684 inline bool operator<(const _C2FieldId &other) const {
685 return _mOffset < other._mOffset ||
686 // NOTE: order parent structure before sub field
687 (_mOffset == other._mOffset && _mSize > other._mSize);
688 }
689
DEFINE_OTHER_COMPARISON_OPERATORS_C2FieldId690 DEFINE_OTHER_COMPARISON_OPERATORS(_C2FieldId)
691
692 #if 0
693 inline uint32_t offset() const { return _mOffset; }
size_C2FieldId694 inline uint32_t size() const { return _mSize; }
695 #endif
696
697 #if defined(FRIEND_TEST)
698 friend void PrintTo(const _C2FieldId &d, ::std::ostream*);
699 #endif
700
701 private:
702 friend struct _C2ParamInspector;
703 friend struct C2FieldDescriptor;
704
705 uint32_t _mOffset; // offset of field
706 uint32_t _mSize; // size of field
707 };
708
709 /**
710 * Structure uniquely specifying a 'field' in a configuration. The field
711 * can be a field of a configuration, a subfield of a field of a configuration,
712 * and even the whole configuration. Moreover, if the field can point to an
713 * element in a array field, or to the entire array field.
714 *
715 * This structure is used for querying supported values for a field, as well
716 * as communicating configuration failures and conflicts when trying to change
717 * a configuration for a component/interface or a store.
718 */
719 struct C2ParamField {
720 //public:
721 /**
722 * Create a field identifier using a configuration parameter (variable),
723 * and a pointer to member.
724 *
725 * ~~~~~~~~~~~~~ (.cpp)
726 *
727 * struct C2SomeParam {
728 * uint32_t mField;
729 * uint32_t mArray[2];
730 * C2OtherStruct mStruct;
731 * uint32_t mFlexArray[];
732 * } *mParam;
733 *
734 * C2ParamField(mParam, &mParam->mField);
735 * C2ParamField(mParam, &mParam->mArray);
736 * C2ParamField(mParam, &mParam->mArray[0]);
737 * C2ParamField(mParam, &mParam->mStruct.mSubField);
738 * C2ParamField(mParam, &mParam->mFlexArray);
739 * C2ParamField(mParam, &mParam->mFlexArray[2]);
740 *
741 * ~~~~~~~~~~~~~
742 *
743 * \todo fix what this is for T[] (for now size becomes T[1])
744 *
745 * \note this does not work for 64-bit members as it triggers a
746 * 'taking address of packed member' warning.
747 *
748 * \param param pointer to parameter
749 * \param offset member pointer
750 */
751 template<typename S, typename T>
C2ParamFieldC2ParamField752 inline C2ParamField(S* param, T* offset)
753 : _mIndex(param->index()),
754 _mFieldId((T*)((uintptr_t)offset - (uintptr_t)param)) {}
755
756 template<typename S, typename T>
MakeC2ParamField757 inline static C2ParamField Make(S& param, T& offset) {
758 return C2ParamField(param.index(), (uintptr_t)&offset - (uintptr_t)¶m, sizeof(T));
759 }
760
761 /**
762 * Create a field identifier using a configuration parameter (variable),
763 * and a member pointer. This method cannot be used to refer to an
764 * array element or a subfield.
765 *
766 * ~~~~~~~~~~~~~ (.cpp)
767 *
768 * C2SomeParam mParam;
769 * C2ParamField(&mParam, &C2SomeParam::mMemberField);
770 *
771 * ~~~~~~~~~~~~~
772 *
773 * \param p pointer to parameter
774 * \param T member pointer to the field member
775 */
776 template<typename R, typename T, typename U>
C2ParamFieldC2ParamField777 inline C2ParamField(U *p, R T::* pm) : _mIndex(p->index()), _mFieldId(p, pm) { }
778
779 /**
780 * Create a field identifier to a configuration parameter (variable).
781 *
782 * ~~~~~~~~~~~~~ (.cpp)
783 *
784 * C2SomeParam mParam;
785 * C2ParamField(&mParam);
786 *
787 * ~~~~~~~~~~~~~
788 *
789 * \param param pointer to parameter
790 */
791 template<typename S>
C2ParamFieldC2ParamField792 inline C2ParamField(S* param)
793 : _mIndex(param->index()), _mFieldId(0u, param->size()) { }
794
795 /** Copy constructor. */
796 inline C2ParamField(const C2ParamField &other) = default;
797
798 /**
799 * Equality operator.
800 */
801 inline bool operator==(const C2ParamField &other) const {
802 return _mIndex == other._mIndex && _mFieldId == other._mFieldId;
803 }
804
805 /**
806 * Ordering operator.
807 */
808 inline bool operator<(const C2ParamField &other) const {
809 return _mIndex < other._mIndex ||
810 (_mIndex == other._mIndex && _mFieldId < other._mFieldId);
811 }
812
DEFINE_OTHER_COMPARISON_OPERATORSC2ParamField813 DEFINE_OTHER_COMPARISON_OPERATORS(C2ParamField)
814
815 protected:
816 inline C2ParamField(C2Param::Index index, uint32_t offset, uint32_t size)
817 : _mIndex(index), _mFieldId(offset, size) {}
818
819 private:
820 friend struct _C2ParamInspector;
821
822 C2Param::Index _mIndex; ///< parameter index
823 _C2FieldId _mFieldId; ///< field identifier
824 };
825
826 /**
827 * A shared (union) representation of numeric values
828 */
829 class C2Value {
830 public:
831 /// A union of supported primitive types.
832 union Primitive {
833 // first member is always zero initialized so it must be the largest
834 uint64_t u64; ///< uint64_t value
835 int64_t i64; ///< int64_t value
836 c2_cntr64_t c64; ///< c2_cntr64_t value
837 uint32_t u32; ///< uint32_t value
838 int32_t i32; ///< int32_t value
839 c2_cntr32_t c32; ///< c2_cntr32_t value
840 float fp; ///< float value
841
842 // constructors - implicit
Primitive(uint64_t value)843 Primitive(uint64_t value) : u64(value) { }
Primitive(int64_t value)844 Primitive(int64_t value) : i64(value) { }
Primitive(c2_cntr64_t value)845 Primitive(c2_cntr64_t value) : c64(value) { }
Primitive(uint32_t value)846 Primitive(uint32_t value) : u32(value) { }
Primitive(int32_t value)847 Primitive(int32_t value) : i32(value) { }
Primitive(c2_cntr32_t value)848 Primitive(c2_cntr32_t value) : c32(value) { }
Primitive(uint8_t value)849 Primitive(uint8_t value) : u32(value) { }
Primitive(char value)850 Primitive(char value) : i32(value) { }
Primitive(float value)851 Primitive(float value) : fp(value) { }
852
853 // allow construction from enum type
854 template<typename E, typename = typename std::enable_if<std::is_enum<E>::value>::type>
Primitive(E value)855 Primitive(E value)
856 : Primitive(static_cast<typename std::underlying_type<E>::type>(value)) { }
857
Primitive()858 Primitive() : u64(0) { }
859
860 /** gets value out of the union */
861 template<typename T> const T &ref() const;
862
863 // verify that we can assume standard aliasing
864 static_assert(sizeof(u64) == sizeof(i64), "");
865 static_assert(sizeof(u64) == sizeof(c64), "");
866 static_assert(sizeof(u32) == sizeof(i32), "");
867 static_assert(sizeof(u32) == sizeof(c32), "");
868 };
869 // verify that we can assume standard aliasing
870 static_assert(offsetof(Primitive, u64) == offsetof(Primitive, i64), "");
871 static_assert(offsetof(Primitive, u64) == offsetof(Primitive, c64), "");
872 static_assert(offsetof(Primitive, u32) == offsetof(Primitive, i32), "");
873 static_assert(offsetof(Primitive, u32) == offsetof(Primitive, c32), "");
874
875 enum type_t : uint32_t {
876 NO_INIT,
877 INT32,
878 UINT32,
879 CNTR32,
880 INT64,
881 UINT64,
882 CNTR64,
883 FLOAT,
884 };
885
886 template<typename T, bool = std::is_enum<T>::value>
TypeFor()887 inline static constexpr type_t TypeFor() {
888 using U = typename std::underlying_type<T>::type;
889 return TypeFor<U>();
890 }
891
892 // deprectated
893 template<typename T, bool B = std::is_enum<T>::value>
typeFor()894 inline static constexpr type_t typeFor() {
895 return TypeFor<T, B>();
896 }
897
898 // constructors - implicit
899 template<typename T>
C2Value(T value)900 C2Value(T value) : _mType(typeFor<T>()), _mValue(value) { }
901
C2Value()902 C2Value() : _mType(NO_INIT) { }
903
type()904 inline type_t type() const { return _mType; }
905
906 template<typename T>
get(T * value)907 inline bool get(T *value) const {
908 if (_mType == typeFor<T>()) {
909 *value = _mValue.ref<T>();
910 return true;
911 }
912 return false;
913 }
914
915 /// returns the address of the value
get()916 void *get() const {
917 return _mType == NO_INIT ? nullptr : (void*)&_mValue;
918 }
919
920 /// returns the size of the contained value
sizeOf()921 size_t inline sizeOf() const {
922 return SizeFor(_mType);
923 }
924
SizeFor(type_t type)925 static size_t SizeFor(type_t type) {
926 switch (type) {
927 case INT32:
928 case UINT32:
929 case CNTR32: return sizeof(_mValue.i32);
930 case INT64:
931 case UINT64:
932 case CNTR64: return sizeof(_mValue.i64);
933 case FLOAT: return sizeof(_mValue.fp);
934 default: return 0;
935 }
936 }
937
938 private:
939 type_t _mType;
940 Primitive _mValue;
941 };
942
ref()943 template<> inline const int32_t &C2Value::Primitive::ref<int32_t>() const { return i32; }
ref()944 template<> inline const int64_t &C2Value::Primitive::ref<int64_t>() const { return i64; }
ref()945 template<> inline const uint32_t &C2Value::Primitive::ref<uint32_t>() const { return u32; }
ref()946 template<> inline const uint64_t &C2Value::Primitive::ref<uint64_t>() const { return u64; }
ref()947 template<> inline const c2_cntr32_t &C2Value::Primitive::ref<c2_cntr32_t>() const { return c32; }
ref()948 template<> inline const c2_cntr64_t &C2Value::Primitive::ref<c2_cntr64_t>() const { return c64; }
ref()949 template<> inline const float &C2Value::Primitive::ref<float>() const { return fp; }
950
951 // provide types for enums and uint8_t, char even though we don't provide reading as them
TypeFor()952 template<> constexpr C2Value::type_t C2Value::TypeFor<char, false>() { return INT32; }
TypeFor()953 template<> constexpr C2Value::type_t C2Value::TypeFor<int32_t, false>() { return INT32; }
TypeFor()954 template<> constexpr C2Value::type_t C2Value::TypeFor<int64_t, false>() { return INT64; }
TypeFor()955 template<> constexpr C2Value::type_t C2Value::TypeFor<uint8_t, false>() { return UINT32; }
TypeFor()956 template<> constexpr C2Value::type_t C2Value::TypeFor<uint32_t, false>() { return UINT32; }
TypeFor()957 template<> constexpr C2Value::type_t C2Value::TypeFor<uint64_t, false>() { return UINT64; }
TypeFor()958 template<> constexpr C2Value::type_t C2Value::TypeFor<c2_cntr32_t, false>() { return CNTR32; }
TypeFor()959 template<> constexpr C2Value::type_t C2Value::TypeFor<c2_cntr64_t, false>() { return CNTR64; }
TypeFor()960 template<> constexpr C2Value::type_t C2Value::TypeFor<float, false>() { return FLOAT; }
961
962 // forward declare easy enum template
963 template<typename E> struct C2EasyEnum;
964
965 /**
966 * field descriptor. A field is uniquely defined by an index into a parameter.
967 * (Note: Stream-id is not captured as a field.)
968 *
969 * Ordering of fields is by offset. In case of structures, it is depth first,
970 * with a structure taking an index just before and in addition to its members.
971 */
972 struct C2FieldDescriptor {
973 //public:
974 /** field types and flags
975 * \note: only 32-bit and 64-bit fields are supported (e.g. no boolean, as that
976 * is represented using INT32).
977 */
978 enum type_t : uint32_t {
979 // primitive types
980 INT32 = C2Value::INT32, ///< 32-bit signed integer
981 UINT32 = C2Value::UINT32, ///< 32-bit unsigned integer
982 CNTR32 = C2Value::CNTR32, ///< 32-bit counter
983 INT64 = C2Value::INT64, ///< 64-bit signed integer
984 UINT64 = C2Value::UINT64, ///< 64-bit signed integer
985 CNTR64 = C2Value::CNTR64, ///< 64-bit counter
986 FLOAT = C2Value::FLOAT, ///< 32-bit floating point
987
988 // array types
989 STRING = 0x100, ///< fixed-size string (POD)
990 BLOB, ///< blob. Blobs have no sub-elements and can be thought of as byte arrays;
991 ///< however, bytes cannot be individually addressed by clients.
992
993 // complex types
994 STRUCT_FLAG = 0x20000, ///< structs. Marked with this flag in addition to their coreIndex.
995 };
996
997 typedef std::pair<C2String, C2Value::Primitive> NamedValueType;
998 typedef std::vector<NamedValueType> NamedValuesType;
999 //typedef std::pair<std::vector<C2String>, std::vector<C2Value::Primitive>> NamedValuesType;
1000
1001 /**
1002 * Template specialization that returns the named values for a type.
1003 *
1004 * \todo hide from client.
1005 *
1006 * \return a vector of name-value pairs.
1007 */
1008 template<typename B>
1009 static NamedValuesType namedValuesFor(const B &);
1010
1011 /** specialization for easy enums */
1012 template<typename E>
namedValuesForC2FieldDescriptor1013 inline static NamedValuesType namedValuesFor(const C2EasyEnum<E> &) {
1014 #pragma GCC diagnostic push
1015 #pragma GCC diagnostic ignored "-Wnull-dereference"
1016 return namedValuesFor(*(E*)nullptr);
1017 #pragma GCC diagnostic pop
1018 }
1019
1020 private:
1021 template<typename B, bool enabled=std::is_arithmetic<B>::value || std::is_enum<B>::value>
1022 struct C2_HIDE _NamedValuesGetter;
1023
1024 public:
C2FieldDescriptorC2FieldDescriptor1025 inline C2FieldDescriptor(uint32_t type, uint32_t extent, C2String name, size_t offset, size_t size)
1026 : _mType((type_t)type), _mExtent(extent), _mName(name), _mFieldId(offset, size) { }
1027
1028 inline C2FieldDescriptor(const C2FieldDescriptor &) = default;
1029
1030 template<typename T, class B=typename std::remove_extent<T>::type>
C2FieldDescriptorC2FieldDescriptor1031 inline C2FieldDescriptor(const T* offset, const char *name)
1032 : _mType(this->GetType((B*)nullptr)),
1033 _mExtent(std::is_array<T>::value ? std::extent<T>::value : 1),
1034 _mName(name),
1035 _mNamedValues(_NamedValuesGetter<B>::getNamedValues()),
1036 _mFieldId(offset) {}
1037
1038 /// \deprecated
1039 template<typename T, typename S, class B=typename std::remove_extent<T>::type>
C2FieldDescriptorC2FieldDescriptor1040 inline C2FieldDescriptor(S*, T S::* field, const char *name)
1041 : _mType(this->GetType((B*)nullptr)),
1042 _mExtent(std::is_array<T>::value ? std::extent<T>::value : 1),
1043 _mName(name),
1044 _mFieldId(&(((S*)0)->*field)) {}
1045
1046 /// returns the type of this field
typeC2FieldDescriptor1047 inline type_t type() const { return _mType; }
1048 /// returns the length of the field in case it is an array. Returns 0 for
1049 /// T[] arrays, returns 1 for T[1] arrays as well as if the field is not an array.
extentC2FieldDescriptor1050 inline size_t extent() const { return _mExtent; }
1051 /// returns the name of the field
nameC2FieldDescriptor1052 inline C2String name() const { return _mName; }
1053
namedValuesC2FieldDescriptor1054 const NamedValuesType &namedValues() const { return _mNamedValues; }
1055
1056 #if defined(FRIEND_TEST)
1057 friend void PrintTo(const C2FieldDescriptor &, ::std::ostream*);
1058 friend bool operator==(const C2FieldDescriptor &, const C2FieldDescriptor &);
1059 FRIEND_TEST(C2ParamTest_ParamFieldList, VerifyStruct);
1060 #endif
1061
1062 private:
1063 /**
1064 * Construct an offseted field descriptor.
1065 */
C2FieldDescriptorC2FieldDescriptor1066 inline C2FieldDescriptor(const C2FieldDescriptor &desc, size_t offset)
1067 : _mType(desc._mType), _mExtent(desc._mExtent),
1068 _mName(desc._mName), _mNamedValues(desc._mNamedValues),
1069 _mFieldId(desc._mFieldId._mOffset + offset, desc._mFieldId._mSize) { }
1070
1071 type_t _mType;
1072 uint32_t _mExtent; // the last member can be arbitrary length if it is T[] array,
1073 // extending to the end of the parameter (this is marked with
1074 // 0). T[0]-s are not fields.
1075 C2String _mName;
1076 NamedValuesType _mNamedValues;
1077
1078 _C2FieldId _mFieldId; // field identifier (offset and size)
1079
1080 // NOTE: We do not capture default value(s) here as that may depend on the component.
1081 // NOTE: We also do not capture bestEffort, as 1) this should be true for most fields,
1082 // 2) this is at parameter granularity.
1083
1084 // type resolution
GetTypeC2FieldDescriptor1085 inline static type_t GetType(int32_t*) { return INT32; }
GetTypeC2FieldDescriptor1086 inline static type_t GetType(uint32_t*) { return UINT32; }
GetTypeC2FieldDescriptor1087 inline static type_t GetType(c2_cntr32_t*) { return CNTR32; }
GetTypeC2FieldDescriptor1088 inline static type_t GetType(int64_t*) { return INT64; }
GetTypeC2FieldDescriptor1089 inline static type_t GetType(uint64_t*) { return UINT64; }
GetTypeC2FieldDescriptor1090 inline static type_t GetType(c2_cntr64_t*) { return CNTR64; }
GetTypeC2FieldDescriptor1091 inline static type_t GetType(float*) { return FLOAT; }
GetTypeC2FieldDescriptor1092 inline static type_t GetType(char*) { return STRING; }
GetTypeC2FieldDescriptor1093 inline static type_t GetType(uint8_t*) { return BLOB; }
1094
1095 template<typename T,
1096 class=typename std::enable_if<std::is_enum<T>::value>::type>
GetTypeC2FieldDescriptor1097 inline static type_t GetType(T*) {
1098 typename std::underlying_type<T>::type underlying(0);
1099 return GetType(&underlying);
1100 }
1101
1102 // verify C2Struct by having a FieldList() and a CORE_INDEX.
1103 template<typename T,
1104 class=decltype(T::CORE_INDEX + 1), class=decltype(T::FieldList())>
GetTypeC2FieldDescriptor1105 inline static type_t GetType(T*) {
1106 static_assert(!std::is_base_of<C2Param, T>::value, "cannot use C2Params as fields");
1107 return (type_t)(T::CORE_INDEX | STRUCT_FLAG);
1108 }
1109
1110 friend struct _C2ParamInspector;
1111 };
1112
1113 // no named values for compound types
1114 template<typename B>
1115 struct C2FieldDescriptor::_NamedValuesGetter<B, false> {
1116 inline static C2FieldDescriptor::NamedValuesType getNamedValues() {
1117 return NamedValuesType();
1118 }
1119 };
1120
1121 template<typename B>
1122 struct C2FieldDescriptor::_NamedValuesGetter<B, true> {
1123 inline static C2FieldDescriptor::NamedValuesType getNamedValues() {
1124 #pragma GCC diagnostic push
1125 #pragma GCC diagnostic ignored "-Wnull-dereference"
1126 return C2FieldDescriptor::namedValuesFor(*(B*)nullptr);
1127 #pragma GCC diagnostic pop
1128 }
1129 };
1130
1131 #define DEFINE_NO_NAMED_VALUES_FOR(type) \
1132 template<> inline C2FieldDescriptor::NamedValuesType C2FieldDescriptor::namedValuesFor(const type &) { \
1133 return NamedValuesType(); \
1134 }
1135
1136 // We cannot subtype constructor for enumerated types so insted define no named values for
1137 // non-enumerated integral types.
1138 DEFINE_NO_NAMED_VALUES_FOR(int32_t)
1139 DEFINE_NO_NAMED_VALUES_FOR(uint32_t)
1140 DEFINE_NO_NAMED_VALUES_FOR(c2_cntr32_t)
1141 DEFINE_NO_NAMED_VALUES_FOR(int64_t)
1142 DEFINE_NO_NAMED_VALUES_FOR(uint64_t)
1143 DEFINE_NO_NAMED_VALUES_FOR(c2_cntr64_t)
1144 DEFINE_NO_NAMED_VALUES_FOR(uint8_t)
1145 DEFINE_NO_NAMED_VALUES_FOR(char)
1146 DEFINE_NO_NAMED_VALUES_FOR(float)
1147
1148 /**
1149 * Describes the fields of a structure.
1150 */
1151 struct C2StructDescriptor {
1152 public:
1153 /// Returns the core index of the struct
1154 inline C2Param::CoreIndex coreIndex() const { return _mType.coreIndex(); }
1155
1156 // Returns the number of fields in this struct (not counting any recursive fields).
1157 // Must be at least 1 for valid structs.
1158 inline size_t numFields() const { return _mFields.size(); }
1159
1160 // Returns the list of direct fields (not counting any recursive fields).
1161 typedef std::vector<C2FieldDescriptor>::const_iterator field_iterator;
1162 inline field_iterator cbegin() const { return _mFields.cbegin(); }
1163 inline field_iterator cend() const { return _mFields.cend(); }
1164
1165 // only supplying const iterator - but these names are needed for range based loops
1166 inline field_iterator begin() const { return _mFields.cbegin(); }
1167 inline field_iterator end() const { return _mFields.cend(); }
1168
1169 template<typename T>
1170 inline C2StructDescriptor(T*)
1171 : C2StructDescriptor(T::CORE_INDEX, T::FieldList()) { }
1172
1173 inline C2StructDescriptor(
1174 C2Param::CoreIndex type,
1175 const std::vector<C2FieldDescriptor> &fields)
1176 : _mType(type), _mFields(fields) { }
1177
1178 private:
1179 friend struct _C2ParamInspector;
1180
1181 inline C2StructDescriptor(
1182 C2Param::CoreIndex type,
1183 std::vector<C2FieldDescriptor> &&fields)
1184 : _mType(type), _mFields(std::move(fields)) { }
1185
1186 const C2Param::CoreIndex _mType;
1187 const std::vector<C2FieldDescriptor> _mFields;
1188 };
1189
1190 /**
1191 * Describes parameters for a component.
1192 */
1193 struct C2ParamDescriptor {
1194 public:
1195 /**
1196 * Returns whether setting this param is required to configure this component.
1197 * This can only be true for builtin params for platform-defined components (e.g. video and
1198 * audio encoders/decoders, video/audio filters).
1199 * For vendor-defined components, it can be true even for vendor-defined params,
1200 * but it is not recommended, in case the component becomes platform-defined.
1201 */
1202 inline bool isRequired() const { return _mAttrib & IS_REQUIRED; }
1203
1204 /**
1205 * Returns whether this parameter is persistent. This is always true for C2Tuning and C2Setting,
1206 * but may be false for C2Info. If true, this parameter persists across frames and applies to
1207 * the current and subsequent frames. If false, this C2Info parameter only applies to the
1208 * current frame and is not assumed to have the same value (or even be present) on subsequent
1209 * frames, unless it is specified for those frames.
1210 */
1211 inline bool isPersistent() const { return _mAttrib & IS_PERSISTENT; }
1212
1213 inline bool isStrict() const { return _mAttrib & IS_STRICT; }
1214
1215 inline bool isReadOnly() const { return _mAttrib & IS_READ_ONLY; }
1216
1217 inline bool isVisible() const { return !(_mAttrib & IS_HIDDEN); }
1218
1219 inline bool isPublic() const { return !(_mAttrib & IS_INTERNAL); }
1220
1221 /// Returns the name of this param.
1222 /// This defaults to the underlying C2Struct's name, but could be altered for a component.
1223 inline C2String name() const { return _mName; }
1224
1225 /// Returns the parameter index
1226 inline C2Param::Index index() const { return _mIndex; }
1227
1228 /// Returns the indices of parameters that this parameter has a dependency on
1229 inline const std::vector<C2Param::Index> &dependencies() const { return _mDependencies; }
1230
1231 /// \deprecated
1232 template<typename T>
1233 inline C2ParamDescriptor(bool isRequired, C2StringLiteral name, const T*)
1234 : _mIndex(T::PARAM_TYPE),
1235 _mAttrib(IS_PERSISTENT | (isRequired ? IS_REQUIRED : 0)),
1236 _mName(name) { }
1237
1238 /// \deprecated
1239 inline C2ParamDescriptor(
1240 bool isRequired, C2StringLiteral name, C2Param::Index index)
1241 : _mIndex(index),
1242 _mAttrib(IS_PERSISTENT | (isRequired ? IS_REQUIRED : 0)),
1243 _mName(name) { }
1244
1245 enum attrib_t : uint32_t {
1246 // flags that default on
1247 IS_REQUIRED = 1u << 0, ///< parameter is required to be specified
1248 IS_PERSISTENT = 1u << 1, ///< parameter retains its value
1249 // flags that default off
1250 IS_STRICT = 1u << 2, ///< parameter is strict
1251 IS_READ_ONLY = 1u << 3, ///< parameter is publicly read-only
1252 IS_HIDDEN = 1u << 4, ///< parameter shall not be visible to clients
1253 IS_INTERNAL = 1u << 5, ///< parameter shall not be used by framework (other than testing)
1254 IS_CONST = 1u << 6 | IS_READ_ONLY, ///< parameter is publicly const (hence read-only)
1255 };
1256
1257 inline C2ParamDescriptor(
1258 C2Param::Index index, attrib_t attrib, C2StringLiteral name)
1259 : _mIndex(index),
1260 _mAttrib(attrib),
1261 _mName(name) { }
1262
1263 inline C2ParamDescriptor(
1264 C2Param::Index index, attrib_t attrib, C2String &&name,
1265 std::vector<C2Param::Index> &&dependencies)
1266 : _mIndex(index),
1267 _mAttrib(attrib),
1268 _mName(name),
1269 _mDependencies(std::move(dependencies)) { }
1270
1271 private:
1272 const C2Param::Index _mIndex;
1273 const uint32_t _mAttrib;
1274 const C2String _mName;
1275 std::vector<C2Param::Index> _mDependencies;
1276
1277 friend struct _C2ParamInspector;
1278 };
1279
1280 DEFINE_ENUM_OPERATORS(::C2ParamDescriptor::attrib_t)
1281
1282
1283 /// \ingroup internal
1284 /// Define a structure without CORE_INDEX.
1285 /// \note _FIELD_LIST is used only during declaration so that C2Struct declarations can end with
1286 /// a simple list of C2FIELD-s and closing bracket. Mark it unused as it is not used in templated
1287 /// structs.
1288 #define DEFINE_BASE_C2STRUCT(name) \
1289 private: \
1290 const static std::vector<C2FieldDescriptor> _FIELD_LIST __unused; /**< structure fields */ \
1291 public: \
1292 typedef C2##name##Struct _type; /**< type name shorthand */ \
1293 static const std::vector<C2FieldDescriptor> FieldList(); /**< structure fields factory */
1294
1295 /// Define a structure with matching CORE_INDEX.
1296 #define DEFINE_C2STRUCT(name) \
1297 public: \
1298 enum : uint32_t { CORE_INDEX = kParamIndex##name }; \
1299 DEFINE_BASE_C2STRUCT(name)
1300
1301 /// Define a flexible structure without CORE_INDEX.
1302 #define DEFINE_BASE_FLEX_C2STRUCT(name, flexMember) \
1303 public: \
1304 FLEX(C2##name##Struct, flexMember) \
1305 DEFINE_BASE_C2STRUCT(name)
1306
1307 /// Define a flexible structure with matching CORE_INDEX.
1308 #define DEFINE_FLEX_C2STRUCT(name, flexMember) \
1309 public: \
1310 FLEX(C2##name##Struct, flexMember) \
1311 enum : uint32_t { CORE_INDEX = kParamIndex##name | C2Param::CoreIndex::IS_FLEX_FLAG }; \
1312 DEFINE_BASE_C2STRUCT(name)
1313
1314 /// \ingroup internal
1315 /// Describe a structure of a templated structure.
1316 // Use list... as the argument gets resubsitituted and it contains commas. Alternative would be
1317 // to wrap list in an expression, e.g. ({ std::vector<C2FieldDescriptor> list; })) which converts
1318 // it from an initializer list to a vector.
1319 #define DESCRIBE_TEMPLATED_C2STRUCT(strukt, list...) \
1320 _DESCRIBE_TEMPLATABLE_C2STRUCT(template<>, strukt, __C2_GENERATE_GLOBAL_VARS__, list)
1321
1322 /// \deprecated
1323 /// Describe the fields of a structure using an initializer list.
1324 #define DESCRIBE_C2STRUCT(name, list...) \
1325 _DESCRIBE_TEMPLATABLE_C2STRUCT(, C2##name##Struct, __C2_GENERATE_GLOBAL_VARS__, list)
1326
1327 /// \ingroup internal
1328 /// Macro layer to get value of enabled that is passed in as a macro variable
1329 #define _DESCRIBE_TEMPLATABLE_C2STRUCT(template, strukt, enabled, list...) \
1330 __DESCRIBE_TEMPLATABLE_C2STRUCT(template, strukt, enabled, list)
1331
1332 /// \ingroup internal
1333 /// Macro layer to resolve to the specific macro based on macro variable
1334 #define __DESCRIBE_TEMPLATABLE_C2STRUCT(template, strukt, enabled, list...) \
1335 ___DESCRIBE_TEMPLATABLE_C2STRUCT##enabled(template, strukt, list)
1336
1337 #define ___DESCRIBE_TEMPLATABLE_C2STRUCT(template, strukt, list...) \
1338 template \
1339 const std::vector<C2FieldDescriptor> strukt::FieldList() { return list; }
1340
1341 #define ___DESCRIBE_TEMPLATABLE_C2STRUCT__C2_GENERATE_GLOBAL_VARS__(template, strukt, list...)
1342
1343 /**
1344 * Describe a field of a structure.
1345 * These must be in order.
1346 *
1347 * There are two ways to use this macro:
1348 *
1349 * ~~~~~~~~~~~~~ (.cpp)
1350 * struct C2VideoWidthStruct {
1351 * int32_t width;
1352 * C2VideoWidthStruct() {} // optional default constructor
1353 * C2VideoWidthStruct(int32_t _width) : width(_width) {}
1354 *
1355 * DEFINE_AND_DESCRIBE_C2STRUCT(VideoWidth)
1356 * C2FIELD(width, "width")
1357 * };
1358 * ~~~~~~~~~~~~~
1359 *
1360 * ~~~~~~~~~~~~~ (.cpp)
1361 * struct C2VideoWidthStruct {
1362 * int32_t width;
1363 * C2VideoWidthStruct() = default; // optional default constructor
1364 * C2VideoWidthStruct(int32_t _width) : width(_width) {}
1365 *
1366 * DEFINE_C2STRUCT(VideoWidth)
1367 * } C2_PACK;
1368 *
1369 * DESCRIBE_C2STRUCT(VideoWidth, {
1370 * C2FIELD(width, "width")
1371 * })
1372 * ~~~~~~~~~~~~~
1373 *
1374 * For flexible structures (those ending in T[]), use the flexible macros:
1375 *
1376 * ~~~~~~~~~~~~~ (.cpp)
1377 * struct C2VideoFlexWidthsStruct {
1378 * int32_t widths[];
1379 * C2VideoFlexWidthsStruct(); // must have a default constructor
1380 *
1381 * private:
1382 * // may have private constructors taking number of widths as the first argument
1383 * // This is used by the C2Param factory methods, e.g.
1384 * // C2VideoFlexWidthsGlobalParam::AllocUnique(size_t, int32_t);
1385 * C2VideoFlexWidthsStruct(size_t flexCount, int32_t value) {
1386 * for (size_t i = 0; i < flexCount; ++i) {
1387 * widths[i] = value;
1388 * }
1389 * }
1390 *
1391 * // If the last argument is T[N] or std::initializer_list<T>, the flexCount will
1392 * // be automatically calculated and passed by the C2Param factory methods, e.g.
1393 * // int widths[] = { 1, 2, 3 };
1394 * // C2VideoFlexWidthsGlobalParam::AllocUnique(widths);
1395 * template<unsigned N>
1396 * C2VideoFlexWidthsStruct(size_t flexCount, const int32_t(&init)[N]) {
1397 * for (size_t i = 0; i < flexCount; ++i) {
1398 * widths[i] = init[i];
1399 * }
1400 * }
1401 *
1402 * DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(VideoFlexWidths, widths)
1403 * C2FIELD(widths, "widths")
1404 * };
1405 * ~~~~~~~~~~~~~
1406 *
1407 * ~~~~~~~~~~~~~ (.cpp)
1408 * struct C2VideoFlexWidthsStruct {
1409 * int32_t mWidths[];
1410 * C2VideoFlexWidthsStruct(); // must have a default constructor
1411 *
1412 * DEFINE_FLEX_C2STRUCT(VideoFlexWidths, mWidths)
1413 * } C2_PACK;
1414 *
1415 * DESCRIBE_C2STRUCT(VideoFlexWidths, {
1416 * C2FIELD(mWidths, "widths")
1417 * })
1418 * ~~~~~~~~~~~~~
1419 *
1420 */
1421 #define DESCRIBE_C2FIELD(member, name) \
1422 C2FieldDescriptor(&((_type*)(nullptr))->member, name),
1423
1424 #define C2FIELD(member, name) _C2FIELD(member, name, __C2_GENERATE_GLOBAL_VARS__)
1425 /// \if 0
1426 #define _C2FIELD(member, name, enabled) __C2FIELD(member, name, enabled)
1427 #define __C2FIELD(member, name, enabled) DESCRIBE_C2FIELD##enabled(member, name)
1428 #define DESCRIBE_C2FIELD__C2_GENERATE_GLOBAL_VARS__(member, name)
1429 /// \endif
1430
1431 /// Define a structure with matching CORE_INDEX and start describing its fields.
1432 /// This must be at the end of the structure definition.
1433 #define DEFINE_AND_DESCRIBE_C2STRUCT(name) \
1434 _DEFINE_AND_DESCRIBE_C2STRUCT(name, DEFINE_C2STRUCT, __C2_GENERATE_GLOBAL_VARS__)
1435
1436 /// Define a base structure (with no CORE_INDEX) and start describing its fields.
1437 /// This must be at the end of the structure definition.
1438 #define DEFINE_AND_DESCRIBE_BASE_C2STRUCT(name) \
1439 _DEFINE_AND_DESCRIBE_C2STRUCT(name, DEFINE_BASE_C2STRUCT, __C2_GENERATE_GLOBAL_VARS__)
1440
1441 /// Define a flexible structure with matching CORE_INDEX and start describing its fields.
1442 /// This must be at the end of the structure definition.
1443 #define DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember) \
1444 _DEFINE_AND_DESCRIBE_FLEX_C2STRUCT( \
1445 name, flexMember, DEFINE_FLEX_C2STRUCT, __C2_GENERATE_GLOBAL_VARS__)
1446
1447 /// Define a flexible base structure (with no CORE_INDEX) and start describing its fields.
1448 /// This must be at the end of the structure definition.
1449 #define DEFINE_AND_DESCRIBE_BASE_FLEX_C2STRUCT(name, flexMember) \
1450 _DEFINE_AND_DESCRIBE_FLEX_C2STRUCT( \
1451 name, flexMember, DEFINE_BASE_FLEX_C2STRUCT, __C2_GENERATE_GLOBAL_VARS__)
1452
1453 /// \if 0
1454 /*
1455 Alternate declaration of field definitions in case no field list is to be generated.
1456 The specific macro is chosed based on the value of __C2_GENERATE_GLOBAL_VARS__ (whether it is
1457 defined (to be empty) or not. This requires two level of macro substitution.
1458 TRICKY: use namespace declaration to handle closing bracket that is normally after
1459 these macros.
1460 */
1461
1462 #define _DEFINE_AND_DESCRIBE_C2STRUCT(name, defineMacro, enabled) \
1463 __DEFINE_AND_DESCRIBE_C2STRUCT(name, defineMacro, enabled)
1464 #define __DEFINE_AND_DESCRIBE_C2STRUCT(name, defineMacro, enabled) \
1465 ___DEFINE_AND_DESCRIBE_C2STRUCT##enabled(name, defineMacro)
1466 #define ___DEFINE_AND_DESCRIBE_C2STRUCT__C2_GENERATE_GLOBAL_VARS__(name, defineMacro) \
1467 defineMacro(name) } C2_PACK; namespace {
1468 #define ___DEFINE_AND_DESCRIBE_C2STRUCT(name, defineMacro) \
1469 defineMacro(name) } C2_PACK; \
1470 const std::vector<C2FieldDescriptor> C2##name##Struct::FieldList() { return _FIELD_LIST; } \
1471 const std::vector<C2FieldDescriptor> C2##name##Struct::_FIELD_LIST = {
1472
1473 #define _DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember, defineMacro, enabled) \
1474 __DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember, defineMacro, enabled)
1475 #define __DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember, defineMacro, enabled) \
1476 ___DEFINE_AND_DESCRIBE_FLEX_C2STRUCT##enabled(name, flexMember, defineMacro)
1477 #define ___DEFINE_AND_DESCRIBE_FLEX_C2STRUCT__C2_GENERATE_GLOBAL_VARS__(name, flexMember, defineMacro) \
1478 defineMacro(name, flexMember) } C2_PACK; namespace {
1479 #define ___DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember, defineMacro) \
1480 defineMacro(name, flexMember) } C2_PACK; \
1481 const std::vector<C2FieldDescriptor> C2##name##Struct::FieldList() { return _FIELD_LIST; } \
1482 const std::vector<C2FieldDescriptor> C2##name##Struct::_FIELD_LIST = {
1483 /// \endif
1484
1485
1486 /**
1487 * Parameter reflector class.
1488 *
1489 * This class centralizes the description of parameter structures. This can be shared
1490 * by multiple components as describing a parameter does not imply support of that
1491 * parameter. However, each supported parameter and any dependent structures within
1492 * must be described by the parameter reflector provided by a component.
1493 */
1494 class C2ParamReflector {
1495 public:
1496 /**
1497 * Describes a parameter structure.
1498 *
1499 * \param[in] coreIndex the core index of the parameter structure containing at least the
1500 * core index
1501 *
1502 * \return the description of the parameter structure
1503 * \retval nullptr if the parameter is not supported by this reflector
1504 *
1505 * This methods shall not block and return immediately.
1506 *
1507 * \note this class does not take a set of indices because we would then prefer
1508 * to also return any dependent structures, and we don't want this logic to be
1509 * repeated in each reflector. Alternately, this could just return a map of all
1510 * descriptions, but we want to conserve memory if client only wants the description
1511 * of a few indices.
1512 */
1513 virtual std::unique_ptr<C2StructDescriptor> describe(C2Param::CoreIndex coreIndex) const = 0;
1514
1515 protected:
1516 virtual ~C2ParamReflector() = default;
1517 };
1518
1519 /**
1520 * Generic supported values for a field.
1521 *
1522 * This can be either a range or a set of values. The range can be a simple range, an arithmetic,
1523 * geometric or multiply-accumulate series with a clear minimum and maximum value. Values can
1524 * be discrete values, or can optionally represent flags to be or-ed.
1525 *
1526 * \note Do not use flags to represent bitfields. Use individual values or separate fields instead.
1527 */
1528 struct C2FieldSupportedValues {
1529 //public:
1530 enum type_t {
1531 EMPTY, ///< no supported values
1532 RANGE, ///< a numeric range that can be continuous or discrete
1533 VALUES, ///< a list of values
1534 FLAGS ///< a list of flags that can be OR-ed
1535 };
1536
1537 type_t type; /** Type of values for this field. */
1538
1539 typedef C2Value::Primitive Primitive;
1540
1541 /**
1542 * Range specifier for supported value. Used if type is RANGE.
1543 *
1544 * If step is 0 and num and denom are both 1, the supported values are any value, for which
1545 * min <= value <= max.
1546 *
1547 * Otherwise, the range represents a geometric/arithmetic/multiply-accumulate series, where
1548 * successive supported values can be derived from previous values (starting at min), using the
1549 * following formula:
1550 * v[0] = min
1551 * v[i] = v[i-1] * num / denom + step for i >= 1, while min < v[i] <= max.
1552 */
1553 struct {
1554 /** Lower end of the range (inclusive). */
1555 Primitive min;
1556 /** Upper end of the range (inclusive if permitted by series). */
1557 Primitive max;
1558 /** Step between supported values. */
1559 Primitive step;
1560 /** Numerator of a geometric series. */
1561 Primitive num;
1562 /** Denominator of a geometric series. */
1563 Primitive denom;
1564 } range;
1565
1566 /**
1567 * List of values. Used if type is VALUES or FLAGS.
1568 *
1569 * If type is VALUES, this is the list of supported values in decreasing preference.
1570 *
1571 * If type is FLAGS, this vector contains { min-mask, flag1, flag2... }. Basically, the first
1572 * value is the required set of flags to be set, and the rest of the values are flags that can
1573 * be set independently. FLAGS is only supported for integral types. Supported flags should
1574 * not overlap, as it can make validation non-deterministic. The standard validation method
1575 * is that starting from the original value, if each flag is removed when fully present (the
1576 * min-mask must be fully present), we shall arrive at 0.
1577 */
1578 std::vector<Primitive> values;
1579
1580 C2FieldSupportedValues()
1581 : type(EMPTY) {
1582 }
1583
1584 template<typename T>
1585 C2FieldSupportedValues(T min, T max, T step = T(std::is_floating_point<T>::value ? 0 : 1))
1586 : type(RANGE),
1587 range{min, max, step, (T)1, (T)1} { }
1588
1589 template<typename T>
1590 C2FieldSupportedValues(T min, T max, T num, T den) :
1591 type(RANGE),
1592 range{min, max, (T)0, num, den} { }
1593
1594 template<typename T>
1595 C2FieldSupportedValues(T min, T max, T step, T num, T den)
1596 : type(RANGE),
1597 range{min, max, step, num, den} { }
1598
1599 /// \deprecated
1600 template<typename T>
1601 C2FieldSupportedValues(bool flags, std::initializer_list<T> list)
1602 : type(flags ? FLAGS : VALUES),
1603 range{(T)0, (T)0, (T)0, (T)0, (T)0} {
1604 for (T value : list) {
1605 values.emplace_back(value);
1606 }
1607 }
1608
1609 /// \deprecated
1610 template<typename T>
1611 C2FieldSupportedValues(bool flags, const std::vector<T>& list)
1612 : type(flags ? FLAGS : VALUES),
1613 range{(T)0, (T)0, (T)0, (T)0, (T)0} {
1614 for(T value : list) {
1615 values.emplace_back(value);
1616 }
1617 }
1618
1619 /// \internal
1620 /// \todo: create separate values vs. flags initializer as for flags we want
1621 /// to list both allowed and required flags
1622 #pragma GCC diagnostic push
1623 #pragma GCC diagnostic ignored "-Wnull-dereference"
1624 template<typename T, typename E=decltype(C2FieldDescriptor::namedValuesFor(*(T*)nullptr))>
1625 C2FieldSupportedValues(bool flags, const T*)
1626 : type(flags ? FLAGS : VALUES),
1627 range{(T)0, (T)0, (T)0, (T)0, (T)0} {
1628 C2FieldDescriptor::NamedValuesType named = C2FieldDescriptor::namedValuesFor(*(T*)nullptr);
1629 if (flags) {
1630 values.emplace_back(0); // min-mask defaults to 0
1631 }
1632 for (const C2FieldDescriptor::NamedValueType &item : named){
1633 values.emplace_back(item.second);
1634 }
1635 }
1636 };
1637 #pragma GCC diagnostic pop
1638
1639 /**
1640 * Supported values for a specific field.
1641 *
1642 * This is a pair of the field specifier together with an optional supported values object.
1643 * This structure is used when reporting parameter configuration failures and conflicts.
1644 */
1645 struct C2ParamFieldValues {
1646 C2ParamField paramOrField; ///< the field or parameter
1647 /// optional supported values for the field if paramOrField specifies an actual field that is
1648 /// numeric (non struct, blob or string). Supported values for arrays (including string and
1649 /// blobs) describe the supported values for each element (character for string, and bytes for
1650 /// blobs). It is optional for read-only strings and blobs.
1651 std::unique_ptr<C2FieldSupportedValues> values;
1652
1653 // This struct is meant to be move constructed.
1654 C2_DEFAULT_MOVE(C2ParamFieldValues);
1655
1656 // Copy constructor/assignment is also provided as this object may get copied.
1657 C2ParamFieldValues(const C2ParamFieldValues &other)
1658 : paramOrField(other.paramOrField),
1659 values(other.values ? std::make_unique<C2FieldSupportedValues>(*other.values) : nullptr) { }
1660
1661 C2ParamFieldValues& operator=(const C2ParamFieldValues &other) {
1662 paramOrField = other.paramOrField;
1663 values = other.values ? std::make_unique<C2FieldSupportedValues>(*other.values) : nullptr;
1664 return *this;
1665 }
1666
1667
1668 /**
1669 * Construct with no values.
1670 */
1671 C2ParamFieldValues(const C2ParamField ¶mOrField_)
1672 : paramOrField(paramOrField_) { }
1673
1674 /**
1675 * Construct with values.
1676 */
1677 C2ParamFieldValues(const C2ParamField ¶mOrField_, const C2FieldSupportedValues &values_)
1678 : paramOrField(paramOrField_),
1679 values(std::make_unique<C2FieldSupportedValues>(values_)) { }
1680
1681 /**
1682 * Construct from fields.
1683 */
1684 C2ParamFieldValues(const C2ParamField ¶mOrField_, std::unique_ptr<C2FieldSupportedValues> &&values_)
1685 : paramOrField(paramOrField_),
1686 values(std::move(values_)) { }
1687 };
1688
1689 /// @}
1690
1691 // include debug header for C2Params.h if C2Debug.h was already included
1692 #ifdef C2UTILS_DEBUG_H_
1693 #include <util/C2Debug-param.h>
1694 #endif
1695
1696 #endif // C2PARAM_H_
1697