Skip to content

Commit

Permalink
improve nanovg_plus
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Nov 7, 2024
1 parent d845261 commit ea0b7bd
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion awtk_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
INPUT_ENGINE = complie_helper.get_value('INPUT_ENGINE', INPUT_ENGINE)

VGCANVAS = 'NANOVG'
# VGCANVAS='NANOVG_PLUS'
VGCANVAS='NANOVG_PLUS'
# VGCANVAS='CAIRO'

VGCANVAS = complie_helper.get_value('VGCANVAS', VGCANVAS)
Expand Down
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* 完善object_array(感谢兆坤提供补丁)
* 完善tk_atob(感谢兆坤提供补丁)
* 统一是否可从名字中获取index的判断逻辑(感谢兆坤提供补丁)
* 完善nanovg_plus,设置为默认渲染引擎(感谢智明提供补丁)

2024/11/06
* 完善编译脚本。
Expand Down
37 changes: 35 additions & 2 deletions src/vgcanvas/vgcanvas_nanovg_gl_common.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#endif

#include "base/system_info.h"
#include "base/vgcanvas_asset_manager.h"

#if defined(WITH_NANOVG_GL3)
#define NANOVG_GL3_IMPLEMENTATION
Expand All @@ -41,31 +42,49 @@
#define NANOVG_GLES3_IMPLEMENTATION
#endif

#ifdef WITH_NANOVG_PLUS_GPU
#include "nanovg_plus.h"
#else
#include "nanovg.h"
#endif

#include "tkc/utf8.h"
#include "tkc/mem.h"
#include "base/opengl.h"
#include "base/vgcanvas.h"
#include "base/image_manager.h"
#include "base/native_window.h"

#ifdef WITH_NANOVG_PLUS_GPU
#include "nanovg_plus_gl.h"
#define vgcanvas_nanovg_init vgcanvas_nanovg_plus_init
#else
#include "nanovg_gl.h"
#include "nanovg_gl_utils.h"
#endif

typedef struct _vgcanvas_nanovg_t {
vgcanvas_t base;

int font_id;
#ifdef WITH_NANOVG_PLUS_GPU
nvgp_context_t* vg;
#else
NVGcontext* vg;
#endif
uint32_t text_align_v;
uint32_t text_align_h;

native_window_t* window;
} vgcanvas_nanovg_t;

#ifdef WITH_NANOVG_PLUS_GPU
#include "vgcanvas_nanovg_plus.h"
#else
#include "texture.inc"
#include "vgcanvas_nanovg_gl.inc"
#include "vgcanvas_asset_manager_nanovg.inc"
#endif

vgcanvas_t* VGCANVAS_CREATE_GL(uint32_t w, uint32_t h, uint32_t stride, bitmap_format_t format,
void* win) {
Expand All @@ -79,15 +98,22 @@ vgcanvas_t* VGCANVAS_CREATE_GL(uint32_t w, uint32_t h, uint32_t stride, bitmap_f

nanovg->base.w = w;
nanovg->base.h = h;
nanovg->base.vt = &vt;
nanovg->window = window;
nanovg->base.ratio = info.ratio;

#if defined(WITH_NANOVG_PLUS_GPU)
nanovg->base.vt = vgcanvas_nanovg_plus_get_vt();
#else
nanovg->base.vt = &vt;
#endif

vgcanvas_nanovg_init((vgcanvas_t*)nanovg);

opengl_init();

#if defined(WITH_NANOVG_GL2)
#if defined(WITH_NANOVG_PLUS_GPU)
nanovg->vg = nvgp_create(NVGP_MODE_GPU, w, h);
#elif defined(WITH_NANOVG_GL2)
nanovg->vg = nvgCreateGL2(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
#elif defined(WITH_NANOVG_GL3)
nanovg->vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
Expand All @@ -101,8 +127,15 @@ vgcanvas_t* VGCANVAS_CREATE_GL(uint32_t w, uint32_t h, uint32_t stride, bitmap_f
assert(!"OpenGL is not supported!");
}

#if defined(WITH_NANOVG_PLUS_GPU)
vgcanvas_asset_manager_add_vg(vgcanvas_asset_manager(), &(nanovg->base),
vgcanvas_asset_manager_nanovg_plus_bitmap_destroy,
vgcanvas_asset_manager_nanovg_plus_font_destroy);
#else
vgcanvas_asset_manager_add_vg(vgcanvas_asset_manager(), &(nanovg->base),
vgcanvas_asset_manager_nanovg_bitmap_destroy,
vgcanvas_asset_manager_nanovg_font_destroy);
#endif

return &(nanovg->base);
}
7 changes: 5 additions & 2 deletions src/vgcanvas/vgcanvas_nanovg_plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "base/image_manager.h"
#include "base/native_window.h"

#include "base/opengl.h"
#include "nanovg_plus.h"
#include "nanovg_plus_gl.h"
#include "nanovg_plus_gl_utils.h"
Expand Down Expand Up @@ -276,7 +277,7 @@ static ret_t vgcanvas_nanovg_plus_destroy(vgcanvas_t* vgcanvas) {
return RET_OK;
}

static ret_t vgcanvas_asset_manager_nanovg_plus_font_destroy(void* vg, const char* font_name,
ret_t vgcanvas_asset_manager_nanovg_plus_font_destroy(void* vg, const char* font_name,
void* specific) {
int32_t id = tk_pointer_to_int(specific);
vgcanvas_nanovg_plus_t* canvas = (vgcanvas_nanovg_plus_t*)vg;
Expand All @@ -286,7 +287,7 @@ static ret_t vgcanvas_asset_manager_nanovg_plus_font_destroy(void* vg, const cha
return RET_OK;
}

static ret_t vgcanvas_asset_manager_nanovg_plus_bitmap_destroy(void* vg, void* specific) {
ret_t vgcanvas_asset_manager_nanovg_plus_bitmap_destroy(void* vg, void* specific) {
vgcanvas_nanovg_plus_t* canvas = (vgcanvas_nanovg_plus_t*)vg;
vgcanvas_nanovg_plus_gl_texture_t* texture = (vgcanvas_nanovg_plus_gl_texture_t*)specific;
if (canvas != NULL && canvas->vg != NULL && texture != NULL) {
Expand Down Expand Up @@ -320,6 +321,8 @@ vgcanvas_t* vgcanvas_create(uint32_t w, uint32_t h, uint32_t stride, bitmap_form
nanovg->base.ratio = info.ratio;

vgcanvas_nanovg_plus_init((vgcanvas_t*)nanovg);

opengl_init();

nanovg->vg = nvgp_create(NVGP_MODE_GPU, w, h);

Expand Down
8 changes: 8 additions & 0 deletions src/vgcanvas/vgcanvas_nanovg_plus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef TK_VGCANVAS_NANOVG_PLUS_H
#define TK_VGCANVAS_NANOVG_PLUS_H
/* internal */
ret_t vgcanvas_nanovg_plus_init(vgcanvas_t* vgcanvas);
const vgcanvas_vtable_t* vgcanvas_nanovg_plus_get_vt();
ret_t vgcanvas_asset_manager_nanovg_plus_font_destroy(void* vg, const char* font_name, void* specific);
ret_t vgcanvas_asset_manager_nanovg_plus_bitmap_destroy(void* vg, void* specific);
#endif
6 changes: 5 additions & 1 deletion src/vgcanvas/vgcanvas_nanovg_plus.inc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static ret_t vgcanvas_nanovg_plus_on_asset_events(void* ctx, event_t* e) {
return RET_OK;
}

static ret_t vgcanvas_nanovg_plus_init(vgcanvas_t* vgcanvas) {
ret_t vgcanvas_nanovg_plus_init(vgcanvas_t* vgcanvas) {
(void)vgcanvas;
return RET_OK;
}
Expand Down Expand Up @@ -800,3 +800,7 @@ static const vgcanvas_vtable_t vt = {
.fbo_to_bitmap = vgcanvas_nanovg_plus_fbo_to_bitmap,
.clear_cache = vgcanvas_nanovg_plus_clear_cache,
.destroy = vgcanvas_nanovg_plus_destroy};

const vgcanvas_vtable_t* vgcanvas_nanovg_plus_get_vt() {
return &vt;
}

0 comments on commit ea0b7bd

Please sign in to comment.