| 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 | |
| 19 | #include <utils/Mutex.h> |
| 20 | #include <utils/Log.h> |
| 21 | #include <utils/RefBase.h> |
| 22 | |
| 23 | #include <ui/FatVector.h> |
| 24 | |
| 25 | #include "FrameInfo.h" |
| 26 | #include "FrameMetricsObserver.h" |
| 27 | |
| 28 | #include <string.h> |
| 29 | #include <mutex> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
| 34 | class FrameMetricsReporter { |
| 35 | public: |
| 36 | FrameMetricsReporter() {} |
| 37 | |
| 38 | void addObserver(FrameMetricsObserver* observer) { |
| 39 | std::lock_guard lock(mObserversLock); |
| 40 | mObservers.push_back(observer); |
| 41 | } |
| 42 | |
| 43 | bool removeObserver(FrameMetricsObserver* observer) { |
| 44 | std::lock_guard lock(mObserversLock); |
| 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 | |
| 54 | bool hasObservers() { |
| 55 | std::lock_guard lock(mObserversLock); |
| 56 | return mObservers.size() > 0; |
| 57 | } |
| 58 | |
| 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 | * The 'frameNumber' and 'surfaceControlId' associated to the frame whose's stats are being |
| 68 | * reported are used to determine whether or not the stats should be reported. We won't report |
| 69 | * stats of frames that are from "old" surfaces (i.e. with surfaceControlIds older than the one |
| 70 | * the observer was attached on) nor those that are from "old" frame numbers. |
| 71 | */ |
| 72 | void reportFrameMetrics(const int64_t* stats, bool hasPresentTime, uint64_t frameNumber, |
| 73 | int32_t surfaceControlId); |
| 74 | |
| 75 | private: |
| 76 | FatVector<sp<FrameMetricsObserver>, 10> mObservers GUARDED_BY(mObserversLock); |
| 77 | std::mutex mObserversLock; |
| 78 | }; |
| 79 | |
| 80 | } // namespace uirenderer |
| 81 | } // namespace android |