imrove nanovg and vgcanvas

This commit is contained in:
lixianjing 2023-04-11 18:26:15 +08:00
parent b79f0bccae
commit ea492668c0
5 changed files with 34 additions and 7 deletions

View File

@ -127,6 +127,8 @@ struct NVGcontext {
float distTol;
float fringeWidth;
float devicePxRatio;
float windowWidth;
float windowHeight;
struct FONScontext* fs;
int fontImages[NVG_MAX_FONTIMAGES];
int fontImageIdx;
@ -175,6 +177,20 @@ static void nvg__deletePathCache(NVGpathCache* c)
free(c);
}
float nvgGetWidth(NVGcontext* ctx) {
if (ctx != NULL) {
return ctx->windowWidth;
}
return 0.0f;
}
float nvgGetHeight(NVGcontext* ctx) {
if (ctx != NULL) {
return ctx->windowHeight;
}
return 0.0f;
}
static NVGpathCache* nvg__allocPathCache(void)
{
NVGpathCache* c = (NVGpathCache*)malloc(sizeof(NVGpathCache));
@ -398,6 +414,8 @@ void nvgBeginFrameEx(NVGcontext* ctx, float windowWidth, float windowHeight, flo
nvgReset(ctx);
}
ctx->windowWidth = windowWidth;
ctx->windowHeight = windowHeight;
nvg__setDevicePixelRatio(ctx, devicePixelRatio);
ctx->params.renderViewport(ctx->params.userPtr, windowWidth, windowHeight, devicePixelRatio);

View File

@ -162,6 +162,9 @@ enum NVGorientation {
void nvgBeginFrame(NVGcontext* ctx, float windowWidth, float windowHeight, float devicePixelRatio, enum NVGorientation orientation);
void nvgBeginFrameEx(NVGcontext* ctx, float windowWidth, float windowHeight, float devicePixelRatio, int reset, enum NVGorientation orientation);
float nvgGetHeight(NVGcontext* ctx);
float nvgGetWidth(NVGcontext* ctx);
// Cancels drawing the current frame.
void nvgCancelFrame(NVGcontext* ctx);

View File

@ -4,6 +4,7 @@
* 增离线画布缓存功能(不修改图片时跳过bsvg解析步骤使用bitmap绘制在线画布)(感谢高源提供补丁)
* 新增图片绘制方式目前仅支持scale、scale_auto(感谢高源提供补丁)
* demouiold svg栏目新增上述两个部分的用例(感谢高源提供补丁)
* 修复在OpenGL下旋转LCD同时嵌套调用离线画布导致绘图不正常的问题(感谢智明/高源提供补丁)
2023/04/06
* 增加函数str\_attach/wstr\_attach/wstr\_append\_int。

View File

@ -39,6 +39,8 @@ typedef struct _framebuffer_object_t {
bool_t init;
int online_fbo;
int offline_fbo;
float online_w;
float online_h;
/* 脱离默认的 OpenGL 绘图方法,使用用户自定义的绘图方法,例如用户自定义的着色器等等 */
bool_t custom_draw_model;
rect_t online_dirty_rect;

View File

@ -103,20 +103,20 @@ static ret_t vgcanvas_nanovg_bind_fbo(vgcanvas_t* vgcanvas, framebuffer_object_t
fbo->online_fbo = nvgluGetCurrFramebuffer();
fbo->online_dirty_rect = rect_init(vgcanvas->dirty_rect.x, vgcanvas->dirty_rect.y, vgcanvas->dirty_rect.w, vgcanvas->dirty_rect.h);
fbo->online_w = nvgGetWidth(vg);
fbo->online_h = nvgGetHeight(vg);
handle->fbo = fbo->offline_fbo;
vgcanvas->dirty_rect = rect_init(0, 0, fbo->w, fbo->h);
nvgluBindFramebuffer(handle);
if (!fbo->custom_draw_model) {
glViewport(0, 0, fbo->w * fbo->ratio, fbo->h * fbo->ratio);
if (!fbo->init) {
glScissor(0, 0, fbo->w * fbo->ratio, fbo->h * fbo->ratio);
glEnable(GL_SCISSOR_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);
fbo->init = TRUE;
}
nvgBeginFrameEx(vg, fbo->w, fbo->h, fbo->ratio, FALSE, vgcanvas_nanovg_lcd_orientation_to_NVGorientation(info->lcd_orientation));
nvgBeginFrameEx(vg, fbo->w, fbo->h, fbo->ratio, FALSE, NVG_ORIENTATION_0);
nvgSave(vg);
nvgReset(vg);
}
@ -125,6 +125,7 @@ static ret_t vgcanvas_nanovg_bind_fbo(vgcanvas_t* vgcanvas, framebuffer_object_t
}
static ret_t vgcanvas_nanovg_unbind_fbo(vgcanvas_t* vgcanvas, framebuffer_object_t* fbo) {
uint32_t w, h;
NVGcontext* vg = NULL;
system_info_t* info = system_info();
vgcanvas_nanovg_t* canvas = (vgcanvas_nanovg_t*)vgcanvas;
@ -141,10 +142,12 @@ static ret_t vgcanvas_nanovg_unbind_fbo(vgcanvas_t* vgcanvas, framebuffer_object
nvgluBindFramebuffer(handle);
vgcanvas->dirty_rect = rect_init(fbo->online_dirty_rect.x, fbo->online_dirty_rect.y, fbo->online_dirty_rect.w, fbo->online_dirty_rect.h);
glViewport(0, 0, vgcanvas->w * fbo->ratio, vgcanvas->h * fbo->ratio);
glScissor(0, 0, vgcanvas->w * fbo->ratio, vgcanvas->h * fbo->ratio);
w = fbo->online_w;
h = fbo->online_h;
glViewport(0, 0, w * fbo->ratio, h * fbo->ratio);
if (!fbo->custom_draw_model) {
nvgBeginFrameEx(vg, vgcanvas->w, vgcanvas->h, fbo->ratio, FALSE, vgcanvas_nanovg_lcd_orientation_to_NVGorientation(info->lcd_orientation));
nvgBeginFrameEx(vg, w, h, fbo->ratio, FALSE, NVG_ORIENTATION_0);
}
return RET_OK;
}