controls: Add vendor control/property support to generation scripts

Add support for vendor-specific controls and properties to libcamera.
The controls/properties are defined by a "vendor" tag in the YAML
control description file, for example:

vendor: rpi
controls:
  - MyExampleControl:
      type: string
      description: |
        Test for libcamera vendor-specific controls.

This will now generate a control id in the libcamera::controls::rpi
namespace, ensuring no id conflict between different vendors, core or
draft libcamera controls. Similarly, a ControlIdMap control is generated
in the libcamera::controls::rpi namespace.

A #define LIBCAMERA_HAS_RPI_VENDOR_CONTROLS is also generated to allow
applications to conditionally compile code if the specific vendor
controls are present. For the python bindings, the control is available
with libcamera.controls.rpi.MyExampleControl. The above controls
example applies similarly to properties.

Existing libcamera controls defined in control_ids.yaml are given the
"libcamera" vendor tag.

A new --mode flag is added to gen-controls.py to specify the mode of
operation, either 'controls' or 'properties' to allow the code generator
to correctly set the #define string.

As a drive-by, sort and redefine the output command line argument in
gen-controls.py and gen-py-controls.py to ('--output', '-o') for
consistency.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Naushir Patuck 2023-11-08 11:09:51 +00:00
parent 61f6b37242
commit bd6658943a
12 changed files with 176 additions and 73 deletions

View file

@ -24,45 +24,59 @@ def find_common_prefix(strings):
def generate_py(controls, mode):
out = ''
for ctrl in controls:
name, ctrl = ctrl.popitem()
vendors_class_def = []
vendor_defs = []
vendors = []
for vendor, ctrl_list in controls.items():
for ctrls in ctrl_list:
name, ctrl = ctrls.popitem()
if ctrl.get('draft'):
ns = 'libcamera::{}::draft::'.format(mode)
container = 'draft'
else:
ns = 'libcamera::{}::'.format(mode)
container = 'controls'
if vendor not in vendors and vendor != 'libcamera':
vendors_class_def.append('class Py{}Controls\n{{\n}};\n'.format(vendor))
vendor_defs.append('\tauto {} = py::class_<Py{}Controls>(controls, \"{}\");'.format(vendor, vendor, vendor))
vendors.append(vendor)
out += f'\t{container}.def_readonly_static("{name}", static_cast<const libcamera::ControlId *>(&{ns}{name}));\n\n'
enum = ctrl.get('enum')
if not enum:
continue
cpp_enum = name + 'Enum'
out += '\tpy::enum_<{}{}>({}, \"{}\")\n'.format(ns, cpp_enum, container, cpp_enum)
if mode == 'controls':
# Adjustments for controls
if name == 'LensShadingMapMode':
prefix = 'LensShadingMapMode'
if ctrl.get('draft'):
ns = 'libcamera::{}::draft::'.format(mode)
container = 'draft'
elif vendor != 'libcamera':
ns = 'libcamera::{}::{}::'.format(mode, vendor)
container = vendor
else:
ns = 'libcamera::{}::'.format(mode)
container = 'controls'
out += f'\t{container}.def_readonly_static("{name}", static_cast<const libcamera::ControlId *>(&{ns}{name}));\n\n'
enum = ctrl.get('enum')
if not enum:
continue
cpp_enum = name + 'Enum'
out += '\tpy::enum_<{}{}>({}, \"{}\")\n'.format(ns, cpp_enum, container, cpp_enum)
if mode == 'controls':
# Adjustments for controls
if name == 'LensShadingMapMode':
prefix = 'LensShadingMapMode'
else:
prefix = find_common_prefix([e['name'] for e in enum])
else:
# Adjustments for properties
prefix = find_common_prefix([e['name'] for e in enum])
else:
# Adjustments for properties
prefix = find_common_prefix([e['name'] for e in enum])
for entry in enum:
cpp_enum = entry['name']
py_enum = entry['name'][len(prefix):]
for entry in enum:
cpp_enum = entry['name']
py_enum = entry['name'][len(prefix):]
out += '\t\t.value(\"{}\", {}{})\n'.format(py_enum, ns, cpp_enum)
out += '\t\t.value(\"{}\", {}{})\n'.format(py_enum, ns, cpp_enum)
out += '\t;\n\n'
out += '\t;\n\n'
return {'controls': out}
return {'controls': out,
'vendors_class_def': '\n'.join(vendors_class_def),
'vendors_defs': '\n'.join(vendor_defs)}
def fill_template(template, data):
@ -75,14 +89,14 @@ def fill_template(template, data):
def main(argv):
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-o', dest='output', metavar='file', type=str,
parser.add_argument('--mode', '-m', type=str, required=True,
help='Mode is either "controls" or "properties"')
parser.add_argument('--output', '-o', metavar='file', type=str,
help='Output file name. Defaults to standard output if not specified.')
parser.add_argument('input', type=str,
help='Input file name.')
parser.add_argument('template', type=str,
help='Template file name.')
parser.add_argument('--mode', type=str, required=True,
help='Mode is either "controls" or "properties"')
args = parser.parse_args(argv[1:])
if args.mode not in ['controls', 'properties']:
@ -90,7 +104,10 @@ def main(argv):
return -1
data = open(args.input, 'rb').read()
controls = yaml.safe_load(data)['controls']
controls = {}
vendor = yaml.safe_load(data)['vendor']
controls[vendor] = yaml.safe_load(data)['controls']
data = generate_py(controls, args.mode)

View file

@ -21,10 +21,13 @@ class PyDraftControls
{
};
${vendors_class_def}
void init_py_controls_generated(py::module& m)
{
auto controls = py::class_<PyControls>(m, "controls");
auto draft = py::class_<PyDraftControls>(controls, "draft");
${vendors_defs}
${controls}
}

View file

@ -21,10 +21,13 @@ class PyDraftProperties
{
};
${vendors_class_def}
void init_py_properties_generated(py::module& m)
{
auto controls = py::class_<PyProperties>(m, "properties");
auto draft = py::class_<PyDraftProperties>(controls, "draft");
${vendors_defs}
${controls}
}