From b761d1240c92571351f6c1ec1afaf93111d3efaf Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Sun, 26 Mar 2017 11:03:52 +0800 Subject: [PATCH] =?UTF-8?q?Allows=20to=20set=20view=20id=20of=20NanoVG?= =?UTF-8?q?=E2=80=99s=20framebuffer=20manually.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit changes the `nvgluCreateFramebuffer()` function to not assign a view id automatically as it would easily reach the limit of `uint8_t`. Instead, a new `nvgluSetViewFramebuffer()` function is created to allow users to assign the view id manually as mentioned in #1095. --- examples/common/nanovg/nanovg_bgfx.cpp | 51 ++++++++++++++++++-------- examples/common/nanovg/nanovg_bgfx.h | 3 ++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/examples/common/nanovg/nanovg_bgfx.cpp b/examples/common/nanovg/nanovg_bgfx.cpp index ad6f800e7..ffbf28891 100644 --- a/examples/common/nanovg/nanovg_bgfx.cpp +++ b/examples/common/nanovg/nanovg_bgfx.cpp @@ -1137,47 +1137,68 @@ bgfx::TextureHandle nvglImageHandle(NVGcontext* ctx, int image) return tex->id; } -NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, - int height, int imageFlags) { +NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, int height, int imageFlags, uint8_t viewId) +{ + NVGLUframebuffer* framebuffer = nvgluCreateFramebuffer(ctx, width, height, imageFlags); + if (framebuffer != nullptr) + { + nvgluSetViewFramebuffer(viewId, framebuffer); + } + return framebuffer; +} + +NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, int height, int imageFlags) +{ NVGLUframebuffer* framebuffer = new NVGLUframebuffer; framebuffer->ctx = ctx; framebuffer->image = nvgCreateImageRGBA(ctx, width, height, imageFlags | NVG_IMAGE_PREMULTIPLIED, NULL); bgfx::TextureHandle texture = nvglImageHandle(ctx, framebuffer->image); - if (!bgfx::isValid(texture)) { + if (!bgfx::isValid(texture)) + { nvgluDeleteFramebuffer(framebuffer); - return NULL; + return nullptr; } framebuffer->handle = bgfx::createFrameBuffer(1, &texture, false); if (!bgfx::isValid(framebuffer->handle)) { nvgluDeleteFramebuffer(framebuffer); - return NULL; + return nullptr; } - static uint8_t s_ViewId = 1; // may have a better way to assign new view id - framebuffer->viewId = s_ViewId++; - bgfx::setViewFrameBuffer(framebuffer->viewId, framebuffer->handle); - bgfx::setViewSeq(framebuffer->viewId, true); return framebuffer; } -void nvgluBindFramebuffer(NVGLUframebuffer* framebuffer) { - static NVGcontext* s_prevCtx = NULL; +void nvgluBindFramebuffer(NVGLUframebuffer* framebuffer) +{ + static NVGcontext* s_prevCtx = nullptr; static uint8_t s_prevViewId; - if (framebuffer != NULL) { + if (framebuffer != nullptr) + { s_prevCtx = framebuffer->ctx; s_prevViewId = nvgViewId(framebuffer->ctx); nvgViewId(framebuffer->ctx, framebuffer->viewId); - } else if (s_prevCtx != NULL) { + } else if (s_prevCtx != nullptr) { nvgViewId(s_prevCtx, s_prevViewId); } } -void nvgluDeleteFramebuffer(NVGLUframebuffer* framebuffer) { - if (framebuffer == NULL) +void nvgluDeleteFramebuffer(NVGLUframebuffer* framebuffer) +{ + if (framebuffer == nullptr) return; if (bgfx::isValid(framebuffer->handle)) + { bgfx::destroyFrameBuffer(framebuffer->handle); + } if (framebuffer->image > 0) + { nvgDeleteImage(framebuffer->ctx, framebuffer->image); + } delete framebuffer; } + +void nvgluSetViewFramebuffer(uint8_t viewId, NVGLUframebuffer* framebuffer) +{ + framebuffer->viewId = viewId; + bgfx::setViewFrameBuffer(viewId, framebuffer->handle); + bgfx::setViewSeq(viewId, true); +} diff --git a/examples/common/nanovg/nanovg_bgfx.h b/examples/common/nanovg/nanovg_bgfx.h index 100a3c22b..36ba0dd73 100644 --- a/examples/common/nanovg/nanovg_bgfx.h +++ b/examples/common/nanovg/nanovg_bgfx.h @@ -30,6 +30,7 @@ void nvgViewId(struct NVGcontext* ctx, unsigned char _viewId); // Example: // float scale = 2; // NVGLUframebuffer* fb = nvgluCreateFramebuffer(ctx, 100 * scale, 100 * scale, 0); +// nvgluSetViewFramebuffer(VIEW_ID, fb); // nvgluBindFramebuffer(fb); // nvgBeginFrame(ctx, 100, 100, scale); // // renders anything offscreen @@ -44,8 +45,10 @@ void nvgViewId(struct NVGcontext* ctx, unsigned char _viewId); // nvgFillPaint(ctx, paint); // nvgFill(ctx); // nvgEndFrame(ctx); +NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, int height, int imageFlags, uint8_t viewId); NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, int height, int imageFlags); void nvgluBindFramebuffer(NVGLUframebuffer* framebuffer); void nvgluDeleteFramebuffer(NVGLUframebuffer* framebuffer); +void nvgluSetViewFramebuffer(uint8_t viewId, NVGLUframebuffer* framebuffer); #endif // NANOVG_BGFX_H_HEADER_GUARD