From fd104e7fde7f53700da58cbab57d73b938b837a7 Mon Sep 17 00:00:00 2001 From: Jesse Hall Date: Thu, 19 Jan 2017 17:59:08 -0800 Subject: [PATCH] Load EGL early in Activity launch, instead of in Zygote Preloading EGL in Zygote was originally a memory footprint optimization, but it turns out to be an important app startup time optimization as well. Preloading EGL in Zygote is incompatible with updatable graphics drivers, but we don't want to do it on-demand as part of drawing the first frame either, since that increases first-frame latency unacceptably. This change removes Zygote preload, and instead loads EGL on a low-priority background thread immediately after choosing which graphics driver to use. This means it is usually done well before drawing the first frame, without significantly disrupting other activity launch work. Test: observe systrace of Calculator launch on bullhead Bug: 34404021 Change-Id: I6a0f6b90ade21848a10d51ddae62c936f70151b5 Merged-In: I887aa09bd35b088b16f53a89838a0c7c98f15761 --- core/java/android/os/GraphicsEnvironment.java | 18 ++++++++++++++++++ core/java/com/android/internal/os/ZygoteInit.java | 10 ---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/core/java/android/os/GraphicsEnvironment.java b/core/java/android/os/GraphicsEnvironment.java index 4b130ed1adc8..20de370fa344 100644 --- a/core/java/android/os/GraphicsEnvironment.java +++ b/core/java/android/os/GraphicsEnvironment.java @@ -19,6 +19,7 @@ package android.os; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.opengl.EGL14; import android.os.SystemProperties; import android.util.Log; @@ -34,6 +35,23 @@ public final class GraphicsEnvironment { private static final String PROPERTY_GFX_DRIVER = "ro.gfx.driver.0"; public static void setupGraphicsEnvironment(Context context) { + chooseDriver(context); + + // Now that we've figured out which driver to use for this process, load and initialize it. + // This can take multiple frame periods, and it would otherwise happen as part of the first + // frame, increasing first-frame latency. Starting it here, as a low-priority background + // thread, means that it's usually done long before we start drawing the first frame, + // without significantly disrupting other activity launch work. + Thread eglInitThread = new Thread( + () -> { + Thread.currentThread().setPriority(Thread.MIN_PRIORITY); + EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); + }, + "EGL Init"); + eglInitThread.start(); + } + + private static void chooseDriver(Context context) { String driverPackageName = SystemProperties.get(PROPERTY_GFX_DRIVER); if (driverPackageName == null || driverPackageName.isEmpty()) { return; diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java index 68299615b5b9..2a1a47651368 100644 --- a/core/java/com/android/internal/os/ZygoteInit.java +++ b/core/java/com/android/internal/os/ZygoteInit.java @@ -26,7 +26,6 @@ import android.icu.impl.CacheValue; import android.icu.text.DecimalFormatSymbols; import android.icu.util.ULocale; import android.net.LocalServerSocket; -import android.opengl.EGL14; import android.os.Process; import android.os.SystemClock; import android.os.SystemProperties; @@ -199,9 +198,6 @@ public class ZygoteInit { Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadResources"); preloadResources(); Trace.traceEnd(Trace.TRACE_TAG_DALVIK); - Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadOpenGL"); - preloadOpenGL(); - Trace.traceEnd(Trace.TRACE_TAG_DALVIK); preloadSharedLibraries(); preloadTextResources(); // Ask the WebViewFactory to do any initialization that must run in the zygote process, @@ -242,12 +238,6 @@ public class ZygoteInit { System.loadLibrary("jnigraphics"); } - private static void preloadOpenGL() { - if (!SystemProperties.getBoolean(PROPERTY_DISABLE_OPENGL_PRELOADING, false)) { - EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); - } - } - private static void preloadTextResources() { Hyphenator.init(); TextView.preloadFontCache(); -- 2.11.0