-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNativeGralloc.cpp
More file actions
324 lines (280 loc) · 12.8 KB
/
NativeGralloc.cpp
File metadata and controls
324 lines (280 loc) · 12.8 KB
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
/* Copyright (C) 2020 KAI OS TECHNOLOGIES (HONG KONG) LIMITED. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <GrallocUsageConversion.h>
#include <hardware/fb.h>
#include <hardware/gralloc.h>
#include <hardware/gralloc1.h>
#include <hardware/hardware.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "NativeGralloc.h"
/* static variables */
static int version = -1;
static hw_module_t *gralloc_hardware_module = NULL;
static framebuffer_device_t *framebuffer_device = NULL;
static gralloc_module_t *gralloc0_module;
static alloc_device_t *gralloc0_alloc;
static gralloc1_device_t *gralloc1_device = NULL;
static int gralloc1_release_implies_delete = 0;
static GRALLOC1_PFN_CREATE_DESCRIPTOR gralloc1_create_descriptor = NULL;
static GRALLOC1_PFN_DESTROY_DESCRIPTOR gralloc1_destroy_descriptor = NULL;
static GRALLOC1_PFN_SET_CONSUMER_USAGE gralloc1_set_consumer_usage = NULL;
static GRALLOC1_PFN_SET_DIMENSIONS gralloc1_set_dimensions = NULL;
static GRALLOC1_PFN_SET_FORMAT gralloc1_set_format = NULL;
static GRALLOC1_PFN_SET_PRODUCER_USAGE gralloc1_set_producer_usage = NULL;
static GRALLOC1_PFN_GET_BACKING_STORE gralloc1_get_backing_store = NULL;
static GRALLOC1_PFN_GET_CONSUMER_USAGE gralloc1_get_consumer_usage = NULL;
static GRALLOC1_PFN_GET_DIMENSIONS gralloc1_get_dimensions = NULL;
static GRALLOC1_PFN_GET_FORMAT gralloc1_get_format = NULL;
static GRALLOC1_PFN_GET_PRODUCER_USAGE gralloc1_get_producer_usage = NULL;
static GRALLOC1_PFN_GET_STRIDE gralloc1_get_stride = NULL;
static GRALLOC1_PFN_ALLOCATE gralloc1_allocate = NULL;
static GRALLOC1_PFN_RETAIN gralloc1_retain = NULL;
static GRALLOC1_PFN_RELEASE gralloc1_release = NULL;
static GRALLOC1_PFN_GET_NUM_FLEX_PLANES gralloc1_get_num_flex_planes = NULL;
static GRALLOC1_PFN_LOCK gralloc1_lock = NULL;
static GRALLOC1_PFN_LOCK_FLEX gralloc1_lock_flex = NULL;
static GRALLOC1_PFN_UNLOCK gralloc1_unlock = NULL;
static GRALLOC1_PFN_SET_LAYER_COUNT gralloc1_set_layer_count = NULL;
static GRALLOC1_PFN_GET_LAYER_COUNT gralloc1_get_layer_count = NULL;
static void gralloc1_init(void);
void native_gralloc_deinitialize(void);
void native_gralloc_initialize(int framebuffer)
{
if (version == -1) {
if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
(const struct hw_module_t **)&gralloc_hardware_module) == 0) {
if ((gralloc1_open(gralloc_hardware_module, &gralloc1_device) == 0)
&& (gralloc1_device != NULL)) {
// success
ALOGI("%s,gralloc1_open success, l:%d",__func__, __LINE__);
gralloc1_init();
version = 1;
atexit(native_gralloc_deinitialize);
} else {
if (framebuffer) {
if (framebuffer_open(gralloc_hardware_module,
&framebuffer_device) == 0) {
if ((gralloc_open(gralloc_hardware_module,
&gralloc0_alloc) == 0) && gralloc0_alloc != NULL) {
// success
version = 0;
ALOGI("gralloc_open success.l:%d", __LINE__);
gralloc0_module = reinterpret_cast<gralloc_module_t *>(gralloc_hardware_module);
atexit(native_gralloc_deinitialize);
} else {
ALOGI("failed to open the gralloc 0 module");
assert(NULL);
}
} else {
ALOGI("failed to open the framebuffer module");
assert(NULL);
}
} else if ((gralloc_open(gralloc_hardware_module,
&gralloc0_alloc) == 0) && gralloc0_alloc != NULL) {
// success
version = 0;
gralloc0_module = reinterpret_cast<gralloc_module_t *>(gralloc_hardware_module);
ALOGI("gralloc_open . l:%d", __LINE__);
atexit(native_gralloc_deinitialize);
} else {
// fail
framebuffer_device = NULL;
gralloc1_device = NULL;
version = -2;
ALOGI("failed to open gralloc module with both version 0 and 1"
" methods. l:%d", __LINE__);
native_gralloc_deinitialize();
assert(NULL);
}
}
} else {
ALOGI("failed to find/load gralloc module. l:%d",__LINE__);
assert(NULL);
}
} else {
// shouldn't reach here.
assert(NULL);
}
}
void native_gralloc_deinitialize(void)
{
ALOGI("%s,deinitialize l:%d",__func__, __LINE__);
if (framebuffer_device) framebuffer_close(framebuffer_device);
framebuffer_device = NULL;
if (gralloc0_alloc) gralloc_close(gralloc0_alloc);
gralloc0_alloc = NULL;
if (gralloc1_device) gralloc1_close(gralloc1_device);
gralloc1_device = NULL;
if (gralloc_hardware_module) dlclose(gralloc_hardware_module->dso);
gralloc_hardware_module = NULL;
}
static void gralloc1_init(void)
{
uint32_t count = 0;
gralloc1_device->getCapabilities(gralloc1_device, &count, NULL);
if (count >= 1) {
uint32_t i;
int32_t *gralloc1_capabilities = (int32_t*)malloc(sizeof(int32_t) * count);
gralloc1_device->getCapabilities(gralloc1_device, &count, gralloc1_capabilities);
// currently the only one that affects us/interests us is release imply delete.
for (i = 0; i < count; i++) {
if (gralloc1_capabilities[i] == GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE) {
gralloc1_release_implies_delete = 1;
}
}
free(gralloc1_capabilities);
}
gralloc1_create_descriptor = (GRALLOC1_PFN_CREATE_DESCRIPTOR)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_CREATE_DESCRIPTOR);
gralloc1_destroy_descriptor = (GRALLOC1_PFN_DESTROY_DESCRIPTOR)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR);
gralloc1_set_consumer_usage = (GRALLOC1_PFN_SET_CONSUMER_USAGE)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_SET_CONSUMER_USAGE);
gralloc1_set_dimensions = (GRALLOC1_PFN_SET_DIMENSIONS)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_SET_DIMENSIONS);
gralloc1_set_format = (GRALLOC1_PFN_SET_FORMAT)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_SET_FORMAT);
gralloc1_set_producer_usage = (GRALLOC1_PFN_SET_PRODUCER_USAGE)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_SET_PRODUCER_USAGE);
gralloc1_get_backing_store = (GRALLOC1_PFN_GET_BACKING_STORE)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_GET_BACKING_STORE);
gralloc1_get_consumer_usage = (GRALLOC1_PFN_GET_CONSUMER_USAGE)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_GET_CONSUMER_USAGE);
gralloc1_get_dimensions = (GRALLOC1_PFN_GET_DIMENSIONS)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_GET_DIMENSIONS);
gralloc1_get_format = (GRALLOC1_PFN_GET_FORMAT)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_GET_FORMAT);
gralloc1_get_producer_usage = (GRALLOC1_PFN_GET_PRODUCER_USAGE)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_GET_PRODUCER_USAGE);
gralloc1_get_stride = (GRALLOC1_PFN_GET_STRIDE)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_GET_STRIDE);
gralloc1_allocate = (GRALLOC1_PFN_ALLOCATE)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_ALLOCATE);
gralloc1_retain = (GRALLOC1_PFN_RETAIN)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_RETAIN);
gralloc1_release = (GRALLOC1_PFN_RELEASE)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_RELEASE);
gralloc1_get_num_flex_planes = (GRALLOC1_PFN_GET_NUM_FLEX_PLANES)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES);
gralloc1_lock = (GRALLOC1_PFN_LOCK)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_LOCK);
gralloc1_lock_flex = (GRALLOC1_PFN_LOCK_FLEX)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_LOCK_FLEX);
gralloc1_unlock = (GRALLOC1_PFN_UNLOCK)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_UNLOCK);
gralloc1_set_layer_count = (GRALLOC1_PFN_SET_LAYER_COUNT)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_SET_LAYER_COUNT);
gralloc1_get_layer_count = (GRALLOC1_PFN_GET_LAYER_COUNT)gralloc1_device->getFunction(
gralloc1_device, GRALLOC1_FUNCTION_GET_LAYER_COUNT);
}
int native_gralloc_release(buffer_handle_t handle, int was_allocated)
{
int ret = -ENOSYS;
if GRALLOC1(
ret = gralloc1_release(gralloc1_device, handle);
// this needs to happen if the last reference is gone, this function is
// only called in such cases.
if (!gralloc1_release_implies_delete) {
native_handle_close((native_handle_t*)handle);
native_handle_delete((native_handle_t*)handle);
}
) else if GRALLOC0(
if (was_allocated) {
ret = gralloc0_alloc->free(gralloc0_alloc, handle);
} else {
ret = gralloc0_module->unregisterBuffer(gralloc0_module, handle);
// this needs to happen if the last reference is gone, this function is
// only called in such cases.
native_handle_close((native_handle_t*)handle);
native_handle_delete((native_handle_t*)handle);
}
) else NO_GRALLOC
return ret;
}
int native_gralloc_retain(buffer_handle_t handle)
{
int ret = -ENOSYS;
if GRALLOC1(
ret = gralloc1_retain(gralloc1_device, handle);
) else if GRALLOC0(
ret = gralloc0_module->registerBuffer(gralloc0_module, handle);
) else NO_GRALLOC
return ret;
}
int native_gralloc_allocate(int width, int height, int format, int usage,
buffer_handle_t *handle_ptr, uint32_t *stride_ptr)
{
int ret = -ENOSYS;
if GRALLOC1(
gralloc1_buffer_descriptor_t desc;
uint64_t producer_usage;
uint64_t consumer_usage;
android_convertGralloc0To1Usage(usage, &producer_usage, &consumer_usage);
// create temporary description (descriptor) of buffer to allocate
ret = gralloc1_create_descriptor(gralloc1_device, &desc);
ret |= gralloc1_set_dimensions(gralloc1_device, desc, width, height);
ret |= gralloc1_set_consumer_usage(gralloc1_device, desc, consumer_usage);
ret |= gralloc1_set_producer_usage(gralloc1_device, desc, producer_usage);
ret |= gralloc1_set_format(gralloc1_device, desc, format);
// actual allocation
ret |= gralloc1_allocate(gralloc1_device, 1, &desc, handle_ptr);
// get stride and release temporary descriptor
ret |= gralloc1_get_stride(gralloc1_device, *handle_ptr, stride_ptr);
ret |= gralloc1_destroy_descriptor(gralloc1_device, desc);
) else if GRALLOC0(
ret = gralloc0_alloc->alloc(gralloc0_alloc,
width, height, format, usage, handle_ptr, (int*)stride_ptr);
) else NO_GRALLOC
return ret;
}
int native_gralloc_lock(buffer_handle_t handle, int usage, int l,
int t, int w, int h, void **vaddr)
{
int ret = -ENOSYS;
if GRALLOC1(
uint64_t producer_usage;
uint64_t consumer_usage;
gralloc1_rect_t access_region;
access_region.left = l;
access_region.top = t;
access_region.width = w;
access_region.height = h;
android_convertGralloc0To1Usage(usage, &producer_usage,
&consumer_usage);
ret = gralloc1_lock(gralloc1_device, handle, producer_usage,
consumer_usage, &access_region, vaddr, -1);
) else if GRALLOC0(
ret = gralloc0_module->lock(gralloc0_module, handle, usage,
l, t, w, h, vaddr);
) else NO_GRALLOC
return ret;
}
int native_gralloc_unlock(buffer_handle_t handle)
{
int ret = -ENOSYS;
if GRALLOC1(
int releaseFence = 0;
ret = gralloc1_unlock(gralloc1_device, handle, &releaseFence);
close(releaseFence);
) else if GRALLOC0(
ret = gralloc0_module->unlock(gralloc0_module, handle);
) else NO_GRALLOC
return ret;
}