blob: 3f2dc1244085d9b63f91da39d28adaa1c4251da4 [file] [log] [blame]
Andres Morales06f5bc72015-12-15 15:21:31 -08001/*
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 Jaggi71db8892021-02-03 23:19:29 +010019#include <utils/Mutex.h>
Andres Morales06f5bc72015-12-15 15:21:31 -080020#include <utils/Log.h>
John Reck1bcacfd2017-11-03 10:12:19 -070021#include <utils/RefBase.h>
Andres Morales06f5bc72015-12-15 15:21:31 -080022
Jorim Jaggi71db8892021-02-03 23:19:29 +010023#include <ui/FatVector.h>
24
Andres Morales06f5bc72015-12-15 15:21:31 -080025#include "FrameInfo.h"
Andres Morales910beb82016-02-02 16:19:40 -080026#include "FrameMetricsObserver.h"
Andres Morales06f5bc72015-12-15 15:21:31 -080027
28#include <string.h>
Andres Morales06f5bc72015-12-15 15:21:31 -080029
30namespace android {
31namespace uirenderer {
32
Andres Morales910beb82016-02-02 16:19:40 -080033class FrameMetricsReporter {
Andres Morales06f5bc72015-12-15 15:21:31 -080034public:
Andres Morales11f02d72016-02-12 18:19:52 -080035 FrameMetricsReporter() {}
Andres Morales06f5bc72015-12-15 15:21:31 -080036
Jorim Jaggi71db8892021-02-03 23:19:29 +010037 void addObserver(FrameMetricsObserver* observer) {
38 std::lock_guard lock(mObserversLock);
39 mObservers.push_back(observer);
40 }
Andres Morales06f5bc72015-12-15 15:21:31 -080041
Andres Morales910beb82016-02-02 16:19:40 -080042 bool removeObserver(FrameMetricsObserver* observer) {
Jorim Jaggi71db8892021-02-03 23:19:29 +010043 std::lock_guard lock(mObserversLock);
Andres Morales06f5bc72015-12-15 15:21:31 -080044 for (size_t i = 0; i < mObservers.size(); i++) {
45 if (mObservers[i].get() == observer) {
46 mObservers.erase(mObservers.begin() + i);
47 return true;
48 }
49 }
50 return false;
51 }
52
Jorim Jaggi71db8892021-02-03 23:19:29 +010053 bool hasObservers() {
54 std::lock_guard lock(mObserversLock);
55 return mObservers.size() > 0;
56 }
Andres Morales06f5bc72015-12-15 15:21:31 -080057
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +000058 /**
59 * Notify observers about the metrics contained in 'stats'.
60 * If an observer is waiting for present time, notify when 'stats' has present time.
61 *
62 * If an observer does not want present time, only notify when 'hasPresentTime' is false.
63 * Never notify both types of observers from the same callback, because the callback with
64 * 'hasPresentTime' is sent at a different time than the one without.
65 */
66 void reportFrameMetrics(const int64_t* stats, bool hasPresentTime) {
Jorim Jaggi71db8892021-02-03 23:19:29 +010067 FatVector<sp<FrameMetricsObserver>, 10> copy;
68 {
69 std::lock_guard lock(mObserversLock);
70 copy.reserve(mObservers.size());
71 for (size_t i = 0; i < mObservers.size(); i++) {
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +000072 const bool wantsPresentTime = mObservers[i]->waitForPresentTime();
73 if (hasPresentTime == wantsPresentTime) {
74 copy.push_back(mObservers[i]);
75 }
Jorim Jaggi71db8892021-02-03 23:19:29 +010076 }
77 }
78 for (size_t i = 0; i < copy.size(); i++) {
79 copy[i]->notify(stats);
Andres Morales06f5bc72015-12-15 15:21:31 -080080 }
81 }
82
Andres Morales06f5bc72015-12-15 15:21:31 -080083private:
Jorim Jaggi71db8892021-02-03 23:19:29 +010084 FatVector<sp<FrameMetricsObserver>, 10> mObservers GUARDED_BY(mObserversLock);
85 std::mutex mObserversLock;
Andres Morales06f5bc72015-12-15 15:21:31 -080086};
87
Chris Blume7b8a8082018-11-30 15:51:58 -080088} // namespace uirenderer
89} // namespace android