camera_feed.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. #include "camera_feed.h"
  31. #include "servers/rendering/rendering_server.h"
  32. void CameraFeed::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("get_id"), &CameraFeed::get_id);
  34. ClassDB::bind_method(D_METHOD("is_active"), &CameraFeed::is_active);
  35. ClassDB::bind_method(D_METHOD("set_active", "active"), &CameraFeed::set_active);
  36. ClassDB::bind_method(D_METHOD("get_name"), &CameraFeed::get_name);
  37. ClassDB::bind_method(D_METHOD("set_name", "name"), &CameraFeed::set_name);
  38. ClassDB::bind_method(D_METHOD("get_position"), &CameraFeed::get_position);
  39. ClassDB::bind_method(D_METHOD("set_position", "position"), &CameraFeed::set_position);
  40. // Note, for transform some feeds may override what the user sets (such as ARKit)
  41. ClassDB::bind_method(D_METHOD("get_transform"), &CameraFeed::get_transform);
  42. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CameraFeed::set_transform);
  43. ClassDB::bind_method(D_METHOD("set_rgb_image", "rgb_image"), &CameraFeed::set_rgb_image);
  44. ClassDB::bind_method(D_METHOD("set_ycbcr_image", "ycbcr_image"), &CameraFeed::set_ycbcr_image);
  45. ClassDB::bind_method(D_METHOD("set_ycbcr_images", "y_image", "cbcr_image"), &CameraFeed::set_ycbcr_images);
  46. ClassDB::bind_method(D_METHOD("set_external", "width", "height"), &CameraFeed::set_external);
  47. ClassDB::bind_method(D_METHOD("get_texture_tex_id", "feed_image_type"), &CameraFeed::get_texture_tex_id);
  48. ClassDB::bind_method(D_METHOD("get_datatype"), &CameraFeed::get_datatype);
  49. ClassDB::bind_method(D_METHOD("get_formats"), &CameraFeed::get_formats);
  50. ClassDB::bind_method(D_METHOD("set_format", "index", "parameters"), &CameraFeed::set_format);
  51. GDVIRTUAL_BIND(_activate_feed);
  52. GDVIRTUAL_BIND(_deactivate_feed);
  53. ADD_SIGNAL(MethodInfo("frame_changed"));
  54. ADD_SIGNAL(MethodInfo("format_changed"));
  55. ADD_GROUP("Feed", "feed_");
  56. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active");
  57. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform");
  58. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "formats"), "", "get_formats");
  59. BIND_ENUM_CONSTANT(FEED_NOIMAGE);
  60. BIND_ENUM_CONSTANT(FEED_RGB);
  61. BIND_ENUM_CONSTANT(FEED_YCBCR);
  62. BIND_ENUM_CONSTANT(FEED_YCBCR_SEP);
  63. BIND_ENUM_CONSTANT(FEED_EXTERNAL);
  64. BIND_ENUM_CONSTANT(FEED_UNSPECIFIED);
  65. BIND_ENUM_CONSTANT(FEED_FRONT);
  66. BIND_ENUM_CONSTANT(FEED_BACK);
  67. }
  68. int CameraFeed::get_id() const {
  69. return id;
  70. }
  71. bool CameraFeed::is_active() const {
  72. return active;
  73. }
  74. void CameraFeed::set_active(bool p_is_active) {
  75. if (p_is_active == active) {
  76. // all good
  77. } else if (p_is_active) {
  78. // attempt to activate this feed
  79. if (activate_feed()) {
  80. active = true;
  81. }
  82. } else {
  83. // just deactivate it
  84. deactivate_feed();
  85. active = false;
  86. }
  87. }
  88. String CameraFeed::get_name() const {
  89. return name;
  90. }
  91. void CameraFeed::set_name(String p_name) {
  92. name = p_name;
  93. }
  94. int CameraFeed::get_base_width() const {
  95. return base_width;
  96. }
  97. int CameraFeed::get_base_height() const {
  98. return base_height;
  99. }
  100. CameraFeed::FeedDataType CameraFeed::get_datatype() const {
  101. return datatype;
  102. }
  103. CameraFeed::FeedPosition CameraFeed::get_position() const {
  104. return position;
  105. }
  106. void CameraFeed::set_position(CameraFeed::FeedPosition p_position) {
  107. position = p_position;
  108. }
  109. Transform2D CameraFeed::get_transform() const {
  110. return transform;
  111. }
  112. void CameraFeed::set_transform(const Transform2D &p_transform) {
  113. transform = p_transform;
  114. }
  115. RID CameraFeed::get_texture(CameraServer::FeedImage p_which) {
  116. return texture[p_which];
  117. }
  118. uint64_t CameraFeed::get_texture_tex_id(CameraServer::FeedImage p_which) {
  119. return RenderingServer::get_singleton()->texture_get_native_handle(texture[p_which]);
  120. }
  121. CameraFeed::CameraFeed() {
  122. // initialize our feed
  123. id = CameraServer::get_singleton()->get_free_id();
  124. base_width = 0;
  125. base_height = 0;
  126. name = "???";
  127. active = false;
  128. datatype = CameraFeed::FEED_RGB;
  129. position = CameraFeed::FEED_UNSPECIFIED;
  130. transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
  131. texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  132. texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  133. }
  134. CameraFeed::CameraFeed(String p_name, FeedPosition p_position) {
  135. // initialize our feed
  136. id = CameraServer::get_singleton()->get_free_id();
  137. base_width = 0;
  138. base_height = 0;
  139. name = p_name;
  140. active = false;
  141. datatype = CameraFeed::FEED_NOIMAGE;
  142. position = p_position;
  143. transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
  144. texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  145. texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  146. }
  147. CameraFeed::~CameraFeed() {
  148. // Free our textures
  149. ERR_FAIL_NULL(RenderingServer::get_singleton());
  150. RenderingServer::get_singleton()->free_rid(texture[CameraServer::FEED_Y_IMAGE]);
  151. RenderingServer::get_singleton()->free_rid(texture[CameraServer::FEED_CBCR_IMAGE]);
  152. }
  153. void CameraFeed::set_rgb_image(const Ref<Image> &p_rgb_img) {
  154. ERR_FAIL_COND(p_rgb_img.is_null());
  155. if (active) {
  156. int new_width = p_rgb_img->get_width();
  157. int new_height = p_rgb_img->get_height();
  158. // Emit `format_changed` signal if feed datatype or frame size is changed.
  159. // The signal is deferred to ensure:
  160. // - They are emitted on Godot's main thread.
  161. // - Both datatype and frame size are updated before the emission.
  162. if (datatype != CameraFeed::FEED_RGB || (base_width != new_width) || (base_height != new_height)) {
  163. call_deferred("emit_signal", format_changed_signal_name);
  164. }
  165. if ((base_width != new_width) || (base_height != new_height)) {
  166. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  167. base_width = new_width;
  168. base_height = new_height;
  169. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_rgb_img);
  170. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
  171. } else {
  172. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img);
  173. }
  174. datatype = CameraFeed::FEED_RGB;
  175. // Most of the time the pixel data of camera devices comes from threads outside Godot.
  176. // Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
  177. call_deferred("emit_signal", frame_changed_signal_name);
  178. }
  179. }
  180. void CameraFeed::set_ycbcr_image(const Ref<Image> &p_ycbcr_img) {
  181. ERR_FAIL_COND(p_ycbcr_img.is_null());
  182. if (active) {
  183. int new_width = p_ycbcr_img->get_width();
  184. int new_height = p_ycbcr_img->get_height();
  185. // Emit `format_changed` signal if feed datatype or frame size is changed.
  186. // The signal is deferred to ensure:
  187. // - They are emitted on Godot's main thread.
  188. // - Both datatype and frame size are updated before the emission.
  189. if (datatype != CameraFeed::FEED_YCBCR || (base_width != new_width) || (base_height != new_height)) {
  190. call_deferred("emit_signal", format_changed_signal_name);
  191. }
  192. if ((base_width != new_width) || (base_height != new_height)) {
  193. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  194. base_width = new_width;
  195. base_height = new_height;
  196. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_ycbcr_img);
  197. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
  198. } else {
  199. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_ycbcr_img);
  200. }
  201. datatype = CameraFeed::FEED_YCBCR;
  202. // Most of the time the pixel data of camera devices comes from threads outside Godot.
  203. // Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
  204. call_deferred("emit_signal", frame_changed_signal_name);
  205. }
  206. }
  207. void CameraFeed::set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img) {
  208. ERR_FAIL_COND(p_y_img.is_null());
  209. ERR_FAIL_COND(p_cbcr_img.is_null());
  210. if (active) {
  211. ///@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..
  212. // Wondering about including that into multiple projects, may cause issues.
  213. // That said, if we convert to RGB, we could enable using texture resources again...
  214. int new_y_width = p_y_img->get_width();
  215. int new_y_height = p_y_img->get_height();
  216. // Emit `format_changed` signal if feed datatype or frame size is changed.
  217. // The signal is deferred to ensure:
  218. // - They are emitted on Godot's main thread.
  219. // - Both datatype and frame size are updated before the emission.
  220. if (datatype != CameraFeed::FEED_YCBCR_SEP || (base_width != new_y_width) || (base_height != new_y_height)) {
  221. call_deferred("emit_signal", format_changed_signal_name);
  222. }
  223. if ((base_width != new_y_width) || (base_height != new_y_height)) {
  224. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  225. base_width = new_y_width;
  226. base_height = new_y_height;
  227. {
  228. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_y_img);
  229. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_Y_IMAGE], new_texture);
  230. }
  231. {
  232. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_cbcr_img);
  233. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_CBCR_IMAGE], new_texture);
  234. }
  235. } else {
  236. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_Y_IMAGE], p_y_img);
  237. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_CBCR_IMAGE], p_cbcr_img);
  238. }
  239. datatype = CameraFeed::FEED_YCBCR_SEP;
  240. // Most of the time the pixel data of camera devices comes from threads outside Godot.
  241. // Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
  242. call_deferred("emit_signal", frame_changed_signal_name);
  243. }
  244. }
  245. void CameraFeed::set_external(int p_width, int p_height) {
  246. // Emit `format_changed` signal if feed datatype or frame size is changed.
  247. // The signal is deferred to ensure:
  248. // - They are emitted on Godot's main thread.
  249. // - Both datatype and frame size are updated before the emission.
  250. if (datatype != CameraFeed::FEED_EXTERNAL || (base_width != p_width) || (base_height != p_height)) {
  251. call_deferred("emit_signal", format_changed_signal_name);
  252. }
  253. if ((base_width != p_width) || (base_height != p_height)) {
  254. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  255. base_width = p_width;
  256. base_height = p_height;
  257. RID new_texture = RenderingServer::get_singleton()->texture_external_create(p_width, p_height, 0);
  258. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_YCBCR_IMAGE], new_texture);
  259. }
  260. datatype = CameraFeed::FEED_EXTERNAL;
  261. // Most of the time the pixel data of camera devices comes from threads outside Godot.
  262. // Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
  263. call_deferred("emit_signal", frame_changed_signal_name);
  264. }
  265. bool CameraFeed::activate_feed() {
  266. bool ret = true;
  267. GDVIRTUAL_CALL(_activate_feed, ret);
  268. return ret;
  269. }
  270. void CameraFeed::deactivate_feed() {
  271. GDVIRTUAL_CALL(_deactivate_feed);
  272. }
  273. bool CameraFeed::set_format(int p_index, const Dictionary &p_parameters) {
  274. return false;
  275. }
  276. Array CameraFeed::get_formats() const {
  277. return Array();
  278. }
  279. CameraFeed::FeedFormat CameraFeed::get_format() const {
  280. FeedFormat feed_format = {};
  281. return feed_format;
  282. }