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