1 /*
2  * Copyright (C) 2020 Samsung Electronics Co. Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <log/log.h>
18 #include "VendorGraphicBuffer.h"
19 #include "mali_gralloc_buffer.h"
20 #include "gralloc_buffer_priv.h"
21 #include "exynos_format.h"
22 
23 #define UNUSED(x) ((void)x)
24 
25 using namespace android;
26 using namespace vendor::graphics;
27 
is_afbc(buffer_handle_t buffer_hnd_p)28 int VendorGraphicBufferMeta::is_afbc(buffer_handle_t buffer_hnd_p)
29 {
30 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(buffer_hnd_p);
31 
32 	if (!gralloc_hnd)
33 		return 0;
34 
35 	return gralloc_hnd->is_compressible;
36 }
37 
is_sbwc(buffer_handle_t buffer_hnd_p)38 int VendorGraphicBufferMeta::is_sbwc(buffer_handle_t buffer_hnd_p)
39 {
40 	const private_handle_t *hnd = static_cast<const private_handle_t *>(buffer_hnd_p);
41 
42 	switch (static_cast<uint32_t>(hnd->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK)) {
43                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC:
44                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC:
45                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC:
46                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC:
47                 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC:
48                 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC:
49                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50:
50                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75:
51                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50:
52                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75:
53                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40:
54                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60:
55                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80:
56                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40:
57                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60:
58                 case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80:
59                         return true;
60         }
61         return false;
62 }
63 
64 #define GRALLOC_META_GETTER(__type__, __name__, __member__) \
65 __type__ VendorGraphicBufferMeta::get_##__name__(buffer_handle_t hnd) \
66 { \
67 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd); \
68 	if (!gralloc_hnd) return 0; \
69 	return gralloc_hnd->__member__; \
70 } \
71 
72 
73 GRALLOC_META_GETTER(uint32_t, format, format);
74 GRALLOC_META_GETTER(uint64_t, internal_format, internal_format);
75 GRALLOC_META_GETTER(uint64_t, frameworkFormat, req_format);
76 
77 GRALLOC_META_GETTER(int, width, width);
78 GRALLOC_META_GETTER(int, height, height);
79 GRALLOC_META_GETTER(uint32_t, stride, stride);
80 GRALLOC_META_GETTER(uint32_t, vstride, plane_info[0].alloc_height);
81 
82 GRALLOC_META_GETTER(uint64_t, producer_usage, producer_usage);
83 GRALLOC_META_GETTER(uint64_t, consumer_usage, consumer_usage);
84 
85 GRALLOC_META_GETTER(uint64_t, flags, flags);
86 
get_dataspace(buffer_handle_t hnd)87 int VendorGraphicBufferMeta::get_dataspace(buffer_handle_t hnd)
88 {
89 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd);
90 
91 	if (!gralloc_hnd)
92 		return -1;
93 
94 	int attr_fd = gralloc_hnd->get_share_attr_fd();
95 	if (attr_fd < 0)
96 	{
97 		ALOGE("Shared attribute region not available to be mapped");
98 		return -1;
99 	}
100 	attr_region* region;
101 	region = (attr_region *) mmap(NULL, sizeof(attr_region), PROT_READ, MAP_SHARED, attr_fd, 0);
102 	if (region == NULL)
103 		return -1;
104 	else if (region == MAP_FAILED)
105 		return -1;
106 
107 	int dataspace = region->force_dataspace == -1 ? region->dataspace : region->force_dataspace;
108 
109 	munmap(region, sizeof(attr_region));
110 
111 	return dataspace;
112 }
113 
set_dataspace(buffer_handle_t hnd,android_dataspace_t dataspace)114 int VendorGraphicBufferMeta::set_dataspace(buffer_handle_t hnd, android_dataspace_t dataspace)
115 {
116 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd);
117 
118 	if (!gralloc_hnd)
119 		return -1;
120 
121 	int attr_fd = gralloc_hnd->get_share_attr_fd();
122 
123 	if (attr_fd <0)
124 		return -1;
125 
126 	attr_region* region = (attr_region*)mmap(NULL, sizeof(attr_region), PROT_READ | PROT_WRITE, MAP_SHARED, attr_fd, 0);
127 	if (region == NULL)
128 		return -1;
129 	else if (region == MAP_FAILED)
130 		return -1;
131 
132 	region->dataspace = dataspace;
133 	region->force_dataspace = dataspace;
134 	munmap(region, sizeof(attr_region));
135 
136 	return 0;
137 }
138 
get_fd(buffer_handle_t hnd,int num)139 int VendorGraphicBufferMeta::get_fd(buffer_handle_t hnd, int num)
140 {
141 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd);
142 
143 	if (!gralloc_hnd)
144 		return -1;
145 
146 	if (num > 2)
147 		return -1;
148 
149 	return gralloc_hnd->fds[num];
150 }
151 
get_size(buffer_handle_t hnd,int num)152 int VendorGraphicBufferMeta::get_size(buffer_handle_t hnd, int num)
153 {
154 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd);
155 
156 	if (!gralloc_hnd)
157 		return 0;
158 
159 	if (num > 2)
160 		return 0;
161 
162 	return gralloc_hnd->sizes[num];
163 }
164 
165 
get_usage(buffer_handle_t hnd)166 uint64_t VendorGraphicBufferMeta::get_usage(buffer_handle_t hnd)
167 {
168 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd);
169 
170 	if (!gralloc_hnd)
171 		return 0;
172 
173 	return gralloc_hnd->producer_usage | gralloc_hnd->consumer_usage;
174 }
175 
get_video_metadata(buffer_handle_t hnd)176 void* VendorGraphicBufferMeta::get_video_metadata(buffer_handle_t hnd)
177 {
178 	private_handle_t *gralloc_hnd =
179 		static_cast<private_handle_t *>(const_cast<native_handle_t *>(hnd));
180 
181 	if (!gralloc_hnd)
182 		return nullptr;
183 
184 	if (gralloc_hnd->flags &
185 		(private_handle_t::PRIV_FLAGS_USES_3PRIVATE_DATA | private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA))
186 	{
187 		int idx = -1;
188 
189 		if (gralloc_hnd->flags & private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA)
190 			idx = 1;
191 		else if (gralloc_hnd->flags & private_handle_t::PRIV_FLAGS_USES_3PRIVATE_DATA)
192 			idx = 2;
193 
194 		if (gralloc_hnd->bases[idx] == 0)
195 			ALOGE("buffer_handle(%p) did not map video metadata", hnd);
196 
197 		return reinterpret_cast<void*>(gralloc_hnd->bases[idx]);
198 	}
199 	else
200 	{
201 		ALOGE("buffer_handle(%p) does not contain video metadata", hnd);
202 		return nullptr;
203 	}
204 }
205 
get_video_metadata_fd(buffer_handle_t hnd)206 int VendorGraphicBufferMeta::get_video_metadata_fd(buffer_handle_t hnd)
207 {
208 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd);
209 
210 	if (!gralloc_hnd)
211 		return -EINVAL;
212 
213 	int idx = -1;
214 
215 	if (gralloc_hnd->flags & VendorGraphicBufferMeta::PRIV_FLAGS_USES_2PRIVATE_DATA)
216 		idx = 1;
217 	else if (gralloc_hnd->flags & VendorGraphicBufferMeta::PRIV_FLAGS_USES_3PRIVATE_DATA)
218 		idx = 2;
219 
220 	if (idx < 0)
221 		return -EINVAL;
222 
223 	return gralloc_hnd->fds[idx];
224 }
225 
226 /* This function is not supported with gralloc3. return nullptr */
get_video_metadata_roiinfo(buffer_handle_t hnd)227 void* VendorGraphicBufferMeta::get_video_metadata_roiinfo(buffer_handle_t hnd)
228 {
229 	UNUSED(hnd);
230 	return nullptr;
231 }
232 
233 /* This function is not used with gralloc3. return nullptr */
import_buffer(buffer_handle_t hnd)234 buffer_handle_t VendorGraphicBufferMeta::import_buffer(buffer_handle_t hnd)
235 {
236 	UNUSED(hnd);
237 	return nullptr;
238 }
239 
240 /* This function is not used with gralloc3. */
free_buffer(buffer_handle_t hnd)241 int VendorGraphicBufferMeta::free_buffer(buffer_handle_t hnd)
242 {
243 	return 0;
244 }
245 
init(const buffer_handle_t handle)246 void VendorGraphicBufferMeta::init(const buffer_handle_t handle)
247 {
248 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(handle);
249 
250 	if (!gralloc_hnd)
251 		return;
252 
253 	fd  = gralloc_hnd->fds[0];
254 	fd1 = gralloc_hnd->fds[1];
255 	fd2 = gralloc_hnd->fds[2];
256 
257 	size  = gralloc_hnd->sizes[0];
258 	size1 = gralloc_hnd->sizes[1];
259 	size2 = gralloc_hnd->sizes[2];
260 
261 	offsets[0] = gralloc_hnd->plane_info[0].offset;
262 	offsets[1] = gralloc_hnd->plane_info[1].offset;
263 	offsets[2] = gralloc_hnd->plane_info[2].offset;
264 
265 	internal_format = gralloc_hnd->internal_format;
266 	frameworkFormat = gralloc_hnd->req_format;
267 
268 	width  = gralloc_hnd->width;
269 	height = gralloc_hnd->height;
270 	stride  = gralloc_hnd->stride;;
271 	vstride = gralloc_hnd->plane_info[0].alloc_height;
272 
273 	producer_usage = gralloc_hnd->producer_usage;
274 	consumer_usage = gralloc_hnd->consumer_usage;
275 
276 	flags = gralloc_hnd->flags;
277 
278 	unique_id = gralloc_hnd->backing_store_id;
279 }
280 
VendorGraphicBufferMeta(buffer_handle_t handle)281 VendorGraphicBufferMeta::VendorGraphicBufferMeta(buffer_handle_t handle)
282 {
283 	init(handle);
284 }
285 
286