Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import static android.view.Display.DEFAULT_DISPLAY; |
| 20 | |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.graphics.Rect; |
| 23 | import android.os.Handler; |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 24 | import android.view.CompositionSamplingListener; |
| 25 | import android.view.SurfaceControl; |
| 26 | import android.view.View; |
Selim Cinek | 3f0edea | 2019-05-15 19:21:23 -0700 | [diff] [blame] | 27 | import android.view.ViewRootImpl; |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 28 | import android.view.ViewTreeObserver; |
| 29 | |
| 30 | import com.android.systemui.R; |
| 31 | |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 32 | import java.io.PrintWriter; |
| 33 | |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 34 | /** |
| 35 | * A helper class to sample regions on the screen and inspect its luminosity. |
| 36 | */ |
| 37 | public class RegionSamplingHelper implements View.OnAttachStateChangeListener, |
| 38 | View.OnLayoutChangeListener { |
| 39 | |
| 40 | private final Handler mHandler = new Handler(); |
| 41 | private final View mSampledView; |
| 42 | |
| 43 | private final CompositionSamplingListener mSamplingListener; |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * The requested sampling bounds that we want to sample from |
| 47 | */ |
| 48 | private final Rect mSamplingRequestBounds = new Rect(); |
| 49 | |
| 50 | /** |
| 51 | * The sampling bounds that are currently registered. |
| 52 | */ |
| 53 | private final Rect mRegisteredSamplingBounds = new Rect(); |
| 54 | private final SamplingCallback mCallback; |
| 55 | private boolean mSamplingEnabled = false; |
| 56 | private boolean mSamplingListenerRegistered = false; |
| 57 | |
| 58 | private float mLastMedianLuma; |
| 59 | private float mCurrentMedianLuma; |
| 60 | private boolean mWaitingOnDraw; |
Winson Chung | 657d50a | 2020-06-20 20:48:25 -0700 | [diff] [blame] | 61 | private boolean mIsDestroyed; |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 62 | |
| 63 | // Passing the threshold of this luminance value will make the button black otherwise white |
| 64 | private final float mLuminanceThreshold; |
| 65 | private final float mLuminanceChangeThreshold; |
| 66 | private boolean mFirstSamplingAfterStart; |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 67 | private boolean mWindowVisible; |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 68 | private SurfaceControl mRegisteredStopLayer = null; |
| 69 | private ViewTreeObserver.OnDrawListener mUpdateOnDraw = new ViewTreeObserver.OnDrawListener() { |
| 70 | @Override |
| 71 | public void onDraw() { |
| 72 | // We need to post the remove runnable, since it's not allowed to remove in onDraw |
| 73 | mHandler.post(mRemoveDrawRunnable); |
| 74 | RegionSamplingHelper.this.onDraw(); |
| 75 | } |
| 76 | }; |
| 77 | private Runnable mRemoveDrawRunnable = new Runnable() { |
| 78 | @Override |
| 79 | public void run() { |
| 80 | mSampledView.getViewTreeObserver().removeOnDrawListener(mUpdateOnDraw); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | public RegionSamplingHelper(View sampledView, SamplingCallback samplingCallback) { |
| 85 | mSamplingListener = new CompositionSamplingListener( |
| 86 | sampledView.getContext().getMainExecutor()) { |
| 87 | @Override |
| 88 | public void onSampleCollected(float medianLuma) { |
| 89 | if (mSamplingEnabled) { |
| 90 | updateMediaLuma(medianLuma); |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | mSampledView = sampledView; |
| 95 | mSampledView.addOnAttachStateChangeListener(this); |
| 96 | mSampledView.addOnLayoutChangeListener(this); |
| 97 | |
| 98 | final Resources res = sampledView.getResources(); |
| 99 | mLuminanceThreshold = res.getFloat(R.dimen.navigation_luminance_threshold); |
| 100 | mLuminanceChangeThreshold = res.getFloat(R.dimen.navigation_luminance_change_threshold); |
| 101 | mCallback = samplingCallback; |
| 102 | } |
| 103 | |
| 104 | private void onDraw() { |
| 105 | if (mWaitingOnDraw) { |
| 106 | mWaitingOnDraw = false; |
| 107 | updateSamplingListener(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void start(Rect initialSamplingBounds) { |
| 112 | if (!mCallback.isSamplingEnabled()) { |
| 113 | return; |
| 114 | } |
| 115 | if (initialSamplingBounds != null) { |
| 116 | mSamplingRequestBounds.set(initialSamplingBounds); |
| 117 | } |
| 118 | mSamplingEnabled = true; |
| 119 | // make sure we notify once |
| 120 | mLastMedianLuma = -1; |
| 121 | mFirstSamplingAfterStart = true; |
| 122 | updateSamplingListener(); |
| 123 | } |
| 124 | |
| 125 | void stop() { |
| 126 | mSamplingEnabled = false; |
| 127 | updateSamplingListener(); |
| 128 | } |
| 129 | |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame] | 130 | void stopAndDestroy() { |
| 131 | stop(); |
| 132 | mSamplingListener.destroy(); |
Winson Chung | 657d50a | 2020-06-20 20:48:25 -0700 | [diff] [blame] | 133 | mIsDestroyed = true; |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public void onViewAttachedToWindow(View view) { |
| 138 | updateSamplingListener(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onViewDetachedFromWindow(View view) { |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame] | 143 | stopAndDestroy(); |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 148 | int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 149 | updateSamplingRect(); |
| 150 | } |
| 151 | |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 152 | private void updateSamplingListener() { |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 153 | boolean isSamplingEnabled = mSamplingEnabled |
| 154 | && !mSamplingRequestBounds.isEmpty() |
| 155 | && mWindowVisible |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 156 | && (mSampledView.isAttachedToWindow() || mFirstSamplingAfterStart); |
| 157 | if (isSamplingEnabled) { |
Selim Cinek | 3f0edea | 2019-05-15 19:21:23 -0700 | [diff] [blame] | 158 | ViewRootImpl viewRootImpl = mSampledView.getViewRootImpl(); |
| 159 | SurfaceControl stopLayerControl = null; |
| 160 | if (viewRootImpl != null) { |
| 161 | stopLayerControl = viewRootImpl.getSurfaceControl(); |
| 162 | } |
| 163 | if (stopLayerControl == null || !stopLayerControl.isValid()) { |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 164 | if (!mWaitingOnDraw) { |
| 165 | mWaitingOnDraw = true; |
| 166 | // The view might be attached but we haven't drawn yet, so wait until the |
| 167 | // next draw to update the listener again with the stop layer, such that our |
| 168 | // own drawing doesn't affect the sampling. |
| 169 | if (mHandler.hasCallbacks(mRemoveDrawRunnable)) { |
| 170 | mHandler.removeCallbacks(mRemoveDrawRunnable); |
| 171 | } else { |
| 172 | mSampledView.getViewTreeObserver().addOnDrawListener(mUpdateOnDraw); |
| 173 | } |
| 174 | } |
| 175 | // If there's no valid surface, let's just sample without a stop layer, so we |
| 176 | // don't have to delay |
| 177 | stopLayerControl = null; |
| 178 | } |
| 179 | if (!mSamplingRequestBounds.equals(mRegisteredSamplingBounds) |
| 180 | || mRegisteredStopLayer != stopLayerControl) { |
| 181 | // We only want to reregister if something actually changed |
| 182 | unregisterSamplingListener(); |
| 183 | mSamplingListenerRegistered = true; |
| 184 | CompositionSamplingListener.register(mSamplingListener, DEFAULT_DISPLAY, |
Vishnu Nair | 4bcd152 | 2019-06-25 17:29:27 -0700 | [diff] [blame] | 185 | stopLayerControl, mSamplingRequestBounds); |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 186 | mRegisteredSamplingBounds.set(mSamplingRequestBounds); |
| 187 | mRegisteredStopLayer = stopLayerControl; |
| 188 | } |
| 189 | mFirstSamplingAfterStart = false; |
| 190 | } else { |
| 191 | unregisterSamplingListener(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private void unregisterSamplingListener() { |
| 196 | if (mSamplingListenerRegistered) { |
| 197 | mSamplingListenerRegistered = false; |
| 198 | mRegisteredStopLayer = null; |
| 199 | mRegisteredSamplingBounds.setEmpty(); |
| 200 | CompositionSamplingListener.unregister(mSamplingListener); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private void updateMediaLuma(float medianLuma) { |
| 205 | mCurrentMedianLuma = medianLuma; |
| 206 | |
| 207 | // If the difference between the new luma and the current luma is larger than threshold |
| 208 | // then apply the current luma, this is to prevent small changes causing colors to flicker |
| 209 | if (Math.abs(mCurrentMedianLuma - mLastMedianLuma) > mLuminanceChangeThreshold) { |
| 210 | mCallback.onRegionDarknessChanged(medianLuma < mLuminanceThreshold /* isRegionDark */); |
| 211 | mLastMedianLuma = medianLuma; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | public void updateSamplingRect() { |
| 216 | Rect sampledRegion = mCallback.getSampledRegion(mSampledView); |
| 217 | if (!mSamplingRequestBounds.equals(sampledRegion)) { |
| 218 | mSamplingRequestBounds.set(sampledRegion); |
| 219 | updateSamplingListener(); |
| 220 | } |
| 221 | } |
| 222 | |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 223 | void setWindowVisible(boolean visible) { |
| 224 | mWindowVisible = visible; |
| 225 | updateSamplingListener(); |
| 226 | } |
| 227 | |
| 228 | void dump(PrintWriter pw) { |
| 229 | pw.println("RegionSamplingHelper:"); |
| 230 | pw.println(" sampleView isAttached: " + mSampledView.isAttachedToWindow()); |
| 231 | pw.println(" sampleView isScValid: " + (mSampledView.isAttachedToWindow() |
| 232 | ? mSampledView.getViewRootImpl().getSurfaceControl().isValid() |
Winson Chung | 657d50a | 2020-06-20 20:48:25 -0700 | [diff] [blame] | 233 | : "notAttached")); |
| 234 | pw.println(" mSamplingEnabled: " + mSamplingEnabled); |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 235 | pw.println(" mSamplingListenerRegistered: " + mSamplingListenerRegistered); |
| 236 | pw.println(" mSamplingRequestBounds: " + mSamplingRequestBounds); |
Winson Chung | 657d50a | 2020-06-20 20:48:25 -0700 | [diff] [blame] | 237 | pw.println(" mRegisteredSamplingBounds: " + mRegisteredSamplingBounds); |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 238 | pw.println(" mLastMedianLuma: " + mLastMedianLuma); |
| 239 | pw.println(" mCurrentMedianLuma: " + mCurrentMedianLuma); |
| 240 | pw.println(" mWindowVisible: " + mWindowVisible); |
Winson Chung | 657d50a | 2020-06-20 20:48:25 -0700 | [diff] [blame] | 241 | pw.println(" mWaitingOnDraw: " + mWaitingOnDraw); |
| 242 | pw.println(" mRegisteredStopLayer: " + mRegisteredStopLayer); |
| 243 | pw.println(" mIsDestroyed: " + mIsDestroyed); |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 246 | public interface SamplingCallback { |
| 247 | /** |
| 248 | * Called when the darkness of the sampled region changes |
| 249 | * @param isRegionDark true if the sampled luminance is below the luminance threshold |
| 250 | */ |
| 251 | void onRegionDarknessChanged(boolean isRegionDark); |
| 252 | |
| 253 | /** |
| 254 | * Get the sampled region of interest from the sampled view |
| 255 | * @param sampledView The view that this helper is attached to for convenience |
| 256 | * @return the region to be sampled in sceen coordinates. Return {@code null} to avoid |
| 257 | * sampling in this frame |
| 258 | */ |
| 259 | Rect getSampledRegion(View sampledView); |
| 260 | |
| 261 | /** |
| 262 | * @return if sampling should be enabled in the current configuration |
| 263 | */ |
| 264 | default boolean isSamplingEnabled() { |
| 265 | return true; |
| 266 | } |
| 267 | } |
| 268 | } |