12
12
import android .view .SurfaceHolder ;
13
13
import android .view .SurfaceView ;
14
14
import android .view .View ;
15
+ import android .widget .FrameLayout ;
15
16
16
17
import com .facebook .react .bridge .ReactContext ;
17
18
import com .facebook .react .uimanager .UIManagerModule ;
18
19
import com .facebook .react .uimanager .events .EventDispatcher ;
19
20
20
21
import java .io .ByteArrayOutputStream ;
21
22
22
- public final class EngineView extends SurfaceView implements SurfaceHolder .Callback , View .OnTouchListener {
23
+ public final class EngineView extends FrameLayout implements SurfaceHolder .Callback , View .OnTouchListener {
24
+ private static final FrameLayout .LayoutParams childViewLayoutParams = new FrameLayout .LayoutParams (LayoutParams .MATCH_PARENT , LayoutParams .MATCH_PARENT );
25
+ private final SurfaceView primarySurfaceView ;
26
+ private final SurfaceView xrSurfaceView ;
23
27
private final EventDispatcher reactEventDispatcher ;
24
28
25
29
public EngineView (ReactContext reactContext ) {
26
30
super (reactContext );
27
- this .getHolder ().addCallback (this );
31
+
32
+ this .primarySurfaceView = new SurfaceView (reactContext );
33
+ this .primarySurfaceView .setLayoutParams (EngineView .childViewLayoutParams );
34
+ this .primarySurfaceView .getHolder ().addCallback (this );
35
+ this .addView (this .primarySurfaceView );
36
+
37
+ this .xrSurfaceView = new SurfaceView (reactContext );
38
+ this .xrSurfaceView .setLayoutParams (childViewLayoutParams );
39
+ this .xrSurfaceView .getHolder ().addCallback (new SurfaceHolder .Callback () {
40
+ @ Override
41
+ public void surfaceCreated (SurfaceHolder holder ) {
42
+ // surfaceChanged is also called when the surface is created, so just do all the handling there
43
+ }
44
+
45
+ @ Override
46
+ public void surfaceChanged (SurfaceHolder holder , int format , int width , int height ) {
47
+ BabylonNativeInterop .updateXRView (holder .getSurface ());
48
+ }
49
+
50
+ @ Override
51
+ public void surfaceDestroyed (SurfaceHolder holder ) {
52
+ BabylonNativeInterop .updateXRView (null );
53
+ }
54
+ });
55
+ this .xrSurfaceView .setVisibility (View .INVISIBLE );
56
+ this .addView (this .xrSurfaceView );
57
+
28
58
this .setOnTouchListener (this );
29
- this .reactEventDispatcher = reactContext .getNativeModule (UIManagerModule .class ).getEventDispatcher ();
30
59
31
- setWillNotDraw (false );
60
+ this .setWillNotDraw (false );
61
+
62
+ this .reactEventDispatcher = reactContext .getNativeModule (UIManagerModule .class ).getEventDispatcher ();
32
63
}
33
64
34
65
@ Override
@@ -51,6 +82,18 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
51
82
return true ;
52
83
}
53
84
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
+
54
97
@ TargetApi (24 )
55
98
public void takeSnapshot () {
56
99
// Only supported on API level 24 and up, return a blank image.
@@ -70,8 +113,13 @@ public void takeSnapshot() {
70
113
helperThread .start ();
71
114
final Handler helperThreadHandler = new Handler (helperThread .getLooper ());
72
115
116
+ SurfaceView surfaceView = this .primarySurfaceView ;
117
+ if (BabylonNativeInterop .isXRActive ()) {
118
+ surfaceView = this .xrSurfaceView ;
119
+ }
120
+
73
121
// Request the pixel copy.
74
- PixelCopy .request (this , bitmap , (copyResult ) -> {
122
+ PixelCopy .request (surfaceView , bitmap , (copyResult ) -> {
75
123
// If the pixel copy was a success then convert the image to a base 64 encoded jpeg and fire the event.
76
124
String encoded = "" ;
77
125
if (copyResult == PixelCopy .SUCCESS ) {
@@ -87,10 +135,4 @@ public void takeSnapshot() {
87
135
helperThread .quitSafely ();
88
136
}, helperThreadHandler );
89
137
}
90
-
91
- @ Override
92
- protected void onDraw (Canvas canvas ) {
93
- BabylonNativeInterop .renderView ();
94
- invalidate ();
95
- }
96
138
}
0 commit comments