camera_feed.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_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_external", "width", "height"), &CameraFeed::set_external);
  46. ClassDB::bind_method(D_METHOD("get_texture_tex_id", "feed_image_type"), &CameraFeed::get_texture_tex_id);
  47. ClassDB::bind_method(D_METHOD("get_datatype"), &CameraFeed::get_datatype);
  48. ClassDB::bind_method(D_METHOD("get_formats"), &CameraFeed::get_formats);
  49. ClassDB::bind_method(D_METHOD("set_format", "index", "parameters"), &CameraFeed::set_format);
  50. ADD_SIGNAL(MethodInfo("frame_changed"));
  51. ADD_SIGNAL(MethodInfo("format_changed"));
  52. ADD_GROUP("Feed", "feed_");
  53. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active");
  54. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform");
  55. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "formats"), "", "get_formats");
  56. BIND_ENUM_CONSTANT(FEED_NOIMAGE);
  57. BIND_ENUM_CONSTANT(FEED_RGB);
  58. BIND_ENUM_CONSTANT(FEED_YCBCR);
  59. BIND_ENUM_CONSTANT(FEED_YCBCR_SEP);
  60. BIND_ENUM_CONSTANT(FEED_EXTERNAL);
  61. BIND_ENUM_CONSTANT(FEED_UNSPECIFIED);
  62. BIND_ENUM_CONSTANT(FEED_FRONT);
  63. BIND_ENUM_CONSTANT(FEED_BACK);
  64. }
  65. int CameraFeed::get_id() const {
  66. return id;
  67. }
  68. bool CameraFeed::is_active() const {
  69. return active;
  70. }
  71. void CameraFeed::set_active(bool p_is_active) {
  72. if (p_is_active == active) {
  73. // all good
  74. } else if (p_is_active) {
  75. // attempt to activate this feed
  76. if (activate_feed()) {
  77. active = true;
  78. }
  79. } else {
  80. // just deactivate it
  81. deactivate_feed();
  82. active = false;
  83. }
  84. }
  85. String CameraFeed::get_name() const {
  86. return name;
  87. }
  88. void CameraFeed::set_name(String p_name) {
  89. name = p_name;
  90. }
  91. int CameraFeed::get_base_width() const {
  92. return base_width;
  93. }
  94. int CameraFeed::get_base_height() const {
  95. return base_height;
  96. }
  97. CameraFeed::FeedDataType CameraFeed::get_datatype() const {
  98. return datatype;
  99. }
  100. CameraFeed::FeedPosition CameraFeed::get_position() const {
  101. return position;
  102. }
  103. void CameraFeed::set_position(CameraFeed::FeedPosition p_position) {
  104. position = p_position;
  105. }
  106. Transform2D CameraFeed::get_transform() const {
  107. return transform;
  108. }
  109. void CameraFeed::set_transform(const Transform2D &p_transform) {
  110. transform = p_transform;
  111. }
  112. RID CameraFeed::get_texture(CameraServer::FeedImage p_which) {
  113. return texture[p_which];
  114. }
  115. uint64_t CameraFeed::get_texture_tex_id(CameraServer::FeedImage p_which) {
  116. return RenderingServer::get_singleton()->texture_get_native_handle(texture[p_which]);
  117. }
  118. CameraFeed::CameraFeed() {
  119. // initialize our feed
  120. id = CameraServer::get_singleton()->get_free_id();
  121. base_width = 0;
  122. base_height = 0;
  123. name = "???";
  124. active = false;
  125. datatype = CameraFeed::FEED_RGB;
  126. position = CameraFeed::FEED_UNSPECIFIED;
  127. transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
  128. texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  129. texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  130. }
  131. CameraFeed::CameraFeed(String p_name, FeedPosition p_position) {
  132. // initialize our feed
  133. id = CameraServer::get_singleton()->get_free_id();
  134. base_width = 0;
  135. base_height = 0;
  136. name = p_name;
  137. active = false;
  138. datatype = CameraFeed::FEED_NOIMAGE;
  139. position = p_position;
  140. transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
  141. texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  142. texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  143. }
  144. CameraFeed::~CameraFeed() {
  145. // Free our textures
  146. ERR_FAIL_NULL(RenderingServer::get_singleton());
  147. RenderingServer::get_singleton()->free(texture[CameraServer::FEED_Y_IMAGE]);
  148. RenderingServer::get_singleton()->free(texture[CameraServer::FEED_CBCR_IMAGE]);
  149. }
  150. void CameraFeed::set_rgb_image(const Ref<Image> &p_rgb_img) {
  151. ERR_FAIL_COND(p_rgb_img.is_null());
  152. if (active) {
  153. int new_width = p_rgb_img->get_width();
  154. int new_height = p_rgb_img->get_height();
  155. if ((base_width != new_width) || (base_height != new_height)) {
  156. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  157. base_width = new_width;
  158. base_height = new_height;
  159. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_rgb_img);
  160. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
  161. emit_signal(SNAME("format_changed"));
  162. } else {
  163. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img);
  164. }
  165. datatype = CameraFeed::FEED_RGB;
  166. }
  167. }
  168. void CameraFeed::set_ycbcr_image(const Ref<Image> &p_ycbcr_img) {
  169. ERR_FAIL_COND(p_ycbcr_img.is_null());
  170. if (active) {
  171. int new_width = p_ycbcr_img->get_width();
  172. int new_height = p_ycbcr_img->get_height();
  173. if ((base_width != new_width) || (base_height != new_height)) {
  174. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  175. base_width = new_width;
  176. base_height = new_height;
  177. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_ycbcr_img);
  178. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
  179. emit_signal(SNAME("format_changed"));
  180. } else {
  181. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_ycbcr_img);
  182. }
  183. datatype = CameraFeed::FEED_YCBCR;
  184. }
  185. }
  186. void CameraFeed::set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img) {
  187. ERR_FAIL_COND(p_y_img.is_null());
  188. ERR_FAIL_COND(p_cbcr_img.is_null());
  189. if (active) {
  190. ///@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..
  191. // Wondering about including that into multiple projects, may cause issues.
  192. // That said, if we convert to RGB, we could enable using texture resources again...
  193. int new_y_width = p_y_img->get_width();
  194. int new_y_height = p_y_img->get_height();
  195. if ((base_width != new_y_width) || (base_height != new_y_height)) {
  196. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  197. base_width = new_y_width;
  198. base_height = new_y_height;
  199. {
  200. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_y_img);
  201. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_Y_IMAGE], new_texture);
  202. }
  203. {
  204. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_cbcr_img);
  205. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_CBCR_IMAGE], new_texture);
  206. }
  207. emit_signal(SNAME("format_changed"));
  208. } else {
  209. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_Y_IMAGE], p_y_img);
  210. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_CBCR_IMAGE], p_cbcr_img);
  211. }
  212. datatype = CameraFeed::FEED_YCBCR_SEP;
  213. }
  214. }
  215. void CameraFeed::set_external(int p_width, int p_height) {
  216. if ((base_width != p_width) || (base_height != p_height)) {
  217. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  218. base_width = p_width;
  219. base_height = p_height;
  220. auto new_texture = RenderingServer::get_singleton()->texture_external_create(p_width, p_height, 0);
  221. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_YCBCR_IMAGE], new_texture);
  222. }
  223. datatype = CameraFeed::FEED_EXTERNAL;
  224. }
  225. bool CameraFeed::activate_feed() {
  226. // nothing to do here
  227. return true;
  228. }
  229. void CameraFeed::deactivate_feed() {
  230. // nothing to do here
  231. }
  232. bool CameraFeed::set_format(int p_index, const Dictionary &p_parameters) {
  233. return false;
  234. }
  235. Array CameraFeed::get_formats() const {
  236. return Array();
  237. }
  238. CameraFeed::FeedFormat CameraFeed::get_format() const {
  239. FeedFormat feed_format = {};
  240. return feed_format;
  241. }