CameraFeed: Create new texture on datatype change

This commit is contained in:
Jason Kuo
2026-01-08 18:03:20 +08:00
parent d5edd4a592
commit d94a50bd5a

View File

@@ -189,21 +189,17 @@ void CameraFeed::set_rgb_image(const Ref<Image> &p_rgb_img) {
int new_width = p_rgb_img->get_width();
int new_height = p_rgb_img->get_height();
// Emit `format_changed` signal if feed datatype or frame size is changed.
// The signal is deferred to ensure:
// - They are emitted on Godot's main thread.
// - Both datatype and frame size are updated before the emission.
if (datatype != CameraFeed::FEED_RGB || (base_width != new_width) || (base_height != new_height)) {
call_deferred("emit_signal", format_changed_signal_name);
}
if ((base_width != new_width) || (base_height != new_height)) {
// We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
base_width = new_width;
base_height = new_height;
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_rgb_img);
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
// `format_changed` signal is deferred to ensure:
// - They are emitted on Godot's main thread.
// - Both datatype and frame size are updated before the emission.
call_deferred("emit_signal", format_changed_signal_name);
} else {
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img);
}
@@ -221,23 +217,19 @@ void CameraFeed::set_ycbcr_image(const Ref<Image> &p_ycbcr_img) {
int new_width = p_ycbcr_img->get_width();
int new_height = p_ycbcr_img->get_height();
// Emit `format_changed` signal if feed datatype or frame size is changed.
// The signal is deferred to ensure:
// - They are emitted on Godot's main thread.
// - Both datatype and frame size are updated before the emission.
if (datatype != CameraFeed::FEED_YCBCR || (base_width != new_width) || (base_height != new_height)) {
call_deferred("emit_signal", format_changed_signal_name);
}
if ((base_width != new_width) || (base_height != new_height)) {
// We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
base_width = new_width;
base_height = new_height;
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_ycbcr_img);
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_YCBCR_IMAGE], new_texture);
// `format_changed` signal is deferred to ensure:
// - They are emitted on Godot's main thread.
// - Both datatype and frame size are updated before the emission.
call_deferred("emit_signal", format_changed_signal_name);
} else {
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_ycbcr_img);
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_YCBCR_IMAGE], p_ycbcr_img);
}
datatype = CameraFeed::FEED_YCBCR;
@@ -258,16 +250,7 @@ void CameraFeed::set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p
int new_y_width = p_y_img->get_width();
int new_y_height = p_y_img->get_height();
// Emit `format_changed` signal if feed datatype or frame size is changed.
// The signal is deferred to ensure:
// - They are emitted on Godot's main thread.
// - Both datatype and frame size are updated before the emission.
if (datatype != CameraFeed::FEED_YCBCR_SEP || (base_width != new_y_width) || (base_height != new_y_height)) {
call_deferred("emit_signal", format_changed_signal_name);
}
if ((base_width != new_y_width) || (base_height != new_y_height)) {
// We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
base_width = new_y_width;
base_height = new_y_height;
{
@@ -278,6 +261,11 @@ void CameraFeed::set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_cbcr_img);
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_CBCR_IMAGE], new_texture);
}
// `format_changed` signal is deferred to ensure:
// - They are emitted on Godot's main thread.
// - Both datatype and frame size are updated before the emission.
call_deferred("emit_signal", format_changed_signal_name);
} else {
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_Y_IMAGE], p_y_img);
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_CBCR_IMAGE], p_cbcr_img);
@@ -291,21 +279,17 @@ void CameraFeed::set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p
}
void CameraFeed::set_external(int p_width, int p_height) {
// Emit `format_changed` signal if feed datatype or frame size is changed.
// The signal is deferred to ensure:
// - They are emitted on Godot's main thread.
// - Both datatype and frame size are updated before the emission.
if (datatype != CameraFeed::FEED_EXTERNAL || (base_width != p_width) || (base_height != p_height)) {
call_deferred("emit_signal", format_changed_signal_name);
}
if ((base_width != p_width) || (base_height != p_height)) {
// We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
base_width = p_width;
base_height = p_height;
RID new_texture = RenderingServer::get_singleton()->texture_external_create(p_width, p_height, 0);
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_YCBCR_IMAGE], new_texture);
// `format_changed` signal is deferred to ensure:
// - They are emitted on Godot's main thread.
// - Both datatype and frame size are updated before the emission.
call_deferred("emit_signal", format_changed_signal_name);
}
datatype = CameraFeed::FEED_EXTERNAL;