Skip to content

Commit 5534bf6

Browse files
authored
Android: Render on animation tick instead of calling invalidate (#179)
1 parent 0882d93 commit 5534bf6

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

Modules/@babylonjs/react-native/android/src/main/java/com/babylonreactnative/EngineView.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.annotation.TargetApi;
44
import android.graphics.Bitmap;
5-
import android.graphics.Canvas;
65
import android.os.Build;
76
import android.os.Handler;
87
import android.os.HandlerThread;
@@ -25,6 +24,7 @@ public final class EngineView extends FrameLayout implements SurfaceHolder.Callb
2524
private final SurfaceView primarySurfaceView;
2625
private final SurfaceView xrSurfaceView;
2726
private final EventDispatcher reactEventDispatcher;
27+
private Runnable renderRunnable;
2828

2929
public EngineView(ReactContext reactContext) {
3030
super(reactContext);
@@ -57,14 +57,25 @@ public void surfaceDestroyed(SurfaceHolder holder) {
5757

5858
this.setOnTouchListener(this);
5959

60-
this.setWillNotDraw(false);
61-
6260
this.reactEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
6361
}
6462

6563
@Override
6664
public void surfaceCreated(SurfaceHolder surfaceHolder) {
67-
// surfaceChanged is also called when the surface is created, so just do all the handling there
65+
this.renderRunnable = new Runnable() {
66+
@Override
67+
public void run() {
68+
if (BabylonNativeInterop.isXRActive()) {
69+
EngineView.this.xrSurfaceView.setVisibility(View.VISIBLE);
70+
} else {
71+
EngineView.this.xrSurfaceView.setVisibility(View.INVISIBLE);
72+
}
73+
74+
BabylonNativeInterop.renderView();
75+
EngineView.this.postOnAnimation(this);
76+
}
77+
};
78+
this.postOnAnimation(this.renderRunnable);
6879
}
6980

7081
@Override
@@ -74,6 +85,8 @@ public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int width, int he
7485

7586
@Override
7687
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
88+
this.removeCallbacks(this.renderRunnable);
89+
this.renderRunnable = null;
7790
}
7891

7992
@Override
@@ -82,18 +95,6 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
8295
return true;
8396
}
8497

85-
@Override
86-
protected void onDraw(Canvas canvas) {
87-
if (BabylonNativeInterop.isXRActive()) {
88-
this.xrSurfaceView.setVisibility(View.VISIBLE);
89-
} else {
90-
this.xrSurfaceView.setVisibility(View.INVISIBLE);
91-
}
92-
93-
BabylonNativeInterop.renderView();
94-
invalidate();
95-
}
96-
9798
@TargetApi(24)
9899
public void takeSnapshot() {
99100
// Only supported on API level 24 and up, return a blank image.

0 commit comments

Comments
 (0)