Skip to content

Commit

Permalink
Fix shared immutability for background threads on Kotlin Native (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt authored Sep 28, 2021
1 parent a9ec6ad commit 7204cc9
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- uses: eskatos/gradle-command-action@v1
with:
dependencies-cache-enabled: true
arguments: mingwX64Test --stacktrace
arguments: mingwX64Test :colormath:mingwX64BackgroundTest --stacktrace

deploy-mac-and-linux:
needs: [ linux-tests, macos-tests, windows-tests ]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- uses: eskatos/gradle-command-action@v1
with:
dependencies-cache-enabled: true
arguments: mingwX64Test --stacktrace
arguments: mingwX64Test :colormath:mingwX64BackgroundTest --stacktrace

env:
GRADLE_OPTS: -Dorg.gradle.configureondemand=true -Dorg.gradle.parallel=true -Dkotlin.incremental=false -Dorg.gradle.project.kotlin.incremental.multiplatform=false -Dorg.gradle.project.kotlin.native.disableCompilerDaemon=true -Dorg.gradle.jvmargs="-Xmx5g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased
### Fixed
- Fix shared immutability for background threads on Kotlin Native

## [3.1.0] - 2021-09-24
### Added
- Optional modules with extensions for converting between Colormath colors and other platform representations.
Expand Down
18 changes: 18 additions & 0 deletions colormath/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@file:Suppress("UNUSED_VARIABLE", "PropertyName")

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithTests
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
import org.jetbrains.kotlin.gradle.plugin.mpp.TestExecutable

plugins {
kotlin("multiplatform")
}
Expand Down Expand Up @@ -42,6 +46,20 @@ kotlin {
}
}
}

targets.withType<KotlinNativeTargetWithTests<*>> {
binaries {
// Configure a separate test where code runs in background
test("background", setOf(NativeBuildType.DEBUG)) {
freeCompilerArgs = freeCompilerArgs + "-trw"
}
}
testRuns {
val background by creating {
setExecutionSourceFrom(binaries.getByName("backgroundDebugTest") as TestExecutable)
}
}
}
}

val jvmJar by tasks.getting(Jar::class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.ajalt.colormath.internal

import kotlin.native.concurrent.SharedImmutable

// Constants used in LAB and LUV conversions.
// http://www.brucelindbloom.com/index.html?LContinuity.html
/** ϵ = (6/29)^3 */
Expand All @@ -13,10 +15,12 @@ internal const val CIE_E_times_K = 8.0

/** The CIECAM02 transform matrix for XYZ -> LMS */
// https://en.wikipedia.org/wiki/CIECAM02#CAT02
@SharedImmutable
internal val CAT02_XYZ_TO_LMS = Matrix(
+0.7328f, +0.4296f, -0.1624f,
-0.7036f, +1.6975f, +0.0061f,
+0.0030f, +0.0136f, +0.9834f,
)

@SharedImmutable
internal val CAT02_LMS_TO_XYZ = CAT02_XYZ_TO_LMS.inverse()
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.ajalt.colormath.ColorSpace
import com.github.ajalt.colormath.internal.*
import com.github.ajalt.colormath.model.RGBColorSpaces.BT2020
import com.github.ajalt.colormath.model.XYZColorSpaces.XYZ65
import kotlin.native.concurrent.SharedImmutable

/**
* The ICtCp color space, designed for high dynamic range and wide color gamut imagery.
Expand Down Expand Up @@ -98,34 +99,45 @@ private object PqNonlinearity : RGBColorSpace.TransferFunctions {
}
}

@SharedImmutable
private val ICTCP_RGB_TO_LMS = Matrix(
1688f, 2146f, 262f,
683f, 2951f, 462f,
99f, 309f, 3688f,
).scalarDiv(4096f, inPlace = true)

@SharedImmutable
private val ICTCP_LMS_TO_ICTCP = Matrix(
2048f, 2048f, 0f,
6610f, -13613f, 7003f,
17933f, -17390f, -543f,
).scalarDiv(4096f, inPlace = true)

@SharedImmutable
private val ICTCP_LMS_to_RGB = ICTCP_RGB_TO_LMS.inverse()

@SharedImmutable
private val ICTCP_ICTCP_to_LMS = ICTCP_LMS_TO_ICTCP.inverse()

// ICtCp defines the XYZ to LMS matrix by multiplying a crosstalk matrix with the old
// Hunt-Pointer-Estevez transform. It's not clear why they use HPE rather than one of the newer
// transforms.
@SharedImmutable
private val ICTCP_CROSSTALK = Matrix(
0.92f, 0.04f, 0.04f,
0.04f, 0.92f, 0.04f,
0.04f, 0.04f, 0.92f,
)

@SharedImmutable
private val HPE_XYZ_TO_LMS = Matrix(
0.4002f, 0.7076f, -0.0808f,
-0.2263f, 1.1653f, 0.0457f,
0f, 0f, 0.9182f,
)

@SharedImmutable
private val ICTCP_XYZ_TO_LMS = ICTCP_CROSSTALK.dot(HPE_XYZ_TO_LMS)

@SharedImmutable
private val ICTCP_LMS_TO_XYZ = ICTCP_XYZ_TO_LMS.inverse()
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.github.ajalt.colormath.WhitePoint
import com.github.ajalt.colormath.internal.*
import kotlin.math.log2
import kotlin.math.pow
import kotlin.native.concurrent.SharedImmutable

object RGBColorSpaces {
/**
Expand Down Expand Up @@ -263,8 +264,13 @@ private data class RGBColorSpaceImpl(
override operator fun invoke(r: Float, g: Float, b: Float, alpha: Float): RGB = RGB(r, g, b, alpha, this)
}

@SharedImmutable
private val SRGB_R = xyY(0.6400, 0.3300)

@SharedImmutable
private val SRGB_G = xyY(0.3000, 0.6000)

@SharedImmutable
private val SRGB_B = xyY(0.1500, 0.0600)

private object SRGBTransferFunctions : RGBColorSpace.TransferFunctions {
Expand All @@ -283,16 +289,27 @@ private object SRGBTransferFunctions : RGBColorSpace.TransferFunctions {
}
}

@SharedImmutable
private val ACES_WHITE_POINT = WhitePoint("ACES", xyY(0.32168, 0.33767))

// values from [Academy TB-2014-004]
@SharedImmutable
private val ACES_AP0_R = xyY(0.73470, 0.26530)

@SharedImmutable
private val ACES_AP0_G = xyY(0.00000, 1.00000)

@SharedImmutable
private val ACES_AP0_B = xyY(0.00010, -0.0770)

// values from [Academy S-2014-004]
@SharedImmutable
private val ACES_AP1_R = xyY(0.713, 0.293)

@SharedImmutable
private val ACES_AP1_G = xyY(0.165, 0.830)

@SharedImmutable
private val ACES_AP1_B = xyY(0.128, 0.044)

// from [Academy S-2014-003]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.github.ajalt.colormath.internal.dot
import com.github.ajalt.colormath.model.*
import com.github.ajalt.colormath.model.RGBColorSpaces.SRGB
import com.github.ajalt.colormath.model.XYZColorSpaces.XYZ65
import kotlin.native.concurrent.SharedImmutable

/**
* Create a chromatic adapter that will adapt colors from a given [sourceWhite] to this color space's
Expand Down Expand Up @@ -68,5 +69,8 @@ private inline fun <T> doAdapt(transform: Matrix, r: Float, g: Float, b: Float,
}
}

@SharedImmutable
private val xyzToSrgb = Matrix(SRGB.matrixFromXyz)

@SharedImmutable
private val srgbToXYZ = Matrix(SRGB.matrixToXyz)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.ajalt.colormath.ColorComponentInfo
import com.github.ajalt.colormath.ColorSpace
import com.github.ajalt.colormath.internal.scaleRange
import com.github.ajalt.colormath.transform.InterpolationMethod.Point
import kotlin.native.concurrent.SharedImmutable

/**
* Interpolate linearly between this color and [other].
Expand Down Expand Up @@ -372,6 +373,7 @@ private fun requireComponentName(space: ColorSpace<*>, name: String): String {
return name.lowercase()
}

@SharedImmutable
private val alphaAdjustment: ComponentAdjustment = { l ->
when {
l.all { it.isNaN() } -> l
Expand Down

0 comments on commit 7204cc9

Please sign in to comment.