xref: /aosp_15_r20/external/drm_hwcomposer/bufferinfo/legacy/BufferInfoImagination.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 "BufferInfoImagination.h"
20 
21 #include <xf86drm.h>
22 
23 #include <cerrno>
24 
25 #include "img_gralloc1_public.h"
26 #include "utils/log.h"
27 
28 namespace android {
29 
30 LEGACY_BUFFER_INFO_GETTER(BufferInfoImagination);
31 
GetBoInfo(buffer_handle_t handle)32 auto BufferInfoImagination::GetBoInfo(buffer_handle_t handle)
33     -> std::optional<BufferInfo> {
34   auto *hnd = (IMG_native_handle_t *)handle;
35   if (!hnd)
36     return {};
37 
38   /* Extra bits are responsible for buffer compression and memory layout */
39   if (hnd->iFormat & ~0x10f) {
40     ALOGV("Special buffer formats are not supported");
41     return {};
42   }
43 
44   BufferInfo bi{};
45 
46   bi.width = hnd->iWidth;
47   bi.height = hnd->iHeight;
48   bi.prime_fds[0] = hnd->fd[0];
49   bi.pitches[0] = ALIGN(hnd->iWidth, HW_ALIGN) * hnd->uiBpp >> 3;
50 
51   switch (hnd->iFormat) {
52 #ifdef HAL_PIXEL_FORMAT_BGRX_8888
53     case HAL_PIXEL_FORMAT_BGRX_8888:
54       bi.format = DRM_FORMAT_XRGB8888;
55       break;
56 #endif
57     default:
58       bi.format = ConvertHalFormatToDrm(hnd->iFormat & 0xf);
59       if (bi.format == DRM_FORMAT_INVALID) {
60         ALOGV("Cannot convert hal format to drm format %u", hnd->iFormat);
61         return {};
62       }
63   }
64 
65   return bi;
66 }
67 
68 }  // namespace android
69