Mercurial > MadButterfly
annotate src/redraw_man.c @ 33:d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
- logical error in X_main.c.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 06 Aug 2008 00:40:04 +0800 |
parents | e06a4a667ce2 |
children | 581a03196093 |
rev | line source |
---|---|
12 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
4 #include <cairo.h> |
12 | 5 #include "mb_types.h" |
6 #include "shapes.h" | |
7 #include "tools.h" | |
8 #include "redraw_man.h" | |
9 | |
26
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
10 /* NOTE: bounding box should also consider width of stroke. |
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
11 */ |
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
12 |
12 | 13 #define OK 0 |
14 #define ERR -1 | |
15 | |
13 | 16 #define OFFSET(type, field) ((void *)&((type *)NULL)->field - (void *)NULL) |
17 #define SWAP(a, b, t) do { t c; c = a; a = b; b = c; } while(0) | |
18 | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
19 #ifdef UNITTEST |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
20 typedef struct _sh_dummy sh_dummy_t; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
21 |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
22 extern void sh_dummy_transform(shape_t *shape); |
20
74d3d5dc9aaa
rename XXX_draw() to XXX_fill()
Thinker K.F. Li <thinker@branda.to>
parents:
19
diff
changeset
|
23 extern void sh_dummy_fill(shape_t *, cairo_t *); |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
24 #endif /* UNITTEST */ |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
25 |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
26 /*! \brief Sort a list of element by a unsigned integer. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
27 * |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
28 * The result is in ascend order. The unsigned integers is |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
29 * at offset specified by 'off' from start address of elemnts. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
30 */ |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
31 static void _insert_sort(void **elms, int num, int off) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
32 int i, j; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
33 unsigned int val; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
34 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
35 for(i = 1; i < num; i++) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
36 val = *(unsigned int *)(elms[i] + off); |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
37 for(j = i; j > 0; j--) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
38 if(*(unsigned int *)(elms[j - 1] + off) <= val) |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
39 break; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
40 elms[j] = elms[j - 1]; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
41 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
42 elms[j] = elms[i]; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
43 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
44 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
45 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
46 static int extend_memblk(void **buf, int o_size, int n_size) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
47 void *new_buf; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
48 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
49 new_buf = realloc(*buf, n_size); |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
50 if(new_buf == NULL) |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
51 return ERR; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
52 |
30
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
53 *buf = new_buf; |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
54 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
55 return OK; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
56 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
57 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
58 static int add_dirty_geo(redraw_man_t *rdman, geo_t *geo) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
59 int max_dirty_geos; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
60 int r; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
61 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
62 if(rdman->n_dirty_geos >= rdman->max_dirty_geos) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
63 max_dirty_geos = rdman->n_geos; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
64 r = extend_memblk((void **)&rdman->dirty_geos, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
65 sizeof(geo_t *) * rdman->n_dirty_geos, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
66 sizeof(geo_t *) * max_dirty_geos); |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
67 if(r != OK) |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
68 return ERR; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
69 rdman->max_dirty_geos = max_dirty_geos; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
70 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
71 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
72 rdman->dirty_geos[rdman->n_dirty_geos++] = geo; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
73 return OK; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
74 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
75 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
76 static int add_dirty_area(redraw_man_t *rdman, area_t *area) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
77 int max_dirty_areas; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
78 int r; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
79 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
80 if(rdman->n_dirty_areas >= rdman->max_dirty_areas) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
81 /* every geo object and coord object can contribute 2 areas. */ |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
82 max_dirty_areas = (rdman->n_geos + rdman->n_coords) * 2; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
83 r = extend_memblk((void **)&rdman->dirty_areas, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
84 sizeof(area_t *) * rdman->n_dirty_areas, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
85 sizeof(area_t *) * max_dirty_areas); |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
86 if(r != OK) |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
87 return ERR; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
88 rdman->max_dirty_areas = max_dirty_areas; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
89 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
90 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
91 rdman->dirty_areas[rdman->n_dirty_areas++] = area; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
92 return OK; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
93 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
94 |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
95 static void area_to_positions(area_t *area, co_aix (*poses)[2]) { |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
96 poses[0][0] = area->x; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
97 poses[0][1] = area->y; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
98 poses[1][0] = area->x + area->w; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
99 poses[1][1] = area->y + area->h;; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
100 } |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
101 |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
102 int redraw_man_init(redraw_man_t *rdman, cairo_t *cr, cairo_t *backend) { |
12 | 103 extern void redraw_man_destroy(redraw_man_t *rdman); |
104 | |
105 memset(rdman, 0, sizeof(redraw_man_t)); | |
106 | |
107 rdman->geo_pool = elmpool_new(sizeof(geo_t), 128); | |
108 if(rdman->geo_pool == NULL) | |
109 return ERR; | |
110 | |
111 rdman->coord_pool = elmpool_new(sizeof(coord_t), 16); | |
112 if(rdman->coord_pool == NULL) { | |
113 elmpool_free(rdman->geo_pool); | |
114 return ERR; | |
115 } | |
116 | |
18
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
117 rdman->shnode_pool = elmpool_new(sizeof(shnode_t), 16); |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
118 if(rdman->shnode_pool == NULL) { |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
119 elmpool_free(rdman->geo_pool); |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
120 elmpool_free(rdman->coord_pool); |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
121 return ERR; |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
122 } |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
123 |
12 | 124 rdman->root_coord = elmpool_elm_alloc(rdman->coord_pool); |
125 if(rdman->root_coord == NULL) | |
126 redraw_man_destroy(rdman); | |
13 | 127 rdman->n_coords = 1; |
12 | 128 coord_init(rdman->root_coord, NULL); |
129 | |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
130 rdman->cr = cr; |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
131 rdman->backend = backend; |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
132 |
12 | 133 return OK; |
134 } | |
135 | |
136 void redraw_man_destroy(redraw_man_t *rdman) { | |
137 elmpool_free(rdman->coord_pool); | |
138 elmpool_free(rdman->geo_pool); | |
18
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
139 elmpool_free(rdman->shnode_pool); |
13 | 140 if(rdman->dirty_coords) |
141 free(rdman->dirty_coords); | |
14 | 142 if(rdman->dirty_geos) |
143 free(rdman->dirty_geos); | |
30
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
144 if(rdman->gen_geos) |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
145 free(rdman->gen_geos); |
12 | 146 } |
147 | |
148 | |
149 #define ASSERT(x) | |
150 /* | |
151 * Change transformation matrix | |
152 * - update aggregated transformation matrix | |
153 * - of coord_t object been changed. | |
154 * - of children coord_t objects. | |
155 * - redraw members of coord_t objects. | |
156 * - redraw shape objects they are overlaid with members. | |
157 * - find out overlaid shape objects. | |
158 * - geo_t of a coord_t object | |
159 * - can make finding more efficiency. | |
160 * - fill overlay geo_t objects of members. | |
161 * | |
162 * Change a shape object | |
163 * - redraw changed object. | |
164 * - redraw shape objects they are overlaid with changed object. | |
165 * - find out overlaid shape objects. | |
166 * | |
167 * That coord and geo of shape objects are setted by user code | |
168 * give user code a chance to collect coord and geo objects together | |
169 * and gain interest of higher cache hit rate. | |
170 */ | |
171 | |
172 /*! \brief Find out all affected shape objects. | |
173 * | |
174 * Find out all shape objects that are overalid with geo_t of | |
175 * a geometry changed object. | |
176 * | |
177 * Linear scan geo_t objects of all shape objects in all_shapes | |
178 * list of a redraw_man_t object. | |
179 */ | |
180 int rdman_find_overlaid_shapes(redraw_man_t *rdman, geo_t *geo, | |
181 geo_t ***overlays) { | |
182 int n_geos; | |
183 geo_t **geos; | |
184 geo_t *geo_cur; | |
185 int n_overlays; | |
186 geo_t **_overlays; | |
187 int i; | |
188 | |
189 n_geos = rdman->n_geos; | |
190 | |
191 geos = (geo_t **)malloc(sizeof(geo_t *) * n_geos); | |
192 if(geos == NULL) | |
193 return -1; | |
194 | |
195 _overlays = (geo_t **)malloc(sizeof(geo_t *) * n_geos); | |
196 if(geos == NULL) { | |
197 free(geos); | |
198 return -1; | |
199 } | |
200 | |
201 geo_cur = STAILQ_HEAD(rdman->all_geos); | |
202 for(i = 0; i < n_geos; i++) { | |
203 geos[i] = geo_cur; | |
204 geo_cur = STAILQ_NEXT(geo_t, next, geo_cur); | |
205 } | |
206 geo_mark_overlay(geo, n_geos, geos, &n_overlays, _overlays); | |
207 | |
208 free(geos); | |
209 *overlays = _overlays; | |
210 | |
211 return n_overlays; | |
212 } | |
213 | |
214 int rdman_add_shape(redraw_man_t *rdman, shape_t *shape, coord_t *coord) { | |
215 geo_t *geo; | |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
216 #ifdef GEO_ORDER |
13 | 217 geo_t *visit; |
218 unsigned int next_order; | |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
219 #endif |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
220 int r; |
12 | 221 |
222 geo = elmpool_elm_alloc(rdman->geo_pool); | |
223 if(geo == NULL) | |
224 return ERR; | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
225 |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
226 geo_init(geo); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
227 |
12 | 228 sh_attach_geo(shape, geo); |
229 STAILQ_INS_TAIL(rdman->all_geos, geo_t, next, geo); | |
230 rdman->n_geos++; | |
13 | 231 |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
232 #ifdef GEO_ORDER |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
233 /* TODO: remove order number. */ |
13 | 234 geo->order = ++rdman->next_geo_order; |
235 if(geo->order == 0) { | |
236 next_order = 0; | |
237 for(visit = STAILQ_HEAD(rdman->all_geos); | |
238 visit != NULL; | |
239 visit = STAILQ_NEXT(geo_t, next, visit)) | |
240 visit->order = ++next_order; | |
241 rdman->next_geo_order = next_order; | |
242 } | |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
243 #endif |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
244 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
245 /* New one should be dirty to recompute it when drawing. */ |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
246 geo->flags |= GEF_DIRTY; |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
247 r = add_dirty_geo(rdman, geo); |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
248 if(r != OK) |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
249 return ERR; |
13 | 250 |
12 | 251 sh_attach_coord(shape, coord); |
252 | |
253 return OK; | |
254 } | |
255 | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
256 /*! \brief Remove a shape object from redraw manager. |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
257 * |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
258 * TODO: redraw shape objects that overlaid with removed one. |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
259 */ |
12 | 260 int rdman_remove_shape(redraw_man_t *rdman, shape_t *shape) { |
261 STAILQ_REMOVE(rdman->all_geos, geo_t, next, shape->geo); | |
262 elmpool_elm_free(rdman->geo_pool, shape->geo); | |
263 sh_detach_geo(shape); | |
264 rdman->n_geos--; | |
265 sh_detach_coord(shape); | |
266 return OK; | |
267 } | |
268 | |
269 coord_t *rdman_coord_new(redraw_man_t *rdman, coord_t *parent) { | |
13 | 270 coord_t *coord, *root_coord; |
271 coord_t *visit; | |
12 | 272 |
273 coord = elmpool_elm_alloc(rdman->coord_pool); | |
274 if(coord == NULL) | |
275 return NULL; | |
276 | |
277 coord_init(coord, parent); | |
13 | 278 rdman->n_coords++; |
279 | |
280 coord->order = ++rdman->next_coord_order; | |
281 if(coord->order == 0) { | |
282 rdman->next_coord_order = 0; | |
283 root_coord = visit = rdman->root_coord; | |
284 /* skip root coord. */ | |
285 visit = preorder_coord_subtree(root_coord, visit); | |
286 while(visit) { | |
287 visit->order = ++rdman->next_coord_order; | |
288 visit = preorder_coord_subtree(root_coord, visit); | |
289 } | |
290 } | |
12 | 291 |
292 return coord; | |
293 } | |
294 | |
295 /*! \brief Free a coord of a redraw_man_t object. | |
296 * | |
297 * \param coord is a coord_t without children and members. | |
298 * \return 0 for successful, -1 for error. | |
299 */ | |
300 int rdman_coord_free(redraw_man_t *rdman, coord_t *coord) { | |
301 coord_t *parent; | |
302 | |
303 parent = coord->parent; | |
304 if(parent == NULL) | |
305 return ERR; | |
306 | |
307 if(STAILQ_HEAD(coord->members) != NULL) | |
308 return ERR; | |
309 | |
310 if(STAILQ_HEAD(coord->children) != NULL) | |
311 return ERR; | |
312 | |
313 STAILQ_REMOVE(parent->children, coord_t, sibling, coord); | |
314 elmpool_elm_free(rdman->coord_pool, coord); | |
13 | 315 rdman->n_coords--; |
316 | |
317 return OK; | |
318 } | |
319 | |
320 /*! \brief Mark a coord is changed. | |
321 * | |
322 * A changed coord_t object is marked as dirty and put | |
323 * into dirty_coords list. | |
324 */ | |
325 int rdman_coord_changed(redraw_man_t *rdman, coord_t *coord) { | |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
326 coord_t *child; |
13 | 327 int max_dirty_coords; |
328 int r; | |
329 | |
330 if(coord->flags & COF_DIRTY) | |
331 return OK; | |
332 | |
333 if(rdman->n_dirty_coords >= rdman->max_dirty_coords) { | |
334 /* Max of dirty_coords is not big enough. */ | |
335 max_dirty_coords = rdman->max_dirty_coords + 16; | |
336 | |
337 r = extend_memblk((void **)&rdman->dirty_coords, | |
338 sizeof(coord_t *) * rdman->n_dirty_coords, | |
339 sizeof(coord_t *) * max_dirty_coords); | |
340 rdman->max_dirty_coords = max_dirty_coords; | |
341 } | |
342 | |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
343 /* Make the coord and child coords dirty. */ |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
344 for(child = coord; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
345 child != NULL; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
346 child = preorder_coord_subtree(coord, child)) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
347 rdman->dirty_coords[rdman->n_dirty_coords++] = coord; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
348 coord->flags |= COF_DIRTY; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
349 } |
13 | 350 |
351 return OK; | |
352 } | |
353 | |
18
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
354 static int _rdman_shape_changed(redraw_man_t *rdman, shape_t *shape) { |
13 | 355 geo_t *geo; |
356 int r; | |
357 | |
358 geo = shape->geo; | |
359 | |
360 if(geo->flags & GEF_DIRTY) | |
361 return OK; | |
362 | |
363 r = add_dirty_geo(rdman, geo); | |
364 if(r == ERR) | |
365 return ERR; | |
366 geo->flags |= GEF_DIRTY; | |
12 | 367 |
368 return OK; | |
369 } | |
370 | |
18
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
371 /*! \brief Mark a shape is changed. |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
372 * |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
373 * The geo_t object of a changed shape is mark as dirty and |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
374 * put into dirty_geos list. |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
375 */ |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
376 int rdman_shape_changed(redraw_man_t *rdman, shape_t *shape) { |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
377 return _rdman_shape_changed(rdman, shape); |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
378 } |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
379 |
29 | 380 int rdman_paint_changed(redraw_man_t *rdman, paint_t *paint) { |
381 shnode_t *node; | |
382 int r; | |
383 | |
384 for(node = STAILQ_HEAD(paint->members); | |
385 node != NULL; | |
386 node = STAILQ_NEXT(shnode_t, next, node)) { | |
387 r = _rdman_shape_changed(rdman, node->shape); | |
388 if(r != OK) | |
389 return ERR; | |
390 } | |
391 return OK; | |
392 } | |
393 | |
28 | 394 /* Clean dirties */ |
395 | |
396 static void clean_shape(shape_t *shape) { | |
397 switch(shape->sh_type) { | |
398 case SHT_PATH: | |
399 sh_path_transform(shape); | |
400 break; | |
401 case SHT_TEXT: | |
402 sh_text_transform(shape); | |
403 break; | |
404 #ifdef UNITTEST | |
405 default: | |
406 sh_dummy_transform(shape); | |
407 break; | |
408 #endif /* UNITTEST */ | |
409 } | |
410 shape->geo->flags &= ~GEF_DIRTY; | |
411 } | |
412 | |
413 static int clean_coord(coord_t *coord) { | |
414 shape_t *shape; | |
415 geo_t *geo; | |
416 co_aix (*poses)[2]; | |
417 int cnt, pos_cnt; | |
418 | |
419 compute_aggr_of_coord(coord); | |
420 | |
421 /* Clean member shapes. */ | |
422 cnt = 0; | |
423 for(shape = STAILQ_HEAD(coord->members); | |
424 shape != NULL; | |
425 shape = STAILQ_NEXT(shape_t, coord_mem_next, shape)) { | |
426 geo = shape->geo; | |
427 SWAP(geo->cur_area, geo->last_area, area_t *); | |
428 clean_shape(shape); | |
429 cnt++; | |
430 } | |
431 | |
432 /* Compute area of the coord. */ | |
433 poses = (co_aix (*)[2])malloc(sizeof(co_aix [2]) * 2 * cnt); | |
434 if(poses == NULL) | |
435 return ERR; | |
436 | |
437 pos_cnt = 0; | |
438 for(shape = STAILQ_HEAD(coord->members); | |
439 shape != NULL; | |
440 shape = STAILQ_NEXT(shape_t, coord_mem_next, shape)) { | |
441 geo = shape->geo; | |
442 | |
443 area_to_positions(geo->cur_area, poses + pos_cnt); | |
444 pos_cnt += 2; | |
445 } | |
446 | |
447 SWAP(coord->cur_area, coord->last_area, area_t *); | |
448 area_init(coord->cur_area, pos_cnt, poses); | |
449 free(poses); | |
450 | |
451 coord->flags &= ~COF_DIRTY; | |
452 | |
453 return OK; | |
454 } | |
455 | |
456 static int clean_rdman_coords(redraw_man_t *rdman) { | |
457 coord_t *coord; | |
458 coord_t **dirty_coords; | |
459 int n_dirty_coords; | |
460 int i, r; | |
461 | |
462 n_dirty_coords = rdman->n_dirty_coords; | |
463 if(n_dirty_coords > 0) { | |
464 dirty_coords = rdman->dirty_coords; | |
465 _insert_sort((void **)dirty_coords, n_dirty_coords, | |
466 OFFSET(coord_t, order)); | |
467 for(i = 0; i < n_dirty_coords; i++) { | |
468 coord = dirty_coords[i]; | |
469 if(!(coord->flags & COF_DIRTY)) | |
470 continue; | |
471 r = clean_coord(coord); | |
472 if(r != OK) | |
473 return ERR; | |
474 /* These two steps can be avoided for drawing all. */ | |
475 add_dirty_area(rdman, &coord->areas[0]); | |
476 add_dirty_area(rdman, &coord->areas[1]); | |
477 } | |
478 rdman->n_dirty_coords = 0; | |
479 } | |
480 return OK; | |
481 } | |
482 | |
483 static int clean_rdman_geos(redraw_man_t *rdman) { | |
484 int i; | |
485 int n_dirty_geos; | |
486 geo_t **dirty_geos; | |
487 geo_t *visit_geo; | |
488 | |
489 n_dirty_geos = rdman->n_dirty_geos; | |
490 if(n_dirty_geos > 0) { | |
491 dirty_geos = rdman->dirty_geos; | |
492 for(i = 0; i < n_dirty_geos; i++) { | |
493 visit_geo = dirty_geos[i]; | |
494 if(!(visit_geo->flags & GEF_DIRTY)) | |
495 continue; | |
496 | |
497 SWAP(visit_geo->cur_area, visit_geo->last_area, area_t *); | |
498 clean_shape(visit_geo->shape); | |
499 add_dirty_area(rdman, visit_geo->cur_area); | |
500 add_dirty_area(rdman, visit_geo->last_area); | |
501 } | |
502 rdman->n_dirty_geos = 0; | |
503 } | |
504 | |
505 return OK; | |
506 } | |
507 | |
29 | 508 static int clean_rdman_dirties(redraw_man_t *rdman) { |
509 int r; | |
510 | |
511 r = clean_rdman_coords(rdman); | |
512 if(r != OK) | |
513 return ERR; | |
514 | |
515 r = clean_rdman_geos(rdman); | |
516 if(r != OK) | |
517 return ERR; | |
518 | |
519 return OK; | |
520 } | |
521 | |
522 | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
523 /* Drawing and Redrawing |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
524 * ============================================================ |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
525 */ |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
526 |
26
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
527 #ifndef UNITTEST |
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
528 static void set_shape_stroke_param(shape_t *shape, cairo_t *cr) { |
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
529 cairo_set_line_width(cr, shape->stroke_width); |
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
530 } |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
531 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
532 static void fill_path_preserve(redraw_man_t *rdman) { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
533 cairo_fill_preserve(rdman->cr); |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
534 } |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
535 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
536 static void fill_path(redraw_man_t *rdman) { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
537 cairo_fill(rdman->cr); |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
538 } |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
539 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
540 static void stroke_path(redraw_man_t *rdman) { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
541 cairo_stroke(rdman->cr); |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
542 } |
26
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
543 #else |
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
544 static void set_shape_stroke_param(shape_t *shape, cairo_t *cr) { |
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
545 } |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
546 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
547 static void fill_path_preserve(redraw_man_t *rdman) { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
548 } |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
549 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
550 static void fill_path(redraw_man_t *rdman) { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
551 } |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
552 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
553 static void stroke_path(redraw_man_t *rdman) { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
554 } |
26
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
555 #endif |
d50f33040de6
Set line width for path.
Thinker K.F. Li <thinker@branda.to>
parents:
25
diff
changeset
|
556 |
22 | 557 static void draw_shape(redraw_man_t *rdman, shape_t *shape) { |
558 paint_t *fill, *stroke; | |
18
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
559 |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
560 if(shape->fill || shape->stroke) { |
19 | 561 switch(shape->sh_type) { |
562 case SHT_PATH: | |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
563 sh_path_draw(shape, rdman->cr); |
19 | 564 break; |
27 | 565 case SHT_TEXT: |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
566 sh_text_draw(shape, rdman->cr); |
27 | 567 break; |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
568 #ifdef UNITTEST |
19 | 569 default: |
20
74d3d5dc9aaa
rename XXX_draw() to XXX_fill()
Thinker K.F. Li <thinker@branda.to>
parents:
19
diff
changeset
|
570 sh_dummy_fill(shape, rdman->cr); |
19 | 571 break; |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
572 #endif /* UNITTEST */ |
19 | 573 } |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
574 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
575 fill = shape->fill; |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
576 if(shape->fill) { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
577 fill->prepare(fill, rdman->cr); |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
578 if(shape->stroke) |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
579 fill_path_preserve(rdman); |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
580 else |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
581 fill_path(rdman); |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
582 } |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
583 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
584 stroke = shape->stroke; |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
585 if(stroke) { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
586 stroke->prepare(stroke, rdman->cr); |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
587 set_shape_stroke_param(shape, rdman->cr); |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
588 stroke_path(rdman); |
22 | 589 } |
590 } | |
13 | 591 } |
592 | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
593 #ifndef UNITTEST |
25
29937c26bb01
Fix bug of a dark line appear when animating.
Thinker K.F. Li <thinker@branda.to>
parents:
24
diff
changeset
|
594 static void clean_canvas(cairo_t *cr) { |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
595 /* TODO: clean to background color. */ |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
596 cairo_set_source_rgb(cr, 0, 0, 0); |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
597 cairo_paint(cr); |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
598 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
599 |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
600 static void make_clip(cairo_t *cr, int n_dirty_areas, |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
601 area_t **dirty_areas) { |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
602 int i; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
603 area_t *area; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
604 |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
605 for(i = 0; i < n_dirty_areas; i++) { |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
606 area = dirty_areas[i]; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
607 cairo_rectangle(cr, area->x, area->y, area->w, area->h); |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
608 } |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
609 cairo_clip(cr); |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
610 } |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
611 |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
612 static void reset_clip(redraw_man_t *rdman) { |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
613 cairo_reset_clip(rdman->cr); |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
614 cairo_reset_clip(rdman->backend); |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
615 } |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
616 |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
617 static void copy_cr_2_backend(redraw_man_t *rdman, int n_dirty_areas, |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
618 area_t **dirty_areas) { |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
619 if(n_dirty_areas) |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
620 make_clip(rdman->backend, n_dirty_areas, dirty_areas); |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
621 |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
622 cairo_paint(rdman->backend); |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
623 } |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
624 #else /* UNITTEST */ |
25
29937c26bb01
Fix bug of a dark line appear when animating.
Thinker K.F. Li <thinker@branda.to>
parents:
24
diff
changeset
|
625 static void clean_canvas(cairo_t *cr) { |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
626 } |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
627 |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
628 static void reset_clip(redraw_man_t *rdman) { |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
629 } |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
630 |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
631 static void copy_cr_2_backend(redraw_man_t *rdman, int n_dirty_areas, |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
632 area_t **dirty_areas) { |
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
633 } |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
634 #endif /* UNITTEST */ |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
635 |
22 | 636 static void draw_shapes_in_areas(redraw_man_t *rdman, |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
637 int n_areas, |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
638 area_t **areas) { |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
639 geo_t *visit_geo; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
640 int i; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
641 |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
642 for(visit_geo = STAILQ_HEAD(rdman->all_geos); |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
643 visit_geo != NULL; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
644 visit_geo = STAILQ_NEXT(geo_t, next, visit_geo)) { |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
645 if(visit_geo->flags & GEF_DIRTY) |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
646 clean_shape(visit_geo->shape); |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
647 for(i = 0; i < n_areas; i++) { |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
648 if(is_overlay(visit_geo->cur_area, areas[i])) { |
22 | 649 draw_shape(rdman, visit_geo->shape); |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
650 break; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
651 } |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
652 } |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
653 } |
13 | 654 } |
655 | |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
656 |
13 | 657 /*! \brief Re-draw all changed shapes or shapes affected by changed coords. |
658 * | |
659 * A coord object has a geo to keep track the range that it's members will | |
660 * draw on. Geo of a coord should be recomputed when the coord is changed. | |
661 * Geo of a coord used to accelerate finding overlay shape objects of | |
662 * a specified geo. A coord object also must be recomputed when one of | |
663 * it's members is changed. | |
664 * | |
665 * New and old geo values of a coord object that is recomputed for | |
666 * changing of it-self must be used to find overlay shape objects. | |
667 * New and old geo values of a shape should also be used to find | |
668 * overlay shape objects, too. If a shape's coord is changed, shape's | |
669 * geo object is not used to find overlay shape objects any more. | |
670 * | |
671 * steps: | |
672 * - update chagned coord objects | |
14 | 673 * - recompute area for changed coord objects |
13 | 674 * - recompute geo for members shape objects |
14 | 675 * - clear dirty of geo for members to prevent from |
676 * recomputing for change of shape objects. | |
677 * - add old and new area value to list of dirty areas. | |
13 | 678 * - recompute geo for changed shape objects |
14 | 679 * - only if a shape object is dirty. |
680 * - put new and old value of area of geo to list of dirty areas. | |
681 * - Scan all shapes and redraw shapes overlaid with dirty areas. | |
13 | 682 * |
14 | 683 * dirty flag of coord objects is cleared after update. |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
684 * dirty flag of geo objects is also cleared after recomputing. |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
685 * Clean dirty flag can prevent redundant computing for geo and |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
686 * corod objects. |
14 | 687 * |
13 | 688 */ |
689 int rdman_redraw_changed(redraw_man_t *rdman) { | |
28 | 690 int r; |
13 | 691 int n_dirty_areas; |
14 | 692 area_t **dirty_areas; |
13 | 693 |
29 | 694 r = clean_rdman_dirties(rdman); |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
695 if(r != OK) |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
696 return ERR; |
13 | 697 |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
698 n_dirty_areas = rdman->n_dirty_areas; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
699 dirty_areas = rdman->dirty_areas; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
700 if(n_dirty_areas > 0) { |
25
29937c26bb01
Fix bug of a dark line appear when animating.
Thinker K.F. Li <thinker@branda.to>
parents:
24
diff
changeset
|
701 clean_canvas(rdman->cr); |
22 | 702 draw_shapes_in_areas(rdman, n_dirty_areas, dirty_areas); |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
703 copy_cr_2_backend(rdman, rdman->n_dirty_areas, rdman->dirty_areas); |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
704 rdman->n_dirty_areas = 0; |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
705 reset_clip(rdman); |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
706 } |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
707 rdman->n_dirty_areas = 0; |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
708 |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
709 return OK; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
710 } |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
711 |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
712 int rdman_redraw_all(redraw_man_t *rdman) { |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
713 geo_t *geo; |
29 | 714 int r; |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
715 |
29 | 716 r = clean_rdman_dirties(rdman); |
717 if(r != OK) | |
718 return ERR; | |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
719 |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
720 for(geo = STAILQ_HEAD(rdman->all_geos); |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
721 geo != NULL; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
14
diff
changeset
|
722 geo = STAILQ_NEXT(geo_t, next, geo)) { |
22 | 723 draw_shape(rdman, geo->shape); |
13 | 724 } |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
725 copy_cr_2_backend(rdman, 0, NULL); |
29 | 726 rdman->n_dirty_areas = 0; |
13 | 727 |
728 return OK; | |
12 | 729 } |
730 | |
30
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
731 int rdman_force_clean(redraw_man_t *rdman) { |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
732 int r; |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
733 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
734 r = clean_rdman_dirties(rdman); |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
735 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
736 return r; |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
737 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
29
diff
changeset
|
738 |
18
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
739 shnode_t *shnode_new(redraw_man_t *rdman, shape_t *shape) { |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
740 shnode_t *node; |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
741 |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
742 node = (shnode_t *)elmpool_elm_alloc(rdman->shnode_pool); |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
743 if(node) { |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
744 node->shape = shape; |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
745 node->next = NULL; |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
746 } |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
747 return node; |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
748 } |
0f3baa488a62
Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
17
diff
changeset
|
749 |
12 | 750 /* |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
751 * Dirty of geo |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
752 * A geo is dirty when any of the shape, size or positions is changed. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
753 * It's geo and positions should be recomputed before drawing. So, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
754 * dirty geos are marked as dirty and put into dirty_geos list. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
755 * The list is inspected before drawing to make sure the right shape, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
756 * size, and positions. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
757 * |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
758 * Dirty of coord |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
759 * A coord is dirty when it's transformation matrix being changed. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
760 * Dirty coords are marked as dirty and put into dirty_coords list. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
761 * Once a coord is dirty, every member geos of it are also dirty. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
762 * Because, their shape, size and positions will be changed. But, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
763 * they are not marked as dirty and put into dirty_geos list, since |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
764 * all these member geos will be recomputed for computing new current |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
765 * area of the coord. The changes of a coord also affect child |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
766 * coords. Once parent is dirty, all children are also dirty for |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
767 * their aggregate matrix out of date. Dirty coords should be |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
768 * clean in preorder of tree traversal. The dirty_coords list |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
769 * are sorted to keep the order before cleaning. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
770 * Whenever a coord is marked dirty and put into dirty_coords list, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
771 * all it's children should also be marked and put. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
772 * |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
773 * The procedure of clean coords comprises recomputing aggregate |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
774 * tranform matrix and area where members spreading in. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
775 * |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
776 * The list is inspected before drawing to recompute new shape, size, |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
777 * and positions of member geos of coords in the list. The drity |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
778 * flag of member geos will be clean. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
779 * |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
780 * Clean coords should be performed before clean geos, since clean |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
781 * coords will also clean member geos. |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
782 */ |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
783 |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
784 /* |
12 | 785 * When redraw an area, the affected elements may also extend to |
786 * outside of the area. Since the order of drawing will change | |
787 * the result, it will infect more and more elements to keep | |
788 * drawing order althrough they are overlaid directly with | |
789 * specified area. | |
790 * | |
791 * To fix the problem, we don't extend the set of redrawing to | |
792 * elements they are not overliad directly. The redrawing is | |
793 * performed on a temporary surface, clipped to fit the area, and | |
794 * update only specified area on the destinate surface. | |
795 */ | |
796 | |
797 /* | |
798 * To accelerate speed of transformation, when a matrix changed, | |
799 * transformation should be aggregated and computed in a loop. | |
800 * It can get intereset of higher hit rate of cache. | |
801 * - shapes prvoide list of positions needed to be transformed. | |
802 * - redraw_man transforms positions from shapes. | |
803 * - shapes drawing with result of transforms. | |
804 * - shapes should be called to give them a chance to update geometries. | |
805 */ | |
806 | |
13 | 807 /* |
808 * functions: | |
809 * - redraw all | |
810 * - redraw changed | |
811 */ | |
812 | |
12 | 813 #ifdef UNITTEST |
814 | |
815 #include <CUnit/Basic.h> | |
19 | 816 #include "paint.h" |
12 | 817 |
818 struct _sh_dummy { | |
819 shape_t shape; | |
820 co_aix x, y; | |
821 co_aix w, h; | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
822 int draw_cnt; |
12 | 823 }; |
824 | |
825 shape_t *sh_dummy_new(co_aix x, co_aix y, co_aix w, co_aix h) { | |
826 sh_dummy_t *dummy; | |
827 | |
828 dummy = (sh_dummy_t *)malloc(sizeof(sh_dummy_t)); | |
829 if(dummy == NULL) | |
830 return NULL; | |
831 | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
832 memset(dummy, 0, sizeof(sh_dummy_t)); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
833 |
12 | 834 dummy->x = x; |
835 dummy->y = y; | |
836 dummy->w = w; | |
837 dummy->h = h; | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
838 dummy->draw_cnt = 0; |
12 | 839 |
840 return (shape_t *)dummy; | |
841 } | |
842 | |
843 void sh_dummy_free(shape_t *sh) { | |
844 free(sh); | |
845 } | |
846 | |
847 void sh_dummy_transform(shape_t *shape) { | |
848 sh_dummy_t *dummy = (sh_dummy_t *)shape; | |
849 co_aix poses[2][2]; | |
850 co_aix x1, y1, x2, y2; | |
851 | |
852 if(shape->geo && shape->coord) { | |
853 x1 = dummy->x; | |
854 y1 = dummy->y; | |
855 x2 = x1 + dummy->w; | |
856 y2 = y1 + dummy->h; | |
857 | |
858 coord_trans_pos(shape->coord, &x1, &y1); | |
859 coord_trans_pos(shape->coord, &x2, &y2); | |
860 poses[0][0] = x1; | |
861 poses[0][1] = y1; | |
862 poses[1][0] = x2; | |
863 poses[1][1] = y2; | |
864 | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
865 if(shape->geo) |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
866 geo_from_positions(shape->geo, 2, poses); |
12 | 867 } |
868 } | |
869 | |
20
74d3d5dc9aaa
rename XXX_draw() to XXX_fill()
Thinker K.F. Li <thinker@branda.to>
parents:
19
diff
changeset
|
870 void sh_dummy_fill(shape_t *shape, cairo_t *cr) { |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
871 sh_dummy_t *dummy; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
872 |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
873 dummy = (sh_dummy_t *)shape; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
874 dummy->draw_cnt++; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
875 } |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
876 |
19 | 877 static void dummy_paint_prepare(paint_t *paint, cairo_t *cr) { |
878 } | |
879 | |
880 static void dummy_paint_free(paint_t *paint) { | |
881 if(paint) | |
882 free(paint); | |
883 } | |
884 | |
885 paint_t *dummy_paint_new(redraw_man_t *rdman) { | |
886 paint_t *paint; | |
887 | |
888 paint = (paint_t *)malloc(sizeof(paint_t)); | |
889 if(paint == NULL) | |
890 return NULL; | |
891 | |
892 paint_init(paint, dummy_paint_prepare, dummy_paint_free); | |
893 | |
894 return paint; | |
895 } | |
896 | |
12 | 897 void test_rdman_find_overlaid_shapes(void) { |
898 redraw_man_t rdman; | |
899 geo_t geo; | |
900 coord_t *coords[3]; | |
901 shape_t *shapes[5]; | |
902 geo_t **overlays; | |
13 | 903 co_aix pos[2][2]; |
12 | 904 int n; |
905 int i; | |
906 | |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
907 redraw_man_init(&rdman, NULL, NULL); |
12 | 908 coords[0] = rdman.root_coord; |
909 for(i = 1; i < 3; i++) { | |
910 coords[i] = rdman_coord_new(&rdman, rdman.root_coord); | |
911 } | |
912 for(i = 0; i < 5; i++) { | |
913 shapes[i] = sh_dummy_new(10 + i * 30, 10 + i * 20, 25, 15); | |
914 CU_ASSERT(shapes[i] != NULL); | |
915 } | |
916 for(i = 0; i < 3; i++) | |
917 rdman_add_shape(&rdman, shapes[i], coords[1]); | |
918 for(i = 3; i < 5; i++) | |
919 rdman_add_shape(&rdman, shapes[i], coords[2]); | |
920 | |
921 coords[1]->matrix[0] = 2; | |
922 coords[0]->matrix[4] = 2; | |
923 | |
924 update_aggr_matrix(coords[0]); | |
925 for(i = 0; i < 5; i++) | |
926 sh_dummy_transform(shapes[i]); | |
927 | |
13 | 928 pos[0][0] = 100; |
929 pos[0][1] = 120; | |
930 pos[1][0] = 100 + 140; | |
931 pos[1][1] = 120 + 40; | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
932 geo_init(&geo); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
933 geo_from_positions(&geo, 2, pos); |
12 | 934 |
935 n = rdman_find_overlaid_shapes(&rdman, &geo, &overlays); | |
936 CU_ASSERT(n == 2); | |
937 CU_ASSERT(overlays != NULL); | |
938 CU_ASSERT(overlays[0] == shapes[2]->geo); | |
939 CU_ASSERT(overlays[1] == shapes[3]->geo); | |
940 | |
941 free(overlays); | |
942 for(i = 0; i < 5; i++) | |
943 sh_dummy_free(shapes[i]); | |
944 | |
945 redraw_man_destroy(&rdman); | |
946 } | |
947 | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
948 void test_rdman_redraw_changed(void) { |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
949 coord_t *coords[3]; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
950 shape_t *shapes[3]; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
951 sh_dummy_t **dummys; |
19 | 952 paint_t *paint; |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
953 redraw_man_t *rdman; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
954 redraw_man_t _rdman; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
955 int i; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
956 |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
957 dummys = (sh_dummy_t **)shapes; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
958 |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
959 rdman = &_rdman; |
24
e598bc809c0f
No more flash when animation.
Thinker K.F. Li <thinker@branda.to>
parents:
23
diff
changeset
|
960 redraw_man_init(rdman, NULL, NULL); |
19 | 961 paint = dummy_paint_new(rdman); |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
962 for(i = 0; i < 3; i++) { |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
963 shapes[i] = sh_dummy_new(0, 0, 50, 50); |
19 | 964 rdman_paint_fill(rdman, paint, shapes[i]); |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
965 coords[i] = rdman_coord_new(rdman, rdman->root_coord); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
966 coords[i]->matrix[2] = 10 + i * 100; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
967 coords[i]->matrix[5] = 10 + i * 100; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
968 rdman_coord_changed(rdman, coords[i]); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
969 rdman_add_shape(rdman, shapes[i], coords[i]); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
970 } |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
971 rdman_redraw_all(rdman); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
972 CU_ASSERT(dummys[0]->draw_cnt == 1); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
973 CU_ASSERT(dummys[1]->draw_cnt == 1); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
974 CU_ASSERT(dummys[2]->draw_cnt == 1); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
975 |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
976 coords[2]->matrix[2] = 100; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
977 coords[2]->matrix[5] = 100; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
978 rdman_coord_changed(rdman, coords[2]); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
979 rdman_redraw_changed(rdman); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
980 |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
981 CU_ASSERT(dummys[0]->draw_cnt == 1); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
982 CU_ASSERT(dummys[1]->draw_cnt == 2); |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
983 CU_ASSERT(dummys[2]->draw_cnt == 2); |
19 | 984 |
985 paint->free(paint); | |
986 redraw_man_destroy(rdman); | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
987 } |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
988 |
12 | 989 CU_pSuite get_redraw_man_suite(void) { |
990 CU_pSuite suite; | |
991 | |
992 suite = CU_add_suite("Suite_redraw_man", NULL, NULL); | |
993 CU_ADD_TEST(suite, test_rdman_find_overlaid_shapes); | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
994 CU_ADD_TEST(suite, test_rdman_redraw_changed); |
12 | 995 |
996 return suite; | |
997 } | |
998 | |
999 #endif /* UNITTEST */ |