altos/draw: Add damage tracking
[fw/altos] / src / draw / ao_copy.c
1 /*
2  * Copyright © 2016 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
15 #include <ao_draw.h>
16 #include <ao_draw_int.h>
17
18 #define bound(val,max,other) do {               \
19                 if (val < 0) {                  \
20                         other += (typeof(other)) (-val);        \
21                         val = 0;                \
22                 }                               \
23                 if (val > max) {                \
24                         other -= (typeof(other)) (val - max);   \
25                         val = max;              \
26                 }                               \
27         } while (0)
28
29 #define bound2(a, max_a, b, max_b) do {         \
30                 bound(a, max_a, b);             \
31                 bound(b, max_b, a);             \
32         } while (0)
33
34 void
35 ao_copy(struct ao_bitmap        *dst,
36         int16_t                 dst_x,
37         int16_t                 dst_y,
38         int16_t                 width,
39         int16_t                 height,
40         const struct ao_bitmap  *src,
41         int16_t                 src_x,
42         int16_t                 src_y,
43         uint8_t                 rop)
44 {
45         int16_t         dst_x2 = dst_x + width, dst_y2 = dst_y + height;
46         int16_t         src_x2 = src_x + width, src_y2 = src_y + height;
47         uint8_t         reverse = 0;
48         uint8_t         upsidedown = 0;
49
50         bound2(dst_x, dst->width, src_x, src->width);
51         bound2(dst_x2, dst->width, src_x2, src->width);
52         bound2(dst_y, dst->height, src_y, src->height);
53         bound2(dst_y2, dst->height, src_y2, src->height);
54
55         if (dst == src) {
56                 reverse = (dst_x > src_x);
57                 upsidedown = (dst_y > src_y);
58         }
59
60         ao_damage(dst, dst_x, dst_y, dst_x2, dst_y2);
61
62         if (dst_x < dst_x2 && dst_y < dst_y2) {
63                 ao_blt(src->base + src_y * src->stride,
64                        src->stride,
65                        src_x,
66                        dst->base + dst_y * dst->stride,
67                        dst->stride,
68                        dst_x,
69                        dst_x2 - dst_x,
70                        dst_y2 - dst_y,
71                        rop,
72                        reverse,
73                        upsidedown);
74         }
75 }
76