xref: /aosp_15_r20/external/drm_hwcomposer/bufferinfo/legacy/BufferInfoMaliHisi.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
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 "drmhwc"
18 
19 #include "BufferInfoMaliHisi.h"
20 
21 #include <xf86drm.h>
22 #include <xf86drmMode.h>
23 
24 #include <cinttypes>
25 
26 #include "gralloc_priv.h"
27 #include "utils/log.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 
GetBoInfo(buffer_handle_t handle)69 auto BufferInfoMaliHisi::GetBoInfo(buffer_handle_t handle)
70     -> std::optional<BufferInfo> {
71   bool is_rgb = false;
72 
73   const auto *hnd = (private_handle_t const *)handle;
74   if (!hnd)
75     return {};
76 
77   if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
78     return {};
79 
80   const uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
81   if (fmt == DRM_FORMAT_INVALID)
82     return {};
83 
84   BufferInfo bi{};
85 
86   is_rgb = IsDrmFormatRgb(fmt);
87   bi.modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format,
88                                                        is_rgb);
89 
90   bi.width = hnd->width;
91   bi.height = hnd->height;
92   bi.format = fmt;
93   bi.pitches[0] = hnd->byte_stride;
94   bi.prime_fds[0] = hnd->share_fd;
95   bi.offsets[0] = 0;
96 
97   switch (fmt) {
98     case DRM_FORMAT_YVU420: {
99       int align = 128;
100       if (hnd->usage &
101           (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
102         align = 16;
103       const int adjusted_height = MALI_ALIGN(hnd->height, 2);
104       const int y_size = adjusted_height * hnd->byte_stride;
105       const int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align);
106       const int v_size = vu_stride * (adjusted_height / 2);
107 
108       /* V plane*/
109       bi.prime_fds[1] = hnd->share_fd;
110       bi.pitches[1] = vu_stride;
111       bi.offsets[1] = y_size;
112       /* U plane */
113       bi.prime_fds[2] = hnd->share_fd;
114       bi.pitches[2] = vu_stride;
115       bi.offsets[2] = y_size + v_size;
116       break;
117     }
118     default:
119       break;
120   }
121 
122   return bi;
123 }
124 
125 }  // namespace android
126