1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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_internal.h"
22
23#ifdef SDL_VIDEO_DRIVER_OFFSCREEN
24
25#include "../SDL_sysvideo.h"
26#include "../../events/SDL_windowevents_c.h"
27#include "../SDL_egl_c.h"
28
29#include "SDL_offscreenwindow.h"
30
31bool OFFSCREEN_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props)
32{
33 SDL_WindowData *offscreen_window = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData));
34
35 if (!offscreen_window) {
36 return false;
37 }
38
39 window->internal = offscreen_window;
40
41 if (window->x == SDL_WINDOWPOS_UNDEFINED) {
42 window->x = 0;
43 }
44
45 if (window->y == SDL_WINDOWPOS_UNDEFINED) {
46 window->y = 0;
47 }
48
49 offscreen_window->sdl_window = window;
50
51#ifdef SDL_VIDEO_OPENGL_EGL
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 } else {
65 offscreen_window->egl_surface = EGL_NO_SURFACE;
66 }
67#endif // SDL_VIDEO_OPENGL_EGL
68
69 return true;
70}
71
72void OFFSCREEN_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
73{
74 SDL_WindowData *offscreen_window = window->internal;
75
76 if (offscreen_window) {
77#ifdef SDL_VIDEO_OPENGL_EGL
78 SDL_EGL_DestroySurface(_this, offscreen_window->egl_surface);
79#endif
80 SDL_free(offscreen_window);
81 }
82
83 window->internal = NULL;
84}
85
86void OFFSCREEN_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
87{
88 SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->pending.w, window->pending.h);
89}
90#endif // SDL_VIDEO_DRIVER_OFFSCREEN
91