Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/camera/camera_feed.cpp
10277 views
1
/**************************************************************************/
2
/* camera_feed.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "camera_feed.h"
32
33
#include "servers/rendering_server.h"
34
35
void CameraFeed::_bind_methods() {
36
ClassDB::bind_method(D_METHOD("get_id"), &CameraFeed::get_id);
37
38
ClassDB::bind_method(D_METHOD("is_active"), &CameraFeed::is_active);
39
ClassDB::bind_method(D_METHOD("set_active", "active"), &CameraFeed::set_active);
40
41
ClassDB::bind_method(D_METHOD("get_name"), &CameraFeed::get_name);
42
ClassDB::bind_method(D_METHOD("set_name", "name"), &CameraFeed::set_name);
43
44
ClassDB::bind_method(D_METHOD("get_position"), &CameraFeed::get_position);
45
ClassDB::bind_method(D_METHOD("set_position", "position"), &CameraFeed::set_position);
46
47
// Note, for transform some feeds may override what the user sets (such as ARKit)
48
ClassDB::bind_method(D_METHOD("get_transform"), &CameraFeed::get_transform);
49
ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CameraFeed::set_transform);
50
51
ClassDB::bind_method(D_METHOD("set_rgb_image", "rgb_image"), &CameraFeed::set_rgb_image);
52
ClassDB::bind_method(D_METHOD("set_ycbcr_image", "ycbcr_image"), &CameraFeed::set_ycbcr_image);
53
ClassDB::bind_method(D_METHOD("set_external", "width", "height"), &CameraFeed::set_external);
54
ClassDB::bind_method(D_METHOD("get_texture_tex_id", "feed_image_type"), &CameraFeed::get_texture_tex_id);
55
56
ClassDB::bind_method(D_METHOD("get_datatype"), &CameraFeed::get_datatype);
57
58
ClassDB::bind_method(D_METHOD("get_formats"), &CameraFeed::get_formats);
59
ClassDB::bind_method(D_METHOD("set_format", "index", "parameters"), &CameraFeed::set_format);
60
61
GDVIRTUAL_BIND(_activate_feed);
62
GDVIRTUAL_BIND(_deactivate_feed);
63
64
ADD_SIGNAL(MethodInfo("frame_changed"));
65
ADD_SIGNAL(MethodInfo("format_changed"));
66
67
ADD_GROUP("Feed", "feed_");
68
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active");
69
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform");
70
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "formats"), "", "get_formats");
71
72
BIND_ENUM_CONSTANT(FEED_NOIMAGE);
73
BIND_ENUM_CONSTANT(FEED_RGB);
74
BIND_ENUM_CONSTANT(FEED_YCBCR);
75
BIND_ENUM_CONSTANT(FEED_YCBCR_SEP);
76
BIND_ENUM_CONSTANT(FEED_EXTERNAL);
77
78
BIND_ENUM_CONSTANT(FEED_UNSPECIFIED);
79
BIND_ENUM_CONSTANT(FEED_FRONT);
80
BIND_ENUM_CONSTANT(FEED_BACK);
81
}
82
83
int CameraFeed::get_id() const {
84
return id;
85
}
86
87
bool CameraFeed::is_active() const {
88
return active;
89
}
90
91
void CameraFeed::set_active(bool p_is_active) {
92
if (p_is_active == active) {
93
// all good
94
} else if (p_is_active) {
95
// attempt to activate this feed
96
if (activate_feed()) {
97
active = true;
98
}
99
} else {
100
// just deactivate it
101
deactivate_feed();
102
active = false;
103
}
104
}
105
106
String CameraFeed::get_name() const {
107
return name;
108
}
109
110
void CameraFeed::set_name(String p_name) {
111
name = p_name;
112
}
113
114
int CameraFeed::get_base_width() const {
115
return base_width;
116
}
117
118
int CameraFeed::get_base_height() const {
119
return base_height;
120
}
121
122
CameraFeed::FeedDataType CameraFeed::get_datatype() const {
123
return datatype;
124
}
125
126
CameraFeed::FeedPosition CameraFeed::get_position() const {
127
return position;
128
}
129
130
void CameraFeed::set_position(CameraFeed::FeedPosition p_position) {
131
position = p_position;
132
}
133
134
Transform2D CameraFeed::get_transform() const {
135
return transform;
136
}
137
138
void CameraFeed::set_transform(const Transform2D &p_transform) {
139
transform = p_transform;
140
}
141
142
RID CameraFeed::get_texture(CameraServer::FeedImage p_which) {
143
return texture[p_which];
144
}
145
146
uint64_t CameraFeed::get_texture_tex_id(CameraServer::FeedImage p_which) {
147
return RenderingServer::get_singleton()->texture_get_native_handle(texture[p_which]);
148
}
149
150
CameraFeed::CameraFeed() {
151
// initialize our feed
152
id = CameraServer::get_singleton()->get_free_id();
153
base_width = 0;
154
base_height = 0;
155
name = "???";
156
active = false;
157
datatype = CameraFeed::FEED_RGB;
158
position = CameraFeed::FEED_UNSPECIFIED;
159
transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
160
texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
161
texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
162
}
163
164
CameraFeed::CameraFeed(String p_name, FeedPosition p_position) {
165
// initialize our feed
166
id = CameraServer::get_singleton()->get_free_id();
167
base_width = 0;
168
base_height = 0;
169
name = p_name;
170
active = false;
171
datatype = CameraFeed::FEED_NOIMAGE;
172
position = p_position;
173
transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
174
texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
175
texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
176
}
177
178
CameraFeed::~CameraFeed() {
179
// Free our textures
180
ERR_FAIL_NULL(RenderingServer::get_singleton());
181
RenderingServer::get_singleton()->free(texture[CameraServer::FEED_Y_IMAGE]);
182
RenderingServer::get_singleton()->free(texture[CameraServer::FEED_CBCR_IMAGE]);
183
}
184
185
void CameraFeed::set_rgb_image(const Ref<Image> &p_rgb_img) {
186
ERR_FAIL_COND(p_rgb_img.is_null());
187
if (active) {
188
int new_width = p_rgb_img->get_width();
189
int new_height = p_rgb_img->get_height();
190
191
if ((base_width != new_width) || (base_height != new_height)) {
192
// We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
193
base_width = new_width;
194
base_height = new_height;
195
196
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_rgb_img);
197
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
198
199
// Defer `format_changed` signals to ensure they are emitted on Godot's main thread.
200
// This also makes sure the datatype of the feed is updated before the emission.
201
call_deferred("emit_signal", format_changed_signal_name);
202
} else {
203
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img);
204
}
205
206
datatype = CameraFeed::FEED_RGB;
207
// Most of the time the pixel data of camera devices comes from threads outside Godot.
208
// Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
209
call_deferred("emit_signal", frame_changed_signal_name);
210
}
211
}
212
213
void CameraFeed::set_ycbcr_image(const Ref<Image> &p_ycbcr_img) {
214
ERR_FAIL_COND(p_ycbcr_img.is_null());
215
if (active) {
216
int new_width = p_ycbcr_img->get_width();
217
int new_height = p_ycbcr_img->get_height();
218
219
if ((base_width != new_width) || (base_height != new_height)) {
220
// We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
221
base_width = new_width;
222
base_height = new_height;
223
224
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_ycbcr_img);
225
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
226
227
// Defer `format_changed` signals to ensure they are emitted on Godot's main thread.
228
// This also makes sure the datatype of the feed is updated before the emission.
229
call_deferred("emit_signal", format_changed_signal_name);
230
} else {
231
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_ycbcr_img);
232
}
233
234
datatype = CameraFeed::FEED_YCBCR;
235
// Most of the time the pixel data of camera devices comes from threads outside Godot.
236
// Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
237
call_deferred("emit_signal", frame_changed_signal_name);
238
}
239
}
240
241
void CameraFeed::set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img) {
242
ERR_FAIL_COND(p_y_img.is_null());
243
ERR_FAIL_COND(p_cbcr_img.is_null());
244
if (active) {
245
///@TODO investigate whether we can use thirdparty/misc/yuv2rgb.h here to convert our YUV data to RGB, our shader approach is potentially faster though..
246
// Wondering about including that into multiple projects, may cause issues.
247
// That said, if we convert to RGB, we could enable using texture resources again...
248
249
int new_y_width = p_y_img->get_width();
250
int new_y_height = p_y_img->get_height();
251
252
if ((base_width != new_y_width) || (base_height != new_y_height)) {
253
// We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
254
base_width = new_y_width;
255
base_height = new_y_height;
256
{
257
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_y_img);
258
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_Y_IMAGE], new_texture);
259
}
260
{
261
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_cbcr_img);
262
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_CBCR_IMAGE], new_texture);
263
}
264
265
// Defer `format_changed` signals to ensure they are emitted on Godot's main thread.
266
// This also makes sure the datatype of the feed is updated before the emission.
267
call_deferred("emit_signal", format_changed_signal_name);
268
} else {
269
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_Y_IMAGE], p_y_img);
270
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_CBCR_IMAGE], p_cbcr_img);
271
}
272
273
datatype = CameraFeed::FEED_YCBCR_SEP;
274
// Most of the time the pixel data of camera devices comes from threads outside Godot.
275
// Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
276
call_deferred("emit_signal", frame_changed_signal_name);
277
}
278
}
279
280
void CameraFeed::set_external(int p_width, int p_height) {
281
if ((base_width != p_width) || (base_height != p_height)) {
282
// We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
283
base_width = p_width;
284
base_height = p_height;
285
286
RID new_texture = RenderingServer::get_singleton()->texture_external_create(p_width, p_height, 0);
287
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_YCBCR_IMAGE], new_texture);
288
}
289
290
datatype = CameraFeed::FEED_EXTERNAL;
291
// Most of the time the pixel data of camera devices comes from threads outside Godot.
292
// Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
293
call_deferred("emit_signal", frame_changed_signal_name);
294
}
295
296
bool CameraFeed::activate_feed() {
297
bool ret = true;
298
GDVIRTUAL_CALL(_activate_feed, ret);
299
return ret;
300
}
301
302
void CameraFeed::deactivate_feed() {
303
GDVIRTUAL_CALL(_deactivate_feed);
304
}
305
306
bool CameraFeed::set_format(int p_index, const Dictionary &p_parameters) {
307
return false;
308
}
309
310
Array CameraFeed::get_formats() const {
311
return Array();
312
}
313
314
CameraFeed::FeedFormat CameraFeed::get_format() const {
315
FeedFormat feed_format = {};
316
return feed_format;
317
}
318
319