Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [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 android.view; |
| 18 | |
| 19 | import android.graphics.Rect; |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 20 | |
| 21 | import com.android.internal.util.Preconditions; |
| 22 | |
| 23 | import java.util.concurrent.Executor; |
| 24 | |
| 25 | /** |
| 26 | * Listener for sampling the result of the screen composition. |
| 27 | * {@hide} |
| 28 | */ |
| 29 | public abstract class CompositionSamplingListener { |
| 30 | |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame^] | 31 | private long mNativeListener; |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 32 | private final Executor mExecutor; |
| 33 | |
| 34 | public CompositionSamplingListener(Executor executor) { |
| 35 | mExecutor = executor; |
| 36 | mNativeListener = nativeCreate(this); |
| 37 | } |
| 38 | |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame^] | 39 | public void destroy() { |
| 40 | if (mNativeListener == 0) { |
| 41 | return; |
| 42 | } |
| 43 | unregister(this); |
| 44 | nativeDestroy(mNativeListener); |
| 45 | mNativeListener = 0; |
| 46 | } |
| 47 | |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 48 | @Override |
| 49 | protected void finalize() throws Throwable { |
| 50 | try { |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame^] | 51 | destroy(); |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 52 | } finally { |
| 53 | super.finalize(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Reports a luma sample from the registered region. |
| 59 | */ |
| 60 | public abstract void onSampleCollected(float medianLuma); |
| 61 | |
| 62 | /** |
| 63 | * Registers a sampling listener. |
| 64 | */ |
| 65 | public static void register(CompositionSamplingListener listener, |
Vishnu Nair | 4bcd152 | 2019-06-25 17:29:27 -0700 | [diff] [blame] | 66 | int displayId, SurfaceControl stopLayer, Rect samplingArea) { |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame^] | 67 | if (listener.mNativeListener == 0) { |
| 68 | return; |
| 69 | } |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 70 | Preconditions.checkArgument(displayId == Display.DEFAULT_DISPLAY, |
| 71 | "default display only for now"); |
Vishnu Nair | 4bcd152 | 2019-06-25 17:29:27 -0700 | [diff] [blame] | 72 | long nativeStopLayerObject = stopLayer != null ? stopLayer.mNativeObject : 0; |
| 73 | nativeRegister(listener.mNativeListener, nativeStopLayerObject, samplingArea.left, |
| 74 | samplingArea.top, samplingArea.right, samplingArea.bottom); |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Unregisters a sampling listener. |
| 79 | */ |
| 80 | public static void unregister(CompositionSamplingListener listener) { |
Winson Chung | 52d938d | 2019-09-25 16:50:05 -0700 | [diff] [blame^] | 81 | if (listener.mNativeListener == 0) { |
| 82 | return; |
| 83 | } |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 84 | nativeUnregister(listener.mNativeListener); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Dispatch the collected sample. |
| 89 | * |
| 90 | * Called from native code on a binder thread. |
| 91 | */ |
| 92 | private static void dispatchOnSampleCollected(CompositionSamplingListener listener, |
| 93 | float medianLuma) { |
| 94 | listener.mExecutor.execute(() -> listener.onSampleCollected(medianLuma)); |
| 95 | } |
| 96 | |
| 97 | private static native long nativeCreate(CompositionSamplingListener thiz); |
| 98 | private static native void nativeDestroy(long ptr); |
Vishnu Nair | 4bcd152 | 2019-06-25 17:29:27 -0700 | [diff] [blame] | 99 | private static native void nativeRegister(long ptr, long stopLayerObject, |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 100 | int samplingAreaLeft, int top, int right, int bottom); |
| 101 | private static native void nativeUnregister(long ptr); |
| 102 | } |