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 #define LOG_TAG "VendorGraphicBuffer"
18
19 #include <log/log.h>
20 #include <gralloctypes/Gralloc4.h>
21 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
22
23 #include "VendorGraphicBuffer.h"
24 #include "mali_gralloc_buffer.h"
25 #include "mali_gralloc_formats.h"
26 #include "hidl_common/SharedMetadata.h"
27 #include "hidl_common/SharedMetadata_struct.h"
28 #include "exynos_format.h"
29
30 using namespace android;
31 using namespace vendor::graphics;
32
33 using arm::mapper::common::shared_metadata;
34 using aidl::android::hardware::graphics::common::Dataspace;
35 using android::gralloc4::encodeDataspace;
36 using android::gralloc4::decodeDataspace;
37 using android::hardware::graphics::mapper::V4_0::IMapper;
38 using android::hardware::graphics::mapper::V4_0::Error;
39
40 #define UNUSED(x) ((void)x)
41 #define SZ_4k 0x1000
42
43 // TODO(b/191912915): This is a non-thread safe version of the validate function
44 // from mapper-4.0-impl library. Ideally this should be decoupled from `impl`
45 // libraries and should depend upon HAL (and it's extension) to call into
46 // Gralloc.
mali_gralloc_reference_validate(buffer_handle_t handle)47 int mali_gralloc_reference_validate(buffer_handle_t handle) {
48 return private_handle_t::validate(handle);
49 }
50
convertNativeHandleToPrivateHandle(buffer_handle_t handle)51 const private_handle_t * convertNativeHandleToPrivateHandle(buffer_handle_t handle) {
52 if (mali_gralloc_reference_validate(handle) < 0)
53 return nullptr;
54
55 return static_cast<const private_handle_t *>(handle);
56 }
57
get_mapper()58 android::sp<IMapper> get_mapper() {
59 static android::sp<IMapper> mapper = []() {
60 auto mapper = IMapper::getService();
61 if (!mapper) {
62 ALOGE("Failed to get mapper service");
63 }
64 return mapper;
65 }();
66
67 return mapper;
68 }
69
get_video_metadata_fd(buffer_handle_t hnd)70 int VendorGraphicBufferMeta::get_video_metadata_fd(buffer_handle_t hnd)
71 {
72 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
73
74 if (!gralloc_hnd)
75 return -EINVAL;
76
77 uint64_t usage = gralloc_hnd->producer_usage | gralloc_hnd->consumer_usage;
78
79 if (usage & VendorGraphicBufferUsage::VIDEO_PRIVATE_DATA)
80 return gralloc_hnd->get_share_attr_fd();
81 else
82 return -EINVAL;
83 }
84
get_dataspace(buffer_handle_t hnd)85 int VendorGraphicBufferMeta::get_dataspace(buffer_handle_t hnd)
86 {
87 native_handle_t* handle = const_cast<native_handle_t*>(hnd);
88 if (!handle) {
89 return -EINVAL;
90 }
91
92 Error error = Error::NONE;
93 Dataspace dataspace;
94 get_mapper()->get(handle, android::gralloc4::MetadataType_Dataspace,
95 [&](const auto& tmpError, const android::hardware::hidl_vec<uint8_t>& tmpVec) {
96 error = tmpError;
97 if (error != Error::NONE) {
98 return;
99 }
100 error = static_cast<Error>(decodeDataspace(tmpVec, &dataspace));
101 });
102
103
104 if (error != Error::NONE) {
105 ALOGE("Failed to get datasapce");
106 return -EINVAL;
107 }
108
109 return static_cast<int>(dataspace);
110 }
111
set_dataspace(buffer_handle_t hnd,android_dataspace_t dataspace)112 int VendorGraphicBufferMeta::set_dataspace(buffer_handle_t hnd, android_dataspace_t dataspace)
113 {
114 native_handle_t* handle = const_cast<native_handle_t*>(hnd);
115 if (!handle) {
116 return -EINVAL;
117 }
118
119 Error error = Error::NONE;
120 android::hardware::hidl_vec<uint8_t> vec;
121 error = static_cast<Error>(encodeDataspace(static_cast<Dataspace>(dataspace), &vec));
122 if (error != Error::NONE) {
123 ALOGE("Error encoding dataspace");
124 return -EINVAL;
125 }
126 error = get_mapper()->set(handle, android::gralloc4::MetadataType_Dataspace, vec);
127
128 if (error != Error::NONE) {
129 ALOGE("Failed to set datasapce");
130 return -EINVAL;
131 }
132
133 return 0;
134 }
135
is_afbc(buffer_handle_t hnd)136 int VendorGraphicBufferMeta::is_afbc(buffer_handle_t hnd)
137 {
138 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
139
140 if (!gralloc_hnd)
141 return 0;
142
143 if (gralloc_hnd->alloc_format & MALI_GRALLOC_INTFMT_AFBCENABLE_MASK)
144 return 1;
145
146 return 0;
147 }
148
is_sbwc(buffer_handle_t hnd)149 int VendorGraphicBufferMeta::is_sbwc(buffer_handle_t hnd)
150 {
151 return is_sbwc_format(VendorGraphicBufferMeta::get_internal_format(hnd));
152 }
153
154 #define GRALLOC_META_GETTER(__type__, __name__, __member__) \
155 __type__ VendorGraphicBufferMeta::get_##__name__(buffer_handle_t hnd) \
156 { \
157 const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd); \
158 if (!gralloc_hnd) return 0; \
159 return gralloc_hnd->__member__; \
160 } \
161
162
get_format(buffer_handle_t hnd)163 uint32_t VendorGraphicBufferMeta::get_format(buffer_handle_t hnd)
164 {
165 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
166
167 if (!gralloc_hnd)
168 return 0;
169
170 return static_cast<uint32_t>(gralloc_hnd->alloc_format);
171 }
172
get_internal_format(buffer_handle_t hnd)173 uint64_t VendorGraphicBufferMeta::get_internal_format(buffer_handle_t hnd)
174 {
175 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);;
176
177 if (!gralloc_hnd)
178 return 0;
179
180 return static_cast<uint64_t>(gralloc_hnd->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK);
181 }
182
183 GRALLOC_META_GETTER(uint64_t, frameworkFormat, req_format);
184
185 GRALLOC_META_GETTER(int, width, width);
186 GRALLOC_META_GETTER(int, height, height);
187 GRALLOC_META_GETTER(uint32_t, stride, stride);
188 GRALLOC_META_GETTER(uint32_t, vstride, plane_info[0].alloc_height);
189
190 GRALLOC_META_GETTER(uint64_t, producer_usage, producer_usage);
191 GRALLOC_META_GETTER(uint64_t, consumer_usage, consumer_usage);
192
193 GRALLOC_META_GETTER(uint64_t, flags, flags);
194
195
get_fd(buffer_handle_t hnd,int num)196 int VendorGraphicBufferMeta::get_fd(buffer_handle_t hnd, int num)
197 {
198 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
199
200 if (!gralloc_hnd)
201 return -1;
202
203 if (num > 2)
204 return -1;
205
206 return gralloc_hnd->fds[num];
207 }
208
get_size(buffer_handle_t hnd,int num)209 int VendorGraphicBufferMeta::get_size(buffer_handle_t hnd, int num)
210 {
211 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
212
213 if (!gralloc_hnd)
214 return 0;
215
216 if (num > 2)
217 return 0;
218
219 return gralloc_hnd->alloc_sizes[num];
220 }
221
222
get_usage(buffer_handle_t hnd)223 uint64_t VendorGraphicBufferMeta::get_usage(buffer_handle_t hnd)
224 {
225 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
226
227 if (!gralloc_hnd)
228 return 0;
229
230 return gralloc_hnd->producer_usage | gralloc_hnd->consumer_usage;
231 }
232
get_video_metadata(buffer_handle_t hnd)233 void* VendorGraphicBufferMeta::get_video_metadata(buffer_handle_t hnd)
234 {
235 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
236
237 if (gralloc_hnd == nullptr)
238 return nullptr;
239
240 return gralloc_hnd->attr_base;
241 }
242
get_video_metadata_roiinfo(buffer_handle_t hnd)243 void* VendorGraphicBufferMeta::get_video_metadata_roiinfo(buffer_handle_t hnd)
244 {
245 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
246
247 if (gralloc_hnd == nullptr)
248 return nullptr;
249
250 if (gralloc_hnd->get_usage() & VendorGraphicBufferUsage::ROIINFO)
251 return static_cast<char*>(gralloc_hnd->attr_base) +
252 sizeof(shared_metadata) + gralloc_hnd->reserved_region_size;
253
254 return nullptr;
255 }
256
init(const buffer_handle_t handle)257 void VendorGraphicBufferMeta::init(const buffer_handle_t handle)
258 {
259 const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(handle);
260
261 if (!gralloc_hnd)
262 return;
263
264 fd = gralloc_hnd->fds[0];
265 fd1 = gralloc_hnd->fds[1];
266 fd2 = gralloc_hnd->fds[2];
267
268 size = gralloc_hnd->alloc_sizes[0];
269 size1 = gralloc_hnd->alloc_sizes[1];
270 size2 = gralloc_hnd->alloc_sizes[2];
271
272 offsets[0] = gralloc_hnd->plane_info[0].offset;
273 offsets[1] = gralloc_hnd->plane_info[1].offset;
274 offsets[2] = gralloc_hnd->plane_info[2].offset;
275
276 uint64_t usage = gralloc_hnd->producer_usage | gralloc_hnd->consumer_usage;
277 if (usage & VendorGraphicBufferUsage::VIDEO_PRIVATE_DATA) {
278 switch (gralloc_hnd->get_share_attr_fd_index()) {
279 case 1:
280 size1 = gralloc_hnd->attr_size;
281 break;
282 case 2:
283 size2 = gralloc_hnd->attr_size;
284 break;
285 }
286 }
287
288 internal_format = gralloc_hnd->alloc_format;
289 frameworkFormat = gralloc_hnd->req_format;
290
291 width = gralloc_hnd->width;
292 height = gralloc_hnd->height;
293 stride = gralloc_hnd->stride;
294 vstride = gralloc_hnd->plane_info[0].alloc_height;
295
296 producer_usage = gralloc_hnd->producer_usage;
297 consumer_usage = gralloc_hnd->consumer_usage;
298
299 flags = gralloc_hnd->flags;
300
301 unique_id = gralloc_hnd->backing_store_id;
302 }
303
import_buffer(buffer_handle_t hnd)304 buffer_handle_t VendorGraphicBufferMeta::import_buffer(buffer_handle_t hnd)
305 {
306 native_handle_t* handle = const_cast<native_handle_t*>(hnd);
307 if (!handle) {
308 return nullptr;
309 }
310
311 native_handle_t* bufferHandle = nullptr;
312 Error error = Error::NONE;
313 get_mapper()->importBuffer(handle, [&](const auto& tmpError, const auto& tmpBuffer) {
314 error = tmpError;
315 if (error != Error::NONE) {
316 return;
317 }
318 bufferHandle = static_cast<native_handle_t*>(tmpBuffer);
319 });
320
321 if (error != Error::NONE) {
322 ALOGE("[%s] Unable to import buffer", __FUNCTION__);
323 return nullptr;
324 }
325
326 return bufferHandle;
327 }
328
free_buffer(buffer_handle_t hnd)329 int VendorGraphicBufferMeta::free_buffer(buffer_handle_t hnd)
330 {
331 native_handle_t* handle = const_cast<native_handle_t*>(hnd);
332 if (!handle) {
333 return -EINVAL;
334 }
335 Error error = get_mapper()->freeBuffer(handle);
336 if (error != Error::NONE) {
337 ALOGE("[%s] Failed to free buffer", __FUNCTION__);
338 return -EINVAL;
339 }
340 return 0;
341 }
342
VendorGraphicBufferMeta(buffer_handle_t handle)343 VendorGraphicBufferMeta::VendorGraphicBufferMeta(buffer_handle_t handle)
344 {
345 init(handle);
346 }
347
348