summaryrefslogtreecommitdiff
path: root/utils/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl
blob: ba34a361f8117c0c38caaad3b9e19602ada16bd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
{#-
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # Copyright (C) 2020, Google Inc.
-#}
{%- import "proxy_functions.tmpl" as proxy_funcs -%}

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * {{module_name}}_ipa_proxy.cpp - Image Processing Algorithm proxy for {{module_name}}
 *
 * This file is auto-generated. Do not edit.
 */

#include <libcamera/ipa/{{module_name}}_ipa_proxy.h>

#include <memory>
#include <string>
#include <vector>

#include <libcamera/ipa/ipa_module_info.h>
#include <libcamera/ipa/{{module_name}}_ipa_interface.h>
#include <libcamera/ipa/{{module_name}}_ipa_serializer.h>

#include "libcamera/internal/control_serializer.h"
#include "libcamera/internal/ipa_data_serializer.h"
#include "libcamera/internal/ipa_module.h"
#include "libcamera/internal/ipa_proxy.h"
#include "libcamera/internal/ipc_pipe.h"
#include "libcamera/internal/ipc_pipe_unixsocket.h"
#include "libcamera/internal/ipc_unixsocket.h"
#include "libcamera/internal/log.h"
#include "libcamera/internal/process.h"
#include "libcamera/internal/thread.h"

namespace libcamera {

LOG_DECLARE_CATEGORY(IPAProxy)

{%- if has_namespace %}
{% for ns in namespace %}
namespace {{ns}} {
{% endfor %}
{%- endif %}

{{proxy_name}}::{{proxy_name}}(IPAModule *ipam, bool isolate)
	: IPAProxy(ipam), running_(false),
	  isolate_(isolate), seq_(0)
{
	LOG(IPAProxy, Debug)
		<< "initializing {{module_name}} proxy: loading IPA from "
		<< ipam->path();

	if (isolate_) {
		const std::string proxyWorkerPath = resolvePath("{{module_name}}_ipa_proxy");
		if (proxyWorkerPath.empty()) {
			LOG(IPAProxy, Error)
				<< "Failed to get proxy worker path";
			return;
		}

		ipc_ = std::make_unique<IPCPipeUnixSocket>(ipam->path().c_str(),
							   proxyWorkerPath.c_str());
		if (!ipc_->isConnected()) {
			LOG(IPAProxy, Error) << "Failed to create IPCPipe";
			return;
		}

		ipc_->recv.connect(this, &{{proxy_name}}::recvMessage);

		valid_ = true;
		return;
	}

	if (!ipam->load())
		return;

	IPAInterface *ipai = ipam->createInterface();
	if (!ipai) {
		LOG(IPAProxy, Error)
			<< "Failed to create IPA context for " << ipam->path();
		return;
	}

	ipa_ = std::unique_ptr<{{interface_name}}>(static_cast<{{interface_name}} *>(ipai));
	proxy_.setIPA(ipa_.get());

{% for method in interface_event.methods %}
	ipa_->{{method.mojom_name}}.connect(this, &{{proxy_name}}::{{method.mojom_name}}Thread);
{%- endfor %}

	valid_ = true;
}

{{proxy_name}}::~{{proxy_name}}()
{
	if (isolate_) {
		IPCMessage::Header header =
			{ static_cast<uint32_t>({{cmd_enum_name}}::Exit), seq_++ };
		IPCMessage msg(header);
		ipc_->sendAsync(msg);
	}
}

{% if interface_event.methods|length > 0 %}
void {{proxy_name}}::recvMessage(const IPCMessage &data)
{
	size_t dataSize = data.data().size();
	{{cmd_event_enum_name}} _cmd = static_cast<{{cmd_event_enum_name}}>(data.header().cmd);

	switch (_cmd) {
{%- for method in interface_event.methods %}
	case {{cmd_event_enum_name}}::{{method.mojom_name|cap}}: {
		{{method.mojom_name}}IPC(data.data().cbegin(), dataSize, data.fds());
		break;
	}
{%- endfor %}
	default:
		LOG(IPAProxy, Error) << "Unknown command " << static_cast<uint32_t>(_cmd);
	}
}
{%- endif %}

{% for method in interface_main.methods %}
{{proxy_funcs.func_sig(proxy_name, method)}}
{
	if (isolate_)
		{{"return " if method|method_return_value != "void"}}{{method.mojom_name}}IPC(
{%- for param in method|method_param_names -%}
		{{param}}{{- ", " if not loop.last}}
{%- endfor -%}
);
	else
		{{"return " if method|method_return_value != "void"}}{{method.mojom_name}}Thread(
{%- for param in method|method_param_names -%}
		{{param}}{{- ", " if not loop.last}}
{%- endfor -%}
);
}

{{proxy_funcs.func_sig(proxy_name, method, "Thread")}}
{
{%- if method.mojom_name == "init" %}
	{{proxy_funcs.init_thread_body()}}
{%- elif method.mojom_name == "stop" %}
	{{proxy_funcs.stop_thread_body()}}
{%- elif method.mojom_name == "start" %}
	running_ = true;
	thread_.start();

	{{ "return " if method|method_return_value != "void" -}}
	proxy_.invokeMethod(&ThreadProxy::start, ConnectionTypeBlocking
	{{- ", " if method|method_param_names}}
	{%- for param in method|method_param_names -%}
		{{param}}{{- ", " if not loop.last}}
	{%- endfor -%}
);
{%- elif not method|is_async %}
	{{ "return " if method|method_return_value != "void" -}}
	ipa_->{{method.mojom_name}}(
	{%- for param in method|method_param_names -%}
		{{param}}{{- ", " if not loop.last}}
	{%- endfor -%}
);
{% elif method|is_async %}
	proxy_.invokeMethod(&ThreadProxy::{{method.mojom_name}}, ConnectionTypeQueued,
	{%- for param in method|method_param_names -%}
		{{param}}{{- ", " if not loop.last}}
	{%- endfor -%}
);
{%- endif %}
}

{{proxy_funcs.func_sig(proxy_name, method, "IPC")}}
{
{%- set has_input = true if method|method_param_inputs|length > 0 %}
{%- set has_output = true if method|method_param_outputs|length > 0 or method|method_return_value != "void" %}
{%- set cmd = cmd_enum_name + "::" + method.mojom_name|cap %}
	IPCMessage::Header _header = { static_cast<uint32_t>({{cmd}}), seq_++ };
	IPCMessage _ipcInputBuf(_header);
{%- if has_output %}
	IPCMessage _ipcOutputBuf;
{%- endif %}

{{proxy_funcs.serialize_call(method|method_param_inputs, '_ipcInputBuf.data()', '_ipcInputBuf.fds()')}}

{% if method|is_async %}
	int _ret = ipc_->sendAsync(_ipcInputBuf);
{%- else %}
	int _ret = ipc_->sendSync(_ipcInputBuf
{{- ", &_ipcOutputBuf" if has_output -}}
);
{%- endif %}
	if (_ret < 0) {
		LOG(IPAProxy, Error) << "Failed to call {{method.mojom_name}}";
{%- if method|method_return_value != "void" %}
		return static_cast<{{method|method_return_value}}>(_ret);
{%- else %}
		return;
{%- endif %}
	}
{% if method|method_return_value != "void" %}
	return IPADataSerializer<{{method.response_parameters|first|name}}>::deserialize(_ipcOutputBuf.data(), 0);
{% elif method|method_param_outputs|length > 0 %}
{{proxy_funcs.deserialize_call(method|method_param_outputs, '_ipcOutputBuf.data()', '_ipcOutputBuf.fds()')}}
{% endif -%}
}

{% endfor %}

{% for method in interface_event.methods %}
{{proxy_funcs.func_sig(proxy_name, method, "Thread")}}
{
	{{method.mojom_name}}.emit({{method.parameters|params_comma_sep}});
}

void {{proxy_name}}::{{method.mojom_name}}IPC(
	std::vector<uint8_t>::const_iterator data,
	size_t dataSize,
	[[maybe_unused]] const std::vector<int32_t> &fds)
{
{%- for param in method.parameters %}
	{{param|name}} {{param.mojom_name}};
{%- endfor %}
{{proxy_funcs.deserialize_call(method.parameters, 'data', 'fds', false, false, true, 'dataSize')}}
	{{method.mojom_name}}.emit({{method.parameters|params_comma_sep}});
}
{% endfor %}

{%- if has_namespace %}
{% for ns in namespace|reverse %}
} /* namespace {{ns}} */
{% endfor %}
{%- endif %}
} /* namespace libcamera */