Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021 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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "TunnelModeEnabledReporter" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
| 21 | #include <algorithm> |
| 22 | |
| 23 | #include "Layer.h" |
| 24 | #include "SurfaceFlinger.h" |
| 25 | #include "TunnelModeEnabledReporter.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | TunnelModeEnabledReporter::TunnelModeEnabledReporter(SurfaceFlinger& flinger) : mFlinger(flinger) {} |
| 30 | |
| 31 | void TunnelModeEnabledReporter::updateTunnelModeStatus() { |
| 32 | bool tunnelModeEnabled = false; |
| 33 | mFlinger.mCurrentState.traverse([&](Layer* layer) { |
| 34 | auto& currentState = layer->getCurrentState(); |
| 35 | if (currentState.sidebandStream != nullptr) { |
| 36 | tunnelModeEnabled = true; |
| 37 | return; |
| 38 | } |
| 39 | }); |
| 40 | dispatchTunnelModeEnabled(tunnelModeEnabled); |
| 41 | } |
| 42 | |
| 43 | void TunnelModeEnabledReporter::dispatchTunnelModeEnabled(bool tunnelModeEnabled) { |
| 44 | std::vector<sp<gui::ITunnelModeEnabledListener>> localListeners; |
| 45 | { |
| 46 | std::scoped_lock lock(mMutex); |
| 47 | if (mTunnelModeEnabled == tunnelModeEnabled) { |
| 48 | return; |
| 49 | } |
| 50 | mTunnelModeEnabled = tunnelModeEnabled; |
| 51 | |
| 52 | std::transform(mListeners.begin(), mListeners.end(), std::back_inserter(localListeners), |
| 53 | [](const std::pair<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>>& |
| 54 | entry) { return entry.second; }); |
| 55 | } |
| 56 | |
| 57 | for (sp<gui::ITunnelModeEnabledListener>& listener : localListeners) { |
| 58 | listener->onTunnelModeEnabledChanged(tunnelModeEnabled); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void TunnelModeEnabledReporter::binderDied(const wp<IBinder>& who) { |
| 63 | std::scoped_lock lock(mMutex); |
| 64 | mListeners.erase(who); |
| 65 | } |
| 66 | |
| 67 | void TunnelModeEnabledReporter::addListener(const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 68 | sp<IBinder> asBinder = IInterface::asBinder(listener); |
| 69 | asBinder->linkToDeath(this); |
| 70 | bool tunnelModeEnabled = false; |
| 71 | { |
| 72 | std::scoped_lock lock(mMutex); |
| 73 | mListeners.emplace(wp<IBinder>(asBinder), listener); |
| 74 | tunnelModeEnabled = mTunnelModeEnabled; |
| 75 | } |
| 76 | listener->onTunnelModeEnabledChanged(tunnelModeEnabled); |
| 77 | } |
| 78 | |
| 79 | void TunnelModeEnabledReporter::removeListener( |
| 80 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 81 | std::lock_guard lock(mMutex); |
| 82 | mListeners.erase(wp<IBinder>(IInterface::asBinder(listener))); |
| 83 | } |
| 84 | |
| 85 | } // namespace android |