1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (C) 2023 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#ifndef AVBUFFER_UNITTEST_H
17fa7767c5Sopenharmony_ci#define AVBUFFER_UNITTEST_H
18fa7767c5Sopenharmony_ci#include <dirent.h>
19fa7767c5Sopenharmony_ci#include <gtest/gtest.h>
20fa7767c5Sopenharmony_ci#include <memory>
21fa7767c5Sopenharmony_ci#include <string>
22fa7767c5Sopenharmony_ci#include <vector>
23fa7767c5Sopenharmony_ci#include "av_hardware_allocator.h"
24fa7767c5Sopenharmony_ci#include "buffer/avbuffer.h"
25fa7767c5Sopenharmony_ci#include "dmabuf_alloc.h"
26fa7767c5Sopenharmony_ci#include "meta.h"
27fa7767c5Sopenharmony_ci#include "meta_key.h"
28fa7767c5Sopenharmony_ci#include "surface_buffer.h"
29fa7767c5Sopenharmony_ci#include "surface_type.h"
30fa7767c5Sopenharmony_ci
31fa7767c5Sopenharmony_ci#define INT_TESTKEY Tag::APP_PID
32fa7767c5Sopenharmony_ci#define LONG_TESTKEY Tag::MEDIA_DURATION
33fa7767c5Sopenharmony_ci#define DOUBLE_TESTKEY Tag::VIDEO_CAPTURE_RATE
34fa7767c5Sopenharmony_ci#define STRING_TESTKEY Tag::MEDIA_FILE_URI
35fa7767c5Sopenharmony_cinamespace OHOS {
36fa7767c5Sopenharmony_cinamespace Media {
37fa7767c5Sopenharmony_ciclass AVBufferMock;
38fa7767c5Sopenharmony_cinamespace AVBufferUT {
39fa7767c5Sopenharmony_ciconst std::string_view INT_CAPI_TESTKEY = "IntKey";
40fa7767c5Sopenharmony_ciconst std::string_view LONG_CAPI_TESTKEY = "LongKey";
41fa7767c5Sopenharmony_ciconst std::string_view FlOAT_CAPI_TESTKEY = "FloatKey";
42fa7767c5Sopenharmony_ciconst std::string_view DOUBLE_CAPI_TESTKEY = "DoubleKey";
43fa7767c5Sopenharmony_ciconst std::string_view STRING_CAPI_TESTKEY = "StringKey";
44fa7767c5Sopenharmony_ci
45fa7767c5Sopenharmony_ciconstexpr int32_t MEMSIZE = 1024 * 1024;
46fa7767c5Sopenharmony_ciconstexpr int32_t POSITION_ONE = 1024 * 64;
47fa7767c5Sopenharmony_ciconstexpr int32_t TEST_BUFFER_SIZE = 1048 * 1048 * 8;
48fa7767c5Sopenharmony_ciconstexpr int32_t TEST_LOOP_DEPTH = 10;
49fa7767c5Sopenharmony_ci
50fa7767c5Sopenharmony_ciconst int32_t INTVALUE = 141;
51fa7767c5Sopenharmony_ciconst int64_t LONGVALUE = 115441;
52fa7767c5Sopenharmony_ciconst float FLOATVALUE = 1.25;
53fa7767c5Sopenharmony_ciconst double DOUBLEVALUE = 1.625;
54fa7767c5Sopenharmony_ciconst std::string STRINGVALUE = "STRING_TESTVALUE";
55fa7767c5Sopenharmony_ci
56fa7767c5Sopenharmony_ciconst int32_t DEFAULT_OFFSET = 1000;
57fa7767c5Sopenharmony_ciconst int64_t DEFAULT_PTS = 33000;
58fa7767c5Sopenharmony_ciconst int64_t DEFAULT_DTS = 100;
59fa7767c5Sopenharmony_ciconst int64_t DEFAULT_DURATION = 1000;
60fa7767c5Sopenharmony_ciconst uint32_t DEFAULT_FLAG = 1 << 0;
61fa7767c5Sopenharmony_ci
62fa7767c5Sopenharmony_ciconst int32_t DEFAULT_PIXELSIZE = 4;
63fa7767c5Sopenharmony_ciconst BufferRequestConfig DEFAULT_CONFIG = {
64fa7767c5Sopenharmony_ci    .width = 800,
65fa7767c5Sopenharmony_ci    .height = 600,
66fa7767c5Sopenharmony_ci    .strideAlignment = 0x8,
67fa7767c5Sopenharmony_ci    .format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888,
68fa7767c5Sopenharmony_ci    .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
69fa7767c5Sopenharmony_ci    .timeout = 0,
70fa7767c5Sopenharmony_ci};
71fa7767c5Sopenharmony_ci
72fa7767c5Sopenharmony_ciclass AVBufferInnerUnitTest : public testing::Test {
73fa7767c5Sopenharmony_cipublic:
74fa7767c5Sopenharmony_ci    static void SetUpTestCase(void);
75fa7767c5Sopenharmony_ci    static void TearDownTestCase(void);
76fa7767c5Sopenharmony_ci    void SetUp(void);
77fa7767c5Sopenharmony_ci    void TearDown(void);
78fa7767c5Sopenharmony_ci
79fa7767c5Sopenharmony_ciprivate:
80fa7767c5Sopenharmony_ci    void CreateLocalHardwareMem();
81fa7767c5Sopenharmony_ci    void CreateLocalHardwareMemSecure();
82fa7767c5Sopenharmony_ci    void CreateLocalSharedMem();
83fa7767c5Sopenharmony_ci    void CreateLocalSurfaceMem();
84fa7767c5Sopenharmony_ci    void CreateLocalVirtualMem();
85fa7767c5Sopenharmony_ci
86fa7767c5Sopenharmony_ci    void CreateLocalSurfaceMemByParcel();
87fa7767c5Sopenharmony_ci    void CreateLocalSurfaceMemBySptr();
88fa7767c5Sopenharmony_ci
89fa7767c5Sopenharmony_ci    void CreateLocalHardwareMemByConfig();
90fa7767c5Sopenharmony_ci    void CreateLocalSharedMemByConfig();
91fa7767c5Sopenharmony_ci    void CreateLocalSurfaceMemByConfig();
92fa7767c5Sopenharmony_ci    void CreateLocalVirtualMemByConfig();
93fa7767c5Sopenharmony_ci
94fa7767c5Sopenharmony_ci    void CreateRemoteHardwareMem();
95fa7767c5Sopenharmony_ci    void CreateRemoteSharedMem();
96fa7767c5Sopenharmony_ci    void CreateRemoteSurfaceMem();
97fa7767c5Sopenharmony_ci    void CreateRemoteSurfaceMemByParcel();
98fa7767c5Sopenharmony_ci    void CreateRemoteSurfaceMemBySptr();
99fa7767c5Sopenharmony_ci    void CreateLocalNullMem();
100fa7767c5Sopenharmony_ci    void GetRemoteBuffer();
101fa7767c5Sopenharmony_ci
102fa7767c5Sopenharmony_ci    void CheckMetaSetAndGet();
103fa7767c5Sopenharmony_ci    void CheckMetaTransParcel();
104fa7767c5Sopenharmony_ci    void CheckAttrTrans();
105fa7767c5Sopenharmony_ci    void CheckMemTrans();
106fa7767c5Sopenharmony_ci    void CheckMemTransPos(int32_t pos);
107fa7767c5Sopenharmony_ci    void CheckMemTransOutOfRange(int32_t pos);
108fa7767c5Sopenharmony_ci    void CheckDataSize();
109fa7767c5Sopenharmony_ci    std::shared_ptr<AVAllocator> allocator_ = nullptr;
110fa7767c5Sopenharmony_ci    std::shared_ptr<AVBuffer> buffer_ = nullptr;
111fa7767c5Sopenharmony_ci    std::shared_ptr<AVBuffer> remoteBuffer_ = nullptr;
112fa7767c5Sopenharmony_ci    std::shared_ptr<Meta> meta_ = nullptr;
113fa7767c5Sopenharmony_ci    std::shared_ptr<MessageParcel> parcel_ = nullptr;
114fa7767c5Sopenharmony_ci    MemoryFlag memFlag_;
115fa7767c5Sopenharmony_ci    int32_t capacity_ = MEMSIZE;
116fa7767c5Sopenharmony_ci    int32_t align_ = 0;
117fa7767c5Sopenharmony_ci    int32_t dmaFd_ = -1;
118fa7767c5Sopenharmony_ci    AVBufferConfig config_;
119fa7767c5Sopenharmony_ci    std::vector<DmabufHeapBuffer> dmaBufferLst_;
120fa7767c5Sopenharmony_ci
121fa7767c5Sopenharmony_ci    std::vector<uint8_t> inputBuffer_;
122fa7767c5Sopenharmony_ci    std::vector<uint8_t> outputBuffer_;
123fa7767c5Sopenharmony_ci};
124fa7767c5Sopenharmony_ci
125fa7767c5Sopenharmony_ciclass HardwareHeapFactory {
126fa7767c5Sopenharmony_cipublic:
127fa7767c5Sopenharmony_ci    static HardwareHeapFactory &GetInstance();
128fa7767c5Sopenharmony_ci    int32_t GetHardwareHeapFd() const;
129fa7767c5Sopenharmony_ci
130fa7767c5Sopenharmony_ciprivate:
131fa7767c5Sopenharmony_ci    HardwareHeapFactory();
132fa7767c5Sopenharmony_ci    ~HardwareHeapFactory();
133fa7767c5Sopenharmony_ci    int32_t dmaHeapFd_ = -1;
134fa7767c5Sopenharmony_ci};
135fa7767c5Sopenharmony_ci
136fa7767c5Sopenharmony_ciclass AVBufferFrameworkUnitTest : public testing::Test {
137fa7767c5Sopenharmony_cipublic:
138fa7767c5Sopenharmony_ci    static void SetUpTestCase(void);
139fa7767c5Sopenharmony_ci    static void TearDownTestCase(void);
140fa7767c5Sopenharmony_ci    void SetUp(void);
141fa7767c5Sopenharmony_ci    void TearDown(void);
142fa7767c5Sopenharmony_ci
143fa7767c5Sopenharmony_ciprivate:
144fa7767c5Sopenharmony_ci    std::shared_ptr<AVBufferMock> buffer_;
145fa7767c5Sopenharmony_ci};
146fa7767c5Sopenharmony_ci} // namespace AVBufferUT
147fa7767c5Sopenharmony_ci} // namespace Media
148fa7767c5Sopenharmony_ci} // namespace OHOS
149fa7767c5Sopenharmony_ci#endif // AVBUFFER_UNITTEST_H