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.
28 #include "../gstgl_fwd.h"
29 #include <gst/gl/gstglcontext.h>
30 #include <gst/gl/gstglfuncs.h>
32 #include "gstglwindow_viv_fb_egl.h"
33 #include "../gstglwindow_private.h"
35 #define GST_CAT_DEFAULT gst_gl_window_debug
37 #define gst_gl_window_viv_fb_egl_parent_class parent_class
38 G_DEFINE_TYPE (GstGLWindowVivFBEGL, gst_gl_window_viv_fb_egl,
41 static guintptr gst_gl_window_viv_fb_egl_get_window_handle (GstGLWindow *
43 static guintptr gst_gl_window_viv_fb_egl_get_display (GstGLWindow * window);
44 static void gst_gl_window_viv_fb_egl_set_window_handle (GstGLWindow * window,
46 static void gst_gl_window_viv_fb_egl_close (GstGLWindow * window);
47 static gboolean gst_gl_window_viv_fb_egl_open (GstGLWindow * window,
49 static void gst_gl_window_viv_fb_egl_draw (GstGLWindow * window);
51 gst_gl_window_viv_fb_egl_set_render_rectangle (GstGLWindow * window,
52 gint x, gint y, gint width, gint height);
53 static gboolean gst_gl_window_viv_fb_egl_controls_viewport (GstGLWindow *
57 gst_gl_window_viv_fb_egl_class_init (GstGLWindowVivFBEGLClass * klass)
59 GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
61 window_class->get_window_handle =
62 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_get_window_handle);
63 window_class->get_display =
64 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_get_display);
65 window_class->set_window_handle =
66 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_set_window_handle);
67 window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_close);
68 window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_open);
69 window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_draw);
70 window_class->set_render_rectangle =
71 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_set_render_rectangle);
72 window_class->controls_viewport =
73 GST_DEBUG_FUNCPTR (gst_gl_window_viv_fb_egl_controls_viewport);
77 gst_gl_window_viv_fb_egl_init (GstGLWindowVivFBEGL * window)
81 /* Must be called in the gl thread */
83 gst_gl_window_viv_fb_egl_new (GstGLDisplay * display)
85 GstGLWindowVivFBEGL *window;
87 if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_VIV_FB) ==
89 /* we require a Vivante FB display to create windows */
92 window = g_object_new (GST_TYPE_GL_WINDOW_VIV_FB_EGL, NULL);
93 gst_object_ref_sink (window);
99 gst_gl_window_viv_fb_egl_close (GstGLWindow * window)
101 GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
103 if (window_egl->win_id && !window_egl->external_window) {
104 fbDestroyWindow (window_egl->win_id);
105 window_egl->win_id = 0;
108 GST_GL_WINDOW_CLASS (parent_class)->close (window);
112 gst_gl_window_viv_fb_egl_get_display (GstGLWindow * window)
114 return gst_gl_display_get_handle (window->display);
118 gst_gl_window_viv_fb_egl_open (GstGLWindow * window, GError ** error)
120 GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
121 EGLNativeDisplayType display;
123 display = (EGLNativeDisplayType) gst_gl_window_get_display (window);
125 window_egl->win_id = fbCreateWindow (display, -1, -1, 0, 0);
126 window_egl->external_window = FALSE;
127 if (!window_egl->win_id) {
128 g_set_error (error, GST_GL_WINDOW_ERROR,
129 GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE, "Can't create window");
133 fbGetWindowGeometry (window_egl->win_id, NULL, NULL,
134 &window_egl->window_width, &window_egl->window_height);
135 window_egl->render_rectangle.x = 0;
136 window_egl->render_rectangle.y = 0;
137 window_egl->render_rectangle.w = window_egl->window_width;
138 window_egl->render_rectangle.h = window_egl->window_height;
139 gst_gl_window_resize (window, window_egl->window_width,
140 window_egl->window_height);
143 ("Opened Vivante FB display succesfully, resolution is (%dx%d), display %p, window %p.",
144 window_egl->window_width, window_egl->window_height, (gpointer) display,
145 (gpointer) window_egl->win_id);
147 return GST_GL_WINDOW_CLASS (parent_class)->open (window, error);
151 gst_gl_window_viv_fb_egl_get_window_handle (GstGLWindow * window)
153 return (guintptr) GST_GL_WINDOW_VIV_FB_EGL (window)->win_id;
157 gst_gl_window_viv_fb_egl_set_window_handle (GstGLWindow * window,
160 GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
163 if (window_egl->win_id)
164 fbDestroyWindow (window_egl->win_id);
165 window_egl->win_id = (EGLNativeWindowType) handle;
166 window_egl->external_window = handle != 0;
168 fbGetWindowGeometry (window_egl->win_id, NULL, NULL, &width, &height);
169 gst_gl_window_resize (window, width, height);
173 draw_cb (gpointer data)
175 GstGLWindowVivFBEGL *window_egl = data;
176 GstGLWindow *window = GST_GL_WINDOW (window_egl);
177 GstGLContext *context = gst_gl_window_get_context (window);
178 const GstGLFuncs *gl;
179 gint viewport_dim[4];
181 gl = context->gl_vtable;
183 if (window->queue_resize) {
186 gst_gl_window_get_surface_dimensions (window, &width, &height);
187 gst_gl_window_resize (window, width, height);
189 gl->GetIntegerv (GL_VIEWPORT, viewport_dim);
190 window_egl->viewport.x = viewport_dim[0] + window_egl->render_rectangle.x;
191 window_egl->viewport.y = viewport_dim[1] + window_egl->render_rectangle.y;
192 window_egl->viewport.w = viewport_dim[2];
193 window_egl->viewport.h = viewport_dim[2];
196 gl->Viewport (window_egl->viewport.x, window_egl->viewport.y,
197 window_egl->viewport.w, window_egl->viewport.h);
200 window->draw (window->draw_data);
202 gst_gl_context_swap_buffers (context);
204 gst_object_unref (context);
208 gst_gl_window_viv_fb_egl_draw (GstGLWindow * window)
210 gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
215 GstGLWindowVivFBEGL *window_egl;
216 GstVideoRectangle rect;
217 } SetRenderRectangleData;
220 _free_set_render_rectangle (SetRenderRectangleData * render)
223 if (render->window_egl)
224 gst_object_unref (render->window_egl);
230 _calculate_viewport_coordinates (GstGLWindowVivFBEGL * window_egl,
231 GstVideoRectangle * req, GstVideoRectangle * result)
234 result->y = window_egl->window_height - (req->y + req->h);
240 _set_render_rectangle (gpointer data)
242 SetRenderRectangleData *render = data;
243 GstGLWindowVivFBEGL *window_egl = render->window_egl;
244 GstGLWindow *window = GST_GL_WINDOW (window_egl);
246 GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
247 render->rect.x, render->rect.y, render->rect.w, render->rect.h);
249 _calculate_viewport_coordinates (window_egl, &render->rect,
250 &window_egl->render_rectangle);
252 gst_gl_window_resize (window, render->rect.w, render->rect.h);
254 window->queue_resize = TRUE;
258 gst_gl_window_viv_fb_egl_set_render_rectangle (GstGLWindow * window,
259 gint x, gint y, gint width, gint height)
261 GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
262 SetRenderRectangleData *render;
264 render = g_new0 (SetRenderRectangleData, 1);
265 render->window_egl = gst_object_ref (window_egl);
268 render->rect.w = width;
269 render->rect.h = height;
271 gst_gl_window_send_message_async (window,
272 (GstGLWindowCB) _set_render_rectangle, render,
273 (GDestroyNotify) _free_set_render_rectangle);
279 gst_gl_window_viv_fb_egl_controls_viewport (GstGLWindow * window)