external_texture.cpp 4.4 KB

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