• 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 "surface_allocator.h"
21 #include "foundation/log.h"
22 #include "display_type.h"
23 #include "sync_fence.h"
24 
25 namespace OHOS {
26 namespace Media {
27 namespace Plugin {
28 constexpr int32_t DEFAULT_SURFACE_WIDTH = 640;
29 constexpr int32_t DEFAULT_SURFACE_HEIGHT = 480;
30 constexpr int32_t DEFAULT_SURFACE_STRIDE_ALIGN = 8;
31 
SurfaceAllocator(sptr<Surface> surface)32 SurfaceAllocator::SurfaceAllocator(sptr<Surface> surface)
33     : Allocator(MemoryType::SURFACE_BUFFER),
34       surface_(surface)
35 {
36     requestConfig_ = {
37         DEFAULT_SURFACE_WIDTH, DEFAULT_SURFACE_HEIGHT, DEFAULT_SURFACE_STRIDE_ALIGN,
38         PixelFormat ::PIXEL_FMT_RGBA_8888, BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA, 0};
39 }
40 
AllocSurfaceBuffer(size_t size)41 sptr<SurfaceBuffer> SurfaceAllocator::AllocSurfaceBuffer(size_t size)
42 {
43     (void)size;
44     if (surface_ == nullptr) {
45         MEDIA_LOG_E("surface is nullptr");
46         return nullptr;
47     }
48     MEDIA_LOG_D("width: " PUBLIC_LOG_D32 ", height :" PUBLIC_LOG_D32 ", align: " PUBLIC_LOG_D32
49                 ", format: " PUBLIC_LOG_D32 ", usage: " PUBLIC_LOG_U64 ", timeout: " PUBLIC_LOG_D32,
50                 requestConfig_.width, requestConfig_.height, requestConfig_.strideAlignment, requestConfig_.format,
51                 requestConfig_.usage, requestConfig_.timeout);
52     OHOS::sptr<OHOS::SurfaceBuffer> surfaceBuffer = nullptr;
53     int32_t releaseFence = -1;
54     auto ret = surface_->RequestBuffer(surfaceBuffer, releaseFence, requestConfig_);
55     if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK || surfaceBuffer == nullptr) {
56         if (ret == OHOS::SurfaceError::SURFACE_ERROR_NO_BUFFER) {
57             MEDIA_LOG_E("buffer queue is no more buffers");
58         } else {
59             MEDIA_LOG_E("surface RequestBuffer fail, ret: " PUBLIC_LOG_U64, static_cast<uint64_t>(ret));
60         }
61         return nullptr;
62     }
63     sptr<SyncFence> autoFence = new(std::nothrow) SyncFence(releaseFence);
64     if (autoFence != nullptr) {
65         autoFence->Wait(100); // 100ms
66     }
67     MEDIA_LOG_D("request surface buffer success, releaseFence: " PUBLIC_LOG_D32, releaseFence);
68     return surfaceBuffer;
69 }
70 
Alloc(size_t size)71 void* SurfaceAllocator::Alloc(size_t size)
72 {
73     return nullptr;
74 }
75 
Free(void * ptr)76 void SurfaceAllocator::Free(void* ptr) // NOLINT: void*
77 {
78     (void)ptr;
79 }
80 
Config(int32_t width,int32_t height,uint64_t usage,int32_t format,int32_t strideAlign,int32_t timeout)81 void SurfaceAllocator::Config(int32_t width, int32_t height, uint64_t usage, int32_t format, int32_t strideAlign,
82                               int32_t timeout)
83 {
84     requestConfig_ = {
85         width, height, strideAlign, format, usage, timeout
86     };
87 }
88 } // namespace Plugin
89 } // namespace Media
90 } // namespace OHOS
91 #endif