-
Notifications
You must be signed in to change notification settings - Fork 0
/
xqx_map_layer.c
380 lines (309 loc) · 9.92 KB
/
xqx_map_layer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2006-2008 Ondrej 'SanTiago' Zajicek
* Copyright (c) 2021 Cyril Hrubis <metan@ucw.cz>
*/
#include <filters/gp_point.h>
#include "xqx_map_layer.h"
static uint32_t search_array(struct xqx_map_layer *ml, unsigned int level, int lx, unsigned int hx, unsigned int hy)
{
while (ml->ay < hy) {
while (ml->ax < hx) {
if (xqx_map_cache_lookup(ml->cc, ml->map, level, ml->ax, ml->ay) == NULL)
return 1;
ml->ax++;
}
ml->ax = lx;
ml->ay++;
}
return 0;
}
static uint32_t find_missing_tile(struct xqx_map_layer *ml)
{
switch (ml->as) {
case 0:
ml->ax = ml->tx2;
ml->ay = ml->ty2;
ml->as = 1;
/* fallthrough */
case 1:
if (search_array(ml, ml->level, ml->tx2, ml->tx3, ml->ty3))
return 3;
ml->ax = ml->tx1;
ml->ay = ml->ty1;
ml->as = 2;
/* fallthrough */
case 2:
if (search_array(ml, ml->level, ml->tx1, ml->tx4, ml->ty2))
return 2;
ml->ax = ml->tx1;
ml->ay = ml->ty3;
ml->as = 3;
/* fallthrough */
case 3:
if (search_array(ml, ml->level, ml->tx1, ml->tx4, ml->ty4))
return 2;
ml->ax = ml->tx1;
ml->ay = ml->ty2;
ml->as = 4;
/* fallthrough */
case 4:
if (search_array(ml, ml->level, ml->tx1, ml->tx2, ml->ty3))
return 2;
ml->ax = ml->tx3;
ml->ay = ml->ty2;
ml->as = 5;
/* fallthrough */
case 5:
if (search_array(ml, ml->level, ml->tx3, ml->tx4, ml->ty3))
return 2;
ml->ax = ml->t2x1;
ml->ay = ml->t2y1;
ml->as = 6;
if (ml->level == 0)
return 0;
/* fallthrough */
case 6:
if (search_array(ml, ml->level - 1, ml->t2x1, ml->t2x2, ml->t2y2))
return 1;
ml->as = 7;
/* fallthrough */
case 7:
return 0;
}
return 0;
}
/* notification from cache about new avamlable tiles */
static void map_layer_cc_notify(void *ml_i, struct xqx_map *map, unsigned int l, unsigned int x, unsigned int y, struct xqx_map_cache_node *tile)
{
struct xqx_map_layer *ml = ml_i;
// printf("CC notify L%u X%u Y%u\n", l, x, y);
(void) map;
(void) tile;
if ((ml->level == l) && (ml->tx2 <= x) && (x < ml->tx3) && (ml->ty2 <= y) && (y < ml->ty3)) {
int sx = (x - ml->tx2) * ml->map->tile_w + ml->pix_off_x;
int sy = (y - ml->ty2) * ml->map->tile_h + ml->pix_off_y;
xqx_view_request_redraw(ml->common.view, sx, sy, sx + ml->map->tile_w, sy + ml->map->tile_h);
}
}
/* query from cache about requested tiles */
static uint32_t map_layer_cc_query(void *ml_i, struct xqx_map **map, uint32_t *l, uint32_t *x, uint32_t *y)
{
struct xqx_map_layer *ml = ml_i;
uint32_t mt = find_missing_tile(ml);
if (mt > 0) {
// printf("AF0 QUERY %d L%d X%d Y%d\n", mt, ml->level, ml->ax, ml->ay);
*map = ml->map;
*x = ml->ax;
*y = ml->ay;
*l = (mt == 1) ? (ml->level - 1) : ml->level;
}
return mt;
}
/* eval tiles from cache for removing */
static uint32_t map_layer_cc_eval(void *ml_i, struct xqx_map_cache_node *cn)
{
struct xqx_map_layer *ml = ml_i;
if ((cn->l == ml->level)
&& (ml->tx2 <= cn->x) && (cn->x < ml->tx3)
&& (ml->ty2 <= cn->y) && (cn->y < ml->ty3))
return 3;
if ((cn->l == ml->level)
&& (ml->tx1 <= cn->x) && (cn->x < ml->tx4)
&& (ml->ty1 <= cn->y) && (cn->y < ml->ty4))
return 2;
/*
if ((cn->l + 1) == ml->level)
{
printf("X %d %d %d\n", (2 * ml->tx2), cn->x, (2 * ml->tx3));
printf("Y %d %d %d\n", (2 * ml->ty2), cn->y, (2 * ml->ty3));
if ((1)
&& ((2 * ml->tx2) <= cn->x) && (cn->x < (2 * ml->tx3))
&& ((2 * ml->ty2) <= cn->y) && (cn->y < (2 * ml->ty3)))
return (1);
}
*/
return 0;
}
static uint32_t get_nearest_level(struct xqx_map *map, int scale_main)
{
int ns = 1;
int l = 0;
while ((ns <= scale_main) && (l < map->num_levels)) {
ns *= 2;
l++;
}
return ((l > 0) ? (l - 1) : l);
}
/* notification from view about view geometry change */
static void map_layer_notify(void *ml_i, struct xqx_view *vw, uint32_t change)
{
struct xqx_map_layer *ml = ml_i;
struct xqx_coordinate *c = &(vw->center);
if (change == XQX_VLC_FINISH)
return;
if ((change == XQX_VLC_INIT) || (change == XQX_VLC_SCALE)) {
ml->level = get_nearest_level(ml->map, vw->scale_main);
xqx_map_cache_request_notification(ml->cc, ml->map, ml->level);
}
// FIXME remove next check
if (vw->scale_main != (1 << ml->level))
printf("WARNING: Inconsistent scale and level\n");
int tw = ml->map->tile_w;
int th = ml->map->tile_h;
int txc = ml->map->num_tiles_x[ml->level];
int tyc = ml->map->num_tiles_y[ml->level];
/* coordinates in current pixel coordinate space (CPCS)
are coordinates in pixels of current zoom,
relative to the start of a image */
/* c. are coordinates of center of view in CPCS */
/* FIXME analyze needed precision */
int64_t tmpx = c->x;
tmpx -= ml->map->geo_cox;
tmpx *= ml->map->geo_psx;
tmpx /= ml->map->geo_csx;
tmpx += ml->map->geo_pox;
int cx = tmpx / (1 << ml->level);
int64_t tmpy = c->y;
tmpy -= ml->map->geo_coy;
tmpy *= ml->map->geo_psy;
tmpy /= ml->map->geo_csy;
tmpy += ml->map->geo_poy;
int cy = tmpy / (1 << ml->level);
/* l. and h. are coordinates of ul,lr corners of view in CPCS */
int lx = cx - (vw->w / 2);
int ly = cy - (vw->h / 2);
int hx = lx + vw->w;
int hy = ly + vw->h;
/* tl. and th. are indices of 'interesting' tiles - tiles
containing corners of view */
int tlx = lx / tw;
int tly = ly / th;
int thx = (hx - 1 + tw) / tw;
int thy = (hy - 1 + th) / th;
/* we clamp indices to interval avamlable tiles */
tlx = CLAMP(tlx, 0, txc);
tly = CLAMP(tly, 0, tyc);
thx = CLAMP(thx, 0, txc);
thy = CLAMP(thy, 0, tyc);
/* ml->pix_off_. are offsets of a start of 'interesting' tile rectangle
in pixels of current zoom, relative to the start of a view */
ml->pix_off_x = tlx * tw - lx;
ml->pix_off_y = tly * th - ly;
/* save 'interesting' tiles coords */
ml->tx2 = tlx;
ml->tx3 = thx;
ml->ty2 = tly;
ml->ty3 = thy;
int dx = (thx - tlx + 1) / 2;
int dy = (thy - tly + 1) / 2;
/* extended interesting tiles */
ml->tx1 = MAX(0, tlx - dx);
ml->ty1 = MAX(0, tly - dy);
ml->tx4 = MIN(thx + dx, txc);
ml->ty4 = MIN(thy + dy, tyc);
/* rectangle for prefetch of lower level */
if (ml->level > 0) {
int t2xc = ml->map->num_tiles_x[ml->level - 1];
int t2yc = ml->map->num_tiles_y[ml->level - 1];
ml->t2x1 = MIN(t2xc, (int) ml->tx2 * 2);
ml->t2y1 = MIN(t2yc, (int) ml->ty2 * 2);
ml->t2x2 = MIN(t2xc, (int) ml->tx3 * 2);
ml->t2y2 = MIN(t2yc, (int) ml->ty3 * 2);
}
// printf("CONF X: %d %d %d %d\n", ml->tx1, ml->tx2, ml->tx3, ml->tx4);
// printf("CONF Y: %d %d %d %d\n", ml->ty1, ml->ty2, ml->ty3, ml->ty4);
// printf("OFF: %d %d\n", ml->pix_off_x, ml->pix_off_y);
ml->as = 0;
int mt = find_missing_tile(ml);
xqx_map_cache_request_attention(ml->cc, mt);
}
static void map_layer_render(void *ml_i, struct xqx_view *vw, gp_pixmap *dst, struct xqx_rectangle *rect)
{
struct xqx_map_layer *ml = ml_i;
(void) vw;
int tw = ml->map->tile_w;
int th = ml->map->tile_h;
/* - first we translate absolute (in window) pixel coordinates to
relative (to first tile in window begin) coordinates
- then we scale from pixels to tiles
- then we translate from relative (to first tile in window) tiles to
absolute tiles
- we should draw tiles satisfying lx <= x < hx, ly <= y < hy
*/
int lx = (rect->lx - ml->pix_off_x) / tw + ml->tx2;
int ly = (rect->ly - ml->pix_off_y) / th + ml->ty2;
int hx = (rect->hx - ml->pix_off_x - 1 + tw) / tw + ml->tx2;
int hy = (rect->hy - ml->pix_off_y - 1 + th) / th + ml->ty2;
/* now crop tile coords to visible tiles range (tiles between t_2 and t_3) */
lx = (lx < (int)ml->tx2) ? (int)ml->tx2 : lx;
ly = (ly < (int)ml->ty2) ? (int)ml->ty2 : ly;
hx = (hx > (int)ml->tx3) ? (int)ml->tx3 : hx;
hy = (hy < (int)ml->ty3) ? (int)ml->ty3 : hy;
//TODO: Optimize?
gp_fill_rect_xyxy(dst, rect->lx, rect->ly, rect->hx, rect->hy, ml->bg_color);
//printf("*** RENDER (%d %d) - (%d %d)\n", lx, ly, hx, hy);
int i, j;
for (i = lx; i < hx; i++) {
for (j = ly; j < hy; j++) {
int ax = (i - ml->tx2) * tw + ml->pix_off_x;
int ay = (j - ml->ty2) * th + ml->pix_off_y;
int aw = tw;
int ah = th;
/* fixup for a width/height of tile at last column/row */
if ((i + 1) == ml->map->num_tiles_x[ml->level])
aw = (ml->map->map_w >> ml->level) - i * tw;
if ((j + 1) == ml->map->num_tiles_y[ml->level])
ah = (ml->map->map_h >> ml->level) - j * th;
struct xqx_map_cache_node *cn = xqx_map_cache_lookup(ml->cc, ml->map, ml->level, i, j);
if (cn == NULL) {
//printf("NODATA (%d %d) at (%d %d)\n", i, j, ax, ay);
/* no data */
/* FIXME add scaling of larger */
} else if (cn->state == XQX_CACHE_NODE_VALID_DATA) {
//printf("DRAW (%d %d) at (%d %d)\n", i, j, ax, ay);
gp_pixmap *pb = cn->data, *tmp;
int invert_pixmap = gp_widgets_color_scheme_get() == GP_WIDGET_COLOR_SCHEME_DARK;
printf("INVERT PIXMAP %i\n", invert_pixmap);
if (invert_pixmap)
tmp = gp_filter_invert_alloc(pb, NULL);
else
tmp = pb;
gp_blit_xywh_clipped(tmp, 0, 0, aw, ah, dst, ax, ay);
if (invert_pixmap)
gp_pixmap_free(tmp);
} else if (cn->state == XQX_CACHE_NODE_VALID_COLOR) {
//printf("COLOR (%d %d) at (%d %d)\n", i, j, ax, ay);
uint32_t rgb = (uintptr_t) cn->data;
gp_pixel color = gp_rgb_to_pixmap_pixel(rgb&0x00ff0000>>16,
rgb&0x0000ff00>>8,
rgb&0x000000ff, dst);
gp_fill_rect_xywh(dst, ax, ay, aw, ah, color);
}
}
}
}
static struct xqx_map_cache_client_ops map_layer_ops = {
map_layer_cc_notify, map_layer_cc_query, map_layer_cc_eval
};
struct xqx_map_layer *xqx_make_map_layer(struct xqx_map *map)
{
struct xqx_map_layer *ml = calloc(1, sizeof(struct xqx_map_layer));
if (!ml)
return NULL;
ml->common.notify_cb = map_layer_notify;
ml->common.render_cb = map_layer_render;
ml->map = map;
ml->cc = xqx_map_cache_make_client(&map_layer_ops, ml);
if (!ml->cc) {
free(ml);
return NULL;
}
return ml;
}
/* image_layer should be removed from view before discarding */
void xqx_discard_map_layer(struct xqx_map_layer *ml)
{
xqx_map_cache_discard_client(ml->cc);
free(ml);
}