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