KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
video.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/video.h
4
5 Copyright (C) 2001 Anders Clerwall (scav)
6 Copyright (C) 2023-2024 Donald Haase
7
8*/
9
10/** \file dc/video.h
11 \brief Functions related to video output.
12 \ingroup video_display
13
14 This file deals with the video output hardware in the Dreamcast. There are
15 functions defined herein that deal with setting up the video hardware,
16 defining the resolution of the display, dealing with the framebuffer, etc.
17
18 \author Anders Clerwall
19 \author Megan Potter
20*/
21
22#ifndef __DC_VIDEO_H
23#define __DC_VIDEO_H
24
25#include <stdbool.h>
26#include <stdint.h>
27#include <sys/cdefs.h>
28__BEGIN_DECLS
29
30/** \defgroup video_display Display
31 \brief Display and framebuffer configuration
32 \ingroup video
33*/
34
35/** \defgroup video_modes Modes
36 \brief Video display modes and management
37 \ingroup video_display
38*/
39
40/** \defgroup video_cables Cables
41 \brief Cable/connector types and management for display
42 \ingroup video_modes
43*/
44
45/** \defgroup vid_ctype Types
46 \brief Type of cable connected to the DC
47 \ingroup video_cables
48
49 The vid_check_cable() function will return one of this set of values to let
50 you know what type of cable is connected to the Dreamcast. These are also
51 used in the video mode settings to limit modes to certain cable types.
52
53 @{
54*/
55#define CT_ANY -1 /**< \brief Any cable type. Used only internally. */
56#define CT_VGA 0 /**< \brief VGA Box */
57#define CT_NONE 1 /**< \brief Nothing connected */
58#define CT_RGB 2 /**< \brief RGB/SCART cable */
59#define CT_COMPOSITE 3 /**< \brief Composite cable or RF switch */
60/** @} */
61
62/** \defgroup video_modes_pixel Pixel Modes
63 \brief Pixel mode settings for the framebuffer
64 \ingroup video_modes
65*/
66
67/** \brief Pixel mode values for the framebuffer
68
69 This set of constants control the pixel mode that the framebuffer is set to.
70 These are hardware-based values and get set in bits 2 and 3 of PVR_FB_CFG_1.
71*/
72typedef enum vid_pixel_mode {
73 PM_RGB555 = 0, /**< \brief RGB555 pixel mode (15-bit) */
74 PM_RGB565 = 1, /**< \brief RGB565 pixel mode (16-bit) */
75 PM_RGB888P = 2, /**< \brief RBG888 packed pixel mode (24-bit) */
76 PM_RGB0888 = 3, /**< \brief RGB0888 pixel mode (32-bit) */
77 PM_RGB888 = 3 /**< \brief Backwards compatibility support */
79
80/** \brief Video pixel mode depths
81 \ingroup video_modes_pixel
82*/
83static const uint8_t vid_pmode_bpp[4] = {2, 2, 3, 4};
84
85/** \defgroup video_modes_display Types
86 \brief Display mode type values
87 \ingroup video_modes
88*/
89
90/** \brief Generic Display Modes
91 \ingroup video_modes_display
92 */
93typedef enum vid_display_mode_generic {
94 DM_GENERIC_FIRST = 0x1000, /**< \brief First valid generic mode */
95 DM_320x240 = 0x1000, /**< \brief 320x240 resolution */
96 DM_640x480, /**< \brief 640x480 resolution */
97 DM_256x256, /**< \brief 256x256 resolution */
98 DM_768x480, /**< \brief 768x480 resolution */
99 DM_768x576, /**< \brief 768x576 resolution */
100 DM_GENERIC_LAST = DM_768x576 /**< \brief Last valid generic mode */
102
103/** \brief Multi-buffered mode setting.
104 \ingroup video_modes_display
105
106 OR this with the generic mode to get multiple framebuffers instead of one.
107*/
108#define DM_MULTIBUFFER 0x2000
109
110//-----------------------------------------------------------------------------
111// More specific modes (and actual indices into the mode table)
112
113/** \brief Specific Display Modes
114 \ingroup video_modes_display
115*/
116typedef enum vid_display_mode {
117 DM_INVALID = 0, /**< \brief Invalid display mode */
118 // Valid modes below
119 DM_320x240_VGA = 1, /**< \brief 320x240 VGA 60Hz */
120 DM_320x240_NTSC, /**< \brief 320x240 NTSC 60Hz */
121 DM_640x480_VGA, /**< \brief 640x480 VGA 60Hz */
122 DM_640x480_NTSC_IL, /**< \brief 640x480 NTSC Interlaced 60Hz */
123 DM_640x480_PAL_IL, /**< \brief 640x480 PAL Interlaced 50Hz */
124 DM_256x256_PAL_IL, /**< \brief 256x256 PAL Interlaced 50Hz */
125 DM_768x480_NTSC_IL, /**< \brief 768x480 NTSC Interlaced 60Hz */
126 DM_768x576_PAL_IL, /**< \brief 768x576 PAL Interlaced 50Hz */
127 DM_768x480_PAL_IL, /**< \brief 768x480 PAL Interlaced 50Hz */
128 DM_320x240_PAL, /**< \brief 320x240 PAL 50Hz */
129 // The below is only for counting..
130 DM_SENTINEL, /**< \brief Sentinel value, for counting */
131 DM_MODE_COUNT /**< \brief Number of modes */
133
134// These are for the "flags" field of "vid_mode_t"
135/** \defgroup vid_flags Flags
136 \brief vid_mode_t Field Flags
137 \ingroup video_modes
138
139 These flags indicate various things related to the modes for a vid_mode_t.
140
141 @{
142*/
143#define VID_INTERLACE 0x00000001 /**< \brief Interlaced display */
144#define VID_LINEDOUBLE 0x00000002 /**< \brief Display each scanline twice */
145#define VID_PIXELDOUBLE 0x00000004 /**< \brief Display each pixel twice */
146#define VID_PAL 0x00000008 /**< \brief 50Hz refresh rate, if not VGA */
147/** @} */
148
149/** \brief Video mode structure.
150 \ingroup video_modes
151
152 KOS maintains a list of valid video modes internally that correspond to the
153 specific display modes enumeration. Each of them is built of one of these.
154
155 \headerfile dc/video.h
156*/
157typedef struct vid_mode {
158 uint16_t generic; /**< \brief Generic mode type for vid_set_mode() */
159 uint16_t width; /**< \brief Width of the display, in pixels */
160 uint16_t height; /**< \brief Height of the display, in pixels */
161 uint32_t flags; /**< \brief Combination of one or more VID_* flags */
162
163 int16_t cable_type; /**< \brief Allowed cable type */
164 vid_pixel_mode_t pm; /**< \brief Pixel mode */
165
166 uint16_t scanlines; /**< \brief Number of scanlines */
167 uint16_t clocks; /**< \brief Clocks per scanline */
168 uint16_t bitmapx; /**< \brief Bitmap window X position */
169 uint16_t bitmapy; /**< \brief Bitmap window Y position (automatically
170 increased for PAL) */
171 uint16_t scanint1; /**< \brief First scanline interrupt position */
172 uint16_t scanint2; /**< \brief Second scanline interrupt position
173 (automatically doubled for VGA) */
174 uint16_t borderx1; /**< \brief Border X starting position */
175 uint16_t borderx2; /**< \brief Border X stop position */
176 uint16_t bordery1; /**< \brief Border Y starting position */
177 uint16_t bordery2; /**< \brief Border Y stop position */
178
179 uint16_t fb_curr; /**< \brief Current framebuffer */
180 uint16_t fb_count; /**< \brief Number of framebuffers */
181 size_t fb_size; /**< \brief Size of each framebuffer */
182} vid_mode_t;
183
184/** \brief The list of builtin video modes. Do not modify these!
185 \ingroup video_modes
186*/
188
189/** \brief The current video mode. Do not modify directly!
190 \ingroup video_modes
191*/
192extern vid_mode_t *vid_mode;
193
194// These point to the current drawing area. If you're not using a multi-buffered
195// mode, that means they do what KOS always used to do (they'll point at the
196// start of VRAM). If you're using a multi-buffered mode, they'll point at the
197// next framebuffer to be displayed. You must use vid_flip for this to work
198// though (if you use vid_set_start, they'll point at the display base, for
199// compatibility's sake).
200
201/** \defgroup video_fb Framebuffer
202 \brief API for framebuffer management
203 \ingroup video_display
204*/
205
206/** \brief 16-bit size pointer to the current drawing area.
207 \ingroup video_fb
208*/
209extern uint16_t *vram_s;
210
211/** \brief 32-bit size pointer to the current drawing area.
212 \ingroup video_fb
213*/
214extern uint32_t *vram_l;
215
216
217/** \brief Retrieve the connected video cable type.
218 \ingroup video_cables
219
220 This function checks the video cable and reports what it finds.
221
222 \retval CT_VGA If a VGA Box or cable is connected.
223 \retval CT_NONE If nothing is connected.
224 \retval CT_RGB If a RGB/SCART cable is connected.
225 \retval CT_COMPOSITE If a composite cable or RF switch is connected.
226*/
227int8_t vid_check_cable(void);
228
229/** \brief Set the VRAM convenience pointers.
230 \ingroup video_fb
231
232 This function sets the vram_s and vram_l pointers to specified offset
233 within VRAM. In multibuffered mode it allows manual management of them.
234
235 \param base The offset within VRAM to set the base to.
236*/
237void vid_set_vram(uint32_t base);
238
239/** \brief Set the VRAM base of the framebuffer.
240 \ingroup video_fb
241
242 This function sets the vram_s and vram_l pointers to specified offset
243 within VRAM and sets the start position of the framebuffer to the same
244 offset.
245
246 \param base The offset within VRAM to set the base to.
247*/
248void vid_set_start(uint32_t base);
249
250/** \brief Get the VRAM base of a framebuffer.
251 \ingroup video_fb
252
253 This function gets the position of the specified framebuffer within VRAM.
254 Any invalid fb value will be treated as the current framebuffer.
255
256 \param fb The number of the framebuffer or -1 for current.
257*/
258uint32_t vid_get_start(int32_t fb);
259
260/** \brief Set the current framebuffer in a multibuffered setup.
261 \ingroup video_fb
262
263 This function sets the displayed framebuffer to the specified buffer.
264 Unlike vid_set_fb, this does not point the vram pointers to the next
265 framebuffer, allowing for non-linear management of the FBs.
266
267 \param fb The framebuffer to display or any other value
268 to display the next one.
269
270*/
271void vid_set_fb(int32_t fb);
272
273/** \brief Flip to a framebuffer in a multibuffered setup.
274 \ingroup video_fb
275
276 This function sets the displayed framebuffer to the specified buffer and
277 sets the vram_s and vram_l pointers to point at the next framebuffer, to
278 allow for tearing-free framebuffer-direct drawing.
279
280 \param fb The framebuffer to display or any other value
281 to display the next one.
282
283*/
284void vid_flip(int32_t fb);
285
286/** \brief Set the border color of the display.
287 \ingroup video_display
288
289 This sets the color of the border area of the display.
290
291 \note
292 On some screens, the border area may not be shown at all,
293 whereas on some displays you may see the whole thing.
294
295 \param r The red value of the color (0-255).
296 \param g The green value of the color (0-255).
297 \param b The blue value of the color (0-255).
298
299 \return Old border color value (RGB888)
300*/
301uint32_t vid_border_color(uint8_t r, uint8_t g, uint8_t b);
302
303/** \brief Clear the framebuffer.
304 \ingroup video_fb
305
306 This function sets the whole framebuffer to the specified color. Internally,
307 this uses the store queues to actually clear the display entirely.
308
309 \note
310 This operates via the vram convenience pointers. In multibuffered mode,
311 by default it will clear the framebuffer you are currently writing to
312 rather than the one being displayed.
313
314 \param r The red value of the color (0-255).
315 \param g The green value of the color (0-255).
316 \param b The blue value of the color (0-255).
317*/
318void vid_clear(uint8_t r, uint8_t g, uint8_t b);
319
320/** \brief Clear VRAM.
321 \ingroup video_fb
322
323 This function is essentially a memset() for the whole of VRAM that will
324 clear it all to 0 bytes.
325*/
326void vid_empty(void);
327
328/** \brief Get the state of video output.
329 \ingroup video_display
330
331 This function gets the state of video output as set via vid_set_enabled.
332
333 \return true if enabled, false if not.
334*/
336
337/** \brief Enable/disable the display.
338 \ingroup video_display
339
340 This function enables or disables video output
341
342 \param val true to enable video output, false to disable.
343
344 \note
345 Unlike vid_clear/vid_empty this does not modify any framebuffer.
346 Instead it merely sets registers that immediately disable output.
347*/
348void vid_set_enabled(bool val);
349
350/** \defgroup video_misc Miscellaneous
351 \brief Miscellaneous video API utilities
352 \ingroup video
353*/
354
355/** \brief Wait for VBlank.
356 \ingroup video_misc
357
358 This function busy loops until the vertical blanking period starts.
359*/
360void vid_waitvbl(void);
361
362/** \brief Set the video mode.
363 \ingroup video_modes
364
365 This function sets the current video mode to the one specified by the
366 parameters.
367
368 \param dm The display mode to use. One of the DM_* values.
369 \param pm The pixel mode to use. One of the PM_* values.
370*/
372
373/** \brief Set the video mode.
374 \ingroup video_modes
375
376 This function sets the current video mode to the mode structure passed in.
377 You can use this to add support to your program for modes that KOS doesn't
378 have support for built-in (of course, you should tell us the settings so we
379 can add them into KOS if you do this).
380
381 \param mode A filled in vid_mode_t for the mode wanted.
382*/
384
385/** \defgroup video_init Initialization
386 \brief Initialization and shutdown of the video subsystem
387 \ingroup video
388
389 @{
390*/
391
392/** \brief Initialize the video system.
393
394 This function initializes the video display, setting the mode to the
395 specified parameters, clearing vram, and setting the first framebuffer as
396 active.
397
398 \param disp_mode The display mode to use. One of the DM_* values.
399 \param pixel_mode The pixel mode to use. One of the PM_* values.
400*/
401void vid_init(int disp_mode, vid_pixel_mode_t pixel_mode);
402
403/** \brief Shut down the video system.
404
405 This function reinitializes the video system to what dcload and friends
406 expect it to be.
407*/
408void vid_shutdown(void);
409
410/** @} */
411
412/** \brief Take a screenshot.
413 \ingroup video_fb
414
415 This function takes the current framebuffer (/vram_l) and dumps it out
416 to a PPM file.
417
418 \param destfn The filename to save to.
419 \return 0 on success, <0 on failure.
420*/
421int vid_screen_shot(const char *destfn);
422
423/** \brief Create image data from the current framebuffer.
424 \ingroup video_fb
425
426 This function takes the current framebuffer (/vram_l) and converts it into
427 24bpp image data. The function allocates memory for the image data, which
428 the caller is responsible for freeing.
429
430 \param buffer A pointer to a uint8_t* that will be allocated and
431 filled with the image data.
432 \return The size of the created image data in bytes, or 0 on failure.
433*/
434size_t vid_screen_shot_data(uint8_t **buffer);
435
436__END_DECLS
437
438#endif // __DC_VIDEO_H
439
int8_t vid_check_cable(void)
Retrieve the connected video cable type.
uint32_t vid_border_color(uint8_t r, uint8_t g, uint8_t b)
Set the border color of the display.
bool vid_get_enabled(void)
Get the state of video output.
void vid_set_enabled(bool val)
Enable/disable the display.
void vid_set_vram(uint32_t base)
Set the VRAM convenience pointers.
uint16_t * vram_s
16-bit size pointer to the current drawing area.
int vid_screen_shot(const char *destfn)
Take a screenshot.
void vid_set_fb(int32_t fb)
Set the current framebuffer in a multibuffered setup.
uint32_t * vram_l
32-bit size pointer to the current drawing area.
void vid_clear(uint8_t r, uint8_t g, uint8_t b)
Clear the framebuffer.
size_t vid_screen_shot_data(uint8_t **buffer)
Create image data from the current framebuffer.
uint32_t vid_get_start(int32_t fb)
Get the VRAM base of a framebuffer.
void vid_set_start(uint32_t base)
Set the VRAM base of the framebuffer.
void vid_empty(void)
Clear VRAM.
void vid_flip(int32_t fb)
Flip to a framebuffer in a multibuffered setup.
void vid_init(int disp_mode, vid_pixel_mode_t pixel_mode)
Initialize the video system.
void vid_shutdown(void)
Shut down the video system.
void vid_waitvbl(void)
Wait for VBlank.
vid_display_mode_generic_t
Generic Display Modes.
Definition video.h:93
vid_display_mode_t
Specific Display Modes.
Definition video.h:116
@ DM_GENERIC_LAST
Last valid generic mode.
Definition video.h:100
@ DM_GENERIC_FIRST
First valid generic mode.
Definition video.h:94
@ DM_640x480
640x480 resolution
Definition video.h:96
@ DM_768x576
768x576 resolution
Definition video.h:99
@ DM_320x240
320x240 resolution
Definition video.h:95
@ DM_768x480
768x480 resolution
Definition video.h:98
@ DM_256x256
256x256 resolution
Definition video.h:97
@ DM_640x480_NTSC_IL
640x480 NTSC Interlaced 60Hz
Definition video.h:122
@ DM_SENTINEL
Sentinel value, for counting.
Definition video.h:130
@ DM_640x480_VGA
640x480 VGA 60Hz
Definition video.h:121
@ DM_320x240_NTSC
320x240 NTSC 60Hz
Definition video.h:120
@ DM_256x256_PAL_IL
256x256 PAL Interlaced 50Hz
Definition video.h:124
@ DM_768x576_PAL_IL
768x576 PAL Interlaced 50Hz
Definition video.h:126
@ DM_320x240_PAL
320x240 PAL 50Hz
Definition video.h:128
@ DM_768x480_PAL_IL
768x480 PAL Interlaced 50Hz
Definition video.h:127
@ DM_640x480_PAL_IL
640x480 PAL Interlaced 50Hz
Definition video.h:123
@ DM_INVALID
Invalid display mode.
Definition video.h:117
@ DM_MODE_COUNT
Number of modes.
Definition video.h:131
@ DM_768x480_NTSC_IL
768x480 NTSC Interlaced 60Hz
Definition video.h:125
@ DM_320x240_VGA
320x240 VGA 60Hz
Definition video.h:119
static const uint8_t vid_pmode_bpp[4]
Video pixel mode depths.
Definition video.h:83
vid_mode_t * vid_mode
The current video mode.
void vid_set_mode(int dm, vid_pixel_mode_t pm)
Set the video mode.
void vid_set_mode_ex(vid_mode_t *mode)
Set the video mode.
vid_mode_t vid_builtin[DM_MODE_COUNT]
The list of builtin video modes.
Video mode structure.
Definition video.h:157
uint16_t scanint1
First scanline interrupt position.
Definition video.h:171
uint32_t flags
Combination of one or more VID_* flags.
Definition video.h:161
uint16_t height
Height of the display, in pixels.
Definition video.h:160
uint16_t scanlines
Number of scanlines.
Definition video.h:166
vid_pixel_mode_t pm
Pixel mode.
Definition video.h:164
uint16_t bitmapy
Bitmap window Y position (automatically increased for PAL)
Definition video.h:169
uint16_t scanint2
Second scanline interrupt position (automatically doubled for VGA)
Definition video.h:172
uint16_t clocks
Clocks per scanline.
Definition video.h:167
size_t fb_size
Size of each framebuffer.
Definition video.h:181
uint16_t fb_count
Number of framebuffers.
Definition video.h:180
uint16_t bitmapx
Bitmap window X position.
Definition video.h:168
uint16_t borderx2
Border X stop position.
Definition video.h:175
uint16_t fb_curr
Current framebuffer.
Definition video.h:179
int16_t cable_type
Allowed cable type.
Definition video.h:163
uint16_t bordery2
Border Y stop position.
Definition video.h:177
uint16_t width
Width of the display, in pixels.
Definition video.h:159
uint16_t bordery1
Border Y starting position.
Definition video.h:176
uint16_t borderx1
Border X starting position.
Definition video.h:174
vid_pixel_mode_t
Pixel mode values for the framebuffer.
Definition video.h:72
@ PM_RGB888P
RBG888 packed pixel mode (24-bit)
Definition video.h:75
@ PM_RGB565
RGB565 pixel mode (16-bit)
Definition video.h:74
@ PM_RGB555
RGB555 pixel mode (15-bit)
Definition video.h:73
@ PM_RGB0888
RGB0888 pixel mode (32-bit)
Definition video.h:76
@ PM_RGB888
Backwards compatibility support.
Definition video.h:77