1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.xr.compose.platform
18 
19 import androidx.activity.ComponentActivity
20 import androidx.compose.runtime.Composable
21 import androidx.compose.runtime.CompositionContext
22 import androidx.lifecycle.DefaultLifecycleObserver
23 import androidx.lifecycle.Lifecycle
24 import androidx.lifecycle.LifecycleOwner
25 import androidx.xr.compose.subspace.SubspaceComposable
26 import androidx.xr.compose.subspace.layout.CoreEntity
27 import androidx.xr.compose.unit.VolumeConstraints
28 import androidx.xr.runtime.Session
29 
30 /**
31  * A 3D scene represented via Compose elements and coordinated with SceneCore.
32  *
33  * This class manages the lifecycle and root element of the spatial scene. It also provides access
34  * to the SceneCore session and environment.
35  *
36  * @param parentCompositionContext the optional composition context when this is a sub-composition
37  * @param rootEntity the optional [CoreEntity] to associate with the root of this composition
38  * @property ownerActivity the [ComponentActivity] that owns this scene.
39  * @property jxrSession the [Session] used to interact with SceneCore.
40  */
41 internal class SpatialComposeScene(
42     /** Context of the activity that this scene is rooted on. */
43     public val ownerActivity: ComponentActivity,
44     @InternalSubspaceApi public val jxrSession: Session,
45     parentCompositionContext: CompositionContext? = null,
46     rootEntity: CoreEntity? = null,
47     rootVolumeConstraints: VolumeConstraints = VolumeConstraints.Unbounded,
48 ) : DefaultLifecycleObserver, LifecycleOwner {
<lambda>null49     init {
50         SceneManager.onSceneCreated(this)
51     }
52 
53     /** Root of the spatial scene graph of this [SpatialComposeScene]. */
54     internal val rootElement: SpatialComposeElement =
55         SpatialComposeElement(this, parentCompositionContext, rootEntity, rootVolumeConstraints)
56 
57     public fun setContent(content: @Composable @SubspaceComposable () -> Unit) {
58         rootElement.setContent(content)
59     }
60 
disposenull61     public fun dispose() {
62         rootElement.disposeComposition()
63         SceneManager.onSceneDisposed(this)
64     }
65 
66     override val lifecycle: Lifecycle
67         get() = ownerActivity.lifecycle
68 }
69