refactor into files
This commit is contained in:
@@ -8,6 +8,10 @@
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include "logger.h"
|
||||
#include "egl_context.h"
|
||||
#include "assets_manager.h"
|
||||
#include "shader.h"
|
||||
#include "external_texture.h"
|
||||
#include "quad.h"
|
||||
#include <glad/gles2.h>
|
||||
#include <glad/egl.h>
|
||||
#include <memory>
|
||||
@@ -17,163 +21,12 @@
|
||||
using namespace aidl::com::omixlab::mosis;
|
||||
using namespace aidl::android::hardware;
|
||||
|
||||
class AssetsManager
|
||||
{
|
||||
static inline AAssetManager* m_asset_manager = nullptr;
|
||||
public:
|
||||
static void Init(AAssetManager* asset_manager)
|
||||
{
|
||||
m_asset_manager = asset_manager;
|
||||
}
|
||||
static std::vector<uint8_t> ReadAll(const std::string& filename)
|
||||
{
|
||||
if (AAsset* asset = AAssetManager_open(m_asset_manager,
|
||||
filename.c_str(), AASSET_MODE_BUFFER))
|
||||
{
|
||||
const auto size = AAsset_getLength(asset);
|
||||
auto buffer = std::vector<uint8_t>(size);
|
||||
AAsset_read(asset, buffer.data(), size);
|
||||
AAsset_close(asset);
|
||||
return buffer;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
class Shader
|
||||
{
|
||||
GLuint m_program = 0;
|
||||
public:
|
||||
~Shader()
|
||||
{
|
||||
if (m_program != 0)
|
||||
{
|
||||
glDeleteProgram(m_program);
|
||||
}
|
||||
}
|
||||
bool load(const std::string& vertexFilename, const std::string& fragmentFilename)
|
||||
{
|
||||
// Read source from assets
|
||||
std::vector<uint8_t> vsSourceRaw = AssetsManager::ReadAll(vertexFilename);
|
||||
std::vector<uint8_t> fsSourceRaw = AssetsManager::ReadAll(fragmentFilename);
|
||||
|
||||
if (vsSourceRaw.empty() || fsSourceRaw.empty())
|
||||
{
|
||||
Logger::Log("Failed to read shader files from assets");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Null-terminate the source strings
|
||||
std::string vsSource(vsSourceRaw.begin(), vsSourceRaw.end());
|
||||
std::string fsSource(fsSourceRaw.begin(), fsSourceRaw.end());
|
||||
|
||||
GLuint vertexShader = compile(GL_VERTEX_SHADER, vsSource.c_str());
|
||||
if (!vertexShader) return false;
|
||||
|
||||
GLuint fragmentShader = compile(GL_FRAGMENT_SHADER, fsSource.c_str());
|
||||
if (!fragmentShader)
|
||||
{
|
||||
glDeleteShader(vertexShader);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_program = glCreateProgram();
|
||||
glAttachShader(m_program, vertexShader);
|
||||
glAttachShader(m_program, fragmentShader);
|
||||
glLinkProgram(m_program);
|
||||
|
||||
// Check for link errors
|
||||
GLint linked;
|
||||
glGetProgramiv(m_program, GL_LINK_STATUS, &linked);
|
||||
if (!linked)
|
||||
{
|
||||
GLint infoLen = 0;
|
||||
glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &infoLen);
|
||||
if (infoLen > 1)
|
||||
{
|
||||
std::vector<char> infoLog(infoLen);
|
||||
glGetProgramInfoLog(m_program, infoLen, nullptr, infoLog.data());
|
||||
Logger::Log(std::format("Error linking program: {}", infoLog.data()));
|
||||
}
|
||||
glDeleteProgram(m_program);
|
||||
m_program = 0;
|
||||
}
|
||||
|
||||
// Shaders can be deleted after linking
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
|
||||
return (m_program != 0);
|
||||
}
|
||||
void use() const
|
||||
{
|
||||
if (m_program) glUseProgram(m_program);
|
||||
}
|
||||
GLint getUniformLocation(const char* name) const
|
||||
{
|
||||
return glGetUniformLocation(m_program, name);
|
||||
}
|
||||
GLint getAttribLocation(const char* name) const
|
||||
{
|
||||
return glGetAttribLocation(m_program, name);
|
||||
}
|
||||
private:
|
||||
GLuint compile(GLenum type, const char* source)
|
||||
{
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &source, nullptr);
|
||||
glCompileShader(shader);
|
||||
|
||||
GLint compiled;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
|
||||
if (!compiled)
|
||||
{
|
||||
GLint infoLen = 0;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
|
||||
if (infoLen > 1)
|
||||
{
|
||||
std::vector<char> infoLog(infoLen);
|
||||
glGetShaderInfoLog(shader, infoLen, nullptr, infoLog.data());
|
||||
Logger::Log(std::format("Error compiling shader type {}: {}",
|
||||
type, infoLog.data()));
|
||||
}
|
||||
glDeleteShader(shader);
|
||||
return 0;
|
||||
}
|
||||
return shader;
|
||||
}
|
||||
};
|
||||
|
||||
class ImportedTexture
|
||||
{
|
||||
GLuint texture = 0;
|
||||
public:
|
||||
bool create(AHardwareBuffer* hardwareBuffer)
|
||||
{
|
||||
AHardwareBuffer_Desc desc{};
|
||||
AHardwareBuffer_describe(hardwareBuffer, &desc);
|
||||
EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hardwareBuffer);
|
||||
EGLImageKHR eglImage = eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT,
|
||||
EGL_NATIVE_BUFFER_ANDROID, clientBuffer, nullptr);
|
||||
if (eglImage == EGL_NO_IMAGE_KHR)
|
||||
{
|
||||
Logger::Log("Failed to create EGL image");
|
||||
return false;
|
||||
}
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eglImage);
|
||||
eglDestroyImageKHR(eglGetCurrentDisplay(), eglImage);
|
||||
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class Renderer : public BnMosisListener
|
||||
{
|
||||
std::unique_ptr<egl::Context> m_egl_context;
|
||||
std::unique_ptr<ImportedTexture> m_texture;
|
||||
std::unique_ptr<ExternalTexture> m_texture;
|
||||
std::unique_ptr<Shader> m_quad_shader;
|
||||
std::unique_ptr<Quad> m_quad;
|
||||
AHardwareBuffer* m_hwbuffer = nullptr;
|
||||
std::thread m_render_loop;
|
||||
bool m_active = false;
|
||||
@@ -182,12 +35,13 @@ class Renderer : public BnMosisListener
|
||||
m_egl_context = std::make_unique<egl::Context>();
|
||||
if (m_egl_context->create(window))
|
||||
{
|
||||
init_resources();
|
||||
m_active = true;
|
||||
while (m_active)
|
||||
{
|
||||
if (m_hwbuffer && !m_texture)
|
||||
{
|
||||
m_texture = std::make_unique<ImportedTexture>();
|
||||
m_texture = std::make_unique<ExternalTexture>();
|
||||
if (!m_texture->create(m_hwbuffer))
|
||||
{
|
||||
Logger::Log("Failed to create texture");
|
||||
@@ -201,13 +55,24 @@ class Renderer : public BnMosisListener
|
||||
Logger::Log("Failed to create EGL context");
|
||||
}
|
||||
}
|
||||
void init_resources()
|
||||
{
|
||||
m_quad_shader = std::make_unique<Shader>();
|
||||
m_quad_shader->load("quad.vs.glsl", "quad.fs.glsl");
|
||||
m_quad = std::make_unique<Quad>();
|
||||
m_quad->create();
|
||||
}
|
||||
void render_frame()
|
||||
{
|
||||
glClearColor(0.f, 0.5f, 0.5f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
if (m_texture)
|
||||
{
|
||||
|
||||
m_quad_shader->use();
|
||||
glUniform1i(m_quad_shader->getUniformLocation("uTexture"), 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_texture->bind();
|
||||
m_quad->draw();
|
||||
}
|
||||
m_egl_context->swap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user