1 /*
2  *  BSD 3-Clause License
3  *  
4  *  Copyright (c) 2016, Mango-Engine Team
5  *  All rights reserved.
6  *  
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions are met:
9  *  
10  *  * Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *  
13  *  * Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *  
17  *  * Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from
19  *    this software without specific prior written permission.
20  *  
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 module mango_engine.graphics.renderer;
33 
34 import mango_engine.mango;
35 import mango_engine.util;
36 import mango_engine.graphics.scene;
37 
38 import std.concurrency;
39 import std.datetime;
40 
41 alias RendererOperation = void delegate() @system;
42 
43 struct RendererOperationMessage {
44     shared RendererOperation operation;
45 }
46 
47 struct SwitchSceneMessage {
48     shared Scene scene;
49 }
50 
51 /// Backend interface class: represents a Renderer.
52 abstract class Renderer {
53     private __gshared Tid threadTid;
54 
55     private shared Scene _scene;
56 
57     private shared bool _running;
58 
59     @property Scene scene() @trusted nothrow { return cast(Scene) _scene;}
60     @property bool running() @safe nothrow { return _running; }
61     
62     protected this() @trusted {
63         _running = true;
64         threadTid = spawn(&startRendererThread, cast(shared) this);
65     }
66 
67     static Renderer build() @safe {
68         mixin(InterfaceClassFactory!("renderer", "Renderer", ""));
69     }
70 
71     void switchScene(Scene scene) @trusted {
72         prioritySend(threadTid, SwitchSceneMessage(cast(shared) scene));
73     }
74 
75     void submitOperation(RendererOperation operation) @trusted {
76         send(threadTid, RendererOperationMessage(operation));
77     }
78 
79     private void doRun() @system {
80         while(_running) {
81             uint counter = 0;
82             try {
83                 while(processOperation() != false && counter < 50) {
84                     counter++;
85                 }
86             } catch(OwnerTerminated e) {
87                 GLOBAL_LOGGER.logError("Renderer thread crashed (Main Thread terminated)!");
88             } catch(Exception e) {
89                 GLOBAL_LOGGER.logError("Error while processing operation!");
90                 GLOBAL_LOGGER.logException("Exception in Renderer thread", e);
91                 _running = false;
92                 return;
93             }
94 
95             render();
96         }
97     }
98 
99     private bool processOperation() @system {
100         return receiveTimeout(0.msecs,
101             (SwitchSceneMessage m) {
102                 this._scene = m.scene;
103             },
104             (RendererOperationMessage m) {
105                 m.operation();
106             }
107         );
108     }
109 
110     void stop() @safe nothrow {
111         _running = false;
112     }
113     
114     abstract void render() @system;
115 }
116 
117 private void startRendererThread(shared Renderer renderer) @system {
118     import core.thread : Thread;
119 
120     Thread.getThis().name = "Renderer";
121     (cast(Renderer) renderer).doRun();
122 }