1 /*
2  * Copyright (C) 2023 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 #ifndef AV_CODEC_FSURFACE_MEMORY_H
17 #define AV_CODEC_FSURFACE_MEMORY_H
18 
19 #include "refbase.h"
20 #include "surface.h"
21 #include "sync_fence.h"
22 
23 namespace OHOS {
24 namespace MediaAVCodec {
25 namespace {
26 constexpr uint64_t USAGE = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA;
27 constexpr int32_t SURFACE_STRIDE_ALIGN = 8;
28 constexpr int32_t TIMEOUT = 0;
29 } // namespace
30 
31 struct SurfaceControl {
32     sptr<Surface> surface = nullptr;
33     BufferRequestConfig requestConfig = {.width = 0,
34                                          .height = 0,
35                                          .strideAlignment = SURFACE_STRIDE_ALIGN,
36                                          .format = 0,
37                                          .usage = USAGE,
38                                          .timeout = TIMEOUT};
39     ScalingMode scalingMode = ScalingMode::SCALING_MODE_SCALE_TO_WINDOW;
40 };
41 
42 class FSurfaceMemory {
43 public:
FSurfaceMemory(SurfaceControl * sInfo)44     FSurfaceMemory(SurfaceControl *sInfo) : sInfo_(sInfo)
45     {
46         AllocSurfaceBuffer();
47     }
48     ~FSurfaceMemory();
49     void AllocSurfaceBuffer();
50     void ReleaseSurfaceBuffer();
51     sptr<SurfaceBuffer> GetSurfaceBuffer();
52     int32_t GetSurfaceBufferStride();
53     sptr<SyncFence> GetFence();
54     void UpdateSurfaceBufferScaleMode();
55     void SetNeedRender(bool needRender);
56     uint8_t *GetBase() const;
57     int32_t GetSize() const;
58 
59 private:
60     sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
61     sptr<SyncFence> fence_ = nullptr;
62     int32_t stride_ = 0;
63     bool needRender_ = false;
64     SurfaceControl *sInfo_ = nullptr;
65 };
66 } // namespace MediaAVCodec
67 } // namespace OHOS
68 #endif
69