From 9082a83d6ac2b990db04752a991d97f8d18d4e34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 16 Dec 2014 11:42:58 +0100 Subject: don't assing nullptr to std::string --- platform/default/headless_view.cpp | 46 +++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 21 deletions(-) (limited to 'platform') diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp index b0fa60fc12..d85910875f 100644 --- a/platform/default/headless_view.cpp +++ b/platform/default/headless_view.cpp @@ -52,32 +52,36 @@ HeadlessView::HeadlessView(std::shared_ptr display) void HeadlessView::loadExtensions() { make_active(); - const std::string extensions = (char *)MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS)); + const char *extension_ptr = (char *)MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS)); + + if (extension_ptr) { + const std::string extensions = extension_ptr; #ifdef MBGL_USE_CGL - if (extensions.find("GL_APPLE_vertex_array_object") != std::string::npos) { - gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)CGLGetProcAddress("glBindVertexArrayAPPLE"); - gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)CGLGetProcAddress("glDeleteVertexArraysAPPLE"); - gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)CGLGetProcAddress("glGenVertexArraysAPPLE"); - gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)CGLGetProcAddress("glIsVertexArrayAPPLE"); - assert(gl::BindVertexArray != nullptr); - assert(gl::DeleteVertexArrays != nullptr); - assert(gl::GenVertexArrays != nullptr); - assert(gl::IsVertexArray != nullptr); - } + if (extensions.find("GL_APPLE_vertex_array_object") != std::string::npos) { + gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)CGLGetProcAddress("glBindVertexArrayAPPLE"); + gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)CGLGetProcAddress("glDeleteVertexArraysAPPLE"); + gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)CGLGetProcAddress("glGenVertexArraysAPPLE"); + gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)CGLGetProcAddress("glIsVertexArrayAPPLE"); + assert(gl::BindVertexArray != nullptr); + assert(gl::DeleteVertexArrays != nullptr); + assert(gl::GenVertexArrays != nullptr); + assert(gl::IsVertexArray != nullptr); + } #endif #ifdef MBGL_USE_GLX - if (extensions.find("GL_ARB_vertex_array_object") != std::string::npos) { - gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glBindVertexArray"); - gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glDeleteVertexArrays"); - gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glGenVertexArrays"); - gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glIsVertexArray"); - assert(gl::BindVertexArray != nullptr); - assert(gl::DeleteVertexArrays != nullptr); - assert(gl::GenVertexArrays != nullptr); - assert(gl::IsVertexArray != nullptr); - } + if (extensions.find("GL_ARB_vertex_array_object") != std::string::npos) { + gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glBindVertexArray"); + gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glDeleteVertexArrays"); + gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glGenVertexArrays"); + gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glIsVertexArray"); + assert(gl::BindVertexArray != nullptr); + assert(gl::DeleteVertexArrays != nullptr); + assert(gl::GenVertexArrays != nullptr); + assert(gl::IsVertexArray != nullptr); + } #endif + } make_inactive(); } -- cgit v1.2.1 From 40a5bcd7d39c9e83b59fe4149c56cc09793fb53b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 16 Dec 2014 11:10:41 +0000 Subject: never create core profile contexts; their API is subtly different (e.g. glGetString(GL_EXTENSIONS) doesn't work, you'd have to use glGetStringi(GL_EXTENSIONS, ...);) --- platform/default/headless_view.cpp | 74 ++------------------------------------ 1 file changed, 2 insertions(+), 72 deletions(-) (limited to 'platform') diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp index d85910875f..4053af226d 100644 --- a/platform/default/headless_view.cpp +++ b/platform/default/headless_view.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -9,12 +10,6 @@ #include #include -#if MBGL_USE_GLX -#ifdef GLX_ARB_create_context -static PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = nullptr; -#endif -#endif - #ifdef MBGL_USE_CGL #include @@ -86,59 +81,6 @@ void HeadlessView::loadExtensions() { make_inactive(); } - -#if MBGL_USE_GLX -#ifdef GLX_ARB_create_context - -// These are all of the OpenGL Core profile version that we know about. -struct core_profile_version { int major, minor; }; -static const core_profile_version coreProfileVersions[] = { - {4, 5}, - {4, 4}, - {4, 3}, - {4, 2}, - {4, 1}, - {4, 0}, - {3, 3}, - {3, 2}, - {3, 1}, - {3, 0}, - {0, 0}, -}; - -GLXContext createCoreProfile(Display *dpy, GLXFBConfig fbconfig) { - static bool contextCreationFailed = false; - GLXContext ctx = 0; - - // Set the Error Handler to avoid crashing the program when the context creation fails. - // It is expected that some context creation attempts fail, e.g. because the OpenGL - // implementation does not support the version we're requesting. - int (*previousErrorHandler)(Display *, XErrorEvent *) = XSetErrorHandler([](Display *, XErrorEvent *) { - contextCreationFailed = true; - return 0; - }); - - // Try to create core profiles from the highest known version on down. - for (int i = 0; !ctx && coreProfileVersions[i].major; i++) { - contextCreationFailed = false; - const int contextFlags[] = { - GLX_CONTEXT_MAJOR_VERSION_ARB, coreProfileVersions[i].major, - GLX_CONTEXT_MINOR_VERSION_ARB, coreProfileVersions[i].minor, - 0 - }; - ctx = glXCreateContextAttribsARB(dpy, fbconfig, 0, True, contextFlags); - if (contextCreationFailed) { - ctx = 0; - } - } - - // Restore the old error handler. - XSetErrorHandler(previousErrorHandler); - return ctx; -} -#endif -#endif - void HeadlessView::createContext() { #if MBGL_USE_CGL CGLError error = CGLCreateContext(display_->pixelFormat, NULL, &glContext); @@ -153,27 +95,15 @@ void HeadlessView::createContext() { #endif #if MBGL_USE_GLX -#ifdef GLX_ARB_create_context - if (glXCreateContextAttribsARB == nullptr) { - glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddressARB((const GLubyte *)"glXCreateContextAttribsARB"); - } -#endif - xDisplay = display_->xDisplay; fbConfigs = display_->fbConfigs; -#ifdef GLX_ARB_create_context - if (glXCreateContextAttribsARB) { - // Try to create a core profile context. - glContext = createCoreProfile(xDisplay, fbConfigs[0]); - } -#endif - if (!glContext) { // Try to create a legacy context glContext = glXCreateNewContext(xDisplay, fbConfigs[0], GLX_RGBA_TYPE, 0, True); if (glContext) { if (!glXIsDirect(xDisplay, glContext)) { + mbgl::Log::Error(mbgl::Event::OpenGL, "Failed to create direct OpenGL Legacy context"); glXDestroyContext(xDisplay, glContext); glContext = 0; } -- cgit v1.2.1 From 936e09651424f111ac2ac64e2e60e2d13b082ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 17 Dec 2014 12:14:32 +0100 Subject: Make file request overridable by platform implementations refs #579: Android can now rename AssetRequest to FileRequest and implement it differently --- platform/default/file_request_libuv.cpp | 209 ++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 platform/default/file_request_libuv.cpp (limited to 'platform') diff --git a/platform/default/file_request_libuv.cpp b/platform/default/file_request_libuv.cpp new file mode 100644 index 0000000000..f874bed8d2 --- /dev/null +++ b/platform/default/file_request_libuv.cpp @@ -0,0 +1,209 @@ +#include +#include +#include + +#include + +#include + +namespace mbgl { + +struct FileRequestBaton { + FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop); + ~FileRequestBaton(); + + void cancel(); + static void file_opened(uv_fs_t *req); + static void file_stated(uv_fs_t *req); + static void file_read(uv_fs_t *req); + static void file_closed(uv_fs_t *req); + static void notify_error(uv_fs_t *req); + static void cleanup(uv_fs_t *req); + + const std::thread::id thread_id; + FileRequest *request = nullptr; + uv_fs_t req; + uv_file fd = -1; + bool canceled = false; + std::string body; + uv_buf_t buffer; +}; + +FileRequestBaton::FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop) + : thread_id(std::this_thread::get_id()), request(request_) { + req.data = this; + uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, file_opened); +} + +FileRequestBaton::~FileRequestBaton() { +} + +void FileRequestBaton::cancel() { + canceled = true; + + // uv_cancel fails frequently when the request has already been started. + // In that case, we have to let it complete and check the canceled bool + // instead. + uv_cancel((uv_req_t *)&req); +} + +void FileRequestBaton::notify_error(uv_fs_t *req) { + FileRequestBaton *ptr = (FileRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) { + ptr->request->response = util::make_unique(); + ptr->request->response->code = req->result == UV_ENOENT ? 404 : 500; +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + ptr->request->response->message = uv_strerror(uv_last_error(req->loop)); +#else + ptr->request->response->message = uv_strerror(int(req->result)); +#endif + ptr->request->notify(); + } +} + +void FileRequestBaton::file_opened(uv_fs_t *req) { + FileRequestBaton *ptr = (FileRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (req->result < 0) { + // Opening failed or was canceled. There isn't much left we can do. + notify_error(req); + cleanup(req); + } else { + const uv_file fd = uv_file(req->result); + + // We're going to reuse this handle, so we need to cleanup first. + uv_fs_req_cleanup(req); + + if (ptr->canceled || !ptr->request) { + // Either the FileRequest object has been destructed, or the + // request was canceled. + uv_fs_close(req->loop, req, fd, file_closed); + } else { + ptr->fd = fd; + uv_fs_fstat(req->loop, req, fd, file_stated); + } + } +} + +void FileRequestBaton::file_stated(uv_fs_t *req) { + FileRequestBaton *ptr = (FileRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (req->result != 0 || ptr->canceled || !ptr->request) { + // Stating failed or was canceled. We already have an open file handle + // though, which we'll have to close. + notify_error(req); + + uv_fs_req_cleanup(req); + uv_fs_close(req->loop, req, ptr->fd, file_closed); + } else { +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + const uv_statbuf_t *stat = static_cast(req->ptr); +#else + const uv_stat_t *stat = static_cast(req->ptr); +#endif + if (stat->st_size > std::numeric_limits::max()) { + // File is too large for us to open this way because uv_buf's only support unsigned + // ints as maximum size. + if (ptr->request) { + ptr->request->response = util::make_unique(); + ptr->request->response->code = UV_EFBIG; +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + ptr->request->response->message = uv_strerror(uv_err_t {UV_EFBIG, 0}); +#else + ptr->request->response->message = uv_strerror(UV_EFBIG); +#endif + ptr->request->notify(); + } + + uv_fs_req_cleanup(req); + uv_fs_close(req->loop, req, ptr->fd, file_closed); + } else { + const unsigned int size = (unsigned int)(stat->st_size); + ptr->body.resize(size); + ptr->buffer = uv_buf_init(const_cast(ptr->body.data()), size); + uv_fs_req_cleanup(req); +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + uv_fs_read(req->loop, req, ptr->fd, ptr->buffer.base, ptr->buffer.len, -1, file_read); +#else + uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, file_read); +#endif + } + } +} + +void FileRequestBaton::file_read(uv_fs_t *req) { + FileRequestBaton *ptr = (FileRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (req->result < 0 || ptr->canceled || !ptr->request) { + // Stating failed or was canceled. We already have an open file handle + // though, which we'll have to close. + notify_error(req); + } else { + // File was successfully read. + if (ptr->request) { + ptr->request->response = util::make_unique(); + ptr->request->response->code = 200; + ptr->request->response->data = std::move(ptr->body); + ptr->request->notify(); + } + } + + uv_fs_req_cleanup(req); + uv_fs_close(req->loop, req, ptr->fd, file_closed); +} + +void FileRequestBaton::file_closed(uv_fs_t *req) { + assert(((FileRequestBaton *)req->data)->thread_id == std::this_thread::get_id()); + + if (req->result < 0) { + // Closing the file failed. But there isn't anything we can do. + } + + cleanup(req); +} + +void FileRequestBaton::cleanup(uv_fs_t *req) { + FileRequestBaton *ptr = (FileRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (ptr->request) { + ptr->request->ptr = nullptr; + } + + uv_fs_req_cleanup(req); + delete ptr; +} + + +FileRequest::FileRequest(const std::string &path_, uv_loop_t *loop) + : BaseRequest(path_), ptr(new FileRequestBaton(this, path, loop)) { +} + +void FileRequest::cancel() { + assert(thread_id == std::this_thread::get_id()); + + if (ptr) { + ptr->cancel(); + + // When deleting a FileRequest object with a uv_fs_* call is in progress, we are making sure + // that the callback doesn't accidentally reference this object again. + ptr->request = nullptr; + ptr = nullptr; + } + + notify(); +} + +FileRequest::~FileRequest() { + assert(thread_id == std::this_thread::get_id()); + cancel(); + + // Note: The FileRequestBaton object is deleted in FileRequestBaton::cleanup(). +} + +} -- cgit v1.2.1 From b5e9998b15d8a51bdca9cdb2d0981f69629f532a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 17 Dec 2014 14:59:05 +0100 Subject: change file:// to asset:// to indicate that they aren't arbitrary files refs #579 --- platform/darwin/application_root.mm | 18 +++ platform/default/application_root.cpp | 14 ++ platform/default/asset_request_libuv.cpp | 222 +++++++++++++++++++++++++++++++ platform/default/file_request_libuv.cpp | 209 ----------------------------- 4 files changed, 254 insertions(+), 209 deletions(-) create mode 100644 platform/darwin/application_root.mm create mode 100644 platform/default/application_root.cpp create mode 100644 platform/default/asset_request_libuv.cpp delete mode 100644 platform/default/file_request_libuv.cpp (limited to 'platform') diff --git a/platform/darwin/application_root.mm b/platform/darwin/application_root.mm new file mode 100644 index 0000000000..19b872c54d --- /dev/null +++ b/platform/darwin/application_root.mm @@ -0,0 +1,18 @@ +#import + +#include + +namespace mbgl { +namespace platform { + +// Returns the path to the default shader cache on this system. +std::string applicationRoot() { + static const std::string root = []() -> std::string { + NSString *path = [[[NSBundle mainBundle] resourceURL] path]; + return {[path cStringUsingEncoding : NSUTF8StringEncoding], + [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding]}; + }(); + return root; +} +} +} diff --git a/platform/default/application_root.cpp b/platform/default/application_root.cpp new file mode 100644 index 0000000000..e6f73e0211 --- /dev/null +++ b/platform/default/application_root.cpp @@ -0,0 +1,14 @@ +#include + +#include + +namespace mbgl { +namespace platform { + +// Returns the path to the default cache database on this system. +std::string applicationRoot() { + return uv::cwd(); +} + +} +} diff --git a/platform/default/asset_request_libuv.cpp b/platform/default/asset_request_libuv.cpp new file mode 100644 index 0000000000..202e39967e --- /dev/null +++ b/platform/default/asset_request_libuv.cpp @@ -0,0 +1,222 @@ +#include +#include +#include +#include + +#include + +#include + +namespace mbgl { + +struct AssetRequestBaton { + AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop); + ~AssetRequestBaton(); + + void cancel(); + static void file_opened(uv_fs_t *req); + static void file_stated(uv_fs_t *req); + static void file_read(uv_fs_t *req); + static void file_closed(uv_fs_t *req); + static void notify_error(uv_fs_t *req); + static void cleanup(uv_fs_t *req); + + const std::thread::id thread_id; + AssetRequest *request = nullptr; + uv_fs_t req; + uv_file fd = -1; + bool canceled = false; + std::string body; + uv_buf_t buffer; +}; + +AssetRequestBaton::AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop) + : thread_id(std::this_thread::get_id()), request(request_) { + req.data = this; + uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, file_opened); +} + +AssetRequestBaton::~AssetRequestBaton() { +} + +void AssetRequestBaton::cancel() { + canceled = true; + + // uv_cancel fails frequently when the request has already been started. + // In that case, we have to let it complete and check the canceled bool + // instead. + uv_cancel((uv_req_t *)&req); +} + +void AssetRequestBaton::notify_error(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) { + ptr->request->response = util::make_unique(); + ptr->request->response->code = req->result == UV_ENOENT ? 404 : 500; +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + ptr->request->response->message = uv_strerror(uv_last_error(req->loop)); +#else + ptr->request->response->message = uv_strerror(int(req->result)); +#endif + ptr->request->notify(); + } +} + +void AssetRequestBaton::file_opened(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (req->result < 0) { + // Opening failed or was canceled. There isn't much left we can do. + notify_error(req); + cleanup(req); + } else { + const uv_file fd = uv_file(req->result); + + // We're going to reuse this handle, so we need to cleanup first. + uv_fs_req_cleanup(req); + + if (ptr->canceled || !ptr->request) { + // Either the AssetRequest object has been destructed, or the + // request was canceled. + uv_fs_close(req->loop, req, fd, file_closed); + } else { + ptr->fd = fd; + uv_fs_fstat(req->loop, req, fd, file_stated); + } + } +} + +void AssetRequestBaton::file_stated(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (req->result != 0 || ptr->canceled || !ptr->request) { + // Stating failed or was canceled. We already have an open file handle + // though, which we'll have to close. + notify_error(req); + + uv_fs_req_cleanup(req); + uv_fs_close(req->loop, req, ptr->fd, file_closed); + } else { +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + const uv_statbuf_t *stat = static_cast(req->ptr); +#else + const uv_stat_t *stat = static_cast(req->ptr); +#endif + if (stat->st_size > std::numeric_limits::max()) { + // File is too large for us to open this way because uv_buf's only support unsigned + // ints as maximum size. + if (ptr->request) { + ptr->request->response = util::make_unique(); + ptr->request->response->code = UV_EFBIG; +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + ptr->request->response->message = uv_strerror(uv_err_t {UV_EFBIG, 0}); +#else + ptr->request->response->message = uv_strerror(UV_EFBIG); +#endif + ptr->request->notify(); + } + + uv_fs_req_cleanup(req); + uv_fs_close(req->loop, req, ptr->fd, file_closed); + } else { + const unsigned int size = (unsigned int)(stat->st_size); + ptr->body.resize(size); + ptr->buffer = uv_buf_init(const_cast(ptr->body.data()), size); + uv_fs_req_cleanup(req); +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + uv_fs_read(req->loop, req, ptr->fd, ptr->buffer.base, ptr->buffer.len, -1, file_read); +#else + uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, file_read); +#endif + } + } +} + +void AssetRequestBaton::file_read(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (req->result < 0 || ptr->canceled || !ptr->request) { + // Stating failed or was canceled. We already have an open file handle + // though, which we'll have to close. + notify_error(req); + } else { + // File was successfully read. + if (ptr->request) { + ptr->request->response = util::make_unique(); + ptr->request->response->code = 200; + ptr->request->response->data = std::move(ptr->body); + ptr->request->notify(); + } + } + + uv_fs_req_cleanup(req); + uv_fs_close(req->loop, req, ptr->fd, file_closed); +} + +void AssetRequestBaton::file_closed(uv_fs_t *req) { + assert(((AssetRequestBaton *)req->data)->thread_id == std::this_thread::get_id()); + + if (req->result < 0) { + // Closing the file failed. But there isn't anything we can do. + } + + cleanup(req); +} + +void AssetRequestBaton::cleanup(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; + assert(ptr->thread_id == std::this_thread::get_id()); + + if (ptr->request) { + ptr->request->ptr = nullptr; + } + + uv_fs_req_cleanup(req); + delete ptr; +} + + +AssetRequest::AssetRequest(const std::string &path_, uv_loop_t *loop) + : BaseRequest(path_) { + if (!path.empty() && path[0] == '/') { + // This is an absolute path. We don't allow this. Note that this is not a way to absolutely + // prevent access to resources outside the application bundle; e.g. there could be symlinks + // in the application bundle that link to outside. We don't care about these. + response = util::make_unique(); + response->code = 403; + response->message = "Path is outside the application bundle"; + notify(); + } else { + // Note: The AssetRequestBaton object is deleted in AssetRequestBaton::cleanup(). + ptr = new AssetRequestBaton(this, platform::applicationRoot() + "/" + path, loop); + } +} + +void AssetRequest::cancel() { + assert(thread_id == std::this_thread::get_id()); + + if (ptr) { + ptr->cancel(); + + // When deleting a AssetRequest object with a uv_fs_* call is in progress, we are making sure + // that the callback doesn't accidentally reference this object again. + ptr->request = nullptr; + ptr = nullptr; + } + + notify(); +} + +AssetRequest::~AssetRequest() { + assert(thread_id == std::this_thread::get_id()); + cancel(); + + // Note: The AssetRequestBaton object is deleted in AssetRequestBaton::cleanup(). +} + +} diff --git a/platform/default/file_request_libuv.cpp b/platform/default/file_request_libuv.cpp deleted file mode 100644 index f874bed8d2..0000000000 --- a/platform/default/file_request_libuv.cpp +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include - -#include - -#include - -namespace mbgl { - -struct FileRequestBaton { - FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop); - ~FileRequestBaton(); - - void cancel(); - static void file_opened(uv_fs_t *req); - static void file_stated(uv_fs_t *req); - static void file_read(uv_fs_t *req); - static void file_closed(uv_fs_t *req); - static void notify_error(uv_fs_t *req); - static void cleanup(uv_fs_t *req); - - const std::thread::id thread_id; - FileRequest *request = nullptr; - uv_fs_t req; - uv_file fd = -1; - bool canceled = false; - std::string body; - uv_buf_t buffer; -}; - -FileRequestBaton::FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop) - : thread_id(std::this_thread::get_id()), request(request_) { - req.data = this; - uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, file_opened); -} - -FileRequestBaton::~FileRequestBaton() { -} - -void FileRequestBaton::cancel() { - canceled = true; - - // uv_cancel fails frequently when the request has already been started. - // In that case, we have to let it complete and check the canceled bool - // instead. - uv_cancel((uv_req_t *)&req); -} - -void FileRequestBaton::notify_error(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; - assert(ptr->thread_id == std::this_thread::get_id()); - - if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) { - ptr->request->response = util::make_unique(); - ptr->request->response->code = req->result == UV_ENOENT ? 404 : 500; -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - ptr->request->response->message = uv_strerror(uv_last_error(req->loop)); -#else - ptr->request->response->message = uv_strerror(int(req->result)); -#endif - ptr->request->notify(); - } -} - -void FileRequestBaton::file_opened(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; - assert(ptr->thread_id == std::this_thread::get_id()); - - if (req->result < 0) { - // Opening failed or was canceled. There isn't much left we can do. - notify_error(req); - cleanup(req); - } else { - const uv_file fd = uv_file(req->result); - - // We're going to reuse this handle, so we need to cleanup first. - uv_fs_req_cleanup(req); - - if (ptr->canceled || !ptr->request) { - // Either the FileRequest object has been destructed, or the - // request was canceled. - uv_fs_close(req->loop, req, fd, file_closed); - } else { - ptr->fd = fd; - uv_fs_fstat(req->loop, req, fd, file_stated); - } - } -} - -void FileRequestBaton::file_stated(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; - assert(ptr->thread_id == std::this_thread::get_id()); - - if (req->result != 0 || ptr->canceled || !ptr->request) { - // Stating failed or was canceled. We already have an open file handle - // though, which we'll have to close. - notify_error(req); - - uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, file_closed); - } else { -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - const uv_statbuf_t *stat = static_cast(req->ptr); -#else - const uv_stat_t *stat = static_cast(req->ptr); -#endif - if (stat->st_size > std::numeric_limits::max()) { - // File is too large for us to open this way because uv_buf's only support unsigned - // ints as maximum size. - if (ptr->request) { - ptr->request->response = util::make_unique(); - ptr->request->response->code = UV_EFBIG; -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - ptr->request->response->message = uv_strerror(uv_err_t {UV_EFBIG, 0}); -#else - ptr->request->response->message = uv_strerror(UV_EFBIG); -#endif - ptr->request->notify(); - } - - uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, file_closed); - } else { - const unsigned int size = (unsigned int)(stat->st_size); - ptr->body.resize(size); - ptr->buffer = uv_buf_init(const_cast(ptr->body.data()), size); - uv_fs_req_cleanup(req); -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - uv_fs_read(req->loop, req, ptr->fd, ptr->buffer.base, ptr->buffer.len, -1, file_read); -#else - uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, file_read); -#endif - } - } -} - -void FileRequestBaton::file_read(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; - assert(ptr->thread_id == std::this_thread::get_id()); - - if (req->result < 0 || ptr->canceled || !ptr->request) { - // Stating failed or was canceled. We already have an open file handle - // though, which we'll have to close. - notify_error(req); - } else { - // File was successfully read. - if (ptr->request) { - ptr->request->response = util::make_unique(); - ptr->request->response->code = 200; - ptr->request->response->data = std::move(ptr->body); - ptr->request->notify(); - } - } - - uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, file_closed); -} - -void FileRequestBaton::file_closed(uv_fs_t *req) { - assert(((FileRequestBaton *)req->data)->thread_id == std::this_thread::get_id()); - - if (req->result < 0) { - // Closing the file failed. But there isn't anything we can do. - } - - cleanup(req); -} - -void FileRequestBaton::cleanup(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; - assert(ptr->thread_id == std::this_thread::get_id()); - - if (ptr->request) { - ptr->request->ptr = nullptr; - } - - uv_fs_req_cleanup(req); - delete ptr; -} - - -FileRequest::FileRequest(const std::string &path_, uv_loop_t *loop) - : BaseRequest(path_), ptr(new FileRequestBaton(this, path, loop)) { -} - -void FileRequest::cancel() { - assert(thread_id == std::this_thread::get_id()); - - if (ptr) { - ptr->cancel(); - - // When deleting a FileRequest object with a uv_fs_* call is in progress, we are making sure - // that the callback doesn't accidentally reference this object again. - ptr->request = nullptr; - ptr = nullptr; - } - - notify(); -} - -FileRequest::~FileRequest() { - assert(thread_id == std::this_thread::get_id()); - cancel(); - - // Note: The FileRequestBaton object is deleted in FileRequestBaton::cleanup(). -} - -} -- cgit v1.2.1 From abcf978174437746c105cbd3e27c58ac55498cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 17 Dec 2014 15:01:15 +0100 Subject: change comment and cache value --- platform/default/application_root.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'platform') diff --git a/platform/default/application_root.cpp b/platform/default/application_root.cpp index e6f73e0211..f25a44d46b 100644 --- a/platform/default/application_root.cpp +++ b/platform/default/application_root.cpp @@ -5,9 +5,10 @@ namespace mbgl { namespace platform { -// Returns the path to the default cache database on this system. +// Returns the path the application root. std::string applicationRoot() { - return uv::cwd(); + static const std::string root = uv::cwd(); + return root; } } -- cgit v1.2.1 From 15b47197a79c05ca6fe4f604a7803ecbcb7a7e4d Mon Sep 17 00:00:00 2001 From: Leith Bade Date: Sat, 20 Dec 2014 14:15:33 +1300 Subject: Use iostream for saving GL program binary. Also check for GL_NUM_PROGRAM_BINARY_FORMATS > 0. --- platform/default/glfw_view.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'platform') diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index e33f8eed15..21cd95c8bf 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -152,12 +152,16 @@ void GLFWView::initialize(mbgl::Map *map_) { } if (extensions.find("GL_ARB_get_program_binary") != std::string::npos) { - gl::GetProgramBinary = (gl::PFNGLGETPROGRAMBINARYPROC)glfwGetProcAddress("glGetProgramBinary"); - gl::ProgramBinary = (gl::PFNGLPROGRAMBINARYPROC)glfwGetProcAddress("glProgramBinary"); - gl::ProgramParameteri = (gl::PFNGLPROGRAMPARAMETERIPROC)glfwGetProcAddress("glProgramParameteri"); - assert(gl::GetProgramBinary != nullptr); - assert(gl::ProgramBinary != nullptr); - assert(gl::ProgramParameteri != nullptr); + GLint numBinaryFormats; + MBGL_CHECK_ERROR(glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &numBinaryFormats)); + if (numBinaryFormats > 0) { + gl::GetProgramBinary = (gl::PFNGLGETPROGRAMBINARYPROC)glfwGetProcAddress("glGetProgramBinary"); + gl::ProgramBinary = (gl::PFNGLPROGRAMBINARYPROC)glfwGetProcAddress("glProgramBinary"); + gl::ProgramParameteri = (gl::PFNGLPROGRAMPARAMETERIPROC)glfwGetProcAddress("glProgramParameteri"); + assert(gl::GetProgramBinary != nullptr); + assert(gl::ProgramBinary != nullptr); + assert(gl::ProgramParameteri != nullptr); + } } } -- cgit v1.2.1 From 0771e5046064875d323245873f457b87e8c4b447 Mon Sep 17 00:00:00 2001 From: Leith Bade Date: Mon, 22 Dec 2014 18:08:48 +1300 Subject: Rename View functions to match conventions. Also tidy up view classes and remove C style casts. Closes #656 --- platform/default/glfw_view.cpp | 204 +++++++++++++++++----------------- platform/default/headless_display.cpp | 6 +- platform/default/headless_view.cpp | 48 ++++---- 3 files changed, 128 insertions(+), 130 deletions(-) (limited to 'platform') diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 21cd95c8bf..7c0456de6e 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -4,7 +4,7 @@ GLFWView::GLFWView(bool fullscreen_) : fullscreen(fullscreen_) { #ifdef NVIDIA - glDiscardFramebufferEXT = (PFNGLDISCARDFRAMEBUFFEREXTPROC)glfwGetProcAddress("glDiscardFramebufferEXT"); + glDiscardFramebufferEXT = reinterpret_cast(glfwGetProcAddress("glDiscardFramebufferEXT")); #endif } @@ -61,34 +61,34 @@ void GLFWView::initialize(mbgl::Map *map_) { int width, height; glfwGetWindowSize(window, &width, &height); - int fb_width, fb_height; - glfwGetFramebufferSize(window, &fb_width, &fb_height); + int fbWidth, fbHeight; + glfwGetFramebufferSize(window, &fbWidth, &fbHeight); resize(window, 0, 0); - glfwSetCursorPosCallback(window, mousemove); - glfwSetMouseButtonCallback(window, mouseclick); + glfwSetCursorPosCallback(window, mouseMove); + glfwSetMouseButtonCallback(window, mouseClick); glfwSetWindowSizeCallback(window, resize); glfwSetFramebufferSizeCallback(window, resize); glfwSetScrollCallback(window, scroll); glfwSetKeyCallback(window, key); - const std::string extensions = (char *)MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS)); + const std::string extensions = reinterpret_cast(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS))); { using namespace mbgl; if (extensions.find("GL_KHR_debug") != std::string::npos) { - gl::DebugMessageControl = (gl::PFNGLDEBUGMESSAGECONTROLPROC)glfwGetProcAddress("glDebugMessageControl"); - gl::DebugMessageInsert = (gl::PFNGLDEBUGMESSAGEINSERTPROC)glfwGetProcAddress("glDebugMessageInsert"); - gl::DebugMessageCallback = (gl::PFNGLDEBUGMESSAGECALLBACKPROC)glfwGetProcAddress("glDebugMessageCallback"); - gl::GetDebugMessageLog = (gl::PFNGLGETDEBUGMESSAGELOGPROC)glfwGetProcAddress("glGetDebugMessageLog"); - gl::GetPointerv = (gl::PFNGLGETPOINTERVPROC)glfwGetProcAddress("glGetPointerv"); - gl::PushDebugGroup = (gl::PFNGLPUSHDEBUGGROUPPROC)glfwGetProcAddress("glPushDebugGroup"); - gl::PopDebugGroup = (gl::PFNGLPOPDEBUGGROUPPROC)glfwGetProcAddress("glPopDebugGroup"); - gl::ObjectLabel = (gl::PFNGLOBJECTLABELPROC)glfwGetProcAddress("glObjectLabel"); - gl::GetObjectLabel = (gl::PFNGLGETOBJECTLABELPROC)glfwGetProcAddress("glGetObjectLabel"); - gl::ObjectPtrLabel = (gl::PFNGLOBJECTPTRLABELPROC)glfwGetProcAddress("glObjectPtrLabel"); - gl::GetObjectPtrLabel = (gl::PFNGLGETOBJECTPTRLABELPROC)glfwGetProcAddress("glGetObjectPtrLabel"); + gl::DebugMessageControl = reinterpret_cast(glfwGetProcAddress("glDebugMessageControl")); + gl::DebugMessageInsert = reinterpret_cast(glfwGetProcAddress("glDebugMessageInsert")); + gl::DebugMessageCallback = reinterpret_cast(glfwGetProcAddress("glDebugMessageCallback")); + gl::GetDebugMessageLog = reinterpret_cast(glfwGetProcAddress("glGetDebugMessageLog")); + gl::GetPointerv = reinterpret_cast(glfwGetProcAddress("glGetPointerv")); + gl::PushDebugGroup = reinterpret_cast(glfwGetProcAddress("glPushDebugGroup")); + gl::PopDebugGroup = reinterpret_cast(glfwGetProcAddress("glPopDebugGroup")); + gl::ObjectLabel = reinterpret_cast(glfwGetProcAddress("glObjectLabel")); + gl::GetObjectLabel = reinterpret_cast(glfwGetProcAddress("glGetObjectLabel")); + gl::ObjectPtrLabel = reinterpret_cast(glfwGetProcAddress("glObjectPtrLabel")); + gl::GetObjectPtrLabel = reinterpret_cast(glfwGetProcAddress("glGetObjectPtrLabel")); assert(gl::DebugMessageControl != nullptr); assert(gl::DebugMessageInsert != nullptr); assert(gl::DebugMessageCallback != nullptr); @@ -102,11 +102,11 @@ void GLFWView::initialize(mbgl::Map *map_) { assert(gl::GetObjectPtrLabel != nullptr); } else { if (extensions.find("GL_ARB_debug_output") != std::string::npos) { - gl::DebugMessageControl = (gl::PFNGLDEBUGMESSAGECONTROLPROC)glfwGetProcAddress("glDebugMessageControlARB"); - gl::DebugMessageInsert = (gl::PFNGLDEBUGMESSAGEINSERTPROC)glfwGetProcAddress("glDebugMessageInsertARB"); - gl::DebugMessageCallback = (gl::PFNGLDEBUGMESSAGECALLBACKPROC)glfwGetProcAddress("glDebugMessageCallbackARB"); - gl::GetDebugMessageLog = (gl::PFNGLGETDEBUGMESSAGELOGPROC)glfwGetProcAddress("glGetDebugMessageLogARB"); - gl::GetPointerv = (gl::PFNGLGETPOINTERVPROC)glfwGetProcAddress("glGetPointerv"); + gl::DebugMessageControl = reinterpret_cast(glfwGetProcAddress("glDebugMessageControlARB")); + gl::DebugMessageInsert = reinterpret_cast(glfwGetProcAddress("glDebugMessageInsertARB")); + gl::DebugMessageCallback = reinterpret_cast(glfwGetProcAddress("glDebugMessageCallbackARB")); + gl::GetDebugMessageLog = reinterpret_cast(glfwGetProcAddress("glGetDebugMessageLogARB")); + gl::GetPointerv = reinterpret_cast(glfwGetProcAddress("glGetPointerv")); assert(gl::DebugMessageControl != nullptr); assert(gl::DebugMessageInsert != nullptr); assert(gl::DebugMessageCallback != nullptr); @@ -115,36 +115,36 @@ void GLFWView::initialize(mbgl::Map *map_) { } if (extensions.find("GL_EXT_debug_marker") != std::string::npos) { - gl::InsertEventMarkerEXT = (gl::PFNGLINSERTEVENTMARKEREXTPROC)glfwGetProcAddress("glInsertEventMarkerEXT"); - gl::PushGroupMarkerEXT = (gl::PFNGLPUSHGROUPMARKEREXTPROC)glfwGetProcAddress("glPushGroupMarkerEXT"); - gl::PopGroupMarkerEXT = (gl::PFNGLPOPGROUPMARKEREXTPROC)glfwGetProcAddress("glPopGroupMarkerEXT"); + gl::InsertEventMarkerEXT = reinterpret_cast(glfwGetProcAddress("glInsertEventMarkerEXT")); + gl::PushGroupMarkerEXT = reinterpret_cast(glfwGetProcAddress("glPushGroupMarkerEXT")); + gl::PopGroupMarkerEXT = reinterpret_cast(glfwGetProcAddress("glPopGroupMarkerEXT")); assert(gl::InsertEventMarkerEXT != nullptr); assert(gl::PushGroupMarkerEXT != nullptr); assert(gl::PopGroupMarkerEXT != nullptr); } if (extensions.find("GL_EXT_debug_label") != std::string::npos) { - gl::LabelObjectEXT = (gl::PFNGLLABELOBJECTEXTPROC)glfwGetProcAddress("glLabelObjectEXT"); - gl::GetObjectLabelEXT = (gl::PFNGLGETOBJECTLABELEXTPROC)glfwGetProcAddress("glGetObjectLabelEXT"); + gl::LabelObjectEXT = reinterpret_cast(glfwGetProcAddress("glLabelObjectEXT")); + gl::GetObjectLabelEXT = reinterpret_cast(glfwGetProcAddress("glGetObjectLabelEXT")); assert(gl::LabelObjectEXT != nullptr); assert(gl::GetObjectLabelEXT != nullptr); } } if (extensions.find("GL_ARB_vertex_array_object") != std::string::npos) { - gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glfwGetProcAddress("glBindVertexArray"); - gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glfwGetProcAddress("glDeleteVertexArrays"); - gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glfwGetProcAddress("glGenVertexArrays"); - gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glfwGetProcAddress("glIsVertexArray"); + gl::BindVertexArray = reinterpret_cast(glfwGetProcAddress("glBindVertexArray")); + gl::DeleteVertexArrays = reinterpret_cast(glfwGetProcAddress("glDeleteVertexArrays")); + gl::GenVertexArrays = reinterpret_cast(glfwGetProcAddress("glGenVertexArrays")); + gl::IsVertexArray = reinterpret_cast(glfwGetProcAddress("glIsVertexArray")); assert(gl::BindVertexArray != nullptr); assert(gl::DeleteVertexArrays != nullptr); assert(gl::GenVertexArrays != nullptr); assert(gl::IsVertexArray != nullptr); } else if (extensions.find("GL_APPLE_vertex_array_object") != std::string::npos) { - gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glfwGetProcAddress("glBindVertexArrayAPPLE"); - gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glfwGetProcAddress("glDeleteVertexArraysAPPLE"); - gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glfwGetProcAddress("glGenVertexArraysAPPLE"); - gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glfwGetProcAddress("glIsVertexArrayAPPLE"); + gl::BindVertexArray = reinterpret_cast(glfwGetProcAddress("glBindVertexArrayAPPLE")); + gl::DeleteVertexArrays = reinterpret_cast(glfwGetProcAddress("glDeleteVertexArraysAPPLE")); + gl::GenVertexArrays = reinterpret_cast(glfwGetProcAddress("glGenVertexArraysAPPLE")); + gl::IsVertexArray = reinterpret_cast(glfwGetProcAddress("glIsVertexArrayAPPLE")); assert(gl::BindVertexArray != nullptr); assert(gl::DeleteVertexArrays != nullptr); assert(gl::GenVertexArrays != nullptr); @@ -155,9 +155,9 @@ void GLFWView::initialize(mbgl::Map *map_) { GLint numBinaryFormats; MBGL_CHECK_ERROR(glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &numBinaryFormats)); if (numBinaryFormats > 0) { - gl::GetProgramBinary = (gl::PFNGLGETPROGRAMBINARYPROC)glfwGetProcAddress("glGetProgramBinary"); - gl::ProgramBinary = (gl::PFNGLPROGRAMBINARYPROC)glfwGetProcAddress("glProgramBinary"); - gl::ProgramParameteri = (gl::PFNGLPROGRAMPARAMETERIPROC)glfwGetProcAddress("glProgramParameteri"); + gl::GetProgramBinary = reinterpret_cast(glfwGetProcAddress("glGetProgramBinary")); + gl::ProgramBinary = reinterpret_cast(glfwGetProcAddress("glProgramBinary")); + gl::ProgramParameteri = reinterpret_cast(glfwGetProcAddress("glProgramParameteri")); assert(gl::GetProgramBinary != nullptr); assert(gl::ProgramBinary != nullptr); assert(gl::ProgramParameteri != nullptr); @@ -169,7 +169,7 @@ void GLFWView::initialize(mbgl::Map *map_) { } void GLFWView::key(GLFWwindow *window, int key, int /*scancode*/, int action, int mods) { - GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); + GLFWView *view = reinterpret_cast(glfwGetWindowUserPointer(window)); if (action == GLFW_RELEASE) { switch (key) { @@ -197,17 +197,17 @@ void GLFWView::key(GLFWwindow *window, int key, int /*scancode*/, int action, in } } -void GLFWView::scroll(GLFWwindow *window, double /*xoffset*/, double yoffset) { - GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); - double delta = yoffset * 40; +void GLFWView::scroll(GLFWwindow *window, double /*xOffset*/, double yOffset) { + GLFWView *view = reinterpret_cast(glfwGetWindowUserPointer(window)); + double delta = yOffset * 40; - bool is_wheel = delta != 0 && std::fmod(delta, 4.000244140625) == 0; + bool isWheel = delta != 0 && std::fmod(delta, 4.000244140625) == 0; - double absdelta = delta < 0 ? -delta : delta; - double scale = 2.0 / (1.0 + std::exp(-absdelta / 100.0)); + double absDelta = delta < 0 ? -delta : delta; + double scale = 2.0 / (1.0 + std::exp(-absDelta / 100.0)); // Make the scroll wheel a bit slower. - if (!is_wheel) { + if (!isWheel) { scale = (scale - 1.0) / 2.0 + 1.0; } @@ -217,22 +217,20 @@ void GLFWView::scroll(GLFWwindow *window, double /*xoffset*/, double yoffset) { } view->map->startScaling(); - view->map->scaleBy(scale, view->last_x, view->last_y); + view->map->scaleBy(scale, view->lastX, view->lastY); } -void GLFWView::resize(GLFWwindow *window, int, int) { - GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); +void GLFWView::resize(GLFWwindow *window, int width, int height ) { + GLFWView *view = reinterpret_cast(glfwGetWindowUserPointer(window)); - int width, height; - glfwGetWindowSize(window, &width, &height); - int fb_width, fb_height; - glfwGetFramebufferSize(window, &fb_width, &fb_height); + int fbWidth, fbHeight; + glfwGetFramebufferSize(window, &fbWidth, &fbHeight); - view->map->resize(width, height, (float)fb_width / (float)width, fb_width, fb_height); + view->map->resize(width, height, static_cast(fbWidth) / static_cast(width), fbWidth, fbHeight); } -void GLFWView::mouseclick(GLFWwindow *window, int button, int action, int modifiers) { - GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); +void GLFWView::mouseClick(GLFWwindow *window, int button, int action, int modifiers) { + GLFWView *view = reinterpret_cast(glfwGetWindowUserPointer(window)); if (button == GLFW_MOUSE_BUTTON_RIGHT || (button == GLFW_MOUSE_BUTTON_LEFT && modifiers & GLFW_MOD_CONTROL)) { @@ -246,33 +244,33 @@ void GLFWView::mouseclick(GLFWwindow *window, int button, int action, int modifi if (action == GLFW_RELEASE) { view->map->stopPanning(); double now = glfwGetTime(); - if (now - view->last_click < 0.4 /* ms */) { + if (now - view->lastClick < 0.4 /* ms */) { if (modifiers & GLFW_MOD_SHIFT) { - view->map->scaleBy(0.5, view->last_x, view->last_y, 0.5); + view->map->scaleBy(0.5, view->lastX, view->lastY, 0.5); } else { - view->map->scaleBy(2.0, view->last_x, view->last_y, 0.5); + view->map->scaleBy(2.0, view->lastX, view->lastY, 0.5); } } - view->last_click = now; + view->lastClick = now; } } } -void GLFWView::mousemove(GLFWwindow *window, double x, double y) { - GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); +void GLFWView::mouseMove(GLFWwindow *window, double x, double y) { + GLFWView *view = reinterpret_cast(glfwGetWindowUserPointer(window)); if (view->tracking) { - double dx = x - view->last_x; - double dy = y - view->last_y; + double dx = x - view->lastX; + double dy = y - view->lastY; if (dx || dy) { view->map->startPanning(); view->map->moveBy(dx, dy); } } else if (view->rotating) { view->map->startRotating(); - view->map->rotateBy(view->last_x, view->last_y, x, y); + view->map->rotateBy(view->lastX, view->lastY, x, y); } - view->last_x = x; - view->last_y = y; + view->lastX = x; + view->lastY = y; } int GLFWView::run() { @@ -295,11 +293,11 @@ int GLFWView::run() { return 0; } -void GLFWView::make_active() { +void GLFWView::activate() { glfwMakeContextCurrent(window); } -void GLFWView::make_inactive() { +void GLFWView::deactivate() { glfwMakeContextCurrent(nullptr); } @@ -311,20 +309,20 @@ void GLFWView::swap() { glfwPostEmptyEvent(); } -void GLFWView::notify_map_change(mbgl::MapChange /*change*/, mbgl::timestamp /*delay*/) { +void GLFWView::notifyMapChange(mbgl::MapChange /*change*/, mbgl::timestamp /*delay*/) { // no-op } void GLFWView::fps() { static int frames = 0; - static double time_elapsed = 0; + static double timeElapsed = 0; frames++; - double current_time = glfwGetTime(); + double currentTime = glfwGetTime(); - if (current_time - time_elapsed >= 1) { - fprintf(stderr, "FPS: %4.2f\n", frames / (current_time - time_elapsed)); - time_elapsed = current_time; + if (currentTime - timeElapsed >= 1) { + fprintf(stderr, "FPS: %4.2f\n", frames / (currentTime - timeElapsed)); + timeElapsed = currentTime; frames = 0; } } @@ -335,70 +333,70 @@ namespace platform { double elapsed() { return glfwGetTime(); } #ifndef GL_ES_VERSION_2_0 -void show_debug_image(std::string name, const char *data, size_t width, size_t height) { +void showDebugImage(std::string name, const char *data, size_t width, size_t height) { glfwInit(); - static GLFWwindow *debug_window = nullptr; - if (!debug_window) { - debug_window = glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr); - if (!debug_window) { + static GLFWwindow *debugWindow = nullptr; + if (!debugWindow) { + debugWindow = glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr); + if (!debugWindow) { glfwTerminate(); fprintf(stderr, "Failed to initialize window\n"); exit(1); } } - GLFWwindow *current_window = glfwGetCurrentContext(); + GLFWwindow *currentWindow = glfwGetCurrentContext(); - glfwSetWindowSize(debug_window, width, height); - glfwMakeContextCurrent(debug_window); + glfwSetWindowSize(debugWindow, width, height); + glfwMakeContextCurrent(debugWindow); - int fb_width, fb_height; - glfwGetFramebufferSize(debug_window, &fb_width, &fb_height); - float scale = (float)fb_width / (float)width; + int fbWidth, fbHeight; + glfwGetFramebufferSize(debugWindow, &fbWidth, &fbHeight); + float scale = static_cast(fbWidth) / static_cast(width); MBGL_CHECK_ERROR(glPixelZoom(scale, -scale)); MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, 1.0f)); MBGL_CHECK_ERROR(glDrawPixels(width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, data)); - glfwSwapBuffers(debug_window); + glfwSwapBuffers(debugWindow); - glfwMakeContextCurrent(current_window); + glfwMakeContextCurrent(currentWindow); } -void show_color_debug_image(std::string name, const char *data, size_t logical_width, size_t logical_height, size_t width, size_t height) { +void showColorDebugImage(std::string name, const char *data, size_t logicalWidth, size_t logicalHeight, size_t width, size_t height) { glfwInit(); - static GLFWwindow *debug_window = nullptr; - if (!debug_window) { - debug_window = glfwCreateWindow(logical_width, logical_height, name.c_str(), nullptr, nullptr); - if (!debug_window) { + static GLFWwindow *debugWindow = nullptr; + if (!debugWindow) { + debugWindow = glfwCreateWindow(logicalWidth, logicalHeight, name.c_str(), nullptr, nullptr); + if (!debugWindow) { glfwTerminate(); fprintf(stderr, "Failed to initialize window\n"); exit(1); } } - GLFWwindow *current_window = glfwGetCurrentContext(); + GLFWwindow *currentWindow = glfwGetCurrentContext(); - glfwSetWindowSize(debug_window, logical_width, logical_height); - glfwMakeContextCurrent(debug_window); + glfwSetWindowSize(debugWindow, logicalWidth, logicalHeight); + glfwMakeContextCurrent(debugWindow); - int fb_width, fb_height; - glfwGetFramebufferSize(debug_window, &fb_width, &fb_height); - float x_scale = (float)fb_width / (float)width; - float y_scale = (float)fb_height / (float)height; + int fbWidth, fbHeight; + glfwGetFramebufferSize(debugWindow, &fbWidth, &fbHeight); + float xScale = static_cast(fbWidth) / static_cast(width); + float yScale = static_cast(fbHeight) / static_cast(height); MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT)); MBGL_CHECK_ERROR(glEnable(GL_BLEND)); MBGL_CHECK_ERROR(glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); - MBGL_CHECK_ERROR(glPixelZoom(x_scale, -y_scale)); + MBGL_CHECK_ERROR(glPixelZoom(xScale, -yScale)); MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, 1.0f)); MBGL_CHECK_ERROR(glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data)); - glfwSwapBuffers(debug_window); + glfwSwapBuffers(debugWindow); - glfwMakeContextCurrent(current_window); + glfwMakeContextCurrent(currentWindow); } #endif diff --git a/platform/default/headless_display.cpp b/platform/default/headless_display.cpp index 40cc0640a9..4756c8d2cb 100644 --- a/platform/default/headless_display.cpp +++ b/platform/default/headless_display.cpp @@ -11,9 +11,9 @@ HeadlessDisplay::HeadlessDisplay() { // If it is, use kCGLOGLPVersion_3_2_Core and enable that extension. CGLPixelFormatAttribute attributes[] = { kCGLPFAOpenGLProfile, - (CGLPixelFormatAttribute) kCGLOGLPVersion_Legacy, + static_cast(kCGLOGLPVersion_Legacy), kCGLPFAAccelerated, - (CGLPixelFormatAttribute) 0 + static_cast(0) }; GLint num; @@ -38,7 +38,7 @@ HeadlessDisplay::HeadlessDisplay() { throw std::runtime_error("Failed to open X display."); } - const char *extensions = (char *)glXQueryServerString(xDisplay, DefaultScreen(xDisplay), GLX_EXTENSIONS); + const char *extensions = reinterpret_cast(glXQueryServerString(xDisplay, DefaultScreen(xDisplay), GLX_EXTENSIONS)); if (!extensions) { throw std::runtime_error("Cannot read GLX extensions."); } diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp index 4053af226d..84aac1834d 100644 --- a/platform/default/headless_view.cpp +++ b/platform/default/headless_view.cpp @@ -24,7 +24,7 @@ CGLProc CGLGetProcAddress(const char *proc) { } CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, proc, kCFStringEncodingASCII); - CGLProc symbol = (CGLProc)CFBundleGetFunctionPointerForName(framework, name); + CGLProc symbol = reinterpret_cast(CFBundleGetFunctionPointerForName(framework, name)); CFRelease(name); return symbol; } @@ -46,18 +46,18 @@ HeadlessView::HeadlessView(std::shared_ptr display) } void HeadlessView::loadExtensions() { - make_active(); - const char *extension_ptr = (char *)MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS)); + activate(); + const char *extension_ptr = reinterpret_cast(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS))); if (extension_ptr) { const std::string extensions = extension_ptr; #ifdef MBGL_USE_CGL if (extensions.find("GL_APPLE_vertex_array_object") != std::string::npos) { - gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)CGLGetProcAddress("glBindVertexArrayAPPLE"); - gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)CGLGetProcAddress("glDeleteVertexArraysAPPLE"); - gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)CGLGetProcAddress("glGenVertexArraysAPPLE"); - gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)CGLGetProcAddress("glIsVertexArrayAPPLE"); + gl::BindVertexArray = reinterpret_cast(CGLGetProcAddress("glBindVertexArrayAPPLE")); + gl::DeleteVertexArrays = reinterpret_cast(CGLGetProcAddress("glDeleteVertexArraysAPPLE")); + gl::GenVertexArrays = reinterpret_cast(CGLGetProcAddress("glGenVertexArraysAPPLE")); + gl::IsVertexArray = reinterpret_cast(CGLGetProcAddress("glIsVertexArrayAPPLE")); assert(gl::BindVertexArray != nullptr); assert(gl::DeleteVertexArrays != nullptr); assert(gl::GenVertexArrays != nullptr); @@ -66,10 +66,10 @@ void HeadlessView::loadExtensions() { #endif #ifdef MBGL_USE_GLX if (extensions.find("GL_ARB_vertex_array_object") != std::string::npos) { - gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glBindVertexArray"); - gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glDeleteVertexArrays"); - gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glGenVertexArrays"); - gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glIsVertexArray"); + gl::BindVertexArray = reinterpret_cast(glXGetProcAddress((const GLubyte *)"glBindVertexArray")); + gl::DeleteVertexArrays = reinterpret_cast(glXGetProcAddress((const GLubyte *)"glDeleteVertexArrays")); + gl::GenVertexArrays = reinterpret_cast(glXGetProcAddress((const GLubyte *)"glGenVertexArrays")); + gl::IsVertexArray = reinterpret_cast(glXGetProcAddress((const GLubyte *)"glIsVertexArray")); assert(gl::BindVertexArray != nullptr); assert(gl::DeleteVertexArrays != nullptr); assert(gl::GenVertexArrays != nullptr); @@ -78,7 +78,7 @@ void HeadlessView::loadExtensions() { #endif } - make_inactive(); + deactivate(); } void HeadlessView::createContext() { @@ -126,7 +126,7 @@ void HeadlessView::createContext() { } void HeadlessView::resize(uint16_t width, uint16_t height, float pixelRatio) { - clear_buffers(); + clearBuffers(); width_ = width; height_ = height; @@ -135,7 +135,7 @@ void HeadlessView::resize(uint16_t width, uint16_t height, float pixelRatio) { const unsigned int w = width_ * pixelRatio_; const unsigned int h = height_ * pixelRatio_; - make_active(); + activate(); // Create depth/stencil buffer MBGL_CHECK_ERROR(glGenRenderbuffersEXT(1, &fboDepthStencil)); @@ -171,7 +171,7 @@ void HeadlessView::resize(uint16_t width, uint16_t height, float pixelRatio) { throw std::runtime_error(error.str()); } - make_inactive(); + deactivate(); } std::unique_ptr HeadlessView::readPixels() { @@ -180,9 +180,9 @@ std::unique_ptr HeadlessView::readPixels() { auto pixels = util::make_unique(w * h); - make_active(); + activate(); MBGL_CHECK_ERROR(glReadPixels(0, 0, width_, height_, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get())); - make_inactive(); + deactivate(); const int stride = w * 4; auto tmp = util::make_unique(stride); @@ -196,8 +196,8 @@ std::unique_ptr HeadlessView::readPixels() { return pixels; } -void HeadlessView::clear_buffers() { - make_active(); +void HeadlessView::clearBuffers() { + activate(); MBGL_CHECK_ERROR(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)); @@ -216,11 +216,11 @@ void HeadlessView::clear_buffers() { fboDepthStencil = 0; } - make_inactive(); + deactivate(); } HeadlessView::~HeadlessView() { - clear_buffers(); + clearBuffers(); #if MBGL_USE_CGL CGLDestroyContext(glContext); @@ -240,11 +240,11 @@ void HeadlessView::notify() { // no-op } -void HeadlessView::notify_map_change(mbgl::MapChange /*change*/, mbgl::timestamp /*delay*/) { +void HeadlessView::notifyMapChange(mbgl::MapChange /*change*/, mbgl::timestamp /*delay*/) { // no-op } -void HeadlessView::make_active() { +void HeadlessView::activate() { #if MBGL_USE_CGL CGLError error = CGLSetCurrentContext(glContext); if (error != kCGLNoError) { @@ -259,7 +259,7 @@ void HeadlessView::make_active() { #endif } -void HeadlessView::make_inactive() { +void HeadlessView::deactivate() { #if MBGL_USE_CGL CGLError error = CGLSetCurrentContext(nullptr); if (error != kCGLNoError) { -- cgit v1.2.1