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 "plugin/common/surface_allocator.h" 21#include "display_type.h" 22#include "foundation/log.h" 23#include "sync_fence.h" 24 25namespace OHOS { 26namespace Media { 27namespace Plugin { 28const std::unordered_map<VideoScaleType, ScalingMode> scaleTypeMap = { 29 { VideoScaleType::VIDEO_SCALE_TYPE_FIT, ScalingMode::SCALING_MODE_SCALE_TO_WINDOW }, 30 { VideoScaleType::VIDEO_SCALE_TYPE_FIT_CROP, ScalingMode::SCALING_MODE_SCALE_CROP} 31}; 32 33OHOS::ScalingMode GetScaleType(VideoScaleType scaleType) 34{ 35 if (!scaleTypeMap.count(scaleType)) { 36 return OHOS::SCALING_MODE_SCALE_TO_WINDOW; 37 } 38 return scaleTypeMap.at(scaleType); 39} 40 41constexpr int32_t DEFAULT_SURFACE_WIDTH = 640; 42constexpr int32_t DEFAULT_SURFACE_HEIGHT = 480; 43constexpr int32_t DEFAULT_SURFACE_STRIDE_ALIGN = 8; 44 45SurfaceAllocator::SurfaceAllocator(sptr<Surface> surface) 46 : Allocator(MemoryType::SURFACE_BUFFER), 47 surface_(surface) 48{ 49 requestConfig_ = { 50 DEFAULT_SURFACE_WIDTH, DEFAULT_SURFACE_HEIGHT, DEFAULT_SURFACE_STRIDE_ALIGN, 51 PixelFormat::PIXEL_FMT_RGBA_8888, BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA, 0}; 52} 53 54sptr<SurfaceBuffer> SurfaceAllocator::AllocSurfaceBuffer() 55{ 56 if (!surface_) { 57 MEDIA_LOG_E("surface is nullptr"); 58 return nullptr; 59 } 60 MEDIA_LOG_DD("width: " PUBLIC_LOG_D32 ", height :" PUBLIC_LOG_D32 ", align: " PUBLIC_LOG_D32 61 ", format: " PUBLIC_LOG_D32 ", usage: " PUBLIC_LOG_U64 ", timeout: " PUBLIC_LOG_D32, 62 requestConfig_.width, requestConfig_.height, requestConfig_.strideAlignment, requestConfig_.format, 63 requestConfig_.usage, requestConfig_.timeout); 64 OHOS::sptr<OHOS::SurfaceBuffer> surfaceBuffer = nullptr; 65 int32_t releaseFence = -1; 66 auto ret = surface_->RequestBuffer(surfaceBuffer, releaseFence, requestConfig_); 67 if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK || surfaceBuffer == nullptr) { 68 if (ret == OHOS::SurfaceError::SURFACE_ERROR_NO_BUFFER) { 69 MEDIA_LOG_DD("buffer queue is no more buffers"); 70 } else { 71 MEDIA_LOG_E("surface RequestBuffer fail, ret: " PUBLIC_LOG_U64, static_cast<uint64_t>(ret)); 72 } 73 return nullptr; 74 } 75 if (surfaceBuffer->Map() != OHOS::SurfaceError::SURFACE_ERROR_OK) { 76 MEDIA_LOG_E("surface buffer Map failed"); 77 surface_->CancelBuffer(surfaceBuffer); 78 return nullptr; 79 } 80 sptr<SyncFence> autoFence = new(std::nothrow) SyncFence(releaseFence); 81 if (autoFence != nullptr) { 82 autoFence->Wait(100); // 100ms 83 } 84 surface_->SetScalingMode(surfaceBuffer->GetSeqNum(), scalingMode_); 85 if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) { 86 MEDIA_LOG_E("surface buffer set scaling mode failed"); 87 surface_->CancelBuffer(surfaceBuffer); 88 return nullptr; 89 } 90 MEDIA_LOG_DD("request surface buffer success, releaseFence: " PUBLIC_LOG_D32, releaseFence); 91 return surfaceBuffer; 92} 93 94void SurfaceAllocator::ReleaseSurfaceBuffer(sptr<SurfaceBuffer>& surfaceBuffer, bool needRender) 95{ 96 if (!needRender) { 97 auto ret = surface_->CancelBuffer(surfaceBuffer); 98 if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) { 99 MEDIA_LOG_E("surface CancelBuffer fail, ret: " PUBLIC_LOG_U64, static_cast<uint64_t>(ret)); 100 } 101 } 102 surfaceBuffer = nullptr; 103} 104 105void* SurfaceAllocator::Alloc(size_t size) 106{ 107 return nullptr; 108} 109 110void SurfaceAllocator::Free(void* ptr) // NOLINT: void* 111{ 112 (void)ptr; 113} 114 115void SurfaceAllocator::Config(int32_t width, int32_t height, uint64_t usage, int32_t format, int32_t strideAlign, 116 int32_t timeout) 117{ 118 requestConfig_ = { 119 width, height, strideAlign, format, usage, timeout 120 }; 121} 122 123void SurfaceAllocator::SetScaleType(VideoScaleType videoScaleType) 124{ 125 scalingMode_ = GetScaleType(videoScaleType); 126} 127 128void SurfaceAllocator::UpdateSurfaceBufferScaleMode(sptr<SurfaceBuffer>& surfaceBuffer) 129{ 130 auto ret = surface_->SetScalingMode(surfaceBuffer->GetSeqNum(), scalingMode_); 131 if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) { 132 MEDIA_LOG_E("update surface buffer scaling mode fail, ret: " PUBLIC_LOG_U64, static_cast<uint64_t>(ret)); 133 } 134} 135} // namespace Plugin 136} // namespace Media 137} // namespace OHOS 138#endif