Browse Source

Fixed issue #80

- increased initial buffer sizes
- change all allocator patterns to over allocate by 1.5x
- fixes bug where nvgl back-end was not allocating +n as requested
shared-context
Mikko Mononen 11 years ago
parent
commit
b2d93429e5
2 changed files with 18 additions and 15 deletions
  1. +14
    -11
      src/nanovg.c
  2. +4
    -4
      src/nanovg_gl.h

+ 14
- 11
src/nanovg.c View File

@@ -25,7 +25,10 @@
#include "stb_image.c"


#define NVG_INIT_PATH_SIZE 256
#define NVG_INIT_COMMANDS_SIZE 256
#define NVG_INIT_POINTS_SIZE 128
#define NVG_INIT_PATHS_SIZE 16
#define NVG_INIT_VERTS_SIZE 256
#define NVG_MAX_STATES 32

#define NVG_KAPPA90 0.5522847493f // Lenght proportional to radius of a cubic bezier handle for 90deg arcs.
@@ -159,20 +162,20 @@ static struct NVGpathCache* nvg__allocPathCache()
if (c == NULL) goto error;
memset(c, 0, sizeof(struct NVGpathCache));

c->points = (struct NVGpoint*)malloc(sizeof(struct NVGpoint)*4);
c->points = (struct NVGpoint*)malloc(sizeof(struct NVGpoint)*NVG_INIT_POINTS_SIZE);
if (!c->points) goto error;
c->npoints = 0;
c->cpoints = 4;
c->cpoints = NVG_INIT_POINTS_SIZE;

c->paths = (struct NVGpath*)malloc(sizeof(struct NVGpath)*4);
c->paths = (struct NVGpath*)malloc(sizeof(struct NVGpath)*NVG_INIT_PATHS_SIZE);
if (!c->paths) goto error;
c->npaths = 0;
c->cpaths = 4;
c->cpaths = NVG_INIT_PATHS_SIZE;

c->verts = (struct NVGvertex*)malloc(sizeof(struct NVGvertex)*4);
c->verts = (struct NVGvertex*)malloc(sizeof(struct NVGvertex)*NVG_INIT_VERTS_SIZE);
if (!c->verts) goto error;
c->nverts = 0;
c->cverts = 4;
c->cverts = NVG_INIT_VERTS_SIZE;

return c;
error:
@@ -197,10 +200,10 @@ struct NVGcontext* nvgCreateInternal(struct NVGparams* params)

ctx->params = *params;

ctx->commands = (float*)malloc(sizeof(float)*NVG_INIT_PATH_SIZE);
ctx->commands = (float*)malloc(sizeof(float)*NVG_INIT_COMMANDS_SIZE);
if (!ctx->commands) goto error;
ctx->ncommands = 0;
ctx->ccommands = NVG_INIT_PATH_SIZE;
ctx->ccommands = NVG_INIT_COMMANDS_SIZE;

ctx->alphaBlend = NVG_STRAIGHT_ALPHA;

@@ -919,7 +922,7 @@ static void nvg__addPath(struct NVGcontext* ctx)
{
struct NVGpath* path;
if (ctx->cache->npaths+1 > ctx->cache->cpaths) {
ctx->cache->cpaths = (ctx->cache->cpaths == 0) ? 8 : (ctx->cache->cpaths*2);
ctx->cache->cpaths = ctx->cache->npaths+1 + ctx->cache->cpaths/2;
ctx->cache->paths = (struct NVGpath*)realloc(ctx->cache->paths, sizeof(struct NVGpath)*ctx->cache->cpaths);
if (ctx->cache->paths == NULL) return;
}
@@ -953,7 +956,7 @@ static void nvg__addPoint(struct NVGcontext* ctx, float x, float y, int flags)
}

if (ctx->cache->npoints+1 > ctx->cache->cpoints) {
ctx->cache->cpoints = (ctx->cache->cpoints == 0) ? 8 : (ctx->cache->cpoints*2);
ctx->cache->cpoints = ctx->cache->npoints+1 + ctx->cache->cpoints/2;
ctx->cache->points = (struct NVGpoint*)realloc(ctx->cache->points, sizeof(struct NVGpoint)*ctx->cache->cpoints);
if (ctx->cache->points == NULL) return;
}


+ 4
- 4
src/nanovg_gl.h View File

@@ -984,7 +984,7 @@ static struct GLNVGcall* glnvg__allocCall(struct GLNVGcontext* gl)
{
struct GLNVGcall* ret = NULL;
if (gl->ncalls+1 > gl->ccalls) {
gl->ccalls = gl->ccalls == 0 ? 32 : gl->ccalls * 2;
gl->ccalls = glnvg__maxi(gl->ncalls+1, 128) + gl->ccalls; // 1.5x Overallocate
gl->calls = (struct GLNVGcall*)realloc(gl->calls, sizeof(struct GLNVGcall) * gl->ccalls);
}
ret = &gl->calls[gl->ncalls++];
@@ -996,7 +996,7 @@ static int glnvg__allocPaths(struct GLNVGcontext* gl, int n)
{
int ret = 0;
if (gl->npaths+n > gl->cpaths) {
gl->cpaths = gl->cpaths == 0 ? glnvg__maxi(n, 32) : gl->cpaths * 2;
gl->cpaths = glnvg__maxi(gl->npaths + n, 128) + gl->cpaths; // 1.5x Overallocate
gl->paths = (struct GLNVGpath*)realloc(gl->paths, sizeof(struct GLNVGpath) * gl->cpaths);
}
ret = gl->npaths;
@@ -1008,7 +1008,7 @@ static int glnvg__allocVerts(struct GLNVGcontext* gl, int n)
{
int ret = 0;
if (gl->nverts+n > gl->cverts) {
gl->cverts = gl->cverts == 0 ? glnvg__maxi(n, 256) : gl->cverts * 2;
gl->cverts = glnvg__maxi(gl->nverts + n, 4096) + gl->cverts/2; // 1.5x Overallocate
gl->verts = (struct NVGvertex*)realloc(gl->verts, sizeof(struct NVGvertex) * gl->cverts);
}
ret = gl->nverts;
@@ -1020,7 +1020,7 @@ static int glnvg__allocFragUniforms(struct GLNVGcontext* gl, int n)
{
int ret = 0, structSize = gl->fragSize;
if (gl->nuniforms+n > gl->cuniforms) {
gl->cuniforms = gl->cuniforms == 0 ? glnvg__maxi(n, 32) : gl->cuniforms * 2;
gl->cuniforms = glnvg__maxi(gl->nuniforms+n, 128) + gl->cuniforms/2; // 1.5x Overallocate
gl->uniforms = (unsigned char*)realloc(gl->uniforms, gl->cuniforms * structSize);
}
ret = gl->nuniforms * structSize;


Loading…
Cancel
Save