camera_feed.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************/
  2. /* camera_feed.h */
  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. #ifndef CAMERA_FEED_H
  31. #define CAMERA_FEED_H
  32. #include "core/io/image.h"
  33. #include "core/math/transform_2d.h"
  34. #include "servers/camera_server.h"
  35. #include "servers/rendering_server.h"
  36. /**
  37. The camera server is a singleton object that gives access to the various
  38. camera feeds that can be used as the background for our environment.
  39. **/
  40. class CameraFeed : public RefCounted {
  41. GDCLASS(CameraFeed, RefCounted);
  42. public:
  43. enum FeedDataType {
  44. FEED_NOIMAGE, // we don't have an image yet
  45. FEED_RGB, // our texture will contain a normal RGB texture that can be used directly
  46. FEED_YCBCR, // our texture will contain a YCbCr texture that needs to be converted to RGB before output
  47. FEED_YCBCR_SEP, // our camera is split into two textures, first plane contains Y data, second plane contains CbCr data
  48. FEED_EXTERNAL, // specific for android atm, camera feed is managed externally, assumed RGB for now
  49. };
  50. enum FeedPosition {
  51. FEED_UNSPECIFIED, // we have no idea
  52. FEED_FRONT, // this is a camera on the front of the device
  53. FEED_BACK // this is a camera on the back of the device
  54. };
  55. private:
  56. int id; // unique id for this, for internal use in case feeds are removed
  57. protected:
  58. struct FeedFormat {
  59. int width = 0;
  60. int height = 0;
  61. String format;
  62. int frame_numerator = 0;
  63. int frame_denominator = 0;
  64. uint32_t pixel_format = 0;
  65. };
  66. String name; // name of our camera feed
  67. FeedDataType datatype; // type of texture data stored
  68. FeedPosition position; // position of camera on the device
  69. Transform2D transform; // display transform
  70. int base_width = 0;
  71. int base_height = 0;
  72. Vector<FeedFormat> formats;
  73. Dictionary parameters;
  74. int selected_format = -1;
  75. bool active; // only when active do we actually update the camera texture each frame
  76. RID texture[CameraServer::FEED_IMAGES]; // texture images needed for this
  77. static void _bind_methods();
  78. public:
  79. int get_id() const;
  80. bool is_active() const;
  81. void set_active(bool p_is_active);
  82. String get_name() const;
  83. void set_name(String p_name);
  84. int get_base_width() const;
  85. int get_base_height() const;
  86. FeedPosition get_position() const;
  87. void set_position(FeedPosition p_position);
  88. Transform2D get_transform() const;
  89. void set_transform(const Transform2D &p_transform);
  90. RID get_texture(CameraServer::FeedImage p_which);
  91. uint64_t get_texture_tex_id(CameraServer::FeedImage p_which);
  92. CameraFeed();
  93. CameraFeed(String p_name, FeedPosition p_position = CameraFeed::FEED_UNSPECIFIED);
  94. virtual ~CameraFeed();
  95. FeedDataType get_datatype() const;
  96. void set_rgb_image(const Ref<Image> &p_rgb_img);
  97. void set_ycbcr_image(const Ref<Image> &p_ycbcr_img);
  98. void set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img);
  99. void set_external(int p_width, int p_height);
  100. virtual bool set_format(int p_index, const Dictionary &p_parameters);
  101. virtual Array get_formats() const;
  102. virtual FeedFormat get_format() const;
  103. virtual bool activate_feed();
  104. virtual void deactivate_feed();
  105. };
  106. VARIANT_ENUM_CAST(CameraFeed::FeedDataType);
  107. VARIANT_ENUM_CAST(CameraFeed::FeedPosition);
  108. #endif // CAMERA_FEED_H