1 /*
2 * Copyright (C) 2020 The Android Open Source Project
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 "hwc-bufferinfo-mali-hisi"
18
19 #include "BufferInfoMaliHisi.h"
20
21 #include <log/log.h>
22 #include <xf86drm.h>
23 #include <xf86drmMode.h>
24
25 #include <cinttypes>
26
27 #include "gralloc_priv.h"
28
29 #define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1))
30
31 namespace android {
32
33 LEGACY_BUFFER_INFO_GETTER(BufferInfoMaliHisi);
34
35 #if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \
36 defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16)
ConvertGrallocFormatToDrmModifiers(uint64_t flags,bool is_rgb)37 uint64_t BufferInfoMaliHisi::ConvertGrallocFormatToDrmModifiers(uint64_t flags,
38 bool is_rgb) {
39 uint64_t features = 0UL;
40
41 if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC)
42 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_16x16;
43
44 if (flags & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
45 features |= (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_SPARSE);
46
47 if (flags & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
48 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_32x8;
49
50 if (flags & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
51 features |= AFBC_FORMAT_MOD_TILED;
52
53 if (features) {
54 if (is_rgb)
55 features |= AFBC_FORMAT_MOD_YTR;
56
57 return DRM_FORMAT_MOD_ARM_AFBC(features);
58 }
59
60 return 0;
61 }
62 #else
ConvertGrallocFormatToDrmModifiers(uint64_t,bool)63 uint64_t BufferInfoMaliHisi::ConvertGrallocFormatToDrmModifiers(
64 uint64_t /* flags */, bool /* is_rgb */) {
65 return 0;
66 }
67 #endif
68
ConvertBoInfo(buffer_handle_t handle,hwc_drm_bo_t * bo)69 int BufferInfoMaliHisi::ConvertBoInfo(buffer_handle_t handle,
70 hwc_drm_bo_t *bo) {
71 bool is_rgb;
72
73 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
74 handle);
75 if (!hnd)
76 return -EINVAL;
77
78 if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
79 return -EINVAL;
80
81 uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
82 if (fmt == DRM_FORMAT_INVALID)
83 return -EINVAL;
84
85 is_rgb = IsDrmFormatRgb(fmt);
86 bo->modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format,
87 is_rgb);
88
89 bo->width = hnd->width;
90 bo->height = hnd->height;
91 bo->hal_format = hnd->req_format;
92 bo->format = fmt;
93 bo->usage = hnd->usage;
94 bo->pitches[0] = hnd->byte_stride;
95 bo->prime_fds[0] = hnd->share_fd;
96 bo->offsets[0] = 0;
97
98 switch (fmt) {
99 case DRM_FORMAT_YVU420: {
100 int align = 128;
101 if (hnd->usage &
102 (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
103 align = 16;
104 int adjusted_height = MALI_ALIGN(hnd->height, 2);
105 int y_size = adjusted_height * hnd->byte_stride;
106 int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align);
107 int v_size = vu_stride * (adjusted_height / 2);
108
109 /* V plane*/
110 bo->prime_fds[1] = hnd->share_fd;
111 bo->pitches[1] = vu_stride;
112 bo->offsets[1] = y_size;
113 /* U plane */
114 bo->prime_fds[2] = hnd->share_fd;
115 bo->pitches[2] = vu_stride;
116 bo->offsets[2] = y_size + v_size;
117 break;
118 }
119 default:
120 break;
121 }
122
123 bo->with_modifiers = true;
124
125 return 0;
126 }
127
128 } // namespace android
129