blob: 1e2314758d5ab20538d3db9829936a7a7a5b9603 [file] [log] [blame]
Wonsik Kimab34ed62019-01-31 15:28:46 -08001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef PIPELINE_WATCHER_H_
18#define PIPELINE_WATCHER_H_
19
20#include <chrono>
21#include <map>
22#include <memory>
23
24#include <C2Work.h>
25
26namespace android {
27
28/**
Wonsik Kim070897f2019-02-15 10:38:54 -080029 * PipelineWatcher watches the pipeline and infers the status of work items from
30 * events.
Wonsik Kimab34ed62019-01-31 15:28:46 -080031 */
32class PipelineWatcher {
33public:
34 typedef std::chrono::steady_clock Clock;
35
36 PipelineWatcher()
37 : mInputDelay(0),
38 mPipelineDelay(0),
39 mOutputDelay(0),
40 mSmoothnessFactor(0) {}
41 ~PipelineWatcher() = default;
42
Wonsik Kim070897f2019-02-15 10:38:54 -080043 /**
44 * \param value the new input delay value
45 * \return this object
46 */
Wonsik Kimab34ed62019-01-31 15:28:46 -080047 PipelineWatcher &inputDelay(uint32_t value);
Wonsik Kim070897f2019-02-15 10:38:54 -080048
49 /**
50 * \param value the new pipeline delay value
51 * \return this object
52 */
Wonsik Kimab34ed62019-01-31 15:28:46 -080053 PipelineWatcher &pipelineDelay(uint32_t value);
Wonsik Kim070897f2019-02-15 10:38:54 -080054
55 /**
56 * \param value the new output delay value
57 * \return this object
58 */
Wonsik Kimab34ed62019-01-31 15:28:46 -080059 PipelineWatcher &outputDelay(uint32_t value);
Wonsik Kim070897f2019-02-15 10:38:54 -080060
61 /**
62 * \param value the new smoothness factor value
63 * \return this object
64 */
Wonsik Kimab34ed62019-01-31 15:28:46 -080065 PipelineWatcher &smoothnessFactor(uint32_t value);
66
Wonsik Kim070897f2019-02-15 10:38:54 -080067 /**
68 * Client queued a work item to the component.
69 *
70 * \param frameIndex input frame index of this work
71 * \param buffers input buffers of the queued work item
72 * \param queuedAt time when the client queued the buffer
73 */
Wonsik Kimab34ed62019-01-31 15:28:46 -080074 void onWorkQueued(
75 uint64_t frameIndex,
76 std::vector<std::shared_ptr<C2Buffer>> &&buffers,
77 const Clock::time_point &queuedAt);
Wonsik Kim070897f2019-02-15 10:38:54 -080078
79 /**
80 * The component released input buffers from a work item.
81 *
82 * \param frameIndex input frame index
83 * \param arrayIndex index of the buffer at the original |buffers| in
84 * onWorkQueued().
85 * \return buffers[arrayIndex]
86 */
Wonsik Kimab34ed62019-01-31 15:28:46 -080087 std::shared_ptr<C2Buffer> onInputBufferReleased(
88 uint64_t frameIndex, size_t arrayIndex);
Wonsik Kim070897f2019-02-15 10:38:54 -080089
90 /**
91 * The component finished processing a work item.
92 *
93 * \param frameIndex input frame index
94 */
Wonsik Kimab34ed62019-01-31 15:28:46 -080095 void onWorkDone(uint64_t frameIndex);
Wonsik Kim070897f2019-02-15 10:38:54 -080096
97 /**
98 * Flush the pipeline.
99 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800100 void flush();
101
Wonsik Kim070897f2019-02-15 10:38:54 -0800102 /**
103 * \return true if pipeline does not need more work items to proceed
104 * smoothly, considering delays and smoothness factor;
105 * false otherwise.
106 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800107 bool pipelineFull() const;
Wonsik Kim070897f2019-02-15 10:38:54 -0800108
109 /**
110 * Return elapsed processing time of a work item, nth from the longest
111 * processing time to the shortest.
112 *
113 * \param now current timestamp
114 * \param n nth work item, from the longest processing time to the
115 * shortest. It's a 0-based index.
116 * \return elapsed processing time of nth work item.
117 */
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800118 Clock::duration elapsed(const Clock::time_point &now, size_t n) const;
Wonsik Kimab34ed62019-01-31 15:28:46 -0800119
120private:
121 uint32_t mInputDelay;
122 uint32_t mPipelineDelay;
123 uint32_t mOutputDelay;
124 uint32_t mSmoothnessFactor;
125
126 struct Frame {
127 Frame(std::vector<std::shared_ptr<C2Buffer>> &&b,
128 const Clock::time_point &q)
129 : buffers(b),
130 queuedAt(q) {}
131 std::vector<std::shared_ptr<C2Buffer>> buffers;
132 const Clock::time_point queuedAt;
133 };
134 std::map<uint64_t, Frame> mFramesInPipeline;
135};
136
137} // namespace android
138
139#endif // PIPELINE_WATCHER_H_