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 #if __cplusplus < 202002 431 inline bool operator!=(const C2Param &o) const { return !operator==(o); } 432 #endif 433 434 /// safe(r) type cast from pointer and size FromC2Param435 inline static C2Param* From(void *addr, size_t len) { 436 // _mSize must fit into size, but really C2Param must also to be a valid param 437 if (len < sizeof(C2Param)) { 438 return nullptr; 439 } 440 // _mSize must match length 441 C2Param *param = (C2Param*)addr; 442 if (param->_mSize != len) { 443 return nullptr; 444 } 445 return param; 446 } 447 448 /// Returns managed clone of |orig| at heap. CopyC2Param449 inline static std::unique_ptr<C2Param> Copy(const C2Param &orig) { 450 if (orig.size() == 0) { 451 return nullptr; 452 } 453 void *mem = ::operator new (orig.size()); 454 C2Param *param = new (mem) C2Param(orig.size(), orig._mIndex); 455 param->updateFrom(orig); 456 return std::unique_ptr<C2Param>(param); 457 } 458 459 /// Returns managed clone of |orig| as a stream parameter at heap. CopyAsStreamC2Param460 inline static std::unique_ptr<C2Param> CopyAsStream( 461 const C2Param &orig, bool output, unsigned stream) { 462 std::unique_ptr<C2Param> copy = Copy(orig); 463 if (copy) { 464 copy->_mIndex.convertToStream(output, stream); 465 } 466 return copy; 467 } 468 469 /// Returns managed clone of |orig| as a port parameter at heap. CopyAsPortC2Param470 inline static std::unique_ptr<C2Param> CopyAsPort(const C2Param &orig, bool output) { 471 std::unique_ptr<C2Param> copy = Copy(orig); 472 if (copy) { 473 copy->_mIndex.convertToPort(output); 474 } 475 return copy; 476 } 477 478 /// Returns managed clone of |orig| as a global parameter at heap. CopyAsGlobalC2Param479 inline static std::unique_ptr<C2Param> CopyAsGlobal(const C2Param &orig) { 480 std::unique_ptr<C2Param> copy = Copy(orig); 481 if (copy) { 482 copy->_mIndex.convertToGlobal(); 483 } 484 return copy; 485 } 486 487 /// Returns managed clone of |orig| as a stream parameter at heap. CopyAsRequestC2Param488 inline static std::unique_ptr<C2Param> CopyAsRequest(const C2Param &orig) { 489 std::unique_ptr<C2Param> copy = Copy(orig); 490 if (copy) { 491 copy->_mIndex.convertToRequest(); 492 } 493 return copy; 494 } 495 496 #if 0 497 template<typename P, class=decltype(C2Param(P()))> AsC2Param498 P *As() { return P::From(this); } 499 template<typename P> AsC2Param500 const P *As() const { return const_cast<const P*>(P::From(const_cast<C2Param*>(this))); } 501 #endif 502 503 protected: 504 /// sets the stream field. Returns true iff successful. setStreamC2Param505 inline bool setStream(unsigned stream) { 506 return _mIndex.setStream(stream); 507 } 508 509 /// sets the port (direction). Returns true iff successful. setPortC2Param510 inline bool setPort(bool output) { 511 return _mIndex.setPort(output); 512 } 513 514 /// sets the size of this parameter. setSizeC2Param515 inline void setSize(size_t size) { 516 if (size < sizeof(C2Param)) { 517 size = 0; 518 } 519 _mSize = c2_min(size, _mSize); 520 } 521 522 public: 523 /// invalidate this parameter. There is no recovery from this call; e.g. parameter 524 /// cannot be 'corrected' to be valid. invalidateC2Param525 inline void invalidate() { _mSize = 0; } 526 527 // if other is the same kind of (valid) param as this, copy it into this and return true. 528 // otherwise, do not copy anything, and return false. updateFromC2Param529 inline bool updateFrom(const C2Param &other) { 530 if (other._mSize <= _mSize && other._mIndex == _mIndex && _mSize > 0) { 531 memcpy(this, &other, other._mSize); 532 return true; 533 } 534 return false; 535 } 536 537 protected: 538 // returns |o| if it is a null ptr, or if can suitably be a param of given |type| (e.g. has 539 // same type (ignoring stream ID), and size). Otherwise, returns null. If |checkDir| is false, 540 // allow undefined or different direction (e.g. as constructed from C2PortParam() vs. 541 // C2PortParam::input), but still require equivalent type (stream, port or global); otherwise, 542 // return null. 543 inline static const C2Param* IfSuitable( 544 const C2Param* o, size_t size, Type type, size_t flexSize = 0, bool checkDir = true) { 545 if (o == nullptr || o->_mSize < size || (flexSize && ((o->_mSize - size) % flexSize))) { 546 return nullptr; 547 } else if (checkDir) { 548 return o->_mIndex.type() == type.mIndex ? o : nullptr; 549 } else if (o->_mIndex.isGlobal()) { 550 return nullptr; 551 } else { 552 return ((o->_mIndex.type() ^ type.mIndex) & ~Type::DIR_MASK) ? nullptr : o; 553 } 554 } 555 556 /// base constructor C2ParamC2Param557 inline C2Param(uint32_t paramSize, Index paramIndex) 558 : _mSize(paramSize), 559 _mIndex(paramIndex) { 560 if (paramSize > sizeof(C2Param)) { 561 memset(this + 1, 0, paramSize - sizeof(C2Param)); 562 } 563 } 564 565 /// base constructor with stream set C2ParamC2Param566 inline C2Param(uint32_t paramSize, Index paramIndex, unsigned stream) 567 : _mSize(paramSize), 568 _mIndex(paramIndex | Index::MakeStreamId(stream)) { 569 if (paramSize > sizeof(C2Param)) { 570 memset(this + 1, 0, paramSize - sizeof(C2Param)); 571 } 572 if (!forStream()) { 573 invalidate(); 574 } 575 } 576 577 private: 578 friend struct _C2ParamInspector; // for testing 579 580 /// returns true iff |o| has the same size and index as this. This performs the 581 /// basic check for equality. equalsC2Param582 inline bool equals(const C2Param &o) const { 583 return _mSize == o._mSize && _mIndex == o._mIndex; 584 } 585 586 uint32_t _mSize; 587 Index _mIndex; 588 }; 589 590 /// \ingroup internal 591 /// allow C2Params access to private methods, e.g. constructors 592 #define C2PARAM_MAKE_FRIENDS \ 593 template<typename U, typename S, int I, class F> friend struct C2GlobalParam; \ 594 template<typename U, typename S, int I, class F> friend struct C2PortParam; \ 595 template<typename U, typename S, int I, class F> friend struct C2StreamParam; \ 596 597 /** 598 * Setting base structure for component method signatures. Wrap constructors. 599 */ 600 struct C2Setting : public C2Param { 601 protected: 602 template<typename ...Args> C2SettingC2Setting603 inline C2Setting(const Args(&... args)) : C2Param(args...) { } 604 public: // TODO 605 enum : uint32_t { PARAM_KIND = Type::KIND_SETTING }; 606 }; 607 608 /** 609 * Tuning base structure for component method signatures. Wrap constructors. 610 */ 611 struct C2Tuning : public C2Setting { 612 protected: 613 template<typename ...Args> C2TuningC2Tuning614 inline C2Tuning(const Args(&... args)) : C2Setting(args...) { } 615 public: // TODO 616 enum : uint32_t { PARAM_KIND = Type::KIND_TUNING }; 617 }; 618 619 /** 620 * Info base structure for component method signatures. Wrap constructors. 621 */ 622 struct C2Info : public C2Param { 623 protected: 624 template<typename ...Args> C2InfoC2Info625 inline C2Info(const Args(&... args)) : C2Param(args...) { } 626 public: // TODO 627 enum : uint32_t { PARAM_KIND = Type::KIND_INFO }; 628 }; 629 630 /** 631 * Structure uniquely specifying a field in an arbitrary structure. 632 * 633 * \note This structure is used differently in C2FieldDescriptor to 634 * identify array fields, such that _mSize is the size of each element. This is 635 * because the field descriptor contains the array-length, and we want to keep 636 * a relevant element size for variable length arrays. 637 */ 638 struct _C2FieldId { 639 //public: 640 /** 641 * Constructor used for C2FieldDescriptor that removes the array extent. 642 * 643 * \param[in] offset pointer to the field in an object at address 0. 644 */ 645 template<typename T, class B=typename std::remove_extent<T>::type> _C2FieldId_C2FieldId646 inline _C2FieldId(T* offset) 647 : // offset is from "0" so will fit on 32-bits 648 _mOffset((uint32_t)(uintptr_t)(offset)), 649 _mSize(sizeof(B)) { } 650 651 /** 652 * Direct constructor from offset and size. 653 * 654 * \param[in] offset offset of the field. 655 * \param[in] size size of the field. 656 */ _C2FieldId_C2FieldId657 inline _C2FieldId(size_t offset, size_t size) 658 : _mOffset(offset), _mSize(size) {} 659 660 /** 661 * Constructor used to identify a field in an object. 662 * 663 * \param U[type] pointer to the object that contains this field. This is needed in case the 664 * field is in an (inherited) base class, in which case T will be that base class. 665 * \param pm[im] member pointer to the field 666 */ 667 template<typename R, typename T, typename U, typename B=typename std::remove_extent<R>::type> _C2FieldId_C2FieldId668 inline _C2FieldId(U *, R T::* pm) 669 : _mOffset((uint32_t)(uintptr_t)(&(((U*)256)->*pm)) - 256u), 670 _mSize(sizeof(B)) { } 671 672 /** 673 * Constructor used to identify a field in an object. 674 * 675 * \param pm[im] member pointer to the field 676 */ 677 template<typename R, typename T, typename B=typename std::remove_extent<R>::type> _C2FieldId_C2FieldId678 inline _C2FieldId(R T::* pm) 679 : _mOffset((uint32_t)(uintptr_t)(&(((T*)0)->*pm))), 680 _mSize(sizeof(B)) { } 681 682 inline bool operator==(const _C2FieldId &other) const { 683 return _mOffset == other._mOffset && _mSize == other._mSize; 684 } 685 686 inline bool operator<(const _C2FieldId &other) const { 687 return _mOffset < other._mOffset || 688 // NOTE: order parent structure before sub field 689 (_mOffset == other._mOffset && _mSize > other._mSize); 690 } 691 DEFINE_OTHER_COMPARISON_OPERATORS_C2FieldId692 DEFINE_OTHER_COMPARISON_OPERATORS(_C2FieldId) 693 694 #if 0 695 inline uint32_t offset() const { return _mOffset; } size_C2FieldId696 inline uint32_t size() const { return _mSize; } 697 #endif 698 699 #if defined(FRIEND_TEST) 700 friend void PrintTo(const _C2FieldId &d, ::std::ostream*); 701 #endif 702 703 private: 704 friend struct _C2ParamInspector; 705 friend struct C2FieldDescriptor; 706 707 uint32_t _mOffset; // offset of field 708 uint32_t _mSize; // size of field 709 }; 710 711 /** 712 * Structure uniquely specifying a 'field' in a configuration. The field 713 * can be a field of a configuration, a subfield of a field of a configuration, 714 * and even the whole configuration. Moreover, if the field can point to an 715 * element in a array field, or to the entire array field. 716 * 717 * This structure is used for querying supported values for a field, as well 718 * as communicating configuration failures and conflicts when trying to change 719 * a configuration for a component/interface or a store. 720 */ 721 struct C2ParamField { 722 //public: 723 /** 724 * Create a field identifier using a configuration parameter (variable), 725 * and a pointer to member. 726 * 727 * ~~~~~~~~~~~~~ (.cpp) 728 * 729 * struct C2SomeParam { 730 * uint32_t mField; 731 * uint32_t mArray[2]; 732 * C2OtherStruct mStruct; 733 * uint32_t mFlexArray[]; 734 * } *mParam; 735 * 736 * C2ParamField(mParam, &mParam->mField); 737 * C2ParamField(mParam, &mParam->mArray); 738 * C2ParamField(mParam, &mParam->mArray[0]); 739 * C2ParamField(mParam, &mParam->mStruct.mSubField); 740 * C2ParamField(mParam, &mParam->mFlexArray); 741 * C2ParamField(mParam, &mParam->mFlexArray[2]); 742 * 743 * ~~~~~~~~~~~~~ 744 * 745 * \todo fix what this is for T[] (for now size becomes T[1]) 746 * 747 * \note this does not work for 64-bit members as it triggers a 748 * 'taking address of packed member' warning. 749 * 750 * \param param pointer to parameter 751 * \param offset member pointer 752 */ 753 template<typename S, typename T> C2ParamFieldC2ParamField754 inline C2ParamField(S* param, T* offset) 755 : _mIndex(param->index()), 756 _mFieldId((T*)((uintptr_t)offset - (uintptr_t)param)) {} 757 758 template<typename S, typename T> MakeC2ParamField759 inline static C2ParamField Make(S& param, T& offset) { 760 return C2ParamField(param.index(), (uintptr_t)&offset - (uintptr_t)¶m, sizeof(T)); 761 } 762 763 /** 764 * Create a field identifier using a configuration parameter (variable), 765 * and a member pointer. This method cannot be used to refer to an 766 * array element or a subfield. 767 * 768 * ~~~~~~~~~~~~~ (.cpp) 769 * 770 * C2SomeParam mParam; 771 * C2ParamField(&mParam, &C2SomeParam::mMemberField); 772 * 773 * ~~~~~~~~~~~~~ 774 * 775 * \param p pointer to parameter 776 * \param T member pointer to the field member 777 */ 778 template<typename R, typename T, typename U> C2ParamFieldC2ParamField779 inline C2ParamField(U *p, R T::* pm) : _mIndex(p->index()), _mFieldId(p, pm) { } 780 781 /** 782 * Create a field identifier to a configuration parameter (variable). 783 * 784 * ~~~~~~~~~~~~~ (.cpp) 785 * 786 * C2SomeParam mParam; 787 * C2ParamField(&mParam); 788 * 789 * ~~~~~~~~~~~~~ 790 * 791 * \param param pointer to parameter 792 */ 793 template<typename S> C2ParamFieldC2ParamField794 inline C2ParamField(S* param) 795 : _mIndex(param->index()), _mFieldId(0u, param->size()) { } 796 797 /** Copy constructor. */ 798 inline C2ParamField(const C2ParamField &other) = default; 799 800 /** 801 * Equality operator. 802 */ 803 inline bool operator==(const C2ParamField &other) const { 804 return _mIndex == other._mIndex && _mFieldId == other._mFieldId; 805 } 806 807 /** 808 * Ordering operator. 809 */ 810 inline bool operator<(const C2ParamField &other) const { 811 return _mIndex < other._mIndex || 812 (_mIndex == other._mIndex && _mFieldId < other._mFieldId); 813 } 814 DEFINE_OTHER_COMPARISON_OPERATORSC2ParamField815 DEFINE_OTHER_COMPARISON_OPERATORS(C2ParamField) 816 817 protected: 818 inline C2ParamField(C2Param::Index index, uint32_t offset, uint32_t size) 819 : _mIndex(index), _mFieldId(offset, size) {} 820 821 private: 822 friend struct _C2ParamInspector; 823 824 C2Param::Index _mIndex; ///< parameter index 825 _C2FieldId _mFieldId; ///< field identifier 826 }; 827 828 /** 829 * A shared (union) representation of numeric values 830 */ 831 class C2Value { 832 public: 833 /// A union of supported primitive types. 834 union Primitive { 835 // first member is always zero initialized so it must be the largest 836 uint64_t u64; ///< uint64_t value 837 int64_t i64; ///< int64_t value 838 c2_cntr64_t c64; ///< c2_cntr64_t value 839 uint32_t u32; ///< uint32_t value 840 int32_t i32; ///< int32_t value 841 c2_cntr32_t c32; ///< c2_cntr32_t value 842 float fp; ///< float value 843 844 // constructors - implicit Primitive(uint64_t value)845 Primitive(uint64_t value) : u64(value) { } Primitive(int64_t value)846 Primitive(int64_t value) : i64(value) { } Primitive(c2_cntr64_t value)847 Primitive(c2_cntr64_t value) : c64(value) { } Primitive(uint32_t value)848 Primitive(uint32_t value) : u32(value) { } Primitive(int32_t value)849 Primitive(int32_t value) : i32(value) { } Primitive(c2_cntr32_t value)850 Primitive(c2_cntr32_t value) : c32(value) { } Primitive(uint8_t value)851 Primitive(uint8_t value) : u32(value) { } Primitive(char value)852 Primitive(char value) : i32(value) { } Primitive(float value)853 Primitive(float value) : fp(value) { } 854 855 // allow construction from enum type 856 template<typename E, typename = typename std::enable_if<std::is_enum<E>::value>::type> Primitive(E value)857 Primitive(E value) 858 : Primitive(static_cast<typename std::underlying_type<E>::type>(value)) { } 859 Primitive()860 Primitive() : u64(0) { } 861 862 /** gets value out of the union */ 863 template<typename T> const T &ref() const; 864 865 // verify that we can assume standard aliasing 866 static_assert(sizeof(u64) == sizeof(i64), ""); 867 static_assert(sizeof(u64) == sizeof(c64), ""); 868 static_assert(sizeof(u32) == sizeof(i32), ""); 869 static_assert(sizeof(u32) == sizeof(c32), ""); 870 }; 871 // verify that we can assume standard aliasing 872 static_assert(offsetof(Primitive, u64) == offsetof(Primitive, i64), ""); 873 static_assert(offsetof(Primitive, u64) == offsetof(Primitive, c64), ""); 874 static_assert(offsetof(Primitive, u32) == offsetof(Primitive, i32), ""); 875 static_assert(offsetof(Primitive, u32) == offsetof(Primitive, c32), ""); 876 877 enum type_t : uint32_t { 878 NO_INIT, 879 INT32, 880 UINT32, 881 CNTR32, 882 INT64, 883 UINT64, 884 CNTR64, 885 FLOAT, 886 }; 887 888 template<typename T, bool = std::is_enum<T>::value> TypeFor()889 inline static constexpr type_t TypeFor() { 890 using U = typename std::underlying_type<T>::type; 891 return TypeFor<U>(); 892 } 893 894 // deprectated 895 template<typename T, bool B = std::is_enum<T>::value> typeFor()896 inline static constexpr type_t typeFor() { 897 return TypeFor<T, B>(); 898 } 899 900 // constructors - implicit 901 template<typename T> C2Value(T value)902 C2Value(T value) : _mType(typeFor<T>()), _mValue(value) { } 903 C2Value()904 C2Value() : _mType(NO_INIT) { } 905 type()906 inline type_t type() const { return _mType; } 907 908 template<typename T> get(T * value)909 inline bool get(T *value) const { 910 if (_mType == typeFor<T>()) { 911 *value = _mValue.ref<T>(); 912 return true; 913 } 914 return false; 915 } 916 917 /// returns the address of the value get()918 void *get() const { 919 return _mType == NO_INIT ? nullptr : (void*)&_mValue; 920 } 921 922 /// returns the size of the contained value sizeOf()923 size_t inline sizeOf() const { 924 return SizeFor(_mType); 925 } 926 SizeFor(type_t type)927 static size_t SizeFor(type_t type) { 928 switch (type) { 929 case INT32: 930 case UINT32: 931 case CNTR32: return sizeof(_mValue.i32); 932 case INT64: 933 case UINT64: 934 case CNTR64: return sizeof(_mValue.i64); 935 case FLOAT: return sizeof(_mValue.fp); 936 default: return 0; 937 } 938 } 939 940 private: 941 type_t _mType; 942 Primitive _mValue; 943 }; 944 945 template<> inline const int32_t &C2Value::Primitive::ref<int32_t>() const { return i32; } 946 template<> inline const int64_t &C2Value::Primitive::ref<int64_t>() const { return i64; } 947 template<> inline const uint32_t &C2Value::Primitive::ref<uint32_t>() const { return u32; } 948 template<> inline const uint64_t &C2Value::Primitive::ref<uint64_t>() const { return u64; } 949 template<> inline const c2_cntr32_t &C2Value::Primitive::ref<c2_cntr32_t>() const { return c32; } 950 template<> inline const c2_cntr64_t &C2Value::Primitive::ref<c2_cntr64_t>() const { return c64; } 951 template<> inline const float &C2Value::Primitive::ref<float>() const { return fp; } 952 953 // provide types for enums and uint8_t, char even though we don't provide reading as them 954 template<> constexpr C2Value::type_t C2Value::TypeFor<char, false>() { return INT32; } 955 template<> constexpr C2Value::type_t C2Value::TypeFor<int32_t, false>() { return INT32; } 956 template<> constexpr C2Value::type_t C2Value::TypeFor<int64_t, false>() { return INT64; } 957 template<> constexpr C2Value::type_t C2Value::TypeFor<uint8_t, false>() { return UINT32; } 958 template<> constexpr C2Value::type_t C2Value::TypeFor<uint32_t, false>() { return UINT32; } 959 template<> constexpr C2Value::type_t C2Value::TypeFor<uint64_t, false>() { return UINT64; } 960 template<> constexpr C2Value::type_t C2Value::TypeFor<c2_cntr32_t, false>() { return CNTR32; } 961 template<> constexpr C2Value::type_t C2Value::TypeFor<c2_cntr64_t, false>() { return CNTR64; } 962 template<> constexpr C2Value::type_t C2Value::TypeFor<float, false>() { return FLOAT; } 963 964 // forward declare easy enum template 965 template<typename E> struct C2EasyEnum; 966 967 /** 968 * field descriptor. A field is uniquely defined by an index into a parameter. 969 * (Note: Stream-id is not captured as a field.) 970 * 971 * Ordering of fields is by offset. In case of structures, it is depth first, 972 * with a structure taking an index just before and in addition to its members. 973 */ 974 struct C2FieldDescriptor { 975 //public: 976 /** field types and flags 977 * \note: only 32-bit and 64-bit fields are supported (e.g. no boolean, as that 978 * is represented using INT32). 979 */ 980 enum type_t : uint32_t { 981 // primitive types 982 INT32 = C2Value::INT32, ///< 32-bit signed integer 983 UINT32 = C2Value::UINT32, ///< 32-bit unsigned integer 984 CNTR32 = C2Value::CNTR32, ///< 32-bit counter 985 INT64 = C2Value::INT64, ///< 64-bit signed integer 986 UINT64 = C2Value::UINT64, ///< 64-bit signed integer 987 CNTR64 = C2Value::CNTR64, ///< 64-bit counter 988 FLOAT = C2Value::FLOAT, ///< 32-bit floating point 989 990 // array types 991 STRING = 0x100, ///< fixed-size string (POD) 992 BLOB, ///< blob. Blobs have no sub-elements and can be thought of as byte arrays; 993 ///< however, bytes cannot be individually addressed by clients. 994 995 // complex types 996 STRUCT_FLAG = 0x20000, ///< structs. Marked with this flag in addition to their coreIndex. 997 }; 998 999 typedef std::pair<C2String, C2Value::Primitive> NamedValueType; 1000 typedef std::vector<NamedValueType> NamedValuesType; 1001 //typedef std::pair<std::vector<C2String>, std::vector<C2Value::Primitive>> NamedValuesType; 1002 1003 /** 1004 * Template specialization that returns the named values for a type. 1005 * 1006 * \todo hide from client. 1007 * 1008 * \return a vector of name-value pairs. 1009 */ 1010 template<typename B> 1011 static NamedValuesType namedValuesFor(const B &); 1012 1013 /** specialization for easy enums */ 1014 template<typename E> namedValuesForC2FieldDescriptor1015 inline static NamedValuesType namedValuesFor(const C2EasyEnum<E> &) { 1016 #pragma GCC diagnostic push 1017 #pragma GCC diagnostic ignored "-Wnull-dereference" 1018 return namedValuesFor(*(E*)nullptr); 1019 #pragma GCC diagnostic pop 1020 } 1021 1022 private: 1023 template<typename B, bool enabled=std::is_arithmetic<B>::value || std::is_enum<B>::value> 1024 struct C2_HIDE _NamedValuesGetter; 1025 1026 public: C2FieldDescriptorC2FieldDescriptor1027 inline C2FieldDescriptor(uint32_t type, uint32_t extent, C2String name, size_t offset, size_t size) 1028 : _mType((type_t)type), _mExtent(extent), _mName(name), _mFieldId(offset, size) { } 1029 1030 inline C2FieldDescriptor(const C2FieldDescriptor &) = default; 1031 1032 template<typename T, class B=typename std::remove_extent<T>::type> C2FieldDescriptorC2FieldDescriptor1033 inline C2FieldDescriptor(const T* offset, const char *name) 1034 : _mType(this->GetType((B*)nullptr)), 1035 _mExtent(std::is_array<T>::value ? std::extent<T>::value : 1), 1036 _mName(name), 1037 _mNamedValues(_NamedValuesGetter<B>::getNamedValues()), 1038 _mFieldId(offset) {} 1039 1040 /// \deprecated 1041 template<typename T, typename S, class B=typename std::remove_extent<T>::type> C2FieldDescriptorC2FieldDescriptor1042 inline C2FieldDescriptor(S*, T S::* field, const char *name) 1043 : _mType(this->GetType((B*)nullptr)), 1044 _mExtent(std::is_array<T>::value ? std::extent<T>::value : 1), 1045 _mName(name), 1046 _mFieldId(&(((S*)0)->*field)) {} 1047 1048 /// returns the type of this field typeC2FieldDescriptor1049 inline type_t type() const { return _mType; } 1050 /// returns the length of the field in case it is an array. Returns 0 for 1051 /// T[] arrays, returns 1 for T[1] arrays as well as if the field is not an array. extentC2FieldDescriptor1052 inline size_t extent() const { return _mExtent; } 1053 /// returns the name of the field nameC2FieldDescriptor1054 inline C2String name() const { return _mName; } 1055 namedValuesC2FieldDescriptor1056 const NamedValuesType &namedValues() const { return _mNamedValues; } 1057 1058 #if defined(FRIEND_TEST) 1059 friend void PrintTo(const C2FieldDescriptor &, ::std::ostream*); 1060 friend bool operator==(const C2FieldDescriptor &, const C2FieldDescriptor &); 1061 FRIEND_TEST(C2ParamTest_ParamFieldList, VerifyStruct); 1062 #endif 1063 1064 private: 1065 /** 1066 * Construct an offseted field descriptor. 1067 */ C2FieldDescriptorC2FieldDescriptor1068 inline C2FieldDescriptor(const C2FieldDescriptor &desc, size_t offset) 1069 : _mType(desc._mType), _mExtent(desc._mExtent), 1070 _mName(desc._mName), _mNamedValues(desc._mNamedValues), 1071 _mFieldId(desc._mFieldId._mOffset + offset, desc._mFieldId._mSize) { } 1072 1073 type_t _mType; 1074 uint32_t _mExtent; // the last member can be arbitrary length if it is T[] array, 1075 // extending to the end of the parameter (this is marked with 1076 // 0). T[0]-s are not fields. 1077 C2String _mName; 1078 NamedValuesType _mNamedValues; 1079 1080 _C2FieldId _mFieldId; // field identifier (offset and size) 1081 1082 // NOTE: We do not capture default value(s) here as that may depend on the component. 1083 // NOTE: We also do not capture bestEffort, as 1) this should be true for most fields, 1084 // 2) this is at parameter granularity. 1085 1086 // type resolution GetTypeC2FieldDescriptor1087 inline static type_t GetType(int32_t*) { return INT32; } GetTypeC2FieldDescriptor1088 inline static type_t GetType(uint32_t*) { return UINT32; } GetTypeC2FieldDescriptor1089 inline static type_t GetType(c2_cntr32_t*) { return CNTR32; } GetTypeC2FieldDescriptor1090 inline static type_t GetType(int64_t*) { return INT64; } GetTypeC2FieldDescriptor1091 inline static type_t GetType(uint64_t*) { return UINT64; } GetTypeC2FieldDescriptor1092 inline static type_t GetType(c2_cntr64_t*) { return CNTR64; } GetTypeC2FieldDescriptor1093 inline static type_t GetType(float*) { return FLOAT; } GetTypeC2FieldDescriptor1094 inline static type_t GetType(char*) { return STRING; } GetTypeC2FieldDescriptor1095 inline static type_t GetType(uint8_t*) { return BLOB; } 1096 1097 template<typename T, 1098 class=typename std::enable_if<std::is_enum<T>::value>::type> GetTypeC2FieldDescriptor1099 inline static type_t GetType(T*) { 1100 typename std::underlying_type<T>::type underlying(0); 1101 return GetType(&underlying); 1102 } 1103 1104 // verify C2Struct by having a FieldList() and a CORE_INDEX. 1105 template<typename T, 1106 class=decltype(T::CORE_INDEX + 1), class=decltype(T::FieldList())> GetTypeC2FieldDescriptor1107 inline static type_t GetType(T*) { 1108 static_assert(!std::is_base_of<C2Param, T>::value, "cannot use C2Params as fields"); 1109 return (type_t)(T::CORE_INDEX | STRUCT_FLAG); 1110 } 1111 1112 friend struct _C2ParamInspector; 1113 }; 1114 1115 // no named values for compound types 1116 template<typename B> 1117 struct C2FieldDescriptor::_NamedValuesGetter<B, false> { 1118 inline static C2FieldDescriptor::NamedValuesType getNamedValues() { 1119 return NamedValuesType(); 1120 } 1121 }; 1122 1123 template<typename B> 1124 struct C2FieldDescriptor::_NamedValuesGetter<B, true> { 1125 inline static C2FieldDescriptor::NamedValuesType getNamedValues() { 1126 #pragma GCC diagnostic push 1127 #pragma GCC diagnostic ignored "-Wnull-dereference" 1128 return C2FieldDescriptor::namedValuesFor(*(B*)nullptr); 1129 #pragma GCC diagnostic pop 1130 } 1131 }; 1132 1133 #define DEFINE_NO_NAMED_VALUES_FOR(type) \ 1134 template<> inline C2FieldDescriptor::NamedValuesType C2FieldDescriptor::namedValuesFor(const type &) { \ 1135 return NamedValuesType(); \ 1136 } 1137 1138 // We cannot subtype constructor for enumerated types so insted define no named values for 1139 // non-enumerated integral types. 1140 DEFINE_NO_NAMED_VALUES_FOR(int32_t) 1141 DEFINE_NO_NAMED_VALUES_FOR(uint32_t) 1142 DEFINE_NO_NAMED_VALUES_FOR(c2_cntr32_t) 1143 DEFINE_NO_NAMED_VALUES_FOR(int64_t) 1144 DEFINE_NO_NAMED_VALUES_FOR(uint64_t) 1145 DEFINE_NO_NAMED_VALUES_FOR(c2_cntr64_t) 1146 DEFINE_NO_NAMED_VALUES_FOR(uint8_t) 1147 DEFINE_NO_NAMED_VALUES_FOR(char) 1148 DEFINE_NO_NAMED_VALUES_FOR(float) 1149 1150 /** 1151 * Describes the fields of a structure. 1152 */ 1153 struct C2StructDescriptor { 1154 public: 1155 /// Returns the core index of the struct 1156 inline C2Param::CoreIndex coreIndex() const { return _mType.coreIndex(); } 1157 1158 // Returns the number of fields in this struct (not counting any recursive fields). 1159 // Must be at least 1 for valid structs. 1160 inline size_t numFields() const { return _mFields.size(); } 1161 1162 // Returns the list of direct fields (not counting any recursive fields). 1163 typedef std::vector<C2FieldDescriptor>::const_iterator field_iterator; 1164 inline field_iterator cbegin() const { return _mFields.cbegin(); } 1165 inline field_iterator cend() const { return _mFields.cend(); } 1166 1167 // only supplying const iterator - but these names are needed for range based loops 1168 inline field_iterator begin() const { return _mFields.cbegin(); } 1169 inline field_iterator end() const { return _mFields.cend(); } 1170 1171 template<typename T> 1172 inline C2StructDescriptor(T*) 1173 : C2StructDescriptor(T::CORE_INDEX, T::FieldList()) { } 1174 1175 inline C2StructDescriptor( 1176 C2Param::CoreIndex type, 1177 const std::vector<C2FieldDescriptor> &fields) 1178 : _mType(type), _mFields(fields) { } 1179 1180 private: 1181 friend struct _C2ParamInspector; 1182 1183 inline C2StructDescriptor( 1184 C2Param::CoreIndex type, 1185 std::vector<C2FieldDescriptor> &&fields) 1186 : _mType(type), _mFields(std::move(fields)) { } 1187 1188 const C2Param::CoreIndex _mType; 1189 const std::vector<C2FieldDescriptor> _mFields; 1190 }; 1191 1192 /** 1193 * Describes parameters for a component. 1194 */ 1195 struct C2ParamDescriptor { 1196 public: 1197 /** 1198 * Returns whether setting this param is required to configure this component. 1199 * This can only be true for builtin params for platform-defined components (e.g. video and 1200 * audio encoders/decoders, video/audio filters). 1201 * For vendor-defined components, it can be true even for vendor-defined params, 1202 * but it is not recommended, in case the component becomes platform-defined. 1203 */ 1204 inline bool isRequired() const { return _mAttrib & IS_REQUIRED; } 1205 1206 /** 1207 * Returns whether this parameter is persistent. This is always true for C2Tuning and C2Setting, 1208 * but may be false for C2Info. If true, this parameter persists across frames and applies to 1209 * the current and subsequent frames. If false, this C2Info parameter only applies to the 1210 * current frame and is not assumed to have the same value (or even be present) on subsequent 1211 * frames, unless it is specified for those frames. 1212 */ 1213 inline bool isPersistent() const { return _mAttrib & IS_PERSISTENT; } 1214 1215 inline bool isStrict() const { return _mAttrib & IS_STRICT; } 1216 1217 inline bool isReadOnly() const { return _mAttrib & IS_READ_ONLY; } 1218 1219 inline bool isVisible() const { return !(_mAttrib & IS_HIDDEN); } 1220 1221 inline bool isPublic() const { return !(_mAttrib & IS_INTERNAL); } 1222 1223 /// Returns the name of this param. 1224 /// This defaults to the underlying C2Struct's name, but could be altered for a component. 1225 inline C2String name() const { return _mName; } 1226 1227 /// Returns the parameter index 1228 inline C2Param::Index index() const { return _mIndex; } 1229 1230 /// Returns the indices of parameters that this parameter has a dependency on 1231 inline const std::vector<C2Param::Index> &dependencies() const { return _mDependencies; } 1232 1233 /// \deprecated 1234 template<typename T> 1235 inline C2ParamDescriptor(bool isRequired, C2StringLiteral name, const T*) 1236 : _mIndex(T::PARAM_TYPE), 1237 _mAttrib(IS_PERSISTENT | (isRequired ? IS_REQUIRED : 0)), 1238 _mName(name) { } 1239 1240 /// \deprecated 1241 inline C2ParamDescriptor( 1242 bool isRequired, C2StringLiteral name, C2Param::Index index) 1243 : _mIndex(index), 1244 _mAttrib(IS_PERSISTENT | (isRequired ? IS_REQUIRED : 0)), 1245 _mName(name) { } 1246 1247 enum attrib_t : uint32_t { 1248 // flags that default on 1249 IS_REQUIRED = 1u << 0, ///< parameter is required to be specified 1250 IS_PERSISTENT = 1u << 1, ///< parameter retains its value 1251 // flags that default off 1252 IS_STRICT = 1u << 2, ///< parameter is strict 1253 IS_READ_ONLY = 1u << 3, ///< parameter is publicly read-only 1254 IS_HIDDEN = 1u << 4, ///< parameter shall not be visible to clients 1255 IS_INTERNAL = 1u << 5, ///< parameter shall not be used by framework (other than testing) 1256 IS_CONST = 1u << 6 | IS_READ_ONLY, ///< parameter is publicly const (hence read-only) 1257 }; 1258 1259 inline C2ParamDescriptor( 1260 C2Param::Index index, attrib_t attrib, C2StringLiteral name) 1261 : _mIndex(index), 1262 _mAttrib(attrib), 1263 _mName(name) { } 1264 1265 inline C2ParamDescriptor( 1266 C2Param::Index index, attrib_t attrib, C2String &&name, 1267 std::vector<C2Param::Index> &&dependencies) 1268 : _mIndex(index), 1269 _mAttrib(attrib), 1270 _mName(name), 1271 _mDependencies(std::move(dependencies)) { } 1272 1273 private: 1274 const C2Param::Index _mIndex; 1275 const uint32_t _mAttrib; 1276 const C2String _mName; 1277 std::vector<C2Param::Index> _mDependencies; 1278 1279 friend struct _C2ParamInspector; 1280 }; 1281 1282 DEFINE_ENUM_OPERATORS(::C2ParamDescriptor::attrib_t) 1283 1284 1285 /// \ingroup internal 1286 /// Define a structure without CORE_INDEX. 1287 /// \note _FIELD_LIST is used only during declaration so that C2Struct declarations can end with 1288 /// a simple list of C2FIELD-s and closing bracket. Mark it unused as it is not used in templated 1289 /// structs. 1290 #define DEFINE_BASE_C2STRUCT(name) \ 1291 private: \ 1292 const static std::vector<C2FieldDescriptor> _FIELD_LIST __unused; /**< structure fields */ \ 1293 public: \ 1294 typedef C2##name##Struct _type; /**< type name shorthand */ \ 1295 static const std::vector<C2FieldDescriptor> FieldList(); /**< structure fields factory */ 1296 1297 /// Define a structure with matching CORE_INDEX. 1298 #define DEFINE_C2STRUCT(name) \ 1299 public: \ 1300 enum : uint32_t { CORE_INDEX = kParamIndex##name }; \ 1301 DEFINE_BASE_C2STRUCT(name) 1302 1303 /// Define a flexible structure without CORE_INDEX. 1304 #define DEFINE_BASE_FLEX_C2STRUCT(name, flexMember) \ 1305 public: \ 1306 FLEX(C2##name##Struct, flexMember) \ 1307 DEFINE_BASE_C2STRUCT(name) 1308 1309 /// Define a flexible structure with matching CORE_INDEX. 1310 #define DEFINE_FLEX_C2STRUCT(name, flexMember) \ 1311 public: \ 1312 FLEX(C2##name##Struct, flexMember) \ 1313 enum : uint32_t { CORE_INDEX = kParamIndex##name | C2Param::CoreIndex::IS_FLEX_FLAG }; \ 1314 DEFINE_BASE_C2STRUCT(name) 1315 1316 /// \ingroup internal 1317 /// Describe a structure of a templated structure. 1318 // Use list... as the argument gets resubsitituted and it contains commas. Alternative would be 1319 // to wrap list in an expression, e.g. ({ std::vector<C2FieldDescriptor> list; })) which converts 1320 // it from an initializer list to a vector. 1321 #define DESCRIBE_TEMPLATED_C2STRUCT(strukt, list...) \ 1322 _DESCRIBE_TEMPLATABLE_C2STRUCT(template<>, strukt, __C2_GENERATE_GLOBAL_VARS__, list) 1323 1324 /// \deprecated 1325 /// Describe the fields of a structure using an initializer list. 1326 #define DESCRIBE_C2STRUCT(name, list...) \ 1327 _DESCRIBE_TEMPLATABLE_C2STRUCT(, C2##name##Struct, __C2_GENERATE_GLOBAL_VARS__, list) 1328 1329 /// \ingroup internal 1330 /// Macro layer to get value of enabled that is passed in as a macro variable 1331 #define _DESCRIBE_TEMPLATABLE_C2STRUCT(template, strukt, enabled, list...) \ 1332 __DESCRIBE_TEMPLATABLE_C2STRUCT(template, strukt, enabled, list) 1333 1334 /// \ingroup internal 1335 /// Macro layer to resolve to the specific macro based on macro variable 1336 #define __DESCRIBE_TEMPLATABLE_C2STRUCT(template, strukt, enabled, list...) \ 1337 ___DESCRIBE_TEMPLATABLE_C2STRUCT##enabled(template, strukt, list) 1338 1339 #define ___DESCRIBE_TEMPLATABLE_C2STRUCT(template, strukt, list...) \ 1340 template \ 1341 const std::vector<C2FieldDescriptor> strukt::FieldList() { return list; } 1342 1343 #define ___DESCRIBE_TEMPLATABLE_C2STRUCT__C2_GENERATE_GLOBAL_VARS__(template, strukt, list...) 1344 1345 /** 1346 * Describe a field of a structure. 1347 * These must be in order. 1348 * 1349 * There are two ways to use this macro: 1350 * 1351 * ~~~~~~~~~~~~~ (.cpp) 1352 * struct C2VideoWidthStruct { 1353 * int32_t width; 1354 * C2VideoWidthStruct() {} // optional default constructor 1355 * C2VideoWidthStruct(int32_t _width) : width(_width) {} 1356 * 1357 * DEFINE_AND_DESCRIBE_C2STRUCT(VideoWidth) 1358 * C2FIELD(width, "width") 1359 * }; 1360 * ~~~~~~~~~~~~~ 1361 * 1362 * ~~~~~~~~~~~~~ (.cpp) 1363 * struct C2VideoWidthStruct { 1364 * int32_t width; 1365 * C2VideoWidthStruct() = default; // optional default constructor 1366 * C2VideoWidthStruct(int32_t _width) : width(_width) {} 1367 * 1368 * DEFINE_C2STRUCT(VideoWidth) 1369 * } C2_PACK; 1370 * 1371 * DESCRIBE_C2STRUCT(VideoWidth, { 1372 * C2FIELD(width, "width") 1373 * }) 1374 * ~~~~~~~~~~~~~ 1375 * 1376 * For flexible structures (those ending in T[]), use the flexible macros: 1377 * 1378 * ~~~~~~~~~~~~~ (.cpp) 1379 * struct C2VideoFlexWidthsStruct { 1380 * int32_t widths[]; 1381 * C2VideoFlexWidthsStruct(); // must have a default constructor 1382 * 1383 * private: 1384 * // may have private constructors taking number of widths as the first argument 1385 * // This is used by the C2Param factory methods, e.g. 1386 * // C2VideoFlexWidthsGlobalParam::AllocUnique(size_t, int32_t); 1387 * C2VideoFlexWidthsStruct(size_t flexCount, int32_t value) { 1388 * for (size_t i = 0; i < flexCount; ++i) { 1389 * widths[i] = value; 1390 * } 1391 * } 1392 * 1393 * // If the last argument is T[N] or std::initializer_list<T>, the flexCount will 1394 * // be automatically calculated and passed by the C2Param factory methods, e.g. 1395 * // int widths[] = { 1, 2, 3 }; 1396 * // C2VideoFlexWidthsGlobalParam::AllocUnique(widths); 1397 * template<unsigned N> 1398 * C2VideoFlexWidthsStruct(size_t flexCount, const int32_t(&init)[N]) { 1399 * for (size_t i = 0; i < flexCount; ++i) { 1400 * widths[i] = init[i]; 1401 * } 1402 * } 1403 * 1404 * DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(VideoFlexWidths, widths) 1405 * C2FIELD(widths, "widths") 1406 * }; 1407 * ~~~~~~~~~~~~~ 1408 * 1409 * ~~~~~~~~~~~~~ (.cpp) 1410 * struct C2VideoFlexWidthsStruct { 1411 * int32_t mWidths[]; 1412 * C2VideoFlexWidthsStruct(); // must have a default constructor 1413 * 1414 * DEFINE_FLEX_C2STRUCT(VideoFlexWidths, mWidths) 1415 * } C2_PACK; 1416 * 1417 * DESCRIBE_C2STRUCT(VideoFlexWidths, { 1418 * C2FIELD(mWidths, "widths") 1419 * }) 1420 * ~~~~~~~~~~~~~ 1421 * 1422 */ 1423 #define DESCRIBE_C2FIELD(member, name) \ 1424 C2FieldDescriptor(&((_type*)(nullptr))->member, name), 1425 1426 #define C2FIELD(member, name) _C2FIELD(member, name, __C2_GENERATE_GLOBAL_VARS__) 1427 /// \if 0 1428 #define _C2FIELD(member, name, enabled) __C2FIELD(member, name, enabled) 1429 #define __C2FIELD(member, name, enabled) DESCRIBE_C2FIELD##enabled(member, name) 1430 #define DESCRIBE_C2FIELD__C2_GENERATE_GLOBAL_VARS__(member, name) 1431 /// \endif 1432 1433 /// Define a structure with matching CORE_INDEX and start describing its fields. 1434 /// This must be at the end of the structure definition. 1435 #define DEFINE_AND_DESCRIBE_C2STRUCT(name) \ 1436 _DEFINE_AND_DESCRIBE_C2STRUCT(name, DEFINE_C2STRUCT, __C2_GENERATE_GLOBAL_VARS__) 1437 1438 /// Define a base structure (with no CORE_INDEX) and start describing its fields. 1439 /// This must be at the end of the structure definition. 1440 #define DEFINE_AND_DESCRIBE_BASE_C2STRUCT(name) \ 1441 _DEFINE_AND_DESCRIBE_C2STRUCT(name, DEFINE_BASE_C2STRUCT, __C2_GENERATE_GLOBAL_VARS__) 1442 1443 /// Define a flexible structure with matching CORE_INDEX and start describing its fields. 1444 /// This must be at the end of the structure definition. 1445 #define DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember) \ 1446 _DEFINE_AND_DESCRIBE_FLEX_C2STRUCT( \ 1447 name, flexMember, DEFINE_FLEX_C2STRUCT, __C2_GENERATE_GLOBAL_VARS__) 1448 1449 /// Define a flexible base structure (with no CORE_INDEX) and start describing its fields. 1450 /// This must be at the end of the structure definition. 1451 #define DEFINE_AND_DESCRIBE_BASE_FLEX_C2STRUCT(name, flexMember) \ 1452 _DEFINE_AND_DESCRIBE_FLEX_C2STRUCT( \ 1453 name, flexMember, DEFINE_BASE_FLEX_C2STRUCT, __C2_GENERATE_GLOBAL_VARS__) 1454 1455 /// \if 0 1456 /* 1457 Alternate declaration of field definitions in case no field list is to be generated. 1458 The specific macro is chosed based on the value of __C2_GENERATE_GLOBAL_VARS__ (whether it is 1459 defined (to be empty) or not. This requires two level of macro substitution. 1460 TRICKY: use namespace declaration to handle closing bracket that is normally after 1461 these macros. 1462 */ 1463 1464 #define _DEFINE_AND_DESCRIBE_C2STRUCT(name, defineMacro, enabled) \ 1465 __DEFINE_AND_DESCRIBE_C2STRUCT(name, defineMacro, enabled) 1466 #define __DEFINE_AND_DESCRIBE_C2STRUCT(name, defineMacro, enabled) \ 1467 ___DEFINE_AND_DESCRIBE_C2STRUCT##enabled(name, defineMacro) 1468 #define ___DEFINE_AND_DESCRIBE_C2STRUCT__C2_GENERATE_GLOBAL_VARS__(name, defineMacro) \ 1469 defineMacro(name) } C2_PACK; namespace { 1470 #define ___DEFINE_AND_DESCRIBE_C2STRUCT(name, defineMacro) \ 1471 defineMacro(name) } C2_PACK; \ 1472 const std::vector<C2FieldDescriptor> C2##name##Struct::FieldList() { return _FIELD_LIST; } \ 1473 const std::vector<C2FieldDescriptor> C2##name##Struct::_FIELD_LIST = { 1474 1475 #define _DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember, defineMacro, enabled) \ 1476 __DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember, defineMacro, enabled) 1477 #define __DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember, defineMacro, enabled) \ 1478 ___DEFINE_AND_DESCRIBE_FLEX_C2STRUCT##enabled(name, flexMember, defineMacro) 1479 #define ___DEFINE_AND_DESCRIBE_FLEX_C2STRUCT__C2_GENERATE_GLOBAL_VARS__(name, flexMember, defineMacro) \ 1480 defineMacro(name, flexMember) } C2_PACK; namespace { 1481 #define ___DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(name, flexMember, defineMacro) \ 1482 defineMacro(name, flexMember) } C2_PACK; \ 1483 const std::vector<C2FieldDescriptor> C2##name##Struct::FieldList() { return _FIELD_LIST; } \ 1484 const std::vector<C2FieldDescriptor> C2##name##Struct::_FIELD_LIST = { 1485 /// \endif 1486 1487 1488 /** 1489 * Parameter reflector class. 1490 * 1491 * This class centralizes the description of parameter structures. This can be shared 1492 * by multiple components as describing a parameter does not imply support of that 1493 * parameter. However, each supported parameter and any dependent structures within 1494 * must be described by the parameter reflector provided by a component. 1495 */ 1496 class C2ParamReflector { 1497 public: 1498 /** 1499 * Describes a parameter structure. 1500 * 1501 * \param[in] coreIndex the core index of the parameter structure containing at least the 1502 * core index 1503 * 1504 * \return the description of the parameter structure 1505 * \retval nullptr if the parameter is not supported by this reflector 1506 * 1507 * This methods shall not block and return immediately. 1508 * 1509 * \note this class does not take a set of indices because we would then prefer 1510 * to also return any dependent structures, and we don't want this logic to be 1511 * repeated in each reflector. Alternately, this could just return a map of all 1512 * descriptions, but we want to conserve memory if client only wants the description 1513 * of a few indices. 1514 */ 1515 virtual std::unique_ptr<C2StructDescriptor> describe(C2Param::CoreIndex coreIndex) const = 0; 1516 1517 protected: 1518 virtual ~C2ParamReflector() = default; 1519 }; 1520 1521 /** 1522 * Generic supported values for a field. 1523 * 1524 * This can be either a range or a set of values. The range can be a simple range, an arithmetic, 1525 * geometric or multiply-accumulate series with a clear minimum and maximum value. Values can 1526 * be discrete values, or can optionally represent flags to be or-ed. 1527 * 1528 * \note Do not use flags to represent bitfields. Use individual values or separate fields instead. 1529 */ 1530 struct C2FieldSupportedValues { 1531 //public: 1532 enum type_t { 1533 EMPTY, ///< no supported values 1534 RANGE, ///< a numeric range that can be continuous or discrete 1535 VALUES, ///< a list of values 1536 FLAGS ///< a list of flags that can be OR-ed 1537 }; 1538 1539 type_t type; /** Type of values for this field. */ 1540 1541 typedef C2Value::Primitive Primitive; 1542 1543 /** 1544 * Range specifier for supported value. Used if type is RANGE. 1545 * 1546 * If step is 0 and num and denom are both 1, the supported values are any value, for which 1547 * min <= value <= max. 1548 * 1549 * Otherwise, the range represents a geometric/arithmetic/multiply-accumulate series, where 1550 * successive supported values can be derived from previous values (starting at min), using the 1551 * following formula: 1552 * v[0] = min 1553 * v[i] = v[i-1] * num / denom + step for i >= 1, while min < v[i] <= max. 1554 */ 1555 struct { 1556 /** Lower end of the range (inclusive). */ 1557 Primitive min; 1558 /** Upper end of the range (inclusive if permitted by series). */ 1559 Primitive max; 1560 /** Step between supported values. */ 1561 Primitive step; 1562 /** Numerator of a geometric series. */ 1563 Primitive num; 1564 /** Denominator of a geometric series. */ 1565 Primitive denom; 1566 } range; 1567 1568 /** 1569 * List of values. Used if type is VALUES or FLAGS. 1570 * 1571 * If type is VALUES, this is the list of supported values in decreasing preference. 1572 * 1573 * If type is FLAGS, this vector contains { min-mask, flag1, flag2... }. Basically, the first 1574 * value is the required set of flags to be set, and the rest of the values are flags that can 1575 * be set independently. FLAGS is only supported for integral types. Supported flags should 1576 * not overlap, as it can make validation non-deterministic. The standard validation method 1577 * is that starting from the original value, if each flag is removed when fully present (the 1578 * min-mask must be fully present), we shall arrive at 0. 1579 */ 1580 std::vector<Primitive> values; 1581 1582 C2FieldSupportedValues() 1583 : type(EMPTY) { 1584 } 1585 1586 template<typename T> 1587 C2FieldSupportedValues(T min, T max, T step = T(std::is_floating_point<T>::value ? 0 : 1)) 1588 : type(RANGE), 1589 range{min, max, step, (T)1, (T)1} { } 1590 1591 template<typename T> 1592 C2FieldSupportedValues(T min, T max, T num, T den) : 1593 type(RANGE), 1594 range{min, max, (T)0, num, den} { } 1595 1596 template<typename T> 1597 C2FieldSupportedValues(T min, T max, T step, T num, T den) 1598 : type(RANGE), 1599 range{min, max, step, num, den} { } 1600 1601 /// \deprecated 1602 template<typename T> 1603 C2FieldSupportedValues(bool flags, std::initializer_list<T> list) 1604 : type(flags ? FLAGS : VALUES), 1605 range{(T)0, (T)0, (T)0, (T)0, (T)0} { 1606 for (T value : list) { 1607 values.emplace_back(value); 1608 } 1609 } 1610 1611 /// \deprecated 1612 template<typename T> 1613 C2FieldSupportedValues(bool flags, const std::vector<T>& list) 1614 : type(flags ? FLAGS : VALUES), 1615 range{(T)0, (T)0, (T)0, (T)0, (T)0} { 1616 for(T value : list) { 1617 values.emplace_back(value); 1618 } 1619 } 1620 1621 /// \internal 1622 /// \todo: create separate values vs. flags initializer as for flags we want 1623 /// to list both allowed and required flags 1624 #pragma GCC diagnostic push 1625 #pragma GCC diagnostic ignored "-Wnull-dereference" 1626 template<typename T, typename E=decltype(C2FieldDescriptor::namedValuesFor(*(T*)nullptr))> 1627 C2FieldSupportedValues(bool flags, const T*) 1628 : type(flags ? FLAGS : VALUES), 1629 range{(T)0, (T)0, (T)0, (T)0, (T)0} { 1630 C2FieldDescriptor::NamedValuesType named = C2FieldDescriptor::namedValuesFor(*(T*)nullptr); 1631 if (flags) { 1632 values.emplace_back(0); // min-mask defaults to 0 1633 } 1634 for (const C2FieldDescriptor::NamedValueType &item : named){ 1635 values.emplace_back(item.second); 1636 } 1637 } 1638 }; 1639 #pragma GCC diagnostic pop 1640 1641 /** 1642 * Supported values for a specific field. 1643 * 1644 * This is a pair of the field specifier together with an optional supported values object. 1645 * This structure is used when reporting parameter configuration failures and conflicts. 1646 */ 1647 struct C2ParamFieldValues { 1648 C2ParamField paramOrField; ///< the field or parameter 1649 /// optional supported values for the field if paramOrField specifies an actual field that is 1650 /// numeric (non struct, blob or string). Supported values for arrays (including string and 1651 /// blobs) describe the supported values for each element (character for string, and bytes for 1652 /// blobs). It is optional for read-only strings and blobs. 1653 std::unique_ptr<C2FieldSupportedValues> values; 1654 1655 // This struct is meant to be move constructed. 1656 C2_DEFAULT_MOVE(C2ParamFieldValues); 1657 1658 // Copy constructor/assignment is also provided as this object may get copied. 1659 C2ParamFieldValues(const C2ParamFieldValues &other) 1660 : paramOrField(other.paramOrField), 1661 values(other.values ? std::make_unique<C2FieldSupportedValues>(*other.values) : nullptr) { } 1662 1663 C2ParamFieldValues& operator=(const C2ParamFieldValues &other) { 1664 paramOrField = other.paramOrField; 1665 values = other.values ? std::make_unique<C2FieldSupportedValues>(*other.values) : nullptr; 1666 return *this; 1667 } 1668 1669 1670 /** 1671 * Construct with no values. 1672 */ 1673 C2ParamFieldValues(const C2ParamField ¶mOrField_) 1674 : paramOrField(paramOrField_) { } 1675 1676 /** 1677 * Construct with values. 1678 */ 1679 C2ParamFieldValues(const C2ParamField ¶mOrField_, const C2FieldSupportedValues &values_) 1680 : paramOrField(paramOrField_), 1681 values(std::make_unique<C2FieldSupportedValues>(values_)) { } 1682 1683 /** 1684 * Construct from fields. 1685 */ 1686 C2ParamFieldValues(const C2ParamField ¶mOrField_, std::unique_ptr<C2FieldSupportedValues> &&values_) 1687 : paramOrField(paramOrField_), 1688 values(std::move(values_)) { } 1689 }; 1690 1691 /// @} 1692 1693 // include debug header for C2Params.h if C2Debug.h was already included 1694 #ifdef C2UTILS_DEBUG_H_ 1695 #include <util/C2Debug-param.h> 1696 #endif 1697 1698 #endif // C2PARAM_H_ 1699