Path: blob/master/scene/resources/external_texture.cpp
20941 views
/**************************************************************************/1/* external_texture.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "external_texture.h"3132#include "servers/rendering/rendering_server.h"3334void ExternalTexture::_bind_methods() {35ClassDB::bind_method(D_METHOD("set_size", "size"), &ExternalTexture::set_size);36ClassDB::bind_method(D_METHOD("get_external_texture_id"), &ExternalTexture::get_external_texture_id);37ClassDB::bind_method(D_METHOD("set_external_buffer_id", "external_buffer_id"), &ExternalTexture::set_external_buffer_id);3839ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");40}4142uint64_t ExternalTexture::get_external_texture_id() const {43_ensure_created();44return RenderingServer::get_singleton()->texture_get_native_handle(texture);45}4647void ExternalTexture::set_size(const Size2 &p_size) {48if (p_size.width > 0 && p_size.height > 0 && p_size != size) {49size = p_size;50_ensure_created();51RenderingServer::get_singleton()->texture_external_update(texture, size.width, size.height, external_buffer);52emit_changed();53}54}5556Size2 ExternalTexture::get_size() const {57return size;58}5960void ExternalTexture::set_external_buffer_id(uint64_t p_external_buffer) {61if (p_external_buffer != external_buffer) {62external_buffer = p_external_buffer;63_ensure_created();64RenderingServer::get_singleton()->texture_external_update(texture, size.width, size.height, external_buffer);65}66}6768int ExternalTexture::get_width() const {69return size.width;70}7172int ExternalTexture::get_height() const {73return size.height;74}7576bool ExternalTexture::has_alpha() const {77return false;78}7980RID ExternalTexture::get_rid() const {81if (!texture.is_valid()) {82texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();83using_placeholder = true;84}85return texture;86}8788void ExternalTexture::_ensure_created() const {89if (texture.is_valid() && !using_placeholder) {90return;91}9293RID new_texture = RenderingServer::get_singleton()->texture_external_create(size.width, size.height);94if (using_placeholder) {95DEV_ASSERT(texture.is_valid());96RenderingServer::get_singleton()->texture_replace(texture, new_texture);97using_placeholder = false;98} else {99texture = new_texture;100}101}102103ExternalTexture::ExternalTexture() {104}105106ExternalTexture::~ExternalTexture() {107if (texture.is_valid()) {108ERR_FAIL_NULL(RenderingServer::get_singleton());109RenderingServer::get_singleton()->free_rid(texture);110}111}112113114