xref: /openbmc/qemu/ui/sdl2-gl.c (revision ed7f5f1d)
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 #include <epoxy/gl.h>
35 
36 static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
37 {
38     if (scon->scanout_mode == scanout) {
39         return;
40     }
41 
42     scon->scanout_mode = scanout;
43     if (!scon->scanout_mode) {
44         if (scon->fbo_id) {
45             glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
46                                       GL_COLOR_ATTACHMENT0_EXT,
47                                       GL_TEXTURE_2D, 0, 0);
48             glDeleteFramebuffers(1, &scon->fbo_id);
49             glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
50             scon->fbo_id = 0;
51         }
52         if (scon->surface) {
53             surface_gl_destroy_texture(scon->gls, scon->surface);
54             surface_gl_create_texture(scon->gls, scon->surface);
55         }
56     }
57 }
58 
59 static void sdl2_gl_render_surface(struct sdl2_console *scon)
60 {
61     int ww, wh;
62 
63     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
64     sdl2_set_scanout_mode(scon, false);
65 
66     SDL_GetWindowSize(scon->real_window, &ww, &wh);
67     surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
68 
69     surface_gl_render_texture(scon->gls, scon->surface);
70     SDL_GL_SwapWindow(scon->real_window);
71 }
72 
73 void sdl2_gl_update(DisplayChangeListener *dcl,
74                     int x, int y, int w, int h)
75 {
76     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
77 
78     assert(scon->opengl);
79 
80     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
81     surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
82     scon->updates++;
83 }
84 
85 void sdl2_gl_switch(DisplayChangeListener *dcl,
86                     DisplaySurface *new_surface)
87 {
88     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
89     DisplaySurface *old_surface = scon->surface;
90 
91     assert(scon->opengl);
92 
93     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
94     surface_gl_destroy_texture(scon->gls, scon->surface);
95 
96     scon->surface = new_surface;
97 
98     if (!new_surface) {
99         console_gl_fini_context(scon->gls);
100         scon->gls = NULL;
101         sdl2_window_destroy(scon);
102         return;
103     }
104 
105     if (!scon->real_window) {
106         sdl2_window_create(scon);
107         scon->gls = console_gl_init_context();
108     } else if (old_surface &&
109                ((surface_width(old_surface)  != surface_width(new_surface)) ||
110                 (surface_height(old_surface) != surface_height(new_surface)))) {
111         sdl2_window_resize(scon);
112     }
113 
114     surface_gl_create_texture(scon->gls, scon->surface);
115 }
116 
117 void sdl2_gl_refresh(DisplayChangeListener *dcl)
118 {
119     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
120 
121     assert(scon->opengl);
122 
123     graphic_hw_update(dcl->con);
124     if (scon->updates && scon->surface) {
125         scon->updates = 0;
126         sdl2_gl_render_surface(scon);
127     }
128     sdl2_poll_events(scon);
129 }
130 
131 void sdl2_gl_redraw(struct sdl2_console *scon)
132 {
133     assert(scon->opengl);
134 
135     if (scon->surface) {
136         sdl2_gl_render_surface(scon);
137     }
138 }
139 
140 QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
141                                      QEMUGLParams *params)
142 {
143     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
144     SDL_GLContext ctx;
145 
146     assert(scon->opengl);
147 
148     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
149 
150     SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
151     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
152                         SDL_GL_CONTEXT_PROFILE_CORE);
153     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
154     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
155 
156     ctx = SDL_GL_CreateContext(scon->real_window);
157     return (QEMUGLContext)ctx;
158 }
159 
160 void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
161 {
162     SDL_GLContext sdlctx = (SDL_GLContext)ctx;
163 
164     SDL_GL_DeleteContext(sdlctx);
165 }
166 
167 int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
168                                  QEMUGLContext ctx)
169 {
170     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
171     SDL_GLContext sdlctx = (SDL_GLContext)ctx;
172 
173     assert(scon->opengl);
174 
175     return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
176 }
177 
178 QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
179 {
180     SDL_GLContext sdlctx;
181 
182     sdlctx = SDL_GL_GetCurrentContext();
183     return (QEMUGLContext)sdlctx;
184 }
185 
186 void sdl2_gl_scanout(DisplayChangeListener *dcl,
187                      uint32_t backing_id, bool backing_y_0_top,
188                      uint32_t x, uint32_t y,
189                      uint32_t w, uint32_t h)
190 {
191     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
192 
193     assert(scon->opengl);
194     scon->x = x;
195     scon->y = y;
196     scon->w = w;
197     scon->h = h;
198     scon->tex_id = backing_id;
199     scon->y0_top = backing_y_0_top;
200 
201     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
202 
203     if (scon->tex_id == 0 || scon->w == 0 || scon->h == 0) {
204         sdl2_set_scanout_mode(scon, false);
205         return;
206     }
207 
208     sdl2_set_scanout_mode(scon, true);
209     if (!scon->fbo_id) {
210         glGenFramebuffers(1, &scon->fbo_id);
211     }
212 
213     glBindFramebuffer(GL_FRAMEBUFFER_EXT, scon->fbo_id);
214     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
215                               GL_TEXTURE_2D, scon->tex_id, 0);
216 }
217 
218 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
219                            uint32_t x, uint32_t y, uint32_t w, uint32_t h)
220 {
221     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
222     int ww, wh, y1, y2;
223 
224     assert(scon->opengl);
225     if (!scon->scanout_mode) {
226         return;
227     }
228     if (!scon->fbo_id) {
229         return;
230     }
231 
232     SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
233 
234     glBindFramebuffer(GL_READ_FRAMEBUFFER, scon->fbo_id);
235     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
236 
237     SDL_GetWindowSize(scon->real_window, &ww, &wh);
238     glViewport(0, 0, ww, wh);
239     y1 = scon->y0_top ? 0 : scon->h;
240     y2 = scon->y0_top ? scon->h : 0;
241     glBlitFramebuffer(0, y1, scon->w, y2,
242                       0, 0, ww, wh,
243                       GL_COLOR_BUFFER_BIT, GL_NEAREST);
244     glBindFramebuffer(GL_FRAMEBUFFER_EXT, scon->fbo_id);
245 
246     SDL_GL_SwapWindow(scon->real_window);
247 }
248