src/video/SDL_sysvideo.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 11 Jul 2013 22:59:20 -0700
changeset 7412 50211a1fd557
parent 7390 e4b98404baa4
child 7523 9e9ab1dc3811
permissions -rw-r--r--
Fixed bug 1946 - OpenGL contexts in threads

The SDL OpenGL context code is now properly thread aware. There are two new functions which return the current OpenGL window and context for the current thread.

There are still places in the cocoa driver where the OpenGL context needs to be updated when the view changes. These will need a different solution and still use the last globally set context to avoid changing behavior.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #ifndef _SDL_sysvideo_h
    24 #define _SDL_sysvideo_h
    25 
    26 #include "SDL_messagebox.h"
    27 #include "SDL_shape.h"
    28 #include "SDL_thread.h"
    29 
    30 /* The SDL video driver */
    31 
    32 typedef struct SDL_WindowShaper SDL_WindowShaper;
    33 typedef struct SDL_ShapeDriver SDL_ShapeDriver;
    34 typedef struct SDL_VideoDisplay SDL_VideoDisplay;
    35 typedef struct SDL_VideoDevice SDL_VideoDevice;
    36 
    37 /* Define the SDL window-shaper structure */
    38 struct SDL_WindowShaper
    39 {
    40     /* The window associated with the shaper */
    41     SDL_Window *window;
    42 
    43     /* The user's specified coordinates for the window, for once we give it a shape. */
    44     Uint32 userx,usery;
    45 
    46     /* The parameters for shape calculation. */
    47     SDL_WindowShapeMode mode;
    48 
    49     /* Has this window been assigned a shape? */
    50     SDL_bool hasshape;
    51 
    52     void *driverdata;
    53 };
    54 
    55 /* Define the SDL shape driver structure */
    56 struct SDL_ShapeDriver
    57 {
    58     SDL_WindowShaper *(*CreateShaper)(SDL_Window * window);
    59     int (*SetWindowShape)(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode);
    60     int (*ResizeWindowShape)(SDL_Window *window);
    61 };
    62 
    63 typedef struct SDL_WindowUserData
    64 {
    65     char *name;
    66     void *data;
    67     struct SDL_WindowUserData *next;
    68 } SDL_WindowUserData;
    69 
    70 /* Define the SDL window structure, corresponding to toplevel windows */
    71 struct SDL_Window
    72 {
    73     const void *magic;
    74     Uint32 id;
    75     char *title;
    76     int x, y;
    77     int w, h;
    78     int min_w, min_h;
    79     int max_w, max_h;
    80     Uint32 flags;
    81 
    82     /* Stored position and size for windowed mode */
    83     SDL_Rect windowed;
    84 
    85     SDL_DisplayMode fullscreen_mode;
    86 
    87     float brightness;
    88     Uint16 *gamma;
    89     Uint16 *saved_gamma;        /* (just offset into gamma) */
    90 
    91     SDL_Surface *surface;
    92     SDL_bool surface_valid;
    93 
    94     SDL_WindowShaper *shaper;
    95 
    96     SDL_WindowUserData *data;
    97 
    98     void *driverdata;
    99 
   100     SDL_Window *prev;
   101     SDL_Window *next;
   102 };
   103 #define FULLSCREEN_VISIBLE(W) \
   104     (((W)->flags & SDL_WINDOW_FULLSCREEN) && \
   105      ((W)->flags & SDL_WINDOW_SHOWN) && \
   106      !((W)->flags & SDL_WINDOW_MINIMIZED))
   107 
   108 /*
   109  * Define the SDL display structure This corresponds to physical monitors
   110  * attached to the system.
   111  */
   112 struct SDL_VideoDisplay
   113 {
   114     char *name;
   115     int max_display_modes;
   116     int num_display_modes;
   117     SDL_DisplayMode *display_modes;
   118     SDL_DisplayMode desktop_mode;
   119     SDL_DisplayMode current_mode;
   120 
   121     SDL_Window *fullscreen_window;
   122 
   123     SDL_VideoDevice *device;
   124 
   125     void *driverdata;
   126 };
   127 
   128 /* Forward declaration */
   129 struct SDL_SysWMinfo;
   130 
   131 /* Define the SDL video driver structure */
   132 #define _THIS   SDL_VideoDevice *_this
   133 
   134 struct SDL_VideoDevice
   135 {
   136     /* * * */
   137     /* The name of this video driver */
   138     const char *name;
   139 
   140     /* * * */
   141     /* Initialization/Query functions */
   142 
   143     /*
   144      * Initialize the native video subsystem, filling in the list of
   145      * displays for this driver, returning 0 or -1 if there's an error.
   146      */
   147     int (*VideoInit) (_THIS);
   148 
   149     /*
   150      * Reverse the effects VideoInit() -- called if VideoInit() fails or
   151      * if the application is shutting down the video subsystem.
   152      */
   153     void (*VideoQuit) (_THIS);
   154 
   155     /* * * */
   156     /*
   157      * Display functions
   158      */
   159 
   160     /*
   161      * Get the bounds of a display
   162      */
   163     int (*GetDisplayBounds) (_THIS, SDL_VideoDisplay * display, SDL_Rect * rect);
   164 
   165     /*
   166      * Get a list of the available display modes for a display.
   167      */
   168     void (*GetDisplayModes) (_THIS, SDL_VideoDisplay * display);
   169 
   170     /*
   171      * Setting the display mode is independent of creating windows, so
   172      * when the display mode is changed, all existing windows should have
   173      * their data updated accordingly, including the display surfaces
   174      * associated with them.
   175      */
   176     int (*SetDisplayMode) (_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
   177 
   178     /* * * */
   179     /*
   180      * Window functions
   181      */
   182     int (*CreateWindow) (_THIS, SDL_Window * window);
   183     int (*CreateWindowFrom) (_THIS, SDL_Window * window, const void *data);
   184     void (*SetWindowTitle) (_THIS, SDL_Window * window);
   185     void (*SetWindowIcon) (_THIS, SDL_Window * window, SDL_Surface * icon);
   186     void (*SetWindowPosition) (_THIS, SDL_Window * window);
   187     void (*SetWindowSize) (_THIS, SDL_Window * window);
   188     void (*SetWindowMinimumSize) (_THIS, SDL_Window * window);
   189     void (*SetWindowMaximumSize) (_THIS, SDL_Window * window);
   190     void (*ShowWindow) (_THIS, SDL_Window * window);
   191     void (*HideWindow) (_THIS, SDL_Window * window);
   192     void (*RaiseWindow) (_THIS, SDL_Window * window);
   193     void (*MaximizeWindow) (_THIS, SDL_Window * window);
   194     void (*MinimizeWindow) (_THIS, SDL_Window * window);
   195     void (*RestoreWindow) (_THIS, SDL_Window * window);
   196     void (*SetWindowBordered) (_THIS, SDL_Window * window, SDL_bool bordered);
   197     void (*SetWindowFullscreen) (_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
   198     int (*SetWindowGammaRamp) (_THIS, SDL_Window * window, const Uint16 * ramp);
   199     int (*GetWindowGammaRamp) (_THIS, SDL_Window * window, Uint16 * ramp);
   200     void (*SetWindowGrab) (_THIS, SDL_Window * window, SDL_bool grabbed);
   201     void (*DestroyWindow) (_THIS, SDL_Window * window);
   202     int (*CreateWindowFramebuffer) (_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch);
   203     int (*UpdateWindowFramebuffer) (_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects);
   204     void (*DestroyWindowFramebuffer) (_THIS, SDL_Window * window);
   205     void (*OnWindowEnter) (_THIS, SDL_Window * window);
   206 
   207     /* * * */
   208     /*
   209      * Shaped-window functions
   210      */
   211     SDL_ShapeDriver shape_driver;
   212 
   213     /* Get some platform dependent window information */
   214       SDL_bool(*GetWindowWMInfo) (_THIS, SDL_Window * window,
   215                                   struct SDL_SysWMinfo * info);
   216 
   217     /* * * */
   218     /*
   219      * OpenGL support
   220      */
   221     int (*GL_LoadLibrary) (_THIS, const char *path);
   222     void *(*GL_GetProcAddress) (_THIS, const char *proc);
   223     void (*GL_UnloadLibrary) (_THIS);
   224       SDL_GLContext(*GL_CreateContext) (_THIS, SDL_Window * window);
   225     int (*GL_MakeCurrent) (_THIS, SDL_Window * window, SDL_GLContext context);
   226     int (*GL_SetSwapInterval) (_THIS, int interval);
   227     int (*GL_GetSwapInterval) (_THIS);
   228     void (*GL_SwapWindow) (_THIS, SDL_Window * window);
   229     void (*GL_DeleteContext) (_THIS, SDL_GLContext context);
   230 
   231     /* * * */
   232     /*
   233      * Event manager functions
   234      */
   235     void (*PumpEvents) (_THIS);
   236 
   237     /* Suspend the screensaver */
   238     void (*SuspendScreenSaver) (_THIS);
   239 
   240     /* Text input */
   241     void (*StartTextInput) (_THIS);
   242     void (*StopTextInput) (_THIS);
   243     void (*SetTextInputRect) (_THIS, SDL_Rect *rect);
   244 
   245     /* Screen keyboard */
   246     SDL_bool (*HasScreenKeyboardSupport) (_THIS);
   247     void (*ShowScreenKeyboard) (_THIS, SDL_Window *window);
   248     void (*HideScreenKeyboard) (_THIS, SDL_Window *window);
   249     SDL_bool (*IsScreenKeyboardShown) (_THIS, SDL_Window *window);
   250 
   251     /* Clipboard */
   252     int (*SetClipboardText) (_THIS, const char *text);
   253     char * (*GetClipboardText) (_THIS);
   254     SDL_bool (*HasClipboardText) (_THIS);
   255 
   256     /* MessageBox */
   257     int (*ShowMessageBox) (_THIS, const SDL_MessageBoxData *messageboxdata, int *buttonid);
   258 
   259     /* * * */
   260     /* Data common to all drivers */
   261     SDL_bool suspend_screensaver;
   262     int num_displays;
   263     SDL_VideoDisplay *displays;
   264     SDL_Window *windows;
   265     Uint8 window_magic;
   266     Uint32 next_object_id;
   267     char * clipboard_text;
   268 
   269     /* * * */
   270     /* Data used by the GL drivers */
   271     struct
   272     {
   273         int red_size;
   274         int green_size;
   275         int blue_size;
   276         int alpha_size;
   277         int depth_size;
   278         int buffer_size;
   279         int stencil_size;
   280         int double_buffer;
   281         int accum_red_size;
   282         int accum_green_size;
   283         int accum_blue_size;
   284         int accum_alpha_size;
   285         int stereo;
   286         int multisamplebuffers;
   287         int multisamplesamples;
   288         int accelerated;
   289         int major_version;
   290         int minor_version;
   291         int flags;
   292         int profile_mask;
   293         int use_egl;
   294         int share_with_current_context;
   295         int retained_backing;
   296         int driver_loaded;
   297         char driver_path[256];
   298         void *dll_handle;
   299     } gl_config;
   300 
   301     /* * * */
   302     /* Cache current GL context; don't call the OS when it hasn't changed. */
   303     SDL_Window *current_glwin;
   304     SDL_GLContext current_glctx;
   305     SDL_TLSID current_glwin_tls;
   306     SDL_TLSID current_glctx_tls;
   307 
   308     /* * * */
   309     /* Data private to this driver */
   310     void *driverdata;
   311     struct SDL_GLDriverData *gl_data;
   312 
   313 #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
   314     struct SDL_PrivateGLESData *gles_data;
   315 #endif
   316 
   317     /* * * */
   318     /* The function used to dispose of this structure */
   319     void (*free) (_THIS);
   320 };
   321 
   322 typedef struct VideoBootStrap
   323 {
   324     const char *name;
   325     const char *desc;
   326     int (*available) (void);
   327     SDL_VideoDevice *(*create) (int devindex);
   328 } VideoBootStrap;
   329 
   330 #if SDL_VIDEO_DRIVER_COCOA
   331 extern VideoBootStrap COCOA_bootstrap;
   332 #endif
   333 #if SDL_VIDEO_DRIVER_X11
   334 extern VideoBootStrap X11_bootstrap;
   335 #endif
   336 #if SDL_VIDEO_DRIVER_DIRECTFB
   337 extern VideoBootStrap DirectFB_bootstrap;
   338 #endif
   339 #if SDL_VIDEO_DRIVER_WINDOWS
   340 extern VideoBootStrap WINDOWS_bootstrap;
   341 #endif
   342 #if SDL_VIDEO_DRIVER_BWINDOW
   343 extern VideoBootStrap BWINDOW_bootstrap;
   344 #endif
   345 #if SDL_VIDEO_DRIVER_PANDORA
   346 extern VideoBootStrap PND_bootstrap;
   347 #endif
   348 #if SDL_VIDEO_DRIVER_UIKIT
   349 extern VideoBootStrap UIKIT_bootstrap;
   350 #endif
   351 #if SDL_VIDEO_DRIVER_ANDROID
   352 extern VideoBootStrap Android_bootstrap;
   353 #endif
   354 #if SDL_VIDEO_DRIVER_PSP
   355 extern VideoBootStrap PSP_bootstrap;
   356 #endif
   357 #if SDL_VIDEO_DRIVER_DUMMY
   358 extern VideoBootStrap DUMMY_bootstrap;
   359 #endif
   360 
   361 extern SDL_VideoDevice *SDL_GetVideoDevice(void);
   362 extern int SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode);
   363 extern int SDL_AddVideoDisplay(const SDL_VideoDisplay * display);
   364 extern SDL_bool SDL_AddDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode * mode);
   365 extern SDL_VideoDisplay *SDL_GetDisplayForWindow(SDL_Window *window);
   366 
   367 extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
   368 
   369 extern void SDL_OnWindowShown(SDL_Window * window);
   370 extern void SDL_OnWindowHidden(SDL_Window * window);
   371 extern void SDL_OnWindowResized(SDL_Window * window);
   372 extern void SDL_OnWindowMinimized(SDL_Window * window);
   373 extern void SDL_OnWindowRestored(SDL_Window * window);
   374 extern void SDL_OnWindowEnter(SDL_Window * window);
   375 extern void SDL_OnWindowLeave(SDL_Window * window);
   376 extern void SDL_OnWindowFocusGained(SDL_Window * window);
   377 extern void SDL_OnWindowFocusLost(SDL_Window * window);
   378 extern void SDL_UpdateWindowGrab(SDL_Window * window);
   379 extern SDL_Window * SDL_GetFocusWindow(void);
   380 
   381 extern SDL_bool SDL_ShouldAllowTopmost(void);
   382 
   383 #endif /* _SDL_sysvideo_h */
   384 
   385 /* vi: set ts=4 sw=4 expandtab: */