• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SURFACE_MEMORY_H
17 #define AV_CODEC_SURFACE_MEMORY_H
18 
19 #include "avsharedmemory.h"
20 #include "refbase.h"
21 #include "surface.h"
22 #include "sync_fence.h"
23 
24 namespace OHOS {
25 namespace MediaAVCodec {
26 namespace {
27 constexpr uint64_t USAGE = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA;
28 constexpr int32_t SURFACE_STRIDE_ALIGN = 8;
29 constexpr int32_t TIMEOUT = 0;
30 } // namespace
31 
32 class SurfaceMemory : public AVSharedMemory {
33 public:
34     SurfaceMemory() = default;
35     virtual ~SurfaceMemory() override;
36     static std::shared_ptr<SurfaceMemory> Create();
37     static void SetSurface(sptr<Surface> surface);
38     static void SetConfig(int32_t width, int32_t height, int32_t format, uint64_t usage = USAGE,
39                           int32_t strideAlign = SURFACE_STRIDE_ALIGN, int32_t timeout = TIMEOUT);
40     static void SetScaleType(ScalingMode videoScaleMode);
41     int32_t Write(const uint8_t *in, int32_t writeSize, int32_t position = INVALID_POSITION);
42     int32_t Read(uint8_t *out, int32_t readSize, int32_t position = INVALID_POSITION);
43     void ClearUsedSize();
44     void AllocSurfaceBuffer();
45     void ReleaseSurfaceBuffer();
46     sptr<SurfaceBuffer> GetSurfaceBuffer();
47     int32_t GetSurfaceBufferStride();
48     int32_t GetFence();
49     int32_t GetUsedSize() const;
50     void UpdateSurfaceBufferScaleMode();
51     void SetNeedRender(bool needRender);
52     virtual uint8_t *GetBase() const override;
53     virtual int32_t GetSize() const override;
54     virtual uint32_t GetFlags() const final;
55 
56 private:
57     // Allocated memory size.
58     sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
59     int32_t size_ = 0;
60     int32_t fence_ = -1;
61     int32_t stride_ = 0;
62     bool needRender_ = false;
63     static sptr<Surface> surface_;
64     static BufferRequestConfig requestConfig_;
65     static ScalingMode scalingMode_;
66     static constexpr int32_t INVALID_POSITION = -1;
67 };
68 } // namespace MediaAVCodec
69 } // namespace OHOS
70 #endif
71