blob: 9475d827d7d65ab4587a5d41233825e9669d8980 [file] [log] [blame]
John Stultz18814f62018-02-22 16:02:49 -08001/*
2 * Copyright (C) 2016 ARM Limited. All rights reserved.
3 *
4 * Copyright (C) 2008 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#include <hardware/hardware.h>
19#include <hardware/gralloc1.h>
20
21#include "mali_gralloc_module.h"
22
23#include "mali_gralloc_private_interface.h"
24#include "mali_gralloc_buffer.h"
25#include "mali_gralloc_ion.h"
26#include "mali_gralloc_bufferdescriptor.h"
27#include "mali_gralloc_bufferallocation.h"
28#include "mali_gralloc_reference.h"
29#include "mali_gralloc_bufferaccess.h"
30#include "framebuffer_device.h"
31#include "gralloc_buffer_priv.h"
32#include "mali_gralloc_debug.h"
33
34typedef struct mali_gralloc_func
35{
36 gralloc1_function_descriptor_t desc;
37 gralloc1_function_pointer_t func;
38} mali_gralloc_func;
39
40static void mali_gralloc_dump(gralloc1_device_t *device, uint32_t *outSize, char *outBuffer)
41{
42 if (NULL == outSize)
43 {
44 ALOGE("Invalid pointer to outSize and return");
45 return;
46 }
47
48 mali_gralloc_dump_internal(outSize, outBuffer);
49 GRALLOC_UNUSED(device);
50}
51
52static int32_t mali_gralloc_create_descriptor(gralloc1_device_t *device, gralloc1_buffer_descriptor_t *outDescriptor)
53{
54 int ret = 0;
55 ret = mali_gralloc_create_descriptor_internal(outDescriptor);
56 GRALLOC_UNUSED(device);
57 return ret;
58}
59
60static int32_t mali_gralloc_destroy_descriptor(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor)
61{
62 int ret = 0;
63 ret = mali_gralloc_destroy_descriptor_internal(descriptor);
64 GRALLOC_UNUSED(device);
65 return ret;
66}
67
68static int32_t mali_gralloc_set_consumer_usage(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
69 /*uint64_t */ gralloc1_consumer_usage_t usage)
70{
71 int ret = 0;
72 ret = mali_gralloc_set_consumerusage_internal(descriptor, usage);
73 GRALLOC_UNUSED(device);
74 return ret;
75}
76
77static int32_t mali_gralloc_set_dimensions(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
78 uint32_t width, uint32_t height)
79{
80 int ret = 0;
81 ret = mali_gralloc_set_dimensions_internal(descriptor, width, height);
82 GRALLOC_UNUSED(device);
83 return ret;
84}
85
86static int32_t mali_gralloc_set_format(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
87 /*int32_t*/ android_pixel_format_t format)
88{
89 int ret = 0;
90 ret = mali_gralloc_set_format_internal(descriptor, format);
91 GRALLOC_UNUSED(device);
92 return ret;
93}
94
95static int32_t mali_gralloc_set_producer_usage(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
96 /*uint64_t */ gralloc1_producer_usage_t usage)
97{
98 int ret = 0;
99 ret = mali_gralloc_set_producerusage_internal(descriptor, usage);
100 GRALLOC_UNUSED(device);
101 return ret;
102}
103
104static int32_t mali_gralloc_get_backing_store(gralloc1_device_t *device, buffer_handle_t buffer,
105 gralloc1_backing_store_t *outStore)
106{
107 int ret = 0;
108 ret = mali_gralloc_get_backing_store_internal(buffer, outStore);
109 GRALLOC_UNUSED(device);
110 return ret;
111}
112
113static int32_t mali_gralloc_get_consumer_usage(gralloc1_device_t *device, buffer_handle_t buffer,
114 uint64_t * /*gralloc1_consumer_usage_t*/ outUsage)
115{
116 int ret = 0;
117 ret = mali_gralloc_get_consumer_usage_internal(buffer, outUsage);
118 GRALLOC_UNUSED(device);
119 return ret;
120}
121
122static int32_t mali_gralloc_get_dimensions(gralloc1_device_t *device, buffer_handle_t buffer, uint32_t *outWidth,
123 uint32_t *outHeight)
124{
125 int ret = 0;
126 ret = mali_gralloc_get_dimensions_internal(buffer, outWidth, outHeight);
127 GRALLOC_UNUSED(device);
128 return ret;
129}
130
131static int32_t mali_gralloc_get_format(gralloc1_device_t *device, buffer_handle_t buffer, int32_t *outFormat)
132{
133 int ret = 0;
134 ret = mali_gralloc_get_format_internal(buffer, outFormat);
135 GRALLOC_UNUSED(device);
136 return ret;
137}
138
139static int32_t mali_gralloc_get_producer_usage(gralloc1_device_t *device, buffer_handle_t buffer,
140 uint64_t * /*gralloc1_producer_usage_t*/ outUsage)
141{
142 int ret = 0;
143 ret = mali_gralloc_get_producer_usage_internal(buffer, outUsage);
144 GRALLOC_UNUSED(device);
145 return ret;
146}
147
148static int32_t mali_gralloc_get_stride(gralloc1_device_t *device, buffer_handle_t buffer, uint32_t *outStride)
149{
150 GRALLOC_UNUSED(device);
151
152 int stride;
153
154 if (mali_gralloc_query_getstride(buffer, &stride) < 0)
155 {
156 return GRALLOC1_ERROR_UNSUPPORTED;
157 }
158
159 *outStride = (uint32_t)stride;
160
161 return GRALLOC1_ERROR_NONE;
162}
163
164static int32_t mali_gralloc_allocate(gralloc1_device_t *device, uint32_t numDescriptors,
165 const gralloc1_buffer_descriptor_t *descriptors, buffer_handle_t *outBuffers)
166{
167 mali_gralloc_module *m;
168 m = reinterpret_cast<private_module_t *>(device->common.module);
169 buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(*descriptors);
170 uint64_t usage;
171 bool shared = false;
172
173 usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage;
174
175#if DISABLE_FRAMEBUFFER_HAL != 1
176
177 if (usage & GRALLOC_USAGE_HW_FB)
178 {
179 int byte_stride;
180 int pixel_stride;
181 int width, height;
182 uint64_t format;
183
184 format = bufDescriptor->hal_format;
185 width = bufDescriptor->width;
186 height = bufDescriptor->height;
187
188#if GRALLOC_FB_SWAP_RED_BLUE == 1
189#ifdef GRALLOC_16_BITS
190 format = HAL_PIXEL_FORMAT_RGB_565;
191#else
192 format = HAL_PIXEL_FORMAT_BGRA_8888;
193#endif
194#endif
195
196 if (fb_alloc_framebuffer(m, bufDescriptor->consumer_usage, bufDescriptor->producer_usage, outBuffers,
197 &pixel_stride, &byte_stride) < 0)
198 {
199 return GRALLOC1_ERROR_NO_RESOURCES;
200 }
201 else
202 {
203 private_handle_t *hnd = (private_handle_t *)*outBuffers;
204
205 /* Allocate a meta-data buffer for framebuffer too. fbhal
206 * ones wont need it but for hwc they will.
207 *
208 * Explicitly ignore allocation errors since it is not critical to have
209 */
210 (void)gralloc_buffer_attr_allocate(hnd);
211
212 hnd->req_format = format;
213 hnd->yuv_info = MALI_YUV_BT601_NARROW;
214 hnd->internal_format = format;
215 hnd->byte_stride = byte_stride;
216 hnd->width = width;
217 hnd->height = height;
218 hnd->stride = pixel_stride;
219 hnd->internalWidth = width;
220 hnd->internalHeight = height;
221 }
222 }
223 else
224#endif
225 {
226 if (mali_gralloc_buffer_allocate(m, (gralloc_buffer_descriptor_t *)descriptors, numDescriptors, outBuffers,
227 &shared) < 0)
228 {
229 ALOGE("Failed to allocate buffer.");
230 return GRALLOC1_ERROR_NO_RESOURCES;
231 }
232
233 if (!shared && 1 != numDescriptors)
234 {
235 return GRALLOC1_ERROR_NOT_SHARED;
236 }
237 }
238
239 return GRALLOC1_ERROR_NONE;
240}
241
242static int32_t mali_gralloc_retain(gralloc1_device_t *device, buffer_handle_t buffer)
243{
244 mali_gralloc_module *m;
245 m = reinterpret_cast<private_module_t *>(device->common.module);
246
247 if (private_handle_t::validate(buffer) < 0)
248 {
249 return GRALLOC1_ERROR_BAD_HANDLE;
250 }
251
252 if (mali_gralloc_reference_retain(m, buffer) < 0)
253 {
254 return GRALLOC1_ERROR_NO_RESOURCES;
255 }
256
257 return GRALLOC1_ERROR_NONE;
258}
259
260static int32_t mali_gralloc_release(gralloc1_device_t *device, buffer_handle_t buffer)
261{
262 mali_gralloc_module *m;
263 m = reinterpret_cast<private_module_t *>(device->common.module);
264
265 if (mali_gralloc_reference_release(m, buffer, true) < 0)
266 {
267 return GRALLOC1_ERROR_BAD_HANDLE;
268 }
269
270 return GRALLOC1_ERROR_NONE;
271}
272
273static int32_t mali_gralloc1_get_num_flex_planes(gralloc1_device_t *device, buffer_handle_t buffer,
274 uint32_t *outNumPlanes)
275{
276 mali_gralloc_module *m;
277 m = reinterpret_cast<private_module_t *>(device->common.module);
278
279 if (private_handle_t::validate(buffer) < 0)
280 {
281 return GRALLOC1_ERROR_BAD_HANDLE;
282 }
283
284 if (mali_gralloc_get_num_flex_planes(m, buffer, outNumPlanes) < 0)
285 {
286 return GRALLOC1_ERROR_UNSUPPORTED;
287 }
288
289 return GRALLOC1_ERROR_NONE;
290}
291
292static int32_t mali_gralloc1_lock_async(gralloc1_device_t *device, buffer_handle_t buffer,
293 uint64_t /*gralloc1_producer_usage_t*/ producerUsage,
294 uint64_t /*gralloc1_consumer_usage_t*/ consumerUsage,
295 const gralloc1_rect_t *accessRegion, void **outData, int32_t acquireFence)
296{
297 mali_gralloc_module *m;
298 m = reinterpret_cast<private_module_t *>(device->common.module);
299
300 if (private_handle_t::validate(buffer) < 0)
301 {
302 return GRALLOC1_ERROR_BAD_HANDLE;
303 }
304
305 if (!((producerUsage | consumerUsage) & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)))
306 {
307 return GRALLOC1_ERROR_BAD_VALUE;
308 }
309
310 if (mali_gralloc_lock_async(m, buffer, producerUsage | consumerUsage, accessRegion->left, accessRegion->top,
311 accessRegion->width, accessRegion->height, outData, acquireFence) < 0)
312 {
313 return GRALLOC1_ERROR_UNSUPPORTED;
314 }
315
316 return GRALLOC1_ERROR_NONE;
317}
318
319static int32_t mali_gralloc1_lock_flex_async(gralloc1_device_t *device, buffer_handle_t buffer,
320 uint64_t /*gralloc1_producer_usage_t*/ producerUsage,
321 uint64_t /*gralloc1_consumer_usage_t*/ consumerUsage,
322 const gralloc1_rect_t *accessRegion,
323 struct android_flex_layout *outFlexLayout, int32_t acquireFence)
324{
325 mali_gralloc_module *m;
326 m = reinterpret_cast<private_module_t *>(device->common.module);
327
328 if (private_handle_t::validate(buffer) < 0)
329 {
330 return GRALLOC1_ERROR_BAD_HANDLE;
331 }
332
333 if (!((producerUsage | consumerUsage) & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)))
334 {
335 return GRALLOC1_ERROR_BAD_VALUE;
336 }
337
338 if (mali_gralloc_lock_flex_async(m, buffer, producerUsage | consumerUsage, accessRegion->left, accessRegion->top,
339 accessRegion->width, accessRegion->height, outFlexLayout, acquireFence) < 0)
340 {
341 return GRALLOC1_ERROR_UNSUPPORTED;
342 }
343
344 return GRALLOC1_ERROR_NONE;
345}
346
347static int32_t mali_gralloc1_unlock_async(gralloc1_device_t *device, buffer_handle_t buffer, int32_t *outReleaseFence)
348{
349 mali_gralloc_module *m;
350 m = reinterpret_cast<private_module_t *>(device->common.module);
351
352 if (private_handle_t::validate(buffer) < 0)
353 {
354 return GRALLOC1_ERROR_BAD_HANDLE;
355 }
356
357 mali_gralloc_unlock_async(m, buffer, outReleaseFence);
358 return GRALLOC1_ERROR_NONE;
359}
360
361static const mali_gralloc_func mali_gralloc_func_list[] = {
362 { GRALLOC1_FUNCTION_DUMP, (gralloc1_function_pointer_t)mali_gralloc_dump },
363 { GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, (gralloc1_function_pointer_t)mali_gralloc_create_descriptor },
364 { GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR, (gralloc1_function_pointer_t)mali_gralloc_destroy_descriptor },
365 { GRALLOC1_FUNCTION_SET_CONSUMER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_set_consumer_usage },
366 { GRALLOC1_FUNCTION_SET_DIMENSIONS, (gralloc1_function_pointer_t)mali_gralloc_set_dimensions },
367 { GRALLOC1_FUNCTION_SET_FORMAT, (gralloc1_function_pointer_t)mali_gralloc_set_format },
368 { GRALLOC1_FUNCTION_SET_PRODUCER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_set_producer_usage },
369 { GRALLOC1_FUNCTION_GET_BACKING_STORE, (gralloc1_function_pointer_t)mali_gralloc_get_backing_store },
370 { GRALLOC1_FUNCTION_GET_CONSUMER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_get_consumer_usage },
371 { GRALLOC1_FUNCTION_GET_DIMENSIONS, (gralloc1_function_pointer_t)mali_gralloc_get_dimensions },
372 { GRALLOC1_FUNCTION_GET_FORMAT, (gralloc1_function_pointer_t)mali_gralloc_get_format },
373 { GRALLOC1_FUNCTION_GET_PRODUCER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_get_producer_usage },
374 { GRALLOC1_FUNCTION_GET_STRIDE, (gralloc1_function_pointer_t)mali_gralloc_get_stride },
375 { GRALLOC1_FUNCTION_ALLOCATE, (gralloc1_function_pointer_t)mali_gralloc_allocate },
376 { GRALLOC1_FUNCTION_RETAIN, (gralloc1_function_pointer_t)mali_gralloc_retain },
377 { GRALLOC1_FUNCTION_RELEASE, (gralloc1_function_pointer_t)mali_gralloc_release },
378 { GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES, (gralloc1_function_pointer_t)mali_gralloc1_get_num_flex_planes },
379 { GRALLOC1_FUNCTION_LOCK, (gralloc1_function_pointer_t)mali_gralloc1_lock_async },
380 { GRALLOC1_FUNCTION_LOCK_FLEX, (gralloc1_function_pointer_t)mali_gralloc1_lock_flex_async },
381 { GRALLOC1_FUNCTION_UNLOCK, (gralloc1_function_pointer_t)mali_gralloc1_unlock_async },
382
383 /* GRALLOC1_FUNCTION_INVALID has to be the last descriptor on the list. */
384 { GRALLOC1_FUNCTION_INVALID, NULL }
385};
386
387static void mali_gralloc_getCapabilities(gralloc1_device_t *dev, uint32_t *outCount, int32_t *outCapabilities)
388{
389 GRALLOC_UNUSED(dev);
390 GRALLOC_UNUSED(outCapabilities);
391
392 if (outCount != NULL)
393 {
394 *outCount = 0;
395 }
396}
397
398static gralloc1_function_pointer_t mali_gralloc_getFunction(gralloc1_device_t *dev, int32_t descriptor)
399{
400 GRALLOC_UNUSED(dev);
401 gralloc1_function_pointer_t rval = NULL;
402 uint32_t pos = 0;
403
404 while (mali_gralloc_func_list[pos].desc != GRALLOC1_FUNCTION_INVALID)
405 {
406 if (mali_gralloc_func_list[pos].desc == descriptor)
407 {
408 rval = mali_gralloc_func_list[pos].func;
409 break;
410 }
411
412 pos++;
413 }
414
415 if (rval == NULL)
416 {
417 rval = mali_gralloc_private_interface_getFunction(descriptor);
418 }
419
420 return rval;
421}
422
423int mali_gralloc_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
424{
425 gralloc1_device_t *dev;
426
427 GRALLOC_UNUSED(name);
428
429 dev = new gralloc1_device_t;
430
431 if (NULL == dev)
432 {
433 return -1;
434 }
435
436 /* initialize our state here */
437 memset(dev, 0, sizeof(*dev));
438
439 /* initialize the procs */
440 dev->common.tag = HARDWARE_DEVICE_TAG;
441 dev->common.version = 0;
442 dev->common.module = const_cast<hw_module_t *>(module);
443 dev->common.close = mali_gralloc_ion_device_close;
444
445 dev->getCapabilities = mali_gralloc_getCapabilities;
446 dev->getFunction = mali_gralloc_getFunction;
447
448 *device = &dev->common;
449
450 return 0;
451}