xref: /aosp_15_r20/external/drm_hwcomposer/bufferinfo/legacy/BufferInfoImagination.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1*0a9764feSAndroid Build Coastguard Worker /*
2*0a9764feSAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*0a9764feSAndroid Build Coastguard Worker  *
4*0a9764feSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*0a9764feSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*0a9764feSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*0a9764feSAndroid Build Coastguard Worker  *
8*0a9764feSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*0a9764feSAndroid Build Coastguard Worker  *
10*0a9764feSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*0a9764feSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*0a9764feSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*0a9764feSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*0a9764feSAndroid Build Coastguard Worker  * limitations under the License.
15*0a9764feSAndroid Build Coastguard Worker  */
16*0a9764feSAndroid Build Coastguard Worker 
17*0a9764feSAndroid Build Coastguard Worker #define LOG_TAG "drmhwc"
18*0a9764feSAndroid Build Coastguard Worker 
19*0a9764feSAndroid Build Coastguard Worker #include "BufferInfoImagination.h"
20*0a9764feSAndroid Build Coastguard Worker 
21*0a9764feSAndroid Build Coastguard Worker #include <xf86drm.h>
22*0a9764feSAndroid Build Coastguard Worker 
23*0a9764feSAndroid Build Coastguard Worker #include <cerrno>
24*0a9764feSAndroid Build Coastguard Worker 
25*0a9764feSAndroid Build Coastguard Worker #include "img_gralloc1_public.h"
26*0a9764feSAndroid Build Coastguard Worker #include "utils/log.h"
27*0a9764feSAndroid Build Coastguard Worker 
28*0a9764feSAndroid Build Coastguard Worker namespace android {
29*0a9764feSAndroid Build Coastguard Worker 
30*0a9764feSAndroid Build Coastguard Worker LEGACY_BUFFER_INFO_GETTER(BufferInfoImagination);
31*0a9764feSAndroid Build Coastguard Worker 
GetBoInfo(buffer_handle_t handle)32*0a9764feSAndroid Build Coastguard Worker auto BufferInfoImagination::GetBoInfo(buffer_handle_t handle)
33*0a9764feSAndroid Build Coastguard Worker     -> std::optional<BufferInfo> {
34*0a9764feSAndroid Build Coastguard Worker   auto *hnd = (IMG_native_handle_t *)handle;
35*0a9764feSAndroid Build Coastguard Worker   if (!hnd)
36*0a9764feSAndroid Build Coastguard Worker     return {};
37*0a9764feSAndroid Build Coastguard Worker 
38*0a9764feSAndroid Build Coastguard Worker   /* Extra bits are responsible for buffer compression and memory layout */
39*0a9764feSAndroid Build Coastguard Worker   if (hnd->iFormat & ~0x10f) {
40*0a9764feSAndroid Build Coastguard Worker     ALOGV("Special buffer formats are not supported");
41*0a9764feSAndroid Build Coastguard Worker     return {};
42*0a9764feSAndroid Build Coastguard Worker   }
43*0a9764feSAndroid Build Coastguard Worker 
44*0a9764feSAndroid Build Coastguard Worker   BufferInfo bi{};
45*0a9764feSAndroid Build Coastguard Worker 
46*0a9764feSAndroid Build Coastguard Worker   bi.width = hnd->iWidth;
47*0a9764feSAndroid Build Coastguard Worker   bi.height = hnd->iHeight;
48*0a9764feSAndroid Build Coastguard Worker   bi.prime_fds[0] = hnd->fd[0];
49*0a9764feSAndroid Build Coastguard Worker   bi.pitches[0] = ALIGN(hnd->iWidth, HW_ALIGN) * hnd->uiBpp >> 3;
50*0a9764feSAndroid Build Coastguard Worker 
51*0a9764feSAndroid Build Coastguard Worker   switch (hnd->iFormat) {
52*0a9764feSAndroid Build Coastguard Worker #ifdef HAL_PIXEL_FORMAT_BGRX_8888
53*0a9764feSAndroid Build Coastguard Worker     case HAL_PIXEL_FORMAT_BGRX_8888:
54*0a9764feSAndroid Build Coastguard Worker       bi.format = DRM_FORMAT_XRGB8888;
55*0a9764feSAndroid Build Coastguard Worker       break;
56*0a9764feSAndroid Build Coastguard Worker #endif
57*0a9764feSAndroid Build Coastguard Worker     default:
58*0a9764feSAndroid Build Coastguard Worker       bi.format = ConvertHalFormatToDrm(hnd->iFormat & 0xf);
59*0a9764feSAndroid Build Coastguard Worker       if (bi.format == DRM_FORMAT_INVALID) {
60*0a9764feSAndroid Build Coastguard Worker         ALOGV("Cannot convert hal format to drm format %u", hnd->iFormat);
61*0a9764feSAndroid Build Coastguard Worker         return {};
62*0a9764feSAndroid Build Coastguard Worker       }
63*0a9764feSAndroid Build Coastguard Worker   }
64*0a9764feSAndroid Build Coastguard Worker 
65*0a9764feSAndroid Build Coastguard Worker   return bi;
66*0a9764feSAndroid Build Coastguard Worker }
67*0a9764feSAndroid Build Coastguard Worker 
68*0a9764feSAndroid Build Coastguard Worker }  // namespace android
69