Path: blob/master/scene/resources/external_texture.cpp
9902 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"3132void ExternalTexture::_bind_methods() {33ClassDB::bind_method(D_METHOD("set_size", "size"), &ExternalTexture::set_size);34ClassDB::bind_method(D_METHOD("get_external_texture_id"), &ExternalTexture::get_external_texture_id);35ClassDB::bind_method(D_METHOD("set_external_buffer_id", "external_buffer_id"), &ExternalTexture::set_external_buffer_id);3637ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");38}3940uint64_t ExternalTexture::get_external_texture_id() const {41_ensure_created();42return RenderingServer::get_singleton()->texture_get_native_handle(texture);43}4445void ExternalTexture::set_size(const Size2 &p_size) {46if (p_size.width > 0 && p_size.height > 0 && p_size != size) {47size = p_size;48_ensure_created();49RenderingServer::get_singleton()->texture_external_update(texture, size.width, size.height, external_buffer);50emit_changed();51}52}5354Size2 ExternalTexture::get_size() const {55return size;56}5758void ExternalTexture::set_external_buffer_id(uint64_t p_external_buffer) {59if (p_external_buffer != external_buffer) {60external_buffer = p_external_buffer;61_ensure_created();62RenderingServer::get_singleton()->texture_external_update(texture, size.width, size.height, external_buffer);63}64}6566int ExternalTexture::get_width() const {67return size.width;68}6970int ExternalTexture::get_height() const {71return size.height;72}7374bool ExternalTexture::has_alpha() const {75return false;76}7778RID ExternalTexture::get_rid() const {79if (!texture.is_valid()) {80texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();81using_placeholder = true;82}83return texture;84}8586void ExternalTexture::_ensure_created() const {87if (texture.is_valid() && !using_placeholder) {88return;89}9091RID new_texture = RenderingServer::get_singleton()->texture_external_create(size.width, size.height);92if (using_placeholder) {93DEV_ASSERT(texture.is_valid());94RenderingServer::get_singleton()->texture_replace(texture, new_texture);95using_placeholder = false;96} else {97texture = new_texture;98}99}100101ExternalTexture::ExternalTexture() {102}103104ExternalTexture::~ExternalTexture() {105if (texture.is_valid()) {106ERR_FAIL_NULL(RenderingServer::get_singleton());107RenderingServer::get_singleton()->free(texture);108}109}110111112