From cac16f1324a338044c52334eaeccf3a5558b9cba Mon Sep 17 00:00:00 2001 From: Vincent Wei Date: Thu, 3 Jun 2021 18:33:31 +0800 Subject: [PATCH] optimize to avoid using malloc/free for blit_ctxt; reset filter and transform of pixman images --- src/include/newgal.h | 12 ++---- src/kernel/compsor-fallback.c | 2 +- src/newgal/blit.c | 54 +++++++++++---------------- src/newgal/blit_N.c | 17 +++------ src/newgal/drm/drmvideo.c | 2 +- src/newgal/stretch.c | 69 +++++++++++++---------------------- src/newgal/surface-cursor.c | 2 +- src/newgal/surface-shared.c | 10 ++--- src/newgal/surface.c | 10 +++-- src/newgal/video.c | 2 +- 10 files changed, 70 insertions(+), 110 deletions(-) diff --git a/src/include/newgal.h b/src/include/newgal.h index c15410c0..f20f63f1 100644 --- a/src/include/newgal.h +++ b/src/include/newgal.h @@ -216,7 +216,10 @@ typedef struct GAL_Surface { #ifdef _MGUSE_PIXMAN pixman_image_t *pix_img; /* The pixman image object for this surface */ - void *blit_ctxt; /* blitting context */ + pixman_image_t *msk_img; + uint16_t pix_op; + uint16_t pix_filter; + uint32_t pix_alpha_bits; #endif #if IS_COMPOSITING_SCHEMA @@ -818,13 +821,6 @@ static inline BOOL GAL_CheckPixmanFormats (GAL_Surface *src, GAL_Surface *dst) return FALSE; } -typedef struct GAL_BlittingContext { - uint16_t op; - uint16_t filter; - uint32_t alpha_bits; - pixman_image_t *msk_img; -} GAL_BlittingContext; - int GAL_SetupBlitting (GAL_Surface *src, GAL_Surface *dst, DWORD ops); int GAL_CleanupBlitting (GAL_Surface *src, GAL_Surface *dst); diff --git a/src/kernel/compsor-fallback.c b/src/kernel/compsor-fallback.c index f6b74ec3..e607366b 100644 --- a/src/kernel/compsor-fallback.c +++ b/src/kernel/compsor-fallback.c @@ -1303,7 +1303,7 @@ static void transit_to_layer (CompositorCtxt* ctxt, MG_Layer* to_layer) } } - _DBG_PRINTF ("Average time to composite the layers: %u (times: %u)\n", total_time_ms / total_times, total_times); + _DBG_PRINTF ("Average time to composite the layers: %ums (times: %u)\n", total_time_ms / total_times, total_times); } CompositorOps __mg_fallback_compositor = { diff --git a/src/newgal/blit.c b/src/newgal/blit.c index 2211d87e..418824f0 100644 --- a/src/newgal/blit.c +++ b/src/newgal/blit.c @@ -332,39 +332,37 @@ int GAL_CalculateBlit(GAL_Surface *surface) int GAL_SetupBlitting (GAL_Surface *src, GAL_Surface *dst, DWORD ops) { if (GAL_CheckPixmanFormats (src, dst)) { - GAL_BlittingContext* ctxt; - src->blit_ctxt = malloc (sizeof (GAL_BlittingContext)); - if (src->blit_ctxt == NULL) - return -1; - - ctxt = src->blit_ctxt; if ((src->flags & GAL_SRCALPHA) && src->format->alpha != GAL_ALPHA_OPAQUE) { - memset (&ctxt->alpha_bits, src->format->alpha, sizeof(uint32_t)); - ctxt->msk_img = pixman_image_create_bits_no_clear (PIXMAN_a8, 1, 1, - &ctxt->alpha_bits, 4); - if (ctxt->msk_img) - pixman_image_set_repeat (ctxt->msk_img, PIXMAN_REPEAT_NORMAL); + memset (&src->pix_alpha_bits, src->format->alpha, sizeof(uint32_t)); + src->msk_img = pixman_image_create_bits_no_clear (PIXMAN_a8, 1, 1, + &src->pix_alpha_bits, 4); + if (src->msk_img) + pixman_image_set_repeat (src->msk_img, PIXMAN_REPEAT_NORMAL); } else - ctxt->msk_img = NULL; + src->msk_img = NULL; ops &= COLOR_BLEND_FLAGS_MASK; if (ops == COLOR_BLEND_LEGACY) { if ((src->flags & GAL_SRCPIXELALPHA) && src->format->Amask && src != dst) { - ctxt->op = PIXMAN_OP_OVER; + src->pix_op = PIXMAN_OP_OVER; } else { - ctxt->op = PIXMAN_OP_SRC; + src->pix_op = PIXMAN_OP_SRC; } } else { - ctxt->op = ops; + src->pix_op = ops; } - if (ctxt->op > PIXMAN_OP_HSL_LUMINOSITY || - ctxt->op < PIXMAN_OP_CLEAR) { - ctxt->op = PIXMAN_OP_SRC; + if (src->pix_op > PIXMAN_OP_HSL_LUMINOSITY || + src->pix_op < PIXMAN_OP_CLEAR) { + src->pix_op = PIXMAN_OP_SRC; } + + /* reset transform and filter */ + pixman_image_set_transform (src->pix_img, NULL); + pixman_image_set_filter (src->pix_img, PIXMAN_FILTER_NEAREST, NULL, 0); } return 0; @@ -372,12 +370,9 @@ int GAL_SetupBlitting (GAL_Surface *src, GAL_Surface *dst, DWORD ops) int GAL_CleanupBlitting (GAL_Surface *src, GAL_Surface *dst) { - if (src->blit_ctxt) { - GAL_BlittingContext* ctxt = src->blit_ctxt; - if (ctxt->msk_img) - pixman_image_unref (ctxt->msk_img); - free (src->blit_ctxt); - src->blit_ctxt = NULL; + if (src->msk_img) { + pixman_image_unref (src->msk_img); + src->msk_img = NULL; } return 0; @@ -390,15 +385,8 @@ static int GAL_PixmanBlit (struct GAL_Surface *src, GAL_Rect *srcrect, pixman_image_t *msk_img; pixman_op_t op; - if (src->blit_ctxt) { - GAL_BlittingContext* ctxt = src->blit_ctxt; - msk_img = ctxt->msk_img; - op = (pixman_op_t)ctxt->op; - } - else { - msk_img = NULL; - op = PIXMAN_OP_SRC; - } + msk_img = src->msk_img; + op = (pixman_op_t)src->pix_op; _DBG_PRINTF ("srcrect: %d, %d, %dx%d; dstrect: %d, %d, %dx%d\n", srcrect->x, srcrect->y, srcrect->w, srcrect->h, diff --git a/src/newgal/blit_N.c b/src/newgal/blit_N.c index 346a1d48..068e9603 100644 --- a/src/newgal/blit_N.c +++ b/src/newgal/blit_N.c @@ -1524,12 +1524,12 @@ GAL_loblit GAL_CalculateBlitN(GAL_Surface *surface, int blit_index) } } else { /* Now the meat, choose the blitter we want */ - int a_need = 0; - if(dstfmt->Amask) + int a_need = 0; + if (dstfmt->Amask) a_need = srcfmt->Amask ? COPY_ALPHA : SET_ALPHA; table = normal_blit[srcfmt->BytesPerPixel-1]; - for ( which=0; table[which].srcR; ++which ) { - if ( srcfmt->Rmask == table[which].srcR && + for (which=0; table[which].srcR; ++which) { + if (srcfmt->Rmask == table[which].srcR && srcfmt->Gmask == table[which].srcG && srcfmt->Bmask == table[which].srcB && dstfmt->BytesPerPixel == table[which].dstbpp && @@ -1538,7 +1538,7 @@ GAL_loblit GAL_CalculateBlitN(GAL_Surface *surface, int blit_index) dstfmt->Bmask == table[which].dstB && (a_need & table[which].alpha) == a_need && (CPU_Flags()&table[which].cpu_flags) == - table[which].cpu_flags ) + table[which].cpu_flags) break; } sdata->aux_data = table[which].aux_data; @@ -1547,13 +1547,6 @@ GAL_loblit GAL_CalculateBlitN(GAL_Surface *surface, int blit_index) blitfun = BlitNtoNCopyAlpha; } -#ifdef DEBUG_ASM - if ( (blitfun == GAL_BlitNtoN) || (blitfun == GAL_BlitNto1) ) - fprintf(stderr, "Using C blit\n"); - else - fprintf(stderr, "Using optimized C blit\n"); -#endif /* DEBUG_ASM */ - return(blitfun); } diff --git a/src/newgal/drm/drmvideo.c b/src/newgal/drm/drmvideo.c index a9ef6221..b42d8b3a 100644 --- a/src/newgal/drm/drmvideo.c +++ b/src/newgal/drm/drmvideo.c @@ -796,7 +796,7 @@ static GAL_Surface* create_surface_from_buffer (_THIS, surface->pixels_off = 0; #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif #if IS_COMPOSITING_SCHEMA surface->shared_header = NULL; diff --git a/src/newgal/stretch.c b/src/newgal/stretch.c index f122c460..fc27f8ce 100644 --- a/src/newgal/stretch.c +++ b/src/newgal/stretch.c @@ -273,44 +273,38 @@ int GAL_SetupStretchBlit (GAL_Surface *src, GAL_Rect *srcrect, double rotation; pixman_f_transform_t ftransform; pixman_transform_t transform; - GAL_BlittingContext* ctxt; - src->blit_ctxt = malloc (sizeof (GAL_BlittingContext)); - if (src->blit_ctxt == NULL) - return -1; - - ctxt = src->blit_ctxt; if ((src->flags & GAL_SRCALPHA) && src->format->alpha != GAL_ALPHA_OPAQUE) { - memset (&ctxt->alpha_bits, src->format->alpha, sizeof(uint32_t)); - ctxt->msk_img = pixman_image_create_bits_no_clear (PIXMAN_a8, 1, 1, - &ctxt->alpha_bits, 4); - if (ctxt->msk_img) - pixman_image_set_repeat (ctxt->msk_img, PIXMAN_REPEAT_NORMAL); + memset (&src->pix_alpha_bits, src->format->alpha, sizeof(uint32_t)); + src->msk_img = pixman_image_create_bits_no_clear (PIXMAN_a8, 1, 1, + &src->pix_alpha_bits, 4); + if (src->msk_img) + pixman_image_set_repeat (src->msk_img, PIXMAN_REPEAT_NORMAL); } else - ctxt->msk_img = NULL; + src->msk_img = NULL; - ctxt->filter = ops >> SCALING_FILTER_SHIFT; - if (ctxt->filter > PIXMAN_FILTER_CONVOLUTION) { - ctxt->filter = PIXMAN_FILTER_FAST; + src->pix_filter = ops >> SCALING_FILTER_SHIFT; + if (src->pix_filter > PIXMAN_FILTER_CONVOLUTION) { + src->pix_filter = PIXMAN_FILTER_FAST; } ops &= COLOR_BLEND_FLAGS_MASK; if (ops == COLOR_BLEND_LEGACY) { if ((src->flags & GAL_SRCPIXELALPHA) && src->format->Amask && src != dst) { - ctxt->op = PIXMAN_OP_OVER; + src->pix_op = PIXMAN_OP_OVER; } else { - ctxt->op = PIXMAN_OP_SRC; + src->pix_op = PIXMAN_OP_SRC; } } else { - ctxt->op = ops; + src->pix_op = ops; } - if (ctxt->op > PIXMAN_OP_HSL_LUMINOSITY || - ctxt->op < PIXMAN_OP_CLEAR) { - ctxt->op = PIXMAN_OP_SRC; + if (src->pix_op > PIXMAN_OP_HSL_LUMINOSITY || + src->pix_op < PIXMAN_OP_CLEAR) { + src->pix_op = PIXMAN_OP_SRC; } pixman_f_transform_init_identity (&ftransform); @@ -350,7 +344,7 @@ int GAL_SetupStretchBlit (GAL_Surface *src, GAL_Rect *srcrect, pixman_image_set_filter (src_img, (pixman_filter_t)filter, NULL, 0); } #else - pixman_image_set_filter (src->pix_img, (pixman_filter_t)ctxt->filter, NULL, 0); + pixman_image_set_filter (src->pix_img, (pixman_filter_t)src->pix_filter, NULL, 0); #endif } @@ -359,21 +353,15 @@ int GAL_SetupStretchBlit (GAL_Surface *src, GAL_Rect *srcrect, int GAL_CleanupStretchBlit (GAL_Surface *src, GAL_Surface *dst) { - if (src->blit_ctxt) { - pixman_transform_t transform; - - GAL_BlittingContext* ctxt = src->blit_ctxt; - if (ctxt->msk_img) - pixman_image_unref (ctxt->msk_img); - free (src->blit_ctxt); - src->blit_ctxt = NULL; - - pixman_transform_init_identity (&transform); - pixman_image_set_transform (src->pix_img, &transform); - pixman_image_set_filter (src->pix_img, PIXMAN_FILTER_FAST, NULL, 0); - pixman_image_set_clip_region32 (dst->pix_img, NULL); + if (src->msk_img) { + pixman_image_unref (src->msk_img); + src->msk_img = NULL; } + pixman_image_set_transform (src->pix_img, NULL); + pixman_image_set_filter (src->pix_img, PIXMAN_FILTER_NEAREST, NULL, 0); + pixman_image_set_clip_region32 (dst->pix_img, NULL); + return 0; } @@ -390,15 +378,8 @@ int GAL_StretchBlt (GAL_Surface *src, GAL_Rect *srcrect, return GAL_StretchBltLegacy (src, srcrect, dst, dstrect, ops & COLOR_BLEND_FLAGS_MASK); - if (src->blit_ctxt) { - GAL_BlittingContext* ctxt = src->blit_ctxt; - msk_img = ctxt->msk_img; - op = (pixman_op_t)ctxt->op; - } - else { - msk_img = NULL; - op = PIXMAN_OP_SRC; - } + msk_img = src->msk_img; + op = (pixman_op_t)src->pix_op; _DBG_PRINTF ("srcrect: %d,%d, %dx%d; dstrect: %d,%d, %dx%d; cliprect: %d,%d, %dx%d\n", srcrect->x, srcrect->y, srcrect->w, srcrect->h, diff --git a/src/newgal/surface-cursor.c b/src/newgal/surface-cursor.c index 47348497..cce53df2 100644 --- a/src/newgal/surface-cursor.c +++ b/src/newgal/surface-cursor.c @@ -105,7 +105,7 @@ GAL_Surface * GAL_CreateCursorSurface (GAL_VideoDevice *video, surface->format_version = 0; #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif surface->shared_header = NULL; surface->dirty_info = NULL; diff --git a/src/newgal/surface-shared.c b/src/newgal/surface-shared.c index 6f30ba80..800d408d 100644 --- a/src/newgal/surface-shared.c +++ b/src/newgal/surface-shared.c @@ -138,7 +138,7 @@ GAL_Surface * GAL_CreateSharedRGBSurface (GAL_VideoDevice *video, surface->format_version = 0; #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif surface->shared_header = NULL; surface->dirty_info = NULL; @@ -278,7 +278,7 @@ error: #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif surface->shared_header = NULL; surface->dirty_info = NULL; @@ -328,7 +328,7 @@ void GAL_FreeSharedSurfaceData (GAL_Surface *surface) surface->pixels = NULL; #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif surface->shared_header = NULL; surface->dirty_info = NULL; @@ -424,7 +424,7 @@ GAL_Surface * GAL_AttachSharedRGBSurface (int fd, size_t map_size, surface->format_version = 0; #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif GAL_SetClipRect (surface, NULL); @@ -481,7 +481,7 @@ void GAL_DettachSharedSurfaceData (GAL_Surface *surface) surface->pixels = NULL; #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif surface->shared_header = NULL; surface->dirty_info = NULL; diff --git a/src/newgal/surface.c b/src/newgal/surface.c index fcdf9b49..d9804592 100644 --- a/src/newgal/surface.c +++ b/src/newgal/surface.c @@ -144,7 +144,7 @@ GAL_Surface * GAL_CreateRGBSurface (Uint32 flags, surface->format_version = 0; #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif #if IS_COMPOSITING_SCHEMA surface->shared_header = NULL; @@ -1870,13 +1870,15 @@ void GAL_FreeSurface (GAL_Surface *surface) #ifdef _MGUSE_PIXMAN if (surface->pix_img) { + _WRN_PRINTF ("There is not cleaned up blitting pixel image: %p\n", surface->pix_img); pixman_image_unref (surface->pix_img); surface->pix_img = NULL; - } - if (surface->blit_ctxt) { - _WRN_PRINTF ("There is not cleaned up blitting context: %p\n", surface->blit_ctxt); + if (surface->msk_img) { + _WRN_PRINTF ("There is not cleaned up blitting mask image: %p\n", surface->msk_img); + pixman_image_unref (surface->msk_img); + surface->msk_img = NULL; } #endif diff --git a/src/newgal/video.c b/src/newgal/video.c index 5f2dcc5d..3abbb7d5 100644 --- a/src/newgal/video.c +++ b/src/newgal/video.c @@ -1257,7 +1257,7 @@ static GAL_Surface *Slave_CreateSurface (GAL_VideoDevice *this, surface->format_version = 0; #ifdef _MGUSE_PIXMAN surface->pix_img = NULL; - surface->blit_ctxt = NULL; + surface->msk_img = NULL; #endif #if IS_COMPOSITING_SCHEMA surface->shared_header = NULL;