Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 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 | /** |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 29 | * PipelineWatcher watches the pipeline and infers the status of work items from |
| 30 | * events. |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 31 | */ |
| 32 | class PipelineWatcher { |
| 33 | public: |
| 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 Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 43 | /** |
| 44 | * \param value the new input delay value |
| 45 | * \return this object |
| 46 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 47 | PipelineWatcher &inputDelay(uint32_t value); |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 48 | |
| 49 | /** |
| 50 | * \param value the new pipeline delay value |
| 51 | * \return this object |
| 52 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 53 | PipelineWatcher &pipelineDelay(uint32_t value); |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 54 | |
| 55 | /** |
| 56 | * \param value the new output delay value |
| 57 | * \return this object |
| 58 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 59 | PipelineWatcher &outputDelay(uint32_t value); |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 60 | |
| 61 | /** |
| 62 | * \param value the new smoothness factor value |
| 63 | * \return this object |
| 64 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 65 | PipelineWatcher &smoothnessFactor(uint32_t value); |
| 66 | |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 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 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 74 | void onWorkQueued( |
| 75 | uint64_t frameIndex, |
| 76 | std::vector<std::shared_ptr<C2Buffer>> &&buffers, |
| 77 | const Clock::time_point &queuedAt); |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 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 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 87 | std::shared_ptr<C2Buffer> onInputBufferReleased( |
| 88 | uint64_t frameIndex, size_t arrayIndex); |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 89 | |
| 90 | /** |
| 91 | * The component finished processing a work item. |
| 92 | * |
| 93 | * \param frameIndex input frame index |
| 94 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 95 | void onWorkDone(uint64_t frameIndex); |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 96 | |
| 97 | /** |
| 98 | * Flush the pipeline. |
| 99 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 100 | void flush(); |
| 101 | |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 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 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 107 | bool pipelineFull() const; |
Wonsik Kim | 070897f | 2019-02-15 10:38:54 -0800 | [diff] [blame^] | 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 | */ |
Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 118 | Clock::duration elapsed(const Clock::time_point &now, size_t n) const; |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 119 | |
| 120 | private: |
| 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_ |