1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2021 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG 18*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "TunnelModeEnabledReporter" 19*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS 20*38e8c45fSAndroid Build Coastguard Worker 21*38e8c45fSAndroid Build Coastguard Worker #include <algorithm> 22*38e8c45fSAndroid Build Coastguard Worker 23*38e8c45fSAndroid Build Coastguard Worker #include "Layer.h" 24*38e8c45fSAndroid Build Coastguard Worker #include "SurfaceFlinger.h" 25*38e8c45fSAndroid Build Coastguard Worker #include "TunnelModeEnabledReporter.h" 26*38e8c45fSAndroid Build Coastguard Worker 27*38e8c45fSAndroid Build Coastguard Worker namespace android { 28*38e8c45fSAndroid Build Coastguard Worker TunnelModeEnabledReporter()29*38e8c45fSAndroid Build Coastguard WorkerTunnelModeEnabledReporter::TunnelModeEnabledReporter() {} 30*38e8c45fSAndroid Build Coastguard Worker updateTunnelModeStatus()31*38e8c45fSAndroid Build Coastguard Workervoid TunnelModeEnabledReporter::updateTunnelModeStatus() { 32*38e8c45fSAndroid Build Coastguard Worker bool tunnelModeEnabled = mTunnelModeCount > 0; 33*38e8c45fSAndroid Build Coastguard Worker dispatchTunnelModeEnabled(tunnelModeEnabled); 34*38e8c45fSAndroid Build Coastguard Worker } 35*38e8c45fSAndroid Build Coastguard Worker dispatchTunnelModeEnabled(bool tunnelModeEnabled)36*38e8c45fSAndroid Build Coastguard Workervoid TunnelModeEnabledReporter::dispatchTunnelModeEnabled(bool tunnelModeEnabled) { 37*38e8c45fSAndroid Build Coastguard Worker std::vector<sp<gui::ITunnelModeEnabledListener>> localListeners; 38*38e8c45fSAndroid Build Coastguard Worker { 39*38e8c45fSAndroid Build Coastguard Worker std::scoped_lock lock(mMutex); 40*38e8c45fSAndroid Build Coastguard Worker if (mTunnelModeEnabled == tunnelModeEnabled) { 41*38e8c45fSAndroid Build Coastguard Worker return; 42*38e8c45fSAndroid Build Coastguard Worker } 43*38e8c45fSAndroid Build Coastguard Worker mTunnelModeEnabled = tunnelModeEnabled; 44*38e8c45fSAndroid Build Coastguard Worker 45*38e8c45fSAndroid Build Coastguard Worker std::transform(mListeners.begin(), mListeners.end(), std::back_inserter(localListeners), 46*38e8c45fSAndroid Build Coastguard Worker [](const std::pair<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>>& 47*38e8c45fSAndroid Build Coastguard Worker entry) { return entry.second; }); 48*38e8c45fSAndroid Build Coastguard Worker } 49*38e8c45fSAndroid Build Coastguard Worker 50*38e8c45fSAndroid Build Coastguard Worker for (sp<gui::ITunnelModeEnabledListener>& listener : localListeners) { 51*38e8c45fSAndroid Build Coastguard Worker listener->onTunnelModeEnabledChanged(tunnelModeEnabled); 52*38e8c45fSAndroid Build Coastguard Worker } 53*38e8c45fSAndroid Build Coastguard Worker } 54*38e8c45fSAndroid Build Coastguard Worker binderDied(const wp<IBinder> & who)55*38e8c45fSAndroid Build Coastguard Workervoid TunnelModeEnabledReporter::binderDied(const wp<IBinder>& who) { 56*38e8c45fSAndroid Build Coastguard Worker std::scoped_lock lock(mMutex); 57*38e8c45fSAndroid Build Coastguard Worker mListeners.erase(who); 58*38e8c45fSAndroid Build Coastguard Worker } 59*38e8c45fSAndroid Build Coastguard Worker addListener(const sp<gui::ITunnelModeEnabledListener> & listener)60*38e8c45fSAndroid Build Coastguard Workervoid TunnelModeEnabledReporter::addListener(const sp<gui::ITunnelModeEnabledListener>& listener) { 61*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> asBinder = IInterface::asBinder(listener); 62*38e8c45fSAndroid Build Coastguard Worker asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this)); 63*38e8c45fSAndroid Build Coastguard Worker bool tunnelModeEnabled = false; 64*38e8c45fSAndroid Build Coastguard Worker { 65*38e8c45fSAndroid Build Coastguard Worker std::scoped_lock lock(mMutex); 66*38e8c45fSAndroid Build Coastguard Worker mListeners.emplace(wp<IBinder>(asBinder), listener); 67*38e8c45fSAndroid Build Coastguard Worker tunnelModeEnabled = mTunnelModeEnabled; 68*38e8c45fSAndroid Build Coastguard Worker } 69*38e8c45fSAndroid Build Coastguard Worker listener->onTunnelModeEnabledChanged(tunnelModeEnabled); 70*38e8c45fSAndroid Build Coastguard Worker } 71*38e8c45fSAndroid Build Coastguard Worker removeListener(const sp<gui::ITunnelModeEnabledListener> & listener)72*38e8c45fSAndroid Build Coastguard Workervoid TunnelModeEnabledReporter::removeListener( 73*38e8c45fSAndroid Build Coastguard Worker const sp<gui::ITunnelModeEnabledListener>& listener) { 74*38e8c45fSAndroid Build Coastguard Worker std::lock_guard lock(mMutex); 75*38e8c45fSAndroid Build Coastguard Worker mListeners.erase(wp<IBinder>(IInterface::asBinder(listener))); 76*38e8c45fSAndroid Build Coastguard Worker } 77*38e8c45fSAndroid Build Coastguard Worker 78*38e8c45fSAndroid Build Coastguard Worker } // namespace android 79