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_TRACKING_H
17 #define HOS_BUFFER_TRACKING_H
18 
19 #include "buffer_trace.h"
20 
21 #define PIPELINE_REPORT_BUFFER_LOCATION(I, F, N) TRACKING_REPORT_BUFFER_LOCATION(I, F, N, false)
22 
23 #define POOL_REPORT_BUFFER_LOCATION(I, F) TRACKING_REPORT_BUFFER_LOCATION(I, F, "", true)
24 
25 #define TRACKING_REPORT_BUFFER_LOCATION(I, F, N, R)         \
26     do {                                                    \
27         auto m = std::make_shared<BufferTrackingMessage>(); \
28         m->trackingId = I;                                  \
29         m->frameNumber = F;                                 \
30         m->nodeName = N;                                    \
31         m->isReturnBack = R;                                \
32         BufferTracking::ReportBufferLocation(m);            \
33     } while (0)
34 
35 namespace OHOS::Camera {
36 class BufferTracking {
37 public:
38     // begin to track a stream, call AddTrackingNode to add node after this function.
39     // trackingId can be steam id, poolId is id of buffer pool which in SinkNode.
40     static void AddTrackingStreamBegin(const int32_t trackingId, const int64_t poolId);
41 
42     // add node complete, call this before StartTracking, after AddTrackingNode.
43     static void AddTrackingStreamEnd(const int32_t trackingId);
44 
45     // disable tracking a stream
46     static void DeleteTrackingStream(const int32_t trackingId);
47 
48     // append node to tracking list, call this before AddTrackingStreamEnd, after AddTrackingStreamBegin.
49     static void AddTrackingNode(const int32_t trackingId, const std::string node);
50 
51     /* report location of a buffer, after this buffer has been delivered to a node or pool.
52      *            report          report    report    report    report      report
53      *               |               |         |         |        |            |
54      * BUFFERPOOL---->SOURCENODE----->NODE----->NODE----->NODE---->SINKNODE---->BUFFERPOOL
55      */
56     static void ReportBufferLocation(const std::shared_ptr<BufferTrackingMessage>& message);
57 
58     // start a thread to tracking buffers of a stream.
59     static void StartTracking();
60 
61     // stop stacking
62     static void StopTracking();
63 
64     // check if there are buffers in a specific node.
65     static int32_t IsNodeEmpty(const int32_t id, const std::string node);
66 
67     // check if there are buffers from beginNode to endNode.
68     static int32_t IsNodeEmpty(const int32_t id, const std::string beginNode, const std::string endNode);
69 
70     static void DumpBufferTrace(const int32_t id, BufferTraceGraph& graph);
71 };
72 } // namespace OHOS::Camera
73 #endif
74