i8254: use QEMU_ALIGN_DOWN
[qemu/kevin.git] / ui / sdl2-gl.c
blobdcad3d0d26aa1e1fad52796ac9190e14f4e1c0f0
1 /*
2 * QEMU SDL display driver -- opengl support
4 * Copyright (c) 2014 Red Hat
6 * Authors:
7 * Gerd Hoffmann <kraxel@redhat.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
28 #include "qemu/osdep.h"
29 #include "qemu-common.h"
30 #include "ui/console.h"
31 #include "ui/input.h"
32 #include "ui/sdl2.h"
33 #include "sysemu/sysemu.h"
35 #include <epoxy/gl.h>
37 static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
39 if (scon->scanout_mode == scanout) {
40 return;
43 scon->scanout_mode = scanout;
44 if (!scon->scanout_mode) {
45 egl_fb_destroy(&scon->guest_fb);
46 if (scon->surface) {
47 surface_gl_destroy_texture(scon->gls, scon->surface);
48 surface_gl_create_texture(scon->gls, scon->surface);
53 static void sdl2_gl_render_surface(struct sdl2_console *scon)
55 int ww, wh;
57 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
58 sdl2_set_scanout_mode(scon, false);
60 SDL_GetWindowSize(scon->real_window, &ww, &wh);
61 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
63 surface_gl_render_texture(scon->gls, scon->surface);
64 SDL_GL_SwapWindow(scon->real_window);
67 void sdl2_gl_update(DisplayChangeListener *dcl,
68 int x, int y, int w, int h)
70 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
72 assert(scon->opengl);
74 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
75 surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
76 scon->updates++;
79 void sdl2_gl_switch(DisplayChangeListener *dcl,
80 DisplaySurface *new_surface)
82 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
83 DisplaySurface *old_surface = scon->surface;
85 assert(scon->opengl);
87 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
88 surface_gl_destroy_texture(scon->gls, scon->surface);
90 scon->surface = new_surface;
92 if (!new_surface) {
93 console_gl_fini_context(scon->gls);
94 scon->gls = NULL;
95 sdl2_window_destroy(scon);
96 return;
99 if (!scon->real_window) {
100 sdl2_window_create(scon);
101 scon->gls = console_gl_init_context();
102 } else if (old_surface &&
103 ((surface_width(old_surface) != surface_width(new_surface)) ||
104 (surface_height(old_surface) != surface_height(new_surface)))) {
105 sdl2_window_resize(scon);
108 surface_gl_create_texture(scon->gls, scon->surface);
111 void sdl2_gl_refresh(DisplayChangeListener *dcl)
113 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
115 assert(scon->opengl);
117 graphic_hw_update(dcl->con);
118 if (scon->updates && scon->surface) {
119 scon->updates = 0;
120 sdl2_gl_render_surface(scon);
122 sdl2_poll_events(scon);
125 void sdl2_gl_redraw(struct sdl2_console *scon)
127 assert(scon->opengl);
129 if (scon->surface) {
130 sdl2_gl_render_surface(scon);
134 QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
135 QEMUGLParams *params)
137 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
138 SDL_GLContext ctx;
140 assert(scon->opengl);
142 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
144 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
145 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
146 SDL_GL_CONTEXT_PROFILE_CORE);
147 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
148 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
150 ctx = SDL_GL_CreateContext(scon->real_window);
151 return (QEMUGLContext)ctx;
154 void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
156 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
158 SDL_GL_DeleteContext(sdlctx);
161 int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
162 QEMUGLContext ctx)
164 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
165 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
167 assert(scon->opengl);
169 return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
172 QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
174 SDL_GLContext sdlctx;
176 sdlctx = SDL_GL_GetCurrentContext();
177 return (QEMUGLContext)sdlctx;
180 void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
182 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
184 assert(scon->opengl);
185 scon->w = 0;
186 scon->h = 0;
187 sdl2_set_scanout_mode(scon, false);
190 void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
191 uint32_t backing_id,
192 bool backing_y_0_top,
193 uint32_t backing_width,
194 uint32_t backing_height,
195 uint32_t x, uint32_t y,
196 uint32_t w, uint32_t h)
198 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
200 assert(scon->opengl);
201 scon->x = x;
202 scon->y = y;
203 scon->w = w;
204 scon->h = h;
205 scon->y0_top = backing_y_0_top;
207 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
209 sdl2_set_scanout_mode(scon, true);
210 egl_fb_create_for_tex(&scon->guest_fb, backing_width, backing_height,
211 backing_id);
214 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
215 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
217 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
218 int ww, wh;
220 assert(scon->opengl);
221 if (!scon->scanout_mode) {
222 return;
224 if (!scon->guest_fb.framebuffer) {
225 return;
228 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
230 SDL_GetWindowSize(scon->real_window, &ww, &wh);
231 egl_fb_setup_default(&scon->win_fb, ww, wh);
232 egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
234 SDL_GL_SwapWindow(scon->real_window);