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>
22 #include <grub/misc.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. */
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. */
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
)
53 /* Iterate thru all registered video drivers. */
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
)
64 /* Restore back to initial mode (where applicable). */
66 grub_video_restore (void)
68 if (grub_video_adapter_active
)
70 grub_video_adapter_active
->fini ();
71 if (grub_errno
!= GRUB_ERR_NONE
)
74 grub_video_adapter_active
= 0;
79 /* Get information about active video mode. */
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. */
89 grub_errno
= GRUB_ERR_NONE
;
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
;
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. */
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. */
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. */
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. */
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. */
232 grub_video_map_color (grub_uint32_t color_name
)
234 if (! grub_video_adapter_active
)
237 return grub_video_adapter_active
->map_color (color_name
);
240 /* Map RGB value to adapter specific color. */
242 grub_video_map_rgb (grub_uint8_t red
, grub_uint8_t green
, grub_uint8_t blue
)
244 if (! grub_video_adapter_active
)
247 return grub_video_adapter_active
->map_rgb (red
, green
, blue
);
250 /* Map RGBA value to adapter specific color. */
252 grub_video_map_rgba (grub_uint8_t red
, grub_uint8_t green
, grub_uint8_t blue
,
255 if (! grub_video_adapter_active
)
258 return grub_video_adapter_active
->map_rgba (red
, green
, blue
, alpha
);
261 /* Unmap video color back to RGBA components. */
263 grub_video_unmap_color (grub_video_color_t color
, grub_uint8_t
*red
,
264 grub_uint8_t
*green
, grub_uint8_t
*blue
,
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
,
277 /* Fill rectangle using specified color. */
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. */
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
,
303 /* Blit render target to active render target. */
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
,
318 /* Scroll viewport and fill new areas with specified color. */
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). */
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. */
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
,
352 /* Delete render target. */
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. */
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. */
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
);
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
))
398 /* Take copy of env.var. as we don't want to modify that. */
399 modevar
= grub_strdup (modestring
);
401 /* Initialize next mode. */
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
;
416 grub_memset (&mode_info
, 0, sizeof (mode_info
));
418 if (grub_video_adapter_active
)
420 err
= grub_video_get_info (&mode_info
);
424 grub_errno
= GRUB_ERR_NONE
;
428 mode_info
.mode_type
= GRUB_VIDEO_MODE_TYPE_PURE_TEXT
;
430 if (suitable
&& hook
)
431 suitable
= hook (grub_video_adapter_active
, &mode_info
);
435 return GRUB_ERR_NONE
;
437 next_mode
+= sizeof ("keep") - 1;
442 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
443 "No suitable mode found.");
446 /* Skip separator. */
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. */
468 /* Reset video mode settings. */
474 /* Save position of next mode and separate modes. */
475 for (; *next_mode
; next_mode
++)
476 if (*next_mode
== ',' || *next_mode
== ';')
486 /* Skip whitespace. */
487 while (grub_isspace (*tmp
))
490 /* Initialize token holders. */
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
;
513 return GRUB_ERR_NONE
;
517 /* Parse <width>x<height>[x<depth>]*/
519 /* Find width value. */
521 param
= grub_strchr(param
, 'x');
526 /* First setup error message. */
527 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
528 "Invalid mode: %s\n",
531 /* Free memory before returning. */
540 width
= grub_strtoul (value
, 0, 0);
541 if (grub_errno
!= GRUB_ERR_NONE
)
545 /* First setup error message. */
546 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
547 "Invalid mode: %s\n",
550 /* Free memory before returning. */
556 /* Find height value. */
558 param
= grub_strchr(param
, 'x');
561 height
= grub_strtoul (value
, 0, 0);
562 if (grub_errno
!= GRUB_ERR_NONE
)
566 /* First setup error message. */
567 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
568 "Invalid mode: %s\n",
571 /* Free memory before returning. */
579 /* We have optional color depth value. */
583 height
= grub_strtoul (value
, 0, 0);
584 if (grub_errno
!= GRUB_ERR_NONE
)
588 /* First setup error message. */
589 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
590 "Invalid mode: %s\n",
593 /* Free memory before returning. */
599 /* Convert color depth value. */
601 depth
= grub_strtoul (value
, 0, 0);
602 if (grub_errno
!= GRUB_ERR_NONE
)
606 /* First setup error message. */
607 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
608 "Invalid mode: %s\n",
611 /* Free memory before returning. */
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. */
626 flags
|= GRUB_VIDEO_MODE_TYPE_RGB
;
628 /* If user requested specific depth, forward that information to driver. */
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
)
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. */
646 if (err
!= GRUB_ERR_NONE
)
648 grub_errno
= GRUB_ERR_NONE
;
652 /* Try to initialize video mode. */
653 err
= p
->setup (width
, height
, flags
);
654 if (err
!= GRUB_ERR_NONE
)
657 grub_errno
= GRUB_ERR_NONE
;
661 err
= p
->get_info (&mode_info
);
662 if (err
!= GRUB_ERR_NONE
)
665 grub_errno
= GRUB_ERR_NONE
;
669 if (hook
&& ! hook (p
, &mode_info
))
672 grub_errno
= GRUB_ERR_NONE
;
676 /* Valid mode found from adapter, and it has been activated.
677 Specify it as active adapter. */
678 grub_video_adapter_active
= p
;
683 return GRUB_ERR_NONE
;
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
)