add tbm surface as the Core api
[platform/core/uifw/libtbm.git] / src / tbm_surface.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #include "config.h"
33 #include "tbm_bufmgr.h"
34 #include "tbm_bufmgr_int.h"
35 #include "tbm_surface_internal.h"
36
37 extern tbm_bufmgr gBufMgr;
38
39 static int
40 _tbm_surface_get_info (struct _tbm_surface *surf, int opt, tbm_surface_info_s *info, int map)
41 {
42     tbm_bo_handle bo_handles[4];
43     int i;
44
45     info->width = surf->info.width;
46     info->height = surf->info.height;
47     info->format = surf->info.format;
48     info->bpp = surf->info.bpp;
49     info->size = surf->info.size;
50     info->num_planes = surf->info.num_planes;
51
52     if (surf->num_bos == 1)
53     {
54         if (map == 1)
55         {
56             bo_handles[0] = tbm_bo_map (surf->bos[0], TBM_DEVICE_CPU, opt);
57             if (bo_handles[0].ptr == NULL)
58                 return 0;
59         }
60         else
61         {
62             bo_handles[0] = tbm_bo_get_handle (surf->bos[0], TBM_DEVICE_CPU);
63             if (bo_handles[0].ptr == NULL)
64                 return 0;
65         }
66
67         for (i = 0; i < surf->info.num_planes; i++)
68         {
69             info->planes[i].size = surf->info.planes[i].size;
70             info->planes[i].offset = surf->info.planes[i].offset;
71             info->planes[i].stride = surf->info.planes[i].stride;
72             info->planes[i].ptr = bo_handles[0].ptr + surf->info.planes[i].offset;
73         }
74     }
75     else
76     {
77         /* TODO: calculate the virtaul address when num_bos is over 1 */
78     }
79
80     return 1;
81 }
82
83 int
84 tbm_surface_query_formats (uint32_t **formats, uint32_t *num)
85 {
86     if (!gBufMgr)
87     {
88         gBufMgr = tbm_bufmgr_init (-1);
89     }
90
91     if (!tbm_surface_internal_query_supported_formats (gBufMgr, formats, num))
92         return TBM_SURFACE_ERROR_INVALID_OPERATION;
93
94     return TBM_SURFACE_ERROR_NONE;
95 }
96
97 tbm_surface_h
98 tbm_surface_create (int width, int height, tbm_format format)
99 {
100     if (!(width > 0) || !(height > 0))
101     {
102         //set_last_result (TBM_SURFACE_ERROR_INVALID_PARAMETER);
103         return NULL;
104     }
105
106     struct _tbm_surface *surf = NULL;
107
108     if (!gBufMgr)
109     {
110         gBufMgr = tbm_bufmgr_init (-1);
111     }
112
113     surf = tbm_surface_internal_create_with_flags (gBufMgr, width, height, format, TBM_BO_DEFAULT);
114     if (!surf)
115     {
116         //set_last_result (TBM_SURFACE_ERROR_INVALID_OPERATION);
117         return NULL;
118     }
119
120     //set_last_result (TBM_SURFACE_ERROR_NONE);
121     return surf;
122 }
123
124
125 int
126 tbm_surface_destroy (tbm_surface_h surface)
127 {
128     struct _tbm_surface *surf = NULL;
129     int i;
130
131     if (!surface)
132         return TBM_SURFACE_ERROR_INVALID_PARAMETER;
133
134     surf = (struct _tbm_surface *)surface;
135
136     for (i = 0; i < surf->num_bos; i++)
137     {
138         tbm_bo_unref (surf->bos[i]);
139         surf->bos[i] = NULL;
140     }
141
142     free (surf);
143     surf = NULL;
144
145     return TBM_SURFACE_ERROR_NONE;
146 }
147
148 int
149 tbm_surface_map (tbm_surface_h surface, int opt, tbm_surface_info_s *info)
150 {
151     TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
152     TBM_RETURN_VAL_IF_FAIL (info != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
153
154     struct _tbm_surface *surf = (struct _tbm_surface *)surface;
155     int ret = 0;
156
157     ret = _tbm_surface_get_info (surf, opt, info, 1);
158     if (ret == 0)
159         return TBM_SURFACE_ERROR_INVALID_OPERATION;
160
161     return TBM_SURFACE_ERROR_NONE;
162 }
163
164 int
165 tbm_surface_unmap (tbm_surface_h surface)
166 {
167     TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
168
169     struct _tbm_surface *surf = (struct _tbm_surface *)surface;
170     int i;
171
172     for (i = 0; i < surf->num_bos; i++)
173         tbm_bo_unmap (surf->bos[i]);
174
175     return TBM_SURFACE_ERROR_NONE;
176 }
177
178 int
179 tbm_surface_get_info (tbm_surface_h surface, tbm_surface_info_s *info)
180 {
181     TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
182     TBM_RETURN_VAL_IF_FAIL (info != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
183
184     struct _tbm_surface *surf = (struct _tbm_surface *)surface;
185     int ret = 0;
186
187     ret = _tbm_surface_get_info (surf, 0, info, 0);
188     if (ret == 0)
189         return TBM_SURFACE_ERROR_INVALID_OPERATION;
190
191     return TBM_SURFACE_ERROR_NONE;
192 }
193
194 int
195 tbm_surface_get_width (tbm_surface_h surface)
196 {
197     TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
198
199     struct _tbm_surface *surf = (struct _tbm_surface *)surface;
200
201     return surf->info.width;
202 }
203
204 int
205 tbm_surface_get_height (tbm_surface_h surface)
206 {
207     TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
208
209     struct _tbm_surface *surf = (struct _tbm_surface *)surface;
210
211     return surf->info.height;
212 }
213
214 tbm_format
215 tbm_surface_get_format (tbm_surface_h surface)
216 {
217     if (surface)
218     {
219         //set_last_result (TBM_SURFACE_ERROR_INVALID_PARAMETER);
220         return 0;
221     }
222
223     struct _tbm_surface *surf = (struct _tbm_surface *)surface;
224
225     //set_last_result (TBM_SURFACE_ERROR_NONE);
226     return surf->info.format;
227 }
228