agge support alpha

This commit is contained in:
lixianjing 2019-12-31 18:18:57 -08:00
parent 9adcb2609e
commit caa5cc6133
9 changed files with 59 additions and 14 deletions

View File

@ -196,6 +196,22 @@ static int aggenvg__renderUpdateTexture(void* uptr, int image, int x, int y, int
return 1;
}
static int aggenvg__renderFindTexture(void* uptr, const void* data) {
int i = 0;
AGGENVGtexture* tex = NULL;
AGGENVGcontext* agge = (AGGENVGcontext*)uptr;
for (; i < agge->ntextures; i++) {
if (agge->textures[i].data == data) {
tex = &agge->textures[i];
break;
}
}
if (tex == NULL) {
return -1;
}
return tex->id;
}
static int aggenvg__renderGetTextureSize(void* uptr, int image, int* w, int* h) {
AGGENVGcontext* agge = (AGGENVGcontext*)uptr;
AGGENVGtexture* tex = aggenvg__findTexture(agge, image);
@ -254,34 +270,35 @@ void renderPaint(AGGENVGcontext* agge, NVGpaint* paint) {
if (paint->image > 0) {
float invxform[6];
AGGENVGtexture* tex = aggenvg__findTexture(agge, paint->image);
if(tex == NULL) return;
nvgTransformInverse(invxform, paint->xform);
switch (tex->type) {
case NVG_TEXTURE_RGBA: {
typedef agge::bitmap<agge::pixel32_rgba, agge::raw_bitmap> rgba_bitmap_t;
rgba_bitmap_t src(tex->width, tex->height, tex->width*4, (uint8_t*)(tex->data));
agge::nanovg_image_blender<PixelT, rgba_bitmap_t> color(&src, (float*)invxform);
agge::nanovg_image_blender<PixelT, rgba_bitmap_t> color(&src, (float*)invxform, paint->innerColor.a);
ren(surface, 0, ras, color, agge::winding<>());
break;
}
case NVG_TEXTURE_BGRA: {
typedef agge::bitmap<agge::pixel32_bgra, agge::raw_bitmap> bgra_bitmap_t;
bgra_bitmap_t src(tex->width, tex->height, tex->width*4, (uint8_t*)(tex->data));
agge::nanovg_image_blender<PixelT, bgra_bitmap_t> color(&src, (float*)invxform);
agge::nanovg_image_blender<PixelT, bgra_bitmap_t> color(&src, (float*)invxform, paint->innerColor.a);
ren(surface, 0, ras, color, agge::winding<>());
break;
}
case NVG_TEXTURE_BGR565: {
typedef agge::bitmap<agge::pixel16_bgr565, agge::raw_bitmap> bgr565_bitmap_t;
bgr565_bitmap_t src(tex->width, tex->height, tex->width*2, (uint8_t*)(tex->data));
agge::nanovg_image_blender<PixelT, bgr565_bitmap_t> color(&src, (float*)invxform);
agge::nanovg_image_blender<PixelT, bgr565_bitmap_t> color(&src, (float*)invxform, paint->innerColor.a);
ren(surface, 0, ras, color, agge::winding<>());
break;
}
case NVG_TEXTURE_RGB: {
typedef agge::bitmap<agge::pixel24_rgb, agge::raw_bitmap> rgb_bitmap_t;
rgb_bitmap_t src(tex->width, tex->height, tex->width*3, (uint8_t*)(tex->data));
agge::nanovg_image_blender<PixelT, rgb_bitmap_t> color(&src, (float*)invxform);
agge::nanovg_image_blender<PixelT, rgb_bitmap_t> color(&src, (float*)invxform, paint->innerColor.a);
ren(surface, 0, ras, color, agge::winding<>());
break;
}
@ -452,6 +469,7 @@ NVGcontext* nvgCreateAGGE(uint32_t w, uint32_t h, uint32_t stride, enum NVGtextu
params.setLineJoin = aggenvg__setLineJoin;
params.setLineCap = aggenvg__setLineCap;
params.renderCreate = aggenvg__renderCreate;
params.findTexture = aggenvg__renderFindTexture;
params.renderCreateTexture = aggenvg__renderCreateTexture;
params.renderDeleteTexture = aggenvg__renderDeleteTexture;
params.renderUpdateTexture = aggenvg__renderUpdateTexture;

View File

@ -13,6 +13,7 @@ class nanovg_image_blender {
public:
nanovg_image_blender(BitmapT* bitmap, float* matrix);
nanovg_image_blender(BitmapT* bitmap, float* matrix, float alpha);
bool get_pixel(float x, float y, pixel32_rgba& ref) const;
void operator()(pixel* pixels, int x, int y, count_t n) const;
@ -21,13 +22,21 @@ class nanovg_image_blender {
private:
BitmapT* _bitmap;
float* _matrix;
float _alpha;
int w;
int h;
};
template <typename PixelT, typename BitmapT>
inline nanovg_image_blender<PixelT, BitmapT>::nanovg_image_blender(BitmapT* bitmap, float* matrix)
: _bitmap(bitmap), _matrix(matrix) {
: _bitmap(bitmap), _matrix(matrix), _alpha(1.0f) {
this->w = bitmap->width();
this->h = bitmap->height();
}
template <typename PixelT, typename BitmapT>
inline nanovg_image_blender<PixelT, BitmapT>::nanovg_image_blender(BitmapT* bitmap, float* matrix, float alpha)
: _bitmap(bitmap), _matrix(matrix), _alpha(alpha) {
this->w = bitmap->width();
this->h = bitmap->height();
}
@ -80,6 +89,8 @@ inline bool nanovg_image_blender<PixelT, BitmapT>::get_pixel(float x, float y,
pixel_linear(p[0], p[2], oy - y1);
ref = p[0];
ref.a *= _alpha;
return true;
}

View File

@ -3202,4 +3202,12 @@ int nvgCreateImageRaw(NVGcontext* ctx, int w, int h, int format, int imageFlags,
return ctx->params.renderCreateTexture(ctx->params.userPtr, format, w, h, imageFlags, data);
}
int nvgFindTextureRaw(NVGcontext* ctx, const void* data)
{
if(ctx->params.findTexture != NULL) {
return ctx->params.findTexture(ctx->params.userPtr, data);
}
return -1;
}
// vim: ft=c nu noet ts=4

View File

@ -661,6 +661,7 @@ struct NVGparams {
void (*setLineJoin)(void* uptr, int lineJoin);
int (*renderCreate)(void* uptr);
int (*findTexture)(void* uptr, const void* data);
int (*renderCreateTexture)(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data);
int (*renderDeleteTexture)(void* uptr, int image);
int (*renderUpdateTexture)(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data);
@ -693,6 +694,8 @@ void nvgDebugDumpPathCache(NVGcontext* ctx);
NVGparams* nvgGetParams(NVGcontext* ctx);
int nvgCreateImageRaw(NVGcontext* ctx, int w, int h, int format, int imageFlags, const unsigned char* data);
int nvgFindTextureRaw(NVGcontext* ctx, const void* data);
#ifdef __cplusplus
}
#endif

View File

@ -20,6 +20,7 @@
*/
#include "tkc/mem.h"
#include "tkc/utils.h"
#include "base/idle.h"
#include "base/enums.h"
#include "base/window.h"

View File

@ -164,7 +164,7 @@ static ret_t lcd_mem_special_set_global_alpha(lcd_t* lcd, uint8_t alpha) {
lcd->global_alpha = alpha;
mem->global_alpha = alpha;
return vg->vt->set_global_alpha(vg, alpha / 0xff);
return vg->vt->set_global_alpha(vg, alpha / 255.0f);
}
static lcd_mem_t* lcd_mem_special_create_lcd_mem(wh_t w, wh_t h, bitmap_format_t fmt) {

View File

@ -32,6 +32,7 @@
#include <SDL.h>
#include <stdio.h>
#include "awtk_global.h"
#include "tkc/time_now.h"
#include "base/input_method.h"

View File

@ -382,7 +382,7 @@ static ret_t vgcanvas_nanovg_set_line_width(vgcanvas_t* vgcanvas, float_t value)
static ret_t vgcanvas_nanovg_set_global_alpha(vgcanvas_t* vgcanvas, float_t value) {
NVGcontext* vg = ((vgcanvas_nanovg_t*)vgcanvas)->vg;
vgcanvas->global_alpha = value;
nvgGlobalAlpha(vg, value);
return RET_OK;

View File

@ -96,9 +96,12 @@ static int vgcanvas_nanovg_ensure_image(vgcanvas_nanovg_t* canvas, bitmap_t* img
uint32_t bpp = bitmap_get_bpp(img);
if (img->flags & BITMAP_FLAG_TEXTURE) {
i = tk_pointer_to_int(img->specific);
return i;
if(img->specific != 0) {
i = nvgFindTextureRaw(canvas->vg, img->specific);
if(i > 0) {
return i;
}
}
}
switch (img->format) {
@ -136,13 +139,13 @@ static int vgcanvas_nanovg_ensure_image(vgcanvas_nanovg_t* canvas, bitmap_t* img
i = nvgCreateImageRaw(canvas->vg, img->w, img->h, f, NVG_IMAGE_NEAREST, data);
if (data != img_data) {
TKMEM_FREE(data);
}
// if (data != img_data) {
// TKMEM_FREE(data);
// }
if (i >= 0) {
img->flags |= BITMAP_FLAG_TEXTURE;
img->specific = tk_pointer_from_int(i);
img->specific = data;//tk_pointer_from_int(i);
image_manager_update_specific(image_manager(), img);
}
bitmap_unlock_buffer(img);