• 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 HOS_CAMERA_BUFFER_POOL_H
17 #define HOS_CAMERA_BUFFER_POOL_H
18 
19 #include "buffer_allocator_factory.h"
20 #include "ibuffer.h"
21 #include "ibuffer_pool.h"
22 #include <condition_variable>
23 #include <list>
24 #include <memory>
25 #include <mutex>
26 #include <utility>
27 
28 namespace OHOS::Camera {
29 class BufferPool : public IBufferPool {
30 public:
31     BufferPool();
32     virtual ~BufferPool();
33 
34     RetCode Init(const uint32_t width,
35                          const uint32_t height,
36                          const uint64_t usage,
37                          const uint32_t bufferFormat,
38                          const uint32_t count,
39                          const int32_t bufferSourceType) override;
40     RetCode AddBuffer(std::shared_ptr<IBuffer>& buffer) override;
41     std::shared_ptr<IBuffer> AcquireBuffer(int timeout) override;
42     RetCode ReturnBuffer(std::shared_ptr<IBuffer>& buffer) override;
43     void EnableTracking(const int32_t id) override;
44     void SetId(const int64_t id) override;
45     void NotifyStop() override;
46     void NotifyStart() override;
47     void ClearBuffers() override;
48     uint32_t GetIdleBufferCount() override;
49     void setSFBuffer(std::shared_ptr<IBuffer>& buffer) override;
50     std::map<int32_t, uint8_t*> getSFBuffer(int32_t index) override;
51     int32_t GetForkBufferId() override;
52     void SetForkBufferId(int32_t index) override;
53     bool GetIsFork() override;
54     void SetIsFork(bool isFork) override;
55 
56 private:
57     RetCode PrepareBuffer();
58     RetCode DestroyBuffer();
59 
60 private:
61     std::mutex lock_;
62     std::condition_variable cv_;
63     std::atomic_bool stop_ = false;
64     int64_t poolId_ = -1;
65     int32_t trackingId_ = -1;
66     uint32_t bufferCount_ = 0;
67     uint32_t bufferWidth_ = 0;
68     uint32_t bufferHeight_ = 0;
69     uint64_t bufferUsage_ = 0;
70     uint32_t bufferFormat_ = CAMERA_FORMAT_INVALID;
71     int32_t bufferSourceType_ = CAMERA_BUFFER_SOURCE_TYPE_NONE;
72     std::shared_ptr<IBufferAllocator> bufferAllocator_ = nullptr;
73     std::list<std::shared_ptr<IBuffer>> idleList_ = {};
74     std::list<std::shared_ptr<IBuffer>> busyList_ = {};
75     std::map<int32_t, uint8_t*> sfBuffer_;
76     std::map<int32_t, uint32_t> sfSize_;
77     int32_t forkBufferId_ = -1;
78     bool isFork_ = false;
79 };
80 } // namespace OHOS::Camera
81 #endif
82