1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 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 
22 #include "../../SDL_internal.h"
23 
24 #if SDL_VIDEO_DRIVER_OFFSCREEN
25 
26 #include "../SDL_egl_c.h"
27 #include "../SDL_sysvideo.h"
28 
29 #include "SDL_offscreenwindow.h"
30 
31 int
OFFSCREEN_CreateWindow(_THIS,SDL_Window * window)32 OFFSCREEN_CreateWindow(_THIS, SDL_Window* window)
33 {
34     OFFSCREEN_Window* offscreen_window = SDL_calloc(1, sizeof(OFFSCREEN_Window));
35 
36     if (!offscreen_window) {
37         return SDL_OutOfMemory();
38     }
39 
40     window->driverdata = offscreen_window;
41 
42     if (window->x == SDL_WINDOWPOS_UNDEFINED) {
43         window->x = 0;
44     }
45 
46     if (window->y == SDL_WINDOWPOS_UNDEFINED) {
47         window->y = 0;
48     }
49 
50     offscreen_window->sdl_window = window;
51 
52     if (window->flags & SDL_WINDOW_OPENGL) {
53 
54         if (!_this->egl_data) {
55             return SDL_SetError("Cannot create an OPENGL window invalid egl_data");
56         }
57 
58         offscreen_window->egl_surface = SDL_EGL_CreateOffscreenSurface(_this, window->w, window->h);
59 
60         if (offscreen_window->egl_surface == EGL_NO_SURFACE) {
61             return SDL_SetError("Failed to created an offscreen surface (EGL display: %p)",
62                                 _this->egl_data->egl_display);
63         }
64     }
65     else {
66         offscreen_window->egl_surface = EGL_NO_SURFACE;
67     }
68 
69     return 0;
70 }
71 
72 void
OFFSCREEN_DestroyWindow(_THIS,SDL_Window * window)73 OFFSCREEN_DestroyWindow(_THIS, SDL_Window* window)
74 {
75     OFFSCREEN_Window* offscreen_window = window->driverdata;
76 
77     if (offscreen_window) {
78         SDL_EGL_DestroySurface(_this, offscreen_window->egl_surface);
79         SDL_free(offscreen_window);
80     }
81 
82     window->driverdata = NULL;
83 }
84 
85 #endif /* SDL_VIDEO_DRIVER_OFFSCREEN */
86 
87 /* vi: set ts=4 sw=4 expandtab: */
88