altos/draw: Use damage for lco demo
[fw/altos] / src / draw / test-frame.c
1 /*
2  * Copyright © 2023 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #define IMAGE_SCALE     8
20 #define WIDTH   128
21 #define HEIGHT  64
22
23 #define IMAGE_WIDTH     (WIDTH * IMAGE_SCALE)
24 #define IMAGE_HEIGHT    (HEIGHT * IMAGE_SCALE)
25 #define IMAGE_STRIDE    ((IMAGE_WIDTH + 31) / 32)
26
27 #define DEFAULT_WIDTH   IMAGE_WIDTH
28 #define DEFAULT_HEIGHT  IMAGE_HEIGHT
29
30 #define PASS_SETUP
31
32 #include "frame.c"
33 #include <ao_draw.h>
34
35 #include <sys/ipc.h>
36 #include <sys/shm.h>
37 #include <X11/extensions/XShm.h>
38
39 #define STRIDE  ((WIDTH + 31) / 32)
40
41 static uint32_t bits[STRIDE * HEIGHT];
42
43 static struct ao_bitmap fb = {
44         .base = bits,
45         .stride = STRIDE,
46         .width = WIDTH,
47         .height = HEIGHT,
48         .damage = AO_BOX_INIT,
49 };
50
51 static XImage *shm_image;
52 static XImage *nonshm_image;
53 static XShmSegmentInfo shm_info;
54
55 void
56 Setup(Display *dpy, Window win)
57 {
58         (void) win;
59         if (XShmQueryExtension(dpy)) {
60                 shm_image = XShmCreateImage(dpy, visual, (unsigned) depth, ZPixmap, NULL, &shm_info, IMAGE_WIDTH, IMAGE_HEIGHT);
61                 shm_info.shmid = shmget(IPC_PRIVATE, (size_t) (shm_image->bytes_per_line * shm_image->height), IPC_CREAT|0777);
62                 shm_info.shmaddr = shm_image->data = shmat(shm_info.shmid, 0, 0);
63                 shm_info.readOnly = True;
64                 XShmAttach(dpy, &shm_info);
65         } else {
66                 nonshm_image = XCreateImage(dpy, visual, (unsigned) depth, ZPixmap, 0, NULL,
67                                      IMAGE_WIDTH, IMAGE_HEIGHT, 32, IMAGE_STRIDE * 4);
68                 nonshm_image->data = calloc((size_t) nonshm_image->bytes_per_line, (size_t) nonshm_image->height);
69         }
70 }
71
72 static void
73 DoDisplay(Display *dpy, Window win, GC gc)
74 {
75         int             ix, iy, ib;
76         int             dx, dy;
77         XImage          *image;
78         uint32_t        *w, *scan, d;
79         unsigned long   white = WhitePixel(dpy, screen);
80         unsigned long   black = BlackPixel(dpy, screen);
81
82         if (shm_image)
83                 image = shm_image;
84         else
85                 image = nonshm_image;
86
87         scan = bits + STRIDE * fb.damage.y1;
88         for (iy = fb.damage.y1; iy < fb.damage.y2; iy++) {
89                 w = scan;
90                 scan += STRIDE;
91                 for (ix = fb.damage.x1 & ~31; ix < fb.damage.x2; ix += 32) {
92                         d = *w++;
93                         for (ib = 0; ib < 32 && ix + ib < WIDTH; ib++) {
94                                 unsigned long p = d & 1 ? white : black;
95                                 d >>= 1;
96                                 for (dy = 0; dy < IMAGE_SCALE; dy++) {
97
98                                         for (dx = 0; dx < IMAGE_SCALE; dx++) {
99                                                 XPutPixel(image, (ix + ib) * IMAGE_SCALE + dx, iy * IMAGE_SCALE + dy, p);
100                                         }
101                                 }
102                         }
103                 }
104         }
105         if (shm_image)
106                 XShmPutImage(dpy, win, gc, image, 0, 0, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, False);
107         else
108                 XPutImage(dpy, win, gc, image, 0, 0, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
109         ao_damage_set_empty(&fb);
110 }