Path: blob/21.2-virgl/src/egl/drivers/dri2/platform_android_mapper.cpp
4570 views
/*1* Mesa 3-D graphics library2*3* Copyright (C) 2021 GlobalLogic Ukraine4* Copyright (C) 2021 Roman Stratiienko ([email protected])5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the "Software"),8* to deal in the Software without restriction, including without limitation9* the rights to use, copy, modify, merge, publish, distribute, sublicense,10* and/or sell copies of the Software, and to permit persons to whom the11* Software is furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice shall be included14* in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING21* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER22* DEALINGS IN THE SOFTWARE.23*/2425#include "platform_android.h"2627#include <system/window.h>28#include <aidl/android/hardware/graphics/common/ChromaSiting.h>29#include <aidl/android/hardware/graphics/common/Dataspace.h>30#include <aidl/android/hardware/graphics/common/ExtendableType.h>31#include <aidl/android/hardware/graphics/common/PlaneLayoutComponent.h>32#include <aidl/android/hardware/graphics/common/PlaneLayoutComponentType.h>33#include <gralloctypes/Gralloc4.h>3435using aidl::android::hardware::graphics::common::ChromaSiting;36using aidl::android::hardware::graphics::common::Dataspace;37using aidl::android::hardware::graphics::common::ExtendableType;38using aidl::android::hardware::graphics::common::PlaneLayout;39using aidl::android::hardware::graphics::common::PlaneLayoutComponent;40using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;41using android::hardware::graphics::common::V1_2::BufferUsage;42using android::hardware::graphics::mapper::V4_0::Error;43using android::hardware::graphics::mapper::V4_0::IMapper;44using android::hardware::hidl_handle;45using android::hardware::hidl_vec;46using MetadataType =47android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;4849Error50GetMetadata(android::sp<IMapper> mapper, const native_handle_t *buffer,51MetadataType type, hidl_vec<uint8_t>* metadata)52{53Error error = Error::NONE;5455auto native_handle = const_cast<native_handle_t*>(buffer);5657auto ret = mapper->get(native_handle, type,58[&](const auto& get_error, const auto& get_metadata) {59error = get_error;60*metadata = get_metadata;61});6263if (!ret.isOk())64error = Error::NO_RESOURCES;6566return error;67}6869std::optional<std::vector<PlaneLayout>> GetPlaneLayouts(70android::sp<IMapper> mapper, const native_handle_t *buffer)71{72hidl_vec<uint8_t> encoded_layouts;7374Error error = GetMetadata(mapper, buffer,75android::gralloc4::MetadataType_PlaneLayouts,76&encoded_layouts);7778if (error != Error::NONE)79return std::nullopt;8081std::vector<PlaneLayout> plane_layouts;8283auto status = android::gralloc4::decodePlaneLayouts(encoded_layouts, &plane_layouts);8485if (status != android::OK)86return std::nullopt;8788return plane_layouts;89}9091extern "C"92{9394int95mapper_metadata_get_buffer_info(struct ANativeWindowBuffer *buf,96struct buffer_info *out_buf_info)97{98static android::sp<IMapper> mapper = IMapper::getService();99struct buffer_info buf_info = *out_buf_info;100if (mapper == nullptr)101return -EINVAL;102103if (!buf->handle)104return -EINVAL;105106buf_info.width = buf->width;107buf_info.height = buf->height;108109hidl_vec<uint8_t> encoded_format;110auto err = GetMetadata(mapper, buf->handle, android::gralloc4::MetadataType_PixelFormatFourCC, &encoded_format);111if (err != Error::NONE)112return -EINVAL;113114auto status = android::gralloc4::decodePixelFormatFourCC(encoded_format, &buf_info.drm_fourcc);115if (status != android::OK)116return -EINVAL;117118hidl_vec<uint8_t> encoded_modifier;119err = GetMetadata(mapper, buf->handle, android::gralloc4::MetadataType_PixelFormatModifier, &encoded_modifier);120if (err != Error::NONE)121return -EINVAL;122123status = android::gralloc4::decodePixelFormatModifier(encoded_modifier, &buf_info.modifier);124if (status != android::OK)125return -EINVAL;126127auto layouts_opt = GetPlaneLayouts(mapper, buf->handle);128129if (!layouts_opt)130return -EINVAL;131132std::vector<PlaneLayout>& layouts = *layouts_opt;133134buf_info.num_planes = layouts.size();135136bool per_plane_unique_fd = buf->handle->numFds == buf_info.num_planes;137138for (uint32_t i = 0; i < layouts.size(); i++) {139buf_info.fds[i] = per_plane_unique_fd ? buf->handle->data[i] : buf->handle->data[0];140buf_info.pitches[i] = layouts[i].strideInBytes;141buf_info.offsets[i] = layouts[i].offsetInBytes;142}143144/* optional attributes */145hidl_vec<uint8_t> encoded_chroma_siting;146err = GetMetadata(mapper, buf->handle, android::gralloc4::MetadataType_ChromaSiting, &encoded_chroma_siting);147if (err == Error::NONE) {148ExtendableType chroma_siting_ext;149status = android::gralloc4::decodeChromaSiting(encoded_chroma_siting, &chroma_siting_ext);150if (status != android::OK)151return -EINVAL;152153ChromaSiting chroma_siting = android::gralloc4::getStandardChromaSitingValue(chroma_siting_ext);154switch (chroma_siting) {155case ChromaSiting::SITED_INTERSTITIAL:156buf_info.horizontal_siting = __DRI_YUV_CHROMA_SITING_0_5;157buf_info.vertical_siting = __DRI_YUV_CHROMA_SITING_0_5;158break;159case ChromaSiting::COSITED_HORIZONTAL:160buf_info.horizontal_siting = __DRI_YUV_CHROMA_SITING_0;161buf_info.vertical_siting = __DRI_YUV_CHROMA_SITING_0_5;162break;163default:164break;165}166}167168hidl_vec<uint8_t> encoded_dataspace;169err = GetMetadata(mapper, buf->handle, android::gralloc4:: MetadataType_Dataspace, &encoded_dataspace);170if (err == Error::NONE) {171Dataspace dataspace;172status = android::gralloc4::decodeDataspace(encoded_dataspace, &dataspace);173if (status != android::OK)174return -EINVAL;175176Dataspace standard = (Dataspace)((int)dataspace & (uint32_t)Dataspace::STANDARD_MASK);177switch (standard) {178case Dataspace::STANDARD_BT709:179buf_info.yuv_color_space = __DRI_YUV_COLOR_SPACE_ITU_REC709;180break;181case Dataspace::STANDARD_BT601_625:182case Dataspace::STANDARD_BT601_625_UNADJUSTED:183case Dataspace::STANDARD_BT601_525:184case Dataspace::STANDARD_BT601_525_UNADJUSTED:185buf_info.yuv_color_space = __DRI_YUV_COLOR_SPACE_ITU_REC601;186break;187case Dataspace::STANDARD_BT2020:188case Dataspace::STANDARD_BT2020_CONSTANT_LUMINANCE:189buf_info.yuv_color_space = __DRI_YUV_COLOR_SPACE_ITU_REC2020;190break;191default:192break;193}194195Dataspace range = (Dataspace)((int)dataspace & (uint32_t)Dataspace::RANGE_MASK);196switch (range) {197case Dataspace::RANGE_FULL:198buf_info.sample_range = __DRI_YUV_FULL_RANGE;199break;200case Dataspace::RANGE_LIMITED:201buf_info.sample_range = __DRI_YUV_NARROW_RANGE;202break;203default:204break;205}206}207208*out_buf_info = buf_info;209210return 0;211}212213} // extern "C"214215216