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

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: Follow review 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
« no previous file with comments | « services/vsync/BUILD.gn ('k') | shell/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..adca2bc38868191bbee83077214148079e1350c4
--- /dev/null
+++ b/services/vsync/src/org/chromium/mojo/vsync/VSyncProviderImpl.java
@@ -0,0 +1,81 @@
+// 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.Looper;
+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 {
+ private final RunLoop mRunLoop;
+ private Choreographer mChoreographer;
+ private AwaitVSyncResponse mCallback;
+ private Binding mBinding = null;
+
+ public VSyncProviderImpl(Core core, Looper looper) {
+ mRunLoop = core.getCurrentRunLoop();
+ // The choreographer must be initialized on a thread with a looper.
+ new Handler(looper).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);
+ 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() {
abarth-chromium 2015/11/17 18:03:36 Why do we need an extra thread hop here? The Sky
qsr 2015/11/18 13:16:56 My guess is that sky is using the main thread to r
+ @Override
+ public void run() {
+ mCallback.call(frameTimeNanos / 1000);
+ mCallback = null;
+ }
+ }, 0);
+ }
+}
« no previous file with comments | « services/vsync/BUILD.gn ('k') | shell/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698