xref: /aosp_15_r20/external/drm_hwcomposer/bufferinfo/legacy/BufferInfoMaliMeson.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
2  * Copyright (C) 2019 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 "BufferInfoMaliMeson.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 namespace android {
30 
31 LEGACY_BUFFER_INFO_GETTER(BufferInfoMaliMeson);
32 
33 #if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \
34     defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16)
ConvertGrallocFormatToDrmModifiers(uint64_t flags)35 uint64_t BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
36     uint64_t flags) {
37   uint64_t features = 0UL;
38 
39   if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC) {
40     if (flags & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
41       features |= AFBC_FORMAT_MOD_BLOCK_SIZE_32x8;
42     else
43       features |= AFBC_FORMAT_MOD_BLOCK_SIZE_16x16;
44   }
45 
46   if (flags & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
47     features |= (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_SPARSE);
48 
49   if (flags & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
50     features |= AFBC_FORMAT_MOD_TILED;
51 
52   if (features)
53     return DRM_FORMAT_MOD_ARM_AFBC(features | AFBC_FORMAT_MOD_YTR);
54 
55   return 0;
56 }
57 #else
ConvertGrallocFormatToDrmModifiers(uint64_t)58 uint64_t BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
59     uint64_t /* flags */) {
60   return 0;
61 }
62 #endif
63 
GetBoInfo(buffer_handle_t handle)64 auto BufferInfoMaliMeson::GetBoInfo(buffer_handle_t handle)
65     -> std::optional<BufferInfo> {
66   const auto *hnd = (private_handle_t const *)handle;
67   if (!hnd)
68     return {};
69 
70   if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
71     return {};
72 
73   const uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
74   if (fmt == DRM_FORMAT_INVALID)
75     return {};
76 
77   BufferInfo bi{};
78 
79   bi.modifiers[0] = BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
80       hnd->internal_format);
81 
82   bi.width = hnd->width;
83   bi.height = hnd->height;
84   bi.format = fmt;
85   bi.prime_fds[0] = hnd->share_fd;
86   bi.pitches[0] = hnd->byte_stride;
87   bi.offsets[0] = 0;
88 
89   return bi;
90 }
91 
92 }  // namespace android
93