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_BUFFER_LOOP_TRACKING_H 17 #define HOS_BUFFER_LOOP_TRACKING_H 18 19 #include "buffer_trace.h" 20 #include "camera.h" 21 #include <atomic> 22 #include <condition_variable> 23 #include <list> 24 #include <memory> 25 #include <mutex> 26 #include <string> 27 #include <thread> 28 #include <algorithm> 29 30 namespace OHOS::Camera { 31 class TrackingNode { 32 public: 33 explicit TrackingNode(std::string name); 34 ~TrackingNode(); 35 36 std::string GetNodeName() const; 37 std::shared_ptr<TrackingBuffer> FindTrackingBuffer(const std::shared_ptr<TrackingBuffer>& buffer); 38 void AttachTrackingBuffer(const std::shared_ptr<TrackingBuffer>& buffer); 39 void DetachTrackingBuffer(const std::shared_ptr<TrackingBuffer>& buffer); 40 bool IsEmpty(); GetTrackingBuffer()41 std::list<std::shared_ptr<TrackingBuffer>> GetTrackingBuffer() const 42 { 43 return trackingBufferList_; 44 } 45 46 bool operator==(const TrackingNode& n) 47 { 48 return this->nodeName_ == n.nodeName_; 49 } 50 51 private: 52 std::mutex lock_; 53 std::string nodeName_ = ""; 54 std::list<std::shared_ptr<TrackingBuffer>> trackingBufferList_ = {}; 55 56 private: 57 TrackingNode() = default; 58 }; 59 60 class TrackingStream { 61 public: 62 explicit TrackingStream(const int32_t id); 63 ~TrackingStream(); 64 65 int32_t GetTrackingStreamId() const; 66 void AttachTrackingNode(const std::shared_ptr<TrackingNode>& node); 67 void DetachTrackingNode(const std::shared_ptr<TrackingNode>& node); 68 void MoveBuffer(std::shared_ptr<TrackingBuffer>& buffer, std::shared_ptr<TrackingNode>& node); 69 void RemoveBuffer(std::shared_ptr<TrackingBuffer>& buffer); 70 void DumpTrace(BufferTraceGraph& graph); 71 bool operator==(const TrackingStream& n) 72 { 73 return this->trackingId_ == n.trackingId_; 74 } 75 std::shared_ptr<TrackingNode> FindTrackingNode(const std::string node); 76 std::shared_ptr<TrackingNode> LocateBuffer(const std::shared_ptr<TrackingBuffer>& buffer); 77 std::list<std::shared_ptr<TrackingNode>> FindTrackingNodePath(const std::string beginNode, 78 const std::string endNode); NodeAddComplete()79 void NodeAddComplete() 80 { 81 addNodeComplete_ = true; 82 } 83 IsNodeComplete()84 bool IsNodeComplete() const 85 { 86 return addNodeComplete_; 87 } 88 89 private: 90 int32_t trackingId_ = -1; 91 bool addNodeComplete_ = false; 92 std::list<std::shared_ptr<TrackingNode>> trackingNodeList_ = {}; 93 std::mutex lock_; 94 std::mutex traceLock_; 95 96 private: 97 TrackingStream() = default; 98 }; 99 100 class BufferLoopTracking { 101 public: 102 static BufferLoopTracking& GetInstance(); 103 void AddTrackingStreamBegin(const int32_t trackingId, const int64_t poolId); 104 void AddTrackingStreamEnd(const int32_t trackingId); 105 void DeleteTrackingStream(const int32_t trackingId); 106 void AddTrackingNode(const int32_t trackingId, const std::string node); 107 void SendBufferMovementMessage(const std::shared_ptr<BufferTrackingMessage>& message); 108 void StartTracking(); 109 void StopTracking(); 110 int32_t IsEmpty(const int32_t id, const std::string node); 111 int32_t IsEmpty(const int32_t id, const std::string beginNode, const std::string endNode); 112 void DumpTrace(const int32_t trackingId, BufferTraceGraph& graph); 113 114 private: 115 BufferLoopTracking() = default; 116 BufferLoopTracking(const BufferLoopTracking&); 117 BufferLoopTracking& operator=(const BufferLoopTracking&); 118 BufferLoopTracking(BufferLoopTracking&&); 119 BufferLoopTracking& operator=(BufferLoopTracking&&); 120 ~BufferLoopTracking(); 121 122 std::shared_ptr<TrackingStream> FindTrackingStream(const int32_t id); 123 std::shared_ptr<BufferTrackingMessage> ReceiveMessage(); 124 void HandleMessage(); 125 126 private: 127 std::mutex lock_; 128 std::mutex messageLock_; 129 std::condition_variable cv_; 130 std::atomic<bool> running_ = false; 131 132 std::unique_ptr<std::thread> handler_ = nullptr; 133 std::list<std::shared_ptr<TrackingStream>> trackingStreamList_ = {}; 134 std::list<std::shared_ptr<BufferTrackingMessage>> messageQueue_ = {}; 135 }; 136 } // namespace OHOS::Camera 137 #endif 138