Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6f9dbcb7 authored by Jorim Jaggi's avatar Jorim Jaggi
Browse files

Implement new thumbnail loading strategy

- By default, we load only the reduced resolution screenshots.
- As soon as the user stops scrolling fast, we also start loading
full resolution screenshots.
- We prefetch reduced resolution screenshots when scrolling from
back to front, as the other direction is automatically prefetched
because the thumbnails aren't immediately visible.

Test: Open many apps, adb restart, scroll fast and slow in recents
Test: runtest systemui -c com.android.systemui.recents.model.HighResThumbnailLoaderTest
Bug: 34829962
Change-Id: I7f7a9842eb28a09a18573426fa9677cee2877124
parent 35e3f53a
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -726,6 +726,10 @@
    <!-- The alpha to apply to the recents row when it doesn't have focus -->
    <item name="recents_recents_row_dim_alpha" format="float" type="dimen">0.5</item>

    <!-- The speed in dp/s at which the user needs to be scrolling in recents such that we start
         loading full resolution screenshots. -->
    <dimen name="recents_fast_fling_velocity">600dp</dimen>

    <!-- The size of the PIP drag-to-dismiss target. -->
    <dimen name="pip_dismiss_target_size">48dp</dimen>

+3 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.os.UserHandle;
@@ -59,6 +60,7 @@ import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
import com.android.systemui.recents.events.component.ShowUserToastEvent;
import com.android.systemui.recents.events.ui.RecentsDrawnEvent;
import com.android.systemui.recents.misc.SystemServicesProxy;
import com.android.systemui.recents.model.HighResThumbnailLoader;
import com.android.systemui.recents.model.RecentsTaskLoader;
import com.android.systemui.stackdivider.Divider;
import com.android.systemui.statusbar.CommandQueue;
@@ -184,6 +186,7 @@ public class Recents extends SystemUI
        return sTaskLoader;
    }


    public static SystemServicesProxy getSystemServices() {
        return sSystemServicesProxy;
    }
+3 −0
Original line number Diff line number Diff line
@@ -375,6 +375,8 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD

        // Notify of the next draw
        mRecentsView.getViewTreeObserver().addOnPreDrawListener(mRecentsDrawnEventListener);

        Recents.getTaskLoader().getHighResThumbnailLoader().setVisible(true);
    }

    @Override
@@ -529,6 +531,7 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD
        mReceivedNewIntent = false;
        EventBus.getDefault().send(new RecentsVisibilityChangedEvent(this, false));
        MetricsLogger.hidden(this, MetricsEvent.OVERVIEW_ACTIVITY);
        Recents.getTaskLoader().getHighResThumbnailLoader().setVisible(false);

        if (!isChangingConfigurations()) {
            // Workaround for b/22542869, if the RecentsActivity is started again, but without going
+3 −3
Original line number Diff line number Diff line
@@ -637,7 +637,7 @@ public class SystemServicesProxy {
    }

    /** Returns the top task thumbnail for the given task id */
    public ThumbnailData getTaskThumbnail(int taskId) {
    public ThumbnailData getTaskThumbnail(int taskId, boolean reduced) {
        if (mAm == null) return null;

        // If we are mocking, then just return a dummy thumbnail
@@ -649,7 +649,7 @@ public class SystemServicesProxy {
            return thumbnailData;
        }

        ThumbnailData thumbnailData = getThumbnail(taskId);
        ThumbnailData thumbnailData = getThumbnail(taskId, reduced);
        if (thumbnailData.thumbnail != null && !ActivityManager.ENABLE_TASK_SNAPSHOTS) {
            thumbnailData.thumbnail.setHasAlpha(false);
            // We use a dumb heuristic for now, if the thumbnail is purely transparent in the top
@@ -669,7 +669,7 @@ public class SystemServicesProxy {
    /**
     * Returns a task thumbnail from the activity manager
     */
    public @NonNull ThumbnailData getThumbnail(int taskId) {
    public @NonNull ThumbnailData getThumbnail(int taskId, boolean reducedResolution) {
        if (mAm == null) {
            return new ThumbnailData();
        }
+215 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.systemui.recents.model;

import static android.os.Process.setThreadPriority;

import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.ArraySet;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.recents.misc.SystemServicesProxy;
import com.android.systemui.recents.model.Task.TaskCallbacks;

import java.util.ArrayDeque;
import java.util.ArrayList;

/**
 * Loader class that loads full-resolution thumbnails when appropriate.
 */
public class HighResThumbnailLoader implements TaskCallbacks {

    @GuardedBy("mLoadQueue")
    private final ArrayDeque<Task> mLoadQueue = new ArrayDeque<>();
    @GuardedBy("mLoadQueue")
    private final ArraySet<Task> mLoadingTasks = new ArraySet<>();
    @GuardedBy("mLoadQueue")
    private boolean mLoaderIdling;

    private final ArrayList<Task> mVisibleTasks = new ArrayList<>();
    private final Thread mLoadThread;
    private final Handler mMainThreadHandler;
    private final SystemServicesProxy mSystemServicesProxy;
    private boolean mLoading;
    private boolean mVisible;
    private boolean mFlingingFast;

    public HighResThumbnailLoader(SystemServicesProxy ssp, Looper looper) {
        mMainThreadHandler = new Handler(looper);
        mLoadThread = new Thread(mLoader, "Recents-HighResThumbnailLoader");
        mLoadThread.start();
        mSystemServicesProxy = ssp;
    }

    public void setVisible(boolean visible) {
        mVisible = visible;
        updateLoading();
    }

    public void setFlingingFast(boolean flingingFast) {
        if (mFlingingFast == flingingFast) {
            return;
        }
        mFlingingFast = flingingFast;
        updateLoading();
    }

    @VisibleForTesting
    boolean isLoading() {
        return mLoading;
    }

    private void updateLoading() {
        setLoading(mVisible && !mFlingingFast);
    }

    private void setLoading(boolean loading) {
        if (loading == mLoading) {
            return;
        }
        synchronized (mLoadQueue) {
            mLoading = loading;
            if (!loading) {
                stopLoading();
            } else {
                startLoading();
            }
        }
    }

    @GuardedBy("mLoadQueue")
    private void startLoading() {
        for (int i = mVisibleTasks.size() - 1; i >= 0; i--) {
            Task t = mVisibleTasks.get(i);
            if ((t.thumbnail == null || t.thumbnail.reducedResolution)
                    && !mLoadQueue.contains(t) && !mLoadingTasks.contains(t)) {
                mLoadQueue.add(t);
            }
        }
        mLoadQueue.notifyAll();
    }

    @GuardedBy("mLoadQueue")
    private void stopLoading() {
        mLoadQueue.clear();
        mLoadQueue.notifyAll();
    }

    /**
     * Needs to be called when a task becomes visible. Note that this is different from
     * {@link TaskCallbacks#onTaskDataLoaded} as this method should only be called once when it
     * becomes visible, whereas onTaskDataLoaded can be called multiple times whenever some data
     * has been updated.
     */
    public void onTaskVisible(Task t) {
        t.addCallback(this);
        mVisibleTasks.add(t);
        if ((t.thumbnail == null || t.thumbnail.reducedResolution) && mLoading) {
            synchronized (mLoadQueue) {
                mLoadQueue.add(t);
                mLoadQueue.notifyAll();
            }
        }
    }

    /**
     * Needs to be called when a task becomes visible. See {@link #onTaskVisible} why this is
     * different from {@link TaskCallbacks#onTaskDataUnloaded()}
     */
    public void onTaskInvisible(Task t) {
        t.removeCallback(this);
        mVisibleTasks.remove(t);
        synchronized (mLoadQueue) {
            mLoadQueue.remove(t);
        }
    }

    @VisibleForTesting
    void waitForLoaderIdle() {
        while (true) {
            synchronized (mLoadQueue) {
                if (mLoadQueue.isEmpty() && mLoaderIdling) {
                    return;
                }
            }
            SystemClock.sleep(100);
        }
    }

    @Override
    public void onTaskDataLoaded(Task task, ThumbnailData thumbnailData) {
        if (thumbnailData != null && !thumbnailData.reducedResolution) {
            synchronized (mLoadQueue) {
                mLoadQueue.remove(task);
            }
        }
    }

    @Override
    public void onTaskDataUnloaded() {
    }

    @Override
    public void onTaskStackIdChanged() {
    }

    private final Runnable mLoader = new Runnable() {

        @Override
        public void run() {
            setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND + 1);
            while (true) {
                Task next = null;
                synchronized (mLoadQueue) {
                    if (!mLoading || mLoadQueue.isEmpty()) {
                        try {
                            mLoaderIdling = true;
                            mLoadQueue.wait();
                            mLoaderIdling = false;
                        } catch (InterruptedException e) {
                            // Don't care.
                        }
                    } else {
                        next = mLoadQueue.poll();
                        if (next != null) {
                            mLoadingTasks.add(next);
                        }
                    }
                }
                if (next != null) {
                    loadTask(next);
                }
            }
        }

        private void loadTask(Task t) {
            ThumbnailData thumbnail = mSystemServicesProxy.getTaskThumbnail(t.key.id,
                    false /* reducedResolution */);
            mMainThreadHandler.post(() -> {
                synchronized (mLoadQueue) {
                    mLoadingTasks.remove(t);
                }
                if (mVisibleTasks.contains(t)) {
                    t.notifyTaskDataLoaded(thumbnail, t.icon);
                }
            });
        }
    };
}
Loading