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; |
| 44 | private final Runnable mUpdateSamplingListener = this::updateSamplingListener; |
| 45 | |
| 46 | /** |
| 47 | * The requested sampling bounds that we want to sample from |
| 48 | */ |
| 49 | private final Rect mSamplingRequestBounds = new Rect(); |
| 50 | |
| 51 | /** |
| 52 | * The sampling bounds that are currently registered. |
| 53 | */ |
| 54 | private final Rect mRegisteredSamplingBounds = new Rect(); |
| 55 | private final SamplingCallback mCallback; |
| 56 | private boolean mSamplingEnabled = false; |
| 57 | private boolean mSamplingListenerRegistered = false; |
| 58 | |
| 59 | private float mLastMedianLuma; |
| 60 | private float mCurrentMedianLuma; |
| 61 | private boolean mWaitingOnDraw; |
| 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(); |
| 133 | } |
| 134 | |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public void onViewAttachedToWindow(View view) { |
| 137 | updateSamplingListener(); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void onViewDetachedFromWindow(View view) { |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame] | 142 | stopAndDestroy(); |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 147 | int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 148 | updateSamplingRect(); |
| 149 | } |
| 150 | |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 151 | private void updateSamplingListener() { |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 152 | boolean isSamplingEnabled = mSamplingEnabled |
| 153 | && !mSamplingRequestBounds.isEmpty() |
| 154 | && mWindowVisible |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 155 | && (mSampledView.isAttachedToWindow() || mFirstSamplingAfterStart); |
| 156 | if (isSamplingEnabled) { |
Selim Cinek | 3f0edea | 2019-05-15 19:21:23 -0700 | [diff] [blame] | 157 | ViewRootImpl viewRootImpl = mSampledView.getViewRootImpl(); |
| 158 | SurfaceControl stopLayerControl = null; |
| 159 | if (viewRootImpl != null) { |
| 160 | stopLayerControl = viewRootImpl.getSurfaceControl(); |
| 161 | } |
| 162 | if (stopLayerControl == null || !stopLayerControl.isValid()) { |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 163 | if (!mWaitingOnDraw) { |
| 164 | mWaitingOnDraw = true; |
| 165 | // The view might be attached but we haven't drawn yet, so wait until the |
| 166 | // next draw to update the listener again with the stop layer, such that our |
| 167 | // own drawing doesn't affect the sampling. |
| 168 | if (mHandler.hasCallbacks(mRemoveDrawRunnable)) { |
| 169 | mHandler.removeCallbacks(mRemoveDrawRunnable); |
| 170 | } else { |
| 171 | mSampledView.getViewTreeObserver().addOnDrawListener(mUpdateOnDraw); |
| 172 | } |
| 173 | } |
| 174 | // If there's no valid surface, let's just sample without a stop layer, so we |
| 175 | // don't have to delay |
| 176 | stopLayerControl = null; |
| 177 | } |
| 178 | if (!mSamplingRequestBounds.equals(mRegisteredSamplingBounds) |
| 179 | || mRegisteredStopLayer != stopLayerControl) { |
| 180 | // We only want to reregister if something actually changed |
| 181 | unregisterSamplingListener(); |
| 182 | mSamplingListenerRegistered = true; |
| 183 | CompositionSamplingListener.register(mSamplingListener, DEFAULT_DISPLAY, |
Vishnu Nair | 4bcd152 | 2019-06-25 17:29:27 -0700 | [diff] [blame] | 184 | stopLayerControl, mSamplingRequestBounds); |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 185 | mRegisteredSamplingBounds.set(mSamplingRequestBounds); |
| 186 | mRegisteredStopLayer = stopLayerControl; |
| 187 | } |
| 188 | mFirstSamplingAfterStart = false; |
| 189 | } else { |
| 190 | unregisterSamplingListener(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private void unregisterSamplingListener() { |
| 195 | if (mSamplingListenerRegistered) { |
| 196 | mSamplingListenerRegistered = false; |
| 197 | mRegisteredStopLayer = null; |
| 198 | mRegisteredSamplingBounds.setEmpty(); |
| 199 | CompositionSamplingListener.unregister(mSamplingListener); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private void updateMediaLuma(float medianLuma) { |
| 204 | mCurrentMedianLuma = medianLuma; |
| 205 | |
| 206 | // If the difference between the new luma and the current luma is larger than threshold |
| 207 | // then apply the current luma, this is to prevent small changes causing colors to flicker |
| 208 | if (Math.abs(mCurrentMedianLuma - mLastMedianLuma) > mLuminanceChangeThreshold) { |
| 209 | mCallback.onRegionDarknessChanged(medianLuma < mLuminanceThreshold /* isRegionDark */); |
| 210 | mLastMedianLuma = medianLuma; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | public void updateSamplingRect() { |
| 215 | Rect sampledRegion = mCallback.getSampledRegion(mSampledView); |
| 216 | if (!mSamplingRequestBounds.equals(sampledRegion)) { |
| 217 | mSamplingRequestBounds.set(sampledRegion); |
| 218 | updateSamplingListener(); |
| 219 | } |
| 220 | } |
| 221 | |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 222 | void setWindowVisible(boolean visible) { |
| 223 | mWindowVisible = visible; |
| 224 | updateSamplingListener(); |
| 225 | } |
| 226 | |
| 227 | void dump(PrintWriter pw) { |
| 228 | pw.println("RegionSamplingHelper:"); |
| 229 | pw.println(" sampleView isAttached: " + mSampledView.isAttachedToWindow()); |
| 230 | pw.println(" sampleView isScValid: " + (mSampledView.isAttachedToWindow() |
| 231 | ? mSampledView.getViewRootImpl().getSurfaceControl().isValid() |
| 232 | : "false")); |
| 233 | pw.println(" mSamplingListenerRegistered: " + mSamplingListenerRegistered); |
| 234 | pw.println(" mSamplingRequestBounds: " + mSamplingRequestBounds); |
| 235 | pw.println(" mLastMedianLuma: " + mLastMedianLuma); |
| 236 | pw.println(" mCurrentMedianLuma: " + mCurrentMedianLuma); |
| 237 | pw.println(" mWindowVisible: " + mWindowVisible); |
| 238 | } |
| 239 | |
Selim Cinek | f2ed3ce | 2019-04-17 18:22:17 -0700 | [diff] [blame] | 240 | public interface SamplingCallback { |
| 241 | /** |
| 242 | * Called when the darkness of the sampled region changes |
| 243 | * @param isRegionDark true if the sampled luminance is below the luminance threshold |
| 244 | */ |
| 245 | void onRegionDarknessChanged(boolean isRegionDark); |
| 246 | |
| 247 | /** |
| 248 | * Get the sampled region of interest from the sampled view |
| 249 | * @param sampledView The view that this helper is attached to for convenience |
| 250 | * @return the region to be sampled in sceen coordinates. Return {@code null} to avoid |
| 251 | * sampling in this frame |
| 252 | */ |
| 253 | Rect getSampledRegion(View sampledView); |
| 254 | |
| 255 | /** |
| 256 | * @return if sampling should be enabled in the current configuration |
| 257 | */ |
| 258 | default boolean isSamplingEnabled() { |
| 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | } |