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 /* Get information about active video mode. */
98 grub_video_get_info_and_fini (struct grub_video_mode_info
*mode_info
,
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
);
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
;
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. */
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. */
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. */
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. */
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. */
252 grub_video_map_color (grub_uint32_t color_name
)
254 if (! grub_video_adapter_active
)
257 return grub_video_adapter_active
->map_color (color_name
);
260 /* Map RGB value to adapter specific color. */
262 grub_video_map_rgb (grub_uint8_t red
, grub_uint8_t green
, grub_uint8_t blue
)
264 if (! grub_video_adapter_active
)
267 return grub_video_adapter_active
->map_rgb (red
, green
, blue
);
270 /* Map RGBA value to adapter specific color. */
272 grub_video_map_rgba (grub_uint8_t red
, grub_uint8_t green
, grub_uint8_t blue
,
275 if (! grub_video_adapter_active
)
278 return grub_video_adapter_active
->map_rgba (red
, green
, blue
, alpha
);
281 /* Unmap video color back to RGBA components. */
283 grub_video_unmap_color (grub_video_color_t color
, grub_uint8_t
*red
,
284 grub_uint8_t
*green
, grub_uint8_t
*blue
,
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
,
297 /* Fill rectangle using specified color. */
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. */
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
,
323 /* Blit render target to active render target. */
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
,
338 /* Scroll viewport and fill new areas with specified color. */
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). */
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. */
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
,
372 /* Delete render target. */
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. */
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. */
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
);
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
))
418 /* Take copy of env.var. as we don't want to modify that. */
419 modevar
= grub_strdup (modestring
);
421 /* Initialize next mode. */
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
;
436 grub_memset (&mode_info
, 0, sizeof (mode_info
));
438 if (grub_video_adapter_active
)
440 err
= grub_video_get_info (&mode_info
);
444 grub_errno
= GRUB_ERR_NONE
;
448 mode_info
.mode_type
= GRUB_VIDEO_MODE_TYPE_PURE_TEXT
;
450 if (suitable
&& hook
)
451 suitable
= hook (grub_video_adapter_active
, &mode_info
);
455 return GRUB_ERR_NONE
;
457 next_mode
+= sizeof ("keep") - 1;
462 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
463 "No suitable mode found.");
466 /* Skip separator. */
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. */
488 /* Reset video mode settings. */
494 /* Save position of next mode and separate modes. */
495 for (; *next_mode
; next_mode
++)
496 if (*next_mode
== ',' || *next_mode
== ';')
506 /* Skip whitespace. */
507 while (grub_isspace (*tmp
))
510 /* Initialize token holders. */
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
;
533 return GRUB_ERR_NONE
;
537 /* Parse <width>x<height>[x<depth>]*/
539 /* Find width value. */
541 param
= grub_strchr(param
, 'x');
546 /* First setup error message. */
547 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
548 "Invalid mode: %s\n",
551 /* Free memory before returning. */
560 width
= grub_strtoul (value
, 0, 0);
561 if (grub_errno
!= GRUB_ERR_NONE
)
565 /* First setup error message. */
566 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
567 "Invalid mode: %s\n",
570 /* Free memory before returning. */
576 /* Find height value. */
578 param
= grub_strchr(param
, 'x');
581 height
= grub_strtoul (value
, 0, 0);
582 if (grub_errno
!= GRUB_ERR_NONE
)
586 /* First setup error message. */
587 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
588 "Invalid mode: %s\n",
591 /* Free memory before returning. */
599 /* We have optional color depth value. */
603 height
= grub_strtoul (value
, 0, 0);
604 if (grub_errno
!= GRUB_ERR_NONE
)
608 /* First setup error message. */
609 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
610 "Invalid mode: %s\n",
613 /* Free memory before returning. */
619 /* Convert color depth value. */
621 depth
= grub_strtoul (value
, 0, 0);
622 if (grub_errno
!= GRUB_ERR_NONE
)
626 /* First setup error message. */
627 rc
= grub_error (GRUB_ERR_BAD_ARGUMENT
,
628 "Invalid mode: %s\n",
631 /* Free memory before returning. */
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. */
646 flags
|= GRUB_VIDEO_MODE_TYPE_RGB
;
648 /* If user requested specific depth, forward that information to driver. */
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
)
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. */
666 if (err
!= GRUB_ERR_NONE
)
668 grub_errno
= GRUB_ERR_NONE
;
672 /* Try to initialize video mode. */
673 err
= p
->setup (width
, height
, flags
);
674 if (err
!= GRUB_ERR_NONE
)
677 grub_errno
= GRUB_ERR_NONE
;
681 err
= p
->get_info (&mode_info
);
682 if (err
!= GRUB_ERR_NONE
)
685 grub_errno
= GRUB_ERR_NONE
;
689 if (hook
&& ! hook (p
, &mode_info
))
692 grub_errno
= GRUB_ERR_NONE
;
696 /* Valid mode found from adapter, and it has been activated.
697 Specify it as active adapter. */
698 grub_video_adapter_active
= p
;
703 return GRUB_ERR_NONE
;
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
)