1/* 2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT) 17 18#define HST_LOG_TAG "SurfaceAllocator" 19 20#include "common/surface_allocator.h" 21#include "display_type.h" 22#include "inner_api/common/log.h" 23#include "sync_fence.h" 24 25namespace { 26constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "SurfaceAllocator" }; 27} 28 29namespace OHOS { 30namespace Media { 31namespace Plugins { 32const std::unordered_map<VideoScaleType, ScalingMode> scaleTypeMap = { 33 { VideoScaleType::VIDEO_SCALE_TYPE_FIT, ScalingMode::SCALING_MODE_SCALE_TO_WINDOW }, 34 { VideoScaleType::VIDEO_SCALE_TYPE_FIT_CROP, ScalingMode::SCALING_MODE_SCALE_CROP} 35}; 36 37OHOS::ScalingMode GetScaleType(VideoScaleType scaleType) 38{ 39 if (!scaleTypeMap.count(scaleType)) { 40 return OHOS::SCALING_MODE_SCALE_TO_WINDOW; 41 } 42 return scaleTypeMap.at(scaleType); 43} 44 45constexpr int32_t DEFAULT_SURFACE_WIDTH = 640; 46constexpr int32_t DEFAULT_SURFACE_HEIGHT = 480; 47constexpr int32_t DEFAULT_SURFACE_STRIDE_ALIGN = 8; 48 49SurfaceAllocator::SurfaceAllocator(sptr<Surface> surface) 50 : Allocator(MemoryType::SURFACE_MEMORY), 51 surface_(surface) 52{ 53 requestConfig_ = { 54 DEFAULT_SURFACE_WIDTH, DEFAULT_SURFACE_HEIGHT, DEFAULT_SURFACE_STRIDE_ALIGN, 55 PixelFormat::PIXEL_FMT_RGBA_8888, BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA, 0}; 56} 57 58sptr<SurfaceBuffer> SurfaceAllocator::AllocSurfaceBuffer() 59{ 60 if (!surface_) { 61 MEDIA_LOG_E("surface is nullptr"); 62 return nullptr; 63 } 64 MEDIA_LOG_DD("width: " PUBLIC_LOG_D32 ", height :" PUBLIC_LOG_D32 ", align: " PUBLIC_LOG_D32 65 ", format: " PUBLIC_LOG_D32 ", usage: " PUBLIC_LOG_U64 ", timeout: " PUBLIC_LOG_D32, 66 requestConfig_.width, requestConfig_.height, requestConfig_.strideAlignment, requestConfig_.format, 67 requestConfig_.usage, requestConfig_.timeout); 68 OHOS::sptr<OHOS::SurfaceBuffer> surfaceBuffer = nullptr; 69 int32_t releaseFence = -1; 70 auto ret = surface_->RequestBuffer(surfaceBuffer, releaseFence, requestConfig_); 71 if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK || surfaceBuffer == nullptr) { 72 if (ret == OHOS::SurfaceError::SURFACE_ERROR_NO_BUFFER) { 73 MEDIA_LOG_DD("buffer queue is no more buffers"); 74 } else { 75 MEDIA_LOG_E("surface RequestBuffer fail, ret: " PUBLIC_LOG_U64, static_cast<uint64_t>(ret)); 76 } 77 return nullptr; 78 } 79 if (surfaceBuffer->Map() != OHOS::SurfaceError::SURFACE_ERROR_OK) { 80 MEDIA_LOG_E("surface buffer Map failed"); 81 surface_->CancelBuffer(surfaceBuffer); 82 return nullptr; 83 } 84 sptr<SyncFence> autoFence = new(std::nothrow) SyncFence(releaseFence); 85 if (autoFence != nullptr) { 86 autoFence->Wait(100); // 100ms 87 } 88 surface_->SetScalingMode(surfaceBuffer->GetSeqNum(), scalingMode_); 89 if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) { 90 MEDIA_LOG_E("surface buffer set scaling mode failed"); 91 surface_->CancelBuffer(surfaceBuffer); 92 return nullptr; 93 } 94 MEDIA_LOG_DD("request surface buffer success, releaseFence: " PUBLIC_LOG_D32, releaseFence); 95 return surfaceBuffer; 96} 97 98void SurfaceAllocator::ReleaseSurfaceBuffer(sptr<SurfaceBuffer>& surfaceBuffer, bool needRender) 99{ 100 if (!needRender) { 101 auto ret = surface_->CancelBuffer(surfaceBuffer); 102 if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) { 103 MEDIA_LOG_E("surface CancelBuffer fail, ret: " PUBLIC_LOG_U64, static_cast<uint64_t>(ret)); 104 } 105 } 106 surfaceBuffer = nullptr; 107} 108 109void* SurfaceAllocator::Alloc(size_t size) 110{ 111 return nullptr; 112} 113 114void SurfaceAllocator::Free(void* ptr) // NOLINT: void* 115{ 116 (void)ptr; 117} 118 119void SurfaceAllocator::Config(int32_t width, int32_t height, uint64_t usage, int32_t format, int32_t strideAlign, 120 int32_t timeout) 121{ 122 requestConfig_ = { 123 width, height, strideAlign, format, usage, timeout 124 }; 125} 126 127void SurfaceAllocator::SetScaleType(VideoScaleType videoScaleType) 128{ 129 scalingMode_ = GetScaleType(videoScaleType); 130} 131 132void SurfaceAllocator::UpdateSurfaceBufferScaleMode(sptr<SurfaceBuffer>& surfaceBuffer) 133{ 134 auto ret = surface_->SetScalingMode(surfaceBuffer->GetSeqNum(), scalingMode_); 135 if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) { 136 MEDIA_LOG_E("update surface buffer scaling mode fail, ret: " PUBLIC_LOG_U64, static_cast<uint64_t>(ret)); 137 } 138} 139} // namespace Plugins 140} // namespace Media 141} // namespace OHOS 142#endif