• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
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>
7  *
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.
12  *
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.
17  *
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.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include "../gstgl_fwd.h"
29 #include <gst/gl/gstglcontext.h>
30 #include <gst/gl/gstglfuncs.h>
31 
32 #include "gstglwindow_viv_fb_egl.h"
33 #include "../gstglwindow_private.h"
34 
35 #define GST_CAT_DEFAULT gst_gl_window_debug
36 
37 #define gst_gl_window_viv_fb_egl_parent_class parent_class
38 G_DEFINE_TYPE (GstGLWindowVivFBEGL, gst_gl_window_viv_fb_egl,
39     GST_TYPE_GL_WINDOW);
40 
41 static guintptr gst_gl_window_viv_fb_egl_get_window_handle (GstGLWindow *
42     window);
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,
45     guintptr handle);
46 static void gst_gl_window_viv_fb_egl_close (GstGLWindow * window);
47 static gboolean gst_gl_window_viv_fb_egl_open (GstGLWindow * window,
48     GError ** error);
49 static void gst_gl_window_viv_fb_egl_draw (GstGLWindow * window);
50 static gboolean
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 *
54     window);
55 
56 static void
gst_gl_window_viv_fb_egl_class_init(GstGLWindowVivFBEGLClass * klass)57 gst_gl_window_viv_fb_egl_class_init (GstGLWindowVivFBEGLClass * klass)
58 {
59   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
60 
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);
74 }
75 
76 static void
gst_gl_window_viv_fb_egl_init(GstGLWindowVivFBEGL * window)77 gst_gl_window_viv_fb_egl_init (GstGLWindowVivFBEGL * window)
78 {
79 }
80 
81 /* Must be called in the gl thread */
82 GstGLWindowVivFBEGL *
gst_gl_window_viv_fb_egl_new(GstGLDisplay * display)83 gst_gl_window_viv_fb_egl_new (GstGLDisplay * display)
84 {
85   GstGLWindowVivFBEGL *window;
86 
87   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_VIV_FB) ==
88       0)
89     /* we require a Vivante FB display to create windows */
90     return NULL;
91 
92   window = g_object_new (GST_TYPE_GL_WINDOW_VIV_FB_EGL, NULL);
93   gst_object_ref_sink (window);
94 
95   return window;
96 }
97 
98 static void
gst_gl_window_viv_fb_egl_close(GstGLWindow * window)99 gst_gl_window_viv_fb_egl_close (GstGLWindow * window)
100 {
101   GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
102 
103   if (window_egl->win_id && !window_egl->external_window) {
104     fbDestroyWindow (window_egl->win_id);
105     window_egl->win_id = 0;
106   }
107 
108   GST_GL_WINDOW_CLASS (parent_class)->close (window);
109 }
110 
111 static guintptr
gst_gl_window_viv_fb_egl_get_display(GstGLWindow * window)112 gst_gl_window_viv_fb_egl_get_display (GstGLWindow * window)
113 {
114   return gst_gl_display_get_handle (window->display);
115 }
116 
117 static gboolean
gst_gl_window_viv_fb_egl_open(GstGLWindow * window,GError ** error)118 gst_gl_window_viv_fb_egl_open (GstGLWindow * window, GError ** error)
119 {
120   GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
121   EGLNativeDisplayType display;
122 
123   display = (EGLNativeDisplayType) gst_gl_window_get_display (window);
124 
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");
130     return FALSE;
131   }
132 
133   return GST_GL_WINDOW_CLASS (parent_class)->open (window, error);
134 }
135 
136 void
gst_gl_window_viv_fb_egl_create_window(GstGLWindowVivFBEGL * window_egl)137 gst_gl_window_viv_fb_egl_create_window (GstGLWindowVivFBEGL * window_egl)
138 {
139   fbGetWindowGeometry (window_egl->win_id, NULL, NULL,
140       &window_egl->window_width, &window_egl->window_height);
141   window_egl->render_rectangle.x = 0;
142   window_egl->render_rectangle.y = 0;
143   window_egl->render_rectangle.w = window_egl->window_width;
144   window_egl->render_rectangle.h = window_egl->window_height;
145   gst_gl_window_resize (GST_GL_WINDOW (window_egl), window_egl->window_width,
146       window_egl->window_height);
147 
148   GST_DEBUG
149       ("create viv-fb window, resolution is (%dx%d), window %p.",
150       window_egl->window_width, window_egl->window_height,
151       (gpointer) window_egl->win_id);
152 }
153 
154 static guintptr
gst_gl_window_viv_fb_egl_get_window_handle(GstGLWindow * window)155 gst_gl_window_viv_fb_egl_get_window_handle (GstGLWindow * window)
156 {
157   return (guintptr) GST_GL_WINDOW_VIV_FB_EGL (window)->win_id;
158 }
159 
160 static void
gst_gl_window_viv_fb_egl_set_window_handle(GstGLWindow * window,guintptr handle)161 gst_gl_window_viv_fb_egl_set_window_handle (GstGLWindow * window,
162     guintptr handle)
163 {
164   GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
165   gint width, height;
166 
167   if (window_egl->win_id)
168     fbDestroyWindow (window_egl->win_id);
169   window_egl->win_id = (EGLNativeWindowType) handle;
170   window_egl->external_window = handle != 0;
171 
172   fbGetWindowGeometry (window_egl->win_id, NULL, NULL, &width, &height);
173   gst_gl_window_resize (window, width, height);
174 }
175 
176 static void
draw_cb(gpointer data)177 draw_cb (gpointer data)
178 {
179   GstGLWindowVivFBEGL *window_egl = data;
180   GstGLWindow *window = GST_GL_WINDOW (window_egl);
181   GstGLContext *context = gst_gl_window_get_context (window);
182   const GstGLFuncs *gl;
183   gint viewport_dim[4];
184 
185   gl = context->gl_vtable;
186 
187   if (window->queue_resize) {
188     guint width, height;
189 
190     gst_gl_window_get_surface_dimensions (window, &width, &height);
191     gst_gl_window_resize (window, width, height);
192 
193     gl->GetIntegerv (GL_VIEWPORT, viewport_dim);
194     window_egl->viewport.x = viewport_dim[0] + window_egl->render_rectangle.x;
195     window_egl->viewport.y = viewport_dim[1] + window_egl->render_rectangle.y;
196     window_egl->viewport.w = viewport_dim[2];
197     window_egl->viewport.h = viewport_dim[2];
198   }
199 
200   gl->Viewport (window_egl->viewport.x, window_egl->viewport.y,
201       window_egl->viewport.w, window_egl->viewport.h);
202 
203   if (window->draw)
204     window->draw (window->draw_data);
205 
206   gst_gl_context_swap_buffers (context);
207 
208   gst_object_unref (context);
209 }
210 
211 static void
gst_gl_window_viv_fb_egl_draw(GstGLWindow * window)212 gst_gl_window_viv_fb_egl_draw (GstGLWindow * window)
213 {
214   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
215 }
216 
217 typedef struct
218 {
219   GstGLWindowVivFBEGL *window_egl;
220   GstVideoRectangle rect;
221 } SetRenderRectangleData;
222 
223 static void
_free_set_render_rectangle(SetRenderRectangleData * render)224 _free_set_render_rectangle (SetRenderRectangleData * render)
225 {
226   if (render) {
227     if (render->window_egl)
228       gst_object_unref (render->window_egl);
229     g_free (render);
230   }
231 }
232 
233 static void
_calculate_viewport_coordinates(GstGLWindowVivFBEGL * window_egl,GstVideoRectangle * req,GstVideoRectangle * result)234 _calculate_viewport_coordinates (GstGLWindowVivFBEGL * window_egl,
235     GstVideoRectangle * req, GstVideoRectangle * result)
236 {
237   result->x = req->x;
238   result->y = window_egl->window_height - (req->y + req->h);
239   result->w = req->w;
240   result->h = req->h;
241 }
242 
243 static void
_set_render_rectangle(gpointer data)244 _set_render_rectangle (gpointer data)
245 {
246   SetRenderRectangleData *render = data;
247   GstGLWindowVivFBEGL *window_egl = render->window_egl;
248   GstGLWindow *window = GST_GL_WINDOW (window_egl);
249 
250   GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
251       render->rect.x, render->rect.y, render->rect.w, render->rect.h);
252 
253   _calculate_viewport_coordinates (window_egl, &render->rect,
254       &window_egl->render_rectangle);
255 
256   gst_gl_window_resize (window, render->rect.w, render->rect.h);
257 
258   window->queue_resize = TRUE;
259 }
260 
261 static gboolean
gst_gl_window_viv_fb_egl_set_render_rectangle(GstGLWindow * window,gint x,gint y,gint width,gint height)262 gst_gl_window_viv_fb_egl_set_render_rectangle (GstGLWindow * window,
263     gint x, gint y, gint width, gint height)
264 {
265   GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
266   SetRenderRectangleData *render;
267 
268   render = g_new0 (SetRenderRectangleData, 1);
269   render->window_egl = gst_object_ref (window_egl);
270   render->rect.x = x;
271   render->rect.y = y;
272   render->rect.w = width;
273   render->rect.h = height;
274 
275   gst_gl_window_send_message_async (window,
276       (GstGLWindowCB) _set_render_rectangle, render,
277       (GDestroyNotify) _free_set_render_rectangle);
278 
279   return TRUE;
280 }
281 
282 static gboolean
gst_gl_window_viv_fb_egl_controls_viewport(GstGLWindow * window)283 gst_gl_window_viv_fb_egl_controls_viewport (GstGLWindow * window)
284 {
285   return TRUE;
286 }
287