-
Notifications
You must be signed in to change notification settings - Fork 0
/
gralloc.c
361 lines (295 loc) · 8.13 KB
/
gralloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright (C) 2010-2011 Chia-I Wu <[email protected]>
* Copyright (C) 2010-2011 LunarG Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#define LOG_TAG "GRALLOC-MOD"
#include <cutils/log.h>
#include <stdlib.h>
#include <stdarg.h>
#include <pthread.h>
#include <errno.h>
#include "gralloc_drm.h"
#include "gralloc_drm_priv.h"
/*
* Initialize the DRM device object, optionally with KMS.
*/
static int drm_init(struct drm_module_t *dmod, int kms)
{
int err = 0;
pthread_mutex_lock(&dmod->mutex);
if (!dmod->drm) {
dmod->drm = gralloc_drm_create();
if (!dmod->drm)
err = -EINVAL;
}
if (!err && kms)
err = gralloc_drm_init_kms(dmod->drm);
pthread_mutex_unlock(&dmod->mutex);
return err;
}
static int drm_mod_perform(const struct gralloc_module_t *mod, int op, ...)
{
struct drm_module_t *dmod = (struct drm_module_t *) mod;
va_list args;
int err;
err = drm_init(dmod, 0);
if (err)
return err;
va_start(args, op);
switch (op) {
case GRALLOC_MODULE_PERFORM_GET_DRM_FD:
{
int *fd = va_arg(args, int *);
*fd = gralloc_drm_get_fd(dmod->drm);
err = 0;
}
break;
/* should we remove this and next ops, and make it transparent? */
case GRALLOC_MODULE_PERFORM_GET_DRM_MAGIC:
{
int32_t *magic = va_arg(args, int32_t *);
err = gralloc_drm_get_magic(dmod->drm, magic);
}
break;
case GRALLOC_MODULE_PERFORM_AUTH_DRM_MAGIC:
{
int32_t magic = va_arg(args, int32_t);
err = gralloc_drm_auth_magic(dmod->drm, magic);
}
break;
case GRALLOC_MODULE_PERFORM_ENTER_VT:
{
err = gralloc_drm_set_master(dmod->drm);
}
break;
case GRALLOC_MODULE_PERFORM_LEAVE_VT:
{
gralloc_drm_drop_master(dmod->drm);
err = 0;
}
break;
default:
err = -EINVAL;
break;
}
va_end(args);
return err;
}
static int drm_mod_register_buffer(const gralloc_module_t *mod,
buffer_handle_t handle)
{
struct drm_module_t *dmod = (struct drm_module_t *) mod;
int err;
err = drm_init(dmod, 0);
if (err)
return err;
return gralloc_drm_handle_register(handle, dmod->drm);
}
static int drm_mod_unregister_buffer(const gralloc_module_t *mod,
buffer_handle_t handle)
{
return gralloc_drm_handle_unregister(handle);
}
static int drm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle,
int usage, int x, int y, int w, int h, void **ptr)
{
struct gralloc_drm_bo_t *bo;
int err;
bo = gralloc_drm_bo_from_handle(handle);
if (!bo)
return -EINVAL;
return gralloc_drm_bo_lock(bo, usage, x, y, w, h, ptr);
}
static int drm_mod_unlock(const gralloc_module_t *mod, buffer_handle_t handle)
{
struct drm_module_t *dmod = (struct drm_module_t *) mod;
struct gralloc_drm_bo_t *bo;
bo = gralloc_drm_bo_from_handle(handle);
if (!bo)
return -EINVAL;
gralloc_drm_bo_unlock(bo);
return 0;
}
static int drm_mod_close_gpu0(struct hw_device_t *dev)
{
struct alloc_device_t *alloc = (struct alloc_device_t *) dev;
free(alloc);
return 0;
}
static int drm_mod_free_gpu0(alloc_device_t *dev, buffer_handle_t handle)
{
struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
struct gralloc_drm_bo_t *bo;
bo = gralloc_drm_bo_from_handle(handle);
if (!bo)
return -EINVAL;
gralloc_drm_bo_decref(bo);
return 0;
}
static int drm_mod_alloc_gpu0(alloc_device_t *dev,
int w, int h, int format, int usage,
buffer_handle_t *handle, int *stride)
{
struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
struct gralloc_drm_bo_t *bo;
int size, bpp, err;
bpp = gralloc_drm_get_bpp(format);
if (!bpp)
return -EINVAL;
bo = gralloc_drm_bo_create(dmod->drm, w, h, format, usage);
if (!bo)
return -ENOMEM;
if (gralloc_drm_bo_need_fb(bo)) {
err = gralloc_drm_bo_add_fb(bo);
if (err) {
ALOGE("failed to add fb");
gralloc_drm_bo_decref(bo);
return err;
}
}
*handle = gralloc_drm_bo_get_handle(bo, stride);
/* in pixels */
*stride /= bpp;
return 0;
}
static int drm_mod_open_gpu0(struct drm_module_t *dmod, hw_device_t **dev)
{
struct alloc_device_t *alloc;
int err;
err = drm_init(dmod, 0);
if (err)
return err;
alloc = calloc(1, sizeof(*alloc));
if (!alloc)
return -EINVAL;
alloc->common.tag = HARDWARE_DEVICE_TAG;
alloc->common.version = 0;
alloc->common.module = &dmod->base.common;
alloc->common.close = drm_mod_close_gpu0;
alloc->alloc = drm_mod_alloc_gpu0;
alloc->free = drm_mod_free_gpu0;
*dev = &alloc->common;
return 0;
}
static int drm_mod_close_fb0(struct hw_device_t *dev)
{
struct framebuffer_device_t *fb = (struct framebuffer_device_t *) dev;
free(fb);
return 0;
}
static int drm_mod_set_swap_interval_fb0(struct framebuffer_device_t *fb,
int interval)
{
if (interval < fb->minSwapInterval || interval > fb->maxSwapInterval)
return -EINVAL;
return 0;
}
static int drm_mod_post_fb0(struct framebuffer_device_t *fb,
buffer_handle_t handle)
{
struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
struct gralloc_drm_bo_t *bo;
bo = gralloc_drm_bo_from_handle(handle);
if (!bo)
return -EINVAL;
return gralloc_drm_bo_post(bo);
}
#include <GLES/gl.h>
static int drm_mod_composition_complete_fb0(struct framebuffer_device_t *fb)
{
struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
if (gralloc_drm_is_kms_pipelined(dmod->drm))
glFlush();
else
glFinish();
return 0;
}
static int drm_mod_open_fb0(struct drm_module_t *dmod, struct hw_device_t **dev)
{
struct framebuffer_device_t *fb;
int err;
err = drm_init(dmod, 1);
if (err)
return err;
fb = calloc(1, sizeof(*fb));
if (!fb)
return -ENOMEM;
fb->common.tag = HARDWARE_DEVICE_TAG;
fb->common.version = 0;
fb->common.module = &dmod->base.common;
fb->common.close = drm_mod_close_fb0;
fb->setSwapInterval = drm_mod_set_swap_interval_fb0;
fb->post = drm_mod_post_fb0;
fb->compositionComplete = drm_mod_composition_complete_fb0;
gralloc_drm_get_kms_info(dmod->drm, fb);
*dev = &fb->common;
ALOGI("mode.hdisplay %d\n"
"mode.vdisplay %d\n"
"mode.vrefresh %f\n"
"format 0x%x\n"
"xdpi %f\n"
"ydpi %f\n",
fb->width,
fb->height,
fb->fps,
fb->format,
fb->xdpi, fb->ydpi);
return 0;
}
static int drm_mod_open(const struct hw_module_t *mod,
const char *name, struct hw_device_t **dev)
{
struct drm_module_t *dmod = (struct drm_module_t *) mod;
int err;
if (strcmp(name, GRALLOC_HARDWARE_GPU0) == 0)
err = drm_mod_open_gpu0(dmod, dev);
else if (strcmp(name, GRALLOC_HARDWARE_FB0) == 0)
err = drm_mod_open_fb0(dmod, dev);
else
err = -EINVAL;
return err;
}
static struct hw_module_methods_t drm_mod_methods = {
.open = drm_mod_open
};
struct drm_module_t HAL_MODULE_INFO_SYM = {
.base = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = GRALLOC_HARDWARE_MODULE_ID,
.name = "DRM Memory Allocator",
.author = "Chia-I Wu",
.methods = &drm_mod_methods
},
.registerBuffer = drm_mod_register_buffer,
.unregisterBuffer = drm_mod_unregister_buffer,
.lock = drm_mod_lock,
.unlock = drm_mod_unlock,
.perform = drm_mod_perform
},
.hwc_reserve_plane = gralloc_drm_reserve_plane,
.hwc_disable_planes = gralloc_drm_disable_planes,
.hwc_set_plane_handle = gralloc_drm_set_plane_handle,
.mutex = PTHREAD_MUTEX_INITIALIZER,
.drm = NULL
};