From 0dfb458c71159742e6330aeaf673339db81b47a4 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Sun, 2 Apr 2017 11:35:11 +0100 Subject: [PATCH] Android opengl debug callback, Android device properties, fix texture internal format --- android/src/main/cpp/main.cpp | 111 ++++++++++++++++++++++++++++++++-- engine/app.cpp | 19 +++--- engine/asset.cpp | 2 +- engine/font.cpp | 2 +- engine/layout.h | 4 +- engine/pch.h | 1 + engine/texture.cpp | 15 +++-- engine/texture.h | 7 ++- engine/util.h | 9 +-- 9 files changed, 135 insertions(+), 35 deletions(-) diff --git a/android/src/main/cpp/main.cpp b/android/src/main/cpp/main.cpp index 0e70904..3c416ea 100755 --- a/android/src/main/cpp/main.cpp +++ b/android/src/main/cpp/main.cpp @@ -26,6 +26,24 @@ #include "app.h" #include "..\..\..\..\engine\asset.h" +typedef void (*GLDEBUGPROC)(GLenum source, + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar* message, + const void* userParam); + +typedef void (*fnDebugMessageCallback)(GLDEBUGPROC callback, const void* userParam); + +#define GL_DEBUG_SEVERITY_HIGH 0x9146 +#define GL_DEBUG_SEVERITY_MEDIUM 0x9147 +#define GL_DEBUG_SEVERITY_LOW 0x9148 +#define GL_DEBUG_SEVERITY_NOTIFICATION 0x826B + +#define GL_DEBUG_OUTPUT 0x92E0 +#define GL_DEBUG_OUTPUT_SYNCHRONOUS 0x8242 + /** * Our saved state data. */ @@ -116,8 +134,10 @@ static int engine_init_display(struct engine* engine) { surface = eglCreateWindowSurface(display, config, engine->app->window, NULL); const EGLint attribs_test[] = { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR | EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR, + //EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR, + EGL_NONE }; context = eglCreateContext(display, config, EGL_NO_CONTEXT, attribs_test); @@ -138,11 +158,92 @@ static int engine_init_display(struct engine* engine) { engine->state.angle = 0; // Check openGL on the system - auto opengl_info = {GL_VENDOR, GL_RENDERER, GL_VERSION/*, GL_EXTENSIONS*/}; + auto opengl_info = { GL_VENDOR, GL_RENDERER, GL_VERSION/*, GL_EXTENSIONS*/ }; for (auto name : opengl_info) { auto info = glGetString(name); LOG("OpenGL Info: %s", info); } + + const char* ext = (const char*) glGetString(GL_EXTENSIONS); + int ext_len = strlen(ext); + std::map ext_map; + static char ext_name[256]; + int ext_name_i = 0; + for (int i = 0; i < ext_len; i++) + { + char c = ext[i]; + if (c == ' ') + { + ext_map.emplace(std::string(ext_name, ext_name_i), true); + ext_name_i = 0; + } + else + { + ext_name[ext_name_i++] = c; + } + } + + if (ext_map.count("GL_KHR_debug")) + { + LOG("GL_KHR_debug supported"); + auto glDebugMessageCallback = (fnDebugMessageCallback)eglGetProcAddress("glDebugMessageCallbackKHR"); + if (glDebugMessageCallback) + { + LOG("glDebugMessageCallback proc found at %p", glDebugMessageCallback); + glDebugMessageCallback([](GLenum source, GLenum type, GLuint id, + GLenum severity, GLsizei length, const GLchar* message, const void* userParam) + { + //if (severity == GL_DEBUG_SEVERITY_MEDIUM || severity == GL_DEBUG_SEVERITY_HIGH) + { + LOG("OPENGL: %.*s", length, message); + } + }, nullptr); + glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); + } + else + { + LOG("glDebugMessageCallback proc NOT FOUND"); + } + } + + + int ret = -1; + FILE* file = popen("getprop", "r"); + std::map os_props; + if (file) + { + char output[100]; + while (fgets(output, sizeof(output), file) != nullptr) + { + int i = 0; + int l = strlen(output); + char buf[64]; + int j = 0; + while (i < l && output[i] != '[') i++; + i++; + while (i < l && output[i] != ']') { buf[j++] = output[i]; i++; } + std::string key(buf, j); + j = 0; + while (i < l && output[i] != '[') i++; + i++; + while (i < l && output[i] != ']') { buf[j++] = output[i]; i++; } + os_props[key] = std::string(buf, j); + //LOG("PROP: %s -> %s", key.c_str(), os_props[key].c_str()); + } + pclose(file); + } + LOG("PROP Android Version: %s", os_props["ro.build.version.release"].c_str()); + LOG("PROP Android SDK: %s", os_props["ro.build.version.sdk"].c_str()); + LOG("PROP Country Code: %s", os_props["ro.csc.country_code"].c_str()); + LOG("PROP ABI: %s", os_props["ro.product.cpu.abilist"].c_str()); + LOG("PROP Brand: %s", os_props["ro.product.brand"].c_str()); + LOG("PROP Maker: %s", os_props["ro.product.manufacturer"].c_str()); + LOG("PROP Mode: %s", os_props["ro.product.model"].c_str()); + //LOG("PROP: %s", os_props[""].c_str()); + //LOG("PROP: %s", os_props[""].c_str()); + //LOG("PROP: %s", os_props[""].c_str()); + // Initialize GL state. //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); //glEnable(GL_CULL_FACE); @@ -203,7 +304,7 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) struct engine* engine = (struct engine*)app->userData; float x = AMotionEvent_getX(event, 0); float y = AMotionEvent_getY(event, 0); - LOG("event source: %d", AInputEvent_getSource(event)); + //LOG("event source: %d", AInputEvent_getSource(event)); MouseEvent e; int32_t eventType = AInputEvent_getType(event); switch (eventType) { @@ -225,7 +326,7 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) App::I.mouse_move(x, y); return 1; default: - LOG("motion action: %d", action); + //LOG("motion action: %d", action); break; } } diff --git a/engine/app.cpp b/engine/app.cpp index 59dcaca..39500d1 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -459,16 +459,17 @@ void App::init() { GL_DEBUG_SEVERITY_MEDIUM, FOREGROUND_GREEN | FOREGROUND_INTENSITY }, { GL_DEBUG_SEVERITY_HIGH, FOREGROUND_RED | FOREGROUND_INTENSITY }, }; - if (severity == GL_DEBUG_SEVERITY_MEDIUM || severity == GL_DEBUG_SEVERITY_HIGH) + if (severity == GL_DEBUG_SEVERITY_MEDIUM || severity == GL_DEBUG_SEVERITY_HIGH || severity == GL_DEBUG_SEVERITY_LOW) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[severity]); - LOG("%.*s", length, message); + LOG("OPENGL: %.*s", length, message); FlushConsoleInputBuffer(GetStdHandle(STD_OUTPUT_HANDLE)); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), info.wAttributes); - __debugbreak(); +// __debugbreak(); } }, nullptr); - glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); + glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); #endif //int n; @@ -550,7 +551,7 @@ bool App::mouse_down(int button, float x, float y) e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL; e.m_pos = { x / zoom, y / zoom }; auto ret = layout[main_id]->on_event(&e); - LOG("mouse click button%d pos %f %f", button, x, y); + //LOG("mouse click button%d pos %f %f", button, x, y); // if (popup) // { @@ -584,7 +585,7 @@ bool App::mouse_up(int button, float x, float y) e.m_pos = { x / zoom, y / zoom }; auto ret = layout[main_id]->on_event(&e); layout[main_id]->update(); - LOG("mouse up button%d pos %f %f", button, x, y); + //LOG("mouse up button%d pos %f %f", button, x, y); return ret == kEventResult::Consumed; } bool App::key_down(int key) @@ -594,7 +595,7 @@ bool App::key_down(int key) e.m_key = key; auto ret = layout[main_id]->on_event(&e); layout[main_id]->update(); - LOG("key down %d '%c'", key, key); + //LOG("key down %d '%c'", key, key); return ret == kEventResult::Consumed; } bool App::key_up(int key) @@ -604,7 +605,7 @@ bool App::key_up(int key) e.m_key = key; auto ret = layout[main_id]->on_event(&e); layout[main_id]->update(); - LOG("key up %d '%c'", key, key); + //LOG("key up %d '%c'", key, key); return ret == kEventResult::Consumed; } bool App::key_char(int key) @@ -614,6 +615,6 @@ bool App::key_char(int key) e.m_key = key; auto ret = layout[main_id]->on_event(&e); layout[main_id]->update(); - LOG("key up %d '%c'", key, key); + //LOG("key up %d '%c'", key, key); return ret == kEventResult::Consumed; } \ No newline at end of file diff --git a/engine/asset.cpp b/engine/asset.cpp index ac66d05..63e726e 100644 --- a/engine/asset.cpp +++ b/engine/asset.cpp @@ -7,7 +7,7 @@ AAssetManager* Asset::m_am; bool Asset::open(const char* path) { - LOG("Asset::open %s", path); + //LOG("Asset::open %s", path); m_current_path = path; #ifdef __ANDROID__ if (!(m_asset = AAssetManager_open(m_am, path, AASSET_MODE_RANDOM))) diff --git a/engine/font.cpp b/engine/font.cpp index 9cdfa20..99d083d 100644 --- a/engine/font.cpp +++ b/engine/font.cpp @@ -16,7 +16,7 @@ bool Font::load(const char* ttf, int font_size) auto bitmap = std::make_unique(w*h); chars.resize(num_chars); int ret = stbtt_BakeFontBitmap(file.m_data, 0, (float)font_size, bitmap.get(), w, h, start_char, num_chars, chars.data()); - font_tex.create(w, h, GL_RED, bitmap.get()); + font_tex.create(w, h, GL_R8, GL_RED, bitmap.get()); file.close(); return true; } diff --git a/engine/layout.h b/engine/layout.h index 2b61949..df90fd4 100644 --- a/engine/layout.h +++ b/engine/layout.h @@ -1529,11 +1529,11 @@ public: ::FindClose(hFind); } #elif __ANDROID__ - LOG("listing brushes"); + //LOG("listing brushes"); AAssetDir* dir = AAssetManager_openDir(Asset::m_am, "data/Icons"); while (const char* name = AAssetDir_getNextFileName(dir)) { - LOG("asset: %s", name); + //LOG("asset: %s", name); names.push_back(name); } AAssetDir_close(dir); diff --git a/engine/pch.h b/engine/pch.h index 67893aa..c9b22c0 100644 --- a/engine/pch.h +++ b/engine/pch.h @@ -13,6 +13,7 @@ #elif __ANDROID__ #include + #include #include #include diff --git a/engine/texture.cpp b/engine/texture.cpp index 659eb12..ab518aa 100644 --- a/engine/texture.cpp +++ b/engine/texture.cpp @@ -14,9 +14,9 @@ bool TextureManager::load(const char* path) return true; } -void TextureManager::assign(uint16_t id, GLuint tex, int w/* = -1*/, int h/* = -1*/, GLuint format/* = GL_RGBA*/) +void TextureManager::assign(uint16_t id, GLuint tex, int w/* = -1*/, int h/* = -1*/, GLuint internal_format/* = GL_RGBA8*/, GLuint format/* = GL_RGBA*/) { - m_textures[id].assign(tex, w, h, format); + m_textures[id].assign(tex, w, h, internal_format, format); } Texture2D& TextureManager::get(uint16_t id) @@ -24,29 +24,32 @@ Texture2D& TextureManager::get(uint16_t id) return m_textures[id]; } -bool Texture2D::create(int width, int height, GLint format, const uint8_t* data) +bool Texture2D::create(int width, int height, GLint internal_format, GLint format, const uint8_t* data) { m_width = width; m_height = height; m_format = format; + m_iformat = internal_format; glGenTextures(1, &m_tex); bind(); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, format, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, data); unbind(); return true; } bool Texture2D::create(const ui::Image& img) { static GLint formats[] = { GL_RED, GL_RG, GL_RGB, GL_RGBA }; - return create(img.width, img.height, formats[img.comp - 1], img.data()); + static GLint iformats[] = { GL_R8, GL_RG8, GL_RGB8, GL_RGBA8 }; + return create(img.width, img.height, iformats[img.comp - 1], formats[img.comp - 1], img.data()); } -void Texture2D::assign(GLuint tex, int w/* = -1*/, int h/* = -1*/, GLuint format/* = GL_RGBA*/) +void Texture2D::assign(GLuint tex, int w/* = -1*/, int h/* = -1*/, GLuint internal_format/* = GL_RGBA8*/, GLuint format/* = GL_RGBA*/) { m_tex = tex; m_width = w; m_height = h; m_format = format; + m_iformat = internal_format; } bool Texture2D::load(std::string filename) diff --git a/engine/texture.h b/engine/texture.h index ee1b80e..227ea14 100644 --- a/engine/texture.h +++ b/engine/texture.h @@ -7,10 +7,11 @@ class Texture2D int m_width; int m_height; GLint m_format; + GLint m_iformat; public: - bool create(int width, int height, GLint format = GL_RGBA, const uint8_t* data = nullptr); + bool create(int width, int height, GLint internal_format, GLint format = GL_RGBA, const uint8_t* data = nullptr); bool create(const ui::Image& img); - void assign(GLuint tex, int w = -1, int h = -1, GLuint format = GL_RGBA); + void assign(GLuint tex, int w = -1, int h = -1, GLuint internal_format = GL_RGBA8, GLuint format = GL_RGBA); bool load(std::string filename); void destroy() { glDeleteTextures(1, &m_tex); } void bind() const { glBindTexture(GL_TEXTURE_2D, m_tex); } @@ -37,6 +38,6 @@ class TextureManager public: static std::map m_textures; static bool load(const char* path); - static void assign(uint16_t id, GLuint tex, int w = -1, int h = -1, GLuint format = GL_RGBA); + static void assign(uint16_t id, GLuint tex, int w = -1, int h = -1, GLuint internal_format = GL_RGBA8, GLuint format = GL_RGBA); static Texture2D& get(uint16_t id); }; diff --git a/engine/util.h b/engine/util.h index 18773cb..ccba0e5 100644 --- a/engine/util.h +++ b/engine/util.h @@ -12,14 +12,7 @@ glm::vec4 rand_color(); glm::vec3 convert_hsv2rgb(const glm::vec3 c); glm::vec3 convert_rgb2hsv(const glm::vec3 c); -//void check_OpenGLError(const char* stmt, const char* fname, int line) -//{ -// GLenum err; -// while ((err = glGetError()) != GL_NO_ERROR) -// { -// //LOG("OpenGL error %08x (%s), at %s:%i - for %s", err, gluErrorString(err), fname, line, stmt); -// } -//} +void check_OpenGLError(const char* stmt, const char* fname, int line); template struct cbuffer {