viv-fb: fix build break for GST_GL_API
[platform/upstream/gst-plugins-base.git] / gst-libs / gst / gl / viv-fb / gstglwindow_viv_fb_egl.c
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
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
77 gst_gl_window_viv_fb_egl_init (GstGLWindowVivFBEGL * window)
78 {
79 }
80
81 /* Must be called in the gl thread */
82 GstGLWindowVivFBEGL *
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
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
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
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   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);
141
142   GST_DEBUG
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);
146
147   return GST_GL_WINDOW_CLASS (parent_class)->open (window, error);
148 }
149
150 static guintptr
151 gst_gl_window_viv_fb_egl_get_window_handle (GstGLWindow * window)
152 {
153   return (guintptr) GST_GL_WINDOW_VIV_FB_EGL (window)->win_id;
154 }
155
156 static void
157 gst_gl_window_viv_fb_egl_set_window_handle (GstGLWindow * window,
158     guintptr handle)
159 {
160   GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
161   gint width, height;
162
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;
167
168   fbGetWindowGeometry (window_egl->win_id, NULL, NULL, &width, &height);
169   gst_gl_window_resize (window, width, height);
170 }
171
172 static void
173 draw_cb (gpointer data)
174 {
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];
180
181   gl = context->gl_vtable;
182
183   if (window->queue_resize) {
184     guint width, height;
185
186     gst_gl_window_get_surface_dimensions (window, &width, &height);
187     gst_gl_window_resize (window, width, height);
188
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];
194   }
195
196   gl->Viewport (window_egl->viewport.x, window_egl->viewport.y,
197       window_egl->viewport.w, window_egl->viewport.h);
198
199   if (window->draw)
200     window->draw (window->draw_data);
201
202   gst_gl_context_swap_buffers (context);
203
204   gst_object_unref (context);
205 }
206
207 static void
208 gst_gl_window_viv_fb_egl_draw (GstGLWindow * window)
209 {
210   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
211 }
212
213 typedef struct
214 {
215   GstGLWindowVivFBEGL *window_egl;
216   GstVideoRectangle rect;
217 } SetRenderRectangleData;
218
219 static void
220 _free_set_render_rectangle (SetRenderRectangleData * render)
221 {
222   if (render) {
223     if (render->window_egl)
224       gst_object_unref (render->window_egl);
225     g_free (render);
226   }
227 }
228
229 static void
230 _calculate_viewport_coordinates (GstGLWindowVivFBEGL * window_egl,
231     GstVideoRectangle * req, GstVideoRectangle * result)
232 {
233   result->x = req->x;
234   result->y = window_egl->window_height - (req->y + req->h);
235   result->w = req->w;
236   result->h = req->h;
237 }
238
239 static void
240 _set_render_rectangle (gpointer data)
241 {
242   SetRenderRectangleData *render = data;
243   GstGLWindowVivFBEGL *window_egl = render->window_egl;
244   GstGLWindow *window = GST_GL_WINDOW (window_egl);
245
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);
248
249   _calculate_viewport_coordinates (window_egl, &render->rect,
250       &window_egl->render_rectangle);
251
252   gst_gl_window_resize (window, render->rect.w, render->rect.h);
253
254   window->queue_resize = TRUE;
255 }
256
257 static gboolean
258 gst_gl_window_viv_fb_egl_set_render_rectangle (GstGLWindow * window,
259     gint x, gint y, gint width, gint height)
260 {
261   GstGLWindowVivFBEGL *window_egl = GST_GL_WINDOW_VIV_FB_EGL (window);
262   SetRenderRectangleData *render;
263
264   render = g_new0 (SetRenderRectangleData, 1);
265   render->window_egl = gst_object_ref (window_egl);
266   render->rect.x = x;
267   render->rect.y = y;
268   render->rect.w = width;
269   render->rect.h = height;
270
271   gst_gl_window_send_message_async (window,
272       (GstGLWindowCB) _set_render_rectangle, render,
273       (GDestroyNotify) _free_set_render_rectangle);
274
275   return TRUE;
276 }
277
278 static gboolean
279 gst_gl_window_viv_fb_egl_controls_viewport (GstGLWindow * window)
280 {
281   return TRUE;
282 }