2009-08-04 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / video / video.c
blobc22947b63ee2b64037d442dc9d654202f01387d5
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 /* Determine optimized blitting formation for specified video mode info. */
97 enum grub_video_blit_format
98 grub_video_get_blit_format (struct grub_video_mode_info *mode_info)
100 /* Check if we have any known 32 bit modes. */
101 if (mode_info->bpp == 32)
103 if ((mode_info->red_mask_size == 8)
104 && (mode_info->red_field_pos == 16)
105 && (mode_info->green_mask_size == 8)
106 && (mode_info->green_field_pos == 8)
107 && (mode_info->blue_mask_size == 8)
108 && (mode_info->blue_field_pos == 0))
110 return GRUB_VIDEO_BLIT_FORMAT_BGRA_8888;
112 else if ((mode_info->red_mask_size == 8)
113 && (mode_info->red_field_pos == 0)
114 && (mode_info->green_mask_size == 8)
115 && (mode_info->green_field_pos == 8)
116 && (mode_info->blue_mask_size == 8)
117 && (mode_info->blue_field_pos == 16))
119 return GRUB_VIDEO_BLIT_FORMAT_RGBA_8888;
122 /* Check if we have any known 24 bit modes. */
123 else if (mode_info->bpp == 24)
125 if ((mode_info->red_mask_size == 8)
126 && (mode_info->red_field_pos == 16)
127 && (mode_info->green_mask_size == 8)
128 && (mode_info->green_field_pos == 8)
129 && (mode_info->blue_mask_size == 8)
130 && (mode_info->blue_field_pos == 0))
132 return GRUB_VIDEO_BLIT_FORMAT_BGR_888;
134 else if ((mode_info->red_mask_size == 8)
135 && (mode_info->red_field_pos == 0)
136 && (mode_info->green_mask_size == 8)
137 && (mode_info->green_field_pos == 8)
138 && (mode_info->blue_mask_size == 8)
139 && (mode_info->blue_field_pos == 16))
141 return GRUB_VIDEO_BLIT_FORMAT_RGB_888;
144 /* Check if we have any known 16 bit modes. */
145 else if (mode_info->bpp == 16)
147 if ((mode_info->red_mask_size == 5)
148 && (mode_info->red_field_pos == 11)
149 && (mode_info->green_mask_size == 6)
150 && (mode_info->green_field_pos == 5)
151 && (mode_info->blue_mask_size == 5)
152 && (mode_info->blue_field_pos == 0))
154 return GRUB_VIDEO_BLIT_FORMAT_BGR_565;
156 else if ((mode_info->red_mask_size == 5)
157 && (mode_info->red_field_pos == 0)
158 && (mode_info->green_mask_size == 6)
159 && (mode_info->green_field_pos == 5)
160 && (mode_info->blue_mask_size == 5)
161 && (mode_info->blue_field_pos == 11))
163 return GRUB_VIDEO_BLIT_FORMAT_RGB_565;
167 /* Backup route. Unknown format. */
169 /* If there are more than 8 bits per color, assume RGB(A) mode. */
170 if (mode_info->bpp > 8)
172 if (mode_info->reserved_mask_size > 0)
174 return GRUB_VIDEO_BLIT_FORMAT_RGBA;
176 else
178 return GRUB_VIDEO_BLIT_FORMAT_RGB;
182 /* Assume as indexcolor mode. */
183 return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR;
186 /* Set new indexed color palette entries. */
187 grub_err_t
188 grub_video_set_palette (unsigned int start, unsigned int count,
189 struct grub_video_palette_data *palette_data)
191 if (! grub_video_adapter_active)
192 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
194 return grub_video_adapter_active->set_palette (start, count, palette_data);
197 /* Get indexed color palette entries. */
198 grub_err_t
199 grub_video_get_palette (unsigned int start, unsigned int count,
200 struct grub_video_palette_data *palette_data)
202 if (! grub_video_adapter_active)
203 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
205 return grub_video_adapter_active->get_palette (start, count, palette_data);
208 /* Set viewport dimensions. */
209 grub_err_t
210 grub_video_set_viewport (unsigned int x, unsigned int y,
211 unsigned int width, unsigned int height)
213 if (! grub_video_adapter_active)
214 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
216 return grub_video_adapter_active->set_viewport (x, y, width, height);
219 /* Get viewport dimensions. */
220 grub_err_t
221 grub_video_get_viewport (unsigned int *x, unsigned int *y,
222 unsigned int *width, unsigned int *height)
224 if (! grub_video_adapter_active)
225 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
227 return grub_video_adapter_active->get_viewport (x, y, width, height);
230 /* Map color name to adapter specific color. */
231 grub_video_color_t
232 grub_video_map_color (grub_uint32_t color_name)
234 if (! grub_video_adapter_active)
235 return 0;
237 return grub_video_adapter_active->map_color (color_name);
240 /* Map RGB value to adapter specific color. */
241 grub_video_color_t
242 grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue)
244 if (! grub_video_adapter_active)
245 return 0;
247 return grub_video_adapter_active->map_rgb (red, green, blue);
250 /* Map RGBA value to adapter specific color. */
251 grub_video_color_t
252 grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue,
253 grub_uint8_t alpha)
255 if (! grub_video_adapter_active)
256 return 0;
258 return grub_video_adapter_active->map_rgba (red, green, blue, alpha);
261 /* Unmap video color back to RGBA components. */
262 grub_err_t
263 grub_video_unmap_color (grub_video_color_t color, grub_uint8_t *red,
264 grub_uint8_t *green, grub_uint8_t *blue,
265 grub_uint8_t *alpha)
267 if (! grub_video_adapter_active)
268 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
270 return grub_video_adapter_active->unmap_color (color,
271 red,
272 green,
273 blue,
274 alpha);
277 /* Fill rectangle using specified color. */
278 grub_err_t
279 grub_video_fill_rect (grub_video_color_t color, int x, int y,
280 unsigned int width, unsigned int height)
282 if (! grub_video_adapter_active)
283 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
285 return grub_video_adapter_active->fill_rect (color, x, y, width, height);
288 /* Blit bitmap to screen. */
289 grub_err_t
290 grub_video_blit_bitmap (struct grub_video_bitmap *bitmap,
291 enum grub_video_blit_operators oper,
292 int x, int y, int offset_x, int offset_y,
293 unsigned int width, unsigned int height)
295 if (! grub_video_adapter_active)
296 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
298 return grub_video_adapter_active->blit_bitmap (bitmap, oper, x, y,
299 offset_x, offset_y,
300 width, height);
303 /* Blit render target to active render target. */
304 grub_err_t
305 grub_video_blit_render_target (struct grub_video_render_target *target,
306 enum grub_video_blit_operators oper,
307 int x, int y, int offset_x, int offset_y,
308 unsigned int width, unsigned int height)
310 if (! grub_video_adapter_active)
311 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
313 return grub_video_adapter_active->blit_render_target (target, oper, x, y,
314 offset_x, offset_y,
315 width, height);
318 /* Scroll viewport and fill new areas with specified color. */
319 grub_err_t
320 grub_video_scroll (grub_video_color_t color, int dx, int dy)
322 if (! grub_video_adapter_active)
323 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
325 return grub_video_adapter_active->scroll (color, dx, dy);
328 /* Swap buffers (swap active render target). */
329 grub_err_t
330 grub_video_swap_buffers (void)
332 if (! grub_video_adapter_active)
333 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
335 return grub_video_adapter_active->swap_buffers ();
338 /* Create new render target. */
339 grub_err_t
340 grub_video_create_render_target (struct grub_video_render_target **result,
341 unsigned int width, unsigned int height,
342 unsigned int mode_type)
344 if (! grub_video_adapter_active)
345 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
347 return grub_video_adapter_active->create_render_target (result,
348 width, height,
349 mode_type);
352 /* Delete render target. */
353 grub_err_t
354 grub_video_delete_render_target (struct grub_video_render_target *target)
356 if (! grub_video_adapter_active)
357 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
359 return grub_video_adapter_active->delete_render_target (target);
362 /* Set active render target. */
363 grub_err_t
364 grub_video_set_active_render_target (struct grub_video_render_target *target)
366 if (! grub_video_adapter_active)
367 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
369 return grub_video_adapter_active->set_active_render_target (target);
372 /* Get active render target. */
373 grub_err_t
374 grub_video_get_active_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->get_active_render_target (target);
382 grub_err_t
383 grub_video_set_mode (char *modestring,
384 int NESTED_FUNC_ATTR (*hook) (grub_video_adapter_t p,
385 struct grub_video_mode_info *mode_info))
387 char *tmp;
388 char *next_mode;
389 char *current_mode;
390 char *param;
391 char *value;
392 char *modevar;
393 int width = -1;
394 int height = -1;
395 int depth = -1;
396 int flags = 0;
398 /* Take copy of env.var. as we don't want to modify that. */
399 modevar = grub_strdup (modestring);
401 /* Initialize next mode. */
402 next_mode = modevar;
404 if (! modevar)
405 return grub_error (GRUB_ERR_OUT_OF_MEMORY,
406 "couldn't allocate space for local modevar copy");
408 if (grub_memcmp (next_mode, "keep", sizeof ("keep")) == 0
409 || grub_memcmp (next_mode, "keep,", sizeof ("keep,") - 1) == 0
410 || grub_memcmp (next_mode, "keep;", sizeof ("keep;") - 1) == 0)
412 struct grub_video_mode_info mode_info;
413 int suitable = 1;
414 grub_err_t err;
416 grub_memset (&mode_info, 0, sizeof (mode_info));
418 if (grub_video_adapter_active)
420 err = grub_video_get_info (&mode_info);
421 if (err)
423 suitable = 0;
424 grub_errno = GRUB_ERR_NONE;
427 else
428 mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_PURE_TEXT;
430 if (suitable && hook)
431 suitable = hook (grub_video_adapter_active, &mode_info);
432 if (suitable)
434 grub_free (modevar);
435 return GRUB_ERR_NONE;
437 next_mode += sizeof ("keep") - 1;
438 if (! *next_mode)
440 grub_free (modevar);
442 return grub_error (GRUB_ERR_BAD_ARGUMENT,
443 "No suitable mode found.");
446 /* Skip separator. */
447 next_mode++;
450 /* De-activate last set video adapter. */
451 if (grub_video_adapter_active)
453 /* Finalize adapter. */
454 grub_video_adapter_active->fini ();
455 if (grub_errno != GRUB_ERR_NONE)
456 grub_errno = GRUB_ERR_NONE;
458 /* Mark active adapter as not set. */
459 grub_video_adapter_active = 0;
462 /* Loop until all modes has been tested out. */
463 while (next_mode != NULL)
465 /* Use last next_mode as current mode. */
466 tmp = next_mode;
468 /* Reset video mode settings. */
469 width = -1;
470 height = -1;
471 depth = -1;
472 flags = 0;
474 /* Save position of next mode and separate modes. */
475 for (; *next_mode; next_mode++)
476 if (*next_mode == ',' || *next_mode == ';')
477 break;
478 if (*next_mode)
480 *next_mode = 0;
481 next_mode++;
483 else
484 next_mode = 0;
486 /* Skip whitespace. */
487 while (grub_isspace (*tmp))
488 tmp++;
490 /* Initialize token holders. */
491 current_mode = tmp;
492 param = tmp;
493 value = NULL;
495 /* XXX: we assume that we're in pure text mode if
496 no video mode is initialized. Is it always true? */
497 if (grub_strcmp (param, "text") == 0)
499 struct grub_video_mode_info mode_info;
501 grub_memset (&mode_info, 0, sizeof (mode_info));
502 mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_PURE_TEXT;
504 if (! hook || hook (0, &mode_info))
506 /* Valid mode found from adapter, and it has been activated.
507 Specify it as active adapter. */
508 grub_video_adapter_active = NULL;
510 /* Free memory. */
511 grub_free (modevar);
513 return GRUB_ERR_NONE;
517 /* Parse <width>x<height>[x<depth>]*/
519 /* Find width value. */
520 value = param;
521 param = grub_strchr(param, 'x');
522 if (param == NULL)
524 grub_err_t rc;
526 /* First setup error message. */
527 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
528 "Invalid mode: %s\n",
529 current_mode);
531 /* Free memory before returning. */
532 grub_free (modevar);
534 return rc;
537 *param = 0;
538 param++;
540 width = grub_strtoul (value, 0, 0);
541 if (grub_errno != GRUB_ERR_NONE)
543 grub_err_t rc;
545 /* First setup error message. */
546 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
547 "Invalid mode: %s\n",
548 current_mode);
550 /* Free memory before returning. */
551 grub_free (modevar);
553 return rc;
556 /* Find height value. */
557 value = param;
558 param = grub_strchr(param, 'x');
559 if (param == NULL)
561 height = grub_strtoul (value, 0, 0);
562 if (grub_errno != GRUB_ERR_NONE)
564 grub_err_t rc;
566 /* First setup error message. */
567 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
568 "Invalid mode: %s\n",
569 current_mode);
571 /* Free memory before returning. */
572 grub_free (modevar);
574 return rc;
577 else
579 /* We have optional color depth value. */
580 *param = 0;
581 param++;
583 height = grub_strtoul (value, 0, 0);
584 if (grub_errno != GRUB_ERR_NONE)
586 grub_err_t rc;
588 /* First setup error message. */
589 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
590 "Invalid mode: %s\n",
591 current_mode);
593 /* Free memory before returning. */
594 grub_free (modevar);
596 return rc;
599 /* Convert color depth value. */
600 value = param;
601 depth = grub_strtoul (value, 0, 0);
602 if (grub_errno != GRUB_ERR_NONE)
604 grub_err_t rc;
606 /* First setup error message. */
607 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
608 "Invalid mode: %s\n",
609 current_mode);
611 /* Free memory before returning. */
612 grub_free (modevar);
614 return rc;
618 /* Try out video mode. */
620 /* If we have 8 or less bits, then assume that it is indexed color mode. */
621 if ((depth <= 8) && (depth != -1))
622 flags |= GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
624 /* We have more than 8 bits, then assume that it is RGB color mode. */
625 if (depth > 8)
626 flags |= GRUB_VIDEO_MODE_TYPE_RGB;
628 /* If user requested specific depth, forward that information to driver. */
629 if (depth != -1)
630 flags |= (depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
631 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
633 /* Try to initialize requested mode. Ignore any errors. */
634 grub_video_adapter_t p;
636 /* Loop thru all possible video adapter trying to find requested mode. */
637 for (p = grub_video_adapter_list; p; p = p->next)
639 grub_err_t err;
640 struct grub_video_mode_info mode_info;
642 grub_memset (&mode_info, 0, sizeof (mode_info));
644 /* Try to initialize adapter, if it fails, skip to next adapter. */
645 err = p->init ();
646 if (err != GRUB_ERR_NONE)
648 grub_errno = GRUB_ERR_NONE;
649 continue;
652 /* Try to initialize video mode. */
653 err = p->setup (width, height, flags);
654 if (err != GRUB_ERR_NONE)
656 p->fini ();
657 grub_errno = GRUB_ERR_NONE;
658 continue;
661 err = p->get_info (&mode_info);
662 if (err != GRUB_ERR_NONE)
664 p->fini ();
665 grub_errno = GRUB_ERR_NONE;
666 continue;
669 if (hook && ! hook (p, &mode_info))
671 p->fini ();
672 grub_errno = GRUB_ERR_NONE;
673 continue;
676 /* Valid mode found from adapter, and it has been activated.
677 Specify it as active adapter. */
678 grub_video_adapter_active = p;
680 /* Free memory. */
681 grub_free (modevar);
683 return GRUB_ERR_NONE;
688 /* Free memory. */
689 grub_free (modevar);
691 return grub_error (GRUB_ERR_BAD_ARGUMENT,
692 "No suitable mode found.");
695 /* Initialize Video API module. */
696 GRUB_MOD_INIT(video_video)
698 grub_video_adapter_active = 0;
699 grub_video_adapter_list = 0;
702 /* Finalize Video API module. */
703 GRUB_MOD_FINI(video_video)