| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.graphics.FrameInfo; |
| 21 |
import android.os.Build; |
| 22 |
import android.os.Looper; |
| 23 |
import android.os.MessageQueue; |
| 24 |
import android.util.Log; |
| 25 |
|
| 26 |
import com.android.internal.annotations.VisibleForTesting; |
| 27 |
|
| 28 |
import dalvik.annotation.optimization.FastNative; |
| 29 |
|
| 30 |
import java.lang.ref.WeakReference; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public abstract class DisplayEventReceiver { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public static final int VSYNC_SOURCE_APP = 0; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static final int VSYNC_SOURCE_SURFACE_FLINGER = 1; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static final int EVENT_REGISTRATION_MODE_CHANGED_FLAG = 0x1; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static final int EVENT_REGISTRATION_FRAME_RATE_OVERRIDE_FLAG = 0x2; |
| 72 |
|
| 73 |
private static final String TAG = "DisplayEventReceiver"; |
| 74 |
|
| 75 |
@UnsupportedAppUsage |
| 76 |
private long mReceiverPtr; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private MessageQueue mMessageQueue; |
| 81 |
|
| 82 |
private static native long nativeInit(WeakReference<DisplayEventReceiver> receiver, |
| 83 |
MessageQueue messageQueue, int vsyncSource, int eventRegistration); |
| 84 |
private static native void nativeDispose(long receiverPtr); |
| 85 |
@FastNative |
| 86 |
private static native void nativeScheduleVsync(long receiverPtr); |
| 87 |
private static native VsyncEventData nativeGetLatestVsyncEventData(long receiverPtr); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
@UnsupportedAppUsage |
| 95 |
public DisplayEventReceiver(Looper looper) { |
| 96 |
this(looper, VSYNC_SOURCE_APP, 0); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public DisplayEventReceiver(Looper looper, int vsyncSource, int eventRegistration) { |
| 108 |
if (looper == null) { |
| 109 |
throw new IllegalArgumentException("looper must not be null"); |
| 110 |
} |
| 111 |
|
| 112 |
mMessageQueue = looper.getQueue(); |
| 113 |
mReceiverPtr = nativeInit(new WeakReference<DisplayEventReceiver>(this), mMessageQueue, |
| 114 |
vsyncSource, eventRegistration); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
protected void finalize() throws Throwable { |
| 119 |
try { |
| 120 |
dispose(true); |
| 121 |
} finally { |
| 122 |
super.finalize(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public void dispose() { |
| 130 |
dispose(false); |
| 131 |
} |
| 132 |
|
| 133 |
private void dispose(boolean finalized) { |
| 134 |
if (mReceiverPtr != 0) { |
| 135 |
nativeDispose(mReceiverPtr); |
| 136 |
mReceiverPtr = 0; |
| 137 |
} |
| 138 |
mMessageQueue = null; |
| 139 |
} |
| 140 |
|
| 141 |
static final class VsyncEventData { |
| 142 |
|
| 143 |
static final FrameTimeline[] INVALID_FRAME_TIMELINES = |
| 144 |
{new FrameTimeline(FrameInfo.INVALID_VSYNC_ID, Long.MAX_VALUE, Long.MAX_VALUE)}; |
| 145 |
|
| 146 |
public static class FrameTimeline { |
| 147 |
FrameTimeline(long vsyncId, long expectedPresentTime, long deadline) { |
| 148 |
this.vsyncId = vsyncId; |
| 149 |
this.expectedPresentTime = expectedPresentTime; |
| 150 |
this.deadline = deadline; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public final long vsyncId; |
| 156 |
|
| 157 |
|
| 158 |
public final long expectedPresentTime; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public final long deadline; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public final long frameInterval; |
| 171 |
|
| 172 |
public final FrameTimeline[] frameTimelines; |
| 173 |
|
| 174 |
public final int preferredFrameTimelineIndex; |
| 175 |
|
| 176 |
|
| 177 |
@SuppressWarnings("unused") |
| 178 |
VsyncEventData(FrameTimeline[] frameTimelines, int preferredFrameTimelineIndex, |
| 179 |
long frameInterval) { |
| 180 |
this.frameTimelines = frameTimelines; |
| 181 |
this.preferredFrameTimelineIndex = preferredFrameTimelineIndex; |
| 182 |
this.frameInterval = frameInterval; |
| 183 |
} |
| 184 |
|
| 185 |
VsyncEventData() { |
| 186 |
this.frameInterval = -1; |
| 187 |
this.frameTimelines = INVALID_FRAME_TIMELINES; |
| 188 |
this.preferredFrameTimelineIndex = 0; |
| 189 |
} |
| 190 |
|
| 191 |
public FrameTimeline preferredFrameTimeline() { |
| 192 |
return frameTimelines[preferredFrameTimelineIndex]; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public void onVsync(long timestampNanos, long physicalDisplayId, int frame, |
| 208 |
VsyncEventData vsyncEventData) { |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
@UnsupportedAppUsage |
| 220 |
public void onHotplug(long timestampNanos, long physicalDisplayId, boolean connected) { |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public void onModeChanged(long timestampNanos, long physicalDisplayId, int modeId) { |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static class FrameRateOverride { |
| 238 |
|
| 239 |
public final int uid; |
| 240 |
|
| 241 |
|
| 242 |
public final float frameRateHz; |
| 243 |
|
| 244 |
|
| 245 |
@VisibleForTesting |
| 246 |
public FrameRateOverride(int uid, float frameRateHz) { |
| 247 |
this.uid = uid; |
| 248 |
this.frameRateHz = frameRateHz; |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public String toString() { |
| 253 |
return "{uid=" + uid + " frameRateHz=" + frameRateHz + "}"; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public void onFrameRateOverridesChanged(long timestampNanos, long physicalDisplayId, |
| 266 |
FrameRateOverride[] overrides) { |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
@UnsupportedAppUsage |
| 274 |
public void scheduleVsync() { |
| 275 |
if (mReceiverPtr == 0) { |
| 276 |
Log.w(TAG, "Attempted to schedule a vertical sync pulse but the display event " |
| 277 |
+ "receiver has already been disposed."); |
| 278 |
} else { |
| 279 |
nativeScheduleVsync(mReceiverPtr); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
VsyncEventData getLatestVsyncEventData() { |
| 287 |
return nativeGetLatestVsyncEventData(mReceiverPtr); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
@SuppressWarnings("unused") |
| 292 |
private void dispatchVsync(long timestampNanos, long physicalDisplayId, int frame, |
| 293 |
VsyncEventData vsyncEventData) { |
| 294 |
onVsync(timestampNanos, physicalDisplayId, frame, vsyncEventData); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
@SuppressWarnings("unused") |
| 299 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 300 |
private void dispatchHotplug(long timestampNanos, long physicalDisplayId, boolean connected) { |
| 301 |
onHotplug(timestampNanos, physicalDisplayId, connected); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
@SuppressWarnings("unused") |
| 306 |
private void dispatchModeChanged(long timestampNanos, long physicalDisplayId, int modeId) { |
| 307 |
onModeChanged(timestampNanos, physicalDisplayId, modeId); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
@SuppressWarnings("unused") |
| 312 |
private void dispatchFrameRateOverrides(long timestampNanos, long physicalDisplayId, |
| 313 |
FrameRateOverride[] overrides) { |
| 314 |
onFrameRateOverridesChanged(timestampNanos, physicalDisplayId, overrides); |
| 315 |
} |
| 316 |
|
| 317 |
} |