diff options
author | Leith Bade <leith@mapbox.com> | 2014-12-21 15:02:47 +1300 |
---|---|---|
committer | Leith Bade <leith@mapbox.com> | 2014-12-21 15:02:47 +1300 |
commit | df19683b6a871350c9f5ef2576d327ef2033d56c (patch) | |
tree | e27e22d7e06310cc45b705b4b5b7342cd9e9ba42 /src | |
parent | 38e2f9831425e7a6dc2d69bd5fa196a5b7cb836f (diff) | |
parent | 2731b582f24063e339c047635c6d7b2316eb8037 (diff) | |
download | qtlocation-mapboxgl-df19683b6a871350c9f5ef2576d327ef2033d56c.tar.gz |
Merge branch 'master' of github.com:mapbox/mapbox-gl-native into android-mason
Conflicts:
Makefile
platform/default/headless_view.cpp
src/mbgl/map/map.cpp
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/map/map.cpp | 39 | ||||
-rw-r--r-- | src/mbgl/shader/shader.cpp | 71 |
2 files changed, 52 insertions, 58 deletions
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 5eed5ad69a..d1e938427c 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -96,6 +96,7 @@ Map::Map(View& view_, FileSource& fileSource_) view(view_), #ifndef NDEBUG mainThread(std::this_thread::get_id()), + mapThread(mainThread), #endif transform(view_), fileSource(fileSource_), @@ -113,7 +114,7 @@ Map::Map(View& view_, FileSource& fileSource_) } Map::~Map() { - if (async) { + if (mode == Mode::Continuous) { stop(); } @@ -137,11 +138,11 @@ uv::worker &Map::getWorker() { void Map::start(bool startPaused) { assert(std::this_thread::get_id() == mainThread); - assert(!async); + assert(mode == Mode::None); // When starting map rendering in another thread, we perform async/continuously // updated rendering. Only in these cases, we attach the async handlers. - async = true; + mode = Mode::Continuous; // Reset the flag. isStopped = false; @@ -212,7 +213,7 @@ void Map::start(bool startPaused) { void Map::stop(std::function<void ()> callback) { assert(std::this_thread::get_id() == mainThread); assert(mainThread != mapThread); - assert(async); + assert(mode == Mode::Continuous); asyncTerminate->send(); @@ -234,12 +235,12 @@ void Map::stop(std::function<void ()> callback) { // already finished executing. thread.join(); - async = false; + mode = Mode::None; } void Map::pause(bool waitForPause) { assert(std::this_thread::get_id() == mainThread); - assert(async); + assert(mode == Mode::Continuous); mutexRun.lock(); pausing = true; mutexRun.unlock(); @@ -257,7 +258,7 @@ void Map::pause(bool waitForPause) { void Map::resume() { assert(std::this_thread::get_id() == mainThread); - assert(async); + assert(mode == Mode::Continuous); mutexRun.lock(); pausing = false; @@ -270,21 +271,22 @@ void Map::run() { COFFEE_TRY() { #endif + if (mode == Mode::None) { #ifndef NDEBUG - if (!async) { mapThread = mainThread; - } #endif + mode = Mode::Static; + } assert(std::this_thread::get_id() == mapThread); - if (async) { + if (mode == Mode::Continuous) { checkForPause(); } setup(); prepare(); - if (async) { + if (mode == Mode::Continuous) { terminating = false; while(!terminating) { uv_run(**loop, UV_RUN_DEFAULT); @@ -299,11 +301,13 @@ void Map::run() { // If the map rendering wasn't started asynchronously, we perform one render // *after* all events have been processed. - if (!async) { + if (mode == Mode::Static) { render(); #ifndef NDEBUG mapThread = std::thread::id(); #endif + mode = Mode::None; + fileSource.clearLoop(); } #ifdef __ANDROID__ } COFFEE_CATCH() { @@ -334,9 +338,12 @@ void Map::checkForPause() { } void Map::rerender() { - // We only send render events if we want to continuously update the map - // (== async rendering). - if (async) { + if (mode == Mode::Static) { + prepare(); + } else if (mode == Mode::Continuous) { + // We only send render events if we want to continuously update the map + // (== async rendering). + assert(asyncRender); asyncRender->send(); } } @@ -373,7 +380,7 @@ void Map::setStyleURL(const std::string &url) { // TODO: Make threadsafe. styleURL = url; - if (async) { + if (mode == Mode::Continuous) { stop(); start(); } diff --git a/src/mbgl/shader/shader.cpp b/src/mbgl/shader/shader.cpp index c43863b52c..62edf1d382 100644 --- a/src/mbgl/shader/shader.cpp +++ b/src/mbgl/shader/shader.cpp @@ -5,8 +5,9 @@ #include <mbgl/platform/platform.hpp> #include <cstring> -#include <cstdlib> #include <cassert> +#include <iostream> +#include <fstream> using namespace mbgl; @@ -25,35 +26,26 @@ Shader::Shader(const char *name_, const GLchar *vertSource, const GLchar *fragSo // Load binary shader if it exists bool skipCompile = false; if (!binaryFileName.empty() && (gl::ProgramBinary != nullptr)) { - FILE *binaryFile = fopen(binaryFileName.c_str(), "rb"); - if (binaryFile != nullptr) { - GLsizei binaryLength; - GLenum binaryFormat; - bool lengthOk = fread(&binaryLength, sizeof(binaryLength), 1, binaryFile) == 1; - bool formatOk = fread(&binaryFormat, sizeof(binaryFormat), 1, binaryFile) == 1; - - if (lengthOk && formatOk && binaryLength > 0) { - std::unique_ptr<char[]> binary = mbgl::util::make_unique<char[]>(binaryLength); - - if (binary != nullptr) { - bool binaryOk = fread(binary.get(), binaryLength, 1, binaryFile) == 1; - - if (binaryOk) { - MBGL_CHECK_ERROR(gl::ProgramBinary(program, binaryFormat, binary.get(), binaryLength)); - - // Check if the binary was valid - GLint status; - MBGL_CHECK_ERROR(glGetProgramiv(program, GL_LINK_STATUS, &status)); - if (status == GL_TRUE) { - skipCompile = true; - } - } - } - } + std::ifstream binaryFile(binaryFileName, std::ios::in | std::ios::binary); + + GLsizei binaryLength; + GLenum binaryFormat; + binaryFile.read(reinterpret_cast<char *>(&binaryLength), sizeof(binaryLength)); + binaryFile.read(reinterpret_cast<char *>(&binaryFormat), sizeof(binaryFormat)); + + std::unique_ptr<char[]> binary = mbgl::util::make_unique<char[]>(binaryLength); + binaryFile.read(binary.get(), binaryLength); - fclose(binaryFile); - binaryFile = nullptr; + MBGL_CHECK_ERROR(gl::ProgramBinary(program, binaryFormat, binary.get(), binaryLength)); + + // Check if the binary was valid + GLint status; + MBGL_CHECK_ERROR(glGetProgramiv(program, GL_LINK_STATUS, &status)); + if (status == GL_TRUE) { + skipCompile = true; } + + binaryFile.close(); } GLuint vertShader = 0; @@ -150,7 +142,7 @@ bool Shader::compileShader(GLuint *shader, GLenum type, const GLchar *source) { *shader = MBGL_CHECK_ERROR(glCreateShader(type)); const GLchar *strings[] = { source }; - const GLsizei lengths[] = { (GLsizei)strlen(source) }; + const GLsizei lengths[] = { (GLsizei)std::strlen(source) }; MBGL_CHECK_ERROR(glShaderSource(*shader, 1, strings, lengths)); MBGL_CHECK_ERROR(glCompileShader(*shader)); @@ -189,19 +181,14 @@ Shader::~Shader() { MBGL_CHECK_ERROR(glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binaryLength)); if (binaryLength > 0) { std::unique_ptr<char[]> binary = mbgl::util::make_unique<char[]>(binaryLength); - if (binary != nullptr) { - MBGL_CHECK_ERROR(gl::GetProgramBinary(program, binaryLength, NULL, &binaryFormat, binary.get())); - - // Write the binary to a file - FILE *binaryFile = fopen(binaryFileName.c_str(), "wb"); - if (binaryFile != nullptr) { - fwrite(&binaryLength, sizeof(binaryLength), 1, binaryFile); - fwrite(&binaryFormat, sizeof(binaryFormat), 1, binaryFile); - fwrite(binary.get(), binaryLength, 1, binaryFile); - fclose(binaryFile); - binaryFile = nullptr; - } - } + MBGL_CHECK_ERROR(gl::GetProgramBinary(program, binaryLength, NULL, &binaryFormat, binary.get())); + + // Write the binary to a file + std::ofstream binaryFile(binaryFileName, std::ios::out | std::ios::trunc | std::ios::binary); + binaryFile.write(reinterpret_cast<char *>(&binaryLength), sizeof(binaryLength)); + binaryFile.write(reinterpret_cast<char *>(&binaryFormat), sizeof(binaryFormat)); + binaryFile.write(binary.get(), binaryLength); + binaryFile.close(); } } |