camera_feed.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* camera_feed.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/visual_server.h"
  32. void CameraFeed::_bind_methods() {
  33. // The setters prefixed with _ are only exposed so we can have feeds through GDNative!
  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("get_name"), &CameraFeed::get_name);
  37. ClassDB::bind_method(D_METHOD("_set_name", "name"), &CameraFeed::set_name);
  38. ClassDB::bind_method(D_METHOD("is_active"), &CameraFeed::is_active);
  39. ClassDB::bind_method(D_METHOD("set_active", "active"), &CameraFeed::set_active);
  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("_set_YCbCr_imgs", "y_img", "cbcr_img"), &CameraFeed::set_YCbCr_imgs);
  48. ClassDB::bind_method(D_METHOD("_allocate_texture", "width", "height", "format", "texture_type", "data_type"), &CameraFeed::allocate_texture);
  49. ADD_GROUP("Feed", "feed_");
  50. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active");
  51. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform");
  52. BIND_ENUM_CONSTANT(FEED_NOIMAGE);
  53. BIND_ENUM_CONSTANT(FEED_RGB);
  54. BIND_ENUM_CONSTANT(FEED_YCBCR);
  55. BIND_ENUM_CONSTANT(FEED_YCBCR_SEP);
  56. BIND_ENUM_CONSTANT(FEED_UNSPECIFIED);
  57. BIND_ENUM_CONSTANT(FEED_FRONT);
  58. BIND_ENUM_CONSTANT(FEED_BACK);
  59. }
  60. int CameraFeed::get_id() const {
  61. return id;
  62. }
  63. bool CameraFeed::is_active() const {
  64. return active;
  65. }
  66. void CameraFeed::set_active(bool p_is_active) {
  67. if (p_is_active == active) {
  68. // all good
  69. } else if (p_is_active) {
  70. // attempt to activate this feed
  71. if (activate_feed()) {
  72. print_line("Activate " + name);
  73. active = true;
  74. }
  75. } else {
  76. // just deactivate it
  77. deactivate_feed();
  78. print_line("Deactivate " + name);
  79. active = false;
  80. }
  81. }
  82. String CameraFeed::get_name() const {
  83. return name;
  84. }
  85. void CameraFeed::set_name(String p_name) {
  86. name = p_name;
  87. }
  88. int CameraFeed::get_base_width() const {
  89. return base_width;
  90. }
  91. int CameraFeed::get_base_height() const {
  92. return base_height;
  93. }
  94. CameraFeed::FeedDataType CameraFeed::get_datatype() const {
  95. return datatype;
  96. }
  97. CameraFeed::FeedPosition CameraFeed::get_position() const {
  98. return position;
  99. }
  100. void CameraFeed::set_position(CameraFeed::FeedPosition p_position) {
  101. position = p_position;
  102. }
  103. Transform2D CameraFeed::get_transform() const {
  104. return transform;
  105. }
  106. void CameraFeed::set_transform(const Transform2D &p_transform) {
  107. transform = p_transform;
  108. }
  109. RID CameraFeed::get_texture(CameraServer::FeedImage p_which) {
  110. return texture[p_which];
  111. }
  112. CameraFeed::CameraFeed() {
  113. // initialize our feed
  114. id = CameraServer::get_singleton()->get_free_id();
  115. name = "???";
  116. active = false;
  117. datatype = CameraFeed::FEED_RGB;
  118. position = CameraFeed::FEED_UNSPECIFIED;
  119. transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
  120. // create a texture object
  121. VisualServer *vs = VisualServer::get_singleton();
  122. texture[CameraServer::FEED_Y_IMAGE] = vs->texture_create(); // also used for RGBA
  123. texture[CameraServer::FEED_CBCR_IMAGE] = vs->texture_create();
  124. }
  125. CameraFeed::CameraFeed(String p_name, FeedPosition p_position) {
  126. // initialize our feed
  127. id = CameraServer::get_singleton()->get_free_id();
  128. base_width = 0;
  129. base_height = 0;
  130. name = p_name;
  131. active = false;
  132. datatype = CameraFeed::FEED_NOIMAGE;
  133. position = p_position;
  134. transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
  135. // create a texture object
  136. VisualServer *vs = VisualServer::get_singleton();
  137. texture[CameraServer::FEED_Y_IMAGE] = vs->texture_create(); // also used for RGBA
  138. texture[CameraServer::FEED_CBCR_IMAGE] = vs->texture_create();
  139. }
  140. CameraFeed::~CameraFeed() {
  141. // Free our textures
  142. VisualServer *vs = VisualServer::get_singleton();
  143. vs->free(texture[CameraServer::FEED_Y_IMAGE]);
  144. vs->free(texture[CameraServer::FEED_CBCR_IMAGE]);
  145. }
  146. void CameraFeed::set_RGB_img(const Ref<Image> &p_rgb_img) {
  147. ERR_FAIL_COND(p_rgb_img.is_null());
  148. if (active) {
  149. VisualServer *vs = VisualServer::get_singleton();
  150. int new_width = p_rgb_img->get_width();
  151. int new_height = p_rgb_img->get_height();
  152. if ((base_width != new_width) || (base_height != new_height)) {
  153. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  154. base_width = new_width;
  155. base_height = new_height;
  156. vs->texture_allocate(texture[CameraServer::FEED_RGBA_IMAGE], new_width, new_height, 0, Image::FORMAT_RGB8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAGS_DEFAULT);
  157. }
  158. vs->texture_set_data(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img);
  159. datatype = CameraFeed::FEED_RGB;
  160. }
  161. }
  162. void CameraFeed::set_YCbCr_img(const Ref<Image> &p_ycbcr_img) {
  163. ERR_FAIL_COND(p_ycbcr_img.is_null());
  164. if (active) {
  165. VisualServer *vs = VisualServer::get_singleton();
  166. int new_width = p_ycbcr_img->get_width();
  167. int new_height = p_ycbcr_img->get_height();
  168. if ((base_width != new_width) || (base_height != new_height)) {
  169. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  170. base_width = new_width;
  171. base_height = new_height;
  172. vs->texture_allocate(texture[CameraServer::FEED_RGBA_IMAGE], new_width, new_height, 0, Image::FORMAT_RGB8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAGS_DEFAULT);
  173. }
  174. vs->texture_set_data(texture[CameraServer::FEED_RGBA_IMAGE], p_ycbcr_img);
  175. datatype = CameraFeed::FEED_YCBCR;
  176. }
  177. }
  178. void CameraFeed::set_YCbCr_imgs(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img) {
  179. ERR_FAIL_COND(p_y_img.is_null());
  180. ERR_FAIL_COND(p_cbcr_img.is_null());
  181. if (active) {
  182. VisualServer *vs = VisualServer::get_singleton();
  183. ///@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..
  184. // Wondering about including that into multiple projects, may cause issues.
  185. // That said, if we convert to RGB, we could enable using texture resources again...
  186. int new_y_width = p_y_img->get_width();
  187. int new_y_height = p_y_img->get_height();
  188. int new_cbcr_width = p_cbcr_img->get_width();
  189. int new_cbcr_height = p_cbcr_img->get_height();
  190. if ((base_width != new_y_width) || (base_height != new_y_height)) {
  191. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  192. base_width = new_y_width;
  193. base_height = new_y_height;
  194. vs->texture_allocate(texture[CameraServer::FEED_Y_IMAGE], new_y_width, new_y_height, 0, Image::FORMAT_R8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_USED_FOR_STREAMING);
  195. ///@TODO GLES2 doesn't support FORMAT_RG8, need to do some form of conversion
  196. vs->texture_allocate(texture[CameraServer::FEED_CBCR_IMAGE], new_cbcr_width, new_cbcr_height, 0, Image::FORMAT_RG8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_USED_FOR_STREAMING);
  197. }
  198. vs->texture_set_data(texture[CameraServer::FEED_Y_IMAGE], p_y_img);
  199. vs->texture_set_data(texture[CameraServer::FEED_CBCR_IMAGE], p_cbcr_img);
  200. datatype = CameraFeed::FEED_YCBCR_SEP;
  201. }
  202. }
  203. void CameraFeed::allocate_texture(int p_width, int p_height, Image::Format p_format, VisualServer::TextureType p_texture_type, FeedDataType p_data_type) {
  204. VisualServer *vs = VisualServer::get_singleton();
  205. if ((base_width != p_width) || (base_height != p_height)) {
  206. // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot...
  207. base_width = p_width;
  208. base_height = p_height;
  209. vs->texture_allocate(texture[0], p_width, p_height, 0, p_format, p_texture_type, VS::TEXTURE_FLAGS_DEFAULT);
  210. }
  211. datatype = p_data_type;
  212. }
  213. bool CameraFeed::activate_feed() {
  214. // nothing to do here
  215. return true;
  216. }
  217. void CameraFeed::deactivate_feed() {
  218. // nothing to do here
  219. }