Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refac: separate utils and error handle GL funcs #334

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 33 additions & 65 deletions code/mobile/android/PhoneVR/app/src/main/cpp/alvr_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,10 @@
#include <vector>

#include "nlohmann/json.hpp"
#include "utils.h"

using namespace nlohmann;

const char LOG_TAG[] = "ALVR_PVR_NATIVE";

void log(AlvrLogLevel level, const char *format, ...) {
va_list args;
va_start(args, format);

char buf[1024];
int count = vsnprintf(buf, sizeof(buf), format, args);
if (count > (int) sizeof(buf))
count = (int) sizeof(buf);
if (count > 0 && buf[count - 1] == '\n')
buf[count - 1] = '\0';

alvr_log(level, buf);

switch (level) {
case ALVR_LOG_LEVEL_DEBUG:
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "%s", buf);
break;
case ALVR_LOG_LEVEL_INFO:
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "%s", buf);
break;
case ALVR_LOG_LEVEL_ERROR:
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "%s", buf);
break;
case ALVR_LOG_LEVEL_WARN:
__android_log_print(ANDROID_LOG_WARN, LOG_TAG, "%s", buf);
break;
}

va_end(args);
}

#define error(...) log(ALVR_LOG_LEVEL_ERROR, __VA_ARGS__)
#define info(...) log(ALVR_LOG_LEVEL_INFO, __VA_ARGS__)
#define debug(...) log(ALVR_LOG_LEVEL_DEBUG, __VA_ARGS__)

uint64_t HEAD_ID = alvr_path_string_to_id("/user/head");

// Note: the Cardboard SDK cannot estimate display time and an heuristic is used instead.
Expand Down Expand Up @@ -345,29 +309,31 @@ extern "C" JNIEXPORT void JNICALL Java_viritualisres_phonevr_ALVRActivity_render
info("Pausing ALVR since glContext is not recreated, deleting textures");
alvr_pause_opengl();

glDeleteTextures(2, CTX.lobbyTextures);
GL(glDeleteTextures(2, CTX.lobbyTextures));
}

if (CTX.renderingParamsChanged || CTX.glContextRecreated) {
info("Rebuilding, binding textures, Resuming ALVR since glContextRecreated %b, "
"renderingParamsChanged %b",
CTX.renderingParamsChanged,
CTX.glContextRecreated);
glGenTextures(2, CTX.lobbyTextures);
GL(glGenTextures(2, CTX.lobbyTextures));

for (auto &lobbyTexture : CTX.lobbyTextures) {
glBindTexture(GL_TEXTURE_2D, lobbyTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
CTX.screenWidth / 2,
CTX.screenHeight,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
nullptr);
GL(glBindTexture(GL_TEXTURE_2D, lobbyTexture));
GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL(glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
CTX.screenWidth / 2,
CTX.screenHeight,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
nullptr));
}

const uint32_t *targetViews[2] = {(uint32_t *) &CTX.lobbyTextures[0],
Expand Down Expand Up @@ -404,21 +370,23 @@ extern "C" JNIEXPORT void JNICALL Java_viritualisres_phonevr_ALVRActivity_render
info("Got settings from ALVR Server - %s", &settings_buffer[900]);
json settings_json = json::parse(&settings_buffer[0]);

glGenTextures(2, CTX.streamTextures);
GL(glGenTextures(2, CTX.streamTextures));

for (auto &streamTexture : CTX.streamTextures) {
glBindTexture(GL_TEXTURE_2D, streamTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
config.view_width,
config.view_height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
nullptr);
GL(glBindTexture(GL_TEXTURE_2D, streamTexture));
GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL(glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
config.view_width,
config.view_height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
nullptr));
}

CTX.fovArr[0] = getFov((CardboardEye) 0);
Expand Down Expand Up @@ -499,7 +467,7 @@ extern "C" JNIEXPORT void JNICALL Java_viritualisres_phonevr_ALVRActivity_render
CTX.streaming = false;
CTX.inputThread.join();

glDeleteTextures(2, CTX.streamTextures);
GL(glDeleteTextures(2, CTX.streamTextures));
info("ALVR Poll Event: ALVR_EVENT_STREAMING_STOPPED, Stream stopped deleted "
"textures.");
}
Expand Down
90 changes: 90 additions & 0 deletions code/mobile/android/PhoneVR/app/src/main/cpp/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef PHONEVR_UTILS_H
#define PHONEVR_UTILS_H

#include "alvr_client_core.h"
#include <GLES3/gl3.h>
#include <android/log.h>
#include <string>

#define error(...) log(ALVR_LOG_LEVEL_ERROR, __FILE_NAME__, __LINE__, __func__, __VA_ARGS__)
#define info(...) log(ALVR_LOG_LEVEL_INFO, __FILE_NAME__, __LINE__, __func__, __VA_ARGS__)
#define debug(...) log(ALVR_LOG_LEVEL_DEBUG, __FILE_NAME__, __LINE__, __func__, __VA_ARGS__)

const char LOG_TAG[] = "ALVR_PVR_NATIVE";

void log(AlvrLogLevel level,
const char *FILE_NAME,
unsigned int LINE_NO,
const char *FUNC,
const char *format,
...) {
va_list args;
va_start(args, format);

char buf[1024];
std::string format_str;
format_str = std::string(FILE_NAME) + ":" + std::to_string(LINE_NO) + ": " + std::string(FUNC) +
"():" + format;

int count = vsnprintf(buf, sizeof(buf), format_str.c_str(), args);
if (count > (int) sizeof(buf))
count = (int) sizeof(buf);
if (count > 0 && buf[count - 1] == '\n')
buf[count - 1] = '\0';

alvr_log(level, buf);

switch (level) {
case ALVR_LOG_LEVEL_DEBUG:
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "%s", buf);
break;
case ALVR_LOG_LEVEL_INFO:
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "%s", buf);
break;
case ALVR_LOG_LEVEL_ERROR:
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "%s", buf);
break;
case ALVR_LOG_LEVEL_WARN:
__android_log_print(ANDROID_LOG_WARN, LOG_TAG, "%s", buf);
break;
}

va_end(args);
}

static const char *GlErrorString(GLenum error) {
switch (error) {
case GL_NO_ERROR:
return "GL_NO_ERROR";
case GL_INVALID_ENUM:
return "GL_INVALID_ENUM";
case GL_INVALID_VALUE:
return "GL_INVALID_VALUE";
case GL_INVALID_OPERATION:
return "GL_INVALID_OPERATION";
case GL_INVALID_FRAMEBUFFER_OPERATION:
return "GL_INVALID_FRAMEBUFFER_OPERATION";
case GL_OUT_OF_MEMORY:
return "GL_OUT_OF_MEMORY";
default:
return "unknown";
}
}

[[maybe_unused]] static void GLCheckErrors(const char *file, int line) {
GLenum error = glGetError();
if (error == GL_NO_ERROR) {
return;
}
while (error != GL_NO_ERROR) {
error("GL error on %s:%d : %s", file, line, GlErrorString(error));
error = glGetError();
}
abort();
}

#define GL(func) \
func; \
GLCheckErrors(__FILE__, __LINE__)

#endif // PHONEVR_UTILS_H
Loading