1 /*
2 * Copyright (C) 2019-2020 RockChip Limited. All rights reserved.
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 /* --------------------------------------------------------------------------------------------------------
18 * File: platform_gralloc4.c
19 *
20 * Desc: 具体实现 platform_gralloc4.h 定义的接口, 基于 IMapper 4.0.
21 *
22 * -----------------------------------------------------------------------------------
23 * < 习语 和 缩略语 > :
24 *
25 * -----------------------------------------------------------------------------------
26 * < 内部模块 or 对象的层次结构 > :
27 *
28 * -----------------------------------------------------------------------------------
29 * < 功能实现的基本流程 > :
30 *
31 * -----------------------------------------------------------------------------------
32 * < 关键标识符 > :
33 *
34 * -----------------------------------------------------------------------------------
35 * < 本模块实现依赖的外部模块 > :
36 * ...
37 * -----------------------------------------------------------------------------------
38 * Note:
39 *
40 * Author: ChenZhen
41 *
42 * Log:
43 * init
44 ----Fri Aug 28 10:17:46 2020
45 */
46
47
48 /* ---------------------------------------------------------------------------------------------------------
49 * Include Files
50 * ---------------------------------------------------------------------------------------------------------
51 */
52
53 #ifdef LOG_TAG
54 #undef LOG_TAG
55 #define LOG_TAG "platform_gralloc4"
56 #endif
57 #define ENABLE_DEBUG_LOG
58 #include <custom_log.h>
59
60 #include <sync/sync.h>
61
62 #include <drm_fourcc.h>
63
64 #ifdef ANDROID_12
65 #include <hardware/hardware_rockchip.h>
66 #endif
67
68 #include "platform_gralloc4.h"
69
70 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
71 #include <gralloctypes/Gralloc4.h>
72 // #include <aidl/arm/graphics/ArmMetadataType.h>
73
74 using android::hardware::graphics::mapper::V4_0::Error;
75 using android::hardware::graphics::mapper::V4_0::IMapper;
76 using android::hardware::hidl_vec;
77
78 using android::gralloc4::MetadataType_PlaneLayouts;
79 using android::gralloc4::decodePlaneLayouts;
80 using android::gralloc4::MetadataType_Usage;
81 using android::gralloc4::decodeUsage;
82 using android::gralloc4::MetadataType_PlaneLayouts;
83 using android::gralloc4::decodePlaneLayouts;
84 using android::gralloc4::MetadataType_PixelFormatFourCC;
85 using android::gralloc4::decodePixelFormatFourCC;
86 using android::gralloc4::MetadataType_PixelFormatModifier;
87 using android::gralloc4::decodePixelFormatModifier;
88 using android::gralloc4::MetadataType_PixelFormatRequested;
89 using android::gralloc4::decodePixelFormatRequested;
90 using android::gralloc4::MetadataType_AllocationSize;
91 using android::gralloc4::decodeAllocationSize;
92 using android::gralloc4::MetadataType_LayerCount;
93 using android::gralloc4::decodeLayerCount;
94 using android::gralloc4::MetadataType_Dataspace;
95 using android::gralloc4::decodeDataspace;
96 using android::gralloc4::MetadataType_Crop;
97 using android::gralloc4::decodeCrop;
98 using android::gralloc4::MetadataType_Width;
99 using android::gralloc4::decodeWidth;
100 using android::gralloc4::MetadataType_Height;
101 using android::gralloc4::decodeHeight;
102
103 using aidl::android::hardware::graphics::common::Dataspace;
104 using aidl::android::hardware::graphics::common::PlaneLayout;
105 using aidl::android::hardware::graphics::common::ExtendableType;
106 using aidl::android::hardware::graphics::common::PlaneLayout;
107 using aidl::android::hardware::graphics::common::Rect;
108 using android::hardware::graphics::common::V1_2::PixelFormat;
109
110
111 #define GRALLOC_ARM_METADATA_TYPE_NAME "arm.graphics.ArmMetadataType"
112 const static IMapper::MetadataType ArmMetadataType_PLANE_FDS
113 {
114 GRALLOC_ARM_METADATA_TYPE_NAME,
115 // static_cast<int64_t>(aidl::arm::graphics::ArmMetadataType::PLANE_FDS)
116 1 // 就是上面的 'PLANE_FDS'
117 };
118
119
120 /* ---------------------------------------------------------------------------------------------------------
121 * External Function Prototypes (referenced in this file)
122 * ---------------------------------------------------------------------------------------------------------
123 */
124
125 /* ---------------------------------------------------------------------------------------------------------
126 * Local Macros
127 * ---------------------------------------------------------------------------------------------------------
128 */
129
130 namespace gralloc4 {
131 /* ---------------------------------------------------------------------------------------------------------
132 * Local Typedefs
133 * ---------------------------------------------------------------------------------------------------------
134 */
135
136
137 /* ---------------------------------------------------------------------------------------------------------
138 * Local Function Prototypes
139 * ---------------------------------------------------------------------------------------------------------
140 */
141
142
143 /* ---------------------------------------------------------------------------------------------------------
144 * Local Variables
145 * ---------------------------------------------------------------------------------------------------------
146 */
147 static constexpr Error kTransactionError = Error::NO_RESOURCES;
148
149 /* ---------------------------------------------------------------------------------------------------------
150 * Global Variables
151 * ---------------------------------------------------------------------------------------------------------
152 */
153
154 /* ---------------------------------------------------------------------------------------------------------
155 * Local Functions Implementation
156 * ---------------------------------------------------------------------------------------------------------
157 */
158
get_service()159 static IMapper &get_service()
160 {
161 static android::sp<IMapper> cached_service = IMapper::getService();
162 return *cached_service;
163 }
164
165 template <typename T>
get_metadata(IMapper & mapper,buffer_handle_t handle,IMapper::MetadataType type,android::status_t (* decode)(const hidl_vec<uint8_t> &,T *),T * value)166 static int get_metadata(IMapper &mapper, buffer_handle_t handle, IMapper::MetadataType type,
167 android::status_t (*decode)(const hidl_vec<uint8_t> &, T *), T *value)
168 {
169 void *handle_arg = const_cast<native_handle_t *>(handle);
170 assert(handle_arg);
171 assert(value);
172 assert(decode);
173
174 int err = 0;
175 mapper.get(handle_arg, type, [&err, value, decode](Error error, const hidl_vec<uint8_t> &metadata)
176 {
177 if (error != Error::NONE)
178 {
179 err = android::BAD_VALUE;
180 return;
181 }
182 err = decode(metadata, value);
183 });
184 return err;
185 }
186
decodeArmPlaneFds(const hidl_vec<uint8_t> & input,std::vector<int64_t> * fds)187 android::status_t static decodeArmPlaneFds(const hidl_vec<uint8_t>& input, std::vector<int64_t>* fds)
188 {
189 assert (fds != nullptr);
190 int64_t size = 0;
191
192 memcpy(&size, input.data(), sizeof(int64_t));
193 if (size < 0)
194 {
195 return android::BAD_VALUE;
196 }
197
198 fds->resize(size);
199
200 const uint8_t *tmp = input.data() + sizeof(int64_t);
201 memcpy(fds->data(), tmp, sizeof(int64_t) * size);
202
203 return android::NO_ERROR;
204 }
205
206 /* ---------------------------------------------------------------------------------------------------------
207 * Global Functions Implementation
208 * ---------------------------------------------------------------------------------------------------------
209 */
210
get_width(buffer_handle_t handle,uint64_t * width)211 int get_width(buffer_handle_t handle, uint64_t* width)
212 {
213 auto &mapper = get_service();
214
215 int err = get_metadata(mapper, handle, MetadataType_Width, decodeWidth, width);
216 if (err != android::OK)
217 {
218 ALOGE("err : %d", err);
219 }
220
221 return err;
222 }
223
get_height(buffer_handle_t handle,uint64_t * height)224 int get_height(buffer_handle_t handle, uint64_t* height)
225 {
226 auto &mapper = get_service();
227
228 int err = get_metadata(mapper, handle, MetadataType_Height, decodeHeight, height);
229 if (err != android::OK)
230 {
231 ALOGE("err : %d", err);
232 }
233
234 return err;
235 }
236
get_pixel_stride(buffer_handle_t handle,int * pixel_stride)237 int get_pixel_stride(buffer_handle_t handle, int* pixel_stride)
238 {
239 auto &mapper = get_service();
240 std::vector<PlaneLayout> layouts;
241 int format_requested;
242
243 int err = get_format_requested(handle, &format_requested);
244 if (err != android::OK )
245 {
246 ALOGE("err : %d", err);
247 return err;
248 }
249
250 /* 若 'format_requested' "不是" HAL_PIXEL_FORMAT_YCrCb_NV12_10, 则 ... */
251 if ( format_requested != HAL_PIXEL_FORMAT_YCrCb_NV12_10 )
252 {
253 err = get_metadata(mapper, handle, MetadataType_PlaneLayouts, decodePlaneLayouts, &layouts);
254 if (err != android::OK || layouts.size() < 1)
255 {
256 ALOGE("Failed to get plane layouts. err : %d", err);
257 return err;
258 }
259
260 if ( layouts.size() > 1 )
261 {
262 W("it's not reasonable to get global pixel_stride of buffer with planes more than 1.");
263 }
264
265 *pixel_stride = (layouts[0].widthInSamples);
266 }
267 /* 否则, 即 'format_requested' "是" HAL_PIXEL_FORMAT_YCrCb_NV12_10, 则 ... */
268 else
269 {
270 uint64_t width;
271 int byte_stride;
272
273 err = get_width(handle, &width);
274 if (err != android::OK )
275 {
276 ALOGE("err : %d", err);
277 return err;
278 }
279
280 // .trick : from CSY : 分配 rk_video_decoder 输出 buffers 时, 要求的 byte_stride of buffer in NV12_10, 已经通过 width 传入.
281 // 原理上, NV12_10 的 pixel_stride 和 byte_stride 是不同的, 但是这里保留 之前 rk_drm_gralloc 的赋值方式.
282 byte_stride = (int)width;
283 *pixel_stride = byte_stride;
284 }
285
286 return err;
287 }
288
get_byte_stride(buffer_handle_t handle,int * byte_stride)289 int get_byte_stride(buffer_handle_t handle, int* byte_stride)
290 {
291 auto &mapper = get_service();
292 std::vector<PlaneLayout> layouts;
293 int format_requested;
294
295 int err = get_format_requested(handle, &format_requested);
296 if (err != android::OK )
297 {
298 ALOGE("err : %d", err);
299 return err;
300 }
301
302 /* 若 'format_requested' "不是" HAL_PIXEL_FORMAT_YCrCb_NV12_10, 则 ... */
303 if ( format_requested != HAL_PIXEL_FORMAT_YCrCb_NV12_10 )
304 {
305 err = get_metadata(mapper, handle, MetadataType_PlaneLayouts, decodePlaneLayouts, &layouts);
306 if (err != android::OK || layouts.size() < 1)
307 {
308 ALOGE("Failed to get plane layouts. err : %d", err);
309 return err;
310 }
311
312 if ( layouts.size() > 1 )
313 {
314 W("it's not reasonable to get global byte_stride of buffer with planes more than 1.");
315 }
316
317 *byte_stride = (layouts[0].strideInBytes);
318 }
319 /* 否则, 即 'format_requested' "是" HAL_PIXEL_FORMAT_YCrCb_NV12_10, 则 ... */
320 else
321 {
322 uint64_t width;
323
324 err = get_width(handle, &width);
325 if (err != android::OK )
326 {
327 ALOGE("err : %d", err);
328 return err;
329 }
330
331 // .KP : from CSY : 分配 rk_video_decoder 输出 buffers 时, 要求的 byte_stride of buffer in NV12_10, 已经通过 width 传入.
332 *byte_stride = (int)width;
333 }
334
335 return err;
336 }
337
get_format_requested(buffer_handle_t handle,int * format_requested)338 int get_format_requested(buffer_handle_t handle, int* format_requested)
339 {
340 auto &mapper = get_service();
341 // android::PixelFormat format; // *format_requested
342 PixelFormat format; // *format_requested
343
344 int err = get_metadata(mapper, handle, MetadataType_PixelFormatRequested, decodePixelFormatRequested, &format);
345 if (err != android::OK)
346 {
347 ALOGE("Failed to get pixel_format_requested. err : %d", err);
348 return err;
349 }
350
351 *format_requested = (int)format;
352
353 return err;
354 }
355
get_usage(buffer_handle_t handle,uint64_t * usage)356 int get_usage(buffer_handle_t handle, uint64_t* usage)
357 {
358 auto &mapper = get_service();
359
360 int err = get_metadata(mapper, handle, MetadataType_Usage, decodeUsage, usage);
361 if (err != android::OK)
362 {
363 ALOGE("Failed to get pixel_format_requested. err : %d", err);
364 return err;
365 }
366
367 return err;
368 }
369
get_allocation_size(buffer_handle_t handle,uint64_t * allocation_size)370 int get_allocation_size(buffer_handle_t handle, uint64_t* allocation_size)
371 {
372 auto &mapper = get_service();
373
374 int err = get_metadata(mapper, handle, MetadataType_AllocationSize, decodeAllocationSize, allocation_size);
375 if (err != android::OK)
376 {
377 ALOGE("Failed to get allocation_size. err : %d", err);
378 return err;
379 }
380
381 return err;
382 }
383
get_share_fd(buffer_handle_t handle,int * share_fd)384 int get_share_fd(buffer_handle_t handle, int* share_fd)
385 {
386 auto &mapper = get_service();
387 std::vector<int64_t> fds;
388
389 int err = get_metadata(mapper, handle, ArmMetadataType_PLANE_FDS, decodeArmPlaneFds, &fds);
390 if (err != android::OK)
391 {
392 ALOGE("Failed to get plane_fds. err : %d", err);
393 return err;
394 }
395 assert (fds.size() > 0);
396
397 *share_fd = (int)(fds[0]);
398
399 return err;
400 }
401
importBuffer(buffer_handle_t rawHandle,buffer_handle_t * outHandle)402 status_t importBuffer(buffer_handle_t rawHandle, buffer_handle_t* outHandle)
403 {
404 auto &mapper = get_service();
405 Error error;
406 auto ret = mapper.importBuffer(android::hardware::hidl_handle(rawHandle),
407 [&](const auto& tmpError, const auto& tmpBuffer)
408 {
409 error = tmpError;
410 if (error != Error::NONE)
411 {
412 return;
413 }
414 *outHandle = static_cast<buffer_handle_t>(tmpBuffer);
415 });
416
417 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
418 }
419
freeBuffer(buffer_handle_t handle)420 void freeBuffer(buffer_handle_t handle)
421 {
422 auto &mapper = get_service();
423 auto buffer = const_cast<native_handle_t*>(handle);
424 auto ret = mapper.freeBuffer(buffer);
425
426 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
427 if (error != Error::NONE)
428 {
429 ALOGE("freeBuffer(%p) failed: %d", handle, error);
430 }
431 }
432
lock(buffer_handle_t bufferHandle,uint64_t usage,int x,int y,int w,int h,void ** outData)433 status_t lock(buffer_handle_t bufferHandle,
434 uint64_t usage,
435 int x,
436 int y,
437 int w,
438 int h,
439 void** outData)
440 {
441 auto &mapper = get_service();
442 auto buffer = const_cast<native_handle_t*>(bufferHandle);
443
444 IMapper::Rect accessRegion = {x, y, w, h};
445
446 android::hardware::hidl_handle acquireFenceHandle; // dummy
447
448 Error error;
449 auto ret = mapper.lock(buffer,
450 usage,
451 accessRegion,
452 acquireFenceHandle,
453 [&](const auto& tmpError, const auto& tmpData) {
454 error = tmpError;
455 if (error != Error::NONE) {
456 return;
457 }
458 *outData = tmpData;
459 });
460
461 error = (ret.isOk()) ? error : kTransactionError;
462
463 ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error);
464
465 return static_cast<status_t>(error);
466 }
467
unlock(buffer_handle_t bufferHandle)468 void unlock(buffer_handle_t bufferHandle)
469 {
470 auto &mapper = get_service();
471 auto buffer = const_cast<native_handle_t*>(bufferHandle);
472
473 Error error;
474 auto ret = mapper.unlock(buffer,
475 [&](const auto& tmpError, const auto& tmpReleaseFence)
476 {
477 error = tmpError;
478 if (error != Error::NONE) {
479 return;
480 }
481
482 auto fenceHandle = tmpReleaseFence.getNativeHandle(); // 预期 unlock() 不会返回有效的 release_fence.
483 if (fenceHandle && fenceHandle->numFds == 1)
484 {
485 ALOGE("got unexpected valid fd of release_fence : %d", fenceHandle->data[0]);
486
487 int fd = dup(fenceHandle->data[0]);
488 if (fd < 0) {
489 ALOGD("failed to dup unlock release fence");
490 sync_wait(fenceHandle->data[0], -1);
491 }
492 }
493 });
494
495 if (!ret.isOk()) {
496 error = kTransactionError;
497 }
498
499 if (error != Error::NONE) {
500 ALOGE("unlock(%p) failed with %d", buffer, error);
501 }
502
503 return;
504 }
505
506 } // namespace gralloc4
507