blob: fa70a285edb5937d9031a8edfdc219705ac2ef6a [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//#define LOG_NDEBUG 0
18#define LOG_TAG "PipelineWatcher"
19
20#include <numeric>
21
22#include <log/log.h>
23
24#include "PipelineWatcher.h"
25
26namespace android {
27
28PipelineWatcher &PipelineWatcher::inputDelay(uint32_t value) {
29 mInputDelay = value;
30 return *this;
31}
32
33PipelineWatcher &PipelineWatcher::pipelineDelay(uint32_t value) {
34 mPipelineDelay = value;
35 return *this;
36}
37
38PipelineWatcher &PipelineWatcher::outputDelay(uint32_t value) {
39 mOutputDelay = value;
40 return *this;
41}
42
43PipelineWatcher &PipelineWatcher::smoothnessFactor(uint32_t value) {
44 mSmoothnessFactor = value;
45 return *this;
46}
47
Houxiang Dai0b573282023-03-11 18:31:56 +080048PipelineWatcher &PipelineWatcher::tunneled(bool value) {
49 mTunneled = value;
50 return *this;
51}
52
Wonsik Kimab34ed62019-01-31 15:28:46 -080053void PipelineWatcher::onWorkQueued(
54 uint64_t frameIndex,
55 std::vector<std::shared_ptr<C2Buffer>> &&buffers,
56 const Clock::time_point &queuedAt) {
57 ALOGV("onWorkQueued(frameIndex=%llu, buffers(size=%zu), queuedAt=%lld)",
58 (unsigned long long)frameIndex,
59 buffers.size(),
60 (long long)queuedAt.time_since_epoch().count());
61 auto it = mFramesInPipeline.find(frameIndex);
62 if (it != mFramesInPipeline.end()) {
63 ALOGD("onWorkQueued: Duplicate frame index (%llu); previous entry removed",
64 (unsigned long long)frameIndex);
65 (void)mFramesInPipeline.erase(it);
66 }
67 (void)mFramesInPipeline.try_emplace(frameIndex, std::move(buffers), queuedAt);
68}
69
70std::shared_ptr<C2Buffer> PipelineWatcher::onInputBufferReleased(
71 uint64_t frameIndex, size_t arrayIndex) {
72 ALOGV("onInputBufferReleased(frameIndex=%llu, arrayIndex=%zu)",
73 (unsigned long long)frameIndex, arrayIndex);
74 auto it = mFramesInPipeline.find(frameIndex);
75 if (it == mFramesInPipeline.end()) {
76 ALOGD("onInputBufferReleased: frameIndex not found (%llu); ignored",
77 (unsigned long long)frameIndex);
78 return nullptr;
79 }
80 if (it->second.buffers.size() <= arrayIndex) {
81 ALOGD("onInputBufferReleased: buffers at %llu: size %zu, requested index: %zu",
82 (unsigned long long)frameIndex, it->second.buffers.size(), arrayIndex);
83 return nullptr;
84 }
85 std::shared_ptr<C2Buffer> buffer(std::move(it->second.buffers[arrayIndex]));
86 ALOGD_IF(!buffer, "onInputBufferReleased: buffer already released (%llu:%zu)",
87 (unsigned long long)frameIndex, arrayIndex);
88 return buffer;
89}
90
91void PipelineWatcher::onWorkDone(uint64_t frameIndex) {
92 ALOGV("onWorkDone(frameIndex=%llu)", (unsigned long long)frameIndex);
93 auto it = mFramesInPipeline.find(frameIndex);
94 if (it == mFramesInPipeline.end()) {
Houxiang Dai0b573282023-03-11 18:31:56 +080095 if (!mTunneled) {
96 ALOGD("onWorkDone: frameIndex not found (%llu); ignored",
97 (unsigned long long)frameIndex);
98 } else {
99 ALOGV("onWorkDone: frameIndex not found (%llu); ignored",
100 (unsigned long long)frameIndex);
101 }
Wonsik Kimab34ed62019-01-31 15:28:46 -0800102 return;
103 }
104 (void)mFramesInPipeline.erase(it);
105}
106
107void PipelineWatcher::flush() {
Wonsik Kim62545252021-01-20 11:25:41 -0800108 ALOGV("flush");
Wonsik Kimab34ed62019-01-31 15:28:46 -0800109 mFramesInPipeline.clear();
110}
111
112bool PipelineWatcher::pipelineFull() const {
113 if (mFramesInPipeline.size() >=
114 mInputDelay + mPipelineDelay + mOutputDelay + mSmoothnessFactor) {
115 ALOGV("pipelineFull: too many frames in pipeline (%zu)", mFramesInPipeline.size());
116 return true;
117 }
118 size_t sizeWithInputReleased = std::count_if(
119 mFramesInPipeline.begin(),
120 mFramesInPipeline.end(),
121 [](const decltype(mFramesInPipeline)::value_type &value) {
122 for (const std::shared_ptr<C2Buffer> &buffer : value.second.buffers) {
123 if (buffer) {
124 return false;
125 }
126 }
127 return true;
128 });
129 if (sizeWithInputReleased >=
130 mPipelineDelay + mOutputDelay + mSmoothnessFactor) {
131 ALOGV("pipelineFull: too many frames in pipeline, with input released (%zu)",
132 sizeWithInputReleased);
133 return true;
134 }
Praveen Chavan7892cea2019-05-08 19:43:30 -0700135
136 size_t sizeWithInputsPending = mFramesInPipeline.size() - sizeWithInputReleased;
137 if (sizeWithInputsPending > mPipelineDelay + mInputDelay + mSmoothnessFactor) {
138 ALOGV("pipelineFull: too many inputs pending (%zu) in pipeline, with inputs released (%zu)",
139 sizeWithInputsPending, sizeWithInputReleased);
140 return true;
141 }
Wonsik Kimab34ed62019-01-31 15:28:46 -0800142 ALOGV("pipeline has room (total: %zu, input released: %zu)",
143 mFramesInPipeline.size(), sizeWithInputReleased);
144 return false;
145}
146
147PipelineWatcher::Clock::duration PipelineWatcher::elapsed(
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800148 const PipelineWatcher::Clock::time_point &now, size_t n) const {
149 if (mFramesInPipeline.size() <= n) {
150 return Clock::duration::zero();
151 }
152 std::vector<Clock::duration> durations;
153 for (const decltype(mFramesInPipeline)::value_type &value : mFramesInPipeline) {
154 Clock::duration elapsed = now - value.second.queuedAt;
155 ALOGV("elapsed: frameIndex = %llu elapsed = %lldms",
156 (unsigned long long)value.first,
157 std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
158 durations.push_back(elapsed);
159 }
Sungtak Leee151ed62019-07-16 17:40:40 -0700160 std::nth_element(durations.begin(), durations.begin() + n, durations.end(),
Wonsik Kim070897f2019-02-15 10:38:54 -0800161 std::greater<Clock::duration>());
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800162 return durations[n];
Wonsik Kimab34ed62019-01-31 15:28:46 -0800163}
164
165} // namespace android