1 /*************************************************************************/
2 /*  visual_server_wrap_mt.cpp                                            */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 "visual_server_wrap_mt.h"
32 #include "core/os/os.h"
33 #include "core/project_settings.h"
34 
thread_exit()35 void VisualServerWrapMT::thread_exit() {
36 
37 	exit = true;
38 }
39 
thread_draw(bool p_swap_buffers,double frame_step)40 void VisualServerWrapMT::thread_draw(bool p_swap_buffers, double frame_step) {
41 
42 	if (!atomic_decrement(&draw_pending)) {
43 
44 		visual_server->draw(p_swap_buffers, frame_step);
45 	}
46 }
47 
thread_flush()48 void VisualServerWrapMT::thread_flush() {
49 
50 	atomic_decrement(&draw_pending);
51 }
52 
_thread_callback(void * _instance)53 void VisualServerWrapMT::_thread_callback(void *_instance) {
54 
55 	VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance);
56 
57 	vsmt->thread_loop();
58 }
59 
thread_loop()60 void VisualServerWrapMT::thread_loop() {
61 
62 	server_thread = Thread::get_caller_id();
63 
64 	OS::get_singleton()->make_rendering_thread();
65 
66 	visual_server->init();
67 
68 	exit = false;
69 	draw_thread_up = true;
70 	while (!exit) {
71 		// flush commands one by one, until exit is requested
72 		command_queue.wait_and_flush_one();
73 	}
74 
75 	command_queue.flush_all(); // flush all
76 
77 	visual_server->finish();
78 }
79 
80 /* EVENT QUEUING */
81 
sync()82 void VisualServerWrapMT::sync() {
83 
84 	if (create_thread) {
85 
86 		atomic_increment(&draw_pending);
87 		command_queue.push_and_sync(this, &VisualServerWrapMT::thread_flush);
88 	} else {
89 
90 		command_queue.flush_all(); //flush all pending from other threads
91 	}
92 }
93 
draw(bool p_swap_buffers,double frame_step)94 void VisualServerWrapMT::draw(bool p_swap_buffers, double frame_step) {
95 
96 	if (create_thread) {
97 
98 		atomic_increment(&draw_pending);
99 		command_queue.push(this, &VisualServerWrapMT::thread_draw, p_swap_buffers, frame_step);
100 	} else {
101 
102 		visual_server->draw(p_swap_buffers, frame_step);
103 	}
104 }
105 
init()106 void VisualServerWrapMT::init() {
107 
108 	if (create_thread) {
109 
110 		print_verbose("VisualServerWrapMT: Creating render thread");
111 		OS::get_singleton()->release_rendering_thread();
112 		if (create_thread) {
113 			thread = Thread::create(_thread_callback, this);
114 			print_verbose("VisualServerWrapMT: Starting render thread");
115 		}
116 		while (!draw_thread_up) {
117 			OS::get_singleton()->delay_usec(1000);
118 		}
119 		print_verbose("VisualServerWrapMT: Finished render thread");
120 	} else {
121 
122 		visual_server->init();
123 	}
124 }
125 
finish()126 void VisualServerWrapMT::finish() {
127 
128 	if (thread) {
129 
130 		command_queue.push(this, &VisualServerWrapMT::thread_exit);
131 		Thread::wait_to_finish(thread);
132 		memdelete(thread);
133 
134 		thread = NULL;
135 	} else {
136 		visual_server->finish();
137 	}
138 
139 	texture_free_cached_ids();
140 	sky_free_cached_ids();
141 	shader_free_cached_ids();
142 	material_free_cached_ids();
143 	mesh_free_cached_ids();
144 	multimesh_free_cached_ids();
145 	immediate_free_cached_ids();
146 	skeleton_free_cached_ids();
147 	directional_light_free_cached_ids();
148 	omni_light_free_cached_ids();
149 	spot_light_free_cached_ids();
150 	reflection_probe_free_cached_ids();
151 	gi_probe_free_cached_ids();
152 	lightmap_capture_free_cached_ids();
153 	particles_free_cached_ids();
154 	camera_free_cached_ids();
155 	viewport_free_cached_ids();
156 	environment_free_cached_ids();
157 	scenario_free_cached_ids();
158 	instance_free_cached_ids();
159 	canvas_free_cached_ids();
160 	canvas_item_free_cached_ids();
161 	canvas_light_occluder_free_cached_ids();
162 	canvas_occluder_polygon_free_cached_ids();
163 }
164 
set_use_vsync_callback(bool p_enable)165 void VisualServerWrapMT::set_use_vsync_callback(bool p_enable) {
166 
167 	singleton_mt->call_set_use_vsync(p_enable);
168 }
169 
170 VisualServerWrapMT *VisualServerWrapMT::singleton_mt = NULL;
171 
VisualServerWrapMT(VisualServer * p_contained,bool p_create_thread)172 VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread) :
173 		command_queue(p_create_thread) {
174 
175 	singleton_mt = this;
176 	OS::switch_vsync_function = set_use_vsync_callback; //as this goes to another thread, make sure it goes properly
177 
178 	visual_server = p_contained;
179 	create_thread = p_create_thread;
180 	thread = NULL;
181 	draw_pending = 0;
182 	draw_thread_up = false;
183 	alloc_mutex = Mutex::create();
184 	pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
185 
186 	if (!p_create_thread) {
187 		server_thread = Thread::get_caller_id();
188 	} else {
189 		server_thread = 0;
190 	}
191 }
192 
~VisualServerWrapMT()193 VisualServerWrapMT::~VisualServerWrapMT() {
194 
195 	memdelete(visual_server);
196 	memdelete(alloc_mutex);
197 	//finish();
198 }
199