implementation
[platform/core/uifw/libtdm.git] / src / tdm_backend.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <boram1288.park@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm_private.h"
41
42 #define BACKEND_FUNC_ENTRY() \
43         tdm_private_display *private_display; \
44         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER); \
45         private_display = (tdm_private_display*)dpy;
46
47 #define BACKEND_FUNC_ENTRY_VOID() \
48         tdm_private_display *private_display; \
49         TDM_RETURN_IF_FAIL(dpy != NULL); \
50         private_display = (tdm_private_display*)dpy;
51
52 static int
53 _check_abi_version(tdm_backend_module *module, int abimaj, int abimin)
54 {
55         int major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
56         int minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
57
58         if (major < abimaj) goto failed;
59         if (major > abimaj) return 0;
60         if (minor < abimin) goto failed;
61         return 0;
62 failed:
63         TDM_ERR("The ABI version(%d.%d) of '%s' is less than %d.%d",
64                         major, minor, module->name ? module->name : "unknown",
65                         abimaj, abimin);
66         return -1;
67 }
68
69 EXTERN tdm_error
70 tdm_backend_register_func_display(tdm_display *dpy,
71                                                                   tdm_func_display *func_display)
72 {
73         tdm_backend_module *module;
74
75         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
76
77         BACKEND_FUNC_ENTRY();
78
79         TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_INVALID_PARAMETER);
80
81         assert(private_display->current_module);
82
83         /* the ABI version of backend module should be more than 1.1 */
84         module = private_display->current_module->module_data;
85         if (_check_abi_version(module, 1, 1) < 0)
86                 return TDM_ERROR_BAD_MODULE;
87
88         private_display->current_module->func_display = *func_display;
89
90         return TDM_ERROR_NONE;
91 }
92
93 EXTERN tdm_error
94 tdm_backend_register_func_output(tdm_display *dpy, tdm_func_output *func_output)
95 {
96         tdm_backend_module *module;
97
98         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
99
100         BACKEND_FUNC_ENTRY();
101
102         TDM_RETURN_VAL_IF_FAIL(func_output != NULL, TDM_ERROR_INVALID_PARAMETER);
103
104         assert(private_display->current_module);
105
106         /* the ABI version of backend module should be more than 1.1 */
107         module = private_display->current_module->module_data;
108         if (_check_abi_version(module, 1, 1) < 0)
109                 return TDM_ERROR_BAD_MODULE;
110
111         private_display->current_module->func_output = *func_output;
112
113         return TDM_ERROR_NONE;
114 }
115
116 EXTERN tdm_error
117 tdm_backend_register_func_layer(tdm_display *dpy, tdm_func_layer *func_layer)
118 {
119         tdm_backend_module *module;
120
121         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
122
123         BACKEND_FUNC_ENTRY();
124
125         TDM_RETURN_VAL_IF_FAIL(func_layer != NULL, TDM_ERROR_INVALID_PARAMETER);
126
127         assert(private_display->current_module);
128
129         /* the ABI version of backend module should be more than 1.1 */
130         module = private_display->current_module->module_data;
131         if (_check_abi_version(module, 1, 1) < 0)
132                 return TDM_ERROR_BAD_MODULE;
133
134         private_display->current_module->func_layer = *func_layer;
135
136         return TDM_ERROR_NONE;
137 }
138
139 /* LCOV_EXCL_START */
140 EXTERN tdm_error
141 tdm_backend_register_func_hwc_window(tdm_display *dpy, tdm_func_hwc_window *func_hwc_window)
142 {
143         tdm_backend_module *module;
144
145         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
146
147         BACKEND_FUNC_ENTRY();
148
149         TDM_RETURN_VAL_IF_FAIL(func_hwc_window != NULL, TDM_ERROR_INVALID_PARAMETER);
150
151         assert(private_display->current_module);
152
153         module = private_display->current_module->module_data;
154         /* FIX ME:
155                    Temporarily, we set the version of hwc window to 1.1 for the development.
156                    Originally the hwc window version is 2.0. */
157         if (_check_abi_version(module, 1, 1) < 0)
158                 return TDM_ERROR_BAD_MODULE;
159
160         private_display->current_module->func_hwc_window = *func_hwc_window;
161
162         return TDM_ERROR_NONE;
163 }
164 /* LCOV_EXCL_STOP */
165
166 EXTERN tdm_error
167 tdm_backend_register_func_pp(tdm_display *dpy, tdm_func_pp *func_pp)
168 {
169         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
170
171         BACKEND_FUNC_ENTRY();
172
173         if (!func_pp)
174                 return TDM_ERROR_NONE;
175
176         assert(private_display->current_module);
177
178         private_display->current_module->capabilities |= TDM_DISPLAY_CAPABILITY_PP;
179         private_display->current_module->func_pp = *func_pp;
180
181         if (!private_display->pp_module)
182                 private_display->pp_module = private_display->current_module;
183         else
184                 TDM_ERR("already has pp backend(%s)", private_display->pp_module->module_data->name);
185
186         return TDM_ERROR_NONE;
187 }
188
189 EXTERN tdm_error
190 tdm_backend_register_func_capture(tdm_display *dpy,
191                                                                   tdm_func_capture *func_capture)
192 {
193         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
194
195         BACKEND_FUNC_ENTRY();
196
197         if (!func_capture)
198                 return TDM_ERROR_NONE;
199
200         assert(private_display->current_module);
201
202         private_display->current_module->capabilities |= TDM_DISPLAY_CAPABILITY_CAPTURE;
203         private_display->current_module->func_capture = *func_capture;
204
205         //TODO: remove later
206         if (!private_display->capture_module)
207                 private_display->capture_module = private_display->current_module;
208         else
209                 TDM_ERR("already has capture backend(%s)", private_display->capture_module->module_data->name);
210
211         return TDM_ERROR_NONE;
212 }
213
214 EXTERN tdm_error
215 tdm_backend_register_output(tdm_display *dpy, tdm_output *output)
216 {
217         tdm_error ret;
218
219         BACKEND_FUNC_ENTRY();
220
221         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
222         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_OPERATION_FAILED);
223
224         /* this function is only for backend. if backend calls this function, it means
225          * that it's triggered by frontend. frontend should set current_module before calling
226          * backend functions.
227          */
228         TDM_RETURN_VAL_IF_FAIL(private_display->current_module != NULL, TDM_ERROR_OPERATION_FAILED);
229
230         ret = tdm_display_update_output(private_display->current_module, output);
231         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
232
233         return TDM_ERROR_NONE;
234 }
235
236 EXTERN void
237 tdm_backend_unregister_output(tdm_display *dpy, tdm_output *output)
238 {
239         tdm_private_output *private_output;
240
241         BACKEND_FUNC_ENTRY_VOID();
242
243         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
244         TDM_RETURN_IF_FAIL(output != NULL);
245
246         /* this function is only for backend. if backend calls this function, it means
247          * that it's triggered by frontend. frontend should set current_module before calling
248          * backend functions.
249          */
250         TDM_RETURN_IF_FAIL(private_display->current_module != NULL);
251
252         private_output = tdm_display_find_private_output(private_display, output);
253         tdm_display_destroy_private_output(private_output);
254 }
255
256 /* LCOV_EXCL_START */
257 /* backend operates itself types */
258 static tdm_private_output*
259 _look_for_frontend_hwc_output(tdm_output *backend_output)
260 {
261         tdm_private_output *frontend_output = NULL, *o = NULL;
262         tdm_private_display *private_display = tdm_display_get();
263         tdm_private_module *private_module = NULL;
264
265         LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
266                 LIST_FOR_EACH_ENTRY(o, &private_module->output_list, link) {
267                         if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
268                                 continue;
269
270                         if (o->output_backend == backend_output) {
271                                 frontend_output = o;
272                                 return frontend_output;
273                         }
274                 }
275         }
276
277         return NULL;
278 }
279
280 EXTERN tdm_error
281 tdm_backend_trigger_need_validate_event(tdm_output *output)
282 {
283         tdm_private_output *private_output;
284         uint64_t value;
285         int res;
286
287         private_output = _look_for_frontend_hwc_output(output);
288         TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_INVALID_PARAMETER);
289
290         value = 1;
291
292         /* do not lock the global display lock here */
293
294         res = write(private_output->need_validate.event_fd, &value, sizeof(value));
295         if (res < 0)
296                 return TDM_ERROR_OPERATION_FAILED;
297
298         return TDM_ERROR_NONE;
299 }
300 /* LCOV_EXCL_STOP */