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 @@ -12,6 +12,7 @@ import com.facebook.common.logging.FLog
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.CLIPPING_PROHIBITED_VIEW
import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.RVG_ADD_CHILDREN_FOR_ACCESSIBILITY
import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.RVG_CLIPPING_BAND
import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.RVG_IS_VIEW_CLIPPED
import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.RVG_ON_VIEW_REMOVED
import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.SOFT_ASSERTIONS
Expand All @@ -23,6 +24,7 @@ internal object ReactSoftExceptionLogger {
@Retention(AnnotationRetention.SOURCE)
@StringDef(
RVG_ADD_CHILDREN_FOR_ACCESSIBILITY,
RVG_CLIPPING_BAND,
RVG_IS_VIEW_CLIPPED,
RVG_ON_VIEW_REMOVED,
CLIPPING_PROHIBITED_VIEW,
Expand All @@ -35,6 +37,7 @@ internal object ReactSoftExceptionLogger {
object Categories {
const val RVG_ADD_CHILDREN_FOR_ACCESSIBILITY: String =
"ReactViewGroup.addChildrenForAccessibility"
const val RVG_CLIPPING_BAND: String = "ReactViewGroup.clippingBand"
const val RVG_IS_VIEW_CLIPPED: String = "ReactViewGroup.isViewClipped"
const val RVG_ON_VIEW_REMOVED: String = "ReactViewGroup.onViewRemoved"
const val CLIPPING_PROHIBITED_VIEW: String = "ReactClippingProhibitedView"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.view

import android.graphics.Rect
import android.view.View
import androidx.annotation.VisibleForTesting
import kotlin.math.max
import kotlin.math.min

/** Tests turn this off to compare the band with checking every child. */
@VisibleForTesting internal var clippingBandEnabled: Boolean = true

/**
* The children of a clipping [ReactViewGroup] that a new clipping rect can change, so that only
* those are updated instead of every child.
*
* Per axis, the running maximum of the children's ends and the running minimum of their starts are
* monotone in any child order, so binary searches find the band of children that can intersect a
* rect. In a list, that is the rows near the rect.
*
* Children outside the band are left as they are, which requires every child outside [rect] to be
* clipped: a pass over every child establishes it, adding, removing or laying out a child keeps it
* ([State.STALE]), and a focused, animating or excluded child left attached breaks it
* ([State.UNKNOWN]).
*/
internal class ClippingBand {

enum class State {
/** A child outside [rect] may be attached: the next update checks every child. */
UNKNOWN,
/** Children were added, removed or laid out: the band is rebuilt on the next update. */
STALE,
/** The band applies. */
VALID,
/**
* The attached children didn't match the band, which shouldn't happen. Every update checks
* every child until clipping is turned off or the view is recycled.
*/
DISABLED,
}

var state: State = State.UNKNOWN
private set

/** The clipping rect the invariant holds for. */
val rect: Rect = Rect()

/**
* Whether a child isn't laid out yet: those are left out of the running bounds below, as their
* empty frame at the origin would make the band reach every child before them.
*/
private var hasChildrenNotLaidOut = false

private var maxBottoms = IntArray(0)
private var minTops = IntArray(0)
private var maxRights = IntArray(0)
private var minLefts = IntArray(0)

/** The number of updates that only checked the bands, and the size of the last ones. */
@VisibleForTesting var bandUpdateCount: Int = 0
@VisibleForTesting var lastBandSize: Int = 0

/** A child was added, removed or laid out against [clippingRect]. */
fun onChildChanged(clippingRect: Rect?) {
when {
state == State.DISABLED || state == State.UNKNOWN -> Unit
// A child attached against a clipping rect other than [rect] may be outside [rect].
clippingRect != rect -> state = State.UNKNOWN
else -> state = State.STALE
}
}

/** A child outside the clipping rect stays attached, or was laid out without an update. */
fun invalidate() {
if (state != State.DISABLED) {
state = State.UNKNOWN
}
}

/**
* Every child is about to be checked: unless one outside the rect stays attached, the band is
* then rebuilt.
*/
fun onCheckingAllChildren() {
if (state != State.DISABLED) {
state = State.STALE
}
}

/** Every child was checked against [clippingRect], and every child outside it is clipped. */
fun onAllChildrenChecked(clippingRect: Rect, childArray: Array<View?>, count: Int) {
rect.set(clippingRect)
build(childArray, count)
}

fun disable() {
state = State.DISABLED
}

/** Rebuilds the band from the children's frames, after [State.STALE]. */
fun build(childArray: Array<View?>, count: Int) {
if (state == State.DISABLED) {
return
}
if (maxBottoms.size < count) {
maxBottoms = IntArray(childArray.size)
minTops = IntArray(childArray.size)
maxRights = IntArray(childArray.size)
minLefts = IntArray(childArray.size)
}
hasChildrenNotLaidOut = false
var maxBottom = Int.MIN_VALUE
var maxRight = Int.MIN_VALUE
for (i in 0..<count) {
val child = checkNotNull(childArray[i])
if (isNotLaidOut(child)) {
hasChildrenNotLaidOut = true
} else {
maxBottom = max(maxBottom, child.bottom)
maxRight = max(maxRight, child.right)
}
maxBottoms[i] = maxBottom
maxRights[i] = maxRight
}
var minTop = Int.MAX_VALUE
var minLeft = Int.MAX_VALUE
for (i in count - 1 downTo 0) {
val child = checkNotNull(childArray[i])
if (!isNotLaidOut(child)) {
minTop = min(minTop, child.top)
minLeft = min(minLeft, child.left)
}
minTops[i] = minTop
minLefts[i] = minLeft
}
state = State.VALID
}

/**
* Whether a child that isn't laid out yet can intersect [rect]: only if [rect] strictly contains
* the origin.
*/
fun mayIntersectChildNotLaidOut(rect: Rect): Boolean =
hasChildrenNotLaidOut && rect.intersects(0, 0, 0, 0)

/** The first child of the band that can intersect [rect], among [count] children. */
fun start(rect: Rect, count: Int): Int =
max(firstAbove(maxBottoms, count, rect.top), firstAbove(maxRights, count, rect.left))

/** The end of the band that can intersect [rect]: no child from it on can. */
fun end(rect: Rect, count: Int): Int =
min(firstAtLeast(minTops, count, rect.bottom), firstAtLeast(minLefts, count, rect.right))

private companion object {
fun isNotLaidOut(child: View): Boolean =
child.left == 0 && child.top == 0 && child.right == 0 && child.bottom == 0

/** The first index of the non-decreasing [values] whose value is greater than [bound]. */
fun firstAbove(values: IntArray, count: Int, bound: Int): Int {
var low = 0
var high = count
while (low < high) {
val mid = (low + high) ushr 1
if (values[mid] > bound) high = mid else low = mid + 1
}
return low
}

/** The first index of the non-decreasing [values] whose value is at least [bound]. */
fun firstAtLeast(values: IntArray, count: Int, bound: Int): Int {
var low = 0
var high = count
while (low < high) {
val mid = (low + high) ushr 1
if (values[mid] >= bound) high = mid else low = mid + 1
}
return low
}
}
}
Loading
Loading