src/video/mir/SDL_mirwindow.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Jul 2014 10:33:32 -0700
changeset 8978 7753e4fd3d1d
parent 8753 497e72bba21b
child 9185 43f4122e1268
permissions -rw-r--r--
Fixed bug 2629 - Mac: crash when calling SDL_DestroyWindow with an active OpenGL context

Alex Szpakowski

Since this commit https://hg.libsdl.org/SDL/rev/1519c462cee6 , calling SDL_DestroyWindow will crash the program if the window has an active OpenGL context.

This is because the Cocoa_DestroyWindow code sets the window's driverdata to NULL and then calls [context setWindow:NULL], which tries to access the window's driverdata, resulting in a null pointer dereference.

I have attached a patch which fixes the issue by moving the line which sets the driverdata to NULL to after the lines which call functions that use the driverdata pointer.
icculus@8153
     1
/*
icculus@8153
     2
  Simple DirectMedia Layer
icculus@8157
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
icculus@8153
     4
icculus@8153
     5
  This software is provided 'as-is', without any express or implied
icculus@8153
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@8153
     7
  arising from the use of this software.
icculus@8153
     8
icculus@8153
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@8153
    10
  including commercial applications, and to alter it and redistribute it
icculus@8153
    11
  freely, subject to the following restrictions:
icculus@8153
    12
icculus@8153
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@8153
    14
     claim that you wrote the original software. If you use this software
icculus@8153
    15
     in a product, an acknowledgment in the product documentation would be
icculus@8153
    16
     appreciated but is not required.
icculus@8153
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@8153
    18
     misrepresented as being the original software.
icculus@8153
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@8153
    20
*/
icculus@8153
    21
icculus@8153
    22
/*
icculus@8153
    23
  Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
icculus@8153
    24
*/
icculus@8153
    25
icculus@8154
    26
#include "../../SDL_internal.h"
icculus@8154
    27
icculus@8154
    28
#if SDL_VIDEO_DRIVER_MIR
icculus@8153
    29
icculus@8153
    30
#include "../SDL_egl_c.h"
icculus@8153
    31
#include "../SDL_sysvideo.h"
icculus@8153
    32
icculus@8153
    33
#include "SDL_mirevents.h"
icculus@8153
    34
#include "SDL_mirwindow.h"
icculus@8153
    35
icculus@8159
    36
#include "SDL_mirdyn.h"
icculus@8159
    37
icculus@8153
    38
int
icculus@8153
    39
IsSurfaceValid(MIR_Window* mir_window)
icculus@8153
    40
{
icculus@8159
    41
    if (!MIR_mir_surface_is_valid(mir_window->surface)) {
icculus@8159
    42
        const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
icculus@8153
    43
        return SDL_SetError("Failed to created a mir surface: %s", error);
icculus@8153
    44
    }
icculus@8153
    45
icculus@8153
    46
    return 0;
icculus@8153
    47
}
icculus@8153
    48
icculus@8153
    49
MirPixelFormat
icculus@8153
    50
FindValidPixelFormat(MIR_Data* mir_data)
icculus@8153
    51
{
icculus@8153
    52
    unsigned int pf_size = 32;
icculus@8153
    53
    unsigned int valid_formats;
icculus@8153
    54
    unsigned int f;
icculus@8153
    55
icculus@8153
    56
    MirPixelFormat formats[pf_size];
icculus@8159
    57
    MIR_mir_connection_get_available_surface_formats(mir_data->connection, formats,
icculus@8153
    58
                                                 pf_size, &valid_formats);
icculus@8153
    59
icculus@8153
    60
    for (f = 0; f < valid_formats; f++) {
icculus@8153
    61
        MirPixelFormat cur_pf = formats[f];
icculus@8153
    62
icculus@8153
    63
        if (cur_pf == mir_pixel_format_abgr_8888 ||
icculus@8153
    64
            cur_pf == mir_pixel_format_xbgr_8888 ||
icculus@8153
    65
            cur_pf == mir_pixel_format_argb_8888 ||
icculus@8153
    66
            cur_pf == mir_pixel_format_xrgb_8888) {
icculus@8153
    67
icculus@8153
    68
            return cur_pf;
icculus@8153
    69
        }
icculus@8153
    70
    }
icculus@8153
    71
icculus@8153
    72
    return mir_pixel_format_invalid;
icculus@8153
    73
}
icculus@8153
    74
icculus@8153
    75
int
icculus@8153
    76
MIR_CreateWindow(_THIS, SDL_Window* window)
icculus@8153
    77
{
icculus@8153
    78
    MIR_Window* mir_window;
icculus@8153
    79
    MIR_Data* mir_data;
icculus@8153
    80
icculus@8153
    81
    MirSurfaceParameters surfaceparm =
icculus@8153
    82
    {
icculus@8153
    83
        .name = "MirSurface",
icculus@8153
    84
        .width = window->w,
icculus@8153
    85
        .height = window->h,
icculus@8153
    86
        .pixel_format = mir_pixel_format_invalid,
brandon@8753
    87
        .buffer_usage = mir_buffer_usage_hardware,
brandon@8753
    88
        .output_id = mir_display_output_id_invalid
icculus@8153
    89
    };
icculus@8153
    90
icculus@8153
    91
    MirEventDelegate delegate = {
icculus@8153
    92
        MIR_HandleInput,
icculus@8153
    93
        window
icculus@8153
    94
    };
icculus@8153
    95
icculus@8153
    96
    mir_window = SDL_calloc(1, sizeof(MIR_Window));
icculus@8153
    97
    if (!mir_window)
icculus@8153
    98
        return SDL_OutOfMemory();
icculus@8153
    99
icculus@8153
   100
    mir_data = _this->driverdata;
icculus@8153
   101
    window->driverdata = mir_window;
icculus@8153
   102
brandon@8749
   103
    if (mir_data->software)
brandon@8749
   104
        surfaceparm.buffer_usage = mir_buffer_usage_software;
brandon@8749
   105
icculus@8153
   106
    if (window->x == SDL_WINDOWPOS_UNDEFINED)
icculus@8153
   107
        window->x = 0;
icculus@8153
   108
icculus@8153
   109
    if (window->y == SDL_WINDOWPOS_UNDEFINED)
icculus@8153
   110
        window->y = 0;
icculus@8153
   111
icculus@8153
   112
    mir_window->mir_data = mir_data;
icculus@8153
   113
    mir_window->sdl_window = window;
icculus@8153
   114
icculus@8153
   115
    surfaceparm.pixel_format = FindValidPixelFormat(mir_data);
icculus@8153
   116
    if (surfaceparm.pixel_format == mir_pixel_format_invalid) {
icculus@8153
   117
        return SDL_SetError("Failed to find a valid pixel format.");
icculus@8153
   118
    }
icculus@8153
   119
icculus@8159
   120
    mir_window->surface = MIR_mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
icculus@8159
   121
    if (!MIR_mir_surface_is_valid(mir_window->surface)) {
icculus@8159
   122
        const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
icculus@8153
   123
        return SDL_SetError("Failed to created a mir surface: %s", error);
icculus@8153
   124
    }
icculus@8153
   125
icculus@8153
   126
    if (window->flags & SDL_WINDOW_OPENGL) {
icculus@8153
   127
        EGLNativeWindowType egl_native_window =
icculus@8159
   128
                        (EGLNativeWindowType)MIR_mir_surface_get_egl_native_window(mir_window->surface);
icculus@8153
   129
icculus@8153
   130
        mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
icculus@8153
   131
icculus@8153
   132
        if (mir_window->egl_surface == EGL_NO_SURFACE) {
icculus@8153
   133
            return SDL_SetError("Failed to created a window surface %p",
icculus@8153
   134
                                _this->egl_data->egl_display);
icculus@8153
   135
        }
icculus@8153
   136
    }
icculus@8153
   137
    else {
icculus@8153
   138
        mir_window->egl_surface = EGL_NO_SURFACE;
icculus@8153
   139
    }
icculus@8153
   140
icculus@8159
   141
    MIR_mir_surface_set_event_handler(mir_window->surface, &delegate);
icculus@8153
   142
icculus@8153
   143
    return 0;
icculus@8153
   144
}
icculus@8153
   145
icculus@8153
   146
void
icculus@8153
   147
MIR_DestroyWindow(_THIS, SDL_Window* window)
icculus@8153
   148
{
icculus@8153
   149
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
   150
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   151
icculus@8153
   152
    if (mir_data) {
icculus@8153
   153
        SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
icculus@8159
   154
        MIR_mir_surface_release_sync(mir_window->surface);
icculus@8153
   155
icculus@8153
   156
        SDL_free(mir_window);
icculus@8153
   157
    }
slouken@8978
   158
    window->driverdata = NULL;
icculus@8153
   159
}
icculus@8153
   160
icculus@8153
   161
SDL_bool
icculus@8153
   162
MIR_GetWindowWMInfo(_THIS, SDL_Window* window, SDL_SysWMinfo* info)
icculus@8153
   163
{
icculus@8153
   164
    if (info->version.major == SDL_MAJOR_VERSION &&
icculus@8153
   165
        info->version.minor == SDL_MINOR_VERSION) {
icculus@8155
   166
        MIR_Window* mir_window = window->driverdata;
icculus@8153
   167
icculus@8153
   168
        info->subsystem = SDL_SYSWM_MIR;
icculus@8155
   169
        info->info.mir.connection = mir_window->mir_data->connection;
icculus@8155
   170
        info->info.mir.surface = mir_window->surface;
icculus@8155
   171
icculus@8153
   172
        return SDL_TRUE;
icculus@8153
   173
    }
icculus@8153
   174
icculus@8153
   175
    return SDL_FALSE;
icculus@8153
   176
}
icculus@8153
   177
icculus@8153
   178
void
icculus@8153
   179
MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
icculus@8153
   180
                        SDL_VideoDisplay* display,
icculus@8153
   181
                        SDL_bool fullscreen)
icculus@8153
   182
{
icculus@8153
   183
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   184
icculus@8153
   185
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   186
        return;
icculus@8153
   187
icculus@8153
   188
    if (fullscreen) {
icculus@8159
   189
        MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_fullscreen);
icculus@8159
   190
    } else {
icculus@8159
   191
        MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
icculus@8153
   192
    }
icculus@8153
   193
}
icculus@8153
   194
icculus@8153
   195
void
icculus@8153
   196
MIR_MaximizeWindow(_THIS, SDL_Window* window)
icculus@8153
   197
{
icculus@8153
   198
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   199
icculus@8153
   200
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   201
        return;
icculus@8153
   202
icculus@8159
   203
    MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_maximized);
icculus@8153
   204
}
icculus@8153
   205
icculus@8153
   206
void
icculus@8153
   207
MIR_MinimizeWindow(_THIS, SDL_Window* window)
icculus@8153
   208
{
icculus@8153
   209
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   210
icculus@8153
   211
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   212
        return;
icculus@8153
   213
icculus@8159
   214
    MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_minimized);
icculus@8153
   215
}
icculus@8153
   216
icculus@8153
   217
void
icculus@8153
   218
MIR_RestoreWindow(_THIS, SDL_Window * window)
icculus@8153
   219
{
icculus@8153
   220
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   221
icculus@8153
   222
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   223
        return;
icculus@8153
   224
icculus@8159
   225
    MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
icculus@8153
   226
}
icculus@8154
   227
icculus@8154
   228
#endif /* SDL_VIDEO_DRIVER_MIR */
icculus@8154
   229
icculus@8154
   230
/* vi: set ts=4 sw=4 expandtab: */