Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 20 | import android.os.Looper; |
| 21 | import android.os.MessageQueue; |
| 22 | import android.util.Log; |
| 23 | |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 24 | import dalvik.annotation.optimization.FastNative; |
| 25 | import dalvik.system.CloseGuard; |
| 26 | |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 27 | import java.lang.ref.WeakReference; |
| 28 | |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Provides a low-level mechanism for an application to receive display events |
| 31 | * such as vertical sync. |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 32 | * |
| 33 | * The display event receive is NOT thread safe. Moreover, its methods must only |
| 34 | * be called on the Looper thread to which it is attached. |
| 35 | * |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 36 | * @hide |
| 37 | */ |
| 38 | public abstract class DisplayEventReceiver { |
Jorim Jaggi | 34a0cdb | 2017-06-08 15:40:38 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * When retrieving vsync events, this specifies that the vsync event should happen at the normal |
| 42 | * vsync-app tick. |
| 43 | * <p> |
| 44 | * Needs to be kept in sync with frameworks/native/include/gui/ISurfaceComposer.h |
| 45 | */ |
| 46 | public static final int VSYNC_SOURCE_APP = 0; |
| 47 | |
| 48 | /** |
| 49 | * When retrieving vsync events, this specifies that the vsync event should happen whenever |
| 50 | * Surface Flinger is processing a frame. |
| 51 | * <p> |
| 52 | * Needs to be kept in sync with frameworks/native/include/gui/ISurfaceComposer.h |
| 53 | */ |
| 54 | public static final int VSYNC_SOURCE_SURFACE_FLINGER = 1; |
| 55 | |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Specifies to suppress config changed events from being generated from Surface Flinger. |
| 58 | * <p> |
| 59 | * Needs to be kept in sync with frameworks/native/include/gui/ISurfaceComposer.h |
| 60 | */ |
| 61 | public static final int CONFIG_CHANGED_EVENT_SUPPRESS = 0; |
| 62 | |
| 63 | /** |
| 64 | * Specifies to generate config changed events from Surface Flinger. |
| 65 | * <p> |
| 66 | * Needs to be kept in sync with frameworks/native/include/gui/ISurfaceComposer.h |
| 67 | */ |
| 68 | public static final int CONFIG_CHANGED_EVENT_DISPATCH = 1; |
| 69 | |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 70 | private static final String TAG = "DisplayEventReceiver"; |
| 71 | |
| 72 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 73 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 74 | @UnsupportedAppUsage |
Ashok Bhat | 2728582 | 2013-12-18 18:00:05 +0000 | [diff] [blame] | 75 | private long mReceiverPtr; |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 76 | |
| 77 | // We keep a reference message queue object here so that it is not |
| 78 | // GC'd while the native peer of the receiver is using them. |
| 79 | private MessageQueue mMessageQueue; |
| 80 | |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 81 | private static native long nativeInit(WeakReference<DisplayEventReceiver> receiver, |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 82 | MessageQueue messageQueue, int vsyncSource, int configChanged); |
Ashok Bhat | 2728582 | 2013-12-18 18:00:05 +0000 | [diff] [blame] | 83 | private static native void nativeDispose(long receiverPtr); |
John Reck | e46af37 | 2016-10-03 16:21:30 -0700 | [diff] [blame] | 84 | @FastNative |
Ashok Bhat | 2728582 | 2013-12-18 18:00:05 +0000 | [diff] [blame] | 85 | private static native void nativeScheduleVsync(long receiverPtr); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Creates a display event receiver. |
| 89 | * |
| 90 | * @param looper The looper to use when invoking callbacks. |
Wale Ogunwale | 71f3099 | 2017-06-19 13:53:32 -0700 | [diff] [blame] | 91 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 92 | @UnsupportedAppUsage |
Wale Ogunwale | 71f3099 | 2017-06-19 13:53:32 -0700 | [diff] [blame] | 93 | public DisplayEventReceiver(Looper looper) { |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 94 | this(looper, VSYNC_SOURCE_APP, CONFIG_CHANGED_EVENT_SUPPRESS); |
Wale Ogunwale | 71f3099 | 2017-06-19 13:53:32 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Creates a display event receiver. |
| 99 | * |
| 100 | * @param looper The looper to use when invoking callbacks. |
Jorim Jaggi | 34a0cdb | 2017-06-08 15:40:38 -0700 | [diff] [blame] | 101 | * @param vsyncSource The source of the vsync tick. Must be on of the VSYNC_SOURCE_* values. |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 102 | * @param configChanged Whether to dispatch config changed events. Must be one of the |
| 103 | * CONFIG_CHANGED_EVENT_* values. |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 104 | */ |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 105 | public DisplayEventReceiver(Looper looper, int vsyncSource, int configChanged) { |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 106 | if (looper == null) { |
| 107 | throw new IllegalArgumentException("looper must not be null"); |
| 108 | } |
| 109 | |
| 110 | mMessageQueue = looper.getQueue(); |
Jorim Jaggi | 34a0cdb | 2017-06-08 15:40:38 -0700 | [diff] [blame] | 111 | mReceiverPtr = nativeInit(new WeakReference<DisplayEventReceiver>(this), mMessageQueue, |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 112 | vsyncSource, configChanged); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 113 | |
| 114 | mCloseGuard.open("dispose"); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected void finalize() throws Throwable { |
| 119 | try { |
Jeff Brown | 3e7e7f0 | 2012-08-27 14:33:53 -0700 | [diff] [blame] | 120 | dispose(true); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 121 | } finally { |
| 122 | super.finalize(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Disposes the receiver. |
| 128 | */ |
| 129 | public void dispose() { |
Jeff Brown | 3e7e7f0 | 2012-08-27 14:33:53 -0700 | [diff] [blame] | 130 | dispose(false); |
| 131 | } |
| 132 | |
| 133 | private void dispose(boolean finalized) { |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 134 | if (mCloseGuard != null) { |
Jeff Brown | 3e7e7f0 | 2012-08-27 14:33:53 -0700 | [diff] [blame] | 135 | if (finalized) { |
| 136 | mCloseGuard.warnIfOpen(); |
| 137 | } |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 138 | mCloseGuard.close(); |
| 139 | } |
Jeff Brown | 3e7e7f0 | 2012-08-27 14:33:53 -0700 | [diff] [blame] | 140 | |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 141 | if (mReceiverPtr != 0) { |
| 142 | nativeDispose(mReceiverPtr); |
| 143 | mReceiverPtr = 0; |
| 144 | } |
| 145 | mMessageQueue = null; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Called when a vertical sync pulse is received. |
| 150 | * The recipient should render a frame and then call {@link #scheduleVsync} |
| 151 | * to schedule the next vertical sync pulse. |
| 152 | * |
| 153 | * @param timestampNanos The timestamp of the pulse, in the {@link System#nanoTime()} |
| 154 | * timebase. |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 155 | * @param physicalDisplayId Stable display ID that uniquely describes a (display, port) pair. |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 156 | * @param frame The frame number. Increases by one for each vertical sync interval. |
| 157 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 158 | @UnsupportedAppUsage |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 159 | public void onVsync(long timestampNanos, long physicalDisplayId, int frame) { |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Called when a display hotplug event is received. |
| 164 | * |
| 165 | * @param timestampNanos The timestamp of the event, in the {@link System#nanoTime()} |
| 166 | * timebase. |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 167 | * @param physicalDisplayId Stable display ID that uniquely describes a (display, port) pair. |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 168 | * @param connected True if the display is connected, false if it disconnected. |
| 169 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 170 | @UnsupportedAppUsage |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 171 | public void onHotplug(long timestampNanos, long physicalDisplayId, boolean connected) { |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Ady Abraham | a5a21f7 | 2019-02-13 16:41:59 -0800 | [diff] [blame] | 175 | * Called when a display config changed event is received. |
| 176 | * |
| 177 | * @param timestampNanos The timestamp of the event, in the {@link System#nanoTime()} |
| 178 | * timebase. |
| 179 | * @param physicalDisplayId Stable display ID that uniquely describes a (display, port) pair. |
| 180 | * @param configId The new config Id |
| 181 | */ |
| 182 | public void onConfigChanged(long timestampNanos, long physicalDisplayId, int configId) { |
| 183 | } |
| 184 | |
| 185 | /** |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 186 | * Schedules a single vertical sync pulse to be delivered when the next |
| 187 | * display frame begins. |
| 188 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 189 | @UnsupportedAppUsage |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 190 | public void scheduleVsync() { |
| 191 | if (mReceiverPtr == 0) { |
| 192 | Log.w(TAG, "Attempted to schedule a vertical sync pulse but the display event " |
| 193 | + "receiver has already been disposed."); |
| 194 | } else { |
| 195 | nativeScheduleVsync(mReceiverPtr); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Called from native code. |
| 200 | @SuppressWarnings("unused") |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 201 | @UnsupportedAppUsage |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 202 | private void dispatchVsync(long timestampNanos, long physicalDisplayId, int frame) { |
| 203 | onVsync(timestampNanos, physicalDisplayId, frame); |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Called from native code. |
| 207 | @SuppressWarnings("unused") |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 208 | @UnsupportedAppUsage |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 209 | private void dispatchHotplug(long timestampNanos, long physicalDisplayId, boolean connected) { |
| 210 | onHotplug(timestampNanos, physicalDisplayId, connected); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 211 | } |
Ady Abraham | a5a21f7 | 2019-02-13 16:41:59 -0800 | [diff] [blame] | 212 | |
| 213 | // Called from native code. |
| 214 | @SuppressWarnings("unused") |
| 215 | private void dispatchConfigChanged(long timestampNanos, long physicalDisplayId, int configId) { |
| 216 | onConfigChanged(timestampNanos, physicalDisplayId, configId); |
| 217 | } |
| 218 | |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 219 | } |