Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions skiko/src/awtMain/kotlin/org/jetbrains/skiko/ResourceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ internal fun autoCloseScope(body: CloseScope.() -> Unit) {
try {
scope.body()
} finally {
resources.asReversed().forEach {
it.close()
for (index in resources.indices.reversed()) {
val item = resources[index]
item.close()
}
}
}
40 changes: 21 additions & 19 deletions skiko/src/awtMain/kotlin/org/jetbrains/skiko/SkiaLayer.awt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ actual open class SkiaLayer internal constructor(

private fun notifyChange(kind: PropertyKind) {
stateChangeListeners[kind]?.let { handlers ->
handlers.forEach { it(this) }
for (index in handlers.indices) {
handlers[index].invoke(this)
}
}
}

Expand Down Expand Up @@ -598,21 +600,23 @@ actual open class SkiaLayer internal constructor(
// If this approach will be changed, create an issue in https://youtrack.jetbrains.com/issues/CMP for changing it in
// https://github.com/JetBrains/compose-multiplatform/blob/e4e2d329709cded91a09cc612d4defbce37aad96/benchmarks/multiplatform/benchmarks/src/commonMain/kotlin/MeasureComposable.kt#L151 as well

val pictureWidth = (backedLayer.width * contentScale).toInt().coerceAtLeast(0)
val pictureHeight = (backedLayer.height * contentScale).toInt().coerceAtLeast(0)
val pictureWidth = (backedLayer.width * contentScale).coerceAtLeast(0f)
val pictureHeight = (backedLayer.height * contentScale).coerceAtLeast(0f)
val intWidth = pictureWidth.toInt()
val intHeight = pictureHeight.toInt()

val bounds = Rect.makeWH(pictureWidth.toFloat(), pictureHeight.toFloat())
val pictureRecorder = pictureRecorder!!
val canvas = pictureRecorder.beginRecording(bounds)
val canvas = pictureRecorder.beginRecording(0f, 0f, pictureWidth, pictureHeight)

// clipping
for (component in clipComponents) {
canvas.clipRectBy(component, contentScale)
for (index in clipComponents.indices) {
val item = clipComponents[index]
canvas.clipRectBy(item, contentScale)
}

try {
isRendering = true
renderDelegate?.onRender(canvas, pictureWidth, pictureHeight, nanoTime)
renderDelegate?.onRender(canvas, intWidth, intHeight, nanoTime)
} finally {
isRendering = false
}
Expand All @@ -623,7 +627,7 @@ actual open class SkiaLayer internal constructor(
synchronized(pictureLock) {
picture?.instance?.close()
val picture = pictureRecorder.finishRecordingAsPicture()
this.picture = PictureHolder(picture, pictureWidth, pictureHeight)
this.picture = PictureHolder(picture, intWidth, intHeight)
}
}
}
Expand Down Expand Up @@ -730,17 +734,15 @@ internal fun defaultFPSCounter(
logOnTick = true
)
}

internal fun Canvas.clipRectBy(rectangle: ClipRectangle, scale: Float) {
@Suppress("NOTHING_TO_INLINE")
internal inline fun Canvas.clipRectBy(rectangle: ClipRectangle, scale: Float) {
clipRect(
Rect.makeLTRB(
rectangle.x * scale,
rectangle.y * scale,
(rectangle.x + rectangle.width) * scale,
(rectangle.y + rectangle.height) * scale
),
ClipMode.DIFFERENCE,
true
left = rectangle.x * scale,
top = rectangle.y * scale,
right = (rectangle.x + rectangle.width) * scale,
bottom = (rectangle.y + rectangle.height) * scale,
mode = ClipMode.DIFFERENCE,
antiAlias = true
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ open class SkiaSwingLayer(
override fun onRender(canvas: Canvas, width: Int, height: Int, nanoTime: Long) {
val scale = graphicsConfiguration.defaultTransform.scaleX.toFloat()
// clipping
for (component in clipComponents) {
canvas.clipRectBy(component, scale)
for (index in clipComponents.indices) {
val item = clipComponents[index]
canvas.clipRectBy(item, scale)
}
renderDelegate.onRender(canvas, width, height, nanoTime)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ class AnimationFrameInfo(
requiredFrame,
duration,
fullyReceived,
ColorAlphaType.values()[alphaTypeOrdinal],
ColorAlphaType.entries[alphaTypeOrdinal],
hasAlphaWithinBounds,
AnimationDisposalMode.values()[disposalMethodOrdinal],
BlendMode.values()[blendModeOrdinal],
AnimationDisposalMode.entries[disposalMethodOrdinal],
BlendMode.entries[blendModeOrdinal],
frameRect
)

Expand Down
70 changes: 63 additions & 7 deletions skiko/src/commonMain/kotlin/org/jetbrains/skia/Canvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ open class Canvas internal constructor(ptr: NativePointer, managed: Boolean, int
Stats.onNativeCall()
try {
interopScope {
_nDrawPoints(_ptr, 0 /* SkCanvas::PointMode::kPoints_PointMode */, coords.size, toInterop(coords), getPtr(paint))
_nDrawPoints(
_ptr,
0 /* SkCanvas::PointMode::kPoints_PointMode */,
coords.size,
toInterop(coords),
getPtr(paint)
)
}
} finally {
reachabilityBarrier(paint)
Expand Down Expand Up @@ -187,7 +193,13 @@ open class Canvas internal constructor(ptr: NativePointer, managed: Boolean, int
Stats.onNativeCall()
try {
interopScope {
_nDrawPoints(_ptr, 1 /* SkCanvas::PointMode::kLines_PointMode */, coords.size, toInterop(coords),getPtr(paint))
_nDrawPoints(
_ptr,
1 /* SkCanvas::PointMode::kLines_PointMode */,
coords.size,
toInterop(coords),
getPtr(paint)
)
}
} finally {
reachabilityBarrier(paint)
Expand Down Expand Up @@ -248,7 +260,13 @@ open class Canvas internal constructor(ptr: NativePointer, managed: Boolean, int
Stats.onNativeCall()
try {
interopScope {
_nDrawPoints(_ptr, 2 /* SkCanvas::PointMode::kPolygon_PointMode */, coords.size, toInterop(coords), getPtr(paint))
_nDrawPoints(
_ptr,
2 /* SkCanvas::PointMode::kPolygon_PointMode */,
coords.size,
toInterop(coords),
getPtr(paint)
)
}
} finally {
reachabilityBarrier(this)
Expand Down Expand Up @@ -1090,7 +1108,7 @@ open class Canvas internal constructor(ptr: NativePointer, managed: Boolean, int
return this
}

fun clipRect(left : Float, top : Float, right: Float, bottom : Float, mode: ClipMode, antiAlias: Boolean): Canvas {
fun clipRect(left: Float, top: Float, right: Float, bottom: Float, mode: ClipMode, antiAlias: Boolean): Canvas {
Stats.onNativeCall()
_nClipRect(_ptr, left, top, right, bottom, mode.ordinal, antiAlias)
return this
Expand All @@ -1104,14 +1122,44 @@ open class Canvas internal constructor(ptr: NativePointer, managed: Boolean, int
return clipRect(r, ClipMode.INTERSECT, antiAlias)
}

fun clipRect(left: Float, top: Float, right: Float, bottom: Float, antiAlias: Boolean): Canvas {
return clipRect(left, top, right, bottom, ClipMode.INTERSECT, antiAlias)
}

fun clipRect(r: Rect): Canvas {
return clipRect(r, ClipMode.INTERSECT, false)
}

fun clipRRect(r: RRect, mode: ClipMode, antiAlias: Boolean): Canvas {
Stats.onNativeCall()
interopScope {
_nClipRRect(_ptr, r.left, r.top, r.right, r.bottom, toInterop(r.radii), r.radii.size, mode.ordinal, antiAlias)
_nClipRRect(
_ptr,
r.left,
r.top,
r.right,
r.bottom,
toInterop(r.radii),
r.radii.size,
mode.ordinal,
antiAlias
)
}
return this
}

fun clipRRect(
left: Float,
top: Float,
right: Float,
bottom: Float,
radii: FloatArray,
mode: ClipMode,
antiAlias: Boolean
): Canvas {
Stats.onNativeCall()
interopScope {
_nClipRRect(_ptr, left, top, right, bottom, toInterop(radii), radii.size, mode.ordinal, antiAlias)
}
return this
}
Expand All @@ -1120,12 +1168,20 @@ open class Canvas internal constructor(ptr: NativePointer, managed: Boolean, int
return clipRRect(r, mode, false)
}

fun clipRRect(left: Float, top: Float, right: Float, bottom: Float, radii: FloatArray, mode: ClipMode): Canvas {
return clipRRect(left, top, right, bottom, radii, mode, false)
}

fun clipRRect(r: RRect, antiAlias: Boolean): Canvas {
return clipRRect(r, ClipMode.INTERSECT, antiAlias)
}

fun clipRRect(r: RRect): Canvas {
return clipRRect(r, ClipMode.INTERSECT, false)
fun clipRRect(left: Float, top: Float, right: Float, bottom: Float, radii: FloatArray, antiAlias: Boolean): Canvas {
return clipRRect(left, top, right, bottom, radii, ClipMode.INTERSECT, antiAlias)
}

fun clipRRect(left: Float, top: Float, right: Float, bottom: Float, radii: FloatArray): Canvas {
return clipRRect(left, top, right, bottom, radii, ClipMode.INTERSECT, false)
}

fun clipPath(p: Path, mode: ClipMode, antiAlias: Boolean): Canvas {
Expand Down
4 changes: 2 additions & 2 deletions skiko/src/commonMain/kotlin/org/jetbrains/skia/Codec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ class Codec internal constructor(ptr: NativePointer) : Managed(ptr, _FinalizerHo
val encodedOrigin: EncodedOrigin
get() = try {
Stats.onNativeCall()
EncodedOrigin.values().get(_nGetEncodedOrigin(_ptr))
EncodedOrigin.entries[_nGetEncodedOrigin(_ptr)]
} finally {
reachabilityBarrier(this)
}
val encodedImageFormat: EncodedImageFormat
get() = try {
Stats.onNativeCall()
EncodedImageFormat.values().get(_nGetEncodedImageFormat(_ptr))
EncodedImageFormat.entries[_nGetEncodedImageFormat(_ptr)]
} finally {
reachabilityBarrier(this)
}
Expand Down
4 changes: 2 additions & 2 deletions skiko/src/commonMain/kotlin/org/jetbrains/skia/Font.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Font : Managed {
var edging: FontEdging
get() = try {
Stats.onNativeCall()
FontEdging.values().get(_nGetEdging(_ptr))
FontEdging.entries[_nGetEdging(_ptr)]
} finally {
reachabilityBarrier(this)
}
Expand All @@ -231,7 +231,7 @@ class Font : Managed {
var hinting: FontHinting
get() = try {
Stats.onNativeCall()
FontHinting.values().get(_nGetHinting(_ptr))
FontHinting.entries[_nGetHinting(_ptr)]
} finally {
reachabilityBarrier(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FontStyle {
}

val slant: FontSlant
get() = FontSlant.values()[_value shr 24 and 255]
get() = FontSlant.entries[_value shr 24 and 255]

fun withSlant(slant: FontSlant): FontStyle {
return FontStyle(weight, width, slant)
Expand Down
Loading
Loading