OSDN Git Service

gbm_gralloc: Remove the invalid int cast and assignment in gbm_mod_perform()
[android-x86/external-gbm_gralloc.git] / gralloc.cpp
1 /*
2  * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3  * Copyright (C) 2010-2011 LunarG Inc.
4  * Copyright (C) 2016 Linaro, Ltd., Rob Herring <robh@kernel.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #define LOG_TAG "GRALLOC-GBM"
26
27 #include <cutils/log.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <pthread.h>
31 #include <errno.h>
32
33 #include <hardware/gralloc.h>
34 #include <system/graphics.h>
35
36 #include <gbm.h>
37
38 #include "gralloc_drm.h"
39 #include "gralloc_gbm_priv.h"
40
41 struct gbm_module_t {
42         gralloc_module_t base;
43
44         pthread_mutex_t mutex;
45         struct gbm_device *gbm;
46 };
47
48 static inline int gralloc_gbm_get_bpp(int format)
49 {
50         int bpp;
51
52         switch (format) {
53         case HAL_PIXEL_FORMAT_RGBA_8888:
54         case HAL_PIXEL_FORMAT_RGBX_8888:
55         case HAL_PIXEL_FORMAT_BGRA_8888:
56                 bpp = 4;
57                 break;
58         case HAL_PIXEL_FORMAT_RGB_888:
59                 bpp = 3;
60                 break;
61         case HAL_PIXEL_FORMAT_RGB_565:
62         case HAL_PIXEL_FORMAT_YCbCr_422_I:
63                 bpp = 2;
64                 break;
65         /* planar; only Y is considered */
66         case HAL_PIXEL_FORMAT_YV12:
67         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
68         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
69                 bpp = 1;
70                 break;
71         default:
72                 bpp = 0;
73                 break;
74         }
75
76         return bpp;
77 }
78
79 /*
80  * Initialize the DRM device object
81  */
82 static int gbm_init(struct gbm_module_t *dmod)
83 {
84         int err = 0;
85
86         pthread_mutex_lock(&dmod->mutex);
87         if (!dmod->gbm) {
88                 dmod->gbm = gbm_dev_create();
89                 if (!dmod->gbm)
90                         err = -EINVAL;
91         }
92         pthread_mutex_unlock(&dmod->mutex);
93
94         return err;
95 }
96
97 static int gbm_mod_perform(const struct gralloc_module_t *mod, int op, ...)
98 {
99         struct gbm_module_t *dmod = (struct gbm_module_t *) mod;
100         va_list args;
101         int err;
102         uint32_t uop = static_cast<uint32_t>(op);
103
104         err = gbm_init(dmod);
105         if (err)
106                 return err;
107
108         va_start(args, op);
109         switch (uop) {
110         case GRALLOC_MODULE_PERFORM_GET_DRM_FD:
111                 {
112                         int *fd = va_arg(args, int *);
113                         *fd = gbm_device_get_fd(dmod->gbm);
114                         err = 0;
115                 }
116                 break;
117         /* TODO: This is a stub and should be implemented fully */
118         case GRALLOC_MODULE_PERFORM_GET_USAGE:
119                 {
120                         err = 0;
121                 }
122                 break;
123         default:
124                 err = -EINVAL;
125                 break;
126         }
127         va_end(args);
128
129         return err;
130 }
131
132 static int gbm_mod_register_buffer(const gralloc_module_t *mod,
133                 buffer_handle_t handle)
134 {
135         struct gbm_module_t *dmod = (struct gbm_module_t *) mod;
136         int err;
137
138         err = gbm_init(dmod);
139         if (err)
140                 return err;
141
142         pthread_mutex_lock(&dmod->mutex);
143         err = gralloc_gbm_handle_register(handle, dmod->gbm);
144         pthread_mutex_unlock(&dmod->mutex);
145
146         return err;
147 }
148
149 static int gbm_mod_unregister_buffer(const gralloc_module_t *mod,
150                 buffer_handle_t handle)
151 {
152         struct gbm_module_t *dmod = (struct gbm_module_t *) mod;
153         int err;
154
155         pthread_mutex_lock(&dmod->mutex);
156         err = gralloc_gbm_handle_unregister(handle);
157         pthread_mutex_unlock(&dmod->mutex);
158
159         return err;
160 }
161
162 static int gbm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle,
163                 int usage, int x, int y, int w, int h, void **ptr)
164 {
165         struct gbm_module_t *dmod = (struct gbm_module_t *) mod;
166         struct gralloc_gbm_bo_t *bo;
167         int err;
168
169         pthread_mutex_lock(&dmod->mutex);
170
171         bo = gralloc_gbm_bo_from_handle(handle);
172         if (!bo) {
173                 err = -EINVAL;
174                 goto unlock;
175         }
176
177         err = gralloc_gbm_bo_lock(bo, usage, x, y, w, h, ptr);
178         ALOGE("buffer %p lock usage = %08x", handle, usage);
179
180 unlock:
181         pthread_mutex_unlock(&dmod->mutex);
182         return err;
183 }
184
185 static int gbm_mod_unlock(const gralloc_module_t *mod, buffer_handle_t handle)
186 {
187         struct gbm_module_t *dmod = (struct gbm_module_t *) mod;
188         struct gralloc_gbm_bo_t *bo;
189         int err = 0;
190
191         pthread_mutex_lock(&dmod->mutex);
192
193         bo = gralloc_gbm_bo_from_handle(handle);
194         if (!bo) {
195                 err = -EINVAL;
196                 goto unlock;
197         }
198
199         gralloc_gbm_bo_unlock(bo);
200
201 unlock:
202         pthread_mutex_unlock(&dmod->mutex);
203         return err;
204 }
205
206 static int gbm_mod_close_gpu0(struct hw_device_t *dev)
207 {
208         struct gbm_module_t *dmod = (struct gbm_module_t *)dev->module;
209         struct alloc_device_t *alloc = (struct alloc_device_t *) dev;
210
211         gbm_dev_destroy(dmod->gbm);
212         delete alloc;
213
214         return 0;
215 }
216
217 static int gbm_mod_free_gpu0(alloc_device_t *dev, buffer_handle_t handle)
218 {
219         struct gbm_module_t *dmod = (struct gbm_module_t *) dev->common.module;
220         struct gralloc_gbm_bo_t *bo;
221         int err = 0;
222
223         pthread_mutex_lock(&dmod->mutex);
224
225         bo = gralloc_gbm_bo_from_handle(handle);
226         if (!bo) {
227                 err = -EINVAL;
228                 goto unlock;
229         }
230
231         gralloc_gbm_bo_decref(bo);
232
233 unlock:
234         pthread_mutex_unlock(&dmod->mutex);
235         return err;
236 }
237
238 static int gbm_mod_alloc_gpu0(alloc_device_t *dev,
239                 int w, int h, int format, int usage,
240                 buffer_handle_t *handle, int *stride)
241 {
242         struct gbm_module_t *dmod = (struct gbm_module_t *) dev->common.module;
243         struct gralloc_gbm_bo_t *bo;
244         int err = 0;
245
246         pthread_mutex_lock(&dmod->mutex);
247
248         bo = gralloc_gbm_bo_create(dmod->gbm, w, h, format, usage);
249         if (!bo) {
250                 err = -errno;
251                 goto unlock;
252         }
253
254         *handle = gralloc_gbm_bo_get_handle(bo);
255         /* in pixels */
256         *stride = gbm_bo_get_stride(gralloc_gbm_bo_to_gbm_bo(bo)) /
257                 gralloc_gbm_get_bpp(format);
258
259         ALOGE("buffer %p usage = %08x", *handle, usage);
260 unlock:
261         pthread_mutex_unlock(&dmod->mutex);
262         return err;
263 }
264
265 static int gbm_mod_open_gpu0(struct gbm_module_t *dmod, hw_device_t **dev)
266 {
267         struct alloc_device_t *alloc;
268         int err;
269
270         err = gbm_init(dmod);
271         if (err)
272                 return err;
273
274         alloc = new alloc_device_t;
275         if (!alloc)
276                 return -EINVAL;
277
278         alloc->common.tag = HARDWARE_DEVICE_TAG;
279         alloc->common.version = 0;
280         alloc->common.module = &dmod->base.common;
281         alloc->common.close = gbm_mod_close_gpu0;
282
283         alloc->alloc = gbm_mod_alloc_gpu0;
284         alloc->free = gbm_mod_free_gpu0;
285
286         *dev = &alloc->common;
287
288         return 0;
289 }
290
291 static int gbm_mod_open(const struct hw_module_t *mod,
292                 const char *name, struct hw_device_t **dev)
293 {
294         struct gbm_module_t *dmod = (struct gbm_module_t *) mod;
295         int err;
296
297         if (strcmp(name, GRALLOC_HARDWARE_GPU0) == 0)
298                 err = gbm_mod_open_gpu0(dmod, dev);
299         else
300                 err = -EINVAL;
301
302         return err;
303 }
304
305 static struct hw_module_methods_t gbm_mod_methods = {
306         .open = gbm_mod_open
307 };
308
309 struct gbm_module_t HAL_MODULE_INFO_SYM = {
310         .base = {
311                 .common = {
312                         .tag = HARDWARE_MODULE_TAG,
313                         .version_major = 1,
314                         .version_minor = 0,
315                         .id = GRALLOC_HARDWARE_MODULE_ID,
316                         .name = "GBM Memory Allocator",
317                         .author = "Rob Herring - Linaro",
318                         .methods = &gbm_mod_methods
319                 },
320                 .registerBuffer = gbm_mod_register_buffer,
321                 .unregisterBuffer = gbm_mod_unregister_buffer,
322                 .lock = gbm_mod_lock,
323                 .unlock = gbm_mod_unlock,
324                 .perform = gbm_mod_perform
325         },
326
327         .mutex = PTHREAD_MUTEX_INITIALIZER,
328         .gbm = NULL,
329 };