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