1 /*
2  * Copyright (C) 2014-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 
19 #ifndef GRALLOC_BUFFER_PRIV_H_
20 #define GRALLOC_BUFFER_PRIV_H_
21 
22 #include "gralloc_priv.h"
23 #include <errno.h>
24 #include <string.h>
25 #include "mali_gralloc_private_interface_types.h"
26 
27 // private gralloc buffer manipulation API
28 
29 struct attr_region
30 {
31 	/* Rectangle to be cropped from the full frame (Origin in top-left corner!) */
32 	int crop_top;
33 	int crop_left;
34 	int crop_height;
35 	int crop_width;
36 	int use_yuv_transform;
37 	int use_sparse_alloc;
38 	mali_hdr_info hdr_info;
39 } __attribute__((packed));
40 
41 typedef struct attr_region attr_region;
42 
43 /*
44  * Allocate shared memory for attribute storage. Only to be
45  * used by gralloc internally.
46  *
47  * Return 0 on success.
48  */
49 int gralloc_buffer_attr_allocate(struct private_handle_t *hnd);
50 
51 /*
52  * Frees the shared memory allocated for attribute storage.
53  * Only to be used by gralloc internally.
54 
55  * Return 0 on success.
56  */
57 int gralloc_buffer_attr_free(struct private_handle_t *hnd);
58 
59 /*
60  * Map the attribute storage area before attempting to
61  * read/write from it.
62  *
63  * Return 0 on success.
64  */
gralloc_buffer_attr_map(struct private_handle_t * hnd,int readwrite)65 static inline int gralloc_buffer_attr_map(struct private_handle_t *hnd, int readwrite)
66 {
67 	int rval = -1;
68 	int prot_flags = PROT_READ;
69 
70 	if (!hnd)
71 	{
72 		goto out;
73 	}
74 
75 	if (hnd->share_attr_fd < 0)
76 	{
77 		ALOGE("Shared attribute region not available to be mapped");
78 		goto out;
79 	}
80 
81 	if (readwrite)
82 	{
83 		prot_flags |= PROT_WRITE;
84 	}
85 
86 	hnd->attr_base =
87 		mmap(NULL, getpagesize(), prot_flags, MAP_SHARED, hnd->share_attr_fd, 0);
88 
89 	if (hnd->attr_base == MAP_FAILED)
90 	{
91 		ALOGE("Failed to mmap shared attribute region err=%s", strerror(errno));
92 		goto out;
93 	}
94 
95 	rval = 0;
96 
97 out:
98 	return rval;
99 }
100 
101 /*
102  * Unmap the attribute storage area when done with it.
103  *
104  * Return 0 on success.
105  */
gralloc_buffer_attr_unmap(struct private_handle_t * hnd)106 static inline int gralloc_buffer_attr_unmap(struct private_handle_t *hnd)
107 {
108 	int rval = -1;
109 
110 	if (!hnd)
111 	{
112 		goto out;
113 	}
114 
115 	if (hnd->attr_base != MAP_FAILED)
116 	{
117 		if (munmap(hnd->attr_base, getpagesize()) == 0)
118 		{
119 			hnd->attr_base = MAP_FAILED;
120 			rval = 0;
121 		}
122 	}
123 
124 out:
125 	return rval;
126 }
127 
128 /*
129  * Read or write an attribute from/to the storage area.
130  *
131  * Return 0 on success.
132  */
gralloc_buffer_attr_write(struct private_handle_t * hnd,buf_attr attr,int * val)133 static inline int gralloc_buffer_attr_write(struct private_handle_t *hnd, buf_attr attr, int *val)
134 {
135 	int rval = -1;
136 
137 	if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST)
138 	{
139 		goto out;
140 	}
141 
142 	if (hnd->attr_base != MAP_FAILED)
143 	{
144 		attr_region *region = (attr_region *)hnd->attr_base;
145 
146 		switch (attr)
147 		{
148 		case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT:
149 			memcpy(&region->crop_top, val, sizeof(int) * 4);
150 			rval = 0;
151 			break;
152 
153 		case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS:
154 			region->use_yuv_transform = *val;
155 			rval = 0;
156 			break;
157 
158 		case GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC:
159 			region->use_sparse_alloc = *val;
160 			rval = 0;
161 			break;
162 
163 		case GRALLOC_ARM_BUFFER_ATTR_HDR_INFO:
164 			memcpy(&region->hdr_info, val, sizeof(mali_hdr_info));
165 			rval = 0;
166 			break;
167 		}
168 	}
169 
170 out:
171 	return rval;
172 }
173 
gralloc_buffer_attr_read(struct private_handle_t * hnd,buf_attr attr,int * val)174 static inline int gralloc_buffer_attr_read(struct private_handle_t *hnd, buf_attr attr, int *val)
175 {
176 	int rval = -1;
177 
178 	if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST)
179 	{
180 		goto out;
181 	}
182 
183 	if (hnd->attr_base != MAP_FAILED)
184 	{
185 		attr_region *region = (attr_region *)hnd->attr_base;
186 
187 		switch (attr)
188 		{
189 		case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT:
190 			memcpy(val, &region->crop_top, sizeof(int) * 4);
191 			rval = 0;
192 			break;
193 
194 		case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS:
195 			*val = region->use_yuv_transform;
196 			rval = 0;
197 			break;
198 
199 		case GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC:
200 			*val = region->use_sparse_alloc;
201 			rval = 0;
202 			break;
203 
204 		case GRALLOC_ARM_BUFFER_ATTR_HDR_INFO:
205 			memcpy(val, &region->hdr_info, sizeof(mali_hdr_info));
206 			rval = 0;
207 			break;
208 		}
209 	}
210 
211 out:
212 	return rval;
213 }
214 
215 #endif /* GRALLOC_BUFFER_PRIV_H_ */
216