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