Merge "ContentSuggestions - restore snapshots when needed" into rvc-dev

This commit is contained in:
Zak Cohen
2020-03-24 23:56:42 +00:00
committed by Android (Google) Code Review
4 changed files with 40 additions and 27 deletions

View File

@@ -21,13 +21,17 @@ import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.contentsuggestions.ClassificationsRequest; import android.app.contentsuggestions.ClassificationsRequest;
import android.app.contentsuggestions.ContentSuggestionsManager;
import android.app.contentsuggestions.IClassificationsCallback; import android.app.contentsuggestions.IClassificationsCallback;
import android.app.contentsuggestions.IContentSuggestionsManager; import android.app.contentsuggestions.IContentSuggestionsManager;
import android.app.contentsuggestions.ISelectionsCallback; import android.app.contentsuggestions.ISelectionsCallback;
import android.app.contentsuggestions.SelectionsRequest; import android.app.contentsuggestions.SelectionsRequest;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.ColorSpace;
import android.graphics.GraphicBuffer;
import android.os.Binder; import android.os.Binder;
import android.os.Bundle; import android.os.Bundle;
import android.os.RemoteException; import android.os.RemoteException;
@@ -133,7 +137,7 @@ public class ContentSuggestionsManagerService extends
if (service != null) { if (service != null) {
// TODO(b/147324195): Temporarily pass bitmap until we change the service API. // TODO(b/147324195): Temporarily pass bitmap until we change the service API.
imageContextRequestExtras.putParcelable(EXTRA_BITMAP, bitmap); imageContextRequestExtras.putParcelable(EXTRA_BITMAP, bitmap);
service.provideContextImageLocked(/* taskId = */ -1, imageContextRequestExtras); service.provideContextImageFromBitmapLocked(imageContextRequestExtras);
} else { } else {
if (VERBOSE) { if (VERBOSE) {
Slog.v(TAG, "provideContextImageLocked: no service for " + userId); Slog.v(TAG, "provideContextImageLocked: no service for " + userId);
@@ -152,10 +156,28 @@ public class ContentSuggestionsManagerService extends
} }
enforceCaller(UserHandle.getCallingUserId(), "provideContextImage"); enforceCaller(UserHandle.getCallingUserId(), "provideContextImage");
GraphicBuffer snapshotBuffer = null;
int colorSpaceId = 0;
// Skip taking TaskSnapshot when bitmap is provided.
if (!imageContextRequestExtras.containsKey(ContentSuggestionsManager.EXTRA_BITMAP)) {
// Can block, so call before acquiring the lock.
ActivityManager.TaskSnapshot snapshot =
mActivityTaskManagerInternal.getTaskSnapshotBlocking(taskId, false);
if (snapshot != null) {
snapshotBuffer = snapshot.getSnapshot();
ColorSpace colorSpace = snapshot.getColorSpace();
if (colorSpace != null) {
colorSpaceId = colorSpace.getId();
}
}
}
synchronized (mLock) { synchronized (mLock) {
final ContentSuggestionsPerUserService service = getServiceForUserLocked(userId); final ContentSuggestionsPerUserService service = getServiceForUserLocked(userId);
if (service != null) { if (service != null) {
service.provideContextImageLocked(taskId, imageContextRequestExtras); service.provideContextImageLocked(taskId, snapshotBuffer, colorSpaceId,
imageContextRequestExtras);
} else { } else {
if (VERBOSE) { if (VERBOSE) {
Slog.v(TAG, "provideContextImageLocked: no service for " + userId); Slog.v(TAG, "provideContextImageLocked: no service for " + userId);

View File

@@ -19,17 +19,14 @@ package com.android.server.contentsuggestions;
import android.Manifest; import android.Manifest;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.AppGlobals; import android.app.AppGlobals;
import android.app.contentsuggestions.ClassificationsRequest; import android.app.contentsuggestions.ClassificationsRequest;
import android.app.contentsuggestions.ContentSuggestionsManager;
import android.app.contentsuggestions.IClassificationsCallback; import android.app.contentsuggestions.IClassificationsCallback;
import android.app.contentsuggestions.ISelectionsCallback; import android.app.contentsuggestions.ISelectionsCallback;
import android.app.contentsuggestions.SelectionsRequest; import android.app.contentsuggestions.SelectionsRequest;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
import android.graphics.ColorSpace;
import android.graphics.GraphicBuffer; import android.graphics.GraphicBuffer;
import android.os.Bundle; import android.os.Bundle;
import android.os.RemoteException; import android.os.RemoteException;
@@ -95,26 +92,17 @@ public final class ContentSuggestionsPerUserService extends
} }
@GuardedBy("mLock") @GuardedBy("mLock")
void provideContextImageLocked(int taskId, @NonNull Bundle imageContextRequestExtras) { void provideContextImageFromBitmapLocked(@NonNull Bundle bitmapContainingExtras) {
// No task or snapshot provided, the bitmap is contained in the extras
provideContextImageLocked(-1, null, 0, bitmapContainingExtras);
}
@GuardedBy("mLock")
void provideContextImageLocked(int taskId, @Nullable GraphicBuffer snapshot,
int colorSpaceIdForSnapshot, @NonNull Bundle imageContextRequestExtras) {
RemoteContentSuggestionsService service = ensureRemoteServiceLocked(); RemoteContentSuggestionsService service = ensureRemoteServiceLocked();
if (service != null) { if (service != null) {
GraphicBuffer snapshotBuffer = null; service.provideContextImage(taskId, snapshot, colorSpaceIdForSnapshot,
int colorSpaceId = 0;
// Skip taking TaskSnapshot when bitmap is provided.
if (!imageContextRequestExtras.containsKey(ContentSuggestionsManager.EXTRA_BITMAP)) {
ActivityManager.TaskSnapshot snapshot =
mActivityTaskManagerInternal.getTaskSnapshotNoRestore(taskId, false);
if (snapshot != null) {
snapshotBuffer = snapshot.getSnapshot();
ColorSpace colorSpace = snapshot.getColorSpace();
if (colorSpace != null) {
colorSpaceId = colorSpace.getId();
}
}
}
service.provideContextImage(taskId, snapshotBuffer, colorSpaceId,
imageContextRequestExtras); imageContextRequestExtras);
} }
} }

View File

@@ -548,8 +548,11 @@ public abstract class ActivityTaskManagerInternal {
/** /**
* Gets bitmap snapshot of the provided task id. * Gets bitmap snapshot of the provided task id.
*
* <p>Warning! this may restore the snapshot from disk so can block, don't call in a latency
* sensitive environment.
*/ */
public abstract ActivityManager.TaskSnapshot getTaskSnapshotNoRestore(int taskId, public abstract ActivityManager.TaskSnapshot getTaskSnapshotBlocking(int taskId,
boolean isLowResolution); boolean isLowResolution);
/** Returns true if uid is considered foreground for activity start purposes. */ /** Returns true if uid is considered foreground for activity start purposes. */

View File

@@ -7477,10 +7477,10 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
} }
@Override @Override
public ActivityManager.TaskSnapshot getTaskSnapshotNoRestore(int taskId, public ActivityManager.TaskSnapshot getTaskSnapshotBlocking(
boolean isLowResolution) { int taskId, boolean isLowResolution) {
return ActivityTaskManagerService.this.getTaskSnapshot(taskId, isLowResolution, return ActivityTaskManagerService.this.getTaskSnapshot(taskId, isLowResolution,
false /* restoreFromDisk */); true /* restoreFromDisk */);
} }
@Override @Override