Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhirkevich Alexander Y authored and Zhirkevich Alexander Y committed Jul 19, 2024
1 parent d432ddb commit 85180b8
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import io.github.alexzhirkevich.compottie.internal.AnimationState
import io.github.alexzhirkevich.compottie.internal.assets.LottieAsset
import io.github.alexzhirkevich.compottie.internal.layers.CompositionLayer
import io.github.alexzhirkevich.compottie.internal.layers.Layer
import io.github.alexzhirkevich.compottie.internal.utils.fastReset
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlin.math.roundToInt
Expand Down Expand Up @@ -342,7 +343,7 @@ private class LottiePainter(
}

override fun DrawScope.onDraw() {
matrix.reset()
matrix.fastReset()

val scale = ContentScale.FillBounds.computeScaleFactor(intrinsicSize, size)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.alexzhirkevich.compottie.internal.helpers.BooleanInt
import io.github.alexzhirkevich.compottie.internal.platform.ComposeBackend
import io.github.alexzhirkevich.compottie.internal.platform.currentComposeBackend
import io.github.alexzhirkevich.compottie.internal.utils.degreeToRadians
import io.github.alexzhirkevich.compottie.internal.utils.fastReset
import io.github.alexzhirkevich.compottie.internal.utils.preConcat
import io.github.alexzhirkevich.compottie.internal.utils.preRotate
import io.github.alexzhirkevich.compottie.internal.utils.preRotateX
Expand Down Expand Up @@ -63,7 +64,7 @@ internal abstract class AnimatedTransform {
fun matrix(state: AnimationState): Matrix {
val autoOrient = state.layer.autoOrient == BooleanInt.Yes

matrix.reset()
matrix.fastReset()

if (isHidden(state)){
return matrix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.alexzhirkevich.compottie.internal.animation
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Matrix
import io.github.alexzhirkevich.compottie.internal.AnimationState
import io.github.alexzhirkevich.compottie.internal.utils.fastReset
import io.github.alexzhirkevich.compottie.internal.utils.preRotate
import io.github.alexzhirkevich.compottie.internal.utils.preScale
import io.github.alexzhirkevich.compottie.internal.utils.preTranslate
Expand Down Expand Up @@ -40,10 +41,8 @@ internal class RepeaterTransform(
val endOpacity : AnimatedNumber? = null,
) : AnimatedTransform() {



fun repeaterMatrix(state: AnimationState, amount: Float): Matrix {
matrix.reset()
matrix.fastReset()

position.interpolated(state).takeIf { it != Vec2.Zero }?.let {
matrix.preTranslate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.github.alexzhirkevich.compottie.internal.AnimationState
import io.github.alexzhirkevich.compottie.internal.animation.AnimatedTransform
import io.github.alexzhirkevich.compottie.internal.animation.interpolatedNorm
import io.github.alexzhirkevich.compottie.internal.platform.addPath
import io.github.alexzhirkevich.compottie.internal.utils.fastReset
import io.github.alexzhirkevich.compottie.internal.utils.fastSetFrom
import io.github.alexzhirkevich.compottie.internal.utils.preConcat
import io.github.alexzhirkevich.compottie.internal.utils.union
Expand Down Expand Up @@ -103,7 +104,7 @@ internal class ContentGroupImpl(
if (hidden(state)) {
return path
}
matrix.reset()
matrix.fastReset()

matrix.fastSetFrom(transform.matrix(state))
pathContents.fastForEachReversed {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ internal class Bezier(
}

fun mapPath(outPath : Path) {
outPath.reset()
outPath.rewind()
outPath.moveTo(initialPoint.x, initialPoint.y)

var pathFromDataCurrentPoint = initialPoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal fun Path.applyTrimPath(
}


tempPath.reset()
tempPath.rewind()
pathMeasure.getSegment(
startDistance = newStart,
stopDistance = newEnd,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import io.github.alexzhirkevich.compottie.internal.platform.addCodePoint
import io.github.alexzhirkevich.compottie.internal.platform.charCount
import io.github.alexzhirkevich.compottie.internal.platform.codePointAt
import io.github.alexzhirkevich.compottie.internal.platform.isModifier
import io.github.alexzhirkevich.compottie.internal.utils.fastReset
import io.github.alexzhirkevich.compottie.internal.utils.preScale
import io.github.alexzhirkevich.compottie.internal.utils.preTranslate
import io.github.alexzhirkevich.compottie.internal.utils.toOffset
Expand Down Expand Up @@ -659,7 +660,7 @@ internal class TextLayer(
fontScale : Float,
document: TextDocument,
) {
matrix.reset();
matrix.fastReset();
matrix.preTranslate(0f, -(document.baselineShift ?: 0f))
matrix.preScale(fontScale, fontScale);
character.data?.draw(drawScope, state, matrix, strokePaint, fillPaint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ internal class FillShape(
) : Shape, DrawingContent {

@Transient
private val path = Path().apply {
fillRule?.asPathFillType()?.let {
fillType = it
}
}
private val path = Path()

@Transient
private val fillType = fillRule?.asPathFillType() ?: path.fillType

@Transient
private var paths: List<PathContent> = emptyList()
Expand Down Expand Up @@ -122,7 +121,9 @@ internal class FillShape(
roundShape?.applyTo(paint, state)

state.layer.effectsApplier.applyTo(paint, state, effectsState)

path.rewind()
path.fillType = fillType

paths.fastForEach {
path.addPath(it.getPath(state), parentMatrix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ internal class GradientFillShape(
val fillRule : FillRule? = null,
) : Shape, DrawingContent {

@Transient
private val path = Path()

@Transient
private val path = Path().apply {
fillRule?.asPathFillType()?.let {
fillType = it
}
}
private val fillType = fillRule?.asPathFillType() ?: path.fillType

@Transient
private val boundsRect = MutableRect(0f,0f,0f,0f)
Expand Down Expand Up @@ -143,6 +141,7 @@ internal class GradientFillShape(
state.layer.effectsApplier.applyTo(paint, state, effectsState)

path.rewind()
path.fillType = fillType

pathContents.fastForEach {
path.addPath(it.getPath(state), parentMatrix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal class MergePathsShape(

override fun getPath(state: AnimationState): Path {

path.reset()
path.rewind()

val hidden = dynamicShape?.hidden.derive(hidden, state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ internal fun Matrix.preConcat(other : Matrix) {
// timesAssign(other)
}

internal fun Matrix.fastReset() {
fastSetFrom(IdentityMatrix)
}

internal fun Matrix.fastSetFrom(other : Matrix){
other.values.copyInto(values)
}
Expand Down Expand Up @@ -80,20 +84,20 @@ internal fun Matrix.preRotate(degree : Float) {

internal fun Matrix.preRotateX(degree : Float) {
preConcat(tempMatrixTransform.apply {
reset()
fastReset()
rotateX(degree)
})
}

internal fun Matrix.preRotateY(degree : Float) {
preConcat(tempMatrixTransform.apply {
reset()
fastReset()
rotateY(degree)
})
}
internal fun Matrix.preRotateZ(degree : Float) {
preConcat(tempMatrixTransform.apply {
reset()
fastReset()
rotateZ(degree)
})
}
Expand Down

0 comments on commit 85180b8

Please sign in to comment.