hide atomic flags as implementation detail

This commit is contained in:
Konstantin Käfer
2014-04-30 12:29:50 +02:00
parent fcfd506a3c
commit f683cbf02a
4 changed files with 29 additions and 18 deletions

View File

@@ -163,11 +163,10 @@ int MapView::run() {
map.start();
while (!glfwWindowShouldClose(window)) {
if (map.swapped.test_and_set() == false) {
if (map.needsSwap()) {
glfwSwapBuffers(window);
map.rendered.clear();
map.swapped();
fps();
map.rerender();
}
glfwWaitEvents();

View File

@@ -39,6 +39,10 @@ public:
// Forces a map update: always triggers a rerender.
void update();
// Controls buffer swapping.
bool needsSwap();
void swapped();
// Size
void resize(uint16_t width, uint16_t height, float ratio = 1);
void resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height);
@@ -105,18 +109,18 @@ private:
// Unconditionally performs a render with the current map state.
void render();
public:
private:
// If cleared, the next time the render thread attempts to render the map, it will *actually*
// render the map.
std::atomic_flag clean = ATOMIC_FLAG_INIT;
std::atomic_flag is_clean = ATOMIC_FLAG_INIT;
// If this flag is cleared, the current back buffer is ready for being swapped with the front
// buffer (i.e. it has rendered data).
std::atomic_flag swapped = ATOMIC_FLAG_INIT;
std::atomic_flag is_swapped = ATOMIC_FLAG_INIT;
// This is cleared once the current front buffer has been presented and the back buffer is
// ready for rendering.
std::atomic_flag rendered = ATOMIC_FLAG_INIT;
std::atomic_flag is_rendered = ATOMIC_FLAG_INIT;
private:
View &view;

View File

@@ -153,11 +153,10 @@ MBXViewController *viewController = nullptr;
- (void)swap
{
if (map->swapped.test_and_set() == false)
if (map->needsSwap())
{
[(GLKView *)self.view display];
map->rendered.clear();
map->rerender();
map->swapped();
}
}

View File

@@ -21,9 +21,9 @@ Map::Map(View& view)
loop(uv_loop_new()) {
// Make sure that we're doing an initial drawing in all cases.
clean.clear();
rendered.clear();
swapped.test_and_set();
is_clean.clear();
is_rendered.clear();
is_swapped.test_and_set();
}
Map::~Map() {
@@ -91,7 +91,16 @@ void Map::rerender() {
}
void Map::update() {
clean.clear();
is_clean.clear();
rerender();
}
bool Map::needsSwap() {
return is_swapped.test_and_set() == false;
}
void Map::swapped() {
is_rendered.clear();
rerender();
}
@@ -100,15 +109,15 @@ void Map::render(uv_async_t *async) {
map->prepare();
if (map->rendered.test_and_set() == false) {
if (map->clean.test_and_set() == false) {
if (map->is_rendered.test_and_set() == false) {
if (map->is_clean.test_and_set() == false) {
map->render();
map->swapped.clear();
map->is_swapped.clear();
map->view.swap();
} else {
// We set the rendered flag in the test above, so we have to reset it
// now that we're not actually rendering because the map is clean.
map->rendered.clear();
map->is_rendered.clear();
}
}
}