Loading...
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 381 382 383 384 385 386 387 388 389 390 | // SPDX-License-Identifier: GPL-2.0+ /* * Copyright (c) 2015 Google, Inc * (C) Copyright 2015 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com * (C) Copyright 2023 Dzmitry Sankouski <dsankouski@gmail.com> */ #include <video.h> #include <video_console.h> #include <dm.h> #include <malloc.h> #include <spl.h> #include <video_font.h> #include "vidconsole_internal.h" /** * console_set_font() - prepare vidconsole for chosen font. * * @dev vidconsole device * @fontdata pointer to font data struct */ static int console_set_font(struct udevice *dev, struct video_fontdata *fontdata) { struct console_simple_priv *priv = dev_get_priv(dev); priv->fontdata = fontdata; vidconsole_set_bitmap_font(dev, fontdata); return 0; } int check_bpix_support(int bpix) { if (bpix == VIDEO_BPP8 && CONFIG_IS_ENABLED(VIDEO_BPP8)) return 0; else if (bpix == VIDEO_BPP16 && CONFIG_IS_ENABLED(VIDEO_BPP16)) return 0; else if (bpix == VIDEO_BPP32 && CONFIG_IS_ENABLED(VIDEO_BPP32)) return 0; else return -ENOSYS; } inline void fill_pixel_and_goto_next(void **dstp, u32 value, int pbytes, int step) { u8 *dst_byte = *dstp; if (pbytes == 4) { u32 *dst = *dstp; *dst = value; } if (pbytes == 2) { u16 *dst = *dstp; *dst = value; } if (pbytes == 1) { u8 *dst = *dstp; *dst = value; } *dstp = dst_byte + step; } inline u32 swap_pixel_and_goto_next(void **dstp, u32 value, int pbytes, int step) { u8 *dst_byte = *dstp; u32 old_value = 0; if (pbytes == 4) { u32 *dst = *dstp; old_value = *dst; *dst = value; } if (pbytes == 2) { u16 *dst = *dstp; old_value = *dst; *dst = value; } if (pbytes == 1) { u8 *dst = *dstp; old_value = *dst; *dst = value; } *dstp = dst_byte + step; return old_value; } int fill_char_vertically(uchar *pfont, void **line, struct video_priv *vid_priv, struct video_fontdata *fontdata, bool direction) { int step, line_step, pbytes, bitcount, width_remainder, ret; void *dst; ret = check_bpix_support(vid_priv->bpix); if (ret) return ret; pbytes = VNBYTES(vid_priv->bpix); if (direction) { step = -pbytes; line_step = -vid_priv->line_length; } else { step = pbytes; line_step = vid_priv->line_length; } width_remainder = fontdata->width % 8; for (int row = 0; row < fontdata->height; row++) { uchar bits; bitcount = 8; dst = *line; for (int col = 0; col < fontdata->byte_width; col++) { if (width_remainder) { bool is_last_col = (fontdata->byte_width - col == 1); if (is_last_col) bitcount = width_remainder; } bits = pfont[col]; for (int bit = 0; bit < bitcount; bit++) { u32 value = (bits & 0x80) ? vid_priv->colour_fg : vid_priv->colour_bg; fill_pixel_and_goto_next(&dst, value, pbytes, step ); bits <<= 1; } } *line += line_step; pfont += fontdata->byte_width; } return ret; } int fill_char_horizontally(uchar *pfont, void **line, struct video_priv *vid_priv, struct video_fontdata *fontdata, bool direction) { int step, line_step, pbytes, bitcount = 8, width_remainder, ret; void *dst; u8 mask; ret = check_bpix_support(vid_priv->bpix); if (ret) return ret; pbytes = VNBYTES(vid_priv->bpix); if (direction) { step = -pbytes; line_step = vid_priv->line_length; } else { step = pbytes; line_step = -vid_priv->line_length; } width_remainder = fontdata->width % 8; for (int col = 0; col < fontdata->byte_width; col++) { mask = 0x80; if (width_remainder) { bool is_last_col = (fontdata->byte_width - col == 1); if (is_last_col) bitcount = width_remainder; } for (int bit = 0; bit < bitcount; bit++) { dst = *line; for (int row = 0; row < fontdata->height; row++) { u32 value = (pfont[row * fontdata->byte_width + col] & mask) ? vid_priv->colour_fg : vid_priv->colour_bg; fill_pixel_and_goto_next(&dst, value, pbytes, step ); } *line += line_step; mask >>= 1; } } return ret; } int cursor_show(struct vidconsole_cursor *curs, struct video_priv *vid_priv, bool direction) { int step, line_step, pbytes, ret, row; void *line, *dst; u32 *save_ptr; uint value; ret = check_bpix_support(vid_priv->bpix); if (ret) return ret; pbytes = VNBYTES(vid_priv->bpix); if (direction) { step = -pbytes; line_step = -vid_priv->line_length; } else { step = pbytes; line_step = vid_priv->line_length; } /* we should not already have saved data */ if (curs->saved) { debug("Trying to show cursor but data is already saved\n"); return -EINVAL; } /* Figure out where to write the cursor in the frame buffer */ line = vid_priv->fb + curs->y * vid_priv->line_length + curs->x * VNBYTES(vid_priv->bpix); /* save pixels under cursor and draw new cursor in one pass */ value = vid_priv->colour_fg; save_ptr = curs->save_data; for (row = 0; row < curs->height; row++) { dst = line; for (int col = 0; col < VIDCONSOLE_CURSOR_WIDTH; col++) *save_ptr++ = swap_pixel_and_goto_next(&dst, value, pbytes, step); line += line_step; } curs->saved = true; return 0; } int cursor_hide(struct vidconsole_cursor *curs, struct video_priv *vid_priv, bool direction) { int step, line_step, pbytes, ret; void *line, *dst; ret = check_bpix_support(vid_priv->bpix); if (ret) return ret; pbytes = VNBYTES(vid_priv->bpix); if (direction) { step = -pbytes; line_step = -vid_priv->line_length; } else { step = pbytes; line_step = vid_priv->line_length; } /* Trying to hide cursor - we should have saved data */ if (!curs->saved) { debug("Trying to hide cursor but no data was saved\n"); return -EINVAL; } /* Figure out where to write the cursor in the frame buffer */ line = vid_priv->fb + curs->y * vid_priv->line_length + curs->x * VNBYTES(vid_priv->bpix); /* Restore saved pixels */ u32 *save_ptr = curs->save_data; dst = line; for (int row = 0; row < curs->height; row++) { void *row_dst = dst; for (int col = 0; col < VIDCONSOLE_CURSOR_WIDTH; col++) fill_pixel_and_goto_next(&row_dst, *save_ptr++, pbytes, step); dst += line_step; } curs->saved = false; return 0; } int console_alloc_cursor(struct udevice *dev) { struct vidconsole_priv *vc_priv; struct vidconsole_cursor *curs; struct video_priv *vid_priv; struct udevice *vid; int save_count; if (!CONFIG_IS_ENABLED(CURSOR) || xpl_phase() < PHASE_BOARD_R) return 0; vc_priv = dev_get_uclass_priv(dev); vid = dev_get_parent(dev); vid_priv = dev_get_uclass_priv(vid); curs = &vc_priv->curs; /* Allocate cursor save buffer for maximum possible cursor height */ save_count = vid_priv->ysize * VIDCONSOLE_CURSOR_WIDTH; curs->save_data = malloc(save_count * sizeof(u32)); if (!curs->save_data) return -ENOMEM; return 0; } int console_probe(struct udevice *dev) { int ret; ret = console_set_font(dev, fonts); if (ret) return ret; if (CONFIG_IS_ENABLED(CURSOR) && xpl_phase() == PHASE_BOARD_R) { ret = console_alloc_cursor(dev); if (ret) return ret; } return 0; } const char *console_simple_get_font_size(struct udevice *dev, uint *sizep) { struct console_simple_priv *priv = dev_get_priv(dev); *sizep = priv->fontdata->width; return priv->fontdata->name; } int console_simple_get_font(struct udevice *dev, int seq, struct vidfont_info *info) { info->name = fonts[seq].name; return info->name ? 0 : -ENOENT; } int console_fixed_putc_xy(struct udevice *dev, uint x_frac, uint y, int cp, struct video_fontdata *fontdata) { struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev); struct udevice *vid = dev->parent; struct video_priv *vid_priv = dev_get_uclass_priv(vid); int pbytes = VNBYTES(vid_priv->bpix); int x, linenum, ret; void *start, *line; u8 ch = console_utf_to_cp437(cp); uchar *pfont = fontdata->video_fontdata + ch * fontdata->char_pixel_bytes; if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac) return -EAGAIN; linenum = y; x = VID_TO_PIXEL(x_frac); start = vid_priv->fb + linenum * vid_priv->line_length + x * pbytes; line = start; ret = fill_char_vertically(pfont, &line, vid_priv, fontdata, NORMAL_DIRECTION); if (ret) return ret; video_damage(dev->parent, x, y, fontdata->width, fontdata->height); return VID_TO_POS(fontdata->width); } int console_simple_select_font(struct udevice *dev, const char *name, uint size) { struct video_fontdata *font; if (!name) { if (fonts->name) console_set_font(dev, fonts); return 0; } for (font = fonts; font->name; font++) { if (!strcmp(name, font->name)) { console_set_font(dev, font); return 0; } }; printf("no such font: %s, make sure it's name has <width>x<height> format\n", name); return -ENOENT; } |