Search
lxdream.org :: lxdream/src/display.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.h
changeset 1245:01e0020adf88
prev1244:6b54ef5ed413
next1251:b8ab59d39756
author nkeynes
date Fri Mar 02 23:49:10 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Android WIP:
* Rename gui_jni.c to gui_android.c - now quite android specific.
* Implement generic EGL driver with very minimal Java wrapper
* Run emulation in separate thread, and implement simple queue for
inter-thread communication.
* Add menu/action-bar items for start + reset
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * The PC side of the video support (responsible for actually displaying / 
     5  * rendering frames)
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #ifndef lxdream_display_H
    21 #define lxdream_display_H 1
    23 #define GL_GLEXT_PROTOTYPES 1
    25 #include <stdint.h>
    26 #include <stdio.h>
    27 #include <glib.h>
    28 #include "lxdream.h"
    29 #include "gettext.h"
    30 #include "config.h"
    31 #ifdef APPLE_BUILD
    32 #include <OpenGL/gl.h>
    33 #include <OpenGL/glext.h>
    34 #else
    35 #if HAVE_GLES2
    36 #include <GLES2/gl2.h>
    37 #include <GLES2/gl2ext.h>
    38 #else
    39 #include <GL/gl.h>
    40 #include <GL/glext.h>
    41 #endif
    42 #endif
    44 #ifdef __cplusplus
    45 extern "C" {
    46 #endif
    48 /**
    49  * Supported colour formats. Note that BGRA4444 is only ever used for texture
    50  * rendering (it's not valid for display purposes).
    51  */
    52 #define COLFMT_BGRA1555  0
    53 #define COLFMT_RGB565    1
    54 #define COLFMT_BGRA4444  2
    55 #define COLFMT_YUV422    3 /* 8-bit YUV (texture source only) */
    56 #define COLFMT_BGR888    4 /* 24-bit BGR */
    57 #define COLFMT_BGRA8888  5
    58 #define COLFMT_INDEX4    6 /* 4 bit indexed colour (texture source only) */
    59 #define COLFMT_INDEX8    7 /* 8-bit indexed colour (texture source only) */
    60 #define COLFMT_BGR0888   8 /* 32-bit BGR */
    61 #define COLFMT_RGB888    9 /* 24-bit RGB (ie GL native) */
    63 /**
    64  * The standard display size (for the purposes of mouse inputs, etc, is 640x480 -
    65  * events should be adjusted accordingly if this is not the actual window size.
    66  */ 
    67 #define DISPLAY_WIDTH 640
    68 #define DISPLAY_HEIGHT 480
    70 struct colour_format {
    71     GLint type, format, int_format;
    72     int bpp;
    73 };
    74 extern struct colour_format colour_formats[];
    76 extern int colour_format_bytes[];
    78 /**
    79  * Structure to hold pixel data held in GL buffers.
    80  */
    81 struct render_buffer {
    82     uint32_t width;
    83     uint32_t height;
    84     uint32_t rowstride;
    85     uint32_t colour_format;
    86     sh4addr_t address; /* Address buffer was rendered to, or -1 for unrendered */
    87     uint32_t size; /* Size of buffer in bytes, must be width*height*bpp */
    88     gboolean inverted;/* True if the buffer is upside down */
    89     uint32_t scale;
    90     GLuint tex_id;    /* texture bound to render buffer, if any (0 = none). 
    91                        * The render buffer does not own the texture */
    92     unsigned int buf_id; /* driver-specific buffer id, if applicable */
    93     gboolean flushed; /* True if the buffer has been flushed to vram */
    94 };
    96 /**
    97  * Structure to hold pixel data stored in pvr2 vram, as opposed to data in
    98  * GL buffers.
    99  */
   100 struct frame_buffer {
   101     uint32_t width;
   102     uint32_t height;
   103     uint32_t rowstride;
   104     uint32_t colour_format;
   105     sh4addr_t address;
   106     uint32_t size; /* Size of buffer in bytes, must be width*height*bpp */
   107     gboolean inverted;/* True if the buffer is upside down */
   108     unsigned char *data;
   109 };
   111 struct display_capabilities {
   112     gboolean has_gl;
   113     int stencil_bits; /* 0 = no stencil buffer */
   114 };
   116 struct vertex_buffer {
   117     /**
   118      * Map the buffer into the host address space (if necessary) in preparation
   119      * for filling the buffer. This also implies a fence operation.
   120      * @param buf previously allocated buffer
   121      * @param size number of bytes of the buffer actually to be used. The buffer
   122      * will be resized if necessary.
   123      * @return a pointer to the start of the buffer.
   124      */
   125     void *(*map)(vertex_buffer_t buf, uint32_t size);
   127     /**
   128      * Unmap the buffer, after the vertex data is written.
   129      * @return the buffer base to use for gl*Pointer calls
   130      */
   131     void *(*unmap)(vertex_buffer_t buf);
   133     /**
   134      * Mark the buffer as finished, indicating that the vertex data is no
   135      * longer required (ie rendering is complete).
   136      */
   137     void (*finished)(vertex_buffer_t buf);
   139     /**
   140      * Delete the buffer and all associated resources.
   141      */
   142     void (*destroy)(vertex_buffer_t buf);
   144     /* Private data */
   145     void *data;
   146     GLuint id;
   147     uint32_t capacity;
   148     uint32_t mapped_size;
   149     GLuint fence;
   150 };
   153 /**
   154  * Core video driver - exports function to setup a GL context, as well as handle
   155  * keyboard input and display resultant output.
   156  */
   157 typedef struct display_driver {
   158     char *name;
   159     /**
   160      * Short (<60 chars) description of the driver. This should be marked for
   161      * localization.
   162      */
   163     char *description;
   164     /**
   165      * Initialize the driver. This is called only once at startup time, and
   166      * is guaranteed to be called before any other methods.
   167      * @return TRUE if the driver was successfully initialized, otherwise
   168      * FALSE.
   169      */
   170     gboolean (*init_driver)(void);
   172     /**
   173      * Cleanly shutdown the driver. Normally only called at system shutdown
   174      * time.
   175      */
   176     void (*shutdown_driver)(void);
   178     /**
   179      * Given a particular keysym, return the keycode associated with it.
   180      * @param keysym The keysym to be resolved, ie "Tab"
   181      * @return the display-specific keycode, or 0 if the keysym cannot
   182      * be resolved.
   183      */
   184     uint16_t (*resolve_keysym)( const gchar *keysym );
   186     /**
   187      * Given a native system keycode, convert it to a dreamcast keyboard code.
   188      */
   189     uint16_t (*convert_to_dckeysym)( uint16_t keycode );
   191     /**
   192      * Given a device-specific event code, return the corresponding keysym.
   193      * The string should be newly allocated (caller will free)
   194      */
   195     gchar *(*get_keysym_for_keycode)( uint16_t keycode );
   197     /**
   198      * Create a render target with the given width and height. If a texture ID
   199      * is supplied (non-zero), the render buffer writes its output to that texture.
   200      * @param tex_id 0 or a valid GL texture name which must have been initialized to
   201      * the correct dimensions. 
   202      */
   203     render_buffer_t (*create_render_buffer)( uint32_t width, uint32_t height, GLuint tex_id );
   205     /**
   206      * Destroy the specified render buffer and release any associated
   207      * resources.
   208      */
   209     void (*destroy_render_buffer)( render_buffer_t buffer );
   211     /**
   212      * Set the current rendering target to the specified buffer.
   213      */
   214     gboolean (*set_render_target)( render_buffer_t buffer );
   216     /**
   217      * Complete rendering and clear the current rendering target. If the 
   218      * buffer has a bound texture, it will be updated if necessary.
   219      */
   220     void (*finish_render)( render_buffer_t buffer );
   222     /**
   223      * Load the supplied frame buffer into the given render buffer.
   224      * Included here to allow driver-specific optimizations.
   225      */
   226     void (*load_frame_buffer)( frame_buffer_t frame, render_buffer_t render );
   228     /**
   229      * Display a single frame using a previously rendered GL buffer.
   230      */
   231     void (*display_render_buffer)( render_buffer_t buffer );
   233     /**
   234      * Display a single blanked frame using a fixed colour for the
   235      * entire frame (specified in BGR888 format). 
   236      */
   237     void (*display_blank)( uint32_t rgb );
   239     /**
   240      * Swap front/back window buffers
   241      */
   242     void (*swap_buffers)();
   244     /**
   245      * Copy the image data from the GL buffer to the target memory buffer,
   246      * using the format etc from the buffer. This may force a glFinish()
   247      * but does not invalidate the buffer.
   248      * @param target buffer to fill with image data, which must be large enough
   249      *  to accomodate the image.
   250      * @param buffer Render buffer to read from.
   251      * @param rowstride rowstride of the target data
   252      * @param format colour format to output the data in.
   253      */
   254     gboolean (*read_render_buffer)( unsigned char *target, render_buffer_t buffer,
   255             int rowstride, int format );
   257     /**
   258      * Create a new vertex buffer
   259      */
   260     vertex_buffer_t (*create_vertex_buffer)( );
   262     /**
   263      * Dump driver-specific information about the implementation to the given stream
   264      */
   265     void (*print_info)( FILE *out );
   267     struct display_capabilities capabilities;
   269 } *display_driver_t;
   271 /**
   272  * Print the configured video drivers to the output stream, one to a line.
   273  */
   274 void print_display_drivers( FILE *out );
   275 display_driver_t get_display_driver_by_name( const char *name );
   276 gboolean display_set_driver( display_driver_t driver );
   278 extern uint32_t pvr2_frame_counter;
   280 extern display_driver_t display_driver;
   282 extern struct display_driver display_gtk_driver;
   283 extern struct display_driver display_osx_driver;
   284 extern struct display_driver display_gl_driver;
   285 extern struct display_driver display_egl_driver;
   286 extern struct display_driver display_null_driver;
   288 /****************** Input methods **********************/
   290 #define MAX_MOUSE_BUTTONS 32
   292 /* Pressure is 0..127  (allowing a joystick to be defined as two half-axes of 7- bits each) */
   293 #define MAX_PRESSURE 0x7F
   295 typedef key_binding_t input_key_callback_t;
   297 /**
   298  * Callback to receive mouse input events
   299  * @param data pointer passed in at the time the hook was registered
   300  * @param buttons bitmask of button states, where bit 0 is button 0 (left), bit 1 is button
   301  * 1 (middle), bit 2 is button 2 (right) and so forth.
   302  * @param x Horizontal movement since the last invocation (in relative mode) or window position 
   303  * (in absolute mode).
   304  * @param y Vertical movement since the last invocation (in relative mode) or window position
   305  * (in absolute mode).
   306  * @param absolute If TRUE, x and y are the current window coordinates 
   307  *  of the mouse cursor. Otherwise, x and y are deltas from the previous mouse position.
   308  */
   309 typedef void (*input_mouse_callback_t)( void *data, uint32_t buttons, int32_t x, int32_t y, gboolean absolute );
   311 gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
   312                              void *data, uint32_t value );
   314 void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
   315                            void *data, uint32_t value );
   317 gboolean input_register_keygroup( lxdream_config_group_t group );
   318 void input_unregister_keygroup( lxdream_config_group_t group );
   319 gboolean input_keygroup_changed( void *data, lxdream_config_group_t group, unsigned key,
   320                              const gchar *oldval, const gchar *newval );
   322 /**
   323  * Register a hook to receive all keyboard input events
   324  */
   325 gboolean input_register_keyboard_hook( input_key_callback_t callback, void *data );
   326 void input_unregister_keyboard_hook( input_key_callback_t callback, void *data );
   328 /**
   329  * Register a mouse event hook.
   330  * @param relative TRUE if the caller wants relative mouse movement, FALSE for
   331  * absolute mouse positioning. It's not generally possible to receive both at the same time.
   332  */
   333 gboolean input_register_mouse_hook( gboolean relative, input_mouse_callback_t callback, void *data );
   334 void input_unregister_mouse_hook( input_mouse_callback_t callback, void *data );
   336 gboolean input_is_key_valid( const gchar *keysym );
   338 gboolean input_is_key_registered( const gchar *keysym );
   340 uint16_t input_keycode_to_dckeysym( uint16_t keycode );
   342 /********************** Display/Input methods ***********************/
   344 /**
   345  * Auxilliary input driver - provides input separate to and in addition to the
   346  * core UI/display. (primarily used for joystick devices)
   347  */
   348 typedef struct input_driver {
   349     const char *id; /* Short identifier to display in the UI for the device (eg "JS0" ) */
   351     /**
   352      * Given a particular keysym, return the keycode associated with it.
   353      * @param keysym The keysym to be resolved, ie "Tab"
   354      * @return the display-specific keycode, or 0 if the keysym cannot
   355      * be resolved.
   356      */
   357     uint16_t (*resolve_keysym)( struct input_driver *driver, const gchar *keysym );
   359     /**
   360      * Given a device-specific event code, convert it to a dreamcast keyboard code.
   361      * This is only required for actual keyboard devices, other devices should just
   362      * leave this method NULL.
   363      */
   364     uint16_t (*convert_to_dckeysym)( struct input_driver *driver, uint16_t keycode );
   366     /**
   367      * Given a device-specific event code, return the corresponding keysym.
   368      * The string should be newly allocated (caller will free)
   369      */
   370     gchar *(*get_keysym_for_keycode)( struct input_driver *driver, uint16_t keycode );
   372     /**
   373      * Destroy the input driver.
   374      */
   375     void (*destroy)( struct input_driver *driver );
   377 } *input_driver_t;       
   379 extern struct input_driver system_mouse_driver;
   381 /**
   382  * Register a new input driver (which must have a unique name)
   383  * @param driver the driver to register
   384  * @param max_keycode the highest possible keycode reported by the device
   385  * @return TRUE on success, FALSE on failure (eg driver already registed).
   386  */
   387 gboolean input_register_device( input_driver_t driver, uint16_t max_keycode );
   389 /**
   390  * Determine if the system has an input driver with the given unique ID.
   391  * @param id driver id to check
   392  * @return TRUE if the device exists, otherwise FALSE
   393  */
   394 gboolean input_has_device( const gchar *id );
   396 /**
   397  * Unregister an input driver.
   398  * @param driver the driver to unregister
   399  * If the driver is not in fact registered, this function has no effect.
   400  */
   401 void input_unregister_device( input_driver_t driver );
   403 /**
   404  * Called from the UI to indicate that the emulation window is focused (ie
   405  * able to receive input). This method is used to gate non-UI input devices -
   406  * when the display is not focused, all input events will be silently ignored.
   407  */
   408 void display_set_focused( gboolean has_focus );
   410 /**
   411  * Fire a keydown event on the specified device
   412  * @param input The input device source generating the event, or NULL for the 
   413  *        default GUI device
   414  * @param keycode The device-specific keycode
   415  * @param pressure The pressure of the key (0 to 127), where 0 is unpressed and
   416  *        127 is maximum pressure. Devices without pressure sensitivity should 
   417  *        always use MAX_PRESSURE (127) 
   418  */
   419 void input_event_keydown( input_driver_t input, uint16_t keycode, uint32_t pressure );
   421 void input_event_keyup( input_driver_t input, uint16_t keycode );
   423 /**
   424  * Receive an input mouse down event. Normally these should be absolute events when
   425  * the mouse is not grabbed, and relative when it is.
   426  * @param button the button pressed, where 0 == first button
   427  * @param x_axis The relative or absolute position of the mouse cursor on the X axis
   428  * @param y_axis The relative or absolute position of the mouse cursor on the Y axis
   429  * @param absolute If TRUE, x_axis and y_axis are the current window coordinates 
   430  *  of the mouse cursor. Otherwise, x_axis and y_axis are deltas from the previous mouse position.
   431  */
   432 void input_event_mousedown( uint16_t button, int32_t x_axis, int32_t y_axis, gboolean absolute );
   434 /**
   435  * Receive an input mouse up event. Normally these should be absolute events when
   436  * the mouse is not grabbed, and relative when it is.
   437  * @param button the button released, where 0 == first button
   438  * @param x_axis The relative or absolute position of the mouse cursor on the X axis
   439  * @param y_axis The relative or absolute position of the mouse cursor on the Y axis
   440  * @param absolute If TRUE, x_axis and y_axis are the current window coordinates 
   441  *  of the mouse cursor. Otherwise, x_axis and y_axis are deltas from the previous mouse position.
   442  */
   443 void input_event_mouseup( uint16_t button, int32_t x_axis, int32_t y_axis, gboolean absolute );
   445 /**
   446  * Receive an input mouse motion event. Normally these should be absolute events when
   447  * the mouse is not grabbed, and relative when it is.
   448  * @param x_axis The relative or absolute position of the mouse cursor on the X axis
   449  * @param y_axis The relative or absolute position of the mouse cursor on the Y axis
   450  * @param absolute If TRUE, x_axis and y_axis are the current window coordinates 
   451  *  of the mouse cursor. Otherwise, x_axis and y_axis are deltas from the previous mouse position.
   452  */
   453 void input_event_mousemove( int32_t x_axis, int32_t y_axis, gboolean absolute );
   455 /**
   456  * Given a keycode and the originating input driver, return the corresponding 
   457  * keysym. The caller is responsible for freeing the string.
   458  * @return a newly allocated string, or NULL of the keycode is unresolvable.
   459  */
   460 gchar *input_keycode_to_keysym( input_driver_t driver, uint16_t keycode );
   462 typedef void (*display_keysym_callback_t)( void *data, const gchar *keysym );
   464 /**
   465  * Set the keysym hook function (normally used by the UI to receive non-UI
   466  * input events during configuration.
   467  */
   468 void input_set_keysym_hook( display_keysym_callback_t hook, void *data );
   472 #ifdef __cplusplus
   473 }
   474 #endif
   475 #endif /* !lxdream_display_H */
.