Skip to content

Commit d5d63bb

Browse files
committed
chore: test out big list of gl surfaces
1 parent 2bbec60 commit d5d63bb

File tree

1 file changed

+109
-89
lines changed

1 file changed

+109
-89
lines changed

src/test/kotlin/Main.kt

Lines changed: 109 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import androidx.compose.animation.core.tween
44
import androidx.compose.desktop.ui.tooling.preview.Preview
55
import androidx.compose.foundation.background
66
import androidx.compose.foundation.layout.*
7+
import androidx.compose.foundation.pager.VerticalPager
8+
import androidx.compose.foundation.pager.rememberPagerState
79
import androidx.compose.material.*
810
import androidx.compose.runtime.*
911
import androidx.compose.ui.Alignment
@@ -16,117 +18,135 @@ import androidx.compose.ui.window.Window
1618
import androidx.compose.ui.window.awaitApplication
1719
import dev.silenium.compose.gl.surface.*
1820
import kotlinx.coroutines.delay
21+
import kotlinx.coroutines.isActive
1922
import me.saket.telephoto.zoomable.ZoomSpec
2023
import me.saket.telephoto.zoomable.rememberZoomableState
2124
import me.saket.telephoto.zoomable.zoomable
2225
import org.jetbrains.skia.Paint
2326
import org.lwjgl.opengl.GL30.*
27+
import kotlin.random.Random
28+
import kotlin.random.nextInt
2429
import kotlin.time.Duration.Companion.milliseconds
2530
import kotlin.time.Duration.Companion.seconds
2631

2732
@Composable
2833
@Preview
2934
fun ApplicationScope.App() {
3035
MaterialTheme(lightColors()) {
31-
Box(contentAlignment = Alignment.TopStart, modifier = Modifier.fillMaxSize().background(Color.White)) {
32-
val state = rememberGLSurfaceState()
33-
var targetHue by remember { mutableStateOf(0f) }
34-
val color by animateColorAsState(
35-
Color.hsl(targetHue, 1f, 0.5f, 0.1f),
36-
animationSpec = tween(durationMillis = 200, easing = LinearEasing)
37-
)
38-
LaunchedEffect(Unit) {
39-
while (true) {
40-
targetHue = (targetHue + 10f) % 360f
41-
delay(200)
42-
}
43-
}
44-
var visible by remember { mutableStateOf(true) }
45-
LaunchedEffect(Unit) {
46-
while (true) {
47-
delay(2.seconds)
48-
visible = !visible
49-
}
36+
val state = rememberPagerState { 1000 }
37+
LaunchedEffect(Unit) {
38+
// while (isActive) {
39+
// delay(100.milliseconds)
40+
// state.animateScrollToPage(Random.nextInt(0..999))
41+
// }
42+
}
43+
VerticalPager(state, modifier = Modifier.fillMaxSize(), beyondViewportPageCount = 1) {
44+
Content()
45+
}
46+
}
47+
}
48+
49+
@Composable
50+
fun ApplicationScope.Content() {
51+
Box(contentAlignment = Alignment.TopStart, modifier = Modifier.fillMaxSize().background(Color.White)) {
52+
val state = rememberGLSurfaceState()
53+
var targetHue by remember { mutableStateOf(0f) }
54+
val color by animateColorAsState(
55+
Color.hsl(targetHue, 1f, 0.5f, 0.1f),
56+
animationSpec = tween(durationMillis = 200, easing = LinearEasing)
57+
)
58+
LaunchedEffect(Unit) {
59+
while (true) {
60+
targetHue = (targetHue + 10f) % 360f
61+
delay(200)
5062
}
51-
LaunchedEffect(Unit) {
52-
while (true) {
53-
delay(300.milliseconds)
54-
if (visible) {
55-
state.requestUpdate()
56-
}
57-
}
63+
}
64+
var visible by remember { mutableStateOf(true) }
65+
LaunchedEffect(Unit) {
66+
while (true) {
67+
delay(2.seconds)
68+
visible = !visible
5869
}
59-
val surfaceView = rememberGLSurface(
60-
state = state,
61-
presentMode = GLSurface.PresentMode.MAILBOX,
62-
fboSizeOverride = FBOSizeOverride(4096, 4096, TransformOrigin.Center),
63-
) {
64-
glClearColor(color.red, color.green, color.blue, color.alpha)
65-
glClear(GL_COLOR_BUFFER_BIT)
66-
try {
67-
Thread.sleep(33, 333)
68-
} catch (e: InterruptedException) {
69-
terminate()
70-
return@rememberGLSurface
70+
}
71+
LaunchedEffect(Unit) {
72+
while (true) {
73+
delay(300.milliseconds)
74+
if (visible) {
75+
state.requestUpdate()
7176
}
72-
glBegin(GL_QUADS)
73-
glColor3f(1f, 0f, 0f)
74-
glVertex2f(-1f, 0f)
75-
glColor3f(0f, 1f, 0f)
76-
glVertex2f(0f, -1f)
77-
glColor3f(0f, 0f, 1f)
78-
glVertex2f(1f, 0f)
79-
glColor3f(1f, 1f, 1f)
80-
glVertex2f(0f, 1f)
81-
glEnd()
82-
83-
val wait = (1000.0 / 60).milliseconds
84-
redrawAfter(null)
8577
}
86-
val modifier = Modifier
87-
.aspectRatio(1f)
88-
.zoomable(rememberZoomableState(ZoomSpec(6f)))
89-
.align(Alignment.Center)
90-
if (visible) {
91-
GLSurfaceView(
92-
surfaceView,
93-
modifier = modifier,
94-
paint = Paint().apply {
95-
alpha = 128
96-
}
97-
)
98-
} else {
99-
Box(
100-
modifier = modifier,
101-
contentAlignment = Alignment.Center,
102-
) {
103-
Text("Surface is not visible", style = MaterialTheme.typography.h6, color = MaterialTheme.colors.onBackground, modifier = Modifier.align(Alignment.Center))
104-
}
78+
}
79+
val surfaceView = rememberGLSurface(
80+
state = state,
81+
presentMode = GLSurface.PresentMode.MAILBOX,
82+
fboSizeOverride = FBOSizeOverride(4096, 4096, TransformOrigin.Center),
83+
swapChainSize = 2,
84+
) {
85+
glClearColor(color.red, color.green, color.blue, color.alpha)
86+
glClear(GL_COLOR_BUFFER_BIT)
87+
try {
88+
Thread.sleep(33, 333)
89+
} catch (e: InterruptedException) {
90+
terminate()
91+
return@rememberGLSurface
10592
}
106-
Surface(modifier = Modifier.align(Alignment.TopStart).padding(4.dp)) {
107-
Column(modifier = Modifier.padding(4.dp).width(400.dp)) {
108-
val display by state.displayStatistics.collectAsState()
109-
Text("Display datapoints: ${display.frameTimes.values.size}")
110-
Text("Display frame time: ${display.frameTimes.median.inWholeMicroseconds / 1000.0} ms")
111-
Text("Display frame time (99th): ${display.frameTimes.percentile(0.99).inWholeMicroseconds / 1000.0} ms")
112-
Text("Display FPS: ${display.fps.median}")
113-
Text("Display FPS (99th): ${display.fps.percentile(0.99, Stats.Percentile.LOWEST)}")
93+
glBegin(GL_QUADS)
94+
glColor3f(1f, 0f, 0f)
95+
glVertex2f(-1f, 0f)
96+
glColor3f(0f, 1f, 0f)
97+
glVertex2f(0f, -1f)
98+
glColor3f(0f, 0f, 1f)
99+
glVertex2f(1f, 0f)
100+
glColor3f(1f, 1f, 1f)
101+
glVertex2f(0f, 1f)
102+
glEnd()
114103

115-
val render by state.renderStatistics.collectAsState()
116-
Text("Render datapoints: ${render.frameTimes.values.size}")
117-
Text("Render frame time: ${render.frameTimes.median.inWholeMicroseconds / 1000.0} ms")
118-
Text("Render frame time (99th): ${render.frameTimes.percentile(0.99).inWholeMicroseconds / 1000.0} ms")
119-
Text("Render FPS: ${render.fps.median} ms")
120-
Text("Render FPS (99th): ${render.fps.percentile(0.99, Stats.Percentile.LOWEST)}")
104+
val wait = (1000.0 / 60).milliseconds
105+
redrawAfter(null)
106+
}
107+
val modifier = Modifier
108+
.aspectRatio(1f)
109+
.zoomable(rememberZoomableState(ZoomSpec(6f)))
110+
.align(Alignment.Center)
111+
if (visible) {
112+
GLSurfaceView(
113+
surfaceView,
114+
modifier = modifier,
115+
paint = Paint().apply {
116+
alpha = 128
121117
}
122-
}
123-
Button(
124-
onClick = ::exitApplication,
125-
modifier = Modifier.align(Alignment.BottomStart).padding(8.dp),
118+
)
119+
} else {
120+
Box(
121+
modifier = modifier,
122+
contentAlignment = Alignment.Center,
126123
) {
127-
Text("Exit application")
124+
Text("Surface is not visible", style = MaterialTheme.typography.h6, color = MaterialTheme.colors.onBackground, modifier = Modifier.align(Alignment.Center))
125+
}
126+
}
127+
Surface(modifier = Modifier.align(Alignment.TopStart).padding(4.dp)) {
128+
Column(modifier = Modifier.padding(4.dp).width(400.dp)) {
129+
val display by state.displayStatistics.collectAsState()
130+
Text("Display datapoints: ${display.frameTimes.values.size}")
131+
Text("Display frame time: ${display.frameTimes.median.inWholeMicroseconds / 1000.0} ms")
132+
Text("Display frame time (99th): ${display.frameTimes.percentile(0.99).inWholeMicroseconds / 1000.0} ms")
133+
Text("Display FPS: ${display.fps.median}")
134+
Text("Display FPS (99th): ${display.fps.percentile(0.99, Stats.Percentile.LOWEST)}")
135+
136+
val render by state.renderStatistics.collectAsState()
137+
Text("Render datapoints: ${render.frameTimes.values.size}")
138+
Text("Render frame time: ${render.frameTimes.median.inWholeMicroseconds / 1000.0} ms")
139+
Text("Render frame time (99th): ${render.frameTimes.percentile(0.99).inWholeMicroseconds / 1000.0} ms")
140+
Text("Render FPS: ${render.fps.median} ms")
141+
Text("Render FPS (99th): ${render.fps.percentile(0.99, Stats.Percentile.LOWEST)}")
128142
}
129143
}
144+
Button(
145+
onClick = ::exitApplication,
146+
modifier = Modifier.align(Alignment.BottomStart).padding(8.dp),
147+
) {
148+
Text("Exit application")
149+
}
130150
}
131151
}
132152

0 commit comments

Comments
 (0)