1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#ifndef HOS_BUFFER_TRACKING_H
17094332d3Sopenharmony_ci#define HOS_BUFFER_TRACKING_H
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci#include "buffer_trace.h"
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_ci#define PIPELINE_REPORT_BUFFER_LOCATION(I, F, N) TRACKING_REPORT_BUFFER_LOCATION(I, F, N, false)
22094332d3Sopenharmony_ci
23094332d3Sopenharmony_ci#define POOL_REPORT_BUFFER_LOCATION(I, F) TRACKING_REPORT_BUFFER_LOCATION(I, F, "", true)
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_ci#define TRACKING_REPORT_BUFFER_LOCATION(I, F, N, R)         \
26094332d3Sopenharmony_ci    do {                                                    \
27094332d3Sopenharmony_ci        auto m = std::make_shared<BufferTrackingMessage>(); \
28094332d3Sopenharmony_ci        m->trackingId = I;                                  \
29094332d3Sopenharmony_ci        m->frameNumber = F;                                 \
30094332d3Sopenharmony_ci        m->nodeName = N;                                    \
31094332d3Sopenharmony_ci        m->isReturnBack = R;                                \
32094332d3Sopenharmony_ci        BufferTracking::ReportBufferLocation(m);            \
33094332d3Sopenharmony_ci    } while (0)
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_cinamespace OHOS::Camera {
36094332d3Sopenharmony_ciclass BufferTracking {
37094332d3Sopenharmony_cipublic:
38094332d3Sopenharmony_ci    // begin to track a stream, call AddTrackingNode to add node after this function.
39094332d3Sopenharmony_ci    // trackingId can be steam id, poolId is id of buffer pool which in SinkNode.
40094332d3Sopenharmony_ci    static void AddTrackingStreamBegin(const int32_t trackingId, const int64_t poolId);
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ci    // add node complete, call this before StartTracking, after AddTrackingNode.
43094332d3Sopenharmony_ci    static void AddTrackingStreamEnd(const int32_t trackingId);
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_ci    // disable tracking a stream
46094332d3Sopenharmony_ci    static void DeleteTrackingStream(const int32_t trackingId);
47094332d3Sopenharmony_ci
48094332d3Sopenharmony_ci    // append node to tracking list, call this before AddTrackingStreamEnd, after AddTrackingStreamBegin.
49094332d3Sopenharmony_ci    static void AddTrackingNode(const int32_t trackingId, const std::string node);
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    /* report location of a buffer, after this buffer has been delivered to a node or pool.
52094332d3Sopenharmony_ci     *            report          report    report    report    report      report
53094332d3Sopenharmony_ci     *               |               |         |         |        |            |
54094332d3Sopenharmony_ci     * BUFFERPOOL---->SOURCENODE----->NODE----->NODE----->NODE---->SINKNODE---->BUFFERPOOL
55094332d3Sopenharmony_ci     */
56094332d3Sopenharmony_ci    static void ReportBufferLocation(const std::shared_ptr<BufferTrackingMessage>& message);
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci    // start a thread to tracking buffers of a stream.
59094332d3Sopenharmony_ci    static void StartTracking();
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_ci    // stop stacking
62094332d3Sopenharmony_ci    static void StopTracking();
63094332d3Sopenharmony_ci
64094332d3Sopenharmony_ci    // check if there are buffers in a specific node.
65094332d3Sopenharmony_ci    static int32_t IsNodeEmpty(const int32_t id, const std::string node);
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    // check if there are buffers from beginNode to endNode.
68094332d3Sopenharmony_ci    static int32_t IsNodeEmpty(const int32_t id, const std::string beginNode, const std::string endNode);
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci    static void DumpBufferTrace(const int32_t id, BufferTraceGraph& graph);
71094332d3Sopenharmony_ci};
72094332d3Sopenharmony_ci} // namespace OHOS::Camera
73094332d3Sopenharmony_ci#endif
74