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 "BufferInfoGetter.h" 20*0a9764feSAndroid Build Coastguard Worker 21*0a9764feSAndroid Build Coastguard Worker #if __ANDROID_API__ >= 30 22*0a9764feSAndroid Build Coastguard Worker #include "BufferInfoMapperMetadata.h" 23*0a9764feSAndroid Build Coastguard Worker #endif 24*0a9764feSAndroid Build Coastguard Worker 25*0a9764feSAndroid Build Coastguard Worker #include <sys/stat.h> 26*0a9764feSAndroid Build Coastguard Worker #include <sys/types.h> 27*0a9764feSAndroid Build Coastguard Worker #include <unistd.h> 28*0a9764feSAndroid Build Coastguard Worker #include <xf86drm.h> 29*0a9764feSAndroid Build Coastguard Worker #include <xf86drmMode.h> 30*0a9764feSAndroid Build Coastguard Worker 31*0a9764feSAndroid Build Coastguard Worker #include <mutex> 32*0a9764feSAndroid Build Coastguard Worker 33*0a9764feSAndroid Build Coastguard Worker #include "utils/log.h" 34*0a9764feSAndroid Build Coastguard Worker #include "utils/properties.h" 35*0a9764feSAndroid Build Coastguard Worker 36*0a9764feSAndroid Build Coastguard Worker namespace android { 37*0a9764feSAndroid Build Coastguard Worker GetInstance()38*0a9764feSAndroid Build Coastguard WorkerBufferInfoGetter *BufferInfoGetter::GetInstance() { 39*0a9764feSAndroid Build Coastguard Worker static std::unique_ptr<BufferInfoGetter> inst; 40*0a9764feSAndroid Build Coastguard Worker if (!inst) { 41*0a9764feSAndroid Build Coastguard Worker #if __ANDROID_API__ >= 30 && defined(USE_IMAPPER4_METADATA_API) 42*0a9764feSAndroid Build Coastguard Worker inst.reset(BufferInfoMapperMetadata::CreateInstance()); 43*0a9764feSAndroid Build Coastguard Worker if (!inst) { 44*0a9764feSAndroid Build Coastguard Worker ALOGW( 45*0a9764feSAndroid Build Coastguard Worker "Generic buffer getter is not available. Falling back to legacy..."); 46*0a9764feSAndroid Build Coastguard Worker } 47*0a9764feSAndroid Build Coastguard Worker #endif 48*0a9764feSAndroid Build Coastguard Worker if (!inst) { 49*0a9764feSAndroid Build Coastguard Worker inst = LegacyBufferInfoGetter::CreateInstance(); 50*0a9764feSAndroid Build Coastguard Worker } 51*0a9764feSAndroid Build Coastguard Worker } 52*0a9764feSAndroid Build Coastguard Worker 53*0a9764feSAndroid Build Coastguard Worker return inst.get(); 54*0a9764feSAndroid Build Coastguard Worker } 55*0a9764feSAndroid Build Coastguard Worker GetUniqueId(buffer_handle_t handle)56*0a9764feSAndroid Build Coastguard Workerstd::optional<BufferUniqueId> BufferInfoGetter::GetUniqueId( 57*0a9764feSAndroid Build Coastguard Worker buffer_handle_t handle) { 58*0a9764feSAndroid Build Coastguard Worker struct stat sb {}; 59*0a9764feSAndroid Build Coastguard Worker if (fstat(handle->data[0], &sb) != 0) { 60*0a9764feSAndroid Build Coastguard Worker return {}; 61*0a9764feSAndroid Build Coastguard Worker } 62*0a9764feSAndroid Build Coastguard Worker 63*0a9764feSAndroid Build Coastguard Worker if (sb.st_size == 0) { 64*0a9764feSAndroid Build Coastguard Worker return {}; 65*0a9764feSAndroid Build Coastguard Worker } 66*0a9764feSAndroid Build Coastguard Worker 67*0a9764feSAndroid Build Coastguard Worker return static_cast<BufferUniqueId>(sb.st_ino); 68*0a9764feSAndroid Build Coastguard Worker } 69*0a9764feSAndroid Build Coastguard Worker Init()70*0a9764feSAndroid Build Coastguard Workerint LegacyBufferInfoGetter::Init() { 71*0a9764feSAndroid Build Coastguard Worker const int ret = hw_get_module( 72*0a9764feSAndroid Build Coastguard Worker GRALLOC_HARDWARE_MODULE_ID, 73*0a9764feSAndroid Build Coastguard Worker // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 74*0a9764feSAndroid Build Coastguard Worker reinterpret_cast<const hw_module_t **>(&gralloc_)); 75*0a9764feSAndroid Build Coastguard Worker if (ret != 0) { 76*0a9764feSAndroid Build Coastguard Worker ALOGE("Failed to open gralloc module"); 77*0a9764feSAndroid Build Coastguard Worker return ret; 78*0a9764feSAndroid Build Coastguard Worker } 79*0a9764feSAndroid Build Coastguard Worker 80*0a9764feSAndroid Build Coastguard Worker ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name, 81*0a9764feSAndroid Build Coastguard Worker gralloc_->common.author); 82*0a9764feSAndroid Build Coastguard Worker 83*0a9764feSAndroid Build Coastguard Worker return 0; 84*0a9764feSAndroid Build Coastguard Worker } 85*0a9764feSAndroid Build Coastguard Worker ConvertHalFormatToDrm(uint32_t hal_format)86*0a9764feSAndroid Build Coastguard Workeruint32_t LegacyBufferInfoGetter::ConvertHalFormatToDrm(uint32_t hal_format) { 87*0a9764feSAndroid Build Coastguard Worker switch (hal_format) { 88*0a9764feSAndroid Build Coastguard Worker case HAL_PIXEL_FORMAT_RGB_888: 89*0a9764feSAndroid Build Coastguard Worker return DRM_FORMAT_BGR888; 90*0a9764feSAndroid Build Coastguard Worker case HAL_PIXEL_FORMAT_BGRA_8888: 91*0a9764feSAndroid Build Coastguard Worker return DRM_FORMAT_ARGB8888; 92*0a9764feSAndroid Build Coastguard Worker case HAL_PIXEL_FORMAT_RGBX_8888: 93*0a9764feSAndroid Build Coastguard Worker return DRM_FORMAT_XBGR8888; 94*0a9764feSAndroid Build Coastguard Worker case HAL_PIXEL_FORMAT_RGBA_8888: 95*0a9764feSAndroid Build Coastguard Worker return DRM_FORMAT_ABGR8888; 96*0a9764feSAndroid Build Coastguard Worker case HAL_PIXEL_FORMAT_RGB_565: 97*0a9764feSAndroid Build Coastguard Worker return DRM_FORMAT_BGR565; 98*0a9764feSAndroid Build Coastguard Worker case HAL_PIXEL_FORMAT_YV12: 99*0a9764feSAndroid Build Coastguard Worker return DRM_FORMAT_YVU420; 100*0a9764feSAndroid Build Coastguard Worker case HAL_PIXEL_FORMAT_RGBA_1010102: 101*0a9764feSAndroid Build Coastguard Worker return DRM_FORMAT_ABGR2101010; 102*0a9764feSAndroid Build Coastguard Worker default: 103*0a9764feSAndroid Build Coastguard Worker ALOGE("Cannot convert hal format to drm format %u", hal_format); 104*0a9764feSAndroid Build Coastguard Worker return DRM_FORMAT_INVALID; 105*0a9764feSAndroid Build Coastguard Worker } 106*0a9764feSAndroid Build Coastguard Worker } 107*0a9764feSAndroid Build Coastguard Worker IsDrmFormatRgb(uint32_t drm_format)108*0a9764feSAndroid Build Coastguard Workerbool BufferInfoGetter::IsDrmFormatRgb(uint32_t drm_format) { 109*0a9764feSAndroid Build Coastguard Worker switch (drm_format) { 110*0a9764feSAndroid Build Coastguard Worker case DRM_FORMAT_ARGB8888: 111*0a9764feSAndroid Build Coastguard Worker case DRM_FORMAT_XBGR8888: 112*0a9764feSAndroid Build Coastguard Worker case DRM_FORMAT_ABGR8888: 113*0a9764feSAndroid Build Coastguard Worker case DRM_FORMAT_BGR888: 114*0a9764feSAndroid Build Coastguard Worker case DRM_FORMAT_BGR565: 115*0a9764feSAndroid Build Coastguard Worker case DRM_FORMAT_ABGR2101010: 116*0a9764feSAndroid Build Coastguard Worker return true; 117*0a9764feSAndroid Build Coastguard Worker default: 118*0a9764feSAndroid Build Coastguard Worker return false; 119*0a9764feSAndroid Build Coastguard Worker } 120*0a9764feSAndroid Build Coastguard Worker } 121*0a9764feSAndroid Build Coastguard Worker 122*0a9764feSAndroid Build Coastguard Worker __attribute__((weak)) std::unique_ptr<LegacyBufferInfoGetter> CreateInstance()123*0a9764feSAndroid Build Coastguard WorkerLegacyBufferInfoGetter::CreateInstance() { 124*0a9764feSAndroid Build Coastguard Worker ALOGE("No legacy buffer info getters available"); 125*0a9764feSAndroid Build Coastguard Worker return nullptr; 126*0a9764feSAndroid Build Coastguard Worker } 127*0a9764feSAndroid Build Coastguard Worker 128*0a9764feSAndroid Build Coastguard Worker } // namespace android 129