Unverified Commit d0a425f4 authored by Nikolay Igotti's avatar Nikolay Igotti Committed by GitHub

Commonize more of Skiko (#234)

parent af035822
...@@ -2,6 +2,8 @@ plugins { ...@@ -2,6 +2,8 @@ plugins {
kotlin("multiplatform") version "1.5.31" kotlin("multiplatform") version "1.5.31"
} }
val coroutinesVersion = "1.5.2"
repositories { repositories {
mavenLocal() mavenLocal()
jcenter() jcenter()
...@@ -32,11 +34,9 @@ if (project.hasProperty("skiko.version")) { ...@@ -32,11 +34,9 @@ if (project.hasProperty("skiko.version")) {
} }
kotlin { kotlin {
val nativeTarget = when (target) {
val nativeTarget = when { "macos-x64" -> macosX64()
osName == "Mac OS X" -> macosX64() "macos-arm64" -> macosArm64()
osName.startsWith("Win") -> mingwX64()
osName.startsWith("Linux") -> linuxX64()
else -> throw GradleException("Host OS is not supported in Kotlin/Native.") else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
} }
...@@ -53,20 +53,28 @@ kotlin { ...@@ -53,20 +53,28 @@ kotlin {
val commonMain by getting { val commonMain by getting {
dependencies { dependencies {
implementation("org.jetbrains.skiko:skiko:$version") implementation("org.jetbrains.skiko:skiko:$version")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
} }
} }
val nativeMain by creating { val nativeMain by creating {
dependsOn(commonMain) dependsOn(commonMain)
dependencies {
}
} }
val macosX64Main by getting { val archTargetMain = when (target) {
dependsOn(nativeMain) "macos-x64" -> {
dependencies { val macosX64Main by getting {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-macosx64:1.5.2") dependsOn(nativeMain)
}
macosX64Main
}
"macos-arm64" -> {
val macosArm64Main by getting {
dependsOn(nativeMain)
}
macosArm64Main
} }
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
} }
} }
} }
...@@ -207,26 +207,28 @@ kotlin { ...@@ -207,26 +207,28 @@ kotlin {
} }
if (supportNative) { if (supportNative) {
val targetString = target
val skiaDir = skiaDir.get().absolutePath val skiaDir = skiaDir.get().absolutePath
val nativeTarget = when (targetString) { val nativeTarget = when (target) {
"macos-x64", "macos-arm64" -> macosX64 { "macos-x64" -> macosX64 {}
compilations.all { "macos-arm64" -> macosArm64 {}
kotlinOptions { else -> null
freeCompilerArgs += listOf( }
"-include-binary", val targetString = target
"$skiaDir/$skiaBinSubdir/libskia.a", nativeTarget?.let {
"-include-binary", it.compilations.all {
"$skiaDir/$skiaBinSubdir/libskshaper.a", kotlinOptions {
"-include-binary", freeCompilerArgs += listOf(
"$skiaDir/$skiaBinSubdir/libskparagraph.a", "-include-binary",
"-include-binary", "$skiaDir/$skiaBinSubdir/libskia.a",
"$buildDir/nativeBridges/static/$targetString/skiko-native-bridges-$targetString.a" "-include-binary",
) "$skiaDir/$skiaBinSubdir/libskshaper.a",
} "-include-binary",
"$skiaDir/$skiaBinSubdir/libskparagraph.a",
"-include-binary",
"$buildDir/nativeBridges/static/$targetString/skiko-native-bridges-$targetString.a"
)
} }
} }
else -> null
} }
} }
...@@ -267,14 +269,30 @@ kotlin { ...@@ -267,14 +269,30 @@ kotlin {
if (supportNative) { if (supportNative) {
// See https://kotlinlang.org/docs/mpp-share-on-platforms.html#configure-the-hierarchical-structure-manually // See https://kotlinlang.org/docs/mpp-share-on-platforms.html#configure-the-hierarchical-structure-manually
val nativeMain by creating {} val nativeMain by creating {
val macosMain by creating { dependsOn(commonMain)
dependsOn(nativeMain)
}
val macosX64Main by getting {
dependsOn(macosMain)
dependencies { dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
}
}
if (targetOs == OS.MacOS) {
val macosMain by creating {
dependsOn(nativeMain)
}
val macosArchMain = when (targetArch) {
Arch.X64 -> {
val macosX64Main by getting {
dependsOn(macosMain)
}
macosX64Main
}
Arch.Arm64 -> {
val macosArm64Main by getting {
dependsOn(macosMain)
}
macosArm64Main
}
else -> throw GradleException("Unsupported arch $targetArch for macOS")
} }
} }
} }
......
package org.jetbrains.skiko
enum class GraphicsApi {
UNKNOWN, SOFTWARE, DIRECT_SOFTWARE, OPENGL, DIRECT3D, VULKAN, METAL, WEBGL
}
enum class GpuPriority(val value: String) {
Auto("auto"), Integrated("integrated"), Discrete("discrete");
companion object {
fun parse(value: String?): GpuPriority? = GpuPriority.values().find { it.value == value }
}
}
package org.jetbrains.skiko
enum class OS(val id: String) {
Linux("linux"),
Windows("windows"),
MacOS("macos"),
JS("js")
;
val isLinux
get() = this == Linux
val isWindows
get() = this == Windows
val isMacOS
get() = this == MacOS
}
enum class Arch(val id: String) {
X64("x64"),
Arm64("arm64"),
JS("js"),
WASM("wasm")
}
enum class KotlinBackend(val id: String) {
JVM("jvm"),
JS("js"),
Native("native")
}
expect val hostOs: OS
expect val hostArch: Arch
expect val hostId: String
expect val kotlinBackend: KotlinBackend
\ No newline at end of file
package org.jetbrains.skiko
actual val hostOs: OS = OS.JS
actual val hostArch: Arch = Arch.JS
actual val hostId by lazy {
"${hostOs.id}-${hostArch.id}"
}
actual val kotlinBackend: KotlinBackend
get() = KotlinBackend.JS
\ No newline at end of file
package org.jetbrains.skiko package org.jetbrains.skiko
import org.jetbrains.skiko.redrawer.Redrawer
import org.jetbrains.skiko.redrawer.WindowsOpenGLRedrawer
import org.jetbrains.skiko.redrawer.LinuxOpenGLRedrawer
enum class GraphicsApi {
UNKNOWN, SOFTWARE, DIRECT_SOFTWARE, OPENGL, DIRECT3D, VULKAN, METAL
}
enum class GpuPriority(val value: String) {
Auto("auto"), Integrated("integrated"), Discrete("discrete");
companion object {
fun parse(value: String?): GpuPriority? = GpuPriority.values().find { it.value == value }
}
}
private val notSupportedAdapters by lazy { private val notSupportedAdapters by lazy {
val resource = SkiaLayer::class.java.getResource("/not-supported-adapter.list").readText() val resource = SkiaLayer::class.java.getResource("/not-supported-adapter.list").readText()
resource.split(";").map { it.trim() } resource.split(";").map { it.trim() }
......
package org.jetbrains.skiko package org.jetbrains.skiko
enum class OS(val id: String) { actual val hostOs: OS by lazy {
Linux("linux"),
Windows("windows"),
MacOS("macos")
;
val isLinux
get() = this == Linux
val isWindows
get() = this == Windows
val isMacOS
get() = this == MacOS
}
enum class Arch(val id: String) {
X64("x64"),
Arm64("arm64")
}
val hostOs by lazy {
val osName = System.getProperty("os.name") val osName = System.getProperty("os.name")
when { when {
osName == "Mac OS X" -> OS.MacOS osName == "Mac OS X" -> OS.MacOS
...@@ -31,7 +10,7 @@ val hostOs by lazy { ...@@ -31,7 +10,7 @@ val hostOs by lazy {
} }
} }
val hostArch by lazy { actual val hostArch: Arch by lazy {
val osArch = System.getProperty("os.arch") val osArch = System.getProperty("os.arch")
when (osArch) { when (osArch) {
"x86_64", "amd64" -> Arch.X64 "x86_64", "amd64" -> Arch.X64
...@@ -40,7 +19,7 @@ val hostArch by lazy { ...@@ -40,7 +19,7 @@ val hostArch by lazy {
} }
} }
val hostId by lazy { actual val hostId by lazy {
"${hostOs.id}-${hostArch.id}" "${hostOs.id}-${hostArch.id}"
} }
...@@ -55,3 +34,6 @@ internal val javaVendor by lazy { ...@@ -55,3 +34,6 @@ internal val javaVendor by lazy {
internal val javaLocation by lazy { internal val javaLocation by lazy {
"${System.getProperty("java.home")}" "${System.getProperty("java.home")}"
} }
actual val kotlinBackend: KotlinBackend
get() = KotlinBackend.JVM
\ No newline at end of file
...@@ -85,6 +85,9 @@ internal val platformOperations: PlatformOperations by lazy { ...@@ -85,6 +85,9 @@ internal val platformOperations: PlatformOperations by lazy {
} }
} }
} }
OS.JS -> {
TODO("Commonize me")
}
} }
} }
......
...@@ -43,6 +43,9 @@ internal interface RenderFactory { ...@@ -43,6 +43,9 @@ internal interface RenderFactory {
GraphicsApi.DIRECT_SOFTWARE -> LinuxSoftwareRedrawer(layer, properties) GraphicsApi.DIRECT_SOFTWARE -> LinuxSoftwareRedrawer(layer, properties)
else -> LinuxOpenGLRedrawer(layer, properties) else -> LinuxOpenGLRedrawer(layer, properties)
} }
OS.JS -> {
TODO("Commonize me")
}
} }
} }
} }
......
...@@ -56,6 +56,7 @@ internal object SkikoProperties { ...@@ -56,6 +56,7 @@ internal object SkikoProperties {
OS.MacOS -> return GraphicsApi.METAL OS.MacOS -> return GraphicsApi.METAL
OS.Linux -> return GraphicsApi.OPENGL OS.Linux -> return GraphicsApi.OPENGL
OS.Windows -> return GraphicsApi.DIRECT3D OS.Windows -> return GraphicsApi.DIRECT3D
OS.JS -> TODO("commonize me")
} }
} }
...@@ -67,6 +68,7 @@ internal object SkikoProperties { ...@@ -67,6 +68,7 @@ internal object SkikoProperties {
OS.Linux -> renderApiList = mutableListOf(GraphicsApi.OPENGL, GraphicsApi.DIRECT_SOFTWARE, GraphicsApi.SOFTWARE) OS.Linux -> renderApiList = mutableListOf(GraphicsApi.OPENGL, GraphicsApi.DIRECT_SOFTWARE, GraphicsApi.SOFTWARE)
OS.MacOS -> renderApiList = mutableListOf(GraphicsApi.METAL, GraphicsApi.SOFTWARE) OS.MacOS -> renderApiList = mutableListOf(GraphicsApi.METAL, GraphicsApi.SOFTWARE)
OS.Windows -> renderApiList = mutableListOf(GraphicsApi.DIRECT3D, GraphicsApi.OPENGL, GraphicsApi.DIRECT_SOFTWARE, GraphicsApi.SOFTWARE) OS.Windows -> renderApiList = mutableListOf(GraphicsApi.DIRECT3D, GraphicsApi.OPENGL, GraphicsApi.DIRECT_SOFTWARE, GraphicsApi.SOFTWARE)
OS.JS -> TODO("commonize me")
} }
renderApiList.remove(head) renderApiList.remove(head)
......
...@@ -5,7 +5,6 @@ import kotlinx.coroutines.* ...@@ -5,7 +5,6 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.swing.Swing import kotlinx.coroutines.swing.Swing
import org.jetbrains.skiko.FrameDispatcher import org.jetbrains.skiko.FrameDispatcher
import org.jetbrains.skiko.FrameLimiter import org.jetbrains.skiko.FrameLimiter
import org.jetbrains.skiko.hostOs
import org.jetbrains.skiko.SkiaLayer import org.jetbrains.skiko.SkiaLayer
import org.jetbrains.skiko.SkiaLayerProperties import org.jetbrains.skiko.SkiaLayerProperties
......
package org.jetbrains.skiko.native
import org.jetbrains.skiko.GraphicsApi
import org.jetbrains.skiko.native.redrawer.MacOsOpenGLRedrawer
internal actual fun makePlatformOperations(): PlatformOperations {
return object: PlatformOperations {
override fun createRedrawer(layer: HardwareLayer, properties: SkiaLayerProperties) =
when (SkikoProperties.renderApi) {
GraphicsApi.OPENGL -> MacOsOpenGLRedrawer(layer, properties)
else -> error("No other renderings for native yet")
}
}
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ package org.jetbrains.skiko.native.redrawer ...@@ -3,6 +3,7 @@ package org.jetbrains.skiko.native.redrawer
import kotlinx.cinterop.CPointer import kotlinx.cinterop.CPointer
import kotlinx.cinterop.useContents import kotlinx.cinterop.useContents
import org.jetbrains.skiko.native.* import org.jetbrains.skiko.native.*
import org.jetbrains.skiko.redrawer.Redrawer
import platform.CoreFoundation.CFTimeInterval import platform.CoreFoundation.CFTimeInterval
import platform.CoreGraphics.CGRectMake import platform.CoreGraphics.CGRectMake
import platform.CoreVideo.CVTimeStamp import platform.CoreVideo.CVTimeStamp
......
// TODO: Commonize! This is a complete copy of jvm counterpart.
package org.jetbrains.skiko.native
enum class GraphicsApi {
UNKNOWN, SOFTWARE, OPENGL, DIRECT3D, VULKAN, METAL
}
\ No newline at end of file
package org.jetbrains.skiko
actual val hostOs: OS by lazy {
when (Platform.osFamily) {
OsFamily.MACOSX -> OS.MacOS
OsFamily.LINUX -> OS.Linux
OsFamily.WINDOWS -> OS.Windows
else -> throw Error("Unsupported OS ${Platform.osFamily}")
}
}
actual val hostArch: Arch by lazy {
when (Platform.cpuArchitecture) {
CpuArchitecture.X64 -> Arch.X64
CpuArchitecture.ARM64 -> Arch.Arm64
else -> throw Error("Unsupported arch ${Platform.cpuArchitecture}")
}
}
actual val hostId by lazy {
"${hostOs.id}-${hostArch.id}"
}
actual val kotlinBackend: KotlinBackend
get() = KotlinBackend.Native
\ No newline at end of file
package org.jetbrains.skiko.native package org.jetbrains.skiko.native
import org.jetbrains.skiko.native.* import org.jetbrains.skiko.redrawer.Redrawer
import org.jetbrains.skiko.native.SkikoProperties.renderApi
import org.jetbrains.skiko.native.redrawer.*
internal interface PlatformOperations { internal interface PlatformOperations {
fun createRedrawer(layer: HardwareLayer, properties: SkiaLayerProperties): Redrawer fun createRedrawer(layer: HardwareLayer, properties: SkiaLayerProperties): Redrawer
} }
// TODO: commonize more.
internal val platformOperations: PlatformOperations by lazy { internal val platformOperations: PlatformOperations by lazy {
object: PlatformOperations { makePlatformOperations()
override fun createRedrawer(layer: HardwareLayer, properties: SkiaLayerProperties) = when(renderApi) {
GraphicsApi.OPENGL -> MacOsOpenGLRedrawer(layer, properties)
else -> error("No software rendering for native yet")
}
}
} }
internal expect fun makePlatformOperations(): PlatformOperations
package org.jetbrains.skiko.native package org.jetbrains.skiko.native
import kotlinx.cinterop.pointed
import kotlinx.cinterop.useContents import kotlinx.cinterop.useContents
import org.jetbrains.skiko.native.context.* import org.jetbrains.skiko.native.context.*
import org.jetbrains.skiko.native.redrawer.*
import org.jetbrains.skia.* import org.jetbrains.skia.*
import org.jetbrains.skiko.redrawer.Redrawer
interface SkiaRenderer { interface SkiaRenderer {
fun onRender(canvas: Canvas, width: Int, height: Int, nanoTime: Long) fun onRender(canvas: Canvas, width: Int, height: Int, nanoTime: Long)
...@@ -54,10 +53,10 @@ open class SkiaLayer( ...@@ -54,10 +53,10 @@ open class SkiaLayer(
val pictureHeight = (height * contentScale).coerceAtLeast(0.0) val pictureHeight = (height * contentScale).coerceAtLeast(0.0)
val bounds = Rect.makeWH(pictureWidth.toFloat(), pictureHeight.toFloat()) val bounds = Rect.makeWH(pictureWidth.toFloat(), pictureHeight.toFloat())
val canvas = pictureRecorder.beginRecording(bounds)!! val canvas = pictureRecorder.beginRecording(bounds)
renderer?.onRender(canvas, pictureWidth.toInt(), pictureHeight.toInt(), nanoTime) renderer?.onRender(canvas, pictureWidth.toInt(), pictureHeight.toInt(), nanoTime)
val picture = pictureRecorder.finishRecordingAsPicture()!! val picture = pictureRecorder.finishRecordingAsPicture()
this.picture = PictureHolder(picture, pictureWidth.toInt(), pictureHeight.toInt()) this.picture = PictureHolder(picture, pictureWidth.toInt(), pictureHeight.toInt())
} }
......
package org.jetbrains.skiko.native package org.jetbrains.skiko.native
import org.jetbrains.skiko.GraphicsApi
// TODO: jvm version uses java properties here. // TODO: jvm version uses java properties here.
// there is no properties support available for native. // there is no properties support available for native.
internal object SkikoProperties { internal object SkikoProperties {
......
package org.jetbrains.skiko.native.redrawer
// TODO: this is exact copy of jvm counterpart. Commonize!
interface Redrawer {
fun dispose()
fun needRedraw()
fun redrawImmediately()
fun syncSize() = Unit
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment