• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
26 namespace android {
27 
28 /**
29  * PipelineWatcher watches the pipeline and infers the status of work items from
30  * events.
31  */
32 class PipelineWatcher {
33 public:
34     typedef std::chrono::steady_clock Clock;
35 
PipelineWatcher()36     PipelineWatcher()
37         : mInputDelay(0),
38           mPipelineDelay(0),
39           mOutputDelay(0),
40           mSmoothnessFactor(0) {}
41     ~PipelineWatcher() = default;
42 
43     /**
44      * \param value the new input delay value
45      * \return  this object
46      */
47     PipelineWatcher &inputDelay(uint32_t value);
48 
49     /**
50      * \param value the new pipeline delay value
51      * \return  this object
52      */
53     PipelineWatcher &pipelineDelay(uint32_t value);
54 
55     /**
56      * \param value the new output delay value
57      * \return  this object
58      */
59     PipelineWatcher &outputDelay(uint32_t value);
60 
61     /**
62      * \param value the new smoothness factor value
63      * \return  this object
64      */
65     PipelineWatcher &smoothnessFactor(uint32_t value);
66 
67     /**
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      */
74     void onWorkQueued(
75             uint64_t frameIndex,
76             std::vector<std::shared_ptr<C2Buffer>> &&buffers,
77             const Clock::time_point &queuedAt);
78 
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      */
87     std::shared_ptr<C2Buffer> onInputBufferReleased(
88             uint64_t frameIndex, size_t arrayIndex);
89 
90     /**
91      * The component finished processing a work item.
92      *
93      * \param frameIndex  input frame index
94      */
95     void onWorkDone(uint64_t frameIndex);
96 
97     /**
98      * Flush the pipeline.
99      */
100     void flush();
101 
102     /**
103      * \return  true  if pipeline does not need more work items to proceed
104      *                smoothly, considering delays and smoothness factor;
105      *          false otherwise.
106      */
107     bool pipelineFull() const;
108 
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      */
118     Clock::duration elapsed(const Clock::time_point &now, size_t n) const;
119 
120 private:
121     uint32_t mInputDelay;
122     uint32_t mPipelineDelay;
123     uint32_t mOutputDelay;
124     uint32_t mSmoothnessFactor;
125 
126     struct Frame {
FrameFrame127         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_
140