Home | History | Annotate | Download | only in gralloc960
      1 /*
      2  * Copyright (C) 2017 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 #ifndef MALI_GRALLOC_BUFFER_H_
     19 #define MALI_GRALLOC_BUFFER_H_
     20 
     21 #include <errno.h>
     22 #include <sys/types.h>
     23 #include <unistd.h>
     24 #include <sys/mman.h>
     25 
     26 #include "mali_gralloc_private_interface_types.h"
     27 
     28 /* NOTE:
     29  * If your framebuffer device driver is integrated with dma_buf, you will have to
     30  * change this IOCTL definition to reflect your integration with the framebuffer
     31  * device.
     32  * Expected return value is a structure filled with a file descriptor
     33  * backing your framebuffer device memory.
     34  */
     35 struct fb_dmabuf_export
     36 {
     37 	__u32 fd;
     38 	__u32 flags;
     39 };
     40 #define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export)
     41 
     42 /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0
     43  * 8 is big enough for "gpu0" & "fb0" currently
     44  */
     45 #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8
     46 #define NUM_FB_BUFFERS 2
     47 
     48 /* Define number of shared file descriptors */
     49 #define GRALLOC_ARM_NUM_FDS 2
     50 
     51 #define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds)
     52 
     53 #define SZ_4K 0x00001000
     54 #define SZ_2M 0x00200000
     55 
     56 struct private_handle_t;
     57 
     58 #ifndef __cplusplus
     59 /* C99 with pedantic don't allow anonymous unions which is used in below struct
     60  * Disable pedantic for C for this struct only.
     61  */
     62 #pragma GCC diagnostic push
     63 #pragma GCC diagnostic ignored "-Wpedantic"
     64 #endif
     65 
     66 #ifdef __cplusplus
     67 struct private_handle_t : public native_handle
     68 {
     69 #else
     70 struct private_handle_t
     71 {
     72 	struct native_handle nativeHandle;
     73 #endif
     74 
     75 #ifdef __cplusplus
     76 	/* Never intended to be used from C code */
     77 	enum
     78 	{
     79 		PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
     80 		PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 0x00000002,
     81 		PRIV_FLAGS_USES_ION = 0x00000004,
     82 		PRIV_FLAGS_USES_ION_DMA_HEAP = 0x00000008
     83 	};
     84 
     85 	enum
     86 	{
     87 		LOCK_STATE_WRITE = 1 << 31,
     88 		LOCK_STATE_MAPPED = 1 << 30,
     89 		LOCK_STATE_READ_MASK = 0x3FFFFFFF
     90 	};
     91 #endif
     92 
     93 	/*
     94 	 * Shared file descriptor for dma_buf sharing. This must be the first element in the
     95 	 * structure so that binder knows where it is and can properly share it between
     96 	 * processes.
     97 	 * DO NOT MOVE THIS ELEMENT!
     98 	 */
     99 	int share_fd;
    100 	int share_attr_fd;
    101 
    102 	// ints
    103 	int magic;
    104 	int req_format;
    105 	uint64_t internal_format;
    106 	int byte_stride;
    107 	int flags;
    108 	int size;
    109 	int width;
    110 	int height;
    111 	int internalWidth;
    112 	int internalHeight;
    113 	int stride;
    114 	union
    115 	{
    116 		void *base;
    117 		uint64_t padding;
    118 	};
    119 	union {
    120 		uint64_t consumer_usage;
    121 		uint64_t usage;
    122 	};
    123 	uint64_t producer_usage;
    124 	uint64_t backing_store_id;
    125 	int backing_store_size;
    126 	int writeOwner;
    127 	int allocating_pid;
    128 	int remote_pid;
    129 	int ref_count;
    130 	// locally mapped shared attribute area
    131 	union
    132 	{
    133 		void *attr_base;
    134 		uint64_t padding3;
    135 	};
    136 
    137 	mali_gralloc_yuv_info yuv_info;
    138 
    139 	// Following members is for framebuffer only
    140 	int fd;
    141 	union
    142 	{
    143 		off_t offset;
    144 		uint64_t padding4;
    145 	};
    146 
    147 	/*
    148 	 * min_pgsz denotes minimum phys_page size used by this buffer.
    149 	 * if buffer memory is physical contiguous set min_pgsz to buff->size
    150 	 * if not sure buff's real phys_page size, you can use SZ_4K for safe.
    151 	 */
    152 	int min_pgsz;
    153 #ifdef __cplusplus
    154 	/*
    155 	 * We track the number of integers in the structure. There are 16 unconditional
    156 	 * integers (magic - pid, yuv_info, fd and offset). Note that the fd element is
    157 	 * considered an int not an fd because it is not intended to be used outside the
    158 	 * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the
    159 	 * number of integers that are conditionally included. Similar considerations apply
    160 	 * to the number of fds.
    161 	 */
    162 	static const int sNumFds = GRALLOC_ARM_NUM_FDS;
    163 	static const int sMagic = 0x3141592;
    164 
    165 	private_handle_t(int _flags, int _size, void *_base, uint64_t _consumer_usage, uint64_t _producer_usage,
    166 	                 int fb_file, off_t fb_offset)
    167 	    : share_fd(-1)
    168 	    , share_attr_fd(-1)
    169 	    , magic(sMagic)
    170 	    , flags(_flags)
    171 	    , size(_size)
    172 	    , width(0)
    173 	    , height(0)
    174 	    , stride(0)
    175 	    , base(_base)
    176 	    , consumer_usage(_consumer_usage)
    177 	    , producer_usage(_producer_usage)
    178 	    , backing_store_id(0x0)
    179 	    , backing_store_size(0)
    180 	    , writeOwner(0)
    181 	    , allocating_pid(getpid())
    182 	    , remote_pid(-1)
    183 	    , ref_count(1)
    184 	    , attr_base(MAP_FAILED)
    185 	    , yuv_info(MALI_YUV_NO_INFO)
    186 	    , fd(fb_file)
    187 	    , offset(fb_offset)
    188 	{
    189 		version = sizeof(native_handle);
    190 		numFds = sNumFds;
    191 		numInts = NUM_INTS_IN_PRIVATE_HANDLE;
    192 	}
    193 
    194 	private_handle_t(int _flags, int _size, int _min_pgsz, uint64_t _consumer_usage, uint64_t _producer_usage,
    195 	                 int _shared_fd, int _req_format, uint64_t _internal_format, int _byte_stride, int _width,
    196 	                 int _height, int _stride, int _internalWidth, int _internalHeight, int _backing_store_size)
    197 	    : share_fd(_shared_fd)
    198 	    , share_attr_fd(-1)
    199 	    , magic(sMagic)
    200 	    , req_format(_req_format)
    201 	    , internal_format(_internal_format)
    202 	    , byte_stride(_byte_stride)
    203 	    , flags(_flags)
    204 	    , size(_size)
    205 	    , width(_width)
    206 	    , height(_height)
    207 	    , internalWidth(_internalWidth)
    208 	    , internalHeight(_internalHeight)
    209 	    , stride(_stride)
    210 	    , base(NULL)
    211 	    , consumer_usage(_consumer_usage)
    212 	    , producer_usage(_producer_usage)
    213 	    , backing_store_id(0x0)
    214 	    , backing_store_size(_backing_store_size)
    215 	    , writeOwner(0)
    216 	    , allocating_pid(getpid())
    217 	    , remote_pid(-1)
    218 	    , ref_count(1)
    219 	    , attr_base(MAP_FAILED)
    220 	    , yuv_info(MALI_YUV_NO_INFO)
    221 	    , fd(-1)
    222 	    , offset(0)
    223 	    , min_pgsz(_min_pgsz)
    224 	{
    225 		version = sizeof(native_handle);
    226 		numFds = sNumFds;
    227 		numInts = NUM_INTS_IN_PRIVATE_HANDLE;
    228 	}
    229 
    230 	~private_handle_t()
    231 	{
    232 		magic = 0;
    233 	}
    234 
    235 	bool usesPhysicallyContiguousMemory()
    236 	{
    237 		return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
    238 	}
    239 
    240 	static int validate(const native_handle *h)
    241 	{
    242 		const private_handle_t *hnd = (const private_handle_t *)h;
    243 
    244 		if (!h || h->version != sizeof(native_handle) || h->numInts != NUM_INTS_IN_PRIVATE_HANDLE ||
    245 		    h->numFds != sNumFds || hnd->magic != sMagic)
    246 		{
    247 			return -EINVAL;
    248 		}
    249 
    250 		return 0;
    251 	}
    252 
    253 	static private_handle_t *dynamicCast(const native_handle *in)
    254 	{
    255 		if (validate(in) == 0)
    256 		{
    257 			return (private_handle_t *)in;
    258 		}
    259 
    260 		return NULL;
    261 	}
    262 #endif
    263 };
    264 #ifndef __cplusplus
    265 /* Restore previous diagnostic for pedantic */
    266 #pragma GCC diagnostic pop
    267 #endif
    268 
    269 #endif /* MALI_GRALLOC_BUFFER_H_ */
    270