Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #pragma once |
| 18 | |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 19 | #include <utils/Mutex.h> |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 20 | #include <utils/Log.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 21 | #include <utils/RefBase.h> |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 22 | |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 23 | #include <ui/FatVector.h> |
| 24 | |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 25 | #include "FrameInfo.h" |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 26 | #include "FrameMetricsObserver.h" |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 27 | |
| 28 | #include <string.h> |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 29 | #include <mutex> |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 34 | class FrameMetricsReporter { |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 35 | public: |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 36 | FrameMetricsReporter() {} |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 37 | |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 38 | void addObserver(FrameMetricsObserver* observer) { |
| 39 | std::lock_guard lock(mObserversLock); |
| 40 | mObservers.push_back(observer); |
| 41 | } |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 42 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 43 | bool removeObserver(FrameMetricsObserver* observer) { |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 44 | std::lock_guard lock(mObserversLock); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 45 | for (size_t i = 0; i < mObservers.size(); i++) { |
| 46 | if (mObservers[i].get() == observer) { |
| 47 | mObservers.erase(mObservers.begin() + i); |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 54 | bool hasObservers() { |
| 55 | std::lock_guard lock(mObserversLock); |
| 56 | return mObservers.size() > 0; |
| 57 | } |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 58 | |
Siarhei Vishniakou | f0cf18d | 2021-02-26 00:15:04 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Notify observers about the metrics contained in 'stats'. |
| 61 | * If an observer is waiting for present time, notify when 'stats' has present time. |
| 62 | * |
| 63 | * If an observer does not want present time, only notify when 'hasPresentTime' is false. |
| 64 | * Never notify both types of observers from the same callback, because the callback with |
| 65 | * 'hasPresentTime' is sent at a different time than the one without. |
| 66 | */ |
| 67 | void reportFrameMetrics(const int64_t* stats, bool hasPresentTime) { |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 68 | FatVector<sp<FrameMetricsObserver>, 10> copy; |
| 69 | { |
| 70 | std::lock_guard lock(mObserversLock); |
| 71 | copy.reserve(mObservers.size()); |
| 72 | for (size_t i = 0; i < mObservers.size(); i++) { |
Siarhei Vishniakou | f0cf18d | 2021-02-26 00:15:04 +0000 | [diff] [blame] | 73 | const bool wantsPresentTime = mObservers[i]->waitForPresentTime(); |
| 74 | if (hasPresentTime == wantsPresentTime) { |
| 75 | copy.push_back(mObservers[i]); |
| 76 | } |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | for (size_t i = 0; i < copy.size(); i++) { |
| 80 | copy[i]->notify(stats); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 84 | private: |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 85 | FatVector<sp<FrameMetricsObserver>, 10> mObservers GUARDED_BY(mObserversLock); |
| 86 | std::mutex mObserversLock; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 89 | } // namespace uirenderer |
| 90 | } // namespace android |