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