Merge "Create ActivityClientRecord early in preExecute" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-05-27 03:50:20 +00:00
committed by Android (Google) Code Review
6 changed files with 84 additions and 26 deletions

View File

@@ -237,7 +237,6 @@ import java.util.Objects;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
@@ -338,6 +337,11 @@ public final class ActivityThread extends ClientTransactionHandler
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>(); final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
/**
* Maps from activity token to local record of the activities that are preparing to be launched.
*/
final Map<IBinder, ActivityClientRecord> mLaunchingActivities =
Collections.synchronizedMap(new ArrayMap<IBinder, ActivityClientRecord>());
/** The activities to be truly destroyed (not include relaunch). */ /** The activities to be truly destroyed (not include relaunch). */
final Map<IBinder, ClientTransactionItem> mActivitiesToBeDestroyed = final Map<IBinder, ClientTransactionItem> mActivitiesToBeDestroyed =
Collections.synchronizedMap(new ArrayMap<IBinder, ClientTransactionItem>()); Collections.synchronizedMap(new ArrayMap<IBinder, ClientTransactionItem>());
@@ -347,7 +351,6 @@ public final class ActivityThread extends ClientTransactionHandler
// Number of activities that are currently visible on-screen. // Number of activities that are currently visible on-screen.
@UnsupportedAppUsage @UnsupportedAppUsage
int mNumVisibleActivities = 0; int mNumVisibleActivities = 0;
private final AtomicInteger mNumLaunchingActivities = new AtomicInteger();
@GuardedBy("mAppThread") @GuardedBy("mAppThread")
private int mLastProcessState = PROCESS_STATE_UNKNOWN; private int mLastProcessState = PROCESS_STATE_UNKNOWN;
@GuardedBy("mAppThread") @GuardedBy("mAppThread")
@@ -3255,6 +3258,21 @@ public final class ActivityThread extends ClientTransactionHandler
return activityRecord != null ? activityRecord.activity : null; return activityRecord != null ? activityRecord.activity : null;
} }
@Override
public void addLaunchingActivity(IBinder token, ActivityClientRecord activity) {
mLaunchingActivities.put(token, activity);
}
@Override
public ActivityClientRecord getLaunchingActivity(IBinder token) {
return mLaunchingActivities.get(token);
}
@Override
public void removeLaunchingActivity(IBinder token) {
mLaunchingActivities.remove(token);
}
@Override @Override
public ActivityClientRecord getActivityClient(IBinder token) { public ActivityClientRecord getActivityClient(IBinder token) {
return mActivities.get(token); return mActivities.get(token);
@@ -3299,7 +3317,7 @@ public final class ActivityThread extends ClientTransactionHandler
// Defer the top state for VM to avoid aggressive JIT compilation affecting activity // Defer the top state for VM to avoid aggressive JIT compilation affecting activity
// launch time. // launch time.
if (processState == ActivityManager.PROCESS_STATE_TOP if (processState == ActivityManager.PROCESS_STATE_TOP
&& mNumLaunchingActivities.get() > 0) { && !mLaunchingActivities.isEmpty()) {
mPendingProcessState = processState; mPendingProcessState = processState;
mH.postDelayed(this::applyPendingProcessState, PENDING_TOP_PROCESS_STATE_TIMEOUT); mH.postDelayed(this::applyPendingProcessState, PENDING_TOP_PROCESS_STATE_TIMEOUT);
} else { } else {
@@ -3315,7 +3333,7 @@ public final class ActivityThread extends ClientTransactionHandler
// Handle the pending configuration if the process state is changed from cached to // Handle the pending configuration if the process state is changed from cached to
// non-cached. Except the case where there is a launching activity because the // non-cached. Except the case where there is a launching activity because the
// LaunchActivityItem will handle it. // LaunchActivityItem will handle it.
if (wasCached && !isCachedProcessState() && mNumLaunchingActivities.get() == 0) { if (wasCached && !isCachedProcessState() && mLaunchingActivities.isEmpty()) {
final Configuration pendingConfig = final Configuration pendingConfig =
mConfigurationController.getPendingConfiguration(false /* clearPending */); mConfigurationController.getPendingConfiguration(false /* clearPending */);
if (pendingConfig == null) { if (pendingConfig == null) {
@@ -3353,11 +3371,6 @@ public final class ActivityThread extends ClientTransactionHandler
} }
} }
@Override
public void countLaunchingActivities(int num) {
mNumLaunchingActivities.getAndAdd(num);
}
@UnsupportedAppUsage @UnsupportedAppUsage
public final void sendActivityResult( public final void sendActivityResult(
IBinder token, String id, int requestCode, IBinder token, String id, int requestCode,

View File

@@ -82,9 +82,6 @@ public abstract class ClientTransactionHandler {
/** Set current process state. */ /** Set current process state. */
public abstract void updateProcessState(int processState, boolean fromIpc); public abstract void updateProcessState(int processState, boolean fromIpc);
/** Count how many activities are launching. */
public abstract void countLaunchingActivities(int num);
// Execute phase related logic and handlers. Methods here execute actual lifecycle transactions // Execute phase related logic and handlers. Methods here execute actual lifecycle transactions
// and deliver callbacks. // and deliver callbacks.
@@ -192,6 +189,26 @@ public abstract class ClientTransactionHandler {
public abstract void handleFixedRotationAdjustments(IBinder token, public abstract void handleFixedRotationAdjustments(IBinder token,
FixedRotationAdjustments fixedRotationAdjustments); FixedRotationAdjustments fixedRotationAdjustments);
/**
* Add {@link ActivityClientRecord} that is preparing to be launched.
* @param token Activity token.
* @param activity An initialized instance of {@link ActivityClientRecord} to use during launch.
*/
public abstract void addLaunchingActivity(IBinder token, ActivityClientRecord activity);
/**
* Get {@link ActivityClientRecord} that is preparing to be launched.
* @param token Activity token.
* @return An initialized instance of {@link ActivityClientRecord} to use during launch.
*/
public abstract ActivityClientRecord getLaunchingActivity(IBinder token);
/**
* Remove {@link ActivityClientRecord} from the launching activity list.
* @param token Activity token.
*/
public abstract void removeLaunchingActivity(IBinder token);
/** /**
* Get {@link android.app.ActivityThread.ActivityClientRecord} instance that corresponds to the * Get {@link android.app.ActivityThread.ActivityClientRecord} instance that corresponds to the
* provided token. * provided token.

View File

@@ -40,7 +40,8 @@ public class ActivityConfigurationChangeItem extends ActivityTransactionItem {
@Override @Override
public void preExecute(android.app.ClientTransactionHandler client, IBinder token) { public void preExecute(android.app.ClientTransactionHandler client, IBinder token) {
final ActivityClientRecord r = getActivityClientRecord(client, token); final ActivityClientRecord r = getActivityClientRecord(client, token,
true /* includeLaunching */);
// Notify the client of an upcoming change in the token configuration. This ensures that // Notify the client of an upcoming change in the token configuration. This ensures that
// batches of config change items only process the newest configuration. // batches of config change items only process the newest configuration.
client.updatePendingActivityConfiguration(r, mConfiguration); client.updatePendingActivityConfiguration(r, mConfiguration);

View File

@@ -55,15 +55,40 @@ public abstract class ActivityTransactionItem extends ClientTransactionItem {
@NonNull ActivityClientRecord getActivityClientRecord( @NonNull ActivityClientRecord getActivityClientRecord(
@NonNull ClientTransactionHandler client, IBinder token) { @NonNull ClientTransactionHandler client, IBinder token) {
final ActivityClientRecord r = client.getActivityClient(token); return getActivityClientRecord(client, token, false /* includeLaunching */);
if (r == null) {
throw new IllegalArgumentException("Activity client record must not be null to execute "
+ "transaction item");
} }
/**
* Get the {@link ActivityClientRecord} instance that corresponds to the provided token.
* @param client Target client handler.
* @param token Target activity token.
* @param includeLaunching Indicate to also find the {@link ActivityClientRecord} in launching
* activity list. It should be noted that there is no activity in
* {@link ActivityClientRecord} from the launching activity list.
* @return The {@link ActivityClientRecord} instance that corresponds to the provided token.
*/
@NonNull ActivityClientRecord getActivityClientRecord(
@NonNull ClientTransactionHandler client, IBinder token, boolean includeLaunching) {
ActivityClientRecord r = client.getActivityClient(token);
if (r != null) {
if (client.getActivity(token) == null) { if (client.getActivity(token) == null) {
throw new IllegalArgumentException("Activity must not be null to execute " throw new IllegalArgumentException("Activity must not be null to execute "
+ "transaction item"); + "transaction item");
} }
return r; return r;
} }
// The activity may not be launched yet. Fallback to check launching activity.
if (includeLaunching) {
r = client.getLaunchingActivity(token);
}
if (r == null) {
throw new IllegalArgumentException("Activity client record must not be null to execute "
+ "transaction item");
}
// We don't need to check the activity of launching activity client records because they
// have not been launched yet.
return r;
}
} }

View File

@@ -82,7 +82,12 @@ public class LaunchActivityItem extends ClientTransactionItem {
@Override @Override
public void preExecute(ClientTransactionHandler client, IBinder token) { public void preExecute(ClientTransactionHandler client, IBinder token) {
client.countLaunchingActivities(1); ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo,
mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState,
mPendingResults, mPendingNewIntents, mActivityOptions, mIsForward, mProfilerInfo,
client, mAssistToken, mFixedRotationAdjustments, mShareableActivityToken,
mLaunchedFromBubble);
client.addLaunchingActivity(token, r);
client.updateProcessState(mProcState, false); client.updateProcessState(mProcState, false);
client.updatePendingConfiguration(mCurConfig); client.updatePendingConfiguration(mCurConfig);
if (mActivityClientController != null) { if (mActivityClientController != null) {
@@ -94,11 +99,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
public void execute(ClientTransactionHandler client, IBinder token, public void execute(ClientTransactionHandler client, IBinder token,
PendingTransactionActions pendingActions) { PendingTransactionActions pendingActions) {
Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo, ActivityClientRecord r = client.getLaunchingActivity(token);
mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState,
mPendingResults, mPendingNewIntents, mActivityOptions, mIsForward, mProfilerInfo,
client, mAssistToken, mFixedRotationAdjustments, mShareableActivityToken,
mLaunchedFromBubble);
client.handleLaunchActivity(r, pendingActions, null /* customIntent */); client.handleLaunchActivity(r, pendingActions, null /* customIntent */);
Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
} }
@@ -106,7 +107,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
@Override @Override
public void postExecute(ClientTransactionHandler client, IBinder token, public void postExecute(ClientTransactionHandler client, IBinder token,
PendingTransactionActions pendingActions) { PendingTransactionActions pendingActions) {
client.countLaunchingActivities(-1); client.removeLaunchingActivity(token);
} }

View File

@@ -40,7 +40,8 @@ public class MoveToDisplayItem extends ActivityTransactionItem {
@Override @Override
public void preExecute(ClientTransactionHandler client, IBinder token) { public void preExecute(ClientTransactionHandler client, IBinder token) {
final ActivityClientRecord r = getActivityClientRecord(client, token); final ActivityClientRecord r = getActivityClientRecord(client, token,
true /* includeLaunching */);
// Notify the client of an upcoming change in the token configuration. This ensures that // Notify the client of an upcoming change in the token configuration. This ensures that
// batches of config change items only process the newest configuration. // batches of config change items only process the newest configuration.
client.updatePendingActivityConfiguration(r, mConfiguration); client.updatePendingActivityConfiguration(r, mConfiguration);