Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
skiko
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liuqiang
skiko
Commits
e9fefc76
Commit
e9fefc76
authored
Feb 03, 2021
by
Igor Demin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FrameDispatcherTest
parent
58d59a86
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
194 additions
and
4 deletions
+194
-4
build.gradle.kts
samples/SkijaInjectSample/build.gradle.kts
+7
-0
FrameDispatcherTest.kt
...rc/test/kotlin/org/jetbrains/skiko/FrameDispatcherTest.kt
+165
-0
FrameDispatcher.kt
...src/jvmMain/kotlin/org/jetbrains/skiko/FrameDispatcher.kt
+22
-4
No files found.
samples/SkijaInjectSample/build.gradle.kts
View file @
e9fefc76
import
org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins
{
kotlin
(
"jvm"
)
version
"1.3.72"
application
...
...
@@ -36,6 +38,7 @@ dependencies {
implementation
(
platform
(
"org.jetbrains.kotlin:kotlin-bom"
))
implementation
(
"org.jetbrains.kotlin:kotlin-stdlib-jdk8"
)
implementation
(
"org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.4.1"
)
implementation
(
"org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.1"
)
implementation
(
"org.jetbrains.skiko:skiko-jvm-runtime-$target:$version"
)
testImplementation
(
"org.jetbrains.kotlin:kotlin-test"
)
testImplementation
(
"org.jetbrains.kotlin:kotlin-test-junit"
)
...
...
@@ -64,4 +67,8 @@ tasks.withType<Test> {
systemProperty
(
"sun.java2d.dpiaware"
,
"false"
)
systemProperty
(
"sun.java2d.uiScale"
,
"1"
)
}
}
tasks
.
withType
<
KotlinCompile
>().
configureEach
{
kotlinOptions
.
freeCompilerArgs
+=
"-Xopt-in=kotlin.RequiresOptI"
}
\ No newline at end of file
samples/SkijaInjectSample/src/test/kotlin/org/jetbrains/skiko/FrameDispatcherTest.kt
0 → 100644
View file @
e9fefc76
package
org.jetbrains.skiko
import
kotlinx.coroutines.*
import
kotlinx.coroutines.test.TestCoroutineScope
import
kotlinx.coroutines.test.runBlockingTest
import
org.junit.Assert.assertEquals
import
org.junit.Test
@OptIn
(
ExperimentalCoroutinesApi
::
class
)
class
FrameDispatcherTest
{
private
var
frameCount
=
0
@Test
fun
`
shouldn
'
t
call
onFrame
after
the
creating
`
()
=
test
{
FrameDispatcher
(
scope
=
this
)
{
frameCount
++
}
advanceUntilIdle
()
assertEquals
(
0
,
frameCount
)
}
@Test
fun
`
scheduleFrame
after
creating
`
()
=
test
{
val
frameDispatcher
=
FrameDispatcher
(
scope
=
this
)
{
frameCount
++
}
frameDispatcher
.
scheduleFrame
()
advanceUntilIdle
()
assertEquals
(
1
,
frameCount
)
}
@Test
fun
`
scheduleFrame
multiple
times
after
creating
`
()
=
test
{
val
frameDispatcher
=
FrameDispatcher
(
scope
=
this
)
{
frameCount
++
}
frameDispatcher
.
scheduleFrame
()
frameDispatcher
.
scheduleFrame
()
frameDispatcher
.
scheduleFrame
()
frameDispatcher
.
scheduleFrame
()
advanceUntilIdle
()
assertEquals
(
1
,
frameCount
)
}
@Test
fun
`
scheduleFrame
second
time
after
first
onFrame
`
()
=
test
{
val
frameDispatcher
=
FrameDispatcher
(
scope
=
this
)
{
frameCount
++
}
frameDispatcher
.
scheduleFrame
()
advanceUntilIdle
()
frameDispatcher
.
scheduleFrame
()
advanceUntilIdle
()
assertEquals
(
2
,
frameCount
)
}
@Test
fun
`
scheduleFrame
second
time
twice
after
first
onFrame
`
()
=
test
{
val
frameDispatcher
=
FrameDispatcher
(
scope
=
this
)
{
frameCount
++
}
frameDispatcher
.
scheduleFrame
()
advanceUntilIdle
()
frameDispatcher
.
scheduleFrame
()
frameDispatcher
.
scheduleFrame
()
advanceUntilIdle
()
assertEquals
(
2
,
frameCount
)
}
@Suppress
(
"JoinDeclarationAndAssignment"
)
@Test
fun
`
scheduleFrame
during
onFrame
`
()
=
test
{
lateinit
var
frameDispatcher
:
FrameDispatcher
frameDispatcher
=
FrameDispatcher
(
scope
=
this
)
{
frameCount
++
frameDispatcher
.
scheduleFrame
()
}
frameDispatcher
.
scheduleFrame
()
yield
()
assertEquals
(
1
,
frameCount
)
yield
()
assertEquals
(
2
,
frameCount
)
yield
()
assertEquals
(
3
,
frameCount
)
yield
()
assertEquals
(
4
,
frameCount
)
}
@Suppress
(
"JoinDeclarationAndAssignment"
)
@Test
fun
`
scheduleFrame
multiple
times
during
onFrame
`
()
=
test
{
lateinit
var
frameDispatcher
:
FrameDispatcher
frameDispatcher
=
FrameDispatcher
(
scope
=
this
)
{
frameCount
++
frameDispatcher
.
scheduleFrame
()
frameDispatcher
.
scheduleFrame
()
frameDispatcher
.
scheduleFrame
()
}
frameDispatcher
.
scheduleFrame
()
yield
()
assertEquals
(
1
,
frameCount
)
yield
()
assertEquals
(
2
,
frameCount
)
yield
()
assertEquals
(
3
,
frameCount
)
yield
()
assertEquals
(
4
,
frameCount
)
}
@Suppress
(
"JoinDeclarationAndAssignment"
)
@Test
fun
`
cancel
coroutine
scope
`
()
=
test
{
val
scope
=
CoroutineScope
(
coroutineContext
)
lateinit
var
frameDispatcher
:
FrameDispatcher
frameDispatcher
=
FrameDispatcher
(
scope
)
{
frameCount
++
frameDispatcher
.
scheduleFrame
()
}
frameDispatcher
.
scheduleFrame
()
yield
()
assertEquals
(
1
,
frameCount
)
yield
()
assertEquals
(
2
,
frameCount
)
yield
()
assertEquals
(
3
,
frameCount
)
scope
.
cancel
()
yield
()
assertEquals
(
3
,
frameCount
)
advanceUntilIdle
()
assertEquals
(
3
,
frameCount
)
}
private
fun
test
(
block
:
suspend
TestCoroutineScope
.()
->
Unit
)
=
runBlockingTest
{
pauseDispatcher
()
val
job
=
Job
()
TestCoroutineScope
(
coroutineContext
+
job
).
block
()
job
.
cancel
()
}
}
\ No newline at end of file
skiko/src/jvmMain/kotlin/org/jetbrains/skiko/FrameDispatcher.kt
View file @
e9fefc76
package
org.jetbrains.skiko
import
kotlinx.coroutines.CompletableDeferred
import
kotlinx.coroutines.
Global
Scope
import
kotlinx.coroutines.
Coroutine
Scope
import
kotlinx.coroutines.launch
import
kotlinx.coroutines.yield
import
kotlin.coroutines.CoroutineContext
/**
* Dispatch frame after call of [scheduleFrame]
* Dispatch frame after call of [scheduleFrame].
*
* After the creating there should be no scheduled frame in the frame loop.
*/
class
FrameDispatcher
(
context
:
CoroutineContext
,
scope
:
CoroutineScope
,
private
val
onFrame
:
suspend
()
->
Unit
)
{
constructor
(
context
:
CoroutineContext
,
onFrame
:
suspend
()
->
Unit
)
:
this
(
CoroutineScope
(
context
),
onFrame
)
private
var
needFrame
=
CompletableDeferred
<
Unit
>()
private
val
job
=
GlobalScope
.
launch
(
context
)
{
private
val
job
=
scope
.
launch
{
while
(
true
)
{
needFrame
.
await
()
needFrame
=
CompletableDeferred
()
...
...
@@ -28,6 +38,14 @@ class FrameDispatcher(
job
.
cancel
()
}
/**
* Schedule next frame to render in the frame loop.
*
* Multiple calls of scheduleFrame before beginning of the frame will cause only one onFrame.
*
* Multiple calls of scheduleFrame after beginning of the frame but before its ending
* will schedule next single onFrame after the current one.
*/
fun
scheduleFrame
()
{
needFrame
.
complete
(
Unit
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment