From f64a89759fcec07a0815a340368003ca8e6cca27 Mon Sep 17 00:00:00 2001 From: Shane Date: Thu, 16 Nov 2023 19:02:35 +0000 Subject: Use applyAsyncUnsafe() instead of apply() to apply a transaction As suggested, applyAsyncUnsafe() can be more efficient than apply() when apply a transaction. This CL makes the corresponding changes to setFrameRateCateogry and setFrameRate call Also, I reduce the frequency of the API calls by comparing the previous frame rate (category) with the current one. We only make an API call when they are different. Bug: 310966165 Test: Manual Test, atest ViewRootImplTest Change-Id: I3f0752da8d088edff4f19725dabd75bd6d11a28f --- core/java/android/view/ViewRootImpl.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index f98e1dd41b1b..5ff028c51ce2 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -3991,9 +3991,7 @@ public final class ViewRootImpl implements ViewParent, // when the values are applicable. setPreferredFrameRate(mPreferredFrameRate); setPreferredFrameRateCategory(mPreferredFrameRateCategory); - mLastPreferredFrameRateCategory = mPreferredFrameRateCategory; mPreferredFrameRateCategory = FRAME_RATE_CATEGORY_NO_PREFERENCE; - mLastPreferredFrameRate = mPreferredFrameRate; mPreferredFrameRate = 0; } @@ -11982,8 +11980,11 @@ public final class ViewRootImpl implements ViewParent, ? FRAME_RATE_CATEGORY_HIGH : preferredFrameRateCategory; try { - mFrameRateTransaction.setFrameRateCategory(mSurfaceControl, - frameRateCategory, false).apply(); + if (mLastPreferredFrameRateCategory != frameRateCategory) { + mFrameRateTransaction.setFrameRateCategory(mSurfaceControl, + frameRateCategory, false).applyAsyncUnsafe(); + mLastPreferredFrameRateCategory = frameRateCategory; + } } catch (Exception e) { Log.e(mTag, "Unable to set frame rate category", e); } @@ -12003,8 +12004,11 @@ public final class ViewRootImpl implements ViewParent, } try { - mFrameRateTransaction.setFrameRate(mSurfaceControl, - preferredFrameRate, Surface.FRAME_RATE_COMPATIBILITY_DEFAULT).apply(); + if (mLastPreferredFrameRate != preferredFrameRate) { + mFrameRateTransaction.setFrameRate(mSurfaceControl, preferredFrameRate, + Surface.FRAME_RATE_COMPATIBILITY_DEFAULT).applyAsyncUnsafe(); + mLastPreferredFrameRate = preferredFrameRate; + } } catch (Exception e) { Log.e(mTag, "Unable to set frame rate", e); } -- cgit v1.2.3-59-g8ed1b