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

Commit 964eafdc authored by Wei Sheng Shih's avatar Wei Sheng Shih Committed by Android (Google) Code Review
Browse files

Merge changes Id0cf1b23,I4df6891e,I759de2e1 into sc-dev

* changes:
  Add trace for tracking the performance of new starting window
  Force enable hardware accelerated for starting window
  Pre-draw AdaptiveIconDrawable and cache it before doFrame
parents 16a52c7b 2c9160c3
Loading
Loading
Loading
Loading
+1 −8
Original line number Diff line number Diff line
@@ -1370,17 +1370,10 @@ public final class ViewRootImpl implements ViewParent,
            // can be used by code on the system process to escape that and enable
            // HW accelerated drawing.  (This is basically for the lock screen.)

            final boolean fakeHwAccelerated = (attrs.privateFlags &
                    WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED) != 0;
            final boolean forceHwAccelerated = (attrs.privateFlags &
                    WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED) != 0;

            if (fakeHwAccelerated) {
                // This is exclusively for the preview windows the window manager
                // shows for launching applications, so they will look more like
                // the app being launched.
                mAttachInfo.mHardwareAccelerationRequested = true;
            } else if (ThreadedRenderer.sRendererEnabled || forceHwAccelerated) {
            if (ThreadedRenderer.sRendererEnabled || forceHwAccelerated) {
                if (mAttachInfo.mThreadedRenderer != null) {
                    mAttachInfo.mThreadedRenderer.destroy();
                }
+0 −25
Original line number Diff line number Diff line
@@ -2217,26 +2217,6 @@ public interface WindowManager extends ViewManager {
        @Flags
        public int flags;

        /**
         * If the window has requested hardware acceleration, but this is not
         * allowed in the process it is in, then still render it as if it is
         * hardware accelerated.  This is used for the starting preview windows
         * in the system process, which don't need to have the overhead of
         * hardware acceleration (they are just a static rendering), but should
         * be rendered as such to match the actual window of the app even if it
         * is hardware accelerated.
         * Even if the window isn't hardware accelerated, still do its rendering
         * as if it was.
         * Like {@link #FLAG_HARDWARE_ACCELERATED} except for trusted system windows
         * that need hardware acceleration (e.g. LockScreen), where hardware acceleration
         * is generally disabled. This flag must be specified in addition to
         * {@link #FLAG_HARDWARE_ACCELERATED} to enable hardware acceleration for system
         * windows.
         *
         * @hide
         */
        public static final int PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED = 0x00000001;

        /**
         * In the system process, we globally do not use hardware acceleration
         * because there are many threads doing UI there and they conflict.
@@ -2463,7 +2443,6 @@ public interface WindowManager extends ViewManager {
         * @hide
         */
        @IntDef(flag = true, prefix="PRIVATE_FLAG_", value = {
                PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED,
                PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED,
                PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS,
                SYSTEM_FLAG_SHOW_FOR_ALL_USERS,
@@ -2498,10 +2477,6 @@ public interface WindowManager extends ViewManager {
         */
        @UnsupportedAppUsage
        @ViewDebug.ExportedProperty(flagMapping = {
                @ViewDebug.FlagToString(
                        mask = PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED,
                        equals = PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED,
                        name = "FAKE_HARDWARE_ACCELERATED"),
                @ViewDebug.FlagToString(
                        mask = PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED,
                        equals = PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED,
+4 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package android.window;

import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;

import android.annotation.ColorInt;
@@ -32,6 +33,7 @@ import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.os.Trace;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
@@ -183,6 +185,7 @@ public final class SplashScreenView extends FrameLayout {
         * Create SplashScreenWindowView object from materials.
         */
        public SplashScreenView build() {
            Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "SplashScreenView#build");
            final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            final SplashScreenView view = (SplashScreenView)
                    layoutInflater.inflate(R.layout.splash_screen_view, null, false);
@@ -226,6 +229,7 @@ public final class SplashScreenView extends FrameLayout {
                        + view.mBrandingImageView + " drawable: " + mBrandingDrawable
                        + " size w: " + mBrandingImageWidth + " h: " + mBrandingImageHeight);
            }
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
            return view;
        }
    }
+80 −82
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.wm.shell.startingsurface;

import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;

import android.annotation.ColorInt;
import android.annotation.NonNull;
import android.app.ActivityThread;
@@ -31,6 +33,7 @@ import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.os.Trace;
import android.util.Slog;
import android.view.SurfaceControl;
import android.window.SplashScreenView;
@@ -70,6 +73,7 @@ public class SplashscreenContentDrawer {
    private int mIconNormalExitDistance;
    private int mIconEarlyExitDistance;
    private final TransactionPool mTransactionPool;
    private final SplashScreenWindowAttrs mTmpAttrs = new SplashScreenWindowAttrs();

    SplashscreenContentDrawer(Context context, int maxAnimatableIconDuration,
            int iconExitAnimDuration, int appRevealAnimDuration, TransactionPool pool) {
@@ -109,61 +113,62 @@ public class SplashscreenContentDrawer {
        return new ColorDrawable(getSystemBGColor());
    }

    SplashScreenView makeSplashScreenContentView(Context context, int iconRes,
            int splashscreenContentResId) {
        updateDensity();
        // splash screen content will be deprecated after S.
        final SplashScreenView ssc =
                makeSplashscreenContentDrawable(context, splashscreenContentResId);

        if (ssc != null) {
            return ssc;
        }

        final SplashScreenWindowAttrs attrs =
                SplashScreenWindowAttrs.createWindowAttrs(context);
        final StartingWindowViewBuilder builder = new StartingWindowViewBuilder();
    private @ColorInt int peekWindowBGColor(Context context) {
        Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "peekWindowBGColor");
        final Drawable themeBGDrawable;

        if (attrs.mWindowBgColor != 0) {
            themeBGDrawable = new ColorDrawable(attrs.mWindowBgColor);
        } else if (attrs.mWindowBgResId != 0) {
            themeBGDrawable = context.getDrawable(attrs.mWindowBgResId);
        if (mTmpAttrs.mWindowBgColor != 0) {
            themeBGDrawable = new ColorDrawable(mTmpAttrs.mWindowBgColor);
        } else if (mTmpAttrs.mWindowBgResId != 0) {
            themeBGDrawable = context.getDrawable(mTmpAttrs.mWindowBgResId);
        } else {
            Slog.w(TAG, "Window background not exist!");
            themeBGDrawable = createDefaultBackgroundDrawable();
        }
        final int estimatedWindowBGColor = estimateWindowBGColor(themeBGDrawable);
        Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
        return estimatedWindowBGColor;
    }

    private int estimateWindowBGColor(Drawable themeBGDrawable) {
        final DrawableColorTester themeBGTester =
                new DrawableColorTester(themeBGDrawable, true /* filterTransparent */);
        if (themeBGTester.nonTransparentRatio() == 0) {
            // the window background is transparent, unable to draw
            Slog.w(TAG, "Window background is transparent, fill background with black color");
            return getSystemBGColor();
        } else {
            return themeBGTester.getDominateColor();
        }
    }

    SplashScreenView makeSplashScreenContentView(Context context, int iconRes) {
        updateDensity();

        getWindowAttrs(context, mTmpAttrs);
        final StartingWindowViewBuilder builder = new StartingWindowViewBuilder();
        final int animationDuration;
        final Drawable iconDrawable;
        if (attrs.mReplaceIcon != null) {
            iconDrawable = attrs.mReplaceIcon;
        if (mTmpAttrs.mReplaceIcon != null) {
            iconDrawable = mTmpAttrs.mReplaceIcon;
            animationDuration = Math.max(0,
                    Math.min(attrs.mAnimationDuration, mMaxAnimatableIconDuration));
                    Math.min(mTmpAttrs.mAnimationDuration, mMaxAnimatableIconDuration));
        } else {
            iconDrawable = iconRes != 0 ? context.getDrawable(iconRes)
                    : context.getPackageManager().getDefaultActivityIcon();
            animationDuration = 0;
        }
        final int themeBGColor = peekWindowBGColor(context);
        // TODO (b/173975965) Tracking the performance on improved splash screen.
        return builder
                .setContext(context)
                .setThemeDrawable(themeBGDrawable)
                .setWindowBGColor(themeBGColor)
                .setIconDrawable(iconDrawable)
                .setIconAnimationDuration(animationDuration)
                .setBrandingDrawable(attrs.mBrandingImage)
                .setIconBackground(attrs.mIconBgColor).build();
                .setBrandingDrawable(mTmpAttrs.mBrandingImage)
                .setIconBackground(mTmpAttrs.mIconBgColor).build();
    }

    static class SplashScreenWindowAttrs {
        private int mWindowBgResId = 0;
        private int mWindowBgColor = Color.TRANSPARENT;
        private Drawable mReplaceIcon = null;
        private Drawable mBrandingImage = null;
        private int mIconBgColor = Color.TRANSPARENT;
        private int mAnimationDuration = 0;

        static SplashScreenWindowAttrs createWindowAttrs(Context context) {
            final SplashScreenWindowAttrs attrs = new SplashScreenWindowAttrs();
    private static void getWindowAttrs(Context context, SplashScreenWindowAttrs attrs) {
        final TypedArray typedArray = context.obtainStyledAttributes(
                com.android.internal.R.styleable.Window);
        attrs.mWindowBgResId = typedArray.getResourceId(R.styleable.Window_windowBackground, 0);
@@ -184,12 +189,18 @@ public class SplashscreenContentDrawer {
                    + " icon " + attrs.mReplaceIcon + " duration " + attrs.mAnimationDuration
                    + " brandImage " + attrs.mBrandingImage);
        }
            return attrs;
    }

    static class SplashScreenWindowAttrs {
        private int mWindowBgResId = 0;
        private int mWindowBgColor = Color.TRANSPARENT;
        private Drawable mReplaceIcon = null;
        private Drawable mBrandingImage = null;
        private int mIconBgColor = Color.TRANSPARENT;
        private int mAnimationDuration = 0;
    }

    private class StartingWindowViewBuilder {
        private Drawable mThemeBGDrawable;
        private Drawable mIconDrawable;
        private int mIconAnimationDuration;
        private Context mContext;
@@ -203,8 +214,8 @@ public class SplashscreenContentDrawer {
        private Drawable mFinalIconDrawable;
        private float mScale = 1f;

        StartingWindowViewBuilder setThemeDrawable(Drawable background) {
            mThemeBGDrawable = background;
        StartingWindowViewBuilder setWindowBGColor(@ColorInt int background) {
            mThemeColor = background;
            mBuildComplete = false;
            return this;
        }
@@ -247,18 +258,14 @@ public class SplashscreenContentDrawer {
                Slog.e(TAG, "Unable to create StartingWindowView, lack of materials!");
                return null;
            }
            if (mThemeBGDrawable == null) {
                Slog.w(TAG, "Theme Background Drawable is null, forget to set Theme Drawable?");
                mThemeBGDrawable = createDefaultBackgroundDrawable();
            }
            processThemeColor();

            if (!processAdaptiveIcon() && mIconDrawable != null) {
                if (DEBUG) {
                    Slog.d(TAG, "The icon is not an AdaptiveIconDrawable");
                }
                mFinalIconDrawable = SplashscreenIconDrawableFactory.makeIconDrawable(
                        mIconBackground != Color.TRANSPARENT
                        ? mIconBackground : mThemeColor, mIconDrawable);
                        ? mIconBackground : mThemeColor, mIconDrawable, mIconSize);
            }
            final int iconSize = mFinalIconDrawable != null ? (int) (mIconSize * mScale) : 0;
            mCachedResult = fillViewWithIcon(mContext, iconSize, mFinalIconDrawable);
@@ -266,22 +273,10 @@ public class SplashscreenContentDrawer {
            return mCachedResult;
        }

        private void createIconDrawable(Drawable iconDrawable) {
        private void createIconDrawable(Drawable iconDrawable, int iconSize) {
            mFinalIconDrawable = SplashscreenIconDrawableFactory.makeIconDrawable(
                    mIconBackground != Color.TRANSPARENT
                    ? mIconBackground : mThemeColor, iconDrawable);
        }

        private void processThemeColor() {
            final DrawableColorTester themeBGTester =
                    new DrawableColorTester(mThemeBGDrawable, true /* filterTransparent */);
            if (themeBGTester.nonTransparentRatio() == 0) {
                // the window background is transparent, unable to draw
                Slog.w(TAG, "Window background is transparent, fill background with black color");
                mThemeColor = getSystemBGColor();
            } else {
                mThemeColor = themeBGTester.getDominateColor();
            }
                    ? mIconBackground : mThemeColor, iconDrawable, iconSize);
        }

        private boolean processAdaptiveIcon() {
@@ -289,6 +284,7 @@ public class SplashscreenContentDrawer {
                return false;
            }

            Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "processAdaptiveIcon");
            final AdaptiveIconDrawable adaptiveIconDrawable = (AdaptiveIconDrawable) mIconDrawable;
            final DrawableColorTester backIconTester =
                    new DrawableColorTester(adaptiveIconDrawable.getBackground());
@@ -325,29 +321,28 @@ public class SplashscreenContentDrawer {
                if (DEBUG) {
                    Slog.d(TAG, "makeSplashScreenContentView: choose fg icon");
                }
                // Using AdaptiveIconDrawable here can help keep the shape consistent with the
                // current settings.
                createIconDrawable(iconForeground);
                // Reference AdaptiveIcon description, outer is 108 and inner is 72, so we
                // should enlarge the size 108/72 if we only draw adaptiveIcon's foreground.
                if (foreIconTester.nonTransparentRatio() < ENLARGE_FOREGROUND_ICON_THRESHOLD) {
                    mScale = 1.5f;
                }
                // Using AdaptiveIconDrawable here can help keep the shape consistent with the
                // current settings.
                final int iconSize = (int) (0.5f + mIconSize * mScale);
                createIconDrawable(iconForeground, iconSize);
            } else {
                if (DEBUG) {
                    Slog.d(TAG, "makeSplashScreenContentView: draw whole icon");
                }
                if (mIconBackground != Color.TRANSPARENT) {
                    createIconDrawable(adaptiveIconDrawable);
                } else {
                    mFinalIconDrawable = adaptiveIconDrawable;
                }
                createIconDrawable(adaptiveIconDrawable, mIconSize);
            }
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
            return true;
        }

        private SplashScreenView fillViewWithIcon(Context context,
                int iconSize, Drawable iconDrawable) {
            Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "fillViewWithIcon");
            final SplashScreenView.Builder builder = new SplashScreenView.Builder(context);
            builder.setIconSize(iconSize).setBackgroundColor(mThemeColor)
                    .setIconBackground(mIconBackground);
@@ -364,6 +359,7 @@ public class SplashscreenContentDrawer {
                Slog.d(TAG, "fillViewWithIcon surfaceWindowView " + splashScreenView);
            }
            splashScreenView.makeSystemUIColorsTransparent();
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
            return splashScreenView;
        }
    }
@@ -395,7 +391,7 @@ public class SplashscreenContentDrawer {
        return root < 0.1;
    }

    private static SplashScreenView makeSplashscreenContentDrawable(Context ctx,
    static SplashScreenView makeSplashscreenContent(Context ctx,
            int splashscreenContentResId) {
        // doesn't support windowSplashscreenContent after S
        // TODO add an allowlist to skip some packages if needed
@@ -525,6 +521,7 @@ public class SplashscreenContentDrawer {
                    new TransparentFilterQuantizer();

            ComplexDrawableTester(Drawable drawable, boolean filterTransparent) {
                Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "ComplexDrawableTester");
                final Rect initialBounds = drawable.copyBounds();
                int width = drawable.getIntrinsicWidth();
                int height = drawable.getIntrinsicHeight();
@@ -559,6 +556,7 @@ public class SplashscreenContentDrawer {
                }
                mPalette = builder.generate();
                bitmap.recycle();
                Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
            }

            @Override
+54 −3
Original line number Diff line number Diff line
@@ -16,11 +16,15 @@

package com.android.wm.shell.startingsurface;

import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.ColorInt;
import android.annotation.NonNull;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
@@ -28,11 +32,13 @@ import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.AdaptiveIconDrawable;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.os.Trace;
import android.util.PathParser;
import android.window.SplashScreenView;

@@ -47,12 +53,57 @@ import java.util.function.Consumer;
public class SplashscreenIconDrawableFactory {

    static Drawable makeIconDrawable(@ColorInt int backgroundColor,
            @NonNull Drawable foregroundDrawable) {
            @NonNull Drawable foregroundDrawable, int iconSize) {
        if (foregroundDrawable instanceof Animatable) {
            return new AnimatableIconDrawable(backgroundColor, foregroundDrawable);
        } else if (foregroundDrawable instanceof AdaptiveIconDrawable) {
            return new ImmobileIconDrawable((AdaptiveIconDrawable) foregroundDrawable, iconSize);
        } else {
            // TODO make a light weight drawable instead of AdaptiveIconDrawable
            return new AdaptiveIconDrawable(new ColorDrawable(backgroundColor), foregroundDrawable);
            return new ImmobileIconDrawable(new AdaptiveIconDrawable(
                    new ColorDrawable(backgroundColor), foregroundDrawable), iconSize);
        }
    }

    private static class ImmobileIconDrawable extends Drawable {
        private Shader mLayersShader;
        private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG
                | Paint.FILTER_BITMAP_FLAG);

        ImmobileIconDrawable(AdaptiveIconDrawable drawable, int iconSize) {
            cachePaint(drawable, iconSize, iconSize);
        }

        private void cachePaint(AdaptiveIconDrawable drawable, int width, int height) {
            Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "cachePaint");
            final Bitmap layersBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            final Canvas canvas = new Canvas(layersBitmap);
            drawable.setBounds(0, 0, width, height);
            drawable.draw(canvas);
            mLayersShader = new BitmapShader(layersBitmap, Shader.TileMode.CLAMP,
                    Shader.TileMode.CLAMP);
            mPaint.setShader(mLayersShader);
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
        }

        @Override
        public void draw(Canvas canvas) {
            final Rect bounds = getBounds();
            canvas.drawRect(bounds, mPaint);
        }

        @Override
        public void setAlpha(int alpha) {

        }

        @Override
        public void setColorFilter(ColorFilter colorFilter) {

        }

        @Override
        public int getOpacity() {
            return 1;
        }
    }

Loading