blob: 3c8aa86dd20965d874404ae58006777015dad203 [file] [log] [blame]
Selim Cinekf2ed3ce2019-04-17 18:22:17 -07001/*
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
17package com.android.systemui.statusbar.phone;
18
19import static android.view.Display.DEFAULT_DISPLAY;
20
Selim Cinekf2ed3ce2019-04-17 18:22:17 -070021import android.content.res.Resources;
22import android.graphics.Rect;
23import android.os.Handler;
Selim Cinekf2ed3ce2019-04-17 18:22:17 -070024import android.view.CompositionSamplingListener;
25import android.view.SurfaceControl;
26import android.view.View;
Selim Cinek3f0edea2019-05-15 19:21:23 -070027import android.view.ViewRootImpl;
Selim Cinekf2ed3ce2019-04-17 18:22:17 -070028import android.view.ViewTreeObserver;
29
30import com.android.systemui.R;
31
Vinit Nayak3f35db52019-08-08 17:31:48 -070032import java.io.PrintWriter;
33
Selim Cinekf2ed3ce2019-04-17 18:22:17 -070034/**
35 * A helper class to sample regions on the screen and inspect its luminosity.
36 */
37public 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 Cinekf2ed3ce2019-04-17 18:22:17 -070044
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 Chung657d50a2020-06-20 20:48:25 -070061 private boolean mIsDestroyed;
Selim Cinekf2ed3ce2019-04-17 18:22:17 -070062
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 Nayak3f35db52019-08-08 17:31:48 -070067 private boolean mWindowVisible;
Selim Cinekf2ed3ce2019-04-17 18:22:17 -070068 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 Chung52d938d2019-09-25 16:50:05 -0700130 void stopAndDestroy() {
131 stop();
132 mSamplingListener.destroy();
Winson Chung657d50a2020-06-20 20:48:25 -0700133 mIsDestroyed = true;
Winson Chung52d938d2019-09-25 16:50:05 -0700134 }
135
Selim Cinekf2ed3ce2019-04-17 18:22:17 -0700136 @Override
137 public void onViewAttachedToWindow(View view) {
138 updateSamplingListener();
139 }
140
141 @Override
142 public void onViewDetachedFromWindow(View view) {
Winson Chung52d938d2019-09-25 16:50:05 -0700143 stopAndDestroy();
Selim Cinekf2ed3ce2019-04-17 18:22:17 -0700144 }
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 Cinekf2ed3ce2019-04-17 18:22:17 -0700152 private void updateSamplingListener() {
Vinit Nayak3f35db52019-08-08 17:31:48 -0700153 boolean isSamplingEnabled = mSamplingEnabled
154 && !mSamplingRequestBounds.isEmpty()
155 && mWindowVisible
Selim Cinekf2ed3ce2019-04-17 18:22:17 -0700156 && (mSampledView.isAttachedToWindow() || mFirstSamplingAfterStart);
157 if (isSamplingEnabled) {
Selim Cinek3f0edea2019-05-15 19:21:23 -0700158 ViewRootImpl viewRootImpl = mSampledView.getViewRootImpl();
159 SurfaceControl stopLayerControl = null;
160 if (viewRootImpl != null) {
161 stopLayerControl = viewRootImpl.getSurfaceControl();
162 }
163 if (stopLayerControl == null || !stopLayerControl.isValid()) {
Selim Cinekf2ed3ce2019-04-17 18:22:17 -0700164 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 Nair4bcd1522019-06-25 17:29:27 -0700185 stopLayerControl, mSamplingRequestBounds);
Selim Cinekf2ed3ce2019-04-17 18:22:17 -0700186 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 Nayak3f35db52019-08-08 17:31:48 -0700223 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 Chung657d50a2020-06-20 20:48:25 -0700233 : "notAttached"));
234 pw.println(" mSamplingEnabled: " + mSamplingEnabled);
Vinit Nayak3f35db52019-08-08 17:31:48 -0700235 pw.println(" mSamplingListenerRegistered: " + mSamplingListenerRegistered);
236 pw.println(" mSamplingRequestBounds: " + mSamplingRequestBounds);
Winson Chung657d50a2020-06-20 20:48:25 -0700237 pw.println(" mRegisteredSamplingBounds: " + mRegisteredSamplingBounds);
Vinit Nayak3f35db52019-08-08 17:31:48 -0700238 pw.println(" mLastMedianLuma: " + mLastMedianLuma);
239 pw.println(" mCurrentMedianLuma: " + mCurrentMedianLuma);
240 pw.println(" mWindowVisible: " + mWindowVisible);
Winson Chung657d50a2020-06-20 20:48:25 -0700241 pw.println(" mWaitingOnDraw: " + mWaitingOnDraw);
242 pw.println(" mRegisteredStopLayer: " + mRegisteredStopLayer);
243 pw.println(" mIsDestroyed: " + mIsDestroyed);
Vinit Nayak3f35db52019-08-08 17:31:48 -0700244 }
245
Selim Cinekf2ed3ce2019-04-17 18:22:17 -0700246 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}