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