blob: d868d75720b57748f80048246ead235d28535008 [file] [log] [blame]
bellard24236862006-04-30 21:28:36 +00001#define CONCAT_I(a, b) a ## b
2#define CONCAT(a, b) CONCAT_I(a, b)
3#define pixel_t CONCAT(uint, CONCAT(BPP, _t))
bellard35127792006-05-14 18:11:49 +00004#ifdef GENERIC
aliguori7eac3a82008-09-15 16:03:41 +00005#define NAME CONCAT(generic_, BPP)
bellard35127792006-05-14 18:11:49 +00006#else
7#define NAME BPP
8#endif
bellard24236862006-04-30 21:28:36 +00009
bellard35127792006-05-14 18:11:49 +000010static void CONCAT(send_hextile_tile_, NAME)(VncState *vs,
11 int x, int y, int w, int h,
aliguori7eac3a82008-09-15 16:03:41 +000012 void *last_bg_,
13 void *last_fg_,
bellard35127792006-05-14 18:11:49 +000014 int *has_bg, int *has_fg)
bellard24236862006-04-30 21:28:36 +000015{
Stefano Stabellini1fc62412009-08-03 10:54:32 +010016 VncDisplay *vd = vs->vd;
Gerd Hoffmann9f649162012-10-10 13:29:43 +020017 uint8_t *row = vnc_server_fb_ptr(vd, x, y);
bellard24236862006-04-30 21:28:36 +000018 pixel_t *irow = (pixel_t *)row;
19 int j, i;
aliguori7eac3a82008-09-15 16:03:41 +000020 pixel_t *last_bg = (pixel_t *)last_bg_;
21 pixel_t *last_fg = (pixel_t *)last_fg_;
bellard24236862006-04-30 21:28:36 +000022 pixel_t bg = 0;
23 pixel_t fg = 0;
24 int n_colors = 0;
25 int bg_count = 0;
26 int fg_count = 0;
27 int flags = 0;
Gerd Hoffmann9f649162012-10-10 13:29:43 +020028 uint8_t data[(vs->client_pf.bytes_per_pixel + 2) * 16 * 16];
bellard24236862006-04-30 21:28:36 +000029 int n_data = 0;
30 int n_subtiles = 0;
31
32 for (j = 0; j < h; j++) {
33 for (i = 0; i < w; i++) {
34 switch (n_colors) {
35 case 0:
36 bg = irow[i];
37 n_colors = 1;
38 break;
39 case 1:
40 if (irow[i] != bg) {
41 fg = irow[i];
42 n_colors = 2;
43 }
44 break;
45 case 2:
46 if (irow[i] != bg && irow[i] != fg) {
47 n_colors = 3;
48 } else {
49 if (irow[i] == bg)
50 bg_count++;
51 else if (irow[i] == fg)
52 fg_count++;
53 }
54 break;
55 default:
56 break;
57 }
58 }
59 if (n_colors > 2)
60 break;
Gerd Hoffmann9f649162012-10-10 13:29:43 +020061 irow += vnc_server_fb_stride(vd) / sizeof(pixel_t);
bellard24236862006-04-30 21:28:36 +000062 }
63
64 if (n_colors > 1 && fg_count > bg_count) {
65 pixel_t tmp = fg;
66 fg = bg;
67 bg = tmp;
68 }
69
70 if (!*has_bg || *last_bg != bg) {
71 flags |= 0x02;
72 *has_bg = 1;
73 *last_bg = bg;
74 }
75
Anthony Liguori02c2b872009-12-14 14:11:56 -060076 if (n_colors < 3 && (!*has_fg || *last_fg != fg)) {
bellard24236862006-04-30 21:28:36 +000077 flags |= 0x04;
78 *has_fg = 1;
79 *last_fg = fg;
80 }
81
82 switch (n_colors) {
83 case 1:
84 n_data = 0;
85 break;
86 case 2:
87 flags |= 0x08;
88
89 irow = (pixel_t *)row;
ths5fafdf22007-09-16 21:08:06 +000090
bellard24236862006-04-30 21:28:36 +000091 for (j = 0; j < h; j++) {
92 int min_x = -1;
93 for (i = 0; i < w; i++) {
94 if (irow[i] == fg) {
95 if (min_x == -1)
96 min_x = i;
97 } else if (min_x != -1) {
98 hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1);
99 n_data += 2;
100 n_subtiles++;
101 min_x = -1;
102 }
103 }
104 if (min_x != -1) {
105 hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1);
106 n_data += 2;
107 n_subtiles++;
108 }
Gerd Hoffmann9f649162012-10-10 13:29:43 +0200109 irow += vnc_server_fb_stride(vd) / sizeof(pixel_t);
bellard24236862006-04-30 21:28:36 +0000110 }
111 break;
112 case 3:
113 flags |= 0x18;
114
115 irow = (pixel_t *)row;
116
117 if (!*has_bg || *last_bg != bg)
118 flags |= 0x02;
119
120 for (j = 0; j < h; j++) {
121 int has_color = 0;
122 int min_x = -1;
ths92190c62006-12-21 16:50:09 +0000123 pixel_t color = 0; /* shut up gcc */
bellard24236862006-04-30 21:28:36 +0000124
125 for (i = 0; i < w; i++) {
126 if (!has_color) {
127 if (irow[i] == bg)
128 continue;
129 color = irow[i];
130 min_x = i;
131 has_color = 1;
132 } else if (irow[i] != color) {
133 has_color = 0;
bellard35127792006-05-14 18:11:49 +0000134#ifdef GENERIC
135 vnc_convert_pixel(vs, data + n_data, color);
Gerd Hoffmann9f649162012-10-10 13:29:43 +0200136 n_data += vs->client_pf.bytes_per_pixel;
bellard35127792006-05-14 18:11:49 +0000137#else
bellard24236862006-04-30 21:28:36 +0000138 memcpy(data + n_data, &color, sizeof(color));
bellard35127792006-05-14 18:11:49 +0000139 n_data += sizeof(pixel_t);
140#endif
141 hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1);
142 n_data += 2;
bellard24236862006-04-30 21:28:36 +0000143 n_subtiles++;
144
145 min_x = -1;
146 if (irow[i] != bg) {
147 color = irow[i];
148 min_x = i;
149 has_color = 1;
150 }
151 }
152 }
153 if (has_color) {
bellard35127792006-05-14 18:11:49 +0000154#ifdef GENERIC
155 vnc_convert_pixel(vs, data + n_data, color);
Gerd Hoffmann9f649162012-10-10 13:29:43 +0200156 n_data += vs->client_pf.bytes_per_pixel;
bellard35127792006-05-14 18:11:49 +0000157#else
158 memcpy(data + n_data, &color, sizeof(color));
159 n_data += sizeof(pixel_t);
160#endif
161 hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1);
162 n_data += 2;
bellard24236862006-04-30 21:28:36 +0000163 n_subtiles++;
164 }
Gerd Hoffmann9f649162012-10-10 13:29:43 +0200165 irow += vnc_server_fb_stride(vd) / sizeof(pixel_t);
bellard24236862006-04-30 21:28:36 +0000166 }
167
Anthony Liguori18cb1d82010-01-07 07:55:34 -0600168 /* A SubrectsColoured subtile invalidates the foreground color */
169 *has_fg = 0;
bellard24236862006-04-30 21:28:36 +0000170 if (n_data > (w * h * sizeof(pixel_t))) {
171 n_colors = 4;
172 flags = 0x01;
173 *has_bg = 0;
174
175 /* we really don't have to invalidate either the bg or fg
176 but we've lost the old values. oh well. */
177 }
Stefan Weil0ea5c0c2012-02-25 14:57:03 +0100178 break;
bellard24236862006-04-30 21:28:36 +0000179 default:
180 break;
181 }
182
183 if (n_colors > 3) {
184 flags = 0x01;
185 *has_fg = 0;
186 *has_bg = 0;
187 n_colors = 4;
188 }
189
190 vnc_write_u8(vs, flags);
191 if (n_colors < 4) {
192 if (flags & 0x02)
Gerd Hoffmann9f649162012-10-10 13:29:43 +0200193 vs->write_pixels(vs, last_bg, sizeof(pixel_t));
bellard24236862006-04-30 21:28:36 +0000194 if (flags & 0x04)
Gerd Hoffmann9f649162012-10-10 13:29:43 +0200195 vs->write_pixels(vs, last_fg, sizeof(pixel_t));
bellard24236862006-04-30 21:28:36 +0000196 if (n_subtiles) {
197 vnc_write_u8(vs, n_subtiles);
198 vnc_write(vs, data, n_data);
199 }
200 } else {
201 for (j = 0; j < h; j++) {
Gerd Hoffmann9f649162012-10-10 13:29:43 +0200202 vs->write_pixels(vs, row, w * 4);
203 row += vnc_server_fb_stride(vd);
bellard24236862006-04-30 21:28:36 +0000204 }
205 }
206}
207
bellard35127792006-05-14 18:11:49 +0000208#undef NAME
bellard24236862006-04-30 21:28:36 +0000209#undef pixel_t
210#undef CONCAT_I
211#undef CONCAT