Home | History | Annotate | Download | only in hal
      1 /* Copyright (c) Imagination Technologies Ltd.
      2  *
      3  * The contents of this file are subject to the MIT license as set out below.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included in
     13  * all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     21  * THE SOFTWARE.
     22  */
     23 
     24 #ifndef IMG_GRALLOC1_H
     25 #define IMG_GRALLOC1_H
     26 
     27 #include <hardware/gralloc1.h>
     28 
     29 #include <stdlib.h>
     30 
     31 #define GRALLOC1_FUNCTION_IMG_EXT_OFF 1000
     32 
     33 enum
     34 {
     35 	GRALLOC1_FUNCTION_BLIT_HANDLE_TO_HANDLE_IMG =
     36 		(GRALLOC1_FUNCTION_IMG_EXT_OFF + GRALLOC_BLIT_HANDLE_TO_HANDLE_IMG),
     37 	GRALLOC1_FUNCTION_GET_BUFFER_CPU_ADDRESSES_IMG =
     38 		(GRALLOC1_FUNCTION_IMG_EXT_OFF + GRALLOC_GET_BUFFER_CPU_ADDRESSES_IMG),
     39 	GRALLOC1_FUNCTION_PUT_BUFFER_CPU_ADDRESSES_IMG =
     40 		(GRALLOC1_FUNCTION_IMG_EXT_OFF + GRALLOC_PUT_BUFFER_CPU_ADDRESSES_IMG),
     41 	GRALLOC1_FUNCTION_GET_DISPLAY_DEVICE_IMG =
     42 		(GRALLOC1_FUNCTION_IMG_EXT_OFF + GRALLOC_GET_DISPLAY_DEVICE_IMG),
     43 	GRALLOC1_FUNCTION_GET_DISPLAY_STATUS_IMG =
     44 		(GRALLOC1_FUNCTION_IMG_EXT_OFF + GRALLOC_GET_DISPLAY_STATUS_IMG),
     45 };
     46 
     47 static inline int gralloc1_register_img
     48 	(gralloc1_device_t *g, buffer_handle_t handle)
     49 {
     50 	GRALLOC1_PFN_RETAIN f =
     51 		(GRALLOC1_PFN_RETAIN)
     52 			g->getFunction(g, GRALLOC1_FUNCTION_RETAIN);
     53 	int32_t err;
     54 
     55 	err = f(g, handle);
     56 	switch (err)
     57 	{
     58 		case GRALLOC1_ERROR_NO_RESOURCES:
     59 			return -EAGAIN;
     60 		case GRALLOC1_ERROR_NONE:
     61 			return 0;
     62 		default:
     63 			return -EINVAL;
     64 	}
     65 }
     66 
     67 static inline int gralloc1_unregister_img
     68 	(gralloc1_device_t *g, buffer_handle_t handle)
     69 {
     70 	GRALLOC1_PFN_RELEASE f =
     71 		(GRALLOC1_PFN_RELEASE)
     72 			g->getFunction(g, GRALLOC1_FUNCTION_RELEASE);
     73 	int32_t err;
     74 
     75 	err = f(g, handle);
     76 	switch (err)
     77 	{
     78 		case GRALLOC1_ERROR_NONE:
     79 			return 0;
     80 		default:
     81 			return -EINVAL;
     82 	}
     83 }
     84 
     85 static inline int gralloc1_device_alloc_img
     86 	(gralloc1_device_t *d, int w, int h, int format, int usage,
     87 	 buffer_handle_t *handle, int *stride)
     88 {
     89 	GRALLOC1_PFN_ALLOCATE allocate =
     90 		(GRALLOC1_PFN_ALLOCATE)
     91 			d->getFunction(d, GRALLOC1_FUNCTION_ALLOCATE);
     92 	GRALLOC1_PFN_CREATE_DESCRIPTOR createDescriptor =
     93 		(GRALLOC1_PFN_CREATE_DESCRIPTOR)
     94 			d->getFunction(d, GRALLOC1_FUNCTION_CREATE_DESCRIPTOR);
     95 	GRALLOC1_PFN_DESTROY_DESCRIPTOR destroyDescriptor =
     96 		(GRALLOC1_PFN_DESTROY_DESCRIPTOR)
     97 			d->getFunction(d, GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR);
     98 	GRALLOC1_PFN_SET_CONSUMER_USAGE setConsumerUsage =
     99 		(GRALLOC1_PFN_SET_CONSUMER_USAGE)
    100 			d->getFunction(d, GRALLOC1_FUNCTION_SET_CONSUMER_USAGE);
    101 	GRALLOC1_PFN_SET_DIMENSIONS setDimensions =
    102 		(GRALLOC1_PFN_SET_DIMENSIONS)
    103 			d->getFunction(d, GRALLOC1_FUNCTION_SET_DIMENSIONS);
    104 	GRALLOC1_PFN_SET_FORMAT setFormat =
    105 		(GRALLOC1_PFN_SET_FORMAT)
    106 			d->getFunction(d, GRALLOC1_FUNCTION_SET_FORMAT);
    107 	GRALLOC1_PFN_SET_PRODUCER_USAGE setProducerUsage =
    108 		(GRALLOC1_PFN_SET_PRODUCER_USAGE)
    109 			d->getFunction(d, GRALLOC1_FUNCTION_SET_PRODUCER_USAGE);
    110 	GRALLOC1_PFN_GET_STRIDE getStride =
    111 		(GRALLOC1_PFN_GET_STRIDE)
    112 			d->getFunction(d, GRALLOC1_FUNCTION_GET_STRIDE);
    113 	uint64_t producerUsage =
    114 		(usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN    |
    115 		          GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN   |
    116 		          GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET |
    117 		          GRALLOC1_PRODUCER_USAGE_PROTECTED         |
    118 		          GRALLOC1_PRODUCER_USAGE_CAMERA            |
    119 		          GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER));
    120 	uint64_t consumerUsage =
    121 		(usage & (GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN    |
    122 		          GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE       |
    123 		          GRALLOC1_CONSUMER_USAGE_HWCOMPOSER        |
    124 		          GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET     |
    125 		          GRALLOC1_CONSUMER_USAGE_CURSOR            |
    126 		          GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER     |
    127 		          GRALLOC1_CONSUMER_USAGE_CAMERA            |
    128 		          GRALLOC1_CONSUMER_USAGE_RENDERSCRIPT));
    129 	gralloc1_buffer_descriptor_t descriptor;
    130 	uint32_t stride32;
    131 	int err = -EINVAL;
    132 	int32_t err32;
    133 
    134 	err32 = createDescriptor(d, &descriptor);
    135 	if (err32 != GRALLOC1_ERROR_NONE)
    136 		goto err_out;
    137 
    138 	err32 = setDimensions(d, descriptor, w, h);
    139 	if (err32 != GRALLOC1_ERROR_NONE)
    140 		goto err_destroy_descriptor;
    141 
    142 	err32 = setFormat(d, descriptor, format);
    143 	if (err32 != GRALLOC1_ERROR_NONE)
    144 		goto err_destroy_descriptor;
    145 
    146 	err32 = setConsumerUsage(d, descriptor, consumerUsage);
    147 	if (err32 != GRALLOC1_ERROR_NONE)
    148 		goto err_destroy_descriptor;
    149 
    150 	err32 = setProducerUsage(d, descriptor, producerUsage);
    151 	if (err32 != GRALLOC1_ERROR_NONE)
    152 		goto err_destroy_descriptor;
    153 
    154 	err32 = allocate(d, 1, &descriptor, handle);
    155 	switch (err32)
    156 	{
    157 		case GRALLOC1_ERROR_NOT_SHARED:
    158 		case GRALLOC1_ERROR_NONE:
    159 			break;
    160 		case GRALLOC1_ERROR_NO_RESOURCES:
    161 			err = -EAGAIN;
    162 		default:
    163 			goto err_destroy_descriptor;
    164 	}
    165 
    166 	err32 = getStride(d, *handle, &stride32);
    167 	if (err32 != GRALLOC1_ERROR_NONE)
    168 	{
    169 		gralloc1_unregister_img(d, *handle);
    170 		goto err_destroy_descriptor;
    171 	}
    172 
    173 	*stride = (int)stride32;
    174 	err = 0;
    175 err_destroy_descriptor:
    176 	destroyDescriptor(d, descriptor);
    177 err_out:
    178 	return err;
    179 }
    180 
    181 static inline int gralloc1_device_free_img
    182 	(gralloc1_device_t *d, buffer_handle_t handle)
    183 {
    184 	return gralloc1_unregister_img(d, handle);
    185 }
    186 
    187 static inline int gralloc1_lock_async_img
    188 	(gralloc1_device_t *g, buffer_handle_t handle, int usage,
    189 	 const gralloc1_rect_t *r, void **vaddr, int acquireFence)
    190 {
    191 	GRALLOC1_PFN_LOCK f =
    192 		(GRALLOC1_PFN_LOCK)
    193 			g->getFunction(g, GRALLOC1_FUNCTION_LOCK);
    194 	uint64_t producerUsage =
    195 		(usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN |
    196 		          GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN));
    197 	uint64_t consumerUsage =
    198 		(usage &  GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
    199 	int32_t err;
    200 
    201 	err = f(g, handle, producerUsage, consumerUsage, r, vaddr, acquireFence);
    202 	switch (err)
    203 	{
    204 		case GRALLOC1_ERROR_NONE:
    205 			return 0;
    206 		case GRALLOC1_ERROR_NO_RESOURCES:
    207 			return -EAGAIN;
    208 		default:
    209 			return -EINVAL;
    210 	}
    211 }
    212 
    213 static inline int gralloc1_unlock_async_img
    214 	(gralloc1_device_t *g, buffer_handle_t handle, int *releaseFence)
    215 {
    216 	GRALLOC1_PFN_UNLOCK f =
    217 		(GRALLOC1_PFN_UNLOCK)
    218 			g->getFunction(g, GRALLOC1_FUNCTION_UNLOCK);
    219 	int32_t err, releaseFence32;
    220 
    221 	err = f(g, handle, &releaseFence32);
    222 	switch (err)
    223 	{
    224 		case GRALLOC1_ERROR_NONE:
    225 			*releaseFence = releaseFence32;
    226 			return 0;
    227 		default:
    228 			return -EINVAL;
    229 	}
    230 }
    231 
    232 typedef int (*GRALLOC1_PFN_BLIT_HANDLE_TO_HANDLE_IMG)
    233 	(gralloc1_device_t *g, buffer_handle_t src, buffer_handle_t dest,
    234 	 int w, int h, int x, int y, int transform, int input_fence,
    235 	 int *output_fence);
    236 
    237 static inline int gralloc1_blit_handle_to_handle_img
    238 	(gralloc1_device_t *g, buffer_handle_t src, buffer_handle_t dest,
    239 	 int w, int h, int x, int y, int transform, int input_fence,
    240 	 int *output_fence)
    241 {
    242 	GRALLOC1_PFN_BLIT_HANDLE_TO_HANDLE_IMG f =
    243 		(GRALLOC1_PFN_BLIT_HANDLE_TO_HANDLE_IMG)
    244 			g->getFunction(g, GRALLOC1_FUNCTION_BLIT_HANDLE_TO_HANDLE_IMG);
    245 
    246 	return f(g, src, dest, w, h, x, y, transform, input_fence, output_fence);
    247 }
    248 
    249 typedef int (*GRALLOC1_PFN_GET_BUFFER_CPU_ADDRESSES_IMG)
    250 	(gralloc1_device_t *g, buffer_handle_t handle, void **vaddrs,
    251 	 size_t *sizes);
    252 
    253 static inline int gralloc1_get_buffer_cpu_addresses_img
    254 	(gralloc1_device_t *g, buffer_handle_t handle, void **vaddrs,
    255 	 size_t *sizes)
    256 {
    257 	GRALLOC1_PFN_GET_BUFFER_CPU_ADDRESSES_IMG f =
    258 		(GRALLOC1_PFN_GET_BUFFER_CPU_ADDRESSES_IMG)
    259 			g->getFunction(g, GRALLOC1_FUNCTION_GET_BUFFER_CPU_ADDRESSES_IMG);
    260 
    261 	return f(g, handle, vaddrs, sizes);
    262 }
    263 
    264 typedef int (*GRALLOC1_PFN_PUT_BUFFER_CPU_ADDRESSES_IMG)
    265 	(gralloc1_device_t *g, buffer_handle_t handle);
    266 
    267 static inline int gralloc1_put_buffer_cpu_addresses_img
    268 	(gralloc1_device_t *g, buffer_handle_t handle)
    269 {
    270 	GRALLOC1_PFN_PUT_BUFFER_CPU_ADDRESSES_IMG f =
    271 		(GRALLOC1_PFN_PUT_BUFFER_CPU_ADDRESSES_IMG)
    272 			g->getFunction(g, GRALLOC1_FUNCTION_PUT_BUFFER_CPU_ADDRESSES_IMG);
    273 
    274 	return f(g, handle);
    275 }
    276 
    277 typedef int (*GRALLOC1_PFN_GET_DISPLAY_DEVICE_IMG)
    278 	(gralloc1_device_t *g, void **ppvDispDev);
    279 
    280 static inline int gralloc1_get_display_device_img
    281 	(gralloc1_device_t *g, void **ppvDispDev)
    282 {
    283 	GRALLOC1_PFN_GET_DISPLAY_DEVICE_IMG f =
    284 		(GRALLOC1_PFN_GET_DISPLAY_DEVICE_IMG)
    285 			g->getFunction(g, GRALLOC1_FUNCTION_GET_DISPLAY_DEVICE_IMG);
    286 
    287 	return f(g, ppvDispDev);
    288 }
    289 
    290 typedef int (*GRALLOC1_PFN_GET_DISPLAY_STATUS_IMG)
    291 	(gralloc1_device_t *g, buffer_handle_t handle, uint32_t *pui32Status);
    292 
    293 static inline int gralloc1_get_display_status_img
    294 	(gralloc1_device_t *g, buffer_handle_t handle, uint32_t *pui32Status)
    295 {
    296 	GRALLOC1_PFN_GET_DISPLAY_STATUS_IMG f =
    297 		(GRALLOC1_PFN_GET_DISPLAY_STATUS_IMG)
    298 			g->getFunction(g, GRALLOC1_FUNCTION_GET_DISPLAY_STATUS_IMG);
    299 
    300 	return f(g, handle, pui32Status);
    301 }
    302 
    303 #endif /* IMG_GRALLOC1_H */
    304