Skip to content

Commit

Permalink
Add gpu acceleration support for editor helper functions
Browse files Browse the repository at this point in the history
Test: ./ultrahdr_unit_test

Change-Id: Ifb4ada9156863bdbd3c5de54a07a1a4cdb1e2fb8
  • Loading branch information
vivekrj1806 authored and DichenZhang1 committed Aug 21, 2024
1 parent d5a4786 commit 4c56a21
Show file tree
Hide file tree
Showing 9 changed files with 687 additions and 67 deletions.
33 changes: 29 additions & 4 deletions lib/include/ultrahdr/editorhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,41 @@ extern void rotate_buffer_clockwise_neon(T* src_buffer, T* dst_buffer, int src_w
int src_stride, int dst_stride, int degrees);
#endif

#ifdef UHDR_ENABLE_GLES

std::unique_ptr<uhdr_raw_image_ext_t> apply_resize_gles(uhdr_raw_image_t* src, int dst_w, int dst_h,
uhdr_opengl_ctxt* gl_ctxt,
GLuint* srcTexture);

std::unique_ptr<uhdr_raw_image_ext_t> apply_mirror_gles(ultrahdr::uhdr_mirror_effect_t* desc,
uhdr_raw_image_t* src,
uhdr_opengl_ctxt* gl_ctxt,
GLuint* srcTexture);

std::unique_ptr<uhdr_raw_image_ext_t> apply_rotate_gles(ultrahdr::uhdr_rotate_effect_t* desc,
uhdr_raw_image_t* src,
uhdr_opengl_ctxt* gl_ctxt,
GLuint* srcTexture);

void apply_crop_gles(uhdr_raw_image_t* src, int left, int top, int wd, int ht,
uhdr_opengl_ctxt* gl_ctxt, GLuint* srcTexture);
#endif

std::unique_ptr<uhdr_raw_image_ext_t> apply_rotate(ultrahdr::uhdr_rotate_effect_t* desc,
uhdr_raw_image_t* src);
uhdr_raw_image_t* src, void* gl_ctxt = nullptr,
void* texture = nullptr);

std::unique_ptr<uhdr_raw_image_ext_t> apply_mirror(ultrahdr::uhdr_mirror_effect_t* desc,
uhdr_raw_image_t* src);
uhdr_raw_image_t* src, void* gl_ctxt = nullptr,
void* texture = nullptr);

std::unique_ptr<uhdr_raw_image_ext_t> apply_resize(ultrahdr::uhdr_resize_effect_t* desc,
uhdr_raw_image* src, int dst_w, int dst_h);
uhdr_raw_image* src, int dst_w, int dst_h,
void* gl_ctxt = nullptr,
void* texture = nullptr);

void apply_crop(uhdr_raw_image_t* src, int left, int top, int wd, int ht);
void apply_crop(uhdr_raw_image_t* src, int left, int top, int wd, int ht, void* gl_ctxt = nullptr,
void* texture = nullptr);

} // namespace ultrahdr

Expand Down
39 changes: 32 additions & 7 deletions lib/include/ultrahdr/ultrahdrcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,28 @@ typedef struct uhdr_gainmap_metadata_ext : uhdr_gainmap_metadata {

#ifdef UHDR_ENABLE_GLES

typedef enum uhdr_effect_shader {
UHDR_MIR_HORZ,
UHDR_MIR_VERT,
UHDR_ROT_90,
UHDR_ROT_180,
UHDR_ROT_270,
UHDR_CROP,
UHDR_RESIZE,
} uhdr_effect_shader_t;

/**\brief OpenGL context */
typedef struct uhdr_opengl_ctxt {
// EGL Context
EGLDisplay mEGLDisplay; /**< EGL display connection */
EGLContext mEGLContext; /**< EGL rendering context */
EGLSurface mEGLSurface; /**< EGL surface for rendering */
EGLConfig mEGLConfig; /**< EGL frame buffer configuration */
EGLDisplay mEGLDisplay; /**< EGL display connection */
EGLContext mEGLContext; /**< EGL rendering context */
EGLSurface mEGLSurface; /**< EGL surface for rendering */
EGLConfig mEGLConfig; /**< EGL frame buffer configuration */

// GLES Context
GLuint mQuadVAO, mQuadVBO, mQuadEBO; /**< GL objects */

uhdr_error_info_t mErrorStatus; /**< Context status */
GLuint mQuadVAO, mQuadVBO, mQuadEBO; /**< GL objects */
GLuint mShaderProgram[UHDR_RESIZE + 1]; /**< Shader programs */
uhdr_error_info_t mErrorStatus; /**< Context status */

uhdr_opengl_ctxt();
~uhdr_opengl_ctxt();
Expand Down Expand Up @@ -260,6 +270,19 @@ typedef struct uhdr_opengl_ctxt {
*/
GLuint create_texture(uhdr_img_fmt_t fmt, int w, int h, void* data);

/*!\breif This method is used to read data from texture into a raw image
* NOTE: For any channel, this method assumes width and stride to be identical
*
* \param[in] texture texture_id
* \param[in] fmt image format
* \param[in] w image width
* \param[in] h image height
* \param[in] data image data
*
* \return none
*/
void read_texture(GLuint* texture, uhdr_img_fmt_t fmt, int w, int h, void* data);

/*!\brief This method is used to set up quad buffers and arrays
*
* \return none
Expand Down Expand Up @@ -296,6 +319,8 @@ typedef struct uhdr_opengl_ctxt {

} uhdr_opengl_ctxt_t; /**< alias for struct uhdr_opengl_ctxt */

bool isBufferDataContiguous(uhdr_raw_image_t* img);

#endif

} // namespace ultrahdr
Expand Down
48 changes: 44 additions & 4 deletions lib/src/editorhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,17 @@ uhdr_resize_effect::uhdr_resize_effect(int width, int height) : m_width{width},
}

std::unique_ptr<uhdr_raw_image_ext_t> apply_rotate(ultrahdr::uhdr_rotate_effect_t* desc,
uhdr_raw_image_t* src) {
uhdr_raw_image_t* src,
[[maybe_unused]] void* gl_ctxt,
[[maybe_unused]] void* texture) {
#ifdef UHDR_ENABLE_GLES
if ((src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888 ||
src->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat || src->fmt == UHDR_IMG_FMT_8bppYCbCr400) &&
isBufferDataContiguous(src) && gl_ctxt != nullptr) {
return apply_rotate_gles(desc, src, static_cast<ultrahdr::uhdr_opengl_ctxt*>(gl_ctxt),
static_cast<GLuint*>(texture));
}
#endif
std::unique_ptr<uhdr_raw_image_ext_t> dst;

if (desc->m_degree == 90 || desc->m_degree == 270) {
Expand Down Expand Up @@ -251,7 +261,17 @@ std::unique_ptr<uhdr_raw_image_ext_t> apply_rotate(ultrahdr::uhdr_rotate_effect_
}

std::unique_ptr<uhdr_raw_image_ext_t> apply_mirror(ultrahdr::uhdr_mirror_effect_t* desc,
uhdr_raw_image_t* src) {
uhdr_raw_image_t* src,
[[maybe_unused]] void* gl_ctxt,
[[maybe_unused]] void* texture) {
#ifdef UHDR_ENABLE_GLES
if ((src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888 ||
src->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat || src->fmt == UHDR_IMG_FMT_8bppYCbCr400) &&
isBufferDataContiguous(src) && gl_ctxt != nullptr) {
return apply_mirror_gles(desc, src, static_cast<ultrahdr::uhdr_opengl_ctxt*>(gl_ctxt),
static_cast<GLuint*>(texture));
}
#endif
std::unique_ptr<uhdr_raw_image_ext_t> dst = std::make_unique<uhdr_raw_image_ext_t>(
src->fmt, src->cg, src->ct, src->range, src->w, src->h, 64);

Expand Down Expand Up @@ -306,7 +326,17 @@ std::unique_ptr<uhdr_raw_image_ext_t> apply_mirror(ultrahdr::uhdr_mirror_effect_
return dst;
}

void apply_crop(uhdr_raw_image_t* src, int left, int top, int wd, int ht) {
void apply_crop(uhdr_raw_image_t* src, int left, int top, int wd, int ht,
[[maybe_unused]] void* gl_ctxt, [[maybe_unused]] void* texture) {
#ifdef UHDR_ENABLE_GLES
if ((src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888 ||
src->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat || src->fmt == UHDR_IMG_FMT_8bppYCbCr400) &&
isBufferDataContiguous(src) && gl_ctxt != nullptr) {
return apply_crop_gles(src, left, top, wd, ht,
static_cast<ultrahdr::uhdr_opengl_ctxt*>(gl_ctxt),
static_cast<GLuint*>(texture));
}
#endif
if (src->fmt == UHDR_IMG_FMT_24bppYCbCrP010) {
uint16_t* src_buffer = static_cast<uint16_t*>(src->planes[UHDR_PLANE_Y]);
src->planes[UHDR_PLANE_Y] = &src_buffer[top * src->stride[UHDR_PLANE_Y] + left];
Expand Down Expand Up @@ -344,7 +374,17 @@ void apply_crop(uhdr_raw_image_t* src, int left, int top, int wd, int ht) {
}

std::unique_ptr<uhdr_raw_image_ext_t> apply_resize(ultrahdr::uhdr_resize_effect_t* desc,
uhdr_raw_image_t* src, int dst_w, int dst_h) {
uhdr_raw_image_t* src, int dst_w, int dst_h,
[[maybe_unused]] void* gl_ctxt,
[[maybe_unused]] void* texture) {
#ifdef UHDR_ENABLE_GLES
if ((src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888 ||
src->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat || src->fmt == UHDR_IMG_FMT_8bppYCbCr400) &&
isBufferDataContiguous(src) && gl_ctxt != nullptr) {
return apply_resize_gles(src, dst_w, dst_h, static_cast<ultrahdr::uhdr_opengl_ctxt*>(gl_ctxt),
static_cast<GLuint*>(texture));
}
#endif
std::unique_ptr<uhdr_raw_image_ext_t> dst = std::make_unique<uhdr_raw_image_ext_t>(
src->fmt, src->cg, src->ct, src->range, dst_w, dst_h, 64);

Expand Down
4 changes: 2 additions & 2 deletions lib/src/gpu/applygainmap_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace ultrahdr {

static const std::string applyGainMapVertexShader = R"__SHADER__(#version 300 es
extern const std::string vertex_shader = R"__SHADER__(#version 300 es
precision highp float;
layout(location = 0) in vec4 aPos;
Expand Down Expand Up @@ -274,7 +274,7 @@ uhdr_error_info_t applyGainMapGLES(uhdr_raw_image_t* sdr_intent, uhdr_raw_image_
}

shaderProgram = opengl_ctxt->create_shader_program(
applyGainMapVertexShader.c_str(),
vertex_shader.c_str(),
getApplyGainMapFragmentShader(sdr_intent->fmt, gainmap_img->fmt, output_ct).c_str());
RET_IF_ERR()

Expand Down
Loading

0 comments on commit 4c56a21

Please sign in to comment.