blob: c73d273bf804d75b2b56f92086170f89a4ef38a8 [file] [log] [blame]
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +01001/*
2 * QEMU SDL display driver -- opengl support
3 *
4 * Copyright (c) 2014 Red Hat
5 *
6 * Authors:
7 * Gerd Hoffmann <kraxel@redhat.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Peter Maydelle16f4c82016-01-29 17:49:51 +000028#include "qemu/osdep.h"
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010029#include "ui/console.h"
30#include "ui/input.h"
31#include "ui/sdl2.h"
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010032
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +010033static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
34{
35 if (scon->scanout_mode == scanout) {
36 return;
37 }
38
39 scon->scanout_mode = scanout;
40 if (!scon->scanout_mode) {
Gerd Hoffmann371c4ef2017-06-14 10:41:48 +020041 egl_fb_destroy(&scon->guest_fb);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +010042 if (scon->surface) {
43 surface_gl_destroy_texture(scon->gls, scon->surface);
44 surface_gl_create_texture(scon->gls, scon->surface);
45 }
46 }
47}
48
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010049static void sdl2_gl_render_surface(struct sdl2_console *scon)
50{
51 int ww, wh;
52
53 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +010054 sdl2_set_scanout_mode(scon, false);
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010055
56 SDL_GetWindowSize(scon->real_window, &ww, &wh);
57 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
58
59 surface_gl_render_texture(scon->gls, scon->surface);
60 SDL_GL_SwapWindow(scon->real_window);
61}
62
63void sdl2_gl_update(DisplayChangeListener *dcl,
64 int x, int y, int w, int h)
65{
66 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
67
68 assert(scon->opengl);
69
70 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
71 surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
72 scon->updates++;
73}
74
75void sdl2_gl_switch(DisplayChangeListener *dcl,
76 DisplaySurface *new_surface)
77{
78 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
79 DisplaySurface *old_surface = scon->surface;
80
81 assert(scon->opengl);
82
83 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
84 surface_gl_destroy_texture(scon->gls, scon->surface);
85
86 scon->surface = new_surface;
87
88 if (!new_surface) {
Gerd Hoffmann46e19e12017-10-10 15:54:49 +020089 qemu_gl_fini_shader(scon->gls);
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010090 scon->gls = NULL;
91 sdl2_window_destroy(scon);
92 return;
93 }
94
95 if (!scon->real_window) {
96 sdl2_window_create(scon);
Gerd Hoffmann46e19e12017-10-10 15:54:49 +020097 scon->gls = qemu_gl_init_shader();
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010098 } else if (old_surface &&
99 ((surface_width(old_surface) != surface_width(new_surface)) ||
100 (surface_height(old_surface) != surface_height(new_surface)))) {
101 sdl2_window_resize(scon);
102 }
103
104 surface_gl_create_texture(scon->gls, scon->surface);
105}
106
107void sdl2_gl_refresh(DisplayChangeListener *dcl)
108{
109 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
110
111 assert(scon->opengl);
112
113 graphic_hw_update(dcl->con);
114 if (scon->updates && scon->surface) {
115 scon->updates = 0;
116 sdl2_gl_render_surface(scon);
117 }
118 sdl2_poll_events(scon);
119}
120
121void sdl2_gl_redraw(struct sdl2_console *scon)
122{
123 assert(scon->opengl);
124
Tao Wu77f60fb2018-07-26 15:59:00 -0700125 if (scon->scanout_mode) {
126 /* sdl2_gl_scanout_flush actually only care about
127 * the first argument. */
128 return sdl2_gl_scanout_flush(&scon->dcl, 0, 0, 0, 0);
129 }
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100130 if (scon->surface) {
131 sdl2_gl_render_surface(scon);
132 }
133}
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100134
135QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
136 QEMUGLParams *params)
137{
138 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
139 SDL_GLContext ctx;
140
141 assert(scon->opengl);
142
143 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
144
145 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
Elie Tournier4867e472018-04-13 14:58:42 +0100146 if (scon->opts->gl == DISPLAYGL_MODE_ON ||
147 scon->opts->gl == DISPLAYGL_MODE_CORE) {
148 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
149 SDL_GL_CONTEXT_PROFILE_CORE);
150 } else if (scon->opts->gl == DISPLAYGL_MODE_ES) {
151 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
152 SDL_GL_CONTEXT_PROFILE_ES);
153 }
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100154 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
155 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
156
157 ctx = SDL_GL_CreateContext(scon->real_window);
Elie Tournier4867e472018-04-13 14:58:42 +0100158
159 /* If SDL fail to create a GL context and we use the "on" flag,
160 * then try to fallback to GLES.
161 */
162 if (!ctx && scon->opts->gl == DISPLAYGL_MODE_ON) {
163 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
164 SDL_GL_CONTEXT_PROFILE_ES);
165 ctx = SDL_GL_CreateContext(scon->real_window);
166 }
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100167 return (QEMUGLContext)ctx;
168}
169
170void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
171{
172 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
173
174 SDL_GL_DeleteContext(sdlctx);
175}
176
177int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
178 QEMUGLContext ctx)
179{
180 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
181 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
182
183 assert(scon->opengl);
184
185 return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
186}
187
188QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
189{
190 SDL_GLContext sdlctx;
191
192 sdlctx = SDL_GL_GetCurrentContext();
193 return (QEMUGLContext)sdlctx;
194}
195
Gerd Hoffmanndb6cdfb2017-02-21 10:37:20 +0100196void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
197{
198 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
199
200 assert(scon->opengl);
201 scon->w = 0;
202 scon->h = 0;
Gerd Hoffmanndb6cdfb2017-02-21 10:37:20 +0100203 sdl2_set_scanout_mode(scon, false);
204}
205
Gerd Hoffmannf4c36bd2017-02-21 10:37:16 +0100206void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
207 uint32_t backing_id,
208 bool backing_y_0_top,
209 uint32_t backing_width,
210 uint32_t backing_height,
211 uint32_t x, uint32_t y,
212 uint32_t w, uint32_t h)
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100213{
214 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
215
216 assert(scon->opengl);
217 scon->x = x;
218 scon->y = y;
219 scon->w = w;
220 scon->h = h;
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100221 scon->y0_top = backing_y_0_top;
222
223 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
224
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100225 sdl2_set_scanout_mode(scon, true);
Gerd Hoffmann74083f92017-09-27 13:50:31 +0200226 egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
227 backing_id, false);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100228}
229
230void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
231 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
232{
233 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
Gerd Hoffmann371c4ef2017-06-14 10:41:48 +0200234 int ww, wh;
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100235
236 assert(scon->opengl);
237 if (!scon->scanout_mode) {
238 return;
239 }
Gerd Hoffmann371c4ef2017-06-14 10:41:48 +0200240 if (!scon->guest_fb.framebuffer) {
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100241 return;
242 }
243
244 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
245
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100246 SDL_GetWindowSize(scon->real_window, &ww, &wh);
Gerd Hoffmann371c4ef2017-06-14 10:41:48 +0200247 egl_fb_setup_default(&scon->win_fb, ww, wh);
248 egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100249
250 SDL_GL_SwapWindow(scon->real_window);
251}