2009-11-02 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2/jjazz.git] / video / video.c
blobc1d66bdd00a458ee69c2094cd4ba23cccdd72a74
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/video.h>
20 #include <grub/types.h>
21 #include <grub/dl.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
25 /* The list of video adapters registered to system. */
26 static grub_video_adapter_t grub_video_adapter_list;
28 /* Active video adapter. */
29 static grub_video_adapter_t grub_video_adapter_active;
31 /* Register video driver. */
32 void
33 grub_video_register (grub_video_adapter_t adapter)
35 adapter->next = grub_video_adapter_list;
36 grub_video_adapter_list = adapter;
39 /* Unregister video driver. */
40 void
41 grub_video_unregister (grub_video_adapter_t adapter)
43 grub_video_adapter_t *p, q;
45 for (p = &grub_video_adapter_list, q = *p; q; p = &(q->next), q = q->next)
46 if (q == adapter)
48 *p = q->next;
49 break;
53 /* Iterate thru all registered video drivers. */
54 void
55 grub_video_iterate (int (*hook) (grub_video_adapter_t adapter))
57 grub_video_adapter_t p;
59 for (p = grub_video_adapter_list; p; p = p->next)
60 if (hook (p))
61 break;
64 /* Restore back to initial mode (where applicable). */
65 grub_err_t
66 grub_video_restore (void)
68 if (grub_video_adapter_active)
70 grub_video_adapter_active->fini ();
71 if (grub_errno != GRUB_ERR_NONE)
72 return grub_errno;
74 grub_video_adapter_active = 0;
76 return GRUB_ERR_NONE;
79 /* Get information about active video mode. */
80 grub_err_t
81 grub_video_get_info (struct grub_video_mode_info *mode_info)
83 if (! grub_video_adapter_active)
84 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
86 /* If mode_info is NULL just report that video adapter is active. */
87 if (! mode_info)
89 grub_errno = GRUB_ERR_NONE;
90 return grub_errno;
93 return grub_video_adapter_active->get_info (mode_info);
96 /* Get information about active video mode. */
97 grub_err_t
98 grub_video_get_info_and_fini (struct grub_video_mode_info *mode_info,
99 void **framebuffer)
101 grub_err_t err;
103 if (! grub_video_adapter_active)
104 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
106 err = grub_video_adapter_active->get_info_and_fini (mode_info, framebuffer);
107 if (err)
108 return err;
110 grub_video_adapter_active = 0;
111 return GRUB_ERR_NONE;
114 /* Determine optimized blitting formation for specified video mode info. */
115 enum grub_video_blit_format
116 grub_video_get_blit_format (struct grub_video_mode_info *mode_info)
118 /* Check if we have any known 32 bit modes. */
119 if (mode_info->bpp == 32)
121 if ((mode_info->red_mask_size == 8)
122 && (mode_info->red_field_pos == 16)
123 && (mode_info->green_mask_size == 8)
124 && (mode_info->green_field_pos == 8)
125 && (mode_info->blue_mask_size == 8)
126 && (mode_info->blue_field_pos == 0))
128 return GRUB_VIDEO_BLIT_FORMAT_BGRA_8888;
130 else if ((mode_info->red_mask_size == 8)
131 && (mode_info->red_field_pos == 0)
132 && (mode_info->green_mask_size == 8)
133 && (mode_info->green_field_pos == 8)
134 && (mode_info->blue_mask_size == 8)
135 && (mode_info->blue_field_pos == 16))
137 return GRUB_VIDEO_BLIT_FORMAT_RGBA_8888;
140 /* Check if we have any known 24 bit modes. */
141 else if (mode_info->bpp == 24)
143 if ((mode_info->red_mask_size == 8)
144 && (mode_info->red_field_pos == 16)
145 && (mode_info->green_mask_size == 8)
146 && (mode_info->green_field_pos == 8)
147 && (mode_info->blue_mask_size == 8)
148 && (mode_info->blue_field_pos == 0))
150 return GRUB_VIDEO_BLIT_FORMAT_BGR_888;
152 else if ((mode_info->red_mask_size == 8)
153 && (mode_info->red_field_pos == 0)
154 && (mode_info->green_mask_size == 8)
155 && (mode_info->green_field_pos == 8)
156 && (mode_info->blue_mask_size == 8)
157 && (mode_info->blue_field_pos == 16))
159 return GRUB_VIDEO_BLIT_FORMAT_RGB_888;
162 /* Check if we have any known 16 bit modes. */
163 else if (mode_info->bpp == 16)
165 if ((mode_info->red_mask_size == 5)
166 && (mode_info->red_field_pos == 11)
167 && (mode_info->green_mask_size == 6)
168 && (mode_info->green_field_pos == 5)
169 && (mode_info->blue_mask_size == 5)
170 && (mode_info->blue_field_pos == 0))
172 return GRUB_VIDEO_BLIT_FORMAT_BGR_565;
174 else if ((mode_info->red_mask_size == 5)
175 && (mode_info->red_field_pos == 0)
176 && (mode_info->green_mask_size == 6)
177 && (mode_info->green_field_pos == 5)
178 && (mode_info->blue_mask_size == 5)
179 && (mode_info->blue_field_pos == 11))
181 return GRUB_VIDEO_BLIT_FORMAT_RGB_565;
184 else if (mode_info->bpp == 1)
185 return GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED;
187 /* Backup route. Unknown format. */
189 /* If there are more than 8 bits per color, assume RGB(A) mode. */
190 if (mode_info->bpp > 8)
192 if (mode_info->reserved_mask_size > 0)
194 return GRUB_VIDEO_BLIT_FORMAT_RGBA;
196 else
198 return GRUB_VIDEO_BLIT_FORMAT_RGB;
202 /* Assume as indexcolor mode. */
203 return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR;
206 /* Set new indexed color palette entries. */
207 grub_err_t
208 grub_video_set_palette (unsigned int start, unsigned int count,
209 struct grub_video_palette_data *palette_data)
211 if (! grub_video_adapter_active)
212 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
214 return grub_video_adapter_active->set_palette (start, count, palette_data);
217 /* Get indexed color palette entries. */
218 grub_err_t
219 grub_video_get_palette (unsigned int start, unsigned int count,
220 struct grub_video_palette_data *palette_data)
222 if (! grub_video_adapter_active)
223 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
225 return grub_video_adapter_active->get_palette (start, count, palette_data);
228 /* Set viewport dimensions. */
229 grub_err_t
230 grub_video_set_viewport (unsigned int x, unsigned int y,
231 unsigned int width, unsigned int height)
233 if (! grub_video_adapter_active)
234 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
236 return grub_video_adapter_active->set_viewport (x, y, width, height);
239 /* Get viewport dimensions. */
240 grub_err_t
241 grub_video_get_viewport (unsigned int *x, unsigned int *y,
242 unsigned int *width, unsigned int *height)
244 if (! grub_video_adapter_active)
245 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
247 return grub_video_adapter_active->get_viewport (x, y, width, height);
250 /* Map color name to adapter specific color. */
251 grub_video_color_t
252 grub_video_map_color (grub_uint32_t color_name)
254 if (! grub_video_adapter_active)
255 return 0;
257 return grub_video_adapter_active->map_color (color_name);
260 /* Map RGB value to adapter specific color. */
261 grub_video_color_t
262 grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue)
264 if (! grub_video_adapter_active)
265 return 0;
267 return grub_video_adapter_active->map_rgb (red, green, blue);
270 /* Map RGBA value to adapter specific color. */
271 grub_video_color_t
272 grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue,
273 grub_uint8_t alpha)
275 if (! grub_video_adapter_active)
276 return 0;
278 return grub_video_adapter_active->map_rgba (red, green, blue, alpha);
281 /* Unmap video color back to RGBA components. */
282 grub_err_t
283 grub_video_unmap_color (grub_video_color_t color, grub_uint8_t *red,
284 grub_uint8_t *green, grub_uint8_t *blue,
285 grub_uint8_t *alpha)
287 if (! grub_video_adapter_active)
288 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
290 return grub_video_adapter_active->unmap_color (color,
291 red,
292 green,
293 blue,
294 alpha);
297 /* Fill rectangle using specified color. */
298 grub_err_t
299 grub_video_fill_rect (grub_video_color_t color, int x, int y,
300 unsigned int width, unsigned int height)
302 if (! grub_video_adapter_active)
303 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
305 return grub_video_adapter_active->fill_rect (color, x, y, width, height);
308 /* Blit bitmap to screen. */
309 grub_err_t
310 grub_video_blit_bitmap (struct grub_video_bitmap *bitmap,
311 enum grub_video_blit_operators oper,
312 int x, int y, int offset_x, int offset_y,
313 unsigned int width, unsigned int height)
315 if (! grub_video_adapter_active)
316 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
318 return grub_video_adapter_active->blit_bitmap (bitmap, oper, x, y,
319 offset_x, offset_y,
320 width, height);
323 /* Blit render target to active render target. */
324 grub_err_t
325 grub_video_blit_render_target (struct grub_video_render_target *target,
326 enum grub_video_blit_operators oper,
327 int x, int y, int offset_x, int offset_y,
328 unsigned int width, unsigned int height)
330 if (! grub_video_adapter_active)
331 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
333 return grub_video_adapter_active->blit_render_target (target, oper, x, y,
334 offset_x, offset_y,
335 width, height);
338 /* Scroll viewport and fill new areas with specified color. */
339 grub_err_t
340 grub_video_scroll (grub_video_color_t color, int dx, int dy)
342 if (! grub_video_adapter_active)
343 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
345 return grub_video_adapter_active->scroll (color, dx, dy);
348 /* Swap buffers (swap active render target). */
349 grub_err_t
350 grub_video_swap_buffers (void)
352 if (! grub_video_adapter_active)
353 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
355 return grub_video_adapter_active->swap_buffers ();
358 /* Create new render target. */
359 grub_err_t
360 grub_video_create_render_target (struct grub_video_render_target **result,
361 unsigned int width, unsigned int height,
362 unsigned int mode_type)
364 if (! grub_video_adapter_active)
365 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
367 return grub_video_adapter_active->create_render_target (result,
368 width, height,
369 mode_type);
372 /* Delete render target. */
373 grub_err_t
374 grub_video_delete_render_target (struct grub_video_render_target *target)
376 if (! grub_video_adapter_active)
377 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
379 return grub_video_adapter_active->delete_render_target (target);
382 /* Set active render target. */
383 grub_err_t
384 grub_video_set_active_render_target (struct grub_video_render_target *target)
386 if (! grub_video_adapter_active)
387 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
389 return grub_video_adapter_active->set_active_render_target (target);
392 /* Get active render target. */
393 grub_err_t
394 grub_video_get_active_render_target (struct grub_video_render_target **target)
396 if (! grub_video_adapter_active)
397 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
399 return grub_video_adapter_active->get_active_render_target (target);
402 grub_err_t
403 grub_video_set_mode (const char *modestring,
404 int NESTED_FUNC_ATTR (*hook) (grub_video_adapter_t p,
405 struct grub_video_mode_info *mode_info))
407 char *tmp;
408 char *next_mode;
409 char *current_mode;
410 char *param;
411 char *value;
412 char *modevar;
413 int width = -1;
414 int height = -1;
415 int depth = -1;
416 int flags = 0;
418 /* Take copy of env.var. as we don't want to modify that. */
419 modevar = grub_strdup (modestring);
421 /* Initialize next mode. */
422 next_mode = modevar;
424 if (! modevar)
425 return grub_error (GRUB_ERR_OUT_OF_MEMORY,
426 "couldn't allocate space for local modevar copy");
428 if (grub_memcmp (next_mode, "keep", sizeof ("keep")) == 0
429 || grub_memcmp (next_mode, "keep,", sizeof ("keep,") - 1) == 0
430 || grub_memcmp (next_mode, "keep;", sizeof ("keep;") - 1) == 0)
432 struct grub_video_mode_info mode_info;
433 int suitable = 1;
434 grub_err_t err;
436 grub_memset (&mode_info, 0, sizeof (mode_info));
438 if (grub_video_adapter_active)
440 err = grub_video_get_info (&mode_info);
441 if (err)
443 suitable = 0;
444 grub_errno = GRUB_ERR_NONE;
447 else
448 mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_PURE_TEXT;
450 if (suitable && hook)
451 suitable = hook (grub_video_adapter_active, &mode_info);
452 if (suitable)
454 grub_free (modevar);
455 return GRUB_ERR_NONE;
457 next_mode += sizeof ("keep") - 1;
458 if (! *next_mode)
460 grub_free (modevar);
462 return grub_error (GRUB_ERR_BAD_ARGUMENT,
463 "No suitable mode found.");
466 /* Skip separator. */
467 next_mode++;
470 /* De-activate last set video adapter. */
471 if (grub_video_adapter_active)
473 /* Finalize adapter. */
474 grub_video_adapter_active->fini ();
475 if (grub_errno != GRUB_ERR_NONE)
476 grub_errno = GRUB_ERR_NONE;
478 /* Mark active adapter as not set. */
479 grub_video_adapter_active = 0;
482 /* Loop until all modes has been tested out. */
483 while (next_mode != NULL)
485 /* Use last next_mode as current mode. */
486 tmp = next_mode;
488 /* Reset video mode settings. */
489 width = -1;
490 height = -1;
491 depth = -1;
492 flags = 0;
494 /* Save position of next mode and separate modes. */
495 for (; *next_mode; next_mode++)
496 if (*next_mode == ',' || *next_mode == ';')
497 break;
498 if (*next_mode)
500 *next_mode = 0;
501 next_mode++;
503 else
504 next_mode = 0;
506 /* Skip whitespace. */
507 while (grub_isspace (*tmp))
508 tmp++;
510 /* Initialize token holders. */
511 current_mode = tmp;
512 param = tmp;
513 value = NULL;
515 /* XXX: we assume that we're in pure text mode if
516 no video mode is initialized. Is it always true? */
517 if (grub_strcmp (param, "text") == 0)
519 struct grub_video_mode_info mode_info;
521 grub_memset (&mode_info, 0, sizeof (mode_info));
522 mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_PURE_TEXT;
524 if (! hook || hook (0, &mode_info))
526 /* Valid mode found from adapter, and it has been activated.
527 Specify it as active adapter. */
528 grub_video_adapter_active = NULL;
530 /* Free memory. */
531 grub_free (modevar);
533 return GRUB_ERR_NONE;
537 /* Parse <width>x<height>[x<depth>]*/
539 /* Find width value. */
540 value = param;
541 param = grub_strchr(param, 'x');
542 if (param == NULL)
544 grub_err_t rc;
546 /* First setup error message. */
547 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
548 "Invalid mode: %s\n",
549 current_mode);
551 /* Free memory before returning. */
552 grub_free (modevar);
554 return rc;
557 *param = 0;
558 param++;
560 width = grub_strtoul (value, 0, 0);
561 if (grub_errno != GRUB_ERR_NONE)
563 grub_err_t rc;
565 /* First setup error message. */
566 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
567 "Invalid mode: %s\n",
568 current_mode);
570 /* Free memory before returning. */
571 grub_free (modevar);
573 return rc;
576 /* Find height value. */
577 value = param;
578 param = grub_strchr(param, 'x');
579 if (param == NULL)
581 height = grub_strtoul (value, 0, 0);
582 if (grub_errno != GRUB_ERR_NONE)
584 grub_err_t rc;
586 /* First setup error message. */
587 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
588 "Invalid mode: %s\n",
589 current_mode);
591 /* Free memory before returning. */
592 grub_free (modevar);
594 return rc;
597 else
599 /* We have optional color depth value. */
600 *param = 0;
601 param++;
603 height = grub_strtoul (value, 0, 0);
604 if (grub_errno != GRUB_ERR_NONE)
606 grub_err_t rc;
608 /* First setup error message. */
609 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
610 "Invalid mode: %s\n",
611 current_mode);
613 /* Free memory before returning. */
614 grub_free (modevar);
616 return rc;
619 /* Convert color depth value. */
620 value = param;
621 depth = grub_strtoul (value, 0, 0);
622 if (grub_errno != GRUB_ERR_NONE)
624 grub_err_t rc;
626 /* First setup error message. */
627 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
628 "Invalid mode: %s\n",
629 current_mode);
631 /* Free memory before returning. */
632 grub_free (modevar);
634 return rc;
638 /* Try out video mode. */
640 /* If we have 8 or less bits, then assume that it is indexed color mode. */
641 if ((depth <= 8) && (depth != -1))
642 flags |= GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
644 /* We have more than 8 bits, then assume that it is RGB color mode. */
645 if (depth > 8)
646 flags |= GRUB_VIDEO_MODE_TYPE_RGB;
648 /* If user requested specific depth, forward that information to driver. */
649 if (depth != -1)
650 flags |= (depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
651 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
653 /* Try to initialize requested mode. Ignore any errors. */
654 grub_video_adapter_t p;
656 /* Loop thru all possible video adapter trying to find requested mode. */
657 for (p = grub_video_adapter_list; p; p = p->next)
659 grub_err_t err;
660 struct grub_video_mode_info mode_info;
662 grub_memset (&mode_info, 0, sizeof (mode_info));
664 /* Try to initialize adapter, if it fails, skip to next adapter. */
665 err = p->init ();
666 if (err != GRUB_ERR_NONE)
668 grub_errno = GRUB_ERR_NONE;
669 continue;
672 /* Try to initialize video mode. */
673 err = p->setup (width, height, flags);
674 if (err != GRUB_ERR_NONE)
676 p->fini ();
677 grub_errno = GRUB_ERR_NONE;
678 continue;
681 err = p->get_info (&mode_info);
682 if (err != GRUB_ERR_NONE)
684 p->fini ();
685 grub_errno = GRUB_ERR_NONE;
686 continue;
689 if (hook && ! hook (p, &mode_info))
691 p->fini ();
692 grub_errno = GRUB_ERR_NONE;
693 continue;
696 /* Valid mode found from adapter, and it has been activated.
697 Specify it as active adapter. */
698 grub_video_adapter_active = p;
700 /* Free memory. */
701 grub_free (modevar);
703 return GRUB_ERR_NONE;
708 /* Free memory. */
709 grub_free (modevar);
711 return grub_error (GRUB_ERR_BAD_ARGUMENT,
712 "No suitable mode found.");
715 /* Initialize Video API module. */
716 GRUB_MOD_INIT(video_video)
720 /* Finalize Video API module. */
721 GRUB_MOD_FINI(video_video)