2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008 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>
23 /* The list of video adapters registered to system. */
24 static grub_video_adapter_t grub_video_adapter_list
;
26 /* Active video adapter. */
27 static grub_video_adapter_t grub_video_adapter_active
;
29 /* Register video driver. */
31 grub_video_register (grub_video_adapter_t adapter
)
33 adapter
->next
= grub_video_adapter_list
;
34 grub_video_adapter_list
= adapter
;
37 /* Unregister video driver. */
39 grub_video_unregister (grub_video_adapter_t adapter
)
41 grub_video_adapter_t
*p
, q
;
43 for (p
= &grub_video_adapter_list
, q
= *p
; q
; p
= &(q
->next
), q
= q
->next
)
51 /* Iterate thru all registered video drivers. */
53 grub_video_iterate (int (*hook
) (grub_video_adapter_t adapter
))
55 grub_video_adapter_t p
;
57 for (p
= grub_video_adapter_list
; p
; p
= p
->next
)
62 /* Setup specified video mode. */
64 grub_video_setup (unsigned int width
, unsigned int height
,
65 unsigned int mode_type
)
67 grub_video_adapter_t p
;
69 /* De-activate last set video adapter. */
70 if (grub_video_adapter_active
)
72 /* Finalize adapter. */
73 grub_video_adapter_active
->fini ();
74 if (grub_errno
!= GRUB_ERR_NONE
)
77 /* Mark active adapter as not set. */
78 grub_video_adapter_active
= 0;
81 /* Loop thru all possible video adapter trying to find requested mode. */
82 for (p
= grub_video_adapter_list
; p
; p
= p
->next
)
84 /* Try to initialize adapter, if it fails, skip to next adapter. */
86 if (grub_errno
!= GRUB_ERR_NONE
)
88 grub_errno
= GRUB_ERR_NONE
;
92 /* Try to initialize video mode. */
93 p
->setup (width
, height
, mode_type
);
94 if (grub_errno
== GRUB_ERR_NONE
)
96 /* Valid mode found from adapter, and it has been activated.
97 Specify it as active adapter. */
98 grub_video_adapter_active
= p
;
102 grub_errno
= GRUB_ERR_NONE
;
104 /* No valid mode found in this adapter, finalize adapter. */
106 if (grub_errno
!= GRUB_ERR_NONE
)
110 /* We couldn't find suitable adapter for specified mode. */
111 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
,
112 "Can't locate valid adapter for mode");
115 /* Restore back to initial mode (where applicable). */
117 grub_video_restore (void)
119 if (grub_video_adapter_active
)
121 grub_video_adapter_active
->fini ();
122 if (grub_errno
!= GRUB_ERR_NONE
)
125 grub_video_adapter_active
= 0;
127 return GRUB_ERR_NONE
;
130 /* Get information about active video mode. */
132 grub_video_get_info (struct grub_video_mode_info
*mode_info
)
134 if (! grub_video_adapter_active
)
135 return grub_error (GRUB_ERR_BAD_DEVICE
, "No video mode activated");
137 /* If mode_info is NULL just report that video adapter is active. */
140 grub_errno
= GRUB_ERR_NONE
;
144 return grub_video_adapter_active
->get_info (mode_info
);
147 /* Determine optimized blitting formation for specified video mode info. */
148 enum grub_video_blit_format
149 grub_video_get_blit_format (struct grub_video_mode_info
*mode_info
)
151 /* Check if we have any known 32 bit modes. */
152 if (mode_info
->bpp
== 32)
154 if ((mode_info
->red_mask_size
== 8)
155 && (mode_info
->red_field_pos
== 0)
156 && (mode_info
->green_mask_size
== 8)
157 && (mode_info
->green_field_pos
== 8)
158 && (mode_info
->blue_mask_size
== 8)
159 && (mode_info
->blue_field_pos
== 16)
160 && (mode_info
->reserved_mask_size
== 8)
161 && (mode_info
->reserved_field_pos
== 24))
163 return GRUB_VIDEO_BLIT_FORMAT_R8G8B8A8
;
167 /* Check if we have any known 24 bit modes. */
168 if (mode_info
->bpp
== 24)
170 if ((mode_info
->red_mask_size
== 8)
171 && (mode_info
->red_field_pos
== 0)
172 && (mode_info
->green_mask_size
== 8)
173 && (mode_info
->green_field_pos
== 8)
174 && (mode_info
->blue_mask_size
== 8)
175 && (mode_info
->blue_field_pos
== 16))
177 return GRUB_VIDEO_BLIT_FORMAT_R8G8B8
;
181 /* If there are more than 8 bits per color, assume RGB(A) mode. */
182 if (mode_info
->bpp
> 8)
184 if (mode_info
->reserved_mask_size
> 0)
186 return GRUB_VIDEO_BLIT_FORMAT_RGBA
;
188 return GRUB_VIDEO_BLIT_FORMAT_RGB
;
191 /* Assume as indexcolor mode. */
192 return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR
;
195 /* Set new indexed color palette entries. */
197 grub_video_set_palette (unsigned int start
, unsigned int count
,
198 struct grub_video_palette_data
*palette_data
)
200 if (! grub_video_adapter_active
)
201 return grub_error (GRUB_ERR_BAD_DEVICE
, "No video mode activated");
203 return grub_video_adapter_active
->set_palette (start
, count
, palette_data
);
206 /* Get indexed color palette entries. */
208 grub_video_get_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
->get_palette (start
, count
, palette_data
);
217 /* Set viewport dimensions. */
219 grub_video_set_viewport (unsigned int x
, unsigned int y
,
220 unsigned int width
, unsigned int height
)
222 if (! grub_video_adapter_active
)
223 return grub_error (GRUB_ERR_BAD_DEVICE
, "No video mode activated");
225 return grub_video_adapter_active
->set_viewport (x
, y
, width
, height
);
228 /* Get viewport dimensions. */
230 grub_video_get_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
->get_viewport (x
, y
, width
, height
);
239 /* Map color name to adapter specific color. */
241 grub_video_map_color (grub_uint32_t color_name
)
243 if (! grub_video_adapter_active
)
246 return grub_video_adapter_active
->map_color (color_name
);
249 /* Map RGB value to adapter specific color. */
251 grub_video_map_rgb (grub_uint8_t red
, grub_uint8_t green
, grub_uint8_t blue
)
253 if (! grub_video_adapter_active
)
256 return grub_video_adapter_active
->map_rgb (red
, green
, blue
);
259 /* Map RGBA value to adapter specific color. */
261 grub_video_map_rgba (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_rgba (red
, green
, blue
, alpha
);
270 /* Unmap video color back to RGBA components. */
272 grub_video_unmap_color (grub_video_color_t color
, grub_uint8_t
*red
,
273 grub_uint8_t
*green
, grub_uint8_t
*blue
,
276 if (! grub_video_adapter_active
)
277 return grub_error (GRUB_ERR_BAD_DEVICE
, "No video mode activated");
279 return grub_video_adapter_active
->unmap_color (color
,
286 /* Fill rectangle using specified color. */
288 grub_video_fill_rect (grub_video_color_t color
, int x
, int y
,
289 unsigned int width
, unsigned int height
)
291 if (! grub_video_adapter_active
)
292 return grub_error (GRUB_ERR_BAD_DEVICE
, "No video mode activated");
294 return grub_video_adapter_active
->fill_rect (color
, x
, y
, width
, height
);
297 /* Blit glyph to screen using specified color. */
299 grub_video_blit_glyph (struct grub_font_glyph
*glyph
,
300 grub_video_color_t color
, int x
, int y
)
302 if (! grub_video_adapter_active
)
303 return grub_error (GRUB_ERR_BAD_DEVICE
, "No video mode activated");
305 return grub_video_adapter_active
->blit_glyph (glyph
, color
, x
, y
);
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
);
402 /* Initialize Video API module. */
403 GRUB_MOD_INIT(video_video
)
405 grub_video_adapter_active
= 0;
406 grub_video_adapter_list
= 0;
409 /* Finalize Video API module. */
410 GRUB_MOD_FINI(video_video
)