summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/classes/CameraFeed.xml26
-rw-r--r--doc/classes/CameraServer.xml2
-rw-r--r--doc/classes/Environment.xml2
-rw-r--r--doc/classes/RenderingServer.xml2
-rw-r--r--drivers/gles3/shaders/tonemap_inc.glsl8
-rw-r--r--modules/camera/SCsub10
-rw-r--r--modules/camera/buffer_decoder.cpp212
-rw-r--r--modules/camera/buffer_decoder.h116
-rw-r--r--modules/camera/camera_feed_linux.cpp363
-rw-r--r--modules/camera/camera_feed_linux.h78
-rw-r--r--modules/camera/camera_linux.cpp169
-rw-r--r--modules/camera/camera_linux.h60
-rw-r--r--modules/camera/config.py2
-rw-r--r--modules/camera/register_types.cpp6
-rw-r--r--modules/multiplayer/editor/editor_network_profiler.cpp13
-rw-r--r--modules/openxr/editor/openxr_select_action_dialog.cpp3
-rw-r--r--modules/openxr/editor/openxr_select_interaction_profile_dialog.cpp32
-rw-r--r--modules/openxr/editor/openxr_select_interaction_profile_dialog.h1
-rw-r--r--pyproject.toml1
-rw-r--r--scene/2d/parallax_2d.cpp6
-rw-r--r--scene/gui/color_picker.cpp15
-rw-r--r--scene/resources/animation.cpp14
-rw-r--r--scene/resources/camera_texture.cpp24
-rw-r--r--scene/resources/camera_texture.h1
-rw-r--r--servers/camera/camera_feed.cpp28
-rw-r--r--servers/camera/camera_feed.h20
-rw-r--r--servers/rendering/renderer_rd/shaders/effects/tonemap.glsl8
-rw-r--r--servers/rendering/renderer_viewport.cpp17
28 files changed, 1189 insertions, 50 deletions
diff --git a/doc/classes/CameraFeed.xml b/doc/classes/CameraFeed.xml
index 974f6d4a33..8033c0880b 100644
--- a/doc/classes/CameraFeed.xml
+++ b/doc/classes/CameraFeed.xml
@@ -34,6 +34,17 @@
Returns the position of camera on the device.
</description>
</method>
+ <method name="set_format">
+ <return type="bool" />
+ <param index="0" name="index" type="int" />
+ <param index="1" name="parameters" type="Dictionary" />
+ <description>
+ Sets the feed format parameters for the given index in the [member formats] array. Returns [code]true[/code] on success. By default YUYV encoded stream is transformed to FEED_RGB. YUYV encoded stream output format can be changed with [param parameters].output value:
+ [code]separate[/code] will result in FEED_YCBCR_SEP
+ [code]grayscale[/code] will result in desaturated FEED_RGB
+ [code]copy[/code] will result in FEED_YCBCR
+ </description>
+ </method>
</methods>
<members>
<member name="feed_is_active" type="bool" setter="set_active" getter="is_active" default="false">
@@ -42,7 +53,22 @@
<member name="feed_transform" type="Transform2D" setter="set_transform" getter="get_transform" default="Transform2D(1, 0, 0, -1, 0, 1)">
The transform applied to the camera's image.
</member>
+ <member name="formats" type="Array" setter="" getter="get_formats" default="[]">
+ Formats supported by the feed. Each entry is a [Dictionary] describing format parameters.
+ </member>
</members>
+ <signals>
+ <signal name="format_changed">
+ <description>
+ Emitted when the format has changed.
+ </description>
+ </signal>
+ <signal name="frame_changed">
+ <description>
+ Emitted when a new frame is available.
+ </description>
+ </signal>
+ </signals>
<constants>
<constant name="FEED_NOIMAGE" value="0" enum="FeedDataType">
No image set for the feed.
diff --git a/doc/classes/CameraServer.xml b/doc/classes/CameraServer.xml
index 020b5d887b..b09010147e 100644
--- a/doc/classes/CameraServer.xml
+++ b/doc/classes/CameraServer.xml
@@ -6,7 +6,7 @@
<description>
The [CameraServer] keeps track of different cameras accessible in Godot. These are external cameras such as webcams or the cameras on your phone.
It is notably used to provide AR modules with a video feed from the camera.
- [b]Note:[/b] This class is currently only implemented on macOS and iOS. To get a [CameraFeed] on iOS, the camera plugin from [url=https://github.com/godotengine/godot-ios-plugins]godot-ios-plugins[/url] is required. On other platforms, no [CameraFeed]s will be available.
+ [b]Note:[/b] This class is currently only implemented on Linux, macOS, and iOS, on other platforms no [CameraFeed]s will be available. To get a [CameraFeed] on iOS, the camera plugin from [url=https://github.com/godotengine/godot-ios-plugins]godot-ios-plugins[/url] is required.
</description>
<tutorials>
</tutorials>
diff --git a/doc/classes/Environment.xml b/doc/classes/Environment.xml
index 8c26509812..de3295fbe0 100644
--- a/doc/classes/Environment.xml
+++ b/doc/classes/Environment.xml
@@ -414,7 +414,7 @@
Linear tonemapper operator. Reads the linear data and passes it on unmodified. This can cause bright lighting to look blown out, with noticeable clipping in the output colors.
</constant>
<constant name="TONE_MAPPER_REINHARDT" value="1" enum="ToneMapper">
- Reinhardt tonemapper operator. Performs a variation on rendered pixels' colors by this formula: [code]color = color / (1 + color)[/code]. This avoids clipping bright highlights, but the resulting image can look a bit dull.
+ Reinhard tonemapper operator. Performs a variation on rendered pixels' colors by this formula: [code]color = color * (1 + color / (white * white)) / (1 + color)[/code]. This avoids clipping bright highlights, but the resulting image can look a bit dull. When [member tonemap_white] is left at the default value of [code]1.0[/code] this is identical to [constant TONE_MAPPER_LINEAR] while also being slightly less performant.
</constant>
<constant name="TONE_MAPPER_FILMIC" value="2" enum="ToneMapper">
Filmic tonemapper operator. This avoids clipping bright highlights, with a resulting image that usually looks more vivid than [constant TONE_MAPPER_REINHARDT].
diff --git a/doc/classes/RenderingServer.xml b/doc/classes/RenderingServer.xml
index 144f78349f..4b2ce6f45e 100644
--- a/doc/classes/RenderingServer.xml
+++ b/doc/classes/RenderingServer.xml
@@ -5219,7 +5219,7 @@
Output color as they came in. This can cause bright lighting to look blown out, with noticeable clipping in the output colors.
</constant>
<constant name="ENV_TONE_MAPPER_REINHARD" value="1" enum="EnvironmentToneMapper">
- Use the Reinhard tonemapper. Performs a variation on rendered pixels' colors by this formula: [code]color = color / (1 + color)[/code]. This avoids clipping bright highlights, but the resulting image can look a bit dull.
+ Use the Reinhard tonemapper. Performs a variation on rendered pixels' colors by this formula: [code]color = color * (1 + color / (white * white)) / (1 + color)[/code]. This avoids clipping bright highlights, but the resulting image can look a bit dull. When [member Environment.tonemap_white] is left at the default value of [code]1.0[/code] this is identical to [constant ENV_TONE_MAPPER_LINEAR] while also being slightly less performant.
</constant>
<constant name="ENV_TONE_MAPPER_FILMIC" value="2" enum="EnvironmentToneMapper">
Use the filmic tonemapper. This avoids clipping bright highlights, with a resulting image that usually looks more vivid than [constant ENV_TONE_MAPPER_REINHARD].
diff --git a/drivers/gles3/shaders/tonemap_inc.glsl b/drivers/gles3/shaders/tonemap_inc.glsl
index fb915aeb38..6738bdf748 100644
--- a/drivers/gles3/shaders/tonemap_inc.glsl
+++ b/drivers/gles3/shaders/tonemap_inc.glsl
@@ -76,8 +76,12 @@ vec3 tonemap_aces(vec3 color, float p_white) {
return color_tonemapped / p_white_tonemapped;
}
+// Based on Reinhard's extended formula, see equation 4 in https://doi.org/cjbgrt
vec3 tonemap_reinhard(vec3 color, float p_white) {
- return (p_white * color + color) / (color * p_white + p_white);
+ float white_squared = p_white * p_white;
+ vec3 white_squared_color = white_squared * color;
+ // Equivalent to color * (1 + color / white_squared) / (1 + color)
+ return (white_squared_color + color * color) / (white_squared_color + white_squared);
}
#define TONEMAPPER_LINEAR 0
@@ -85,7 +89,7 @@ vec3 tonemap_reinhard(vec3 color, float p_white) {
#define TONEMAPPER_FILMIC 2
#define TONEMAPPER_ACES 3
-vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color
+vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR
// Ensure color values passed to tonemappers are positive.
// They can be negative in the case of negative lights, which leads to undesired behavior.
if (tonemapper == TONEMAPPER_LINEAR) {
diff --git a/modules/camera/SCsub b/modules/camera/SCsub
index 9a6147d433..ba7fca8794 100644
--- a/modules/camera/SCsub
+++ b/modules/camera/SCsub
@@ -5,10 +5,16 @@ Import("env_modules")
env_camera = env_modules.Clone()
-if env["platform"] == "windows":
+if env["platform"] in ["windows", "macos", "linuxbsd"]:
env_camera.add_source_files(env.modules_sources, "register_types.cpp")
+
+if env["platform"] == "windows":
env_camera.add_source_files(env.modules_sources, "camera_win.cpp")
elif env["platform"] == "macos":
- env_camera.add_source_files(env.modules_sources, "register_types.cpp")
env_camera.add_source_files(env.modules_sources, "camera_macos.mm")
+
+elif env["platform"] == "linuxbsd":
+ env_camera.add_source_files(env.modules_sources, "camera_linux.cpp")
+ env_camera.add_source_files(env.modules_sources, "camera_feed_linux.cpp")
+ env_camera.add_source_files(env.modules_sources, "buffer_decoder.cpp")
diff --git a/modules/camera/buffer_decoder.cpp b/modules/camera/buffer_decoder.cpp
new file mode 100644
index 0000000000..024a68f080
--- /dev/null
+++ b/modules/camera/buffer_decoder.cpp
@@ -0,0 +1,212 @@
+/**************************************************************************/
+/* buffer_decoder.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#include "buffer_decoder.h"
+
+#include "servers/camera/camera_feed.h"
+
+#include <linux/videodev2.h>
+
+BufferDecoder::BufferDecoder(CameraFeed *p_camera_feed) {
+ camera_feed = p_camera_feed;
+ width = camera_feed->get_format().width;
+ height = camera_feed->get_format().height;
+ image.instantiate();
+}
+
+AbstractYuyvBufferDecoder::AbstractYuyvBufferDecoder(CameraFeed *p_camera_feed) :
+ BufferDecoder(p_camera_feed) {
+ switch (camera_feed->get_format().pixel_format) {
+ case V4L2_PIX_FMT_YYUV:
+ component_indexes = new int[4]{ 0, 1, 2, 3 };
+ break;
+ case V4L2_PIX_FMT_YVYU:
+ component_indexes = new int[4]{ 0, 2, 3, 1 };
+ break;
+ case V4L2_PIX_FMT_UYVY:
+ component_indexes = new int[4]{ 1, 3, 0, 2 };
+ break;
+ case V4L2_PIX_FMT_VYUY:
+ component_indexes = new int[4]{ 1, 3, 2, 0 };
+ break;
+ default:
+ component_indexes = new int[4]{ 0, 2, 1, 3 };
+ }
+}
+
+AbstractYuyvBufferDecoder::~AbstractYuyvBufferDecoder() {
+ delete[] component_indexes;
+}
+
+SeparateYuyvBufferDecoder::SeparateYuyvBufferDecoder(CameraFeed *p_camera_feed) :
+ AbstractYuyvBufferDecoder(p_camera_feed) {
+ y_image_data.resize(width * height);
+ cbcr_image_data.resize(width * height);
+ y_image.instantiate();
+ cbcr_image.instantiate();
+}
+
+void SeparateYuyvBufferDecoder::decode(StreamingBuffer p_buffer) {
+ uint8_t *y_dst = (uint8_t *)y_image_data.ptrw();
+ uint8_t *uv_dst = (uint8_t *)cbcr_image_data.ptrw();
+ uint8_t *src = (uint8_t *)p_buffer.start;
+ uint8_t *y0_src = src + component_indexes[0];
+ uint8_t *y1_src = src + component_indexes[1];
+ uint8_t *u_src = src + component_indexes[2];
+ uint8_t *v_src = src + component_indexes[3];
+
+ for (int i = 0; i < width * height; i += 2) {
+ *y_dst++ = *y0_src;
+ *y_dst++ = *y1_src;
+ *uv_dst++ = *u_src;
+ *uv_dst++ = *v_src;
+
+ y0_src += 4;
+ y1_src += 4;
+ u_src += 4;
+ v_src += 4;
+ }
+
+ if (y_image.is_valid()) {
+ y_image->set_data(width, height, false, Image::FORMAT_L8, y_image_data);
+ } else {
+ y_image.instantiate(width, height, false, Image::FORMAT_RGB8, y_image_data);
+ }
+ if (cbcr_image.is_valid()) {
+ cbcr_image->set_data(width, height, false, Image::FORMAT_L8, cbcr_image_data);
+ } else {
+ cbcr_image.instantiate(width, height, false, Image::FORMAT_RGB8, cbcr_image_data);
+ }
+
+ camera_feed->set_YCbCr_imgs(y_image, cbcr_image);
+}
+
+YuyvToGrayscaleBufferDecoder::YuyvToGrayscaleBufferDecoder(CameraFeed *p_camera_feed) :
+ AbstractYuyvBufferDecoder(p_camera_feed) {
+ image_data.resize(width * height);
+}
+
+void YuyvToGrayscaleBufferDecoder::decode(StreamingBuffer p_buffer) {
+ uint8_t *dst = (uint8_t *)image_data.ptrw();
+ uint8_t *src = (uint8_t *)p_buffer.start;
+ uint8_t *y0_src = src + component_indexes[0];
+ uint8_t *y1_src = src + component_indexes[1];
+
+ for (int i = 0; i < width * height; i += 2) {
+ *dst++ = *y0_src;
+ *dst++ = *y1_src;
+
+ y0_src += 4;
+ y1_src += 4;
+ }
+
+ if (image.is_valid()) {
+ image->set_data(width, height, false, Image::FORMAT_L8, image_data);
+ } else {
+ image.instantiate(width, height, false, Image::FORMAT_RGB8, image_data);
+ }
+
+ camera_feed->set_RGB_img(image);
+}
+
+YuyvToRgbBufferDecoder::YuyvToRgbBufferDecoder(CameraFeed *p_camera_feed) :
+ AbstractYuyvBufferDecoder(p_camera_feed) {
+ image_data.resize(width * height * 3);
+}
+
+void YuyvToRgbBufferDecoder::decode(StreamingBuffer p_buffer) {
+ uint8_t *src = (uint8_t *)p_buffer.start;
+ uint8_t *y0_src = src + component_indexes[0];
+ uint8_t *y1_src = src + component_indexes[1];
+ uint8_t *u_src = src + component_indexes[2];
+ uint8_t *v_src = src + component_indexes[3];
+ uint8_t *dst = (uint8_t *)image_data.ptrw();
+
+ for (int i = 0; i < width * height; i += 2) {
+ int u = *u_src;
+ int v = *v_src;
+ int u1 = (((u - 128) << 7) + (u - 128)) >> 6;
+ int rg = (((u - 128) << 1) + (u - 128) + ((v - 128) << 2) + ((v - 128) << 1)) >> 3;
+ int v1 = (((v - 128) << 1) + (v - 128)) >> 1;
+
+ *dst++ = CLAMP(*y0_src + v1, 0, 255);
+ *dst++ = CLAMP(*y0_src - rg, 0, 255);
+ *dst++ = CLAMP(*y0_src + u1, 0, 255);
+
+ *dst++ = CLAMP(*y1_src + v1, 0, 255);
+ *dst++ = CLAMP(*y1_src - rg, 0, 255);
+ *dst++ = CLAMP(*y1_src + u1, 0, 255);
+
+ y0_src += 4;
+ y1_src += 4;
+ u_src += 4;
+ v_src += 4;
+ }
+
+ if (image.is_valid()) {
+ image->set_data(width, height, false, Image::FORMAT_RGB8, image_data);
+ } else {
+ image.instantiate(width, height, false, Image::FORMAT_RGB8, image_data);
+ }
+
+ camera_feed->set_RGB_img(image);
+}
+
+CopyBufferDecoder::CopyBufferDecoder(CameraFeed *p_camera_feed, bool p_rgba) :
+ BufferDecoder(p_camera_feed) {
+ rgba = p_rgba;
+ image_data.resize(width * height * (rgba ? 4 : 2));
+}
+
+void CopyBufferDecoder::decode(StreamingBuffer p_buffer) {
+ uint8_t *dst = (uint8_t *)image_data.ptrw();
+ memcpy(dst, p_buffer.start, p_buffer.length);
+
+ if (image.is_valid()) {
+ image->set_data(width, height, false, rgba ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8, image_data);
+ } else {
+ image.instantiate(width, height, false, rgba ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8, image_data);
+ }
+
+ camera_feed->set_RGB_img(image);
+}
+
+JpegBufferDecoder::JpegBufferDecoder(CameraFeed *p_camera_feed) :
+ BufferDecoder(p_camera_feed) {
+}
+
+void JpegBufferDecoder::decode(StreamingBuffer p_buffer) {
+ image_data.resize(p_buffer.length);
+ uint8_t *dst = (uint8_t *)image_data.ptrw();
+ memcpy(dst, p_buffer.start, p_buffer.length);
+ if (image->load_jpg_from_buffer(image_data) == OK) {
+ camera_feed->set_RGB_img(image);
+ }
+}
diff --git a/modules/camera/buffer_decoder.h b/modules/camera/buffer_decoder.h
new file mode 100644
index 0000000000..97cc66b6da
--- /dev/null
+++ b/modules/camera/buffer_decoder.h
@@ -0,0 +1,116 @@
+/**************************************************************************/
+/* buffer_decoder.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifndef BUFFER_DECODER_H
+#define BUFFER_DECODER_H
+
+#include "core/io/image.h"
+#include "core/templates/vector.h"
+
+class CameraFeed;
+
+struct StreamingBuffer {
+ void *start = nullptr;
+ size_t length = 0;
+};
+
+class BufferDecoder {
+protected:
+ CameraFeed *camera_feed = nullptr;
+ Ref<Image> image;
+ int width = 0;
+ int height = 0;
+
+public:
+ virtual void decode(StreamingBuffer p_buffer) = 0;
+
+ BufferDecoder(CameraFeed *p_camera_feed);
+ virtual ~BufferDecoder() {}
+};
+
+class AbstractYuyvBufferDecoder : public BufferDecoder {
+protected:
+ int *component_indexes = nullptr;
+
+public:
+ AbstractYuyvBufferDecoder(CameraFeed *p_camera_feed);
+ ~AbstractYuyvBufferDecoder();
+};
+
+class SeparateYuyvBufferDecoder : public AbstractYuyvBufferDecoder {
+private:
+ Vector<uint8_t> y_image_data;
+ Vector<uint8_t> cbcr_image_data;
+ Ref<Image> y_image;
+ Ref<Image> cbcr_image;
+
+public:
+ SeparateYuyvBufferDecoder(CameraFeed *p_camera_feed);
+ virtual void decode(StreamingBuffer p_buffer) override;
+};
+
+class YuyvToGrayscaleBufferDecoder : public AbstractYuyvBufferDecoder {
+private:
+ Vector<uint8_t> image_data;
+
+public:
+ YuyvToGrayscaleBufferDecoder(CameraFeed *p_camera_feed);
+ virtual void decode(StreamingBuffer p_buffer) override;
+};
+
+class YuyvToRgbBufferDecoder : public AbstractYuyvBufferDecoder {
+private:
+ Vector<uint8_t> image_data;
+
+public:
+ YuyvToRgbBufferDecoder(CameraFeed *p_camera_feed);
+ virtual void decode(StreamingBuffer p_buffer) override;
+};
+
+class CopyBufferDecoder : public BufferDecoder {
+private:
+ Vector<uint8_t> image_data;
+ bool rgba = false;
+
+public:
+ CopyBufferDecoder(CameraFeed *p_camera_feed, bool p_rgba);
+ virtual void decode(StreamingBuffer p_buffer) override;
+};
+
+class JpegBufferDecoder : public BufferDecoder {
+private:
+ Vector<uint8_t> image_data;
+
+public:
+ JpegBufferDecoder(CameraFeed *p_camera_feed);
+ virtual void decode(StreamingBuffer p_buffer) override;
+};
+
+#endif // BUFFER_DECODER_H
diff --git a/modules/camera/camera_feed_linux.cpp b/modules/camera/camera_feed_linux.cpp
new file mode 100644
index 0000000000..9ed8eb0d0a
--- /dev/null
+++ b/modules/camera/camera_feed_linux.cpp
@@ -0,0 +1,363 @@
+/**************************************************************************/
+/* camera_feed_linux.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#include "camera_feed_linux.h"
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+void CameraFeedLinux::update_buffer_thread_func(void *p_func) {
+ if (p_func) {
+ CameraFeedLinux *camera_feed_linux = (CameraFeedLinux *)p_func;
+ camera_feed_linux->_update_buffer();
+ }
+}
+
+void CameraFeedLinux::_update_buffer() {
+ while (!exit_flag.is_set()) {
+ _read_frame();
+ usleep(10000);
+ }
+}
+
+void CameraFeedLinux::_query_device(const String &p_device_name) {
+ file_descriptor = open(p_device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
+ ERR_FAIL_COND_MSG(file_descriptor == -1, vformat("Cannot open file descriptor for %s. Error: %d.", p_device_name, errno));
+
+ struct v4l2_capability capability;
+ if (ioctl(file_descriptor, VIDIOC_QUERYCAP, &capability) == -1) {
+ ERR_FAIL_MSG(vformat("Cannot query device. Error: %d.", errno));
+ }
+ name = String((char *)capability.card);
+
+ for (int index = 0;; index++) {
+ struct v4l2_fmtdesc fmtdesc;
+ memset(&fmtdesc, 0, sizeof(fmtdesc));
+ fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ fmtdesc.index = index;
+
+ if (ioctl(file_descriptor, VIDIOC_ENUM_FMT, &fmtdesc) == -1) {
+ break;
+ }
+
+ for (int res_index = 0;; res_index++) {
+ struct v4l2_frmsizeenum frmsizeenum;
+ memset(&frmsizeenum, 0, sizeof(frmsizeenum));
+ frmsizeenum.pixel_format = fmtdesc.pixelformat;
+ frmsizeenum.index = res_index;
+
+ if (ioctl(file_descriptor, VIDIOC_ENUM_FRAMESIZES, &frmsizeenum) == -1) {
+ break;
+ }
+
+ for (int framerate_index = 0;; framerate_index++) {
+ struct v4l2_frmivalenum frmivalenum;
+ memset(&frmivalenum, 0, sizeof(frmivalenum));
+ frmivalenum.pixel_format = fmtdesc.pixelformat;
+ frmivalenum.width = frmsizeenum.discrete.width;
+ frmivalenum.height = frmsizeenum.discrete.height;
+ frmivalenum.index = framerate_index;
+
+ if (ioctl(file_descriptor, VIDIOC_ENUM_FRAMEINTERVALS, &frmivalenum) == -1) {
+ if (framerate_index == 0) {
+ _add_format(fmtdesc, frmsizeenum.discrete, -1, 1);
+ }
+ break;
+ }
+
+ _add_format(fmtdesc, frmsizeenum.discrete, frmivalenum.discrete.numerator, frmivalenum.discrete.denominator);
+ }
+ }
+ }
+
+ close(file_descriptor);
+}
+
+void CameraFeedLinux::_add_format(v4l2_fmtdesc p_description, v4l2_frmsize_discrete p_size, int p_frame_numerator, int p_frame_denominator) {
+ FeedFormat feed_format;
+ feed_format.width = p_size.width;
+ feed_format.height = p_size.height;
+ feed_format.format = String((char *)p_description.description);
+ feed_format.frame_numerator = p_frame_numerator;
+ feed_format.frame_denominator = p_frame_denominator;
+ feed_format.pixel_format = p_description.pixelformat;
+ print_verbose(vformat("%s %dx%d@%d/%dfps", (char *)p_description.description, p_size.width, p_size.height, p_frame_denominator, p_frame_numerator));
+ formats.push_back(feed_format);
+}
+
+bool CameraFeedLinux::_request_buffers() {
+ struct v4l2_requestbuffers requestbuffers;
+
+ memset(&requestbuffers, 0, sizeof(requestbuffers));
+ requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ requestbuffers.memory = V4L2_MEMORY_MMAP;
+ requestbuffers.count = 4;
+
+ if (ioctl(file_descriptor, VIDIOC_REQBUFS, &requestbuffers) == -1) {
+ ERR_FAIL_V_MSG(false, vformat("ioctl(VIDIOC_REQBUFS) error: %d.", errno));
+ }
+
+ ERR_FAIL_COND_V_MSG(requestbuffers.count < 2, false, "Not enough buffers granted.");
+
+ buffer_count = requestbuffers.count;
+ buffers = new StreamingBuffer[buffer_count];
+
+ for (unsigned int i = 0; i < buffer_count; i++) {
+ struct v4l2_buffer buffer;
+
+ memset(&buffer, 0, sizeof(buffer));
+ buffer.type = requestbuffers.type;
+ buffer.memory = V4L2_MEMORY_MMAP;
+ buffer.index = i;
+
+ if (ioctl(file_descriptor, VIDIOC_QUERYBUF, &buffer) == -1) {
+ delete[] buffers;
+ ERR_FAIL_V_MSG(false, vformat("ioctl(VIDIOC_QUERYBUF) error: %d.", errno));
+ }
+
+ buffers[i].length = buffer.length;
+ buffers[i].start = mmap(NULL, buffer.length, PROT_READ | PROT_WRITE, MAP_SHARED, file_descriptor, buffer.m.offset);
+
+ if (buffers[i].start == MAP_FAILED) {
+ for (unsigned int b = 0; b < i; b++) {
+ _unmap_buffers(i);
+ }
+ delete[] buffers;
+ ERR_FAIL_V_MSG(false, "Mapping buffers failed.");
+ }
+ }
+
+ return true;
+}
+
+bool CameraFeedLinux::_start_capturing() {
+ for (unsigned int i = 0; i < buffer_count; i++) {
+ struct v4l2_buffer buffer;
+
+ memset(&buffer, 0, sizeof(buffer));
+ buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buffer.memory = V4L2_MEMORY_MMAP;
+ buffer.index = i;
+
+ if (ioctl(file_descriptor, VIDIOC_QBUF, &buffer) == -1) {
+ ERR_FAIL_V_MSG(false, vformat("ioctl(VIDIOC_QBUF) error: %d.", errno));
+ }
+ }
+
+ enum v4l2_buf_type type;
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+
+ if (ioctl(file_descriptor, VIDIOC_STREAMON, &type) == -1) {
+ ERR_FAIL_V_MSG(false, vformat("ioctl(VIDIOC_STREAMON) error: %d.", errno));
+ }
+
+ return true;
+}
+
+void CameraFeedLinux::_read_frame() {
+ struct v4l2_buffer buffer;
+ memset(&buffer, 0, sizeof(buffer));
+ buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buffer.memory = V4L2_MEMORY_MMAP;
+
+ if (ioctl(file_descriptor, VIDIOC_DQBUF, &buffer) == -1) {
+ if (errno != EAGAIN) {
+ print_error(vformat("ioctl(VIDIOC_DQBUF) error: %d.", errno));
+ exit_flag.set();
+ }
+ return;
+ }
+
+ buffer_decoder->decode(buffers[buffer.index]);
+
+ if (ioctl(file_descriptor, VIDIOC_QBUF, &buffer) == -1) {
+ print_error(vformat("ioctl(VIDIOC_QBUF) error: %d.", errno));
+ }
+
+ emit_signal(SNAME("frame_changed"));
+}
+
+void CameraFeedLinux::_stop_capturing() {
+ enum v4l2_buf_type type;
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+
+ if (ioctl(file_descriptor, VIDIOC_STREAMOFF, &type) == -1) {
+ print_error(vformat("ioctl(VIDIOC_STREAMOFF) error: %d.", errno));
+ }
+}
+
+void CameraFeedLinux::_unmap_buffers(unsigned int p_count) {
+ for (unsigned int i = 0; i < p_count; i++) {
+ munmap(buffers[i].start, buffers[i].length);
+ }
+}
+
+void CameraFeedLinux::_start_thread() {
+ exit_flag.clear();
+ thread = memnew(Thread);
+ thread->start(CameraFeedLinux::update_buffer_thread_func, this);
+}
+
+String CameraFeedLinux::get_device_name() const {
+ return device_name;
+}
+
+bool CameraFeedLinux::activate_feed() {
+ file_descriptor = open(device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
+ if (_request_buffers() && _start_capturing()) {
+ buffer_decoder = _create_buffer_decoder();
+ _start_thread();
+ return true;
+ }
+ ERR_FAIL_V_MSG(false, "Could not activate feed.");
+}
+
+BufferDecoder *CameraFeedLinux::_create_buffer_decoder() {
+ switch (formats[selected_format].pixel_format) {
+ case V4L2_PIX_FMT_MJPEG:
+ case V4L2_PIX_FMT_JPEG:
+ return memnew(JpegBufferDecoder(this));
+ case V4L2_PIX_FMT_YUYV:
+ case V4L2_PIX_FMT_YYUV:
+ case V4L2_PIX_FMT_YVYU:
+ case V4L2_PIX_FMT_UYVY:
+ case V4L2_PIX_FMT_VYUY: {
+ String output = parameters["output"];
+ if (output == "separate") {
+ return memnew(SeparateYuyvBufferDecoder(this));
+ }
+ if (output == "grayscale") {
+ return memnew(YuyvToGrayscaleBufferDecoder(this));
+ }
+ if (output == "copy") {
+ return memnew(CopyBufferDecoder(this, false));
+ }
+ return memnew(YuyvToRgbBufferDecoder(this));
+ }
+ default:
+ return memnew(CopyBufferDecoder(this, true));
+ }
+}
+
+void CameraFeedLinux::deactivate_feed() {
+ exit_flag.set();
+ thread->wait_to_finish();
+ memdelete(thread);
+ _stop_capturing();
+ _unmap_buffers(buffer_count);
+ delete[] buffers;
+ memdelete(buffer_decoder);
+ for (int i = 0; i < CameraServer::FEED_IMAGES; i++) {
+ RID placeholder = RenderingServer::get_singleton()->texture_2d_placeholder_create();
+ RenderingServer::get_singleton()->texture_replace(texture[i], placeholder);
+ }
+ base_width = 0;
+ base_height = 0;
+ close(file_descriptor);
+
+ emit_signal(SNAME("format_changed"));
+}
+
+Array CameraFeedLinux::get_formats() const {
+ Array result;
+ for (const FeedFormat &format : formats) {
+ Dictionary dictionary;
+ dictionary["width"] = format.width;
+ dictionary["height"] = format.height;
+ dictionary["format"] = format.format;
+ dictionary["frame_numerator"] = format.frame_numerator;
+ dictionary["frame_denominator"] = format.frame_denominator;
+ result.push_back(dictionary);
+ }
+ return result;
+}
+
+CameraFeed::FeedFormat CameraFeedLinux::get_format() const {
+ return formats[selected_format];
+}
+
+bool CameraFeedLinux::set_format(int p_index, const Dictionary &p_parameters) {
+ ERR_FAIL_COND_V_MSG(active, false, "Feed is active.");
+ ERR_FAIL_INDEX_V_MSG(p_index, formats.size(), false, "Invalid format index.");
+
+ parameters = p_parameters.duplicate();
+ selected_format = p_index;
+
+ FeedFormat feed_format = formats[p_index];
+
+ file_descriptor = open(device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
+
+ struct v4l2_format format;
+ memset(&format, 0, sizeof(format));
+ format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ format.fmt.pix.width = feed_format.width;
+ format.fmt.pix.height = feed_format.height;
+ format.fmt.pix.pixelformat = feed_format.pixel_format;
+
+ if (ioctl(file_descriptor, VIDIOC_S_FMT, &format) == -1) {
+ close(file_descriptor);
+ ERR_FAIL_V_MSG(false, vformat("Cannot set format, error: %d.", errno));
+ }
+
+ if (feed_format.frame_numerator > 0) {
+ struct v4l2_streamparm param;
+ memset(&param, 0, sizeof(param));
+
+ param.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ param.parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+ param.parm.capture.timeperframe.numerator = feed_format.frame_numerator;
+ param.parm.capture.timeperframe.denominator = feed_format.frame_denominator;
+
+ if (ioctl(file_descriptor, VIDIOC_S_PARM, &param) == -1) {
+ close(file_descriptor);
+ ERR_FAIL_V_MSG(false, vformat("Cannot set framerate, error: %d.", errno));
+ }
+ }
+ close(file_descriptor);
+
+ emit_signal(SNAME("format_changed"));
+
+ return true;
+}
+
+CameraFeedLinux::CameraFeedLinux(const String &p_device_name) :
+ CameraFeed() {
+ device_name = p_device_name;
+ _query_device(device_name);
+ set_format(0, Dictionary());
+}
+
+CameraFeedLinux::~CameraFeedLinux() {
+ if (is_active()) {
+ deactivate_feed();
+ }
+}
diff --git a/modules/camera/camera_feed_linux.h b/modules/camera/camera_feed_linux.h
new file mode 100644
index 0000000000..bf29201c99
--- /dev/null
+++ b/modules/camera/camera_feed_linux.h
@@ -0,0 +1,78 @@
+/**************************************************************************/
+/* camera_feed_linux.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifndef CAMERA_FEED_LINUX_H
+#define CAMERA_FEED_LINUX_H
+
+#include "buffer_decoder.h"
+
+#include "core/os/thread.h"
+#include "servers/camera/camera_feed.h"
+
+#include <linux/videodev2.h>
+
+struct StreamingBuffer;
+
+class CameraFeedLinux : public CameraFeed {
+private:
+ SafeFlag exit_flag;
+ Thread *thread = nullptr;
+ String device_name;
+ int file_descriptor = -1;
+ StreamingBuffer *buffers = nullptr;
+ unsigned int buffer_count = 0;
+ BufferDecoder *buffer_decoder = nullptr;
+
+ static void update_buffer_thread_func(void *p_func);
+
+ void _update_buffer();
+ void _query_device(const String &p_device_name);
+ void _add_format(v4l2_fmtdesc description, v4l2_frmsize_discrete size, int frame_numerator, int frame_denominator);
+ bool _request_buffers();
+ bool _start_capturing();
+ void _read_frame();
+ void _stop_capturing();
+ void _unmap_buffers(unsigned int p_count);
+ BufferDecoder *_create_buffer_decoder();
+ void _start_thread();
+
+public:
+ String get_device_name() const;
+ bool activate_feed();
+ void deactivate_feed();
+ bool set_format(int p_index, const Dictionary &p_parameters);
+ Array get_formats() const;
+ FeedFormat get_format() const;
+
+ CameraFeedLinux(const String &p_device_name);
+ virtual ~CameraFeedLinux();
+};
+
+#endif // CAMERA_FEED_LINUX_H
diff --git a/modules/camera/camera_linux.cpp b/modules/camera/camera_linux.cpp
new file mode 100644
index 0000000000..0cfb6b7b9e
--- /dev/null
+++ b/modules/camera/camera_linux.cpp
@@ -0,0 +1,169 @@
+/**************************************************************************/
+/* camera_linux.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#include "camera_linux.h"
+
+#include "camera_feed_linux.h"
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+void CameraLinux::camera_thread_func(void *p_camera_linux) {
+ if (p_camera_linux) {
+ CameraLinux *camera_linux = (CameraLinux *)p_camera_linux;
+ camera_linux->_update_devices();
+ }
+}
+
+void CameraLinux::_update_devices() {
+ while (!exit_flag.is_set()) {
+ {
+ MutexLock lock(camera_mutex);
+
+ for (int i = feeds.size() - 1; i >= 0; i--) {
+ Ref<CameraFeedLinux> feed = (Ref<CameraFeedLinux>)feeds[i];
+ String device_name = feed->get_device_name();
+ if (!_is_active(device_name)) {
+ remove_feed(feed);
+ }
+ }
+
+ DIR *devices = opendir("/dev");
+
+ if (devices) {
+ struct dirent *device;
+
+ while ((device = readdir(devices)) != nullptr) {
+ if (strncmp(device->d_name, "video", 5) != 0) {
+ continue;
+ }
+ String device_name = String("/dev/") + String(device->d_name);
+ if (!_has_device(device_name)) {
+ _add_device(device_name);
+ }
+ }
+ }
+
+ closedir(devices);
+ }
+
+ usleep(1000000);
+ }
+}
+
+bool CameraLinux::_has_device(const String &p_device_name) {
+ for (int i = 0; i < feeds.size(); i++) {
+ Ref<CameraFeedLinux> feed = (Ref<CameraFeedLinux>)feeds[i];
+ if (feed->get_device_name() == p_device_name) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void CameraLinux::_add_device(const String &p_device_name) {
+ int file_descriptor = _open_device(p_device_name);
+
+ if (file_descriptor != -1) {
+ if (_is_video_capture_device(file_descriptor)) {
+ Ref<CameraFeedLinux> feed = memnew(CameraFeedLinux(p_device_name));
+ add_feed(feed);
+ }
+ }
+
+ close(file_descriptor);
+}
+
+int CameraLinux::_open_device(const String &p_device_name) {
+ struct stat s;
+
+ if (stat(p_device_name.ascii(), &s) == -1) {
+ return -1;
+ }
+
+ if (!S_ISCHR(s.st_mode)) {
+ return -1;
+ }
+
+ return open(p_device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
+}
+
+// TODO any cheaper/cleaner way to check if file descriptor is invalid?
+bool CameraLinux::_is_active(const String &p_device_name) {
+ struct v4l2_capability capability;
+ bool result = false;
+ int file_descriptor = _open_device(p_device_name);
+ if (file_descriptor != -1 && ioctl(file_descriptor, VIDIOC_QUERYCAP, &capability) != -1) {
+ result = true;
+ }
+ close(file_descriptor);
+ return result;
+}
+
+bool CameraLinux::_is_video_capture_device(int p_file_descriptor) {
+ struct v4l2_capability capability;
+
+ if (ioctl(p_file_descriptor, VIDIOC_QUERYCAP, &capability) == -1) {
+ print_verbose("Cannot query device");
+ return false;
+ }
+
+ if (!(capability.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
+ print_verbose(vformat("%s is no video capture device\n", String((char *)capability.card)));
+ return false;
+ }
+
+ if (!(capability.capabilities & V4L2_CAP_STREAMING)) {
+ print_verbose(vformat("%s does not support streaming", String((char *)capability.card)));
+ return false;
+ }
+
+ return _can_query_format(p_file_descriptor, V4L2_BUF_TYPE_VIDEO_CAPTURE);
+}
+
+bool CameraLinux::_can_query_format(int p_file_descriptor, int p_type) {
+ struct v4l2_format format;
+ memset(&format, 0, sizeof(format));
+ format.type = p_type;
+
+ return ioctl(p_file_descriptor, VIDIOC_G_FMT, &format) != -1;
+}
+
+CameraLinux::CameraLinux() {
+ camera_thread.start(CameraLinux::camera_thread_func, this);
+};
+
+CameraLinux::~CameraLinux() {
+ exit_flag.set();
+ camera_thread.wait_to_finish();
+}
diff --git a/modules/camera/camera_linux.h b/modules/camera/camera_linux.h
new file mode 100644
index 0000000000..66f6aa0ffb
--- /dev/null
+++ b/modules/camera/camera_linux.h
@@ -0,0 +1,60 @@
+/**************************************************************************/
+/* camera_linux.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifndef CAMERA_LINUX_H
+#define CAMERA_LINUX_H
+
+#include "core/os/mutex.h"
+#include "core/os/thread.h"
+#include "servers/camera_server.h"
+
+class CameraLinux : public CameraServer {
+private:
+ SafeFlag exit_flag;
+ Thread camera_thread;
+ Mutex camera_mutex;
+
+ static void camera_thread_func(void *p_camera_linux);
+
+ void _update_devices();
+ bool _has_device(const String &p_device_name);
+ void _add_device(const String &p_device_name);
+ void _remove_device(const String &p_device_name);
+ int _open_device(const String &p_device_name);
+ bool _is_active(const String &p_device_name);
+ bool _is_video_capture_device(int p_file_descriptor);
+ bool _can_query_format(int p_file_descriptor, int p_type);
+
+public:
+ CameraLinux();
+ ~CameraLinux();
+};
+
+#endif // CAMERA_LINUX_H
diff --git a/modules/camera/config.py b/modules/camera/config.py
index d2b2542dd9..7b368d2193 100644
--- a/modules/camera/config.py
+++ b/modules/camera/config.py
@@ -1,5 +1,5 @@
def can_build(env, platform):
- return platform == "macos" or platform == "windows"
+ return platform == "macos" or platform == "windows" or platform == "linuxbsd"
def configure(env):
diff --git a/modules/camera/register_types.cpp b/modules/camera/register_types.cpp
index feee6769f8..666ea8ba65 100644
--- a/modules/camera/register_types.cpp
+++ b/modules/camera/register_types.cpp
@@ -30,6 +30,9 @@
#include "register_types.h"
+#if defined(LINUXBSD_ENABLED)
+#include "camera_linux.h"
+#endif
#if defined(WINDOWS_ENABLED)
#include "camera_win.h"
#endif
@@ -42,6 +45,9 @@ void initialize_camera_module(ModuleInitializationLevel p_level) {
return;
}
+#if defined(LINUXBSD_ENABLED)
+ CameraServer::make_default<CameraLinux>();
+#endif
#if defined(WINDOWS_ENABLED)
CameraServer::make_default<CameraWindows>();
#endif
diff --git a/modules/multiplayer/editor/editor_network_profiler.cpp b/modules/multiplayer/editor/editor_network_profiler.cpp
index 3a51712c70..f5f20d6931 100644
--- a/modules/multiplayer/editor/editor_network_profiler.cpp
+++ b/modules/multiplayer/editor/editor_network_profiler.cpp
@@ -193,6 +193,9 @@ void EditorNetworkProfiler::_update_button_text() {
}
void EditorNetworkProfiler::started() {
+ _clear_pressed();
+ activate->set_disabled(false);
+
if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false)) {
set_profiling(true);
refresh_timer->start();
@@ -200,6 +203,7 @@ void EditorNetworkProfiler::started() {
}
void EditorNetworkProfiler::stopped() {
+ activate->set_disabled(true);
set_profiling(false);
refresh_timer->stop();
}
@@ -218,6 +222,7 @@ void EditorNetworkProfiler::_clear_pressed() {
set_bandwidth(0, 0);
refresh_rpc_data();
refresh_replication_data();
+ clear_button->set_disabled(true);
}
void EditorNetworkProfiler::_autostart_toggled(bool p_toggled_on) {
@@ -235,6 +240,9 @@ void EditorNetworkProfiler::_replication_button_clicked(TreeItem *p_item, int p_
}
void EditorNetworkProfiler::add_rpc_frame_data(const RPCNodeInfo &p_frame) {
+ if (clear_button->is_disabled()) {
+ clear_button->set_disabled(false);
+ }
dirty = true;
if (!rpc_data.has(p_frame.node)) {
rpc_data.insert(p_frame.node, p_frame);
@@ -251,6 +259,9 @@ void EditorNetworkProfiler::add_rpc_frame_data(const RPCNodeInfo &p_frame) {
}
void EditorNetworkProfiler::add_sync_frame_data(const SyncInfo &p_frame) {
+ if (clear_button->is_disabled()) {
+ clear_button->set_disabled(false);
+ }
dirty = true;
if (!sync_data.has(p_frame.synchronizer)) {
sync_data[p_frame.synchronizer] = p_frame;
@@ -292,11 +303,13 @@ EditorNetworkProfiler::EditorNetworkProfiler() {
activate = memnew(Button);
activate->set_toggle_mode(true);
activate->set_text(TTR("Start"));
+ activate->set_disabled(true);
activate->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_activate_pressed));
hb->add_child(activate);
clear_button = memnew(Button);
clear_button->set_text(TTR("Clear"));
+ clear_button->set_disabled(true);
clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_clear_pressed));
hb->add_child(clear_button);
diff --git a/modules/openxr/editor/openxr_select_action_dialog.cpp b/modules/openxr/editor/openxr_select_action_dialog.cpp
index a4ccc98408..89dea09be4 100644
--- a/modules/openxr/editor/openxr_select_action_dialog.cpp
+++ b/modules/openxr/editor/openxr_select_action_dialog.cpp
@@ -66,7 +66,7 @@ void OpenXRSelectActionDialog::_on_select_action(const String p_action) {
void OpenXRSelectActionDialog::open() {
ERR_FAIL_COND(action_map.is_null());
- // out with the old...
+ // Out with the old.
while (main_vb->get_child_count() > 0) {
memdelete(main_vb->get_child(0));
}
@@ -74,6 +74,7 @@ void OpenXRSelectActionDialog::open() {
selected_action = "";
action_buttons.clear();
+ // In with the new.
Array action_sets = action_map->get_action_sets();
for (int i = 0; i < action_sets.size(); i++) {
Ref<OpenXRActionSet> action_set = action_sets[i];
diff --git a/modules/openxr/editor/openxr_select_interaction_profile_dialog.cpp b/modules/openxr/editor/openxr_select_interaction_profile_dialog.cpp
index 53b8cbd401..ee8940f30b 100644
--- a/modules/openxr/editor/openxr_select_interaction_profile_dialog.cpp
+++ b/modules/openxr/editor/openxr_select_interaction_profile_dialog.cpp
@@ -66,15 +66,15 @@ void OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile(const
void OpenXRSelectInteractionProfileDialog::open(PackedStringArray p_do_not_include) {
int available_count = 0;
- // out with the old...
- while (main_vb->get_child_count() > 0) {
- memdelete(main_vb->get_child(0));
+ // Out with the old.
+ while (main_vb->get_child_count() > 1) {
+ memdelete(main_vb->get_child(1));
}
selected_interaction_profile = "";
ip_buttons.clear();
- // in with the new
+ // In with the new.
PackedStringArray interaction_profiles = OpenXRInteractionProfileMetadata::get_singleton()->get_interaction_profile_paths();
for (int i = 0; i < interaction_profiles.size(); i++) {
const String &path = interaction_profiles[i];
@@ -82,6 +82,7 @@ void OpenXRSelectInteractionProfileDialog::open(PackedStringArray p_do_not_inclu
Button *ip_button = memnew(Button);
ip_button->set_flat(true);
ip_button->set_text(OpenXRInteractionProfileMetadata::get_singleton()->get_profile(path)->display_name);
+ ip_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
ip_button->connect(SceneStringName(pressed), callable_mp(this, &OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile).bind(path));
main_vb->add_child(ip_button);
@@ -90,23 +91,16 @@ void OpenXRSelectInteractionProfileDialog::open(PackedStringArray p_do_not_inclu
}
}
- if (available_count == 0) {
- // give warning that we have all profiles selected
-
- } else {
- // TODO maybe if we only have one, auto select it?
-
- popup_centered();
- }
+ all_selected->set_visible(available_count == 0);
+ get_cancel_button()->set_visible(available_count > 0);
+ popup_centered();
}
void OpenXRSelectInteractionProfileDialog::ok_pressed() {
- if (selected_interaction_profile == "") {
- return;
+ if (selected_interaction_profile != "") {
+ emit_signal("interaction_profile_selected", selected_interaction_profile);
}
- emit_signal("interaction_profile_selected", selected_interaction_profile);
-
hide();
}
@@ -118,6 +112,10 @@ OpenXRSelectInteractionProfileDialog::OpenXRSelectInteractionProfileDialog() {
add_child(scroll);
main_vb = memnew(VBoxContainer);
- // main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
+ main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
scroll->add_child(main_vb);
+
+ all_selected = memnew(Label);
+ all_selected->set_text(TTR("All interaction profiles have been added to the action map."));
+ main_vb->add_child(all_selected);
}
diff --git a/modules/openxr/editor/openxr_select_interaction_profile_dialog.h b/modules/openxr/editor/openxr_select_interaction_profile_dialog.h
index 1d1615321c..d85e4cd4d6 100644
--- a/modules/openxr/editor/openxr_select_interaction_profile_dialog.h
+++ b/modules/openxr/editor/openxr_select_interaction_profile_dialog.h
@@ -51,6 +51,7 @@ private:
VBoxContainer *main_vb = nullptr;
ScrollContainer *scroll = nullptr;
+ Label *all_selected = nullptr;
protected:
static void _bind_methods();
diff --git a/pyproject.toml b/pyproject.toml
index 1cf789b460..78c8d8c20e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -70,6 +70,7 @@ ignore-words-list = """\
numer,
ot,
outin,
+ parm,
requestor,
te,
textin,
diff --git a/scene/2d/parallax_2d.cpp b/scene/2d/parallax_2d.cpp
index fdb2d2cdd0..c6176390dc 100644
--- a/scene/2d/parallax_2d.cpp
+++ b/scene/2d/parallax_2d.cpp
@@ -83,7 +83,11 @@ void Parallax2D::_validate_property(PropertyInfo &p_property) const {
void Parallax2D::_camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset, const Point2 &p_adj_screen_pos) {
if (!ignore_camera_scroll) {
if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
- set_screen_offset((p_adj_screen_pos + Vector2(0.5, 0.5)).floor());
+ Size2 vps = get_viewport_rect().size;
+ Vector2 offset;
+ offset.x = ((int)vps.width % 2) ? 0.0 : 0.5;
+ offset.y = ((int)vps.height % 2) ? 0.0 : 0.5;
+ set_screen_offset((p_adj_screen_pos + offset).floor());
} else {
set_screen_offset(p_adj_screen_pos);
}
diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp
index c92dcbc153..002a738b83 100644
--- a/scene/gui/color_picker.cpp
+++ b/scene/gui/color_picker.cpp
@@ -33,8 +33,6 @@
#include "core/input/input.h"
#include "core/io/image.h"
#include "core/math/color.h"
-#include "core/os/keyboard.h"
-#include "core/os/os.h"
#include "scene/gui/color_mode.h"
#include "scene/gui/margin_container.h"
#include "scene/resources/image_texture.h"
@@ -42,7 +40,6 @@
#include "scene/resources/style_box_texture.h"
#include "scene/theme/theme_db.h"
#include "servers/display_server.h"
-#include "thirdparty/misc/ok_color.h"
#include "thirdparty/misc/ok_color_shader.h"
List<Color> ColorPicker::preset_cache;
@@ -1567,7 +1564,7 @@ void ColorPicker::_pick_button_pressed_legacy() {
picker_preview_label = memnew(Label);
picker_preview->set_anchors_preset(Control::PRESET_CENTER_TOP);
- picker_preview_label->set_text("Color Picking active");
+ picker_preview_label->set_text(ETR("Color Picking active"));
picker_preview->add_child(picker_preview_label);
picker_preview_style_box = (Ref<StyleBoxFlat>)memnew(StyleBoxFlat);
@@ -1905,7 +1902,7 @@ ColorPicker::ColorPicker() {
mode_popup->add_radio_check_item(modes[i]->get_name(), i);
}
mode_popup->add_separator();
- mode_popup->add_check_item("Colorized Sliders", MODE_MAX);
+ mode_popup->add_check_item(ETR("Colorized Sliders"), MODE_MAX);
mode_popup->set_item_checked(current_mode, true);
mode_popup->set_item_checked(MODE_MAX + 1, true);
mode_popup->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::_set_mode_popup_value));
@@ -1933,7 +1930,7 @@ ColorPicker::ColorPicker() {
hex_hbc->set_alignment(ALIGNMENT_BEGIN);
vbr->add_child(hex_hbc);
- hex_hbc->add_child(memnew(Label("Hex")));
+ hex_hbc->add_child(memnew(Label(ETR("Hex"))));
text_type = memnew(Button);
hex_hbc->add_child(text_type);
@@ -1997,8 +1994,7 @@ ColorPicker::ColorPicker() {
preset_group.instantiate();
- btn_preset = memnew(Button);
- btn_preset->set_text("Swatches");
+ btn_preset = memnew(Button(ETR("Swatches")));
btn_preset->set_flat(true);
btn_preset->set_toggle_mode(true);
btn_preset->set_focus_mode(FOCUS_NONE);
@@ -2014,8 +2010,7 @@ ColorPicker::ColorPicker() {
recent_preset_group.instantiate();
- btn_recent_preset = memnew(Button);
- btn_recent_preset->set_text("Recent Colors");
+ btn_recent_preset = memnew(Button(ETR("Recent Colors")));
btn_recent_preset->set_flat(true);
btn_recent_preset->set_toggle_mode(true);
btn_recent_preset->set_focus_mode(FOCUS_NONE);
diff --git a/scene/resources/animation.cpp b/scene/resources/animation.cpp
index a2ed6af23c..9abc6a02d2 100644
--- a/scene/resources/animation.cpp
+++ b/scene/resources/animation.cpp
@@ -321,8 +321,12 @@ bool Animation::_set(const StringName &p_name, const Variant &p_value) {
Vector<real_t> times = d["times"];
Vector<real_t> values = d["points"];
#ifdef TOOLS_ENABLED
- ERR_FAIL_COND_V(!d.has("handle_modes"), false);
- Vector<int> handle_modes = d["handle_modes"];
+ Vector<int> handle_modes;
+ if (d.has("handle_modes")) {
+ handle_modes = d["handle_modes"];
+ } else {
+ handle_modes.resize_zeroed(times.size());
+ }
#endif // TOOLS_ENABLED
ERR_FAIL_COND_V(times.size() * 5 != values.size(), false);
@@ -4804,9 +4808,9 @@ void Animation::compress(uint32_t p_page_size, uint32_t p_fps, float p_split_tol
continue; // This track is exhausted (all keys were added already), don't consider.
}
}
-
- uint32_t key_frame = double(track_get_key_time(uncomp_track, time_tracks[i].key_index)) / frame_len;
-
+ double key_time = track_get_key_time(uncomp_track, time_tracks[i].key_index);
+ double result = key_time / frame_len;
+ uint32_t key_frame = Math::fast_ftoi(result);
if (time_tracks[i].needs_start_frame && key_frame > base_page_frame) {
start_frame = true;
best_frame = base_page_frame;
diff --git a/scene/resources/camera_texture.cpp b/scene/resources/camera_texture.cpp
index b575a099ed..b219f89e59 100644
--- a/scene/resources/camera_texture.cpp
+++ b/scene/resources/camera_texture.cpp
@@ -47,6 +47,11 @@ void CameraTexture::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "camera_is_active"), "set_camera_active", "get_camera_active");
}
+void CameraTexture::_on_format_changed() {
+ // FIXME: `emit_changed` is more appropriate, but causes errors for some reason.
+ callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();
+}
+
int CameraTexture::get_width() const {
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
if (feed.is_valid()) {
@@ -82,13 +87,26 @@ RID CameraTexture::get_rid() const {
}
Ref<Image> CameraTexture::get_image() const {
- // not (yet) supported
- return Ref<Image>();
+ return RenderingServer::get_singleton()->texture_2d_get(get_rid());
}
void CameraTexture::set_camera_feed_id(int p_new_id) {
+ Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
+ if (feed.is_valid()) {
+ if (feed->is_connected("format_changed", callable_mp(this, &CameraTexture::_on_format_changed))) {
+ feed->disconnect("format_changed", callable_mp(this, &CameraTexture::_on_format_changed));
+ }
+ }
+
camera_feed_id = p_new_id;
+
+ feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
+ if (feed.is_valid()) {
+ feed->connect("format_changed", callable_mp(this, &CameraTexture::_on_format_changed));
+ }
+
notify_property_list_changed();
+ callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();
}
int CameraTexture::get_camera_feed_id() const {
@@ -98,6 +116,7 @@ int CameraTexture::get_camera_feed_id() const {
void CameraTexture::set_which_feed(CameraServer::FeedImage p_which) {
which_feed = p_which;
notify_property_list_changed();
+ callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();
}
CameraServer::FeedImage CameraTexture::get_which_feed() const {
@@ -109,6 +128,7 @@ void CameraTexture::set_camera_active(bool p_active) {
if (feed.is_valid()) {
feed->set_active(p_active);
notify_property_list_changed();
+ callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();
}
}
diff --git a/scene/resources/camera_texture.h b/scene/resources/camera_texture.h
index 521121f9ea..dd216a72d6 100644
--- a/scene/resources/camera_texture.h
+++ b/scene/resources/camera_texture.h
@@ -43,6 +43,7 @@ private:
protected:
static void _bind_methods();
+ void _on_format_changed();
public:
virtual int get_width() const override;
diff --git a/servers/camera/camera_feed.cpp b/servers/camera/camera_feed.cpp
index 0661ffd576..8f6a40481d 100644
--- a/servers/camera/camera_feed.cpp
+++ b/servers/camera/camera_feed.cpp
@@ -56,9 +56,16 @@ void CameraFeed::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_datatype"), &CameraFeed::get_datatype);
+ ClassDB::bind_method(D_METHOD("get_formats"), &CameraFeed::get_formats);
+ ClassDB::bind_method(D_METHOD("set_format", "index", "parameters"), &CameraFeed::set_format);
+
+ ADD_SIGNAL(MethodInfo("frame_changed"));
+ ADD_SIGNAL(MethodInfo("format_changed"));
+
ADD_GROUP("Feed", "feed_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active");
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform");
+ ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "formats"), "", "get_formats");
BIND_ENUM_CONSTANT(FEED_NOIMAGE);
BIND_ENUM_CONSTANT(FEED_RGB);
@@ -84,13 +91,11 @@ void CameraFeed::set_active(bool p_is_active) {
} else if (p_is_active) {
// attempt to activate this feed
if (activate_feed()) {
- print_line("Activate " + name);
active = true;
}
} else {
// just deactivate it
deactivate_feed();
- print_line("Deactivate " + name);
active = false;
}
}
@@ -183,6 +188,8 @@ void CameraFeed::set_RGB_img(const Ref<Image> &p_rgb_img) {
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_rgb_img);
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
+
+ emit_signal(SNAME("format_changed"));
} else {
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img);
}
@@ -204,6 +211,8 @@ void CameraFeed::set_YCbCr_img(const Ref<Image> &p_ycbcr_img) {
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_ycbcr_img);
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
+
+ emit_signal(SNAME("format_changed"));
} else {
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_ycbcr_img);
}
@@ -235,6 +244,8 @@ void CameraFeed::set_YCbCr_imgs(const Ref<Image> &p_y_img, const Ref<Image> &p_c
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_cbcr_img);
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_CBCR_IMAGE], new_texture);
}
+
+ emit_signal(SNAME("format_changed"));
} 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);
@@ -252,3 +263,16 @@ bool CameraFeed::activate_feed() {
void CameraFeed::deactivate_feed() {
// nothing to do here
}
+
+bool CameraFeed::set_format(int p_index, const Dictionary &p_parameters) {
+ return false;
+}
+
+Array CameraFeed::get_formats() const {
+ return Array();
+}
+
+CameraFeed::FeedFormat CameraFeed::get_format() const {
+ FeedFormat feed_format = {};
+ return feed_format;
+}
diff --git a/servers/camera/camera_feed.h b/servers/camera/camera_feed.h
index b85a44cfae..5d1f54be07 100644
--- a/servers/camera/camera_feed.h
+++ b/servers/camera/camera_feed.h
@@ -60,14 +60,26 @@ public:
private:
int id; // unique id for this, for internal use in case feeds are removed
- int base_width;
- int base_height;
protected:
+ struct FeedFormat {
+ int width = 0;
+ int height = 0;
+ String format;
+ int frame_numerator = 0;
+ int frame_denominator = 0;
+ uint32_t pixel_format = 0;
+ };
+
String name; // name of our camera feed
FeedDataType datatype; // type of texture data stored
FeedPosition position; // position of camera on the device
Transform2D transform; // display transform
+ int base_width = 0;
+ int base_height = 0;
+ Vector<FeedFormat> formats;
+ Dictionary parameters;
+ int selected_format = -1;
bool active; // only when active do we actually update the camera texture each frame
RID texture[CameraServer::FEED_IMAGES]; // texture images needed for this
@@ -102,6 +114,10 @@ public:
void set_YCbCr_img(const Ref<Image> &p_ycbcr_img);
void set_YCbCr_imgs(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img);
+ virtual bool set_format(int p_index, const Dictionary &p_parameters);
+ virtual Array get_formats() const;
+ virtual FeedFormat get_format() const;
+
virtual bool activate_feed();
virtual void deactivate_feed();
};
diff --git a/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl b/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl
index 841f02f673..fa3b45a962 100644
--- a/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl
+++ b/servers/rendering/renderer_rd/shaders/effects/tonemap.glsl
@@ -256,8 +256,12 @@ vec3 tonemap_aces(vec3 color, float white) {
return color_tonemapped / white_tonemapped;
}
+// Based on Reinhard's extended formula, see equation 4 in https://doi.org/cjbgrt
vec3 tonemap_reinhard(vec3 color, float white) {
- return (white * color + color) / (color * white + white);
+ float white_squared = white * white;
+ vec3 white_squared_color = white_squared * color;
+ // Equivalent to color * (1 + color / white_squared) / (1 + color)
+ return (white_squared_color + color * color) / (white_squared_color + white_squared);
}
vec3 linear_to_srgb(vec3 color) {
@@ -272,7 +276,7 @@ vec3 linear_to_srgb(vec3 color) {
#define TONEMAPPER_FILMIC 2
#define TONEMAPPER_ACES 3
-vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always outputs clamped [0;1] color
+vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR
// Ensure color values passed to tonemappers are positive.
// They can be negative in the case of negative lights, which leads to undesired behavior.
if (params.tonemapper == TONEMAPPER_LINEAR) {
diff --git a/servers/rendering/renderer_viewport.cpp b/servers/rendering/renderer_viewport.cpp
index 781d29ffaa..4d6435f48a 100644
--- a/servers/rendering/renderer_viewport.cpp
+++ b/servers/rendering/renderer_viewport.cpp
@@ -41,14 +41,31 @@
static Transform2D _canvas_get_transform(RendererViewport::Viewport *p_viewport, RendererCanvasCull::Canvas *p_canvas, RendererViewport::Viewport::CanvasData *p_canvas_data, const Vector2 &p_vp_size) {
Transform2D xf = p_viewport->global_transform;
+ Vector2 pixel_snap_offset;
+ if (p_viewport->snap_2d_transforms_to_pixel) {
+ // We use `floor(p + 0.5)` to snap canvas items, but `ceil(p - 0.5)`
+ // to snap viewport transform because the viewport transform is inverse
+ // to the camera transform. Also, if the viewport size is not divisible
+ // by 2, the center point is offset by 0.5 px and we need to add 0.5
+ // before rounding to cancel it out.
+ pixel_snap_offset.x = (p_viewport->size.width % 2) ? 0.0 : -0.5;
+ pixel_snap_offset.y = (p_viewport->size.height % 2) ? 0.0 : -0.5;
+ }
+
float scale = 1.0;
if (p_viewport->canvas_map.has(p_canvas->parent)) {
Transform2D c_xform = p_viewport->canvas_map[p_canvas->parent].transform;
+ if (p_viewport->snap_2d_transforms_to_pixel) {
+ c_xform.columns[2] = (c_xform.columns[2] * p_canvas->parent_scale + pixel_snap_offset).ceil() / p_canvas->parent_scale;
+ }
xf = xf * c_xform;
scale = p_canvas->parent_scale;
}
Transform2D c_xform = p_canvas_data->transform;
+ if (p_viewport->snap_2d_transforms_to_pixel) {
+ c_xform.columns[2] = (c_xform.columns[2] + pixel_snap_offset).ceil();
+ }
xf = xf * c_xform;
if (scale != 1.0 && !RSG::canvas->disable_scale) {