alpha: Clean up includes
[qemu/ar7.git] / ui / sdl2-gl.c
blob2bb3d06babf48684f760dbfa5a73eb933f51889f
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-common.h"
29 #include "ui/console.h"
30 #include "ui/input.h"
31 #include "ui/sdl2.h"
32 #include "sysemu/sysemu.h"
34 #include <epoxy/gl.h>
36 static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
38 if (scon->scanout_mode == scanout) {
39 return;
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;
52 if (scon->surface) {
53 surface_gl_destroy_texture(scon->gls, scon->surface);
54 surface_gl_create_texture(scon->gls, scon->surface);
59 static void sdl2_gl_render_surface(struct sdl2_console *scon)
61 int ww, wh;
63 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
64 sdl2_set_scanout_mode(scon, false);
66 SDL_GetWindowSize(scon->real_window, &ww, &wh);
67 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
69 surface_gl_render_texture(scon->gls, scon->surface);
70 SDL_GL_SwapWindow(scon->real_window);
73 void sdl2_gl_update(DisplayChangeListener *dcl,
74 int x, int y, int w, int h)
76 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
78 assert(scon->opengl);
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++;
85 void sdl2_gl_switch(DisplayChangeListener *dcl,
86 DisplaySurface *new_surface)
88 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
89 DisplaySurface *old_surface = scon->surface;
91 assert(scon->opengl);
93 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
94 surface_gl_destroy_texture(scon->gls, scon->surface);
96 scon->surface = new_surface;
98 if (!new_surface) {
99 console_gl_fini_context(scon->gls);
100 scon->gls = NULL;
101 sdl2_window_destroy(scon);
102 return;
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);
114 surface_gl_create_texture(scon->gls, scon->surface);
117 void sdl2_gl_refresh(DisplayChangeListener *dcl)
119 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
121 assert(scon->opengl);
123 graphic_hw_update(dcl->con);
124 if (scon->updates && scon->surface) {
125 scon->updates = 0;
126 sdl2_gl_render_surface(scon);
128 sdl2_poll_events(scon);
131 void sdl2_gl_redraw(struct sdl2_console *scon)
133 assert(scon->opengl);
135 if (scon->surface) {
136 sdl2_gl_render_surface(scon);
140 QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
141 QEMUGLParams *params)
143 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
144 SDL_GLContext ctx;
146 assert(scon->opengl);
148 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
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);
156 ctx = SDL_GL_CreateContext(scon->real_window);
157 return (QEMUGLContext)ctx;
160 void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
162 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
164 SDL_GL_DeleteContext(sdlctx);
167 int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
168 QEMUGLContext ctx)
170 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
171 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
173 assert(scon->opengl);
175 return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
178 QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
180 SDL_GLContext sdlctx;
182 sdlctx = SDL_GL_GetCurrentContext();
183 return (QEMUGLContext)sdlctx;
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)
191 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
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;
201 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
203 if (scon->tex_id == 0 || scon->w == 0 || scon->h == 0) {
204 sdl2_set_scanout_mode(scon, false);
205 return;
208 sdl2_set_scanout_mode(scon, true);
209 if (!scon->fbo_id) {
210 glGenFramebuffers(1, &scon->fbo_id);
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);
218 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
219 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
221 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
222 int ww, wh, y1, y2;
224 assert(scon->opengl);
225 if (!scon->scanout_mode) {
226 return;
228 if (!scon->fbo_id) {
229 return;
232 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
234 glBindFramebuffer(GL_READ_FRAMEBUFFER, scon->fbo_id);
235 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
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);
246 SDL_GL_SwapWindow(scon->real_window);