• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef CAMERA_DEMO_TEST_H
17 #define CAMERA_DEMO_TEST_H
18 
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <thread>
24 #include <vector>
25 #include <gtest/gtest.h>
26 #include "display_type.h"
27 #include "ibuffer.h"
28 #include "ibuffer_pool.h"
29 #include "surface.h"
30 #include "ibuffer_consumer_listener.h"
31 
32 namespace OHOS::CameraUtest {
33 using namespace OHOS::Camera;
34 
35 class BufferManagerTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39 
40     void SetUp(void);
41     void TearDown(void);
42 
43 public:
44     class Stream {
45     public:
46         Stream() = default;
47         ~Stream() = default;
48 #ifdef CAMERA_BUILT_ON_OHOS_LITE
49         bool Init(std::shared_ptr<OHOS::Surface>& producer);
50 #else
51         bool Init(OHOS::sptr<OHOS::IBufferProducer>& producer);
52 #endif
53         void StartStream();
54         void StopStream();
55         void EnqueueBufferNonBlock();
56         void DequeueBuffer(std::shared_ptr<IBuffer>& buffer);
57         std::shared_ptr<IBufferPool> GetBufferPool() const;
GetPoolId()58         int64_t GetPoolId() const
59         {
60             return bufferPoolId_;
61         }
62 
63     private:
64         void StartInnerStream() const;
65         void StartExternalStream();
66 
67     private:
68         std::mutex lock_;
69 #ifdef CAMERA_BUILT_ON_OHOS_LITE
70         std::shared_ptr<OHOS::Surface> producer_ = nullptr;
71 #else
72         OHOS::sptr<OHOS::Surface> producer_ = nullptr;
73 #endif
74 
75         uint32_t width_ = 1920;
76         uint32_t height_ = 1080;
77         uint32_t queueSize_ = 7;
78         uint64_t usage_ = CAMERA_USAGE_SW_WRITE_OFTEN | CAMERA_USAGE_SW_READ_OFTEN | CAMERA_USAGE_MEM_DMA;
79         uint32_t format_ = CAMERA_FORMAT_YCBCR_420_SP;
80         int64_t bufferPoolId_ = -1;
81 
82         std::shared_ptr<IBufferPool> bufferPool_ = nullptr;
83 
84 #ifdef CAMERA_BUILT_ON_OHOS_LITE
85         std::vector<std::pair<OHOS::SurfaceBuffer*, std::shared_ptr<IBuffer>>> bufferVec_ = {};
86 #else
87         std::vector<std::pair<OHOS::sptr<OHOS::SurfaceBuffer>, std::shared_ptr<IBuffer>>> bufferVec_ = {};
88         int32_t releaseFence_ = 0;
89         OHOS::BufferRequestConfig requestConfig_ = {};
90         OHOS::BufferFlushConfig flushConfig_ = {};
91 #endif
92     };
93 
94 #ifndef CAMERA_BUILT_ON_OHOS_LITE
95     class TestBufferConsumerListener : public IBufferConsumerListener {
96         public:
TestBufferConsumerListener()97             TestBufferConsumerListener()
98             {
99             }
100 
~TestBufferConsumerListener()101             ~TestBufferConsumerListener()
102             {
103             }
104 
OnBufferAvailable()105             void OnBufferAvailable()
106             {
107             }
108     };
109 #endif
110 
111     class Node {
112     public:
Node(const std::string name)113         explicit Node(const std::string name)
114         {
115             name_ = name;
116         }
117         virtual ~Node() = default;
118 
119         virtual void Connect(std::shared_ptr<Node>& nextNode);
120         virtual void Deliver(std::shared_ptr<IBuffer>& buffer);
121         virtual void Receive(std::shared_ptr<IBuffer>& buffer);
122         virtual void Process(std::shared_ptr<IBuffer>& buffer);
123         virtual std::string GetName() const;
124 
125     private:
126         std::string name_ = "";
127         std::shared_ptr<Node> nextNode_ = nullptr;
128 
129     private:
130         Node() = default;
131     };
132 
133     class SinkNode : public Node {
134     public:
SinkNode(const std::string name)135         explicit SinkNode(const std::string name) : Node(name) {}
~SinkNode()136         ~SinkNode() override {}
137         void Deliver(std::shared_ptr<IBuffer>& buffer) override;
138         void BindCallback(const std::function<void(std::shared_ptr<IBuffer>&)>& callback);
139 
140     private:
141         std::function<void(std::shared_ptr<IBuffer>&)> callback_ = nullptr;
142     };
143 
144     class SourceNode : public Node {
145     public:
SourceNode(const std::string name)146         explicit SourceNode(const std::string name) : Node(name) {}
147         void Process(std::shared_ptr<IBuffer>& buffer) override;
~SourceNode()148         ~SourceNode() override {}
149     private:
150         int cacheSize_ = 3;
151         std::list<std::shared_ptr<IBuffer>> cache_ = {};
152     };
153 
154     class Pipeline {
155     public:
156         Pipeline() = default;
157         ~Pipeline() = default;
158 
159         bool AddStream(std::shared_ptr<Stream>& stream);
160         void StartStream();
161         void StopStream();
162 
163     private:
164         void CollectBuffers();
165         void DeliverBuffer();
166         void DeliverBuffer(std::shared_ptr<IBuffer>& buffer);
167         bool BuildPipeline();
168 
169     private:
170         bool running = true;
171         std::mutex streamLock_;
172         std::thread* collectThread_ = nullptr;
173         std::shared_ptr<Node> sourceNode_ = nullptr;
174         uint64_t frameNumber_ = 0;
175 
176         struct LocalStream {
177             std::mutex deviceLock;
178             std::shared_ptr<Stream> stream = nullptr;
179             std::thread* deliverThread = nullptr;
180             std::list<std::shared_ptr<IBuffer>> deviceBufferList = {};
181         };
182         std::shared_ptr<LocalStream> localStream_ = nullptr;
183     };
184 };
185 } // namespace OHOS::CameraUtest
186 #endif // CAMERA_DEMO_TEST_H
187