From fb8210794735dce7148815d89b28f4cdfb71f5d0 Mon Sep 17 00:00:00 2001 From: H.G. Muller Date: Fri, 5 Oct 2012 10:16:47 +0200 Subject: [PATCH] Maintain in-memory copy of the board image As reading back from the xlib-surface proves incredibly slow (cairo probably converts the entire image from Xt format to cairo format when we set it as a source, even though we only want to read a few dozen pixels), we now keep an exact copy of the board image as a cairo bitmap in memory. This restores the speed of the animation, but unfortunately leads to awful code. --- xboard.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 56 insertions(+), 9 deletions(-) diff --git a/xboard.c b/xboard.c index 3041c88..49ab155 100644 --- a/xboard.c +++ b/xboard.c @@ -341,7 +341,7 @@ WindowPlacement wpTags; #define SOLID 0 #define OUTLINE 1 Boolean cairoAnimate = True; -static cairo_surface_t *csBoardWindow; +static cairo_surface_t *csBoardWindow, *csBoardBackup; static cairo_surface_t *pngPieceBitmaps[2][(int)BlackPawn]; // scaled pieces as used static cairo_surface_t *pngPieceBitmaps2[2][(int)BlackPawn+4]; // scaled pieces in store static cairo_surface_t *pngBoardBitmap[2]; @@ -2497,18 +2497,25 @@ do_flash_delay (unsigned long msec) } void -DrawBorder (int x, int y, int type) +DoDrawBorder (cairo_surface_t *cs, int x, int y, int type) { cairo_t *cr; DrawSeekOpen(); - cr = cairo_create(csBoardWindow); + cr = cairo_create(cs); cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE); cairo_rectangle(cr, x, y, squareSize+lineGap, squareSize+lineGap); SetPen(cr, lineGap, type == 1 ? appData.highlightSquareColor : appData.premoveHighlightColor, 0); cairo_stroke(cr); } +void +DrawBorder (int x, int y, int type) +{ + DoDrawBorder(csBoardWindow, x, y, type); + DoDrawBorder(csBoardBackup, x, y, type); +} + static int CutOutSquare (int x, int y, int *x0, int *y0, int kind) { @@ -2563,6 +2570,14 @@ BlankSquare (int x, int y, int color, ChessSquare piece, Drawable dest, int fac) cairo_rectangle (cr, x*fac, y*fac, squareSize, squareSize); cairo_fill (cr); cairo_destroy (cr); + if(fac) { + cr = cairo_create (csBoardBackup); + cairo_set_source_surface (cr, pngBoardBitmap[color], x*fac - x0, y*fac - y0); + cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE); + cairo_rectangle (cr, x*fac, y*fac, squareSize, squareSize); + cairo_fill (cr); + cairo_destroy (cr); + } } else XCopyArea(xDisplay, xpmBoardBitmap[color], dest, wlPieceGC, x0, y0, squareSize, squareSize, x*fac, y*fac); @@ -2733,6 +2748,10 @@ pngDrawPiece (ChessSquare piece, int square_color, int x, int y, Drawable dest) cairo_set_source_surface (cr, pngPieceBitmaps[kind][piece], x, y); cairo_paint(cr); cairo_destroy (cr); + cr = cairo_create (csBoardBackup); + cairo_set_source_surface (cr, pngPieceBitmaps[kind][piece], x, y); + cairo_paint(cr); + cairo_destroy (cr); } typedef void (*DrawFunc)(); @@ -2757,11 +2776,11 @@ ChooseDrawFunc () } void -DrawDot (int marker, int x, int y, int r) +DoDrawDot (int marker, int x, int y, int r, cairo_surface_t *cs) { cairo_t *cr; DrawSeekOpen(); - cr = cairo_create(csBoardWindow); + cr = cairo_create(cs); cairo_arc(cr, x+r/2, y+r/2, r/2, 0.0, 2*M_PI); if(appData.monoMode) { SetPen(cr, 2, marker == 2 ? "#000000" : "#FFFFFF", 0); @@ -2777,6 +2796,13 @@ DrawDot (int marker, int x, int y, int r) } void +DrawDot (int marker, int x, int y, int r) +{ + DoDrawDot(marker, x, y, r, csBoardWindow); + DoDrawDot(marker, x, y, r, csBoardBackup); +} + +void DrawOneSquare (int x, int y, ChessSquare piece, int square_color, int marker, char *string, int align) { // basic front-end board-draw function: takes care of everything that can be in square: // piece, background, coordinate/count, marker dot @@ -3007,7 +3033,10 @@ DrawSeekOpen () { int boardWidth = lineGap + BOARD_WIDTH * (squareSize + lineGap); int boardHeight = lineGap + BOARD_HEIGHT * (squareSize + lineGap); - if(!csBoardWindow) csBoardWindow = cairo_xlib_surface_create(xDisplay, xBoardWindow, DefaultVisual(xDisplay, 0), boardWidth, boardHeight); + if(!csBoardWindow) { + csBoardWindow = cairo_xlib_surface_create(xDisplay, xBoardWindow, DefaultVisual(xDisplay, 0), boardWidth, boardHeight); + csBoardBackup = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, boardWidth, boardHeight); + } } void @@ -3016,7 +3045,7 @@ DrawSeekClose () } void -DrawGrid() +DoDrawGrid(cairo_surface_t *cs) { /* draws a grid starting around Nx, Ny squares starting at x,y */ int i; @@ -3024,7 +3053,7 @@ DrawGrid() DrawSeekOpen(); /* get a cairo_t */ - cr = cairo_create (csBoardWindow); + cr = cairo_create (cs); cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE); SetPen(cr, lineGap, "#000000", 0); @@ -3043,6 +3072,13 @@ DrawGrid() return; } +void +DrawGrid() +{ + DoDrawGrid(csBoardWindow); + DoDrawGrid(csBoardBackup); +} + /* * event handler for redrawing the board */ @@ -4039,11 +4075,22 @@ void CopyRectangle (AnimNr anr, int srcBuf, int destBuf, int srcX, int srcY, int width, int height, int destX, int destY) { if(cairoAnimate) { - cairo_t *cr = cairo_create (c_animBufs[anr+destBuf]); + cairo_t *cr;// = cairo_create (c_animBufs[anr+destBuf]); + cr = cairo_create (c_animBufs[anr+destBuf]); + if(c_animBufs[anr+srcBuf] == csBoardWindow) + cairo_set_source_surface (cr, csBoardBackup, destX - srcX, destY - srcY); + else + cairo_set_source_surface (cr, c_animBufs[anr+srcBuf], destX - srcX, destY - srcY); + cairo_rectangle (cr, destX, destY, width, height); + cairo_fill (cr); + cairo_destroy (cr); + if(c_animBufs[anr+destBuf] == csBoardWindow) { + cr = cairo_create (csBoardBackup); // also draw to backup cairo_set_source_surface (cr, c_animBufs[anr+srcBuf], destX - srcX, destY - srcY); cairo_rectangle (cr, destX, destY, width, height); cairo_fill (cr); cairo_destroy (cr); + } } else XCopyArea(xDisplay, animBufs[anr+srcBuf], animBufs[anr+destBuf], animGCs[anr], srcX, srcY, width, height, destX, destY); -- 1.7.0.4