Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.facebook.react.bridge.WritableMap
import com.facebook.react.fabric.events.EventEmitterWrapper
import com.facebook.react.fabric.mounting.mountitems.MountItem
import com.facebook.react.touch.JSResponderHandler
import com.facebook.react.uimanager.AnimatedTransform
import com.facebook.react.uimanager.RootViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewManagerRegistry
Expand Down Expand Up @@ -278,6 +279,19 @@ internal class MountingManager(
return true
}

/** See [SurfaceMountingManager.updateAnimatedPropsTyped]. */
@UiThread
fun updateAnimatedPropsTyped(
reactTag: Int,
hasOpacity: Boolean,
opacity: Double,
transform: AnimatedTransform?,
): Boolean {
assertOnUiThread()
val surfaceMountingManager = getSurfaceManagerForView(reactTag) ?: return false
return surfaceMountingManager.updateAnimatedPropsTyped(reactTag, hasOpacity, opacity, transform)
}

@UiThread
fun updatePropsSynchronously(reactTag: Int, props: ReadableMap?) {
assertOnUiThread()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import com.facebook.react.fabric.mounting.MountingManager.MountItemExecutor
import com.facebook.react.fabric.mounting.mountitems.MountItem
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.touch.JSResponderHandler
import com.facebook.react.uimanager.AnimatedPropsApplier
import com.facebook.react.uimanager.AnimatedTransform
import com.facebook.react.uimanager.IViewGroupManager
import com.facebook.react.uimanager.IllegalViewOperationException
import com.facebook.react.uimanager.ReactOverflowViewWithInset
Expand Down Expand Up @@ -118,6 +120,18 @@ internal constructor(
@ThreadConfined(ThreadConfined.UI)
private val tagToSynchronousMountProps = SparseArrayCompat<MutableMap<String, Any>>()

// Overrides from the typed animated props path, kept typed until a React update needs them as
// props. Folded into [tagToSynchronousMountProps] by [flushTypedAnimatedOverride].
@ThreadConfined(ThreadConfined.UI)
private val tagToTypedAnimatedOverride = SparseArrayCompat<TypedAnimatedOverride>()

private class TypedAnimatedOverride {
var hasOpacity: Boolean = false
var opacity: Double = 0.0
var hasTransform: Boolean = false
val transform: AnimatedTransform = AnimatedTransform()
}

@AnyThread
public fun attachRootView(rootView: View, themedReactContext: ThemedReactContext) {
this.context = themedReactContext
Expand Down Expand Up @@ -283,6 +297,7 @@ internal constructor(
context = null
onViewAttachMountItems.clear()
tagToSynchronousMountProps.clear()
tagToTypedAnimatedOverride.clear()
FLog.e(TAG, "Surface [$surfaceId] was stopped on SurfaceMountingManager.")
}

Expand Down Expand Up @@ -622,6 +637,7 @@ internal constructor(

public fun storeSynchronousMountPropsOverride(reactTag: Int, props: ReadableMap) {
if (ReactNativeFeatureFlags.overrideBySynchronousMountPropsAtMountingAndroid()) {
flushTypedAnimatedOverride(reactTag)
val propsMap = getAnimatedPropsMap(props)
val synchronousMountProps = tagToSynchronousMountProps[reactTag] ?: mutableMapOf()
clearAnimatedNullPropsMapKeys(props, synchronousMountProps)
Expand All @@ -638,6 +654,66 @@ internal constructor(
updateProps(reactTag, props, true)
}

/**
* Typed counterpart of [storeSynchronousMountPropsOverride] plus [updatePropsSynchronously] for
* opacity and transform ([transform] null when absent). Returns false when this view isn't
* covered by [AnimatedPropsApplier], so the caller can use the props map path instead.
*
* Unlike the map path, this leaves [ViewState.currentProps] as the last committed props instead
* of replacing them with the animated subset. Covered view managers don't read them in
* `updateState`.
*/
@UiThread
internal fun updateAnimatedPropsTyped(
reactTag: Int,
hasOpacity: Boolean,
opacity: Double,
transform: AnimatedTransform?,
): Boolean {
if (isStopped) {
return false
}
val viewState = getNullableViewState(reactTag) ?: return false
val view = viewState.view ?: return false
val viewManager = viewState.viewManager ?: return false
if (!AnimatedPropsApplier.supports(viewManager)) {
return false
}
if (!hasOpacity && transform == null) {
return true
}
if (ReactNativeFeatureFlags.overrideBySynchronousMountPropsAtMountingAndroid()) {
val override =
tagToTypedAnimatedOverride[reactTag]
?: TypedAnimatedOverride().also { tagToTypedAnimatedOverride.put(reactTag, it) }
if (hasOpacity) {
override.hasOpacity = true
override.opacity = opacity
}
if (transform != null) {
override.transform.copyFrom(transform)
override.hasTransform = true
}
}
AnimatedPropsApplier.apply(viewManager, view, hasOpacity, opacity, transform)
return true
}

/** Moves a typed override into [tagToSynchronousMountProps], after any older map overrides. */
private fun flushTypedAnimatedOverride(reactTag: Int) {
val override = tagToTypedAnimatedOverride[reactTag] ?: return
tagToTypedAnimatedOverride.remove(reactTag)
val props =
tagToSynchronousMountProps[reactTag]
?: mutableMapOf<String, Any>().also { tagToSynchronousMountProps.put(reactTag, it) }
if (override.hasOpacity) {
props[PROP_OPACITY] = override.opacity
}
if (override.hasTransform) {
props[PROP_TRANSFORM] = override.transform.toPropsList()
}
}

public fun updateProps(reactTag: Int, props: ReadableMap) {
updateProps(reactTag, props, false)
}
Expand All @@ -661,6 +737,12 @@ internal constructor(
return
}

if (
ReactNativeFeatureFlags.overrideBySynchronousMountPropsAtMountingAndroid() &&
!shouldSkipSynchronousMountPropsOverride
) {
flushTypedAnimatedOverride(reactTag)
}
if (
ReactNativeFeatureFlags.overrideBySynchronousMountPropsAtMountingAndroid() &&
!shouldSkipSynchronousMountPropsOverride &&
Expand Down Expand Up @@ -1071,11 +1153,9 @@ internal constructor(
return
}

if (
ReactNativeFeatureFlags.overrideBySynchronousMountPropsAtMountingAndroid() &&
tagToSynchronousMountProps.containsKey(reactTag)
) {
if (ReactNativeFeatureFlags.overrideBySynchronousMountPropsAtMountingAndroid()) {
tagToSynchronousMountProps.remove(reactTag)
tagToTypedAnimatedOverride.remove(reactTag)
}

val viewState = getNullableViewState(reactTag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,52 @@ import com.facebook.react.bridge.ReadableMap
import com.facebook.react.fabric.FabricUIManager.IS_DEVELOPMENT_ENVIRONMENT
import com.facebook.react.fabric.mounting.MountItemDispatcher
import com.facebook.react.fabric.mounting.MountingManager
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.uimanager.AnimatedTransform
import com.facebook.react.uimanager.ViewProps

/** Applies a view's typed animated props. Primitive parameters, so a call doesn't box. */
internal fun interface TypedAnimatedPropsApplier {
/** Returns false when the caller should apply the view's props through the props map path. */
fun apply(
viewTag: Int,
hasOpacity: Boolean,
opacity: Double,
transform: AnimatedTransform?,
): Boolean
}

internal class BatchedAnimatedPropsMountItem(
private val intBuffer: IntArray,
private val doubleBuffer: DoubleArray,
private val rawPropsBuffer: Array<Any?>,
private val mountItemDispatcher: MountItemDispatcher,
) : MountItem {

// Result of the last successful [scanTyped].
private var scannedIntEnd = 0
private var scannedDoubleEnd = 0
private var scannedHasOpacity = false
private var scannedOpacity = 0.0
private var scannedHasTransform = false
private val scannedTransform = AnimatedTransform()

override fun execute(mountingManager: MountingManager) {
decode { viewTag, props ->
val typedApplier =
if (ReactNativeFeatureFlags.enableTypedAnimatedPropsAndroid()) {
TypedAnimatedPropsApplier { viewTag, hasOpacity, opacity, transform ->
try {
mountingManager.updateAnimatedPropsTyped(viewTag, hasOpacity, opacity, transform)
} catch (ex: Exception) {
// Match SynchronousMountItem's handling of view-manager failures. Report the view as
// handled: it may be partly updated, so applying it again through props is no better.
true
}
}
} else {
null
}
decode(typedApplier) { viewTag, props ->
try {
if (!mountingManager.updateAnimatedPropsSynchronously(viewTag, props)) {
mountItemDispatcher.addMountItem(SynchronousMountItem(viewTag, props))
Expand All @@ -35,7 +70,74 @@ internal class BatchedAnimatedPropsMountItem(
}
}

/**
* Reads one view, starting after its tag, when its commands are only opacity and plain number
* transform operations. Returns false, without consuming anything, when the view has any other
* command, so the caller decodes it as props.
*
* Views whose manager the typed path doesn't cover are scanned and then decoded as props; the
* scan only reads the buffers.
*/
private fun scanTyped(intStart: Int, doubleStart: Int): Boolean {
var intIdx = intStart
var doubleIdx = doubleStart
scannedHasOpacity = false
scannedHasTransform = false
while (true) {
when (intBuffer[intIdx++]) {
CMD_END_OF_VIEW -> break
CMD_OPACITY -> {
scannedHasOpacity = true
scannedOpacity = doubleBuffer[doubleIdx++]
}
CMD_START_OF_TRANSFORM -> {
// A later transform replaces an earlier one, as it does in the props map.
scannedHasTransform = true
scannedTransform.clear()
while (true) {
val transformCmd = intBuffer[intIdx++]
if (transformCmd == CMD_END_OF_TRANSFORM) {
break
}
val op =
when (transformCmd) {
CMD_TRANSLATE_X,
CMD_TRANSLATE_Y -> {
if (intBuffer[intIdx++] != CMD_UNIT_PX) {
return false
}
if (transformCmd == CMD_TRANSLATE_X) AnimatedTransform.TRANSLATE_X
else AnimatedTransform.TRANSLATE_Y
}
CMD_SCALE -> AnimatedTransform.SCALE
CMD_SCALE_X -> AnimatedTransform.SCALE_X
CMD_SCALE_Y -> AnimatedTransform.SCALE_Y
CMD_ROTATE -> AnimatedTransform.ROTATE
CMD_ROTATE_X -> AnimatedTransform.ROTATE_X
CMD_ROTATE_Y -> AnimatedTransform.ROTATE_Y
CMD_ROTATE_Z -> AnimatedTransform.ROTATE_Z
CMD_PERSPECTIVE -> AnimatedTransform.PERSPECTIVE
else -> return false
}
scannedTransform.add(op, doubleBuffer[doubleIdx++])
}
}
else -> return false
}
}
scannedIntEnd = intIdx
scannedDoubleEnd = doubleIdx
return true
}

internal fun decode(apply: (Int, ReadableMap) -> Unit) {
decode(null, apply)
}

internal fun decode(
typedApplier: TypedAnimatedPropsApplier?,
apply: (Int, ReadableMap) -> Unit,
) {
var intIdx = 0
var doubleIdx = 0
var rawIdx = 0
Expand All @@ -51,6 +153,21 @@ internal class BatchedAnimatedPropsMountItem(
continue
}

if (
typedApplier != null &&
scanTyped(intIdx, doubleIdx) &&
typedApplier.apply(
viewTag,
scannedHasOpacity,
scannedOpacity,
if (scannedHasTransform) scannedTransform else null,
)
) {
intIdx = scannedIntEnd
doubleIdx = scannedDoubleEnd
continue
}

val props = JavaOnlyMap()
while (true) {
val cmd = intBuffer[intIdx++]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<9a675a67a9ce5e7ca443dd319e2af91a>>
* @generated SignedSource<<22f95267d83e97d88852287061a6df4f>>
*/

/**
Expand Down Expand Up @@ -354,6 +354,12 @@ public object ReactNativeFeatureFlags {
@JvmStatic
public fun enableSwiftUIBasedFilters(): Boolean = accessor.enableSwiftUIBasedFilters()

/**
* Applies opacity and transform updates from the shared animation backend to Android views through typed setters, instead of building a props map and dispatching through the generic ViewManager setters on every frame.
*/
@JvmStatic
public fun enableTypedAnimatedPropsAndroid(): Boolean = accessor.enableTypedAnimatedPropsAndroid()

/**
* Enables View Culling: as soon as a view goes off screen, it can be reused anywhere in the UI and pieced together with other items to create new UI elements.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<6b668cbb1ef60126d7e08b2d853b1262>>
* @generated SignedSource<<f6b0745352928f5e4a301ee1665f6999>>
*/

/**
Expand Down Expand Up @@ -74,6 +74,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
private var enablePropsUpdateReconciliationAndroidCache: Boolean? = null
private var enableResizeObserverByDefaultCache: Boolean? = null
private var enableSwiftUIBasedFiltersCache: Boolean? = null
private var enableTypedAnimatedPropsAndroidCache: Boolean? = null
private var enableViewCullingCache: Boolean? = null
private var enableViewRecyclingCache: Boolean? = null
private var enableViewRecyclingForImageCache: Boolean? = null
Expand Down Expand Up @@ -610,6 +611,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun enableTypedAnimatedPropsAndroid(): Boolean {
var cached = enableTypedAnimatedPropsAndroidCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.enableTypedAnimatedPropsAndroid()
enableTypedAnimatedPropsAndroidCache = cached
}
return cached
}

override fun enableViewCulling(): Boolean {
var cached = enableViewCullingCache
if (cached == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<0890a0b7979d00172b0a79abd94f6c3e>>
* @generated SignedSource<<3f593d5418d172954b56dfe92bb2f77e>>
*/

/**
Expand Down Expand Up @@ -136,6 +136,8 @@ public object ReactNativeFeatureFlagsCxxInterop {

@DoNotStrip @JvmStatic public external fun enableSwiftUIBasedFilters(): Boolean

@DoNotStrip @JvmStatic public external fun enableTypedAnimatedPropsAndroid(): Boolean

@DoNotStrip @JvmStatic public external fun enableViewCulling(): Boolean

@DoNotStrip @JvmStatic public external fun enableViewRecycling(): Boolean
Expand Down
Loading
Loading