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 {
kotlin("multiplatform") version "1.5.31"
}
val coroutinesVersion = "1.5.2"
repositories {
mavenLocal()
jcenter()
......@@ -32,11 +34,9 @@ if (project.hasProperty("skiko.version")) {
}
kotlin {
val nativeTarget = when {
osName == "Mac OS X" -> macosX64()
osName.startsWith("Win") -> mingwX64()
osName.startsWith("Linux") -> linuxX64()
val nativeTarget = when (target) {
"macos-x64" -> macosX64()
"macos-arm64" -> macosArm64()
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
......@@ -53,20 +53,28 @@ kotlin {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.skiko:skiko:$version")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
}
}
val nativeMain by creating {
dependsOn(commonMain)
dependencies {
}
}
val archTargetMain = when (target) {
"macos-x64" -> {
val macosX64Main by getting {
dependsOn(nativeMain)
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-macosx64:1.5.2")
}
macosX64Main
}
"macos-arm64" -> {
val macosArm64Main by getting {
dependsOn(nativeMain)
}
macosArm64Main
}
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
}
}
......@@ -207,11 +207,15 @@ kotlin {
}
if (supportNative) {
val targetString = target
val skiaDir = skiaDir.get().absolutePath
val nativeTarget = when (targetString) {
"macos-x64", "macos-arm64" -> macosX64 {
compilations.all {
val nativeTarget = when (target) {
"macos-x64" -> macosX64 {}
"macos-arm64" -> macosArm64 {}
else -> null
}
val targetString = target
nativeTarget?.let {
it.compilations.all {
kotlinOptions {
freeCompilerArgs += listOf(
"-include-binary",
......@@ -226,8 +230,6 @@ kotlin {
}
}
}
else -> null
}
}
sourceSets {
......@@ -267,14 +269,30 @@ kotlin {
if (supportNative) {
// See https://kotlinlang.org/docs/mpp-share-on-platforms.html#configure-the-hierarchical-structure-manually
val nativeMain by creating {}
val nativeMain by creating {
dependsOn(commonMain)
dependencies {
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)
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0")
}
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
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 {
val resource = SkiaLayer::class.java.getResource("/not-supported-adapter.list").readText()
resource.split(";").map { it.trim() }
......
package org.jetbrains.skiko
enum class OS(val id: String) {
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 {
actual val hostOs: OS by lazy {
val osName = System.getProperty("os.name")
when {
osName == "Mac OS X" -> OS.MacOS
......@@ -31,7 +10,7 @@ val hostOs by lazy {
}
}
val hostArch by lazy {
actual val hostArch: Arch by lazy {
val osArch = System.getProperty("os.arch")
when (osArch) {
"x86_64", "amd64" -> Arch.X64
......@@ -40,7 +19,7 @@ val hostArch by lazy {
}
}
val hostId by lazy {
actual val hostId by lazy {
"${hostOs.id}-${hostArch.id}"
}
......@@ -55,3 +34,6 @@ internal val javaVendor by lazy {
internal val javaLocation by lazy {
"${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 {
}
}
}
OS.JS -> {
TODO("Commonize me")
}
}
}
......
......@@ -43,6 +43,9 @@ internal interface RenderFactory {
GraphicsApi.DIRECT_SOFTWARE -> LinuxSoftwareRedrawer(layer, properties)
else -> LinuxOpenGLRedrawer(layer, properties)
}
OS.JS -> {
TODO("Commonize me")
}
}
}
}
......
......@@ -56,6 +56,7 @@ internal object SkikoProperties {
OS.MacOS -> return GraphicsApi.METAL
OS.Linux -> return GraphicsApi.OPENGL
OS.Windows -> return GraphicsApi.DIRECT3D
OS.JS -> TODO("commonize me")
}
}
......@@ -67,6 +68,7 @@ internal object SkikoProperties {
OS.Linux -> renderApiList = mutableListOf(GraphicsApi.OPENGL, GraphicsApi.DIRECT_SOFTWARE, GraphicsApi.SOFTWARE)
OS.MacOS -> renderApiList = mutableListOf(GraphicsApi.METAL, GraphicsApi.SOFTWARE)
OS.Windows -> renderApiList = mutableListOf(GraphicsApi.DIRECT3D, GraphicsApi.OPENGL, GraphicsApi.DIRECT_SOFTWARE, GraphicsApi.SOFTWARE)
OS.JS -> TODO("commonize me")
}
renderApiList.remove(head)
......
......@@ -5,7 +5,6 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.swing.Swing
import org.jetbrains.skiko.FrameDispatcher
import org.jetbrains.skiko.FrameLimiter
import org.jetbrains.skiko.hostOs
import org.jetbrains.skiko.SkiaLayer
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
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.useContents
import org.jetbrains.skiko.native.*
import org.jetbrains.skiko.redrawer.Redrawer
import platform.CoreFoundation.CFTimeInterval
import platform.CoreGraphics.CGRectMake
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
import org.jetbrains.skiko.native.*
import org.jetbrains.skiko.native.SkikoProperties.renderApi
import org.jetbrains.skiko.native.redrawer.*
import org.jetbrains.skiko.redrawer.Redrawer
internal interface PlatformOperations {
fun createRedrawer(layer: HardwareLayer, properties: SkiaLayerProperties): Redrawer
}
// TODO: commonize more.
internal val platformOperations: PlatformOperations by lazy {
object: PlatformOperations {
override fun createRedrawer(layer: HardwareLayer, properties: SkiaLayerProperties) = when(renderApi) {
GraphicsApi.OPENGL -> MacOsOpenGLRedrawer(layer, properties)
else -> error("No software rendering for native yet")
}
}
makePlatformOperations()
}
internal expect fun makePlatformOperations(): PlatformOperations
package org.jetbrains.skiko.native
import kotlinx.cinterop.pointed
import kotlinx.cinterop.useContents
import org.jetbrains.skiko.native.context.*
import org.jetbrains.skiko.native.redrawer.*
import org.jetbrains.skia.*
import org.jetbrains.skiko.redrawer.Redrawer
interface SkiaRenderer {
fun onRender(canvas: Canvas, width: Int, height: Int, nanoTime: Long)
......@@ -54,10 +53,10 @@ open class SkiaLayer(
val pictureHeight = (height * contentScale).coerceAtLeast(0.0)
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)
val picture = pictureRecorder.finishRecordingAsPicture()!!
val picture = pictureRecorder.finishRecordingAsPicture()
this.picture = PictureHolder(picture, pictureWidth.toInt(), pictureHeight.toInt())
}
......
package org.jetbrains.skiko.native
import org.jetbrains.skiko.GraphicsApi
// TODO: jvm version uses java properties here.
// there is no properties support available for native.
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