Adding upstream version 3.51.
[syslinux-debian/hramrach.git] / com32 / lib / sys / vesa / drawtxt.c
blob5accd861ce95064548f5a1a8337387b10cac9f65
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2006 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
28 #include <inttypes.h>
29 #include <colortbl.h>
30 #include "vesa.h"
31 #include "video.h"
32 #include "fmtpixel.h"
33 #include "fill.h"
36 * Visible cursor information
38 static uint8_t cursor_pattern[FONT_MAX_HEIGHT];
39 static struct vesa_char *cursor_pointer = NULL;
40 static int cursor_x, cursor_y;
42 static inline void *copy_dword(void *dst, void *src, size_t dword_count)
44 asm volatile("cld; rep; movsl"
45 : "+D" (dst), "+S" (src), "+c" (dword_count));
46 return dst; /* Updated destination pointer */
49 static inline __attribute__((always_inline))
50 uint8_t alpha_val(uint8_t fg, uint8_t bg, uint8_t alpha)
52 unsigned int tmp;
54 tmp = __vesacon_srgb_to_linear[fg] * alpha;
55 tmp += __vesacon_srgb_to_linear[bg] * (255-alpha);
57 return __vesacon_linear_to_srgb[tmp >> 12];
60 static uint32_t alpha_pixel(uint32_t fg, uint32_t bg)
62 uint8_t alpha = fg >> 24;
63 uint8_t fg_r = fg >> 16;
64 uint8_t fg_g = fg >> 8;
65 uint8_t fg_b = fg;
66 uint8_t bg_r = bg >> 16;
67 uint8_t bg_g = bg >> 8;
68 uint8_t bg_b = bg;
70 return
71 (alpha_val(fg_r, bg_r, alpha) << 16)|
72 (alpha_val(fg_g, bg_g, alpha) << 8)|
73 (alpha_val(fg_b, bg_b, alpha));
76 static void vesacon_update_characters(int row, int col, int nrows, int ncols)
78 const int height = __vesacon_font_height;
79 const int width = FONT_WIDTH;
80 uint32_t *bgrowptr, *bgptr, bgval, fgval;
81 uint32_t fgcolor = 0, bgcolor = 0, color;
82 uint8_t chbits = 0, chxbits = 0, chsbits = 0;
83 int i, j, jx, pixrow, pixsrow;
84 struct vesa_char *rowptr, *rowsptr, *cptr, *csptr;
85 unsigned int bytes_per_pixel = __vesacon_bytes_per_pixel;
86 unsigned long pixel_offset, bytes_per_row;
87 uint8_t row_buffer[VIDEO_X_SIZE*4], *rowbufptr;
88 uint8_t *fbrowptr;
89 uint8_t sha;
91 bgrowptr = &__vesacon_background[row*height+VIDEO_BORDER][col*width+VIDEO_BORDER];
93 pixel_offset = ((row*height+VIDEO_BORDER)*VIDEO_X_SIZE)+
94 (col*width+VIDEO_BORDER);
96 switch (__vesacon_pixel_format) {
97 case PXF_BGR24:
98 bytes_per_pixel = 3;
99 break;
100 case PXF_BGRA32:
101 bytes_per_pixel = 4;
102 break;
103 case PXF_LE_RGB16_565:
104 bytes_per_pixel = 2;
105 break;
106 default:
107 bytes_per_pixel = 0;
108 break;
111 bytes_per_row = bytes_per_pixel*VIDEO_X_SIZE;
112 fbrowptr = ((uint8_t *)__vesa_info.mi.lfb_ptr)+pixel_offset*bytes_per_pixel;
114 /* Note that we keep a 1-character guard area around the real text area... */
115 rowptr = &__vesacon_text_display[(row+1)*(TEXT_PIXEL_COLS/FONT_WIDTH+2)+(col+1)];
116 rowsptr = rowptr - ((TEXT_PIXEL_COLS/FONT_WIDTH+2)+1);
117 pixrow = 0;
118 pixsrow = height-1;
120 for (i = height*nrows; i >= 0; i--) {
121 bgptr = bgrowptr;
122 rowbufptr = row_buffer;
124 cptr = rowptr;
125 csptr = rowsptr;
127 chsbits = __vesacon_graphics_font[csptr->ch][pixsrow];
128 if (__unlikely(csptr == cursor_pointer))
129 chsbits |= cursor_pattern[pixsrow];
130 sha = console_color_table[csptr->attr].shadow;
131 chsbits &= (sha & 0x02) ? 0xff : 0x00;
132 chsbits ^= (sha & 0x01) ? 0xff : 0x00;
133 chsbits <<= (width-2);
134 csptr++;
136 /* Draw two pixels beyond the end of the line. One for the shadow,
137 and one to make sure we have a whole dword of data for the copy
138 operation at the end. Note that this code depends on the fact that
139 all characters begin on dword boundaries in the frame buffer. */
141 for (jx = 1, j = width*ncols+1; j >= 0; j--) {
142 chbits <<= 1;
143 chsbits <<= 1;
144 chxbits <<= 1;
146 switch (jx) {
147 case 1:
148 chbits = __vesacon_graphics_font[cptr->ch][pixrow];
149 if (__unlikely(cptr == cursor_pointer))
150 chbits |= cursor_pattern[pixrow];
151 sha = console_color_table[cptr->attr].shadow;
152 chxbits = chbits;
153 chxbits &= (sha & 0x02) ? 0xff : 0x00;
154 chxbits ^= (sha & 0x01) ? 0xff : 0x00;
155 fgcolor = console_color_table[cptr->attr].argb_fg;
156 bgcolor = console_color_table[cptr->attr].argb_bg;
157 cptr++;
158 jx--;
159 break;
160 case 0:
161 chsbits = __vesacon_graphics_font[csptr->ch][pixsrow];
162 if (__unlikely(csptr == cursor_pointer))
163 chsbits |= cursor_pattern[pixsrow];
164 sha = console_color_table[csptr->attr].shadow;
165 chsbits &= (sha & 0x02) ? 0xff : 0x00;
166 chsbits ^= (sha & 0x01) ? 0xff : 0x00;
167 csptr++;
168 jx = width-1;
169 break;
170 default:
171 jx--;
172 break;
175 /* If this pixel is raised, use the offsetted value */
176 bgval = (chxbits & 0x80) ? bgptr[VIDEO_X_SIZE+1] : *bgptr;
177 bgptr++;
179 /* If this pixel is set, use the fg color, else the bg color */
180 fgval = (chbits & 0x80) ? fgcolor : bgcolor;
182 /* Produce the combined color pixel value */
183 color = alpha_pixel(fgval, bgval);
185 /* Apply the shadow (75% shadow) */
186 if ((chsbits & ~chxbits) & 0x80) {
187 color >>= 2;
188 color &= 0x3f3f3f;
191 rowbufptr = format_pixel(rowbufptr, color, __vesacon_pixel_format);
194 /* Copy to frame buffer */
195 /* Note that the dword_count is rounded down, not up. That's because
196 the row_buffer includes a spillover pixel. */
197 copy_dword(fbrowptr, row_buffer, (rowbufptr-row_buffer) >> 2);
199 bgrowptr += VIDEO_X_SIZE;
200 fbrowptr += bytes_per_row;
202 if (++pixrow == height) {
203 rowptr += TEXT_PIXEL_COLS/FONT_WIDTH+2;
204 pixrow = 0;
206 if (++pixsrow == height) {
207 rowsptr += TEXT_PIXEL_COLS/FONT_WIDTH+2;
208 pixsrow = 0;
213 /* Bounding box for changed text. The (x1, y1) coordinates are +1! */
214 static unsigned int upd_x0 = -1U, upd_x1, upd_y0 = -1U, upd_y1;
216 /* Update the range already touched by various variables */
217 void __vesacon_doit(void)
219 if (upd_x1 > upd_x0 && upd_y1 > upd_y0) {
220 vesacon_update_characters(upd_y0, upd_x0, upd_y1-upd_y0, upd_x1-upd_x0);
221 upd_x0 = upd_y0 = -1U;
222 upd_x1 = upd_y1 = 0;
226 /* Mark a range for update; note argument sequence is the same as
227 vesacon_update_characters() */
228 static inline void vesacon_touch(int row, int col, int rows, int cols)
230 unsigned int y0 = row;
231 unsigned int x0 = col;
232 unsigned int y1 = y0+rows;
233 unsigned int x1 = x0+cols;
235 if (y0 < upd_y0)
236 upd_y0 = y0;
237 if (y1 > upd_y1)
238 upd_y1 = y1;
239 if (x0 < upd_x0)
240 upd_x0 = x0;
241 if (x1 > upd_x1)
242 upd_x1 = x1;
245 /* Erase a region of the screen */
246 void __vesacon_erase(int x0, int y0, int x1, int y1, attr_t attr)
248 int y;
249 struct vesa_char *ptr = &__vesacon_text_display
250 [(y0+1)*(TEXT_PIXEL_COLS/FONT_WIDTH+2)+(x0+1)];
251 struct vesa_char fill = {
252 .ch = ' ',
253 .attr = attr,
255 int ncols = x1-x0+1;
257 for (y = y0; y <= y1; y++) {
258 vesacon_fill(ptr, fill, ncols);
259 ptr += TEXT_PIXEL_COLS/FONT_WIDTH+2;
262 vesacon_touch(y0, x0, y1-y0+1, ncols);
265 /* Scroll the screen up */
266 void __vesacon_scroll_up(int nrows, attr_t attr)
268 struct vesa_char *fromptr = &__vesacon_text_display
269 [(nrows+1)*(TEXT_PIXEL_COLS/FONT_WIDTH+2)];
270 struct vesa_char *toptr = &__vesacon_text_display
271 [(TEXT_PIXEL_COLS/FONT_WIDTH+2)];
272 int dword_count = (__vesacon_text_rows-nrows)*(TEXT_PIXEL_COLS/FONT_WIDTH+2);
273 struct vesa_char fill = {
274 .ch = ' ',
275 .attr = attr,
278 toptr = copy_dword(toptr, fromptr, dword_count);
280 dword_count = nrows*(TEXT_PIXEL_COLS/FONT_WIDTH+2);
282 vesacon_fill(toptr, fill, dword_count);
284 vesacon_touch(0, 0, __vesacon_text_rows, TEXT_PIXEL_COLS/FONT_WIDTH);
287 /* Draw one character text at a specific area of the screen */
288 void __vesacon_write_char(int x, int y, uint8_t ch, attr_t attr)
290 struct vesa_char *ptr = &__vesacon_text_display
291 [(y+1)*(TEXT_PIXEL_COLS/FONT_WIDTH+2)+(x+1)];
293 ptr->ch = ch;
294 ptr->attr = attr;
296 vesacon_touch(y, x, 1, 1);
299 void __vesacon_set_cursor(int x, int y, int visible)
301 struct vesa_char *ptr = &__vesacon_text_display
302 [(y+1)*(TEXT_PIXEL_COLS/FONT_WIDTH+2)+(x+1)];
304 if (cursor_pointer)
305 vesacon_touch(cursor_y, cursor_x, 1, 1);
307 if (!visible) {
308 /* Invisible cursor */
309 cursor_pointer = NULL;
310 } else {
311 cursor_pointer = ptr;
312 vesacon_touch(y, x, 1, 1);
315 cursor_x = x;
316 cursor_y = y;
319 void __vesacon_init_cursor(int font_height)
321 int r0 = font_height - (font_height < 10 ? 2 : 3);
323 if (r0 < 0)
324 r0 = 0;
326 /* cursor_pattern is assumed to be all zero */
327 cursor_pattern[r0] = 0xff;
328 cursor_pattern[r0+1] = 0xff;
331 void __vesacon_redraw_text(void)
333 vesacon_update_characters(0, 0, __vesacon_text_rows,
334 TEXT_PIXEL_COLS/FONT_WIDTH);