Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Unified Diff: services/vsync/src/org/chromium/mojo/vsync/VSyncProviderImpl.java

Issue 1458453003: Add mojo service to get notified of vsync. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Add comment Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/vsync/src/org/chromium/mojo/vsync/VSyncProviderImpl.java
diff --git a/services/vsync/src/org/chromium/mojo/vsync/VSyncProviderImpl.java b/services/vsync/src/org/chromium/mojo/vsync/VSyncProviderImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..291c451cb1d6d16e67c6543f6a6402bbdbc20589
--- /dev/null
+++ b/services/vsync/src/org/chromium/mojo/vsync/VSyncProviderImpl.java
@@ -0,0 +1,82 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo.vsync;
+
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.view.Choreographer;
+
+import org.chromium.mojo.system.Core;
+import org.chromium.mojo.system.MojoException;
+import org.chromium.mojo.system.RunLoop;
+import org.chromium.mojom.vsync.VSyncProvider;
+
+/**
+ * Android implementation of VSyncProvider.
+ */
+public class VSyncProviderImpl implements VSyncProvider, Choreographer.FrameCallback {
ppi 2015/11/17 16:40:46 nit: listening to FrameCallback should be an imple
qsr 2015/11/17 17:19:40 As discussed, this class is already an implementat
+ private final RunLoop mRunLoop;
+ private Choreographer mChoreographer;
+ private AwaitVSyncResponse mCallback;
+ private Binding mBinding = null;
+
+ public VSyncProviderImpl(Core core, HandlerThread mHandlerThread) {
+ mRunLoop = core.getCurrentRunLoop();
+ // The choreographer must be initialized on a thread with a looper.
+ new Handler(mHandlerThread.getLooper())
+ .post(new Runnable() {
+ @Override
+ public void run() {
+ final Choreographer choreographer = Choreographer.getInstance();
+ mRunLoop.postDelayedTask(new Runnable() {
+ @Override
+ public void run() {
+ mChoreographer = choreographer;
+ if (mCallback != null) {
+ mChoreographer.postFrameCallback(VSyncProviderImpl.this);
+ }
+ }
+ }, 0);
+ }
+ });
+ }
+
+ public void setBinding(Binding binding) {
+ if (mBinding != null) {
+ mBinding.unbind().close();
+ }
+ mBinding = binding;
+ }
+
+ @Override
+ public void close() {}
+
+ @Override
+ public void onConnectionError(MojoException e) {}
+
+ @Override
+ public void awaitVSync(final AwaitVSyncResponse callback) {
+ if (mCallback != null) {
+ setBinding(null);
ppi 2015/11/17 16:40:46 should we clear mCallback here? Is it a problem if
qsr 2015/11/17 17:19:41 It is not a problem in the current state. If a cle
+ return;
+ }
+ mCallback = callback;
+ if (mChoreographer != null) {
+ // Posting from another thread is allowed on a choreographer.
+ mChoreographer.postFrameCallback(this);
+ }
+ }
+
+ @Override
+ public void doFrame(final long frameTimeNanos) {
+ mRunLoop.postDelayedTask(new Runnable() {
+ @Override
+ public void run() {
+ mCallback.call(frameTimeNanos / 1000);
+ mCallback = null;
+ }
+ }, 0);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698