external_texture.cpp 4.5 KB

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