From c4b865796db4b09390ee426ce014b32802f398fc Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Tue, 9 Aug 2016 16:12:08 +0800 Subject: [PATCH 1/6] Implements composite operation for blending rendering between frames. This commit implements the `nvgGlobalCompositeOperation()` function to support blending between frames. All operations defined in HTML5 canvas API are supported. Also, it is possible to create custom composite operation by calling `nvgBlendFunc()` or `nvgBlendFuncSeparate()` functions. --- src/nanovg.c | 19 ++++++++++++++++++ src/nanovg.h | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ src/nanovg_gl.h | 42 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/nanovg.c b/src/nanovg.c index 43371ec..2ac3503 100644 --- a/src/nanovg.c +++ b/src/nanovg.c @@ -338,6 +338,25 @@ void nvgEndFrame(NVGcontext* ctx) } } +NVGcompositeOperation nvgBlendFunc(int sfactor, int dfactor) +{ + return nvgBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor); +} + +NVGcompositeOperation nvgBlendFuncSeparate(int srcRGB, int dstRGB, int srcAlpha, int dstAlpha) +{ + NVGcompositeOperation operation; + operation.srcRGB = srcRGB; + operation.dstRGB = dstRGB; + operation.srcAlpha = srcAlpha; + operation.dstAlpha = dstAlpha; + return operation; +} + +void nvgGlobalCompositeOperation(NVGcontext* ctx, NVGcompositeOperation op) { + ctx->params.renderCompositeOperation(ctx->params.userPtr, op); +} + NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b) { return nvgRGBA(r,g,b,255); diff --git a/src/nanovg.h b/src/nanovg.h index 603b8e3..3c2c4a4 100644 --- a/src/nanovg.h +++ b/src/nanovg.h @@ -83,6 +83,41 @@ enum NVGalign { NVG_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline. }; +enum NVGblendFactor { + NVG_ZERO = 1<<0, + NVG_ONE = 1<<1, + NVG_SRC_COLOR = 1<<2, + NVG_ONE_MINUS_SRC_COLOR = 1<<3, + NVG_DST_COLOR = 1<<4, + NVG_ONE_MINUS_DST_COLOR = 1<<5, + NVG_SRC_ALPHA = 1<<6, + NVG_ONE_MINUS_SRC_ALPHA = 1<<7, + NVG_DST_ALPHA = 1<<8, + NVG_ONE_MINUS_DST_ALPHA = 1<<9, + NVG_SRC_ALPHA_SATURATE = 1<<10, +}; + +struct NVGcompositeOperation { + int srcRGB; + int dstRGB; + int srcAlpha; + int dstAlpha; +}; +typedef struct NVGcompositeOperation NVGcompositeOperation; + +// Predefined composite operations. +#define NVG_SOURCE_OVER nvgBlendFunc(NVG_ONE, NVG_ONE_MINUS_SRC_ALPHA) +#define NVG_SOURCE_IN nvgBlendFunc(NVG_DST_ALPHA, NVG_ZERO) +#define NVG_SOURCE_OUT nvgBlendFunc(NVG_ONE_MINUS_DST_ALPHA, NVG_ZERO) +#define NVG_ATOP nvgBlendFunc(NVG_DST_ALPHA, NVG_ONE_MINUS_SRC_ALPHA) +#define NVG_DESTINATION_OVER nvgBlendFunc(NVG_ONE_MINUS_DST_ALPHA, NVG_ONE) +#define NVG_DESTINATION_IN nvgBlendFunc(NVG_ZERO, NVG_SRC_ALPHA) +#define NVG_DESTINATION_OUT nvgBlendFunc(NVG_ZERO, NVG_ONE_MINUS_SRC_ALPHA) +#define NVG_DESTINATION_ATOP nvgBlendFunc(NVG_ONE_MINUS_DST_ALPHA, NVG_SRC_ALPHA) +#define NVG_LIGHTER nvgBlendFunc(NVG_ONE, NVG_ONE) +#define NVG_COPY nvgBlendFunc(NVG_ONE, NVG_ZERO) +#define NVG_XOR nvgBlendFunc(NVG_ONE_MINUS_DST_ALPHA NVG_ONE_MINUS_SRC_ALPHA) + struct NVGglyphPosition { const char* str; // Position of the glyph in the input string. float x; // The x-coordinate of the logical glyph position. @@ -123,6 +158,22 @@ void nvgCancelFrame(NVGcontext* ctx); // Ends drawing flushing remaining render state. void nvgEndFrame(NVGcontext* ctx); +// +// Composite operation +// +// Composite operation in NanoVG works between frames. The default composite +// operation of NanoVG is NVG_SOURCE_OVER, and the value is reset whenever +// calling nvgBeginFrame(). + +// Creates a composite operation with custom pixel arithmetic. The parameters should be one of NVGblendFactor. +NVGcompositeOperation nvgBlendFunc(int sfactor, int dfactor); + +// Creates a composite operation with custom pixel arithmetic for RGB and alpha components separately. The parameters should be one of NVGblendFactor. +NVGcompositeOperation nvgBlendFuncSeparate(int srcRGB, int dstRGB, int srcAlpha, int dstAlpha); + +// Sets the composite operation for the current frame. This function should be called between nvgBeginFrame() and nvgEndFrame(). The default composite operation of NanoVG is NVG_SOURCE_OVER. +void nvgGlobalCompositeOperation(NVGcontext* ctx, NVGcompositeOperation op); + // // Color utils // @@ -590,6 +641,7 @@ struct NVGparams { int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h); void (*renderViewport)(void* uptr, int width, int height, float devicePixelRatio); void (*renderCancel)(void* uptr); + void (*renderCompositeOperation)(void* uptr, NVGcompositeOperation op); void (*renderFlush)(void* uptr); void (*renderFill)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, const float* bounds, const NVGpath* paths, int npaths); void (*renderStroke)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, float strokeWidth, const NVGpath* paths, int npaths); diff --git a/src/nanovg_gl.h b/src/nanovg_gl.h index 7580af1..b368319 100644 --- a/src/nanovg_gl.h +++ b/src/nanovg_gl.h @@ -219,6 +219,7 @@ struct GLNVGfragUniforms { typedef struct GLNVGfragUniforms GLNVGfragUniforms; struct GLNVGcontext { + NVGcompositeOperation compositeOperation; GLNVGshader shader; GLNVGtexture* textures; float view[2]; @@ -944,6 +945,7 @@ static void glnvg__setUniforms(GLNVGcontext* gl, int uniformOffset, int image) static void glnvg__renderViewport(void* uptr, int width, int height, float devicePixelRatio) { GLNVGcontext* gl = (GLNVGcontext*)uptr; + gl->compositeOperation = NVG_SOURCE_OVER; // resets composition gl->view[0] = (float)width; gl->view[1] = (float)height; } @@ -1072,6 +1074,43 @@ static void glnvg__renderCancel(void* uptr) { gl->nuniforms = 0; } +static GLenum glnvg_convertBlendFuncFactor(int factor) +{ + if (factor == NVG_ZERO) + return GL_ZERO; + if (factor == NVG_ONE) + return GL_ONE; + if (factor == NVG_SRC_COLOR) + return GL_SRC_COLOR; + if (factor == NVG_ONE_MINUS_SRC_COLOR) + return GL_ONE_MINUS_SRC_COLOR; + if (factor == NVG_DST_COLOR) + return GL_DST_COLOR; + if (factor == NVG_ONE_MINUS_DST_COLOR) + return GL_ONE_MINUS_DST_COLOR; + if (factor == NVG_SRC_ALPHA) + return GL_SRC_ALPHA; + if (factor == NVG_ONE_MINUS_SRC_ALPHA) + return GL_ONE_MINUS_SRC_ALPHA; + if (factor == NVG_DST_ALPHA) + return GL_DST_ALPHA; + if (factor == NVG_ONE_MINUS_DST_ALPHA) + return GL_ONE_MINUS_DST_ALPHA; + if (factor == NVG_SRC_ALPHA_SATURATE) + return GL_SRC_ALPHA_SATURATE; +} + +static void glnvg__blendCompositeOperation(NVGcompositeOperation operation) +{ + glBlendFuncSeparate(glnvg_convertBlendFuncFactor(operation.srcRGB), glnvg_convertBlendFuncFactor(operation.dstRGB), glnvg_convertBlendFuncFactor(operation.srcAlpha), glnvg_convertBlendFuncFactor(operation.dstAlpha)); +} + +static void glnvg__renderCompositeOperation(void* uptr, NVGcompositeOperation op) +{ + GLNVGcontext* gl = (GLNVGcontext*)uptr; + gl->compositeOperation = op; +} + static void glnvg__renderFlush(void* uptr) { GLNVGcontext* gl = (GLNVGcontext*)uptr; @@ -1082,7 +1121,7 @@ static void glnvg__renderFlush(void* uptr) // Setup require GL state. glUseProgram(gl->shader.prog); - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glnvg__blendCompositeOperation(gl->compositeOperation); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CCW); @@ -1473,6 +1512,7 @@ NVGcontext* nvgCreateGLES3(int flags) params.renderGetTextureSize = glnvg__renderGetTextureSize; params.renderViewport = glnvg__renderViewport; params.renderCancel = glnvg__renderCancel; + params.renderCompositeOperation = glnvg__renderCompositeOperation; params.renderFlush = glnvg__renderFlush; params.renderFill = glnvg__renderFill; params.renderStroke = glnvg__renderStroke; From 1bf25e6f674f8b161a44ab5137b03566a2b7bdc9 Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Wed, 10 Aug 2016 23:01:25 +0800 Subject: [PATCH 2/6] Improves the implementation of global composite operation. This commit updates the implmentation of global composite operation to follow the advices described at https://github.com/memononen/nanovg/pull/298/files/c4b865796db4b09390ee426ce014b32802f398fc#r74248556 --- src/nanovg.c | 127 ++++++++++++++++++++++++++++++++++++++---------- src/nanovg.h | 49 +++++++++---------- src/nanovg_gl.h | 17 ++----- 3 files changed, 129 insertions(+), 64 deletions(-) diff --git a/src/nanovg.c b/src/nanovg.c index 2ac3503..734e12d 100644 --- a/src/nanovg.c +++ b/src/nanovg.c @@ -66,6 +66,7 @@ enum NVGpointFlags }; struct NVGstate { + NVGcompositeOperationState compositeOperation; NVGpaint fill; NVGpaint stroke; float strokeWidth; @@ -203,6 +204,79 @@ static void nvg__setDevicePixelRatio(NVGcontext* ctx, float ratio) ctx->devicePxRatio = ratio; } +static NVGcompositeOperationState nvg__compositeOperationState(int op) +{ + int sfactor, dfactor; + + if (op == NVG_SOURCE_OVER) + { + sfactor = NVG_ONE; + dfactor = NVG_ONE_MINUS_SRC_ALPHA; + } + else if (op == NVG_SOURCE_IN) + { + sfactor = NVG_DST_ALPHA; + dfactor = NVG_ZERO; + } + else if (op == NVG_SOURCE_OUT) + { + sfactor = NVG_ONE_MINUS_DST_ALPHA; + dfactor = NVG_ZERO; + } + else if (op == NVG_ATOP) + { + sfactor = NVG_DST_ALPHA; + dfactor = NVG_ONE_MINUS_SRC_ALPHA; + } + else if (op == NVG_DESTINATION_OVER) + { + sfactor = NVG_ONE_MINUS_DST_ALPHA; + dfactor = NVG_ONE; + } + else if (op == NVG_DESTINATION_IN) + { + sfactor = NVG_ZERO; + dfactor = NVG_SRC_ALPHA; + } + else if (op == NVG_DESTINATION_OUT) + { + sfactor = NVG_ZERO; + dfactor = NVG_ONE_MINUS_SRC_ALPHA; + } + else if (op == NVG_DESTINATION_ATOP) + { + sfactor = NVG_ONE_MINUS_DST_ALPHA; + dfactor = NVG_SRC_ALPHA; + } + else if (op == NVG_LIGHTER) + { + sfactor = NVG_ONE; + dfactor = NVG_ONE; + } + else if (op == NVG_COPY) + { + sfactor = NVG_ONE; + dfactor = NVG_ZERO; + } + else if (op == NVG_XOR) + { + sfactor = NVG_ONE_MINUS_DST_ALPHA; + dfactor = NVG_ONE_MINUS_SRC_ALPHA; + } + + NVGcompositeOperationState state; + state.srcRGB = sfactor; + state.dstRGB = dfactor; + state.srcAlpha = sfactor; + state.dstAlpha = dfactor; + return state; +} + +static NVGstate* nvg__getState(NVGcontext* ctx) +{ + return &ctx->states[ctx->nstates-1]; +} + NVGcontext* nvgCreateInternal(NVGparams* params) { FONSparams fontParams; @@ -310,7 +384,8 @@ void nvgCancelFrame(NVGcontext* ctx) void nvgEndFrame(NVGcontext* ctx) { - ctx->params.renderFlush(ctx->params.userPtr); + NVGstate* state = nvg__getState(ctx); + ctx->params.renderFlush(ctx->params.userPtr, state->compositeOperation); if (ctx->fontImageIdx != 0) { int fontImage = ctx->fontImages[ctx->fontImageIdx]; int i, j, iw, ih; @@ -338,25 +413,6 @@ void nvgEndFrame(NVGcontext* ctx) } } -NVGcompositeOperation nvgBlendFunc(int sfactor, int dfactor) -{ - return nvgBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor); -} - -NVGcompositeOperation nvgBlendFuncSeparate(int srcRGB, int dstRGB, int srcAlpha, int dstAlpha) -{ - NVGcompositeOperation operation; - operation.srcRGB = srcRGB; - operation.dstRGB = dstRGB; - operation.srcAlpha = srcAlpha; - operation.dstAlpha = dstAlpha; - return operation; -} - -void nvgGlobalCompositeOperation(NVGcontext* ctx, NVGcompositeOperation op) { - ctx->params.renderCompositeOperation(ctx->params.userPtr, op); -} - NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b) { return nvgRGBA(r,g,b,255); @@ -452,12 +508,6 @@ NVGcolor nvgHSLA(float h, float s, float l, unsigned char a) return col; } - -static NVGstate* nvg__getState(NVGcontext* ctx) -{ - return &ctx->states[ctx->nstates-1]; -} - void nvgTransformIdentity(float* t) { t[0] = 1.0f; t[1] = 0.0f; @@ -590,6 +640,7 @@ void nvgReset(NVGcontext* ctx) nvg__setPaintColor(&state->fill, nvgRGBA(255,255,255,255)); nvg__setPaintColor(&state->stroke, nvgRGBA(0,0,0,255)); + state->compositeOperation = nvg__compositeOperationState(NVG_SOURCE_OVER); state->strokeWidth = 1.0f; state->miterLimit = 10.0f; state->lineCap = NVG_BUTT; @@ -958,6 +1009,30 @@ void nvgResetScissor(NVGcontext* ctx) state->scissor.extent[1] = -1.0f; } +// Global composite operation. +void nvgGlobalCompositeOperation(NVGcontext* ctx, int op) +{ + NVGstate* state = nvg__getState(ctx); + state->compositeOperation = nvg__compositeOperationState(op); +} + +void nvgGlobalCompositeBlendFunc(NVGcontext* ctx, int sfactor, int dfactor) +{ + nvgGlobalCompositeBlendFuncSeparate(ctx, sfactor, dfactor, sfactor, dfactor); +} + +void nvgGlobalCompositeBlendFuncSeparate(NVGcontext* ctx, int srcRGB, int dstRGB, int srcAlpha, int dstAlpha) +{ + NVGcompositeOperationState op; + op.srcRGB = srcRGB; + op.dstRGB = dstRGB; + op.srcAlpha = srcAlpha; + op.dstAlpha = dstAlpha; + + NVGstate* state = nvg__getState(ctx); + state->compositeOperation = op; +} + static int nvg__ptEquals(float x1, float y1, float x2, float y2, float tol) { float dx = x2 - x1; diff --git a/src/nanovg.h b/src/nanovg.h index 3c2c4a4..32c6837 100644 --- a/src/nanovg.h +++ b/src/nanovg.h @@ -97,26 +97,27 @@ enum NVGblendFactor { NVG_SRC_ALPHA_SATURATE = 1<<10, }; -struct NVGcompositeOperation { +struct NVGcompositeOperationState { int srcRGB; int dstRGB; int srcAlpha; int dstAlpha; }; -typedef struct NVGcompositeOperation NVGcompositeOperation; - -// Predefined composite operations. -#define NVG_SOURCE_OVER nvgBlendFunc(NVG_ONE, NVG_ONE_MINUS_SRC_ALPHA) -#define NVG_SOURCE_IN nvgBlendFunc(NVG_DST_ALPHA, NVG_ZERO) -#define NVG_SOURCE_OUT nvgBlendFunc(NVG_ONE_MINUS_DST_ALPHA, NVG_ZERO) -#define NVG_ATOP nvgBlendFunc(NVG_DST_ALPHA, NVG_ONE_MINUS_SRC_ALPHA) -#define NVG_DESTINATION_OVER nvgBlendFunc(NVG_ONE_MINUS_DST_ALPHA, NVG_ONE) -#define NVG_DESTINATION_IN nvgBlendFunc(NVG_ZERO, NVG_SRC_ALPHA) -#define NVG_DESTINATION_OUT nvgBlendFunc(NVG_ZERO, NVG_ONE_MINUS_SRC_ALPHA) -#define NVG_DESTINATION_ATOP nvgBlendFunc(NVG_ONE_MINUS_DST_ALPHA, NVG_SRC_ALPHA) -#define NVG_LIGHTER nvgBlendFunc(NVG_ONE, NVG_ONE) -#define NVG_COPY nvgBlendFunc(NVG_ONE, NVG_ZERO) -#define NVG_XOR nvgBlendFunc(NVG_ONE_MINUS_DST_ALPHA NVG_ONE_MINUS_SRC_ALPHA) +typedef struct NVGcompositeOperationState NVGcompositeOperationState; + +enum NVGcompositeOperation { + NVG_SOURCE_OVER, + NVG_SOURCE_IN, + NVG_SOURCE_OUT, + NVG_ATOP, + NVG_DESTINATION_OVER, + NVG_DESTINATION_IN, + NVG_DESTINATION_OUT, + NVG_DESTINATION_ATOP, + NVG_LIGHTER, + NVG_COPY, + NVG_XOR, +}; struct NVGglyphPosition { const char* str; // Position of the glyph in the input string. @@ -162,17 +163,16 @@ void nvgEndFrame(NVGcontext* ctx); // Composite operation // // Composite operation in NanoVG works between frames. The default composite -// operation of NanoVG is NVG_SOURCE_OVER, and the value is reset whenever -// calling nvgBeginFrame(). +// operation of NanoVG is NVG_SOURCE_OVER. -// Creates a composite operation with custom pixel arithmetic. The parameters should be one of NVGblendFactor. -NVGcompositeOperation nvgBlendFunc(int sfactor, int dfactor); +// Sets the composite operation. The op parameter should be one of NVGcompositeOperation. +void nvgGlobalCompositeOperation(NVGcontext* ctx, int op); -// Creates a composite operation with custom pixel arithmetic for RGB and alpha components separately. The parameters should be one of NVGblendFactor. -NVGcompositeOperation nvgBlendFuncSeparate(int srcRGB, int dstRGB, int srcAlpha, int dstAlpha); +// Sets the composite operation with custom pixel arithmetic. The parameters should be one of NVGblendFactor. +void nvgGlobalCompositeBlendFunc(NVGcontext* ctx, int sfactor, int dfactor); -// Sets the composite operation for the current frame. This function should be called between nvgBeginFrame() and nvgEndFrame(). The default composite operation of NanoVG is NVG_SOURCE_OVER. -void nvgGlobalCompositeOperation(NVGcontext* ctx, NVGcompositeOperation op); +// Sets the composite operation with custom pixel arithmetic for RGB and alpha components separately. The parameters should be one of NVGblendFactor. +void nvgGlobalCompositeBlendFuncSeparate(NVGcontext* ctx, int srcRGB, int dstRGB, int srcAlpha, int dstAlpha); // // Color utils @@ -641,8 +641,7 @@ struct NVGparams { int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h); void (*renderViewport)(void* uptr, int width, int height, float devicePixelRatio); void (*renderCancel)(void* uptr); - void (*renderCompositeOperation)(void* uptr, NVGcompositeOperation op); - void (*renderFlush)(void* uptr); + void (*renderFlush)(void* uptr, NVGcompositeOperationState compositeOperation); void (*renderFill)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, const float* bounds, const NVGpath* paths, int npaths); void (*renderStroke)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, float strokeWidth, const NVGpath* paths, int npaths); void (*renderTriangles)(void* uptr, NVGpaint* paint, NVGscissor* scissor, const NVGvertex* verts, int nverts); diff --git a/src/nanovg_gl.h b/src/nanovg_gl.h index b368319..fa3c992 100644 --- a/src/nanovg_gl.h +++ b/src/nanovg_gl.h @@ -219,7 +219,6 @@ struct GLNVGfragUniforms { typedef struct GLNVGfragUniforms GLNVGfragUniforms; struct GLNVGcontext { - NVGcompositeOperation compositeOperation; GLNVGshader shader; GLNVGtexture* textures; float view[2]; @@ -945,7 +944,6 @@ static void glnvg__setUniforms(GLNVGcontext* gl, int uniformOffset, int image) static void glnvg__renderViewport(void* uptr, int width, int height, float devicePixelRatio) { GLNVGcontext* gl = (GLNVGcontext*)uptr; - gl->compositeOperation = NVG_SOURCE_OVER; // resets composition gl->view[0] = (float)width; gl->view[1] = (float)height; } @@ -1100,18 +1098,12 @@ static GLenum glnvg_convertBlendFuncFactor(int factor) return GL_SRC_ALPHA_SATURATE; } -static void glnvg__blendCompositeOperation(NVGcompositeOperation operation) +static void glnvg__blendCompositeOperation(NVGcompositeOperationState op) { - glBlendFuncSeparate(glnvg_convertBlendFuncFactor(operation.srcRGB), glnvg_convertBlendFuncFactor(operation.dstRGB), glnvg_convertBlendFuncFactor(operation.srcAlpha), glnvg_convertBlendFuncFactor(operation.dstAlpha)); + glBlendFuncSeparate(glnvg_convertBlendFuncFactor(op.srcRGB), glnvg_convertBlendFuncFactor(op.dstRGB), glnvg_convertBlendFuncFactor(op.srcAlpha), glnvg_convertBlendFuncFactor(op.dstAlpha)); } -static void glnvg__renderCompositeOperation(void* uptr, NVGcompositeOperation op) -{ - GLNVGcontext* gl = (GLNVGcontext*)uptr; - gl->compositeOperation = op; -} - -static void glnvg__renderFlush(void* uptr) +static void glnvg__renderFlush(void* uptr, NVGcompositeOperationState compositeOperation) { GLNVGcontext* gl = (GLNVGcontext*)uptr; int i; @@ -1121,7 +1113,7 @@ static void glnvg__renderFlush(void* uptr) // Setup require GL state. glUseProgram(gl->shader.prog); - glnvg__blendCompositeOperation(gl->compositeOperation); + glnvg__blendCompositeOperation(compositeOperation); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CCW); @@ -1512,7 +1504,6 @@ NVGcontext* nvgCreateGLES3(int flags) params.renderGetTextureSize = glnvg__renderGetTextureSize; params.renderViewport = glnvg__renderViewport; params.renderCancel = glnvg__renderCancel; - params.renderCompositeOperation = glnvg__renderCompositeOperation; params.renderFlush = glnvg__renderFlush; params.renderFill = glnvg__renderFill; params.renderStroke = glnvg__renderStroke; From 1d856c1b0016a82c93516b7430e0e7167a2d14ee Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Thu, 11 Aug 2016 02:00:23 +0800 Subject: [PATCH 3/6] Changes the order of declaration. --- src/nanovg.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/nanovg.h b/src/nanovg.h index 32c6837..229aa63 100644 --- a/src/nanovg.h +++ b/src/nanovg.h @@ -97,14 +97,6 @@ enum NVGblendFactor { NVG_SRC_ALPHA_SATURATE = 1<<10, }; -struct NVGcompositeOperationState { - int srcRGB; - int dstRGB; - int srcAlpha; - int dstAlpha; -}; -typedef struct NVGcompositeOperationState NVGcompositeOperationState; - enum NVGcompositeOperation { NVG_SOURCE_OVER, NVG_SOURCE_IN, @@ -119,6 +111,14 @@ enum NVGcompositeOperation { NVG_XOR, }; +struct NVGcompositeOperationState { + int srcRGB; + int dstRGB; + int srcAlpha; + int dstAlpha; +}; +typedef struct NVGcompositeOperationState NVGcompositeOperationState; + struct NVGglyphPosition { const char* str; // Position of the glyph in the input string. float x; // The x-coordinate of the logical glyph position. From ba8272c5b54775786c0aebebab031b956796e071 Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Thu, 11 Aug 2016 02:06:53 +0800 Subject: [PATCH 4/6] Updates comments. --- src/nanovg.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/nanovg.h b/src/nanovg.h index 229aa63..2d790b7 100644 --- a/src/nanovg.h +++ b/src/nanovg.h @@ -79,8 +79,8 @@ enum NVGalign { // Vertical align NVG_ALIGN_TOP = 1<<3, // Align text vertically to top. NVG_ALIGN_MIDDLE = 1<<4, // Align text vertically to middle. - NVG_ALIGN_BOTTOM = 1<<5, // Align text vertically to bottom. - NVG_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline. + NVG_ALIGN_BOTTOM = 1<<5, // Align text vertically to bottom. + NVG_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline. }; enum NVGblendFactor { @@ -162,8 +162,9 @@ void nvgEndFrame(NVGcontext* ctx); // // Composite operation // -// Composite operation in NanoVG works between frames. The default composite -// operation of NanoVG is NVG_SOURCE_OVER. +// Composite operation in NanoVG is applied only when calling nvgEndFrame() +// instead of drawing API. The default composite operation of NanoVG is +// NVG_SOURCE_OVER. // Sets the composite operation. The op parameter should be one of NVGcompositeOperation. void nvgGlobalCompositeOperation(NVGcontext* ctx, int op); @@ -234,7 +235,7 @@ void nvgReset(NVGcontext* ctx); // Solid color is simply defined as a color value, different kinds of paints can be created // using nvgLinearGradient(), nvgBoxGradient(), nvgRadialGradient() and nvgImagePattern(). // -// Current render style can be saved and restored using nvgSave() and nvgRestore(). +// Current render style can be saved and restored using nvgSave() and nvgRestore(). // Sets current stroke style to a solid color. void nvgStrokeColor(NVGcontext* ctx, NVGcolor color); @@ -282,7 +283,7 @@ void nvgGlobalAlpha(NVGcontext* ctx, float alpha); // Apart from nvgResetTransform(), each transformation function first creates // specific transformation matrix and pre-multiplies the current transformation by it. // -// Current coordinate system (transformation) can be saved and restored using nvgSave() and nvgRestore(). +// Current coordinate system (transformation) can be saved and restored using nvgSave() and nvgRestore(). // Resets current transform to a identity matrix. void nvgResetTransform(NVGcontext* ctx); @@ -419,7 +420,7 @@ NVGpaint nvgImagePattern(NVGcontext* ctx, float ox, float oy, float ex, float ey // Scissoring // // Scissoring allows you to clip the rendering into a rectangle. This is useful for various -// user interface cases like rendering a text edit or a timeline. +// user interface cases like rendering a text edit or a timeline. // Sets the current scissor rectangle. // The scissor rectangle is transformed by the current transform. @@ -474,7 +475,7 @@ void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float rad // Closes current sub-path with a line segment. void nvgClosePath(NVGcontext* ctx); -// Sets the current sub-path winding, see NVGwinding and NVGsolidity. +// Sets the current sub-path winding, see NVGwinding and NVGsolidity. void nvgPathWinding(NVGcontext* ctx, int dir); // Creates new circle arc shaped sub-path. The arc center is at cx,cy, the arc radius is r, @@ -491,7 +492,7 @@ void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r // Creates new ellipse shaped sub-path. void nvgEllipse(NVGcontext* ctx, float cx, float cy, float rx, float ry); -// Creates new circle shaped sub-path. +// Creates new circle shaped sub-path. void nvgCircle(NVGcontext* ctx, float cx, float cy, float r); // Fills the current path with current fill style. @@ -554,7 +555,7 @@ void nvgFontBlur(NVGcontext* ctx, float blur); // Sets the letter spacing of current text style. void nvgTextLetterSpacing(NVGcontext* ctx, float spacing); -// Sets the proportional line height of current text style. The line height is specified as multiple of font size. +// Sets the proportional line height of current text style. The line height is specified as multiple of font size. void nvgTextLineHeight(NVGcontext* ctx, float lineHeight); // Sets the text align of current text style, see NVGalign for options. From 9490a8c18856485a9e07d7fd944023e806c6b3c7 Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Thu, 11 Aug 2016 02:10:57 +0800 Subject: [PATCH 5/6] Updates comment again. --- src/nanovg.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/nanovg.h b/src/nanovg.h index 2d790b7..648a449 100644 --- a/src/nanovg.h +++ b/src/nanovg.h @@ -162,9 +162,7 @@ void nvgEndFrame(NVGcontext* ctx); // // Composite operation // -// Composite operation in NanoVG is applied only when calling nvgEndFrame() -// instead of drawing API. The default composite operation of NanoVG is -// NVG_SOURCE_OVER. +// The default composite operation of NanoVG is NVG_SOURCE_OVER. // Sets the composite operation. The op parameter should be one of NVGcompositeOperation. void nvgGlobalCompositeOperation(NVGcontext* ctx, int op); From 09dc767e3f25ec97e7398c18eca6a4c6bb3763f5 Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Thu, 11 Aug 2016 02:39:31 +0800 Subject: [PATCH 6/6] Updates comment for composite operation. --- src/nanovg.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nanovg.h b/src/nanovg.h index 648a449..80b256d 100644 --- a/src/nanovg.h +++ b/src/nanovg.h @@ -162,7 +162,9 @@ void nvgEndFrame(NVGcontext* ctx); // // Composite operation // -// The default composite operation of NanoVG is NVG_SOURCE_OVER. +// The composite operations in NanoVG are modeled after HTML Canvas API, and +// the blend func is based on OpenGL (see corresponding manuals for more info). +// The colors in the blending state have premultiplied alpha. // Sets the composite operation. The op parameter should be one of NVGcompositeOperation. void nvgGlobalCompositeOperation(NVGcontext* ctx, int op);