1 /*
2 * Copyright (C) 2014-2018 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; /* DEPRECATED. Now explicitly signalled by gralloc through MALI_GRALLOC_INTFMT_AFBC_YUV_TRANSFORM */
37 int use_sparse_alloc;
38 mali_hdr_info hdr_info;
39 android_dataspace_t dataspace;
40 android_dataspace_t force_dataspace;
41 } __attribute__((packed));
42
43 typedef struct attr_region attr_region;
44
45 /*
46 * Allocate shared memory for attribute storage. Only to be
47 * used by gralloc internally.
48 *
49 * Return 0 on success.
50 */
51 int gralloc_buffer_attr_allocate(struct private_handle_t *hnd);
52
53 /*
54 * Frees the shared memory allocated for attribute storage.
55 * Only to be used by gralloc internally.
56
57 * Return 0 on success.
58 */
59 int gralloc_buffer_attr_free(struct private_handle_t *hnd);
60
61 /*
62 * Map the attribute storage area before attempting to
63 * read/write from it.
64 *
65 * Return 0 on success.
66 */
gralloc_buffer_attr_map(struct private_handle_t * hnd,int readwrite)67 static inline int gralloc_buffer_attr_map(struct private_handle_t *hnd, int readwrite)
68 {
69 int rval = -1;
70 int prot_flags = PROT_READ;
71 int share_attr_fd = -1;
72
73 if (!hnd)
74 {
75 goto out;
76 }
77
78 share_attr_fd = hnd->get_share_attr_fd();
79 if (share_attr_fd < 0)
80 {
81 ALOGE("Shared attribute region not available to be mapped");
82 goto out;
83 }
84
85 if (readwrite)
86 {
87 prot_flags |= PROT_WRITE;
88 }
89
90 hnd->attr_base = mmap(NULL, PAGE_SIZE, prot_flags, MAP_SHARED, share_attr_fd, 0);
91
92 if (hnd->attr_base == MAP_FAILED)
93 {
94 ALOGE("Failed to mmap shared attribute region err=%s", strerror(errno));
95 goto out;
96 }
97
98 rval = 0;
99
100 out:
101 return rval;
102 }
103
104 /*
105 * Unmap the attribute storage area when done with it.
106 *
107 * Return 0 on success.
108 */
gralloc_buffer_attr_unmap(struct private_handle_t * hnd)109 static inline int gralloc_buffer_attr_unmap(struct private_handle_t *hnd)
110 {
111 int rval = -1;
112
113 if (!hnd)
114 {
115 goto out;
116 }
117
118 if (hnd->attr_base != MAP_FAILED)
119 {
120 if (munmap(hnd->attr_base, PAGE_SIZE) == 0)
121 {
122 hnd->attr_base = MAP_FAILED;
123 rval = 0;
124 }
125 }
126
127 out:
128 return rval;
129 }
130
131 /*
132 * Read or write an attribute from/to the storage area.
133 *
134 * Return 0 on success.
135 */
gralloc_buffer_attr_write(struct private_handle_t * hnd,buf_attr attr,int * val)136 static inline int gralloc_buffer_attr_write(struct private_handle_t *hnd, buf_attr attr, int *val)
137 {
138 int rval = -1;
139
140 if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST)
141 {
142 goto out;
143 }
144
145 if (hnd->attr_base != MAP_FAILED)
146 {
147 attr_region *region = (attr_region *)hnd->attr_base;
148
149 switch (attr)
150 {
151 case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT:
152 memcpy(®ion->crop_top, val, sizeof(int) * 4);
153 rval = 0;
154 break;
155
156 case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS:
157 region->use_yuv_transform = *val;
158 rval = 0;
159 break;
160
161 case GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC:
162 region->use_sparse_alloc = *val;
163 rval = 0;
164 break;
165
166 case GRALLOC_ARM_BUFFER_ATTR_HDR_INFO:
167 memcpy(®ion->hdr_info, val, sizeof(mali_hdr_info));
168 rval = 0;
169 break;
170
171 case GRALLOC_ARM_BUFFER_ATTR_DATASPACE:
172 region->dataspace = *((android_dataspace_t *)val);
173 rval = 0;
174 break;
175
176 case GRALLOC_ARM_BUFFER_ATTR_FORCE_DATASPACE:
177 region->force_dataspace = *((android_dataspace_t *)val);
178 rval = 0;
179 break;
180 }
181 }
182
183 out:
184 return rval;
185 }
186
gralloc_buffer_attr_read(struct private_handle_t * hnd,buf_attr attr,int * val)187 static inline int gralloc_buffer_attr_read(struct private_handle_t *hnd, buf_attr attr, int *val)
188 {
189 int rval = -1;
190
191 if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST)
192 {
193 goto out;
194 }
195
196 if (hnd->attr_base != MAP_FAILED)
197 {
198 attr_region *region = (attr_region *)hnd->attr_base;
199
200 switch (attr)
201 {
202 case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT:
203 memcpy(val, ®ion->crop_top, sizeof(int) * 4);
204 rval = 0;
205 break;
206
207 case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS:
208 *val = region->use_yuv_transform;
209 rval = 0;
210 break;
211
212 case GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC:
213 *val = region->use_sparse_alloc;
214 rval = 0;
215 break;
216
217 case GRALLOC_ARM_BUFFER_ATTR_HDR_INFO:
218 memcpy(val, ®ion->hdr_info, sizeof(mali_hdr_info));
219 rval = 0;
220 break;
221
222 case GRALLOC_ARM_BUFFER_ATTR_DATASPACE:
223 *val = region->dataspace;
224 rval = 0;
225 break;
226
227 case GRALLOC_ARM_BUFFER_ATTR_FORCE_DATASPACE:
228 *val = region->force_dataspace;
229 rval = 0;
230 break;
231 }
232 }
233
234 out:
235 return rval;
236 }
237
238 #endif /* GRALLOC_BUFFER_PRIV_H_ */
239