tbm_module: make tbm_module_bufmgr_bind_native_display
[platform/core/uifw/libtbm.git] / src / tbm_dummy_display.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2018 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>,
8                  Changyeon Lee <cyeon.lee@samsung.com>,
9                  Sangjin Lee <lsj119@samsung.com>
10
11 Permission is hereby granted, free of charge, to any person obtaining a
12 copy of this software and associated documentation files (the
13 "Software"), to deal in the Software without restriction, including
14 without limitation the rights to use, copy, modify, merge, publish,
15 distribute, sub license, and/or sell copies of the Software, and to
16 permit persons to whom the Software is furnished to do so, subject to
17 the following conditions:
18
19 The above copyright notice and this permission notice (including the
20 next paragraph) shall be included in all copies or substantial portions
21 of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
26 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
31 **************************************************************************/
32 #include "config.h"
33 #include "tbm_dummy_display.h"
34 #include "tbm_bufmgr_int.h"
35 #include "list.h"
36
37 /* LCOV_EXCL_START */
38
39 static int init_list = 0;
40 static struct list_head g_dummy_display_list;
41 static pthread_mutex_t tbm_dummy_display_lock = PTHREAD_MUTEX_INITIALIZER;
42 void _tbm_dummy_display_mutex_unlock(void);
43
44 struct _tbm_dummy_display {
45         struct list_head link;
46 };
47
48 void
49 _tbm_dummy_display_mutex_lock(void)
50 {
51         pthread_mutex_lock(&tbm_dummy_display_lock);
52 }
53
54 void
55 _tbm_dummy_display_mutex_unlock(void)
56 {
57         pthread_mutex_unlock(&tbm_dummy_display_lock);
58 }
59
60 static int
61 _tbm_dummy_display_is_valid(struct _tbm_dummy_display *dummy_dpy)
62 {
63         struct _tbm_dummy_display *tmp_dpy = NULL;
64
65         if (!init_list) {
66                 _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
67                 return 0;
68         }
69
70         if (!LIST_IS_EMPTY(&g_dummy_display_list)) {
71                 LIST_FOR_EACH_ENTRY(tmp_dpy, &g_dummy_display_list, link) {
72                         if (tmp_dpy == dummy_dpy) {
73                                 TBM_INFO("tbm_dummy_display(%p)\n", dummy_dpy);
74                                 return 1;
75                         }
76                 }
77         }
78
79         _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
80
81         return 0;
82 }
83
84 tbm_dummy_display *
85 tbm_dummy_display_create(void)
86 {
87         struct _tbm_dummy_display *dpy = NULL;
88
89         _tbm_dummy_display_mutex_lock();
90         _tbm_set_last_result(TBM_ERROR_NONE);
91
92         /* init list */
93         if (!init_list) {
94                 LIST_INITHEAD(&g_dummy_display_list);
95                 init_list = 1;
96         }
97
98         dpy = calloc(1, sizeof(struct _tbm_dummy_display));
99         if (!dpy) {
100                 /* LCOV_EXCL_START */
101                 TBM_ERR("fail to allocate struct _tbm_dummy_display.\n");
102                 _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
103                 _tbm_dummy_display_mutex_unlock();
104                 return NULL;
105                 /* LCOV_EXCL_STOP */
106         }
107
108         LIST_ADD(&dpy->link, &g_dummy_display_list);
109
110         _tbm_dummy_display_mutex_unlock();
111
112         return (tbm_dummy_display *)dpy;
113 }
114
115 void
116 tbm_dummy_display_destroy(tbm_dummy_display *dpy)
117 {
118         struct _tbm_dummy_display *dummy_dpy = NULL;
119
120         _tbm_dummy_display_mutex_lock();
121         _tbm_set_last_result(TBM_ERROR_NONE);
122
123         dummy_dpy = (struct _tbm_dummy_display *)dpy;
124
125         if (!_tbm_dummy_display_is_valid(dummy_dpy)) {
126                 _tbm_dummy_display_mutex_unlock();
127                 return;
128         }
129
130         LIST_DEL(&dummy_dpy->link);
131
132         free(dummy_dpy);
133
134         _tbm_dummy_display_mutex_unlock();
135 }
136
137 int
138 tbm_dummy_display_is_valid(tbm_dummy_display *dpy)
139 {
140         int ret = 0;
141         struct _tbm_dummy_display *dummy_dpy = NULL;
142
143         _tbm_dummy_display_mutex_lock();
144         _tbm_set_last_result(TBM_ERROR_NONE);
145
146         dummy_dpy = (struct _tbm_dummy_display *)dpy;
147
148         /* Return silently if dummy_display is null. */
149         if (!dummy_dpy) {
150                 _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
151                 _tbm_dummy_display_mutex_unlock();
152                 return 0;
153         }
154
155         ret = _tbm_dummy_display_is_valid(dummy_dpy);
156
157         _tbm_dummy_display_mutex_unlock();
158
159         return ret;
160 }
161
162 /* LCOV_EXCL_STOP */