| 1 | /**************************************************************************/ |
| 2 | /* external_texture.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 | |
| 31 | #include "external_texture.h" |
| 32 | |
| 33 | #include "servers/rendering/rendering_server.h" |
| 34 | |
| 35 | void ExternalTexture::_bind_methods() { |
| 36 | ClassDB::bind_method(D_METHOD("set_size", "size"), &ExternalTexture::set_size); |
| 37 | ClassDB::bind_method(D_METHOD("get_external_texture_id"), &ExternalTexture::get_external_texture_id); |
| 38 | ClassDB::bind_method(D_METHOD("set_external_buffer_id", "external_buffer_id"), &ExternalTexture::set_external_buffer_id); |
| 39 | |
| 40 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size"); |
| 41 | } |
| 42 | |
| 43 | uint64_t ExternalTexture::get_external_texture_id() const { |
| 44 | _ensure_created(); |
| 45 | return RenderingServer::get_singleton()->texture_get_native_handle(texture); |
| 46 | } |
| 47 | |
| 48 | void ExternalTexture::set_size(const Size2 &p_size) { |
| 49 | if (p_size.width > 0 && p_size.height > 0 && p_size != size) { |
| 50 | size = p_size; |
| 51 | _ensure_created(); |
| 52 | RenderingServer::get_singleton()->texture_external_update(texture, size.width, size.height, external_buffer); |
| 53 | emit_changed(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | Size2 ExternalTexture::get_size() const { |
| 58 | return size; |
| 59 | } |
| 60 | |
| 61 | void ExternalTexture::set_external_buffer_id(uint64_t p_external_buffer) { |
| 62 | if (p_external_buffer != external_buffer) { |
| 63 | external_buffer = p_external_buffer; |
| 64 | _ensure_created(); |
| 65 | RenderingServer::get_singleton()->texture_external_update(texture, size.width, size.height, external_buffer); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int ExternalTexture::get_width() const { |
| 70 | return size.width; |
| 71 | } |
| 72 | |
| 73 | int ExternalTexture::get_height() const { |
| 74 | return size.height; |
| 75 | } |
| 76 | |
| 77 | bool ExternalTexture::has_alpha() const { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | RID ExternalTexture::get_rid() const { |
| 82 | if (!texture.is_valid()) { |
| 83 | texture = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 84 | using_placeholder = true; |
| 85 | } |
| 86 | return texture; |
| 87 | } |
| 88 | |
| 89 | void ExternalTexture::_ensure_created() const { |
| 90 | if (texture.is_valid() && !using_placeholder) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | RID new_texture = RenderingServer::get_singleton()->texture_external_create(size.width, size.height); |
| 95 | if (using_placeholder) { |
| 96 | DEV_ASSERT(texture.is_valid()); |
| 97 | RenderingServer::get_singleton()->texture_replace(texture, new_texture); |
| 98 | using_placeholder = false; |
| 99 | } else { |
| 100 | texture = new_texture; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | ExternalTexture::ExternalTexture() { |
| 105 | } |
| 106 | |
| 107 | ExternalTexture::~ExternalTexture() { |
| 108 | if (texture.is_valid()) { |
| 109 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 110 | RenderingServer::get_singleton()->free_rid(texture); |
| 111 | } |
| 112 | } |