@@ -23,7 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
2424using namespace ruis ::render;
2525
26- const ruis::render::context* ruis::render::context::cur_context = nullptr ;
26+ std::vector< const ruis::render::context*> ruis::render::context::cur_context_stack ;
2727
2828context::context (
2929 utki::shared_ref<ruis::render::native_window> native_window, //
@@ -32,18 +32,30 @@ context::context(
3232 native_window(std::move(native_window)),
3333 initial_matrix(std::move(params.initial_matrix))
3434{
35- if (!cur_context) {
35+ // TODO: document this behaviour in doxygen that first created context becomes bound by default
36+ if (cur_context_stack.empty ()) {
3637 // this created context is the only one existing context at the moment, bind it just to have some context always bound
3738 this ->native_window .get ().bind_rendering_context ();
38- cur_context = this ;
39+ cur_context_stack. push_back ( this ) ;
3940 }
4041}
4142
4243context::~context ()
4344{
4445 // if the context is bound during destruction then it is the last context
4546 if (this ->is_current ()) {
46- cur_context = nullptr ;
47+ utki::assert (cur_context_stack.back () == this , SL);
48+ cur_context_stack.pop_back ();
49+ if (!cur_context_stack.empty ()) {
50+ // bind the previous context
51+ cur_context_stack.back ()->native_window .get ().bind_rendering_context ();
52+ }
53+ }else {
54+ auto i = std::find (cur_context_stack.begin (), //
55+ cur_context_stack.end (), this );
56+ if (i != cur_context_stack.end ()){
57+ cur_context_stack.erase (i);
58+ }
4759 }
4860}
4961
@@ -57,18 +69,16 @@ void context::apply(std::function<void()> func)
5769 return ;
5870 }
5971
60- auto old_cc = cur_context;
6172 utki::scope_exit restore_old_cc_scope_exit ([&]() {
62- if (!old_cc) {
63- // There was no current context, leave current one bound.
64- return ;
73+ utki::assert (this ->is_current (), SL);
74+ cur_context_stack.pop_back ();
75+ if (!cur_context_stack.empty ()) {
76+ cur_context_stack.back ()->native_window .get ().bind_rendering_context ();
6577 }
66- old_cc->native_window .get ().bind_rendering_context ();
67- cur_context = old_cc;
6878 });
6979
80+ cur_context_stack.push_back (this );
7081 this ->native_window .get ().bind_rendering_context ();
71- cur_context = this ;
7282
7383 utki::assert (this ->is_current (), SL);
7484
0 commit comments