132a6e48fSopenharmony_ci/* 232a6e48fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 332a6e48fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 432a6e48fSopenharmony_ci * you may not use this file except in compliance with the License. 532a6e48fSopenharmony_ci * You may obtain a copy of the License at 632a6e48fSopenharmony_ci * 732a6e48fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 832a6e48fSopenharmony_ci * 932a6e48fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1032a6e48fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1132a6e48fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1232a6e48fSopenharmony_ci * See the License for the specific language governing permissions and 1332a6e48fSopenharmony_ci * limitations under the License. 1432a6e48fSopenharmony_ci */ 1532a6e48fSopenharmony_ci 1632a6e48fSopenharmony_ci#include "native_buffer_inner.h" 1732a6e48fSopenharmony_ci 1832a6e48fSopenharmony_ci#include <cinttypes> 1932a6e48fSopenharmony_ci#include "surface_type.h" 2032a6e48fSopenharmony_ci#include "buffer_log.h" 2132a6e48fSopenharmony_ci#include "native_window.h" 2232a6e48fSopenharmony_ci#include "surface_buffer_impl.h" 2332a6e48fSopenharmony_ci#include "metadata_helper.h" 2432a6e48fSopenharmony_ci 2532a6e48fSopenharmony_ciusing namespace OHOS; 2632a6e48fSopenharmony_ciusing namespace HDI::Display::Graphic::Common::V1_0; 2732a6e48fSopenharmony_cistatic std::unordered_map<OH_NativeBuffer_ColorSpace, CM_ColorSpaceType> NATIVE_COLORSPACE_TO_HDI_MAP = { 2832a6e48fSopenharmony_ci {OH_COLORSPACE_NONE, CM_COLORSPACE_NONE}, 2932a6e48fSopenharmony_ci {OH_COLORSPACE_BT601_EBU_FULL, CM_BT601_EBU_FULL}, 3032a6e48fSopenharmony_ci {OH_COLORSPACE_BT601_SMPTE_C_FULL, CM_BT601_SMPTE_C_FULL}, 3132a6e48fSopenharmony_ci {OH_COLORSPACE_BT709_FULL, CM_BT709_FULL}, 3232a6e48fSopenharmony_ci {OH_COLORSPACE_BT2020_HLG_FULL, CM_BT2020_HLG_FULL}, 3332a6e48fSopenharmony_ci {OH_COLORSPACE_BT2020_PQ_FULL, CM_BT2020_PQ_FULL}, 3432a6e48fSopenharmony_ci {OH_COLORSPACE_BT601_EBU_LIMIT, CM_BT601_EBU_LIMIT}, 3532a6e48fSopenharmony_ci {OH_COLORSPACE_BT601_SMPTE_C_LIMIT, CM_BT601_SMPTE_C_LIMIT}, 3632a6e48fSopenharmony_ci {OH_COLORSPACE_BT709_LIMIT, CM_BT709_LIMIT}, 3732a6e48fSopenharmony_ci {OH_COLORSPACE_BT2020_HLG_LIMIT, CM_BT2020_HLG_LIMIT}, 3832a6e48fSopenharmony_ci {OH_COLORSPACE_BT2020_PQ_LIMIT, CM_BT2020_PQ_LIMIT}, 3932a6e48fSopenharmony_ci {OH_COLORSPACE_SRGB_FULL, CM_SRGB_FULL}, 4032a6e48fSopenharmony_ci {OH_COLORSPACE_P3_FULL, CM_P3_FULL}, 4132a6e48fSopenharmony_ci {OH_COLORSPACE_P3_HLG_FULL, CM_P3_HLG_FULL}, 4232a6e48fSopenharmony_ci {OH_COLORSPACE_P3_PQ_FULL, CM_P3_PQ_FULL}, 4332a6e48fSopenharmony_ci {OH_COLORSPACE_ADOBERGB_FULL, CM_ADOBERGB_FULL}, 4432a6e48fSopenharmony_ci {OH_COLORSPACE_SRGB_LIMIT, CM_SRGB_LIMIT}, 4532a6e48fSopenharmony_ci {OH_COLORSPACE_P3_LIMIT, CM_P3_LIMIT}, 4632a6e48fSopenharmony_ci {OH_COLORSPACE_P3_HLG_LIMIT, CM_P3_HLG_LIMIT}, 4732a6e48fSopenharmony_ci {OH_COLORSPACE_P3_PQ_LIMIT, CM_P3_PQ_LIMIT}, 4832a6e48fSopenharmony_ci {OH_COLORSPACE_ADOBERGB_LIMIT, CM_ADOBERGB_LIMIT}, 4932a6e48fSopenharmony_ci {OH_COLORSPACE_LINEAR_SRGB, CM_LINEAR_SRGB}, 5032a6e48fSopenharmony_ci {OH_COLORSPACE_LINEAR_BT709, CM_LINEAR_BT709}, 5132a6e48fSopenharmony_ci {OH_COLORSPACE_LINEAR_P3, CM_LINEAR_P3}, 5232a6e48fSopenharmony_ci {OH_COLORSPACE_LINEAR_BT2020, CM_LINEAR_BT2020}, 5332a6e48fSopenharmony_ci {OH_COLORSPACE_DISPLAY_SRGB, CM_DISPLAY_SRGB}, 5432a6e48fSopenharmony_ci {OH_COLORSPACE_DISPLAY_P3_SRGB, CM_DISPLAY_P3_SRGB}, 5532a6e48fSopenharmony_ci {OH_COLORSPACE_DISPLAY_P3_HLG, CM_DISPLAY_P3_HLG}, 5632a6e48fSopenharmony_ci {OH_COLORSPACE_DISPLAY_P3_PQ, CM_DISPLAY_P3_PQ}, 5732a6e48fSopenharmony_ci {OH_COLORSPACE_DISPLAY_BT2020_SRGB, CM_DISPLAY_BT2020_SRGB}, 5832a6e48fSopenharmony_ci {OH_COLORSPACE_DISPLAY_BT2020_HLG, CM_DISPLAY_BT2020_HLG}, 5932a6e48fSopenharmony_ci {OH_COLORSPACE_DISPLAY_BT2020_PQ, CM_DISPLAY_BT2020_PQ} 6032a6e48fSopenharmony_ci}; 6132a6e48fSopenharmony_ci 6232a6e48fSopenharmony_cistatic std::unordered_map<OH_NativeBuffer_MetadataType, CM_HDR_Metadata_Type> NATIVE_METADATATYPE_TO_HDI_MAP = { 6332a6e48fSopenharmony_ci {OH_VIDEO_HDR_HLG, CM_VIDEO_HLG}, 6432a6e48fSopenharmony_ci {OH_VIDEO_HDR_HDR10, CM_VIDEO_HDR10}, 6532a6e48fSopenharmony_ci {OH_VIDEO_HDR_VIVID, CM_VIDEO_HDR_VIVID}, 6632a6e48fSopenharmony_ci}; 6732a6e48fSopenharmony_ci 6832a6e48fSopenharmony_cistatic OH_NativeBuffer* OH_NativeBufferFromSurfaceBuffer(SurfaceBuffer* buffer) 6932a6e48fSopenharmony_ci{ 7032a6e48fSopenharmony_ci if (buffer == nullptr) { 7132a6e48fSopenharmony_ci return nullptr; 7232a6e48fSopenharmony_ci } 7332a6e48fSopenharmony_ci return buffer->SurfaceBufferToNativeBuffer(); 7432a6e48fSopenharmony_ci} 7532a6e48fSopenharmony_ci 7632a6e48fSopenharmony_cistatic SurfaceBuffer* OH_NativeBufferToSurfaceBuffer(OH_NativeBuffer *buffer) 7732a6e48fSopenharmony_ci{ 7832a6e48fSopenharmony_ci return SurfaceBuffer::NativeBufferToSurfaceBuffer(buffer); 7932a6e48fSopenharmony_ci} 8032a6e48fSopenharmony_ci 8132a6e48fSopenharmony_cistatic const SurfaceBuffer* OH_NativeBufferToSurfaceBuffer(const OH_NativeBuffer *buffer) 8232a6e48fSopenharmony_ci{ 8332a6e48fSopenharmony_ci return SurfaceBuffer::NativeBufferToSurfaceBuffer(buffer); 8432a6e48fSopenharmony_ci} 8532a6e48fSopenharmony_ci 8632a6e48fSopenharmony_ciOH_NativeBuffer* OH_NativeBuffer_Alloc(const OH_NativeBuffer_Config* config) 8732a6e48fSopenharmony_ci{ 8832a6e48fSopenharmony_ci if (config == nullptr) { 8932a6e48fSopenharmony_ci return nullptr; 9032a6e48fSopenharmony_ci } 9132a6e48fSopenharmony_ci BufferRequestConfig bfConfig = {}; 9232a6e48fSopenharmony_ci bfConfig.width = config->width; 9332a6e48fSopenharmony_ci bfConfig.height = config->height; 9432a6e48fSopenharmony_ci bfConfig.strideAlignment = 0x8; // set 0x8 as default value to alloc SurfaceBufferImpl 9532a6e48fSopenharmony_ci bfConfig.format = config->format; // PixelFormat 9632a6e48fSopenharmony_ci bfConfig.usage = config->usage; 9732a6e48fSopenharmony_ci bfConfig.timeout = 0; 9832a6e48fSopenharmony_ci bfConfig.colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB; 9932a6e48fSopenharmony_ci bfConfig.transform = GraphicTransformType::GRAPHIC_ROTATE_NONE; 10032a6e48fSopenharmony_ci sptr<SurfaceBuffer> bufferImpl = new SurfaceBufferImpl(); 10132a6e48fSopenharmony_ci GSError ret = bufferImpl->Alloc(bfConfig); 10232a6e48fSopenharmony_ci if (ret != OHOS::SURFACE_ERROR_OK) { 10332a6e48fSopenharmony_ci BLOGE("Alloc failed ret: %{public}d, config info: width[%{public}d, height[%{public}d," 10432a6e48fSopenharmony_ci "format[%{public}d], usage[%{public}d]", ret, config->width, config->height, 10532a6e48fSopenharmony_ci config->format, config->usage); 10632a6e48fSopenharmony_ci return nullptr; 10732a6e48fSopenharmony_ci } 10832a6e48fSopenharmony_ci 10932a6e48fSopenharmony_ci OH_NativeBuffer* buffer = OH_NativeBufferFromSurfaceBuffer(bufferImpl); 11032a6e48fSopenharmony_ci int32_t err = OH_NativeBuffer_Reference(buffer); 11132a6e48fSopenharmony_ci if (err != OHOS::SURFACE_ERROR_OK) { 11232a6e48fSopenharmony_ci BLOGE("NativeBufferReference failed, err: %{public}d.", err); 11332a6e48fSopenharmony_ci return nullptr; 11432a6e48fSopenharmony_ci } 11532a6e48fSopenharmony_ci return buffer; 11632a6e48fSopenharmony_ci} 11732a6e48fSopenharmony_ci 11832a6e48fSopenharmony_ciint32_t OH_NativeBuffer_Reference(OH_NativeBuffer *buffer) 11932a6e48fSopenharmony_ci{ 12032a6e48fSopenharmony_ci if (buffer == nullptr) { 12132a6e48fSopenharmony_ci return OHOS::GSERROR_INVALID_ARGUMENTS; 12232a6e48fSopenharmony_ci } 12332a6e48fSopenharmony_ci OHOS::RefBase *ref = reinterpret_cast<OHOS::RefBase *>(buffer); 12432a6e48fSopenharmony_ci ref->IncStrongRef(ref); 12532a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 12632a6e48fSopenharmony_ci} 12732a6e48fSopenharmony_ci 12832a6e48fSopenharmony_ciint32_t OH_NativeBuffer_Unreference(OH_NativeBuffer *buffer) 12932a6e48fSopenharmony_ci{ 13032a6e48fSopenharmony_ci if (buffer == nullptr) { 13132a6e48fSopenharmony_ci return OHOS::GSERROR_INVALID_ARGUMENTS; 13232a6e48fSopenharmony_ci } 13332a6e48fSopenharmony_ci OHOS::RefBase *ref = reinterpret_cast<OHOS::RefBase *>(buffer); 13432a6e48fSopenharmony_ci ref->DecStrongRef(ref); 13532a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 13632a6e48fSopenharmony_ci} 13732a6e48fSopenharmony_ci 13832a6e48fSopenharmony_civoid OH_NativeBuffer_GetConfig(OH_NativeBuffer *buffer, OH_NativeBuffer_Config* config) 13932a6e48fSopenharmony_ci{ 14032a6e48fSopenharmony_ci if (buffer == nullptr || config == nullptr) { 14132a6e48fSopenharmony_ci return; 14232a6e48fSopenharmony_ci } 14332a6e48fSopenharmony_ci const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 14432a6e48fSopenharmony_ci config->width = sbuffer->GetWidth(); 14532a6e48fSopenharmony_ci config->height = sbuffer->GetHeight(); 14632a6e48fSopenharmony_ci config->format = sbuffer->GetFormat(); 14732a6e48fSopenharmony_ci config->usage = sbuffer->GetUsage(); 14832a6e48fSopenharmony_ci config->stride = sbuffer->GetStride(); 14932a6e48fSopenharmony_ci} 15032a6e48fSopenharmony_ci 15132a6e48fSopenharmony_ciint32_t OH_NativeBuffer_Map(OH_NativeBuffer *buffer, void **virAddr) 15232a6e48fSopenharmony_ci{ 15332a6e48fSopenharmony_ci if (buffer == nullptr || virAddr == nullptr) { 15432a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_INVALID_PARAM; 15532a6e48fSopenharmony_ci } 15632a6e48fSopenharmony_ci SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 15732a6e48fSopenharmony_ci int32_t ret = sbuffer->Map(); 15832a6e48fSopenharmony_ci if (ret == OHOS::SURFACE_ERROR_OK) { 15932a6e48fSopenharmony_ci *virAddr = sbuffer->GetVirAddr(); 16032a6e48fSopenharmony_ci } else { 16132a6e48fSopenharmony_ci BLOGE("Map failed, ret:%{public}d", ret); 16232a6e48fSopenharmony_ci ret = OHOS::SURFACE_ERROR_UNKOWN; 16332a6e48fSopenharmony_ci } 16432a6e48fSopenharmony_ci return ret; 16532a6e48fSopenharmony_ci} 16632a6e48fSopenharmony_ci 16732a6e48fSopenharmony_ciint32_t OH_NativeBuffer_Unmap(OH_NativeBuffer *buffer) 16832a6e48fSopenharmony_ci{ 16932a6e48fSopenharmony_ci if (buffer == nullptr) { 17032a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_INVALID_PARAM; 17132a6e48fSopenharmony_ci } 17232a6e48fSopenharmony_ci SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 17332a6e48fSopenharmony_ci int32_t ret = sbuffer->Unmap(); 17432a6e48fSopenharmony_ci if (ret != OHOS::SURFACE_ERROR_OK) { 17532a6e48fSopenharmony_ci BLOGE("Unmap failed, ret:%{public}d", ret); 17632a6e48fSopenharmony_ci ret = OHOS::SURFACE_ERROR_UNKOWN; 17732a6e48fSopenharmony_ci } 17832a6e48fSopenharmony_ci return ret; 17932a6e48fSopenharmony_ci} 18032a6e48fSopenharmony_ci 18132a6e48fSopenharmony_ciuint32_t OH_NativeBuffer_GetSeqNum(OH_NativeBuffer *buffer) 18232a6e48fSopenharmony_ci{ 18332a6e48fSopenharmony_ci if (buffer == nullptr) { 18432a6e48fSopenharmony_ci return UINT_MAX; 18532a6e48fSopenharmony_ci } 18632a6e48fSopenharmony_ci const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 18732a6e48fSopenharmony_ci return sbuffer->GetSeqNum(); 18832a6e48fSopenharmony_ci} 18932a6e48fSopenharmony_ci 19032a6e48fSopenharmony_ciconst BufferHandle* OH_NativeBuffer_GetBufferHandle(const OH_NativeBuffer *buffer) 19132a6e48fSopenharmony_ci{ 19232a6e48fSopenharmony_ci if (buffer == nullptr) { 19332a6e48fSopenharmony_ci return nullptr; 19432a6e48fSopenharmony_ci } 19532a6e48fSopenharmony_ci const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 19632a6e48fSopenharmony_ci return sbuffer->GetBufferHandle(); 19732a6e48fSopenharmony_ci} 19832a6e48fSopenharmony_ci 19932a6e48fSopenharmony_civoid OH_NativeBuffer_GetNativeBufferConfig(const OH_NativeBuffer *buffer, OH_NativeBuffer_Config* config) 20032a6e48fSopenharmony_ci{ 20132a6e48fSopenharmony_ci if (buffer == nullptr || config == nullptr) { 20232a6e48fSopenharmony_ci return; 20332a6e48fSopenharmony_ci } 20432a6e48fSopenharmony_ci const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 20532a6e48fSopenharmony_ci config->width = sbuffer->GetWidth(); 20632a6e48fSopenharmony_ci config->height = sbuffer->GetHeight(); 20732a6e48fSopenharmony_ci config->format = sbuffer->GetFormat(); 20832a6e48fSopenharmony_ci config->usage = sbuffer->GetUsage(); 20932a6e48fSopenharmony_ci config->stride = sbuffer->GetStride(); 21032a6e48fSopenharmony_ci} 21132a6e48fSopenharmony_ci 21232a6e48fSopenharmony_ciOH_NativeBuffer* OH_NativeBufferFromNativeWindowBuffer(OHNativeWindowBuffer* nativeWindowBuffer) 21332a6e48fSopenharmony_ci{ 21432a6e48fSopenharmony_ci if (nativeWindowBuffer == nullptr) { 21532a6e48fSopenharmony_ci return nullptr; 21632a6e48fSopenharmony_ci } 21732a6e48fSopenharmony_ci OH_NativeBuffer* buffer = OH_NativeBufferFromSurfaceBuffer(nativeWindowBuffer->sfbuffer); 21832a6e48fSopenharmony_ci return buffer; 21932a6e48fSopenharmony_ci} 22032a6e48fSopenharmony_ci 22132a6e48fSopenharmony_ciint32_t OH_NativeBuffer_SetColorSpace(OH_NativeBuffer *buffer, OH_NativeBuffer_ColorSpace colorSpace) 22232a6e48fSopenharmony_ci{ 22332a6e48fSopenharmony_ci if (buffer == nullptr || NATIVE_COLORSPACE_TO_HDI_MAP.find(colorSpace) == NATIVE_COLORSPACE_TO_HDI_MAP.end()) { 22432a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_INVALID_PARAM; 22532a6e48fSopenharmony_ci } 22632a6e48fSopenharmony_ci sptr<SurfaceBuffer> sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 22732a6e48fSopenharmony_ci GSError ret = MetadataHelper::SetColorSpaceType(sbuffer, NATIVE_COLORSPACE_TO_HDI_MAP[colorSpace]); 22832a6e48fSopenharmony_ci if (ret == OHOS::GSERROR_HDI_ERROR) { 22932a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_NOT_SUPPORT; 23032a6e48fSopenharmony_ci } else if (ret != OHOS::SURFACE_ERROR_OK) { 23132a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 23232a6e48fSopenharmony_ci } 23332a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 23432a6e48fSopenharmony_ci} 23532a6e48fSopenharmony_ci 23632a6e48fSopenharmony_ciint32_t OH_NativeBuffer_MapPlanes(OH_NativeBuffer *buffer, void **virAddr, OH_NativeBuffer_Planes *outPlanes) 23732a6e48fSopenharmony_ci{ 23832a6e48fSopenharmony_ci if (buffer == nullptr || virAddr == nullptr || outPlanes == nullptr) { 23932a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_INVALID_PARAM; 24032a6e48fSopenharmony_ci } 24132a6e48fSopenharmony_ci sptr<SurfaceBuffer> sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 24232a6e48fSopenharmony_ci int32_t ret = sbuffer->Map(); 24332a6e48fSopenharmony_ci if (ret == OHOS::SURFACE_ERROR_OK) { 24432a6e48fSopenharmony_ci *virAddr = sbuffer->GetVirAddr(); 24532a6e48fSopenharmony_ci } else { 24632a6e48fSopenharmony_ci BLOGE("Map failed, %{public}d", ret); 24732a6e48fSopenharmony_ci return ret; 24832a6e48fSopenharmony_ci } 24932a6e48fSopenharmony_ci OH_NativeBuffer_Planes *planes = nullptr; 25032a6e48fSopenharmony_ci GSError retVal = sbuffer->GetPlanesInfo(reinterpret_cast<void**>(&planes)); 25132a6e48fSopenharmony_ci if (retVal != OHOS::SURFACE_ERROR_OK) { 25232a6e48fSopenharmony_ci BLOGE("GetPlanesInfo failed, retVal:%{public}d", retVal); 25332a6e48fSopenharmony_ci return retVal; 25432a6e48fSopenharmony_ci } 25532a6e48fSopenharmony_ci outPlanes->planeCount = planes->planeCount; 25632a6e48fSopenharmony_ci for (uint32_t i = 0; i < planes->planeCount && i < 4; i++) { // 4: max plane count 25732a6e48fSopenharmony_ci outPlanes->planes[i].offset = planes->planes[i].offset; 25832a6e48fSopenharmony_ci outPlanes->planes[i].rowStride = planes->planes[i].rowStride; 25932a6e48fSopenharmony_ci outPlanes->planes[i].columnStride = planes->planes[i].columnStride; 26032a6e48fSopenharmony_ci } 26132a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 26232a6e48fSopenharmony_ci} 26332a6e48fSopenharmony_ci 26432a6e48fSopenharmony_ciint32_t OH_NativeBuffer_FromNativeWindowBuffer(OHNativeWindowBuffer *nativeWindowBuffer, OH_NativeBuffer **buffer) 26532a6e48fSopenharmony_ci{ 26632a6e48fSopenharmony_ci if (nativeWindowBuffer == nullptr || buffer == nullptr) { 26732a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_INVALID_PARAM; 26832a6e48fSopenharmony_ci } 26932a6e48fSopenharmony_ci *buffer = OH_NativeBufferFromSurfaceBuffer(nativeWindowBuffer->sfbuffer); 27032a6e48fSopenharmony_ci if (*buffer == nullptr) { 27132a6e48fSopenharmony_ci BLOGE("get sfbuffer is nullptr"); 27232a6e48fSopenharmony_ci return OHOS::GSERROR_INVALID_OPERATING; 27332a6e48fSopenharmony_ci } 27432a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 27532a6e48fSopenharmony_ci} 27632a6e48fSopenharmony_ci 27732a6e48fSopenharmony_ciint32_t OH_NativeBuffer_GetColorSpace(OH_NativeBuffer *buffer, OH_NativeBuffer_ColorSpace *colorSpace) 27832a6e48fSopenharmony_ci{ 27932a6e48fSopenharmony_ci if (buffer == nullptr || colorSpace == nullptr) { 28032a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_INVALID_PARAM; 28132a6e48fSopenharmony_ci } 28232a6e48fSopenharmony_ci sptr<SurfaceBuffer> sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 28332a6e48fSopenharmony_ci OHOS::HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType colorSpaceType; 28432a6e48fSopenharmony_ci GSError ret = MetadataHelper::GetColorSpaceType(sbuffer, colorSpaceType); 28532a6e48fSopenharmony_ci if (ret == OHOS::GSERROR_HDI_ERROR) { 28632a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_NOT_SUPPORT; 28732a6e48fSopenharmony_ci } else if (ret != OHOS::SURFACE_ERROR_OK) { 28832a6e48fSopenharmony_ci BLOGE("GetColorSpaceType failed!, retVal:%{public}d", ret); 28932a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 29032a6e48fSopenharmony_ci } 29132a6e48fSopenharmony_ci auto it = std::find_if(NATIVE_COLORSPACE_TO_HDI_MAP.begin(), NATIVE_COLORSPACE_TO_HDI_MAP.end(), 29232a6e48fSopenharmony_ci [colorSpaceType](const std::pair<OH_NativeBuffer_ColorSpace, CM_ColorSpaceType>& element) { 29332a6e48fSopenharmony_ci return element.second == colorSpaceType; 29432a6e48fSopenharmony_ci }); 29532a6e48fSopenharmony_ci if (it != NATIVE_COLORSPACE_TO_HDI_MAP.end()) { 29632a6e48fSopenharmony_ci *colorSpace = it->first; 29732a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 29832a6e48fSopenharmony_ci } 29932a6e48fSopenharmony_ci BLOGE("the colorSpace does not support it."); 30032a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 30132a6e48fSopenharmony_ci} 30232a6e48fSopenharmony_ci 30332a6e48fSopenharmony_ciint32_t OH_NativeBuffer_SetMetadataValue(OH_NativeBuffer *buffer, OH_NativeBuffer_MetadataKey metadataKey, 30432a6e48fSopenharmony_ci int32_t size, uint8_t *metadata) 30532a6e48fSopenharmony_ci{ 30632a6e48fSopenharmony_ci if (buffer == nullptr || metadata == nullptr || size <= 0) { 30732a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_INVALID_PARAM; 30832a6e48fSopenharmony_ci } 30932a6e48fSopenharmony_ci sptr<SurfaceBuffer> sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 31032a6e48fSopenharmony_ci GSError ret = GSERROR_OK; 31132a6e48fSopenharmony_ci std::vector<uint8_t> mD(metadata, metadata + size); 31232a6e48fSopenharmony_ci if (metadataKey == OH_HDR_DYNAMIC_METADATA) { 31332a6e48fSopenharmony_ci ret = MetadataHelper::SetHDRDynamicMetadata(sbuffer, mD); 31432a6e48fSopenharmony_ci } else if (metadataKey == OH_HDR_STATIC_METADATA) { 31532a6e48fSopenharmony_ci ret = MetadataHelper::SetHDRStaticMetadata(sbuffer, mD); 31632a6e48fSopenharmony_ci } else if (metadataKey == OH_HDR_METADATA_TYPE) { 31732a6e48fSopenharmony_ci OH_NativeBuffer_MetadataType hdrMetadataType = static_cast<OH_NativeBuffer_MetadataType>(*metadata); 31832a6e48fSopenharmony_ci ret = MetadataHelper::SetHDRMetadataType(sbuffer, NATIVE_METADATATYPE_TO_HDI_MAP[hdrMetadataType]); 31932a6e48fSopenharmony_ci } else { 32032a6e48fSopenharmony_ci BLOGE("the metadataKey does not support it."); 32132a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 32232a6e48fSopenharmony_ci } 32332a6e48fSopenharmony_ci if (ret == OHOS::GSERROR_HDI_ERROR) { 32432a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_NOT_SUPPORT; 32532a6e48fSopenharmony_ci } else if (ret != OHOS::SURFACE_ERROR_OK) { 32632a6e48fSopenharmony_ci BLOGE("SetHDRMetadata failed!, retVal:%{public}d", ret); 32732a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 32832a6e48fSopenharmony_ci } 32932a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 33032a6e48fSopenharmony_ci} 33132a6e48fSopenharmony_ci 33232a6e48fSopenharmony_cistatic GSError OH_NativeBuffer_GetMatedataValueType(sptr<SurfaceBuffer> sbuffer, int32_t *size, uint8_t **metadata) 33332a6e48fSopenharmony_ci{ 33432a6e48fSopenharmony_ci CM_HDR_Metadata_Type hdrMetadataType = CM_METADATA_NONE; 33532a6e48fSopenharmony_ci GSError ret = MetadataHelper::GetHDRMetadataType(sbuffer, hdrMetadataType); 33632a6e48fSopenharmony_ci if (ret == OHOS::GSERROR_HDI_ERROR) { 33732a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_NOT_SUPPORT; 33832a6e48fSopenharmony_ci } else if (ret != OHOS::SURFACE_ERROR_OK) { 33932a6e48fSopenharmony_ci BLOGE("GetHDRMetadataType failed!, ret: %{public}d", ret); 34032a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 34132a6e48fSopenharmony_ci } 34232a6e48fSopenharmony_ci auto it = std::find_if(NATIVE_METADATATYPE_TO_HDI_MAP.begin(), NATIVE_METADATATYPE_TO_HDI_MAP.end(), 34332a6e48fSopenharmony_ci [hdrMetadataType](const std::pair<OH_NativeBuffer_MetadataType, CM_HDR_Metadata_Type>& element) { 34432a6e48fSopenharmony_ci return element.second == hdrMetadataType; 34532a6e48fSopenharmony_ci }); 34632a6e48fSopenharmony_ci if (it != NATIVE_METADATATYPE_TO_HDI_MAP.end()) { 34732a6e48fSopenharmony_ci *size = sizeof(OH_NativeBuffer_MetadataType); 34832a6e48fSopenharmony_ci *metadata = new uint8_t[*size]; 34932a6e48fSopenharmony_ci errno_t err = memcpy_s(*metadata, *size, &(it->first), *size); 35032a6e48fSopenharmony_ci if (err != 0) { 35132a6e48fSopenharmony_ci delete[] *metadata; 35232a6e48fSopenharmony_ci *metadata = nullptr; 35332a6e48fSopenharmony_ci BLOGE("memcpy_s failed!, ret: %{public}d", err); 35432a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 35532a6e48fSopenharmony_ci } 35632a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 35732a6e48fSopenharmony_ci } 35832a6e48fSopenharmony_ci BLOGE("the hdrMetadataType does not support it."); 35932a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_NOT_SUPPORT; 36032a6e48fSopenharmony_ci} 36132a6e48fSopenharmony_ci 36232a6e48fSopenharmony_ciint32_t OH_NativeBuffer_GetMetadataValue(OH_NativeBuffer *buffer, OH_NativeBuffer_MetadataKey metadataKey, 36332a6e48fSopenharmony_ci int32_t *size, uint8_t **metadata) 36432a6e48fSopenharmony_ci{ 36532a6e48fSopenharmony_ci if (buffer == nullptr || metadata == nullptr || size == nullptr) { 36632a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_INVALID_PARAM; 36732a6e48fSopenharmony_ci } 36832a6e48fSopenharmony_ci sptr<SurfaceBuffer> sbuffer = OH_NativeBufferToSurfaceBuffer(buffer); 36932a6e48fSopenharmony_ci GSError ret = GSERROR_OK; 37032a6e48fSopenharmony_ci std::vector<uint8_t> mD; 37132a6e48fSopenharmony_ci if (metadataKey == OH_HDR_DYNAMIC_METADATA) { 37232a6e48fSopenharmony_ci ret = MetadataHelper::GetHDRDynamicMetadata(sbuffer, mD); 37332a6e48fSopenharmony_ci } else if (metadataKey == OH_HDR_STATIC_METADATA) { 37432a6e48fSopenharmony_ci ret = MetadataHelper::GetHDRStaticMetadata(sbuffer, mD); 37532a6e48fSopenharmony_ci } else if (metadataKey == OH_HDR_METADATA_TYPE) { 37632a6e48fSopenharmony_ci ret = OH_NativeBuffer_GetMatedataValueType(sbuffer, size, metadata); 37732a6e48fSopenharmony_ci return ret; 37832a6e48fSopenharmony_ci } else { 37932a6e48fSopenharmony_ci BLOGE("the metadataKey does not support it."); 38032a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 38132a6e48fSopenharmony_ci } 38232a6e48fSopenharmony_ci if (ret == OHOS::GSERROR_HDI_ERROR) { 38332a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_NOT_SUPPORT; 38432a6e48fSopenharmony_ci } else if (ret != OHOS::SURFACE_ERROR_OK) { 38532a6e48fSopenharmony_ci BLOGE("SetHDRSMetadata failed!, ret: %{public}d", ret); 38632a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 38732a6e48fSopenharmony_ci } 38832a6e48fSopenharmony_ci *size = mD.size(); 38932a6e48fSopenharmony_ci if (mD.empty()) { 39032a6e48fSopenharmony_ci BLOGE("Metadata is empty!"); 39132a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 39232a6e48fSopenharmony_ci } 39332a6e48fSopenharmony_ci *metadata = new uint8_t[mD.size()]; 39432a6e48fSopenharmony_ci errno_t err = memcpy_s(*metadata, mD.size(), &mD[0], mD.size()); 39532a6e48fSopenharmony_ci if (err != 0) { 39632a6e48fSopenharmony_ci delete[] *metadata; 39732a6e48fSopenharmony_ci *metadata = nullptr; 39832a6e48fSopenharmony_ci BLOGE("memcpy_s failed!, ret: %{public}d", err); 39932a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_UNKOWN; 40032a6e48fSopenharmony_ci } 40132a6e48fSopenharmony_ci return OHOS::SURFACE_ERROR_OK; 40232a6e48fSopenharmony_ci}