camera_feed.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // The setters prefixed with _ are only exposed so we can have feeds through GDExtension!
  34. // They should not be called by the end user.
  35. ClassDB::bind_method(D_METHOD("get_id"), &CameraFeed::get_id);
  36. ClassDB::bind_method(D_METHOD("is_active"), &CameraFeed::is_active);
  37. ClassDB::bind_method(D_METHOD("set_active", "active"), &CameraFeed::set_active);
  38. ClassDB::bind_method(D_METHOD("get_name"), &CameraFeed::get_name);
  39. ClassDB::bind_method(D_METHOD("_set_name", "name"), &CameraFeed::set_name);
  40. ClassDB::bind_method(D_METHOD("get_position"), &CameraFeed::get_position);
  41. ClassDB::bind_method(D_METHOD("_set_position", "position"), &CameraFeed::set_position);
  42. // Note, for transform some feeds may override what the user sets (such as ARKit)
  43. ClassDB::bind_method(D_METHOD("get_transform"), &CameraFeed::get_transform);
  44. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CameraFeed::set_transform);
  45. ClassDB::bind_method(D_METHOD("_set_RGB_img", "rgb_img"), &CameraFeed::set_RGB_img);
  46. ClassDB::bind_method(D_METHOD("_set_YCbCr_img", "ycbcr_img"), &CameraFeed::set_YCbCr_img);
  47. ClassDB::bind_method(D_METHOD("get_datatype"), &CameraFeed::get_datatype);
  48. ADD_GROUP("Feed", "feed_");
  49. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active");
  50. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform");
  51. BIND_ENUM_CONSTANT(FEED_NOIMAGE);
  52. BIND_ENUM_CONSTANT(FEED_RGB);
  53. BIND_ENUM_CONSTANT(FEED_YCBCR);
  54. BIND_ENUM_CONSTANT(FEED_YCBCR_SEP);
  55. BIND_ENUM_CONSTANT(FEED_UNSPECIFIED);
  56. BIND_ENUM_CONSTANT(FEED_FRONT);
  57. BIND_ENUM_CONSTANT(FEED_BACK);
  58. }
  59. int CameraFeed::get_id() const {
  60. return id;
  61. }
  62. bool CameraFeed::is_active() const {
  63. return active;
  64. }
  65. void CameraFeed::set_active(bool p_is_active) {
  66. if (p_is_active == active) {
  67. // all good
  68. } else if (p_is_active) {
  69. // attempt to activate this feed
  70. if (activate_feed()) {
  71. print_line("Activate " + name);
  72. active = true;
  73. }
  74. } else {
  75. // just deactivate it
  76. deactivate_feed();
  77. print_line("Deactivate " + name);
  78. active = false;
  79. }
  80. }
  81. String CameraFeed::get_name() const {
  82. return name;
  83. }
  84. void CameraFeed::set_name(String p_name) {
  85. name = p_name;
  86. }
  87. int CameraFeed::get_base_width() const {
  88. return base_width;
  89. }
  90. int CameraFeed::get_base_height() const {
  91. return base_height;
  92. }
  93. CameraFeed::FeedDataType CameraFeed::get_datatype() const {
  94. return datatype;
  95. }
  96. CameraFeed::FeedPosition CameraFeed::get_position() const {
  97. return position;
  98. }
  99. void CameraFeed::set_position(CameraFeed::FeedPosition p_position) {
  100. position = p_position;
  101. }
  102. Transform2D CameraFeed::get_transform() const {
  103. return transform;
  104. }
  105. void CameraFeed::set_transform(const Transform2D &p_transform) {
  106. transform = p_transform;
  107. }
  108. RID CameraFeed::get_texture(CameraServer::FeedImage p_which) {
  109. return texture[p_which];
  110. }
  111. CameraFeed::CameraFeed() {
  112. // initialize our feed
  113. id = CameraServer::get_singleton()->get_free_id();
  114. base_width = 0;
  115. base_height = 0;
  116. name = "???";
  117. active = false;
  118. datatype = CameraFeed::FEED_RGB;
  119. position = CameraFeed::FEED_UNSPECIFIED;
  120. transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
  121. texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  122. texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  123. }
  124. CameraFeed::CameraFeed(String p_name, FeedPosition p_position) {
  125. // initialize our feed
  126. id = CameraServer::get_singleton()->get_free_id();
  127. base_width = 0;
  128. base_height = 0;
  129. name = p_name;
  130. active = false;
  131. datatype = CameraFeed::FEED_NOIMAGE;
  132. position = p_position;
  133. transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
  134. texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  135. texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  136. }
  137. CameraFeed::~CameraFeed() {
  138. // Free our textures
  139. ERR_FAIL_NULL(RenderingServer::get_singleton());
  140. RenderingServer::get_singleton()->free(texture[CameraServer::FEED_Y_IMAGE]);
  141. RenderingServer::get_singleton()->free(texture[CameraServer::FEED_CBCR_IMAGE]);
  142. }
  143. void CameraFeed::set_RGB_img(const Ref<Image> &p_rgb_img) {
  144. ERR_FAIL_COND(p_rgb_img.is_null());
  145. if (active) {
  146. int new_width = p_rgb_img->get_width();
  147. int new_height = p_rgb_img->get_height();
  148. if ((base_width != new_width) || (base_height != new_height)) {
  149. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  150. base_width = new_width;
  151. base_height = new_height;
  152. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_rgb_img);
  153. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
  154. } else {
  155. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img);
  156. }
  157. datatype = CameraFeed::FEED_RGB;
  158. }
  159. }
  160. void CameraFeed::set_YCbCr_img(const Ref<Image> &p_ycbcr_img) {
  161. ERR_FAIL_COND(p_ycbcr_img.is_null());
  162. if (active) {
  163. int new_width = p_ycbcr_img->get_width();
  164. int new_height = p_ycbcr_img->get_height();
  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_ycbcr_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_ycbcr_img);
  173. }
  174. datatype = CameraFeed::FEED_YCBCR;
  175. }
  176. }
  177. void CameraFeed::set_YCbCr_imgs(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img) {
  178. ERR_FAIL_COND(p_y_img.is_null());
  179. ERR_FAIL_COND(p_cbcr_img.is_null());
  180. if (active) {
  181. ///@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..
  182. // Wondering about including that into multiple projects, may cause issues.
  183. // That said, if we convert to RGB, we could enable using texture resources again...
  184. int new_y_width = p_y_img->get_width();
  185. int new_y_height = p_y_img->get_height();
  186. if ((base_width != new_y_width) || (base_height != new_y_height)) {
  187. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  188. base_width = new_y_width;
  189. base_height = new_y_height;
  190. {
  191. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_y_img);
  192. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_Y_IMAGE], new_texture);
  193. }
  194. {
  195. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_cbcr_img);
  196. RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_CBCR_IMAGE], new_texture);
  197. }
  198. } else {
  199. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_Y_IMAGE], p_y_img);
  200. RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_CBCR_IMAGE], p_cbcr_img);
  201. }
  202. datatype = CameraFeed::FEED_YCBCR_SEP;
  203. }
  204. }
  205. bool CameraFeed::activate_feed() {
  206. // nothing to do here
  207. return true;
  208. }
  209. void CameraFeed::deactivate_feed() {
  210. // nothing to do here
  211. }