-
Notifications
You must be signed in to change notification settings - Fork 24
/
gralloc.cpp
281 lines (229 loc) · 6.34 KB
/
gralloc.cpp
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
/*
* 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 <string.h>
#include <pthread.h>
#include <errno.h>
#include "gralloc_drm.h"
#include "gralloc_drm_priv.h"
/*
* Initialize the DRM device object
*/
static int drm_init(struct drm_module_t *dmod)
{
int err = 0;
pthread_mutex_lock(&dmod->mutex);
if (!dmod->drm) {
dmod->drm = gralloc_drm_create();
if (!dmod->drm)
err = -EINVAL;
}
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);
if (err)
return err;
va_start(args, op);
switch (op) {
case static_cast<int>(GRALLOC_MODULE_PERFORM_GET_DRM_FD):
{
int *fd = va_arg(args, int *);
*fd = gralloc_drm_get_fd(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);
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_lock_ycbcr(const gralloc_module_t *mod, buffer_handle_t bhandle,
int usage, int x, int y, int w, int h, struct android_ycbcr *ycbcr)
{
struct gralloc_drm_handle_t *handle;
struct gralloc_drm_bo_t *bo;
void *ptr;
int err;
bo = gralloc_drm_bo_from_handle(bhandle);
if (!bo)
return -EINVAL;
handle = bo->handle;
switch(handle->format) {
case HAL_PIXEL_FORMAT_YCbCr_420_888:
break;
default:
return -EINVAL;
}
err = gralloc_drm_bo_lock(bo, usage, x, y, w, h, &ptr);
if (err)
return err;
switch(handle->format) {
case HAL_PIXEL_FORMAT_YCbCr_420_888:
ycbcr->y = ptr;
ycbcr->cb = (uint8_t *)ptr + handle->stride * handle->height;
ycbcr->cr = (uint8_t *)ycbcr->cb + 1;
ycbcr->ystride = handle->stride;
ycbcr->cstride = handle->stride;
ycbcr->chroma_step = 2;
break;
default:
break;
}
return 0;
}
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 drm_module_t *dmod = (struct drm_module_t *)dev->module;
struct alloc_device_t *alloc = (struct alloc_device_t *) dev;
gralloc_drm_destroy(dmod->drm);
delete 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;
*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);
if (err)
return err;
alloc = new alloc_device_t;
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_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
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,
.lock_ycbcr = drm_mod_lock_ycbcr,
},
.mutex = PTHREAD_MUTEX_INITIALIZER,
.drm = NULL
};