Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Fix queue event lifecycle on googlevr and noapi platforms #944

Merged
merged 1 commit into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions app/src/googlevr/java/org/mozilla/vrbrowser/PlatformActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.vr.ndk.base.AndroidCompat;
import com.google.vr.ndk.base.GvrLayout;

import java.util.ArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

Expand All @@ -30,6 +32,8 @@ public static boolean filterPermission(final String aPermission) {
private GvrLayout mLayout;
private GLSurfaceView mView;
private static final String FLAT_ACTIVITY_CLASSNAME = "org.mozilla.vrbrowser.BrowserActivity";
private ArrayList<Runnable> mPendingEvents;
private boolean mSurfaceCreated = false;

private final Runnable activityDestroyedRunnable = new Runnable() {
@Override
Expand Down Expand Up @@ -63,6 +67,7 @@ protected void onCreate(Bundle savedInstanceState) {
Log.e(LOGTAG, "PlatformActivity onCreate");
super.onCreate(savedInstanceState);

mPendingEvents = new ArrayList<>();
AndroidCompat.setVrModeEnabled(this, true);

// Keep the screen on
Expand All @@ -85,6 +90,8 @@ protected void onCreate(Bundle savedInstanceState) {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.e(LOGTAG, "In onSurfaceCreated");
activityCreated(getAssets(), mLayout.getGvrApi().getNativeGvrContext());
mSurfaceCreated = true;
notifyPendingEvents();
}

@Override
Expand Down Expand Up @@ -122,7 +129,7 @@ public void run() {
protected void onPause() {
Log.e(LOGTAG, "PlatformActivity onPause");
synchronized (activityPausedRunnable) {
mView.queueEvent(activityPausedRunnable);
queueRunnable(activityPausedRunnable);
try {
activityPausedRunnable.wait();
} catch(InterruptedException e) {
Expand All @@ -140,7 +147,7 @@ protected void onResume() {
super.onResume();
mLayout.onResume();
mView.onResume();
mView.queueEvent(activityResumedRunnable);
queueRunnable(activityResumedRunnable);
setImmersiveSticky();
}

Expand All @@ -149,7 +156,7 @@ protected void onDestroy() {
Log.e(LOGTAG, "PlatformActivity onDestroy");
super.onDestroy();
synchronized (activityDestroyedRunnable) {
mView.queueEvent(activityDestroyedRunnable);
queueRunnable(activityDestroyedRunnable);
try {
activityDestroyedRunnable.wait();
} catch(InterruptedException e) {
Expand All @@ -170,8 +177,27 @@ void setImmersiveSticky() {
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}


void queueRunnable(Runnable aRunnable) {
mView.queueEvent(aRunnable);
if (mSurfaceCreated) {
mView.queueEvent(aRunnable);
} else {
synchronized (mPendingEvents) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are events getting queued off of the main UI thread?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queueRunnable is always called from the main thread. But the pendingEvents array is iterated also when the surfaceCreated callback is received to notify the pending events

mPendingEvents.add(aRunnable);
}
if (mSurfaceCreated) {
notifyPendingEvents();
}
}
}

private void notifyPendingEvents() {
synchronized (mPendingEvents) {
for (Runnable runnable: mPendingEvents) {
mView.queueEvent(runnable);
}
mPendingEvents.clear();
}
}

private native void activityCreated(Object aAssetManager, final long aContext);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,15 +712,15 @@ BrowserWorld::AddWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement) {
const float worldHeight = worldWidth / aspect;

WidgetPtr widget;
if (aPlacement->cylinder && m.cylinderDensity > 0 && m.device) {
if (aPlacement->cylinder && m.cylinderDensity > 0) {
VRLayerCylinderPtr layer = m.device->CreateLayerCylinder(textureWidth, textureHeight, VRLayerQuad::SurfaceType::AndroidSurface);
CylinderPtr cylinder = Cylinder::Create(m.create, layer);
widget = Widget::Create(m.context, aHandle, textureWidth, textureHeight, worldWidth, worldHeight, cylinder);
}

if (!widget) {
VRLayerQuadPtr layer;
if (aPlacement->layer && m.device) {
if (aPlacement->layer) {
layer = m.device->CreateLayerQuad(textureWidth, textureHeight, VRLayerQuad::SurfaceType::AndroidSurface);
}

Expand Down
61 changes: 33 additions & 28 deletions app/src/noapi/java/org/mozilla/vrbrowser/PlatformActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import android.view.View;
import android.widget.ImageButton;

import java.util.ArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

Expand All @@ -26,6 +28,8 @@ public static boolean filterPermission(final String aPermission) {
}

private GLSurfaceView mView;
private ArrayList<Runnable> mPendingEvents;
private boolean mSurfaceCreated = false;

private final Runnable activityDestroyedRunnable = new Runnable() {
@Override
Expand Down Expand Up @@ -60,6 +64,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.noapi_layout);
mPendingEvents = new ArrayList<>();
mView = findViewById(R.id.gl_view);
mView.setEGLContextClientVersion(3);
mView.setEGLConfigChooser(8, 8, 8, 0, 16, 0);
Expand All @@ -71,6 +76,8 @@ protected void onCreate(Bundle savedInstanceState) {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.e(LOGTAG, "In onSurfaceCreated");
activityCreated(getAssets());
mSurfaceCreated = true;
notifyPendingEvents();
}

@Override
Expand Down Expand Up @@ -112,12 +119,7 @@ public boolean onTouchEvent(MotionEvent aEvent) {

final float xx = aEvent.getX(0);
final float yy = aEvent.getY(0);
mView.queueEvent(new Runnable() {
@Override
public void run() {
touchEvent(isDown, xx, yy);
}
});
queueRunnable(() -> touchEvent(isDown, xx, yy));
return true;
}

Expand All @@ -136,20 +138,15 @@ public boolean onGenericMotionEvent(MotionEvent aEvent) {

final float xx = aEvent.getX(0);
final float yy = aEvent.getY(0);
mView.queueEvent(new Runnable() {
@Override
public void run() {
touchEvent(false, xx, yy);
}
});
queueRunnable(() -> touchEvent(false, xx, yy));
return true;
}

@Override
protected void onPause() {
Log.e(LOGTAG, "PlatformActivity onPause");
synchronized (activityPausedRunnable) {
mView.queueEvent(activityPausedRunnable);
queueRunnable(activityPausedRunnable);
try {
activityPausedRunnable.wait();
} catch(InterruptedException e) {
Expand All @@ -165,7 +162,7 @@ protected void onResume() {
Log.e(LOGTAG, "PlatformActivity onResume");
super.onResume();
mView.onResume();
mView.queueEvent(activityResumedRunnable);
queueRunnable(activityResumedRunnable);
// setImmersiveSticky();
}

Expand All @@ -174,7 +171,7 @@ protected void onDestroy() {
Log.e(LOGTAG, "PlatformActivity onDestroy");
super.onDestroy();
synchronized (activityDestroyedRunnable) {
mView.queueEvent(activityDestroyedRunnable);
queueRunnable(activityDestroyedRunnable);
try {
activityDestroyedRunnable.wait();
} catch(InterruptedException e) {
Expand All @@ -196,7 +193,25 @@ protected void onDestroy() {
// }

void queueRunnable(Runnable aRunnable) {
mView.queueEvent(aRunnable);
if (mSurfaceCreated) {
mView.queueEvent(aRunnable);
} else {
synchronized (mPendingEvents) {
mPendingEvents.add(aRunnable);
}
if (mSurfaceCreated) {
notifyPendingEvents();
}
}
}

private void notifyPendingEvents() {
synchronized (mPendingEvents) {
for (Runnable runnable: mPendingEvents) {
mView.queueEvent(runnable);
}
mPendingEvents.clear();
}
}

private void setupUI() {
Expand Down Expand Up @@ -261,21 +276,11 @@ public void onClick(View view) {
}

private void dispatchMoveAxis(final float aX, final float aY, final float aZ) {
mView.queueEvent(new Runnable() {
@Override
public void run() {
moveAxis(aX, aY, aZ);
}
});
queueRunnable(() -> moveAxis(aX, aY, aZ));
}

private void dispatchRotateHeading(final float aHeading) {
mView.queueEvent(new Runnable() {
@Override
public void run() {
rotateHeading(aHeading);
}
});
queueRunnable(() -> rotateHeading(aHeading));
}

private native void activityCreated(Object aAssetManager);
Expand Down