Skip to content
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

feat: Use animation matrix For Skew prop #38494

Closed
wants to merge 3 commits into from

Conversation

xxrlzzz
Copy link

@xxrlzzz xxrlzzz commented Jul 18, 2023

Summary:

This PR intent to solve Skew property on Android: #27649.

After android Q, we can use setAnimationMatrix to implement css Transform. (This api can also be accessed before android Q)

Old method for implement Transform is maintain a local 4*4 matrix, after finish handle all properties decompose back to a 3*3 matrix, and set back to android view use api setTranslation, setRotation, setScale. There is no such a api to direct set skew property. But the native layer of android is using a 3*3 skia matrix to handle all those transform properties.
So we could handle those Transform on our side and call setAnimationMatrix directly.

There is also some shortage for this fix, a 3*3 matrix is unable to handle 3d rotation and perspective. We will fail back to old implementation when those property is used.

Note this pr may have a conflict with #37606.

Changelog:

[ANDROID] [Fixed] - Implement of css Transform property.

Test Plan:

RNTester App: Apis -> Animated -> Transform Styles.

@facebook-github-bot
Copy link
Contributor

Hi @xxrlzzz!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@xxrlzzz
Copy link
Author

xxrlzzz commented Jul 18, 2023

Original Effect:

original.mp4

Fixed Effect:

fixed.mp4

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 18, 2023
@analysis-bot
Copy link

analysis-bot commented Jul 18, 2023

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 16,517,138 -1,364,411
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 19,887,736 -1,352,295
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: 8ff05b5
Branch: main

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jul 18, 2023
}
}

for (int transformIdx = 0, size = transforms.size(); transformIdx < size; transformIdx++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to process all transforms in order, you can't have separate loops for translate and other properties, as it may cause the resulting matrix to be incorrect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't sure disorder of properties will leds a incorrect result matrix, since every property are handled by matrix multiply rather direct set matrix element.
The reason for separate translate and other properties is we need to know the real center of the view to set origin of rotate and scale (default origin is the top-left point of the view, rather center of the view like it used to be)

@xxrlzzz
Copy link
Author

xxrlzzz commented Jul 26, 2023

I note there is already has setAnimationMatrix calls in BaseViewManager. This api has marked public after android api 21. Its normally free to call it with @SuppressLint("NewApi").

We can use implementation form AdnroidX transition library to access this api saftly.

@xxrlzzz xxrlzzz changed the title feat: Use animation matrix after android Q For Skew prop feat: Use animation matrix For Skew prop Jul 26, 2023
@xxrlzzz
Copy link
Author

xxrlzzz commented Jul 26, 2023

Compatibility test (device with api before android Q)

2023-07-26.19.38.56.mov
2023-07-26.19.18.00.mov

@xxrlzzz xxrlzzz marked this pull request as draft July 31, 2023 02:34
@xxrlzzz
Copy link
Author

xxrlzzz commented Jul 31, 2023

This pr is current depend on #38558 to solve unable to get view width, height problem

@DieTime
Copy link

DieTime commented Nov 24, 2023

@xxrlzzz Is this PR still relevant? As I understand it, the PR on which this one depends was merged.

@xxrlzzz
Copy link
Author

xxrlzzz commented Nov 28, 2023

@xxrlzzz Is this PR still relevant? As I understand it, the PR on which this one depends was merged.

ok, I will continue this PR ASAP.

@xxrlzzz xxrlzzz marked this pull request as ready for review December 2, 2023 07:29
@xxrlzzz
Copy link
Author

xxrlzzz commented Dec 2, 2023

without transform-origin
https://github.com/facebook/react-native/assets/40893153/c6fc91f9-a7a2-4693-9cfe-41650f9e3696

with transform-origin
https://github.com/facebook/react-native/assets/40893153/8bbe1701-e736-4abe-b8d6-11b77785671e

Copy link
Member

@javache javache left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Sorry for the delay on reviewing this, we should try to get this imported.

Comment on lines 176 to 180
float[] offsets = getTranslateForTransformOrigin(viewWidth, viewHeight, transformOrigin);
float originX = viewWidth / 2, originY = viewHeight / 2;
if (offsets != null) {
originX += offsets[0];
originY += offsets[1];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should account for this by doing a transform as a first step and then the reverse transform as the last step, similar to processTransform

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@javache
Copy link
Member

javache commented Apr 15, 2024

While the implementation seems right here, I'm a bit concerned about adopting this given the Android SDK docs

This is used in animation frameworks, such as Transition. When the animation finishes, the matrix should be cleared by calling this method with null as the matrix parameter. Application developers should use transformation methods like setRotation(float), setScaleX(float), setScaleX(float), setTranslationX(float)} and setTranslationY(float) (float)}} instead.

There's also significant complexity being introduced here in terms of what code paths can potentially triggered. Adding a different transform property to the array can significantly change how the transform is set.

@xxrlzzz xxrlzzz closed this Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants