correct the header file dependency
[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 <sc1.lim@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 static int
48 _check_abi_version(tdm_backend_module *module, int abimaj, int abimin)
49 {
50         int major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
51         int minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
52
53         if (major < abimaj) goto failed;
54         if (major > abimaj) return 0;
55         if (minor < abimin) goto failed;
56         return 0;
57 failed:
58         TDM_ERR("The ABI version(%d.%d) of '%s' is less than %d.%d",
59                         major, minor, module->name ? module->name : "unknown",
60                         abimaj, abimin);
61         return -1;
62 }
63
64 EXTERN tdm_error
65 tdm_backend_register_func_display(tdm_display *dpy,
66                                                                   tdm_func_display *func_display)
67 {
68         tdm_backend_module *module;
69
70         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
71
72         BACKEND_FUNC_ENTRY();
73
74         TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_INVALID_PARAMETER);
75
76         /* the ABI version of backend module should be more than 1.1 */
77         module = private_display->module_data;
78         if (_check_abi_version(module, 1, 1) < 0)
79                 return TDM_ERROR_BAD_MODULE;
80
81         private_display->func_display = *func_display;
82
83         return TDM_ERROR_NONE;
84 }
85
86 EXTERN tdm_error
87 tdm_backend_register_func_output(tdm_display *dpy, tdm_func_output *func_output)
88 {
89         tdm_backend_module *module;
90
91         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
92
93         BACKEND_FUNC_ENTRY();
94
95         TDM_RETURN_VAL_IF_FAIL(func_output != NULL, TDM_ERROR_INVALID_PARAMETER);
96
97         /* the ABI version of backend module should be more than 1.1 */
98         module = private_display->module_data;
99         if (_check_abi_version(module, 1, 1) < 0)
100                 return TDM_ERROR_BAD_MODULE;
101
102         private_display->func_output = *func_output;
103
104         return TDM_ERROR_NONE;
105 }
106
107 EXTERN tdm_error
108 tdm_backend_register_func_layer(tdm_display *dpy, tdm_func_layer *func_layer)
109 {
110         tdm_backend_module *module;
111
112         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
113
114         BACKEND_FUNC_ENTRY();
115
116         TDM_RETURN_VAL_IF_FAIL(func_layer != NULL, TDM_ERROR_INVALID_PARAMETER);
117
118         /* the ABI version of backend module should be more than 1.1 */
119         module = private_display->module_data;
120         if (_check_abi_version(module, 1, 1) < 0)
121                 return TDM_ERROR_BAD_MODULE;
122
123         private_display->func_layer = *func_layer;
124
125         return TDM_ERROR_NONE;
126 }
127
128 EXTERN tdm_error
129 tdm_backend_register_func_hwc_window(tdm_display *dpy, tdm_func_hwc_window *func_hwc_window)
130 {
131         tdm_backend_module *module;
132
133         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
134
135         BACKEND_FUNC_ENTRY();
136
137         TDM_RETURN_VAL_IF_FAIL(func_hwc_window != NULL, TDM_ERROR_INVALID_PARAMETER);
138
139         module = private_display->module_data;
140         /* FIX ME:
141                    Temporarily, we set the version of hwc window to 1.1 for the development.
142                    Originally the hwc window version is 2.0. */
143         if (_check_abi_version(module, 1, 1) < 0)
144                 return TDM_ERROR_BAD_MODULE;
145
146         private_display->func_hwc_window = *func_hwc_window;
147
148         return TDM_ERROR_NONE;
149 }
150
151 EXTERN tdm_error
152 tdm_backend_register_func_pp(tdm_display *dpy, tdm_func_pp *func_pp)
153 {
154         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
155
156         BACKEND_FUNC_ENTRY();
157
158         if (!func_pp)
159                 return TDM_ERROR_NONE;
160
161         private_display->capabilities |= TDM_DISPLAY_CAPABILITY_PP;
162         private_display->func_pp = *func_pp;
163
164         return TDM_ERROR_NONE;
165 }
166
167 EXTERN tdm_error
168 tdm_backend_register_func_capture(tdm_display *dpy,
169                                                                   tdm_func_capture *func_capture)
170 {
171         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
172
173         BACKEND_FUNC_ENTRY();
174
175         if (!func_capture)
176                 return TDM_ERROR_NONE;
177
178         private_display->capabilities |= TDM_DISPLAY_CAPABILITY_CAPTURE;
179         private_display->func_capture = *func_capture;
180
181         return TDM_ERROR_NONE;
182 }
183
184 /* backend operates itself types */
185 static tdm_private_output*
186 _look_for_frontend_hwc_output(tdm_output *backend_output)
187 {
188         tdm_private_output *frontend_output = NULL, *o = NULL;
189         tdm_private_display *dpy;
190
191         dpy = tdm_display_init(NULL);
192         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, NULL);
193
194         LIST_FOR_EACH_ENTRY(o, &dpy->output_list, link) {
195                 if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
196                         continue;
197
198                 if (o->output_backend == backend_output) {
199                         frontend_output = o;
200                         break;
201                 }
202         }
203
204         tdm_display_deinit(dpy);
205
206         return frontend_output;
207 }
208
209 EXTERN tdm_error
210 tdm_backend_trigger_need_validate_event(tdm_output *output)
211 {
212         tdm_private_output *private_output;
213         uint64_t value;
214         int res;
215
216         private_output = _look_for_frontend_hwc_output(output);
217         TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_INVALID_PARAMETER);
218
219         value = 1;
220
221         /* do not lock the global display lock here */
222
223         res = write(private_output->need_validate.event_fd, &value, sizeof(value));
224         if (res < 0)
225                 return TDM_ERROR_OPERATION_FAILED;
226
227         return TDM_ERROR_NONE;
228 }