• 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         bool Init(OHOS::sptr<OHOS::IBufferProducer>& producer);
49         void StartStream();
50         void StopStream();
51         void EnqueueBufferNonBlock();
52         void DequeueBuffer(std::shared_ptr<IBuffer>& buffer);
53         std::shared_ptr<IBufferPool> GetBufferPool() const;
GetPoolId()54         int64_t GetPoolId() const
55         {
56             return bufferPoolId_;
57         }
58 
59     private:
60         void StartInnerStream() const;
61         void StartExternalStream();
62 
63     private:
64         std::mutex lock_;
65         OHOS::sptr<OHOS::Surface> producer_ = nullptr;
66 
67         uint32_t width_ = 1920;
68         uint32_t height_ = 1080;
69         uint32_t queueSize_ = 7;
70         uint64_t usage_ = CAMERA_USAGE_SW_WRITE_OFTEN | CAMERA_USAGE_SW_READ_OFTEN | CAMERA_USAGE_MEM_DMA;
71         uint32_t format_ = CAMERA_FORMAT_YCBCR_420_SP;
72         int64_t bufferPoolId_ = -1;
73 
74         std::shared_ptr<IBufferPool> bufferPool_ = nullptr;
75 
76         std::vector<std::pair<OHOS::sptr<OHOS::SurfaceBuffer>, std::shared_ptr<IBuffer>>> bufferVec_ = {};
77         int32_t releaseFence_ = 0;
78         std::vector<int32_t> deletingBuffers_ = {};
79         OHOS::BufferRequestConfig requestConfig_ = {};
80         OHOS::BufferFlushConfig flushConfig_ = {};
81     };
82 
83     class TestBufferConsumerListener: public IBufferConsumerListener {
84         public:
TestBufferConsumerListener()85             TestBufferConsumerListener()
86             {
87             }
88 
~TestBufferConsumerListener()89             ~TestBufferConsumerListener()
90             {
91             }
92 
OnBufferAvailable()93             void OnBufferAvailable()
94             {
95             }
96     };
97 
98     class Node {
99     public:
Node(const std::string name)100         explicit Node(const std::string name)
101         {
102             name_ = name;
103         }
104         virtual ~Node() = default;
105 
106         virtual void Connect(std::shared_ptr<Node>& nextNode);
107         virtual void Deliver(std::shared_ptr<IBuffer>& buffer);
108         virtual void Receive(std::shared_ptr<IBuffer>& buffer);
109         virtual void Process(std::shared_ptr<IBuffer>& buffer);
110 
111     public:
112         std::string name_ = "";
113         std::shared_ptr<Node> nextNode_ = nullptr;
114 
115     private:
116         Node() = default;
117     };
118 
119     class SinkNode : public Node {
120     public:
SinkNode(const std::string name)121         SinkNode(const std::string name) : Node(name) {}
~SinkNode()122         ~SinkNode() {}
123         void Deliver(std::shared_ptr<IBuffer>& buffer) override;
124         void BindCallback(std::function<void(std::shared_ptr<IBuffer>&)> callback);
125 
126     private:
127         std::function<void(std::shared_ptr<IBuffer>&)> callback_ = nullptr;
128     };
129 
130     class SourceNode : public Node {
131     public:
SourceNode(const std::string name)132         SourceNode(const std::string name) : Node(name) {}
133         void Process(std::shared_ptr<IBuffer>& buffer) override;
~SourceNode()134         ~SourceNode() {}
135     private:
136         int cacheSize_ = 3;
137         std::list<std::shared_ptr<IBuffer>> cache_ = {};
138     };
139 
140     class Pipeline {
141     public:
142         Pipeline() = default;
143         ~Pipeline() = default;
144 
145         bool AddStream(std::shared_ptr<Stream>& stream);
146         void StartStream();
147         void StopStream();
148 
149     private:
150         void CollectBuffers();
151         void DeliverBuffer();
152         void DeliverBuffer(std::shared_ptr<IBuffer>& buffer);
153         bool BuildPipeline();
154 
155     private:
156         bool running = true;
157         std::mutex streamLock_;
158         std::thread* collectThread_ = nullptr;
159         std::shared_ptr<Node> sourceNode_ = nullptr;
160         uint64_t frameNumber = 0;
161 
162         struct LocalStream {
163             std::mutex deviceLock_;
164             std::shared_ptr<Stream> stream_ = nullptr;
165             std::thread* deliverThread_ = nullptr;
166             std::list<std::shared_ptr<IBuffer>> deviceBufferList_ = {};
167         };
168         std::shared_ptr<LocalStream> localStream_ = nullptr;
169     };
170 };
171 } // namespace OHOS::CameraUtest
172 #endif // CAMERA_DEMO_TEST_H
173