camera_feed.cpp 11 KB

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