Files
MapLibreParticleLab/src/mbgl/map/environment.cpp
Konstantin Käfer 61fa436193 coalesce buffer deletions and run them in bulk during rendering
On iOS, the main thread does framebuffer operations just before it requests a rerender of the view contents. This means that GL operations on the Map thread will interfere with that and cause a crash. While most GL operations happen throughout rendering (which is synchronized with the main thread), deletion of shared_ptrs release in turn their OpenGL objects. This may happen at any time and isn't synced with rendering.

This patch changes this so instead of deleting all objects immediately, we are collecting all abandoned IDs and delete them in bulk once the main thread has given us permission to use OpenGL calls (i.e. during render()).
2015-03-20 19:47:54 +01:00

188 lines
5.0 KiB
C++

#include <mbgl/map/environment.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/platform/gl.hpp>
#include <uv.h>
#include <atomic>
#include <cassert>
#include <mutex>
#include <unordered_map>
namespace mbgl {
namespace {
class ThreadInfoStore {
private:
struct ThreadInfo {
Environment* env;
ThreadType type;
std::string name;
};
public:
ThreadInfoStore() {
registerThread(nullptr, ThreadType::Main, "Main");
}
~ThreadInfoStore() {
unregisterThread();
assert(threadSet.size() == 0);
}
void registerThread(Environment* env, ThreadType type, const std::string& name) {
std::lock_guard<std::mutex> lock(mtx);
// FIXME: We should never need to overwrite a thread here and we only allow
// this today because on the Static mode, the Map thread and the Main thread
// are same. Replace this with emplace() when this gets fixed.
threadSet[std::this_thread::get_id()] = ThreadInfo{ env, type, name };
}
void unregisterThread() {
std::lock_guard<std::mutex> lock(mtx);
ThreadSet::iterator it = threadSet.find(std::this_thread::get_id());
if (it != threadSet.end()) {
threadSet.erase(it);
}
}
const ThreadInfo& getThreadInfo() const {
static ThreadInfo emptyInfo;
std::lock_guard<std::mutex> lock(mtx);
ThreadSet::const_iterator it = threadSet.find(std::this_thread::get_id());
if (it != threadSet.end()) {
return it->second;
} else {
return emptyInfo;
}
}
private:
typedef std::unordered_map<std::thread::id, ThreadInfo> ThreadSet;
ThreadSet threadSet;
mutable std::mutex mtx;
};
unsigned makeEnvironmentID() {
static std::atomic<unsigned> id(0);
return id++;
}
ThreadInfoStore threadInfoStore;
} // namespace
EnvironmentScope::EnvironmentScope(Environment& env, ThreadType type, const std::string& name)
: id(std::this_thread::get_id()) {
threadInfoStore.registerThread(&env, type, name);
}
EnvironmentScope::~EnvironmentScope() {
assert(id == std::this_thread::get_id());
threadInfoStore.unregisterThread();
}
Environment::Environment(FileSource& fs)
: id(makeEnvironmentID()), fileSource(fs), loop(uv_loop_new()) {
}
Environment::~Environment() {
assert(abandonedVAOs.empty());
assert(abandonedTextures.empty());
assert(abandonedBuffers.empty());
}
Environment& Environment::Get() {
Environment* env = threadInfoStore.getThreadInfo().env;
assert(env);
return *env;
}
bool Environment::inScope() {
return threadInfoStore.getThreadInfo().env;
}
bool Environment::currentlyOn(ThreadType type) {
return static_cast<uint8_t>(threadInfoStore.getThreadInfo().type) & static_cast<uint8_t>(type);
}
std::string Environment::threadName() {
return threadInfoStore.getThreadInfo().name;
}
unsigned Environment::getID() const {
return id;
}
void Environment::requestAsync(const Resource& resource,
std::function<void(const Response&)> callback) {
fileSource.request(resource, *this, std::move(callback));
}
Request* Environment::request(const Resource& resource,
std::function<void(const Response&)> callback) {
assert(currentlyOn(ThreadType::Map));
return fileSource.request(resource, loop, *this, std::move(callback));
}
void Environment::cancelRequest(Request* req) {
assert(currentlyOn(ThreadType::Map));
fileSource.cancel(req);
}
// #############################################################################################
#pragma mark - OpenGL cleanup
void Environment::abandonVAO(uint32_t vao) {
assert(currentlyOn(ThreadType::Map));
abandonedVAOs.emplace_back(vao);
}
void Environment::abandonBuffer(uint32_t buffer) {
assert(currentlyOn(ThreadType::Map));
abandonedBuffers.emplace_back(buffer);
}
void Environment::abandonTexture(uint32_t texture) {
assert(currentlyOn(ThreadType::Map));
abandonedTextures.emplace_back(texture);
}
// Actually remove the objects we marked as abandoned with the above methods.
void Environment::performCleanup() {
assert(currentlyOn(ThreadType::Map));
if (!abandonedVAOs.empty()) {
MBGL_CHECK_ERROR(gl::DeleteVertexArrays(static_cast<GLsizei>(abandonedVAOs.size()),
abandonedVAOs.data()));
abandonedVAOs.clear();
}
if (!abandonedTextures.empty()) {
MBGL_CHECK_ERROR(glDeleteTextures(static_cast<GLsizei>(abandonedTextures.size()),
abandonedTextures.data()));
abandonedTextures.clear();
}
if (!abandonedBuffers.empty()) {
MBGL_CHECK_ERROR(glDeleteBuffers(static_cast<GLsizei>(abandonedBuffers.size()),
abandonedBuffers.data()));
abandonedBuffers.clear();
}
}
// #############################################################################################
void Environment::terminate() {
fileSource.abort(*this);
}
}