add tbm_sync to support for the timeline and the fence
[platform/core/uifw/libtbm.git] / src / tbm_sync.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2012 - 2016 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>,
8          Changyeon Lee <cyeon.lee@samsung.com>,
9                  Boram Park <boram1288.park@samsung.com>,
10                  Sangjin Lee <lsj119@samsung.com>
11
12 Permission is hereby granted, free of charge, to any person obtaining a
13 copy of this software and associated documentation files (the
14 "Software"), to deal in the Software without restriction, including
15 without limitation the rights to use, copy, modify, merge, publish,
16 distribute, sub license, and/or sell copies of the Software, and to
17 permit persons to whom the Software is furnished to do so, subject to
18 the following conditions:
19
20 The above copyright notice and this permission notice (including the
21 next paragraph) shall be included in all copies or substantial portions
22 of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
27 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
28 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
32 **************************************************************************/
33
34 #include "tbm_bufmgr.h"
35 #include "tbm_bufmgr_int.h"
36 #include "tbm_sync.h"
37
38 static pthread_mutex_t tbm_sync_lock;
39 static int tbm_sync_support = 0;
40
41 static bool
42 _tbm_sync_mutex_init(void)
43 {
44         static bool tbm_sync_mutex_init = false;
45
46         if (tbm_sync_mutex_init)
47                 return true;
48
49         if (pthread_mutex_init(&tbm_sync_lock, NULL)) {
50                 TBM_LOG_E("fail: tbm_sync mutex init\n");
51                 return false;
52         }
53
54         tbm_sync_mutex_init = true;
55
56         return true;
57 }
58
59 static void
60 _tbm_sync_mutex_lock(void)
61 {
62         if (!_tbm_sync_mutex_init())
63                 return;
64
65         pthread_mutex_lock(&tbm_sync_lock);
66 }
67
68 static void
69 _tbm_sync_mutex_unlock(void)
70 {
71         pthread_mutex_unlock(&tbm_sync_lock);
72 }
73
74 static tbm_sync_error_e
75 _tbm_sync_check_capability(void)
76 {
77         tbm_bufmgr bufmgr = NULL;
78         unsigned int capabilities = TBM_BUFMGR_CAPABILITY_NONE;
79
80     if (tbm_sync_support)
81                 return TBM_SYNC_ERROR_NONE;
82
83         /* check the bufmgr */
84         bufmgr = _tbm_bufmgr_get_bufmgr();
85         if (!bufmgr) {
86                 return TBM_SYNC_ERROR_INVALID_OPERATION;
87         }
88
89         /* check the tbm_sync capability */
90         capabilities = tbm_bufmgr_get_capability(bufmgr);
91         if ((capabilities&TBM_BUFMGR_CAPABILITY_TBM_SYNC) != TBM_BUFMGR_CAPABILITY_TBM_SYNC) {
92                 //TODO: check the sw_sync device node... to verify the timeline sync
93                 tbm_sync_support = 1;
94
95                 return TBM_SYNC_ERROR_INVALID_OPERATION;
96         }
97
98         return TBM_SYNC_ERROR_NONE;
99 }
100
101 tbm_sync_timeline_h
102 tbm_sync_timeline_create(tbm_sync_error_e *error)
103 {
104         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
105         tbm_sync_timeline_h timeline = NULL;
106
107         _tbm_sync_mutex_lock();
108
109         /* check the tbm_sync capability */
110         ret = _tbm_sync_check_capability();;
111         if (ret != TBM_SYNC_ERROR_NONE)
112                 goto done;
113
114         /* TODO: sync_timeline_create */
115
116
117 done:
118         if (error)
119                 *error = ret;
120
121         _tbm_sync_mutex_unlock();
122
123         return timeline;
124 }
125
126 tbm_sync_error_e
127 tbm_sync_timeline_destroy(tbm_sync_timeline_h timeline)
128 {
129         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
130
131         _tbm_sync_mutex_lock();
132
133         /* check the tbm_sync capability */
134         ret = _tbm_sync_check_capability();;
135         if (ret != TBM_SYNC_ERROR_NONE)
136                 goto done;
137
138         /* TODO: sync_timeline_destroy */
139
140
141 done:
142         _tbm_sync_mutex_unlock();
143
144     return ret;
145 }
146
147 tbm_sync_timeline_h
148 tbm_sync_timeline_import(tbm_fd fd, tbm_sync_error_e *error)
149 {
150         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
151         tbm_sync_timeline_h timeline = NULL;
152
153         _tbm_sync_mutex_lock();
154
155         /* check the tbm_sync capability */
156         ret = _tbm_sync_check_capability();;
157         if (ret != TBM_SYNC_ERROR_NONE)
158                 goto done;
159
160         /* TODO: sync_timeline_import */
161
162 done:
163         if (error)
164                 *error = ret;
165
166         _tbm_sync_mutex_unlock();
167
168         return timeline;
169 }
170
171 tbm_sync_error_e
172 tbm_sync_timeline_increase_count(tbm_sync_timeline_h timeline, unsigned int interval)
173 {
174         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
175
176         _tbm_sync_mutex_lock();
177
178         /* check the tbm_sync capability */
179         ret = _tbm_sync_check_capability();;
180         if (ret != TBM_SYNC_ERROR_NONE)
181                 goto done;
182
183         /* TODO: sync_timeline_increase_count */
184
185 done:
186         _tbm_sync_mutex_unlock();
187
188         return ret;
189 }
190
191 unsigned int
192 tbm_sync_timeline_get_cur_count(tbm_sync_timeline_h timeline, tbm_sync_error_e *error)
193 {
194         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
195         unsigned int cur_count = 0;
196
197         _tbm_sync_mutex_lock();
198
199         /* check the tbm_sync capability */
200         ret = _tbm_sync_check_capability();;
201         if (ret != TBM_SYNC_ERROR_NONE)
202                 goto done;
203
204         /* TODO: sync_timeline_get_cur_count */
205
206 done:
207         if (error)
208                 *error = ret;
209
210         _tbm_sync_mutex_unlock();
211
212         return cur_count;
213 }
214
215 tbm_sync_fence_h
216 tbm_sync_fence_create(tbm_sync_timeline_h timeline, const char *name, unsigned int count_val, tbm_sync_error_e *error)
217 {
218         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
219         tbm_sync_fence_h fence = NULL;
220
221         _tbm_sync_mutex_lock();
222
223         /* check the tbm_sync capability */
224         ret = _tbm_sync_check_capability();;
225         if (ret != TBM_SYNC_ERROR_NONE)
226                 goto done;
227
228         /* TODO: sync_fence_create */
229
230 done:
231         if (error)
232                 *error = ret;
233
234         _tbm_sync_mutex_unlock();
235
236         return fence;
237 }
238
239 tbm_sync_error_e
240 tbm_sync_fence_destroy(tbm_sync_fence_h fence)
241 {
242         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
243
244         _tbm_sync_mutex_lock();
245
246         /* check the tbm_sync capability */
247         ret = _tbm_sync_check_capability();;
248         if (ret != TBM_SYNC_ERROR_NONE)
249                 goto done;
250
251         /* TODO: sync_fence_destroy */
252
253 done:
254         _tbm_sync_mutex_unlock();
255
256         return ret;
257 }
258
259 tbm_sync_error_e
260 tbm_sync_fence_wait(tbm_sync_fence_h fence, int timeout)
261 {
262         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
263
264         _tbm_sync_mutex_lock();
265
266         /* check the tbm_sync capability */
267         ret = _tbm_sync_check_capability();;
268         if (ret != TBM_SYNC_ERROR_NONE)
269                 goto done;
270
271         /* TODO: sync_fence_wait */
272
273 done:
274         _tbm_sync_mutex_unlock();
275
276         return ret;
277 }
278
279 tbm_sync_fence_h
280 tbm_sync_fence_merge(tbm_sync_fence_h fence1, tbm_sync_fence_h fence2, const char *name, tbm_sync_error_e *error)
281 {
282         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
283         tbm_sync_fence_h fence = NULL;
284
285         _tbm_sync_mutex_lock();
286
287         /* check the tbm_sync capability */
288         ret = _tbm_sync_check_capability();;
289         if (ret != TBM_SYNC_ERROR_NONE)
290                 goto done;
291
292         /* TODO: tbm_sync_fence_merge */
293
294 done:
295         if (error)
296                 *error = ret;
297
298         _tbm_sync_mutex_unlock();
299
300         return fence;
301 }
302
303 unsigned int
304 tbm_sync_fence_get_count_val(tbm_sync_fence_h fence, tbm_sync_error_e *error)
305 {
306         tbm_sync_error_e ret = TBM_SYNC_ERROR_NONE;
307         unsigned int count_val = 0;
308
309         _tbm_sync_mutex_lock();
310
311         /* check the tbm_sync capability */
312         ret = _tbm_sync_check_capability();;
313         if (ret != TBM_SYNC_ERROR_NONE)
314                 goto done;
315
316         /* TODO: sync_fence_get_count_val */
317
318 done:
319         if (error)
320                 *error = ret;
321
322         _tbm_sync_mutex_unlock();
323
324         return count_val;
325 }
326