Skip to content

Commit fc35102

Browse files
committed
stuff
1 parent 6cb3977 commit fc35102

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/ruis/render/context.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2323

2424
using 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

2828
context::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

4243
context::~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

src/ruis/render/context.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

2222
#pragma once
2323

24+
#include <vector>
25+
2426
#include <rasterimage/image_variant.hpp>
2527
#include <utki/shared.hpp>
2628

@@ -39,7 +41,8 @@ class context : public std::enable_shared_from_this<context>
3941
{
4042
friend class frame_buffer;
4143

42-
static const context* cur_context;
44+
// Context destruction is rare, so removing from the middle of stack is rare, ok to use std::vector.
45+
static std::vector<const context*> cur_context_stack;
4346

4447
protected:
4548
utki::shared_ref<context> get_shared_ref()
@@ -84,7 +87,9 @@ class context : public std::enable_shared_from_this<context>
8487

8588
bool is_current() const noexcept
8689
{
87-
return cur_context == this;
90+
// if at least one context exists then the cur_context_stack is not empty
91+
utki::assert(!cur_context_stack.empty(), SL);
92+
return cur_context_stack.back() == this;
8893
}
8994

9095
/**

0 commit comments

Comments
 (0)