-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Fix skewX/skewY transforms on Android Q+ (#27649) #56724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab88d5a
Fix skewX/skewY transforms on Android Q+
qflen bea6baf
Make TouchTargetHelper hit-test the parallelogram, not the rectangle
qflen b124213
Record SkewMatrixHelper in the ReactAndroid public API snapshot
qflen 103cfeb
Correct the skew_animation_matrix tag comment to reflect the stored M…
qflen 06d9d5f
Remove obsolete setWindowDisplayMetrics calls from the SkewMatrixHelp…
qflen 88e8967
Consolidate skew detection into a single hasAffine2DSkewTransform
qflen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 223 additions & 0 deletions
223
.../react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/SkewMatrixHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| /* | ||
| * 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.uimanager | ||
|
|
||
| import android.graphics.Matrix | ||
| import com.facebook.common.logging.FLog | ||
| import com.facebook.react.bridge.ReadableArray | ||
| import com.facebook.react.bridge.ReadableMap | ||
| import com.facebook.react.bridge.ReadableType | ||
| import com.facebook.react.common.ReactConstants | ||
|
|
||
| /** | ||
| * Builds a 2D-affine [Matrix] from a `transform` array for the subset of operations Android `View` | ||
| * cannot represent through its individual property setters (specifically `skewX` / `skewY`). Used | ||
| * by [BaseViewManager.setTransformProperty] to apply such transforms via | ||
| * `View.setAnimationMatrix` on Android Q+. | ||
| */ | ||
| public object SkewMatrixHelper { | ||
|
|
||
| @JvmStatic | ||
| public fun hasSkewTransform(transforms: ReadableArray): Boolean { | ||
| if (isRawMatrixShorthand(transforms)) return false | ||
| for (i in 0 until transforms.size()) { | ||
| if (transforms.getType(i) != ReadableType.Map) continue | ||
| val map = transforms.getMap(i) ?: continue | ||
| val type = firstKey(map) ?: continue | ||
| if (type == "skewX" || type == "skewY") return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if [transforms] contains only operations representable by a Skia [Matrix] in 2D: | ||
| * `rotate` / `rotateZ`, `scale`, `scaleX` / `scaleY`, `translate` / `translateX` / `translateY` | ||
| * with zero Z, `skewX`, `skewY`. Returns false for `matrix`, `perspective`, `rotateX`, `rotateY`, | ||
| * a `translate` with a non-zero Z component, and the raw 16-element matrix shorthand used by | ||
| * Fabric LayoutAnimations. | ||
| */ | ||
| @JvmStatic | ||
| public fun isAffine2DTransform(transforms: ReadableArray): Boolean { | ||
| if (isRawMatrixShorthand(transforms)) return false | ||
| for (i in 0 until transforms.size()) { | ||
| if (transforms.getType(i) != ReadableType.Map) continue | ||
| val map = transforms.getMap(i) ?: continue | ||
| val type = firstKey(map) ?: continue | ||
| when (type) { | ||
| "matrix", | ||
| "perspective", | ||
| "rotateX", | ||
| "rotateY" -> return false | ||
| "translate" -> { | ||
| val value = map.getArray(type) | ||
| if (value != null && | ||
| value.size() > 2 && | ||
| value.getType(2) == ReadableType.Number && | ||
| value.getDouble(2) != 0.0) { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * Builds a [Matrix] in pixel coordinates by walking [transforms] left-to-right and applying each | ||
| * operation via `Matrix.preX` around the resolved pivot. Pivot is the view center; if | ||
| * [transformOrigin] is set, it overrides per-axis (Number values are DIP, "P%" strings are | ||
| * P/100 of the view dimension; Z is ignored). | ||
| * | ||
| * Composition is pre-multiplication: when the resulting matrix is applied to a point, the | ||
| * rightmost (last) array entry is applied first. Matches CSS / iOS conventions and the | ||
| * left-to-right-iteration / right-multiply contract of [MatrixMathHelper.multiplyInto] used by | ||
| * [TransformHelper.processTransform]. | ||
| */ | ||
| @JvmStatic | ||
| public fun buildAffine2DMatrix( | ||
| transforms: ReadableArray, | ||
| viewWidthDip: Float, | ||
| viewHeightDip: Float, | ||
| transformOrigin: ReadableArray?, | ||
| ): Matrix { | ||
| val pivotXPx: Float | ||
| val pivotYPx: Float | ||
| if (transformOrigin == null) { | ||
| pivotXPx = PixelUtil.toPixelFromDIP(viewWidthDip / 2f) | ||
| pivotYPx = PixelUtil.toPixelFromDIP(viewHeightDip / 2f) | ||
| } else { | ||
| pivotXPx = | ||
| PixelUtil.toPixelFromDIP( | ||
| resolveOriginAxis(transformOrigin, 0, viewWidthDip, viewWidthDip / 2f)) | ||
| pivotYPx = | ||
| PixelUtil.toPixelFromDIP( | ||
| resolveOriginAxis(transformOrigin, 1, viewHeightDip, viewHeightDip / 2f)) | ||
| } | ||
|
|
||
| val matrix = Matrix() | ||
| for (i in 0 until transforms.size()) { | ||
| if (transforms.getType(i) != ReadableType.Map) continue | ||
| val map = transforms.getMap(i) ?: continue | ||
| val type = firstKey(map) ?: continue | ||
| when (type) { | ||
| "rotate", | ||
| "rotateZ" -> | ||
| matrix.preRotate( | ||
| Math.toDegrees(convertToRadians(map, type)).toFloat(), pivotXPx, pivotYPx) | ||
| "scale" -> { | ||
| val s = map.getDouble(type).toFloat() | ||
| matrix.preScale(s, s, pivotXPx, pivotYPx) | ||
| } | ||
| "scaleX" -> matrix.preScale(map.getDouble(type).toFloat(), 1f, pivotXPx, pivotYPx) | ||
| "scaleY" -> matrix.preScale(1f, map.getDouble(type).toFloat(), pivotXPx, pivotYPx) | ||
| "skewX" -> | ||
| matrix.preSkew( | ||
| Math.tan(convertToRadians(map, type)).toFloat(), 0f, pivotXPx, pivotYPx) | ||
| "skewY" -> | ||
| matrix.preSkew( | ||
| 0f, Math.tan(convertToRadians(map, type)).toFloat(), pivotXPx, pivotYPx) | ||
| "translate" -> { | ||
| val value = map.getArray(type) | ||
| if (value != null && value.size() >= 1) { | ||
| val tx = parseTranslateValue(value, 0, viewWidthDip) | ||
| val ty = if (value.size() > 1) parseTranslateValue(value, 1, viewHeightDip) else 0.0 | ||
| matrix.preTranslate( | ||
| PixelUtil.toPixelFromDIP(tx.toFloat()), PixelUtil.toPixelFromDIP(ty.toFloat())) | ||
| } | ||
| } | ||
| "translateX" -> | ||
| matrix.preTranslate( | ||
| PixelUtil.toPixelFromDIP(parseScalarTranslate(map, type, viewWidthDip).toFloat()), | ||
| 0f) | ||
| "translateY" -> | ||
| matrix.preTranslate( | ||
| 0f, | ||
| PixelUtil.toPixelFromDIP(parseScalarTranslate(map, type, viewHeightDip).toFloat())) | ||
| } | ||
| } | ||
| return matrix | ||
| } | ||
|
|
||
| private fun isRawMatrixShorthand(transforms: ReadableArray): Boolean = | ||
| transforms.size() == 16 && transforms.getType(0) == ReadableType.Number | ||
|
|
||
| private fun firstKey(map: ReadableMap): String? { | ||
| val iter = map.keySetIterator() | ||
| return if (iter.hasNextKey()) iter.nextKey() else null | ||
| } | ||
|
|
||
| private fun resolveOriginAxis( | ||
| origin: ReadableArray, | ||
| axis: Int, | ||
| dimensionDip: Float, | ||
| defaultDip: Float, | ||
| ): Float { | ||
| if (origin.size() <= axis) return defaultDip | ||
| return when (origin.getType(axis)) { | ||
| ReadableType.Number -> origin.getDouble(axis).toFloat() | ||
| ReadableType.String -> { | ||
| val part = origin.getString(axis) ?: return defaultDip | ||
| if (!part.endsWith("%")) return defaultDip | ||
| try { | ||
| (part.dropLast(1).toDouble() * dimensionDip / 100.0).toFloat() | ||
| } catch (e: NumberFormatException) { | ||
| defaultDip | ||
| } | ||
| } | ||
| else -> defaultDip | ||
| } | ||
| } | ||
|
|
||
| private fun parseTranslateValue(value: ReadableArray, index: Int, dimensionDip: Float): Double { | ||
| if (value.getType(index) != ReadableType.String) { | ||
| return value.getDouble(index) | ||
| } | ||
| val s = value.getString(index) ?: return 0.0 | ||
| return parseTranslateString(s, dimensionDip) | ||
| } | ||
|
|
||
| private fun parseScalarTranslate(map: ReadableMap, key: String, dimensionDip: Float): Double { | ||
| if (map.getType(key) != ReadableType.String) { | ||
| return map.getDouble(key) | ||
| } | ||
| val s = map.getString(key) ?: return 0.0 | ||
| return parseTranslateString(s, dimensionDip) | ||
| } | ||
|
|
||
| // Mirrors TransformHelper.parseTranslateValue; kept local for the same reason as | ||
| // convertToRadians below. | ||
| private fun parseTranslateString(s: String, dimensionDip: Float): Double { | ||
| return try { | ||
| if (s.endsWith("%")) { | ||
| s.dropLast(1).toDouble() * dimensionDip / 100.0 | ||
| } else { | ||
| s.toDouble() | ||
| } | ||
| } catch (e: NumberFormatException) { | ||
| FLog.w(ReactConstants.TAG, "Invalid translate value: $s") | ||
| 0.0 | ||
| } | ||
| } | ||
|
|
||
| // Mirrors TransformHelper.convertToRadians; kept local so this helper is self-contained. | ||
| private fun convertToRadians(transformMap: ReadableMap, key: String): Double { | ||
| if (transformMap.getType(key) != ReadableType.String) { | ||
| return transformMap.getDouble(key) | ||
| } | ||
| var stringValue = transformMap.getString(key) ?: return 0.0 | ||
| var inRadians = true | ||
| if (stringValue.endsWith("rad")) { | ||
| stringValue = stringValue.dropLast(3) | ||
| } else if (stringValue.endsWith("deg")) { | ||
| inRadians = false | ||
| stringValue = stringValue.dropLast(3) | ||
| } | ||
| val value = stringValue.toDouble() | ||
| return if (inRadians) value else MatrixMathHelper.degreesToRadians(value) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simplify to single call?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, pushed at 88e8967