Add qemu 2.4.0
[kvmfornfv.git] / qemu / ui / sdl2-gl.c
1 /*
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
28 #include "qemu-common.h"
29 #include "ui/console.h"
30 #include "ui/input.h"
31 #include "ui/sdl2.h"
32 #include "sysemu/sysemu.h"
33
34 static void sdl2_gl_render_surface(struct sdl2_console *scon)
35 {
36     int ww, wh;
37
38     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
39
40     SDL_GetWindowSize(scon->real_window, &ww, &wh);
41     surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
42
43     surface_gl_render_texture(scon->gls, scon->surface);
44     SDL_GL_SwapWindow(scon->real_window);
45 }
46
47 void sdl2_gl_update(DisplayChangeListener *dcl,
48                     int x, int y, int w, int h)
49 {
50     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
51
52     assert(scon->opengl);
53
54     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
55     surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
56     scon->updates++;
57 }
58
59 void sdl2_gl_switch(DisplayChangeListener *dcl,
60                     DisplaySurface *new_surface)
61 {
62     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
63     DisplaySurface *old_surface = scon->surface;
64
65     assert(scon->opengl);
66
67     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
68     surface_gl_destroy_texture(scon->gls, scon->surface);
69
70     scon->surface = new_surface;
71
72     if (!new_surface) {
73         console_gl_fini_context(scon->gls);
74         scon->gls = NULL;
75         sdl2_window_destroy(scon);
76         return;
77     }
78
79     if (!scon->real_window) {
80         sdl2_window_create(scon);
81         scon->gls = console_gl_init_context();
82     } else if (old_surface &&
83                ((surface_width(old_surface)  != surface_width(new_surface)) ||
84                 (surface_height(old_surface) != surface_height(new_surface)))) {
85         sdl2_window_resize(scon);
86     }
87
88     surface_gl_create_texture(scon->gls, scon->surface);
89 }
90
91 void sdl2_gl_refresh(DisplayChangeListener *dcl)
92 {
93     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
94
95     assert(scon->opengl);
96
97     graphic_hw_update(dcl->con);
98     if (scon->updates && scon->surface) {
99         scon->updates = 0;
100         sdl2_gl_render_surface(scon);
101     }
102     sdl2_poll_events(scon);
103 }
104
105 void sdl2_gl_redraw(struct sdl2_console *scon)
106 {
107     assert(scon->opengl);
108
109     if (scon->surface) {
110         sdl2_gl_render_surface(scon);
111     }
112 }