2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* visual_server_wrap_mt.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "visual_server_wrap_mt.h"
|
2014-03-14 01:57:24 +00:00
|
|
|
#include "globals.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "os/os.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
void VisualServerWrapMT::thread_exit() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
exit = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisualServerWrapMT::thread_draw() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
draw_mutex->lock();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
draw_pending--;
|
2017-03-18 23:36:26 +00:00
|
|
|
bool draw = (draw_pending == 0); // only draw when no more flushes are pending
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
draw_mutex->unlock();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (draw) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
visual_server->draw();
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisualServerWrapMT::thread_flush() {
|
|
|
|
|
|
|
|
draw_mutex->lock();
|
|
|
|
|
|
|
|
draw_pending--;
|
|
|
|
|
|
|
|
draw_mutex->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualServerWrapMT::_thread_callback(void *_instance) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
vsmt->thread_loop();
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void VisualServerWrapMT::thread_loop() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
server_thread = Thread::get_caller_ID();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
OS::get_singleton()->make_rendering_thread();
|
|
|
|
|
|
|
|
visual_server->init();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
exit = false;
|
|
|
|
draw_thread_up = true;
|
|
|
|
while (!exit) {
|
2014-02-10 01:10:30 +00:00
|
|
|
// flush commands one by one, until exit is requested
|
|
|
|
command_queue.wait_and_flush_one();
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
command_queue.flush_all(); // flush all
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
visual_server->finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EVENT QUEUING */
|
|
|
|
|
2015-06-07 01:06:58 +00:00
|
|
|
void VisualServerWrapMT::sync() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (create_thread) {
|
|
|
|
|
2015-06-07 01:06:58 +00:00
|
|
|
/* TODO: sync with the thread */
|
|
|
|
|
|
|
|
/*
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!draw_mutex);
|
|
|
|
draw_mutex->lock();
|
|
|
|
draw_pending++; //cambiar por un saferefcount
|
|
|
|
draw_mutex->unlock();
|
2015-06-07 01:06:58 +00:00
|
|
|
*/
|
|
|
|
//command_queue.push( this, &VisualServerWrapMT::thread_flush);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
command_queue.flush_all(); //flush all pending from other threads
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualServerWrapMT::draw() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (create_thread) {
|
|
|
|
|
2015-06-07 01:06:58 +00:00
|
|
|
/* TODO: Make it draw
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!draw_mutex);
|
|
|
|
draw_mutex->lock();
|
|
|
|
draw_pending++; //cambiar por un saferefcount
|
|
|
|
draw_mutex->unlock();
|
|
|
|
|
|
|
|
command_queue.push( this, &VisualServerWrapMT::thread_draw);
|
2015-06-07 01:06:58 +00:00
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
visual_server->draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualServerWrapMT::init() {
|
|
|
|
|
|
|
|
if (create_thread) {
|
|
|
|
|
|
|
|
draw_mutex = Mutex::create();
|
|
|
|
print_line("CREATING RENDER THREAD");
|
|
|
|
OS::get_singleton()->release_rendering_thread();
|
|
|
|
if (create_thread) {
|
2017-03-18 23:36:26 +00:00
|
|
|
thread = Thread::create(_thread_callback, this);
|
2014-02-10 01:10:30 +00:00
|
|
|
print_line("STARTING RENDER THREAD");
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
while (!draw_thread_up) {
|
2014-02-10 01:10:30 +00:00
|
|
|
OS::get_singleton()->delay_usec(1000);
|
|
|
|
}
|
|
|
|
print_line("DONE RENDER THREAD");
|
|
|
|
} else {
|
|
|
|
|
|
|
|
visual_server->init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualServerWrapMT::finish() {
|
|
|
|
|
|
|
|
if (thread) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
command_queue.push(this, &VisualServerWrapMT::thread_exit);
|
|
|
|
Thread::wait_to_finish(thread);
|
2014-02-10 01:10:30 +00:00
|
|
|
memdelete(thread);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
texture_free_cached_ids();
|
2014-06-16 13:22:26 +00:00
|
|
|
mesh_free_cached_ids();
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
thread = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
visual_server->finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (draw_mutex)
|
|
|
|
memdelete(draw_mutex);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread)
|
|
|
|
: command_queue(p_create_thread) {
|
|
|
|
|
|
|
|
visual_server = p_contained;
|
|
|
|
create_thread = p_create_thread;
|
|
|
|
thread = NULL;
|
|
|
|
draw_mutex = NULL;
|
|
|
|
draw_pending = 0;
|
|
|
|
draw_thread_up = false;
|
|
|
|
alloc_mutex = Mutex::create();
|
|
|
|
texture_pool_max_size = GLOBAL_DEF("render/thread_textures_prealloc", 5);
|
|
|
|
mesh_pool_max_size = GLOBAL_DEF("core/rid_pool_prealloc", 20);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!p_create_thread) {
|
2017-03-18 23:36:26 +00:00
|
|
|
server_thread = Thread::get_caller_ID();
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
server_thread = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VisualServerWrapMT::~VisualServerWrapMT() {
|
|
|
|
|
|
|
|
memdelete(visual_server);
|
2014-03-14 01:57:24 +00:00
|
|
|
memdelete(alloc_mutex);
|
2014-02-10 01:10:30 +00:00
|
|
|
//finish();
|
|
|
|
}
|