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