3 * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
4 * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
5 * Copyright (C) 2015 Freescale Semiconductor <b55597@freescale.com>
6 * Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include "../gstgl_fwd.h"
25 #include <gst/gl/gstglcontext.h>
26 #include <gst/gl/gstglfuncs.h>
28 #include "gstglwindow_viv_fb_egl.h"
29 #include "../gstglwindow_private.h"
31 #define GST_CAT_DEFAULT gst_gl_window_debug
33 #define gst_gl_window_viv_fb_egl_parent_class parent_class
34 G_DEFINE_TYPE (GstGLWindowVivFBEGL, gst_gl_window_viv_fb_egl,
37 static guintptr gst_gl_window_viv_fb_egl_get_window_handle (GstGLWindow *
39 static guintptr gst_gl_window_viv_fb_egl_get_display (GstGLWindow * window);
40 static void gst_gl_window_viv_fb_egl_set_window_handle (GstGLWindow * window,
42 static void gst_gl_window_viv_fb_egl_close (GstGLWindow * window);
43 static gboolean gst_gl_window_viv_fb_egl_open (GstGLWindow * window,
45 static void gst_gl_window_viv_fb_egl_draw (GstGLWindow * window);
47 gst_gl_window_viv_fb_egl_set_render_rectangle (GstGLWindow * window,
48 gint x, gint y, gint width, gint height);
49 static gboolean gst_gl_window_viv_fb_egl_controls_viewport (GstGLWindow *
53 gst_gl_window_viv_fb_egl_class_init (GstGLWindowVivFBEGLClass * klass)
55 GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
57 window_class->get_window_handle =
58 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_get_window_handle);
59 window_class->get_display =
60 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_get_display);
61 window_class->set_window_handle =
62 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_set_window_handle);
63 window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_close);
64 window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_open);
65 window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_draw);
66 window_class->set_render_rectangle =
67 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_set_render_rectangle);
68 window_class->controls_viewport =
69 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_controls_viewport);
73 gst_gl_window_viv_fb_egl_init (GstGLWindowVivFBEGL * window)
77 /* Must be called in the gl thread */
79 gst_gl_window_viv_fb_egl_new (GstGLDisplay * display)
81 GstGLWindowVivFBEGL *window;
83 if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_VIV_FB) ==
85 /* we require a Vivante FB display to create windows */
88 window = g_object_new (GST_TYPE_GL_WINDOW_VIV_FB_EGL, NULL);
89 gst_object_ref_sink (window);
95 gst_gl_window_viv_fb_egl_close (GstGLWindow * window)
97 GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
99 if (window_egl->win_id && !window_egl->external_window) {
100 fbDestroyWindow (window_egl->win_id);
101 window_egl->win_id = 0;
104 GST_GL_WINDOW_CLASS (parent_class)->close (window);
108 gst_gl_window_viv_fb_egl_get_display (GstGLWindow * window)
110 return gst_gl_display_get_handle (window->display);
114 gst_gl_window_viv_fb_egl_open (GstGLWindow * window, GError ** error)
116 GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
117 EGLNativeDisplayType display;
119 display = (EGLNativeDisplayType) gst_gl_window_get_display (window);
121 window_egl->win_id = fbCreateWindow (display, -1, -1, 0, 0);
122 window_egl->external_window = FALSE;
123 if (!window_egl->win_id) {
124 g_set_error (error, GST_GL_WINDOW_ERROR,
125 GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE, "Can't create window");
129 fbGetWindowGeometry (window_egl->win_id, NULL, NULL,
130 &window_egl->window_width, &window_egl->window_height);
131 window_egl->render_rectangle.x = 0;
132 window_egl->render_rectangle.y = 0;
133 window_egl->render_rectangle.w = window_egl->window_width;
134 window_egl->render_rectangle.h = window_egl->window_height;
135 gst_gl_window_resize (window, window_egl->window_width,
136 window_egl->window_height);
139 ("Opened Vivante FB display succesfully, resolution is (%dx%d), display %p, window %p.",
140 window_egl->window_width, window_egl->window_height, (gpointer) display,
141 (gpointer) window_egl->win_id);
143 return GST_GL_WINDOW_CLASS (parent_class)->open (window, error);
147 gst_gl_window_viv_fb_egl_get_window_handle (GstGLWindow * window)
149 return (guintptr) GST_GL_WINDOW_VIV_FB_EGL (window)->win_id;
153 gst_gl_window_viv_fb_egl_set_window_handle (GstGLWindow * window,
156 GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
159 if (window_egl->win_id)
160 fbDestroyWindow (window_egl->win_id);
161 window_egl->win_id = (EGLNativeWindowType) handle;
162 window_egl->external_window = handle != 0;
164 fbGetWindowGeometry (window_egl->win_id, NULL, NULL, &width, &height);
165 gst_gl_window_resize (window, width, height);
169 draw_cb (gpointer data)
171 GstGLWindowVivFBEGL *window_egl = data;
172 GstGLWindow *window = GST_GL_WINDOW (window_egl);
173 GstGLContext *context = gst_gl_window_get_context (window);
174 const GstGLFuncs *gl;
175 gint viewport_dim[4];
177 gl = context->gl_vtable;
179 if (window->queue_resize) {
182 gst_gl_window_get_surface_dimensions (window, &width, &height);
183 gst_gl_window_resize (window, width, height);
185 gl->GetIntegerv (GL_VIEWPORT, viewport_dim);
186 window_egl->viewport.x = viewport_dim[0] + window_egl->render_rectangle.x;
187 window_egl->viewport.y = viewport_dim[1] + window_egl->render_rectangle.y;
188 window_egl->viewport.w = viewport_dim[2];
189 window_egl->viewport.h = viewport_dim[2];
192 gl->Viewport (window_egl->viewport.x, window_egl->viewport.y,
193 window_egl->viewport.w, window_egl->viewport.h);
196 window->draw (window->draw_data);
198 gst_gl_context_swap_buffers (context);
200 gst_object_unref (context);
204 gst_gl_window_viv_fb_egl_draw (GstGLWindow * window)
206 gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
211 GstGLWindowVivFBEGL *window_egl;
212 GstVideoRectangle rect;
213 } SetRenderRectangleData;
216 _free_set_render_rectangle (SetRenderRectangleData * render)
219 if (render->window_egl)
220 gst_object_unref (render->window_egl);
226 _calculate_viewport_coordinates (GstGLWindowVivFBEGL * window_egl,
227 GstVideoRectangle * req, GstVideoRectangle * result)
230 result->y = window_egl->window_height - (req->y + req->h);
236 _set_render_rectangle (gpointer data)
238 SetRenderRectangleData *render = data;
239 GstGLWindowVivFBEGL *window_egl = render->window_egl;
240 GstGLWindow *window = GST_GL_WINDOW (window_egl);
242 GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
243 render->rect.x, render->rect.y, render->rect.w, render->rect.h);
245 _calculate_viewport_coordinates (window_egl, &render->rect,
246 &window_egl->render_rectangle);
248 gst_gl_window_resize (window, render->rect.w, render->rect.h);
250 window->queue_resize = TRUE;
254 gst_gl_window_viv_fb_egl_set_render_rectangle (GstGLWindow * window,
255 gint x, gint y, gint width, gint height)
257 GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
258 SetRenderRectangleData *render;
260 render = g_new0 (SetRenderRectangleData, 1);
261 render->window_egl = gst_object_ref (window_egl);
264 render->rect.w = width;
265 render->rect.h = height;
267 gst_gl_window_send_message_async (window,
268 (GstGLWindowCB) _set_render_rectangle, render,
269 (GDestroyNotify) _free_set_render_rectangle);
275 gst_gl_window_viv_fb_egl_controls_viewport (GstGLWindow * window)