summaryrefslogtreecommitdiff
path: root/media/codec2/sfplugin/PipelineWatcher.h
blob: 1e2314758d5ab20538d3db9829936a7a7a5b9603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef PIPELINE_WATCHER_H_
#define PIPELINE_WATCHER_H_

#include <chrono>
#include <map>
#include <memory>

#include <C2Work.h>

namespace android {

/**
 * PipelineWatcher watches the pipeline and infers the status of work items from
 * events.
 */
class PipelineWatcher {
public:
    typedef std::chrono::steady_clock Clock;

    PipelineWatcher()
        : mInputDelay(0),
          mPipelineDelay(0),
          mOutputDelay(0),
          mSmoothnessFactor(0) {}
    ~PipelineWatcher() = default;

    /**
     * \param value the new input delay value
     * \return  this object
     */
    PipelineWatcher &inputDelay(uint32_t value);

    /**
     * \param value the new pipeline delay value
     * \return  this object
     */
    PipelineWatcher &pipelineDelay(uint32_t value);

    /**
     * \param value the new output delay value
     * \return  this object
     */
    PipelineWatcher &outputDelay(uint32_t value);

    /**
     * \param value the new smoothness factor value
     * \return  this object
     */
    PipelineWatcher &smoothnessFactor(uint32_t value);

    /**
     * Client queued a work item to the component.
     *
     * \param frameIndex  input frame index of this work
     * \param buffers     input buffers of the queued work item
     * \param queuedAt    time when the client queued the buffer
     */
    void onWorkQueued(
            uint64_t frameIndex,
            std::vector<std::shared_ptr<C2Buffer>> &&buffers,
            const Clock::time_point &queuedAt);

    /**
     * The component released input buffers from a work item.
     *
     * \param frameIndex  input frame index
     * \param arrayIndex  index of the buffer at the original |buffers| in
     *                    onWorkQueued().
     * \return  buffers[arrayIndex]
     */
    std::shared_ptr<C2Buffer> onInputBufferReleased(
            uint64_t frameIndex, size_t arrayIndex);

    /**
     * The component finished processing a work item.
     *
     * \param frameIndex  input frame index
     */
    void onWorkDone(uint64_t frameIndex);

    /**
     * Flush the pipeline.
     */
    void flush();

    /**
     * \return  true  if pipeline does not need more work items to proceed
     *                smoothly, considering delays and smoothness factor;
     *          false otherwise.
     */
    bool pipelineFull() const;

    /**
     * Return elapsed processing time of a work item, nth from the longest
     * processing time to the shortest.
     *
     * \param now current timestamp
     * \param n   nth work item, from the longest processing time to the
     *            shortest. It's a 0-based index.
     * \return  elapsed processing time of nth work item.
     */
    Clock::duration elapsed(const Clock::time_point &now, size_t n) const;

private:
    uint32_t mInputDelay;
    uint32_t mPipelineDelay;
    uint32_t mOutputDelay;
    uint32_t mSmoothnessFactor;

    struct Frame {
        Frame(std::vector<std::shared_ptr<C2Buffer>> &&b,
              const Clock::time_point &q)
            : buffers(b),
              queuedAt(q) {}
        std::vector<std::shared_ptr<C2Buffer>> buffers;
        const Clock::time_point queuedAt;
    };
    std::map<uint64_t, Frame> mFramesInPipeline;
};

}  // namespace android

#endif  // PIPELINE_WATCHER_H_