Merging gst-python
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / gst-libs / gst / gl / egl / gstgldisplay_egl_device.c
1 /*
2  * GStreamer
3  * Copyright (C) 2014 Matthew Waters <ystreet00@gmail.com>
4  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gstgldisplay_egl_device
24  * @short_description: EGL EGLDeviceEXT object
25  * @title: GstGLDisplayEGLDevice
26  * @see_also: #GstGLDisplay, #GstGLDisplayEGL
27  *
28  * #GstGLDisplayEGLDevice represents a `EGLDeviceEXT` handle created internally
29  * (gst_gl_display_egl_device_new()) or wrapped by the application
30  * (gst_gl_display_egl_device_new_with_egl_device())
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "gstgldisplay_egl.h"
38 #include "gstgldisplay_egl_device.h"
39
40 #include <gst/gl/gstglfeature.h>
41
42 #include "gstegl.h"
43 #include "gstglmemoryegl.h"
44
45 #ifndef EGL_DEVICE_EXT
46 typedef void *EGLDeviceEXT;
47 #endif
48
49 GST_DEBUG_CATEGORY_STATIC (gst_gl_display_debug);
50 #define GST_CAT_DEFAULT gst_gl_display_debug
51
52 typedef EGLBoolean (*eglQueryDevicesEXT_type) (EGLint max_devices,
53     EGLDeviceEXT * devices, EGLint * num_devices);
54
55 G_DEFINE_TYPE (GstGLDisplayEGLDevice, gst_gl_display_egl_device,
56     GST_TYPE_GL_DISPLAY);
57
58 static guintptr gst_gl_display_egl_device_get_handle (GstGLDisplay * display);
59
60 static void
61 gst_gl_display_egl_device_class_init (GstGLDisplayEGLDeviceClass * klass)
62 {
63   GstGLDisplayClass *display_class = GST_GL_DISPLAY_CLASS (klass);
64
65   display_class->get_handle =
66       GST_DEBUG_FUNCPTR (gst_gl_display_egl_device_get_handle);
67 }
68
69 static void
70 gst_gl_display_egl_device_init (GstGLDisplayEGLDevice * self)
71 {
72   GstGLDisplay *display = GST_GL_DISPLAY (self);
73
74   display->type = GST_GL_DISPLAY_TYPE_EGL_DEVICE;
75
76   gst_gl_memory_egl_init_once ();
77 }
78
79 static guintptr
80 gst_gl_display_egl_device_get_handle (GstGLDisplay * display)
81 {
82   GstGLDisplayEGLDevice *self = GST_GL_DISPLAY_EGL_DEVICE (display);
83
84   return (guintptr) self->device;
85 }
86
87 /**
88  * gst_gl_display_egl_device_new:
89  * @device_index: the index of device to use
90  *
91  * Create a new #GstGLDisplayEGLDevice with an EGLDevice supported device
92  *
93  * Returns: (transfer full): a new #GstGLDisplayEGLDevice or %NULL
94  *
95  * Since: 1.18
96  */
97 GstGLDisplayEGLDevice *
98 gst_gl_display_egl_device_new (guint device_index)
99 {
100   GstGLDisplayEGLDevice *ret;
101   eglQueryDevicesEXT_type query_device_func;
102   EGLint num_devices = 0;
103   EGLDeviceEXT *device_list;
104
105   GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
106
107   query_device_func =
108       (eglQueryDevicesEXT_type) eglGetProcAddress ("eglQueryDevicesEXT");
109
110   if (!query_device_func) {
111     GST_ERROR ("eglQueryDevicesEXT is unavailable");
112     return NULL;
113   }
114
115   if (query_device_func (0, NULL, &num_devices) == EGL_FALSE) {
116     GST_ERROR ("eglQueryDevicesEXT fail");
117     return NULL;
118   } else if (num_devices < 1) {
119     GST_ERROR ("no EGLDevice supported device");
120     return NULL;
121   }
122
123   if (num_devices <= device_index) {
124     GST_ERROR ("requested index %d exceeds the number of devices %d",
125         device_index, num_devices);
126     return NULL;
127   }
128
129   device_list = g_alloca (sizeof (EGLDeviceEXT) * num_devices);
130   query_device_func (num_devices, device_list, &num_devices);
131
132   ret = g_object_new (GST_TYPE_GL_DISPLAY_EGL_DEVICE, NULL);
133   gst_object_ref_sink (ret);
134
135   ret->device = device_list[device_index];
136
137   return ret;
138 }
139
140 /**
141  * gst_gl_display_egl_device_new_with_egl_device:
142  * @device: an existing EGLDeviceEXT
143  *
144  * Creates a new #GstGLDisplayEGLDevice with EGLDeviceEXT .
145  * The @device must be created using EGLDevice enumeration.
146  *
147  * Returns: (transfer full): a new #GstGLDisplayEGLDevice
148  *
149  * Since: 1.18
150  */
151 GstGLDisplayEGLDevice *
152 gst_gl_display_egl_device_new_with_egl_device (gpointer device)
153 {
154   GstGLDisplayEGLDevice *ret;
155
156   g_return_val_if_fail (device != NULL, NULL);
157
158   GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
159
160   ret = g_object_new (GST_TYPE_GL_DISPLAY_EGL_DEVICE, NULL);
161   gst_object_ref_sink (ret);
162
163   ret->device = device;
164
165   return ret;
166 }