diff options
author | Romain Guy <romainguy@google.com> | 2013-04-17 18:54:38 -0700 |
---|---|---|
committer | Romain Guy <romainguy@google.com> | 2013-05-02 13:32:09 -0700 |
commit | 3b748a44c6bd2ea05fe16839caf73dbe50bd7ae9 (patch) | |
tree | ea104e727beba793c10952f5b328478020859599 /core/java/android/view/HardwareRenderer.java | |
parent | dd424cf079d2677e9333d89466f93166787e61c8 (diff) | |
download | frameworks_base-3b748a44c6bd2ea05fe16839caf73dbe50bd7ae9.zip frameworks_base-3b748a44c6bd2ea05fe16839caf73dbe50bd7ae9.tar.gz frameworks_base-3b748a44c6bd2ea05fe16839caf73dbe50bd7ae9.tar.bz2 |
Pack preloaded framework assets in a texture atlas
When the Android runtime starts, the system preloads a series of assets
in the Zygote process. These assets are shared across all processes.
Unfortunately, each one of these assets is later uploaded in its own
OpenGL texture, once per process. This wastes memory and generates
unnecessary OpenGL state changes.
This CL introduces an asset server that provides an atlas to all processes.
Note: bitmaps used by skia shaders are *not* sampled from the atlas.
It's an uncommon use case and would require extra texture transforms
in the GL shaders.
WHAT IS THE ASSETS ATLAS
The "assets atlas" is a single, shareable graphic buffer that contains
all the system's preloaded bitmap drawables (this includes 9-patches.)
The atlas is made of two distinct objects: the graphic buffer that
contains the actual pixels and the map which indicates where each
preloaded bitmap can be found in the atlas (essentially a pair of
x and y coordinates.)
HOW IS THE ASSETS ATLAS GENERATED
Because we need to support a wide variety of devices and because it
is easy to change the list of preloaded drawables, the atlas is
generated at runtime, during the startup phase of the system process.
There are several steps that lead to the atlas generation:
1. If the device is booting for the first time, or if the device was
updated, we need to find the best atlas configuration. To do so,
the atlas service tries a number of width, height and algorithm
variations that allows us to pack as many assets as possible while
using as little memory as possible. Once a best configuration is found,
it gets written to disk in /data/system/framework_atlas
2. Given a best configuration (algorithm variant, dimensions and
number of bitmaps that can be packed in the atlas), the atlas service
packs all the preloaded bitmaps into a single graphic buffer object.
3. The packing is done using Skia in a temporary native bitmap. The
Skia bitmap is then copied into the graphic buffer using OpenGL ES
to benefit from texture swizzling.
HOW PROCESSES USE THE ATLAS
Whenever a process' hardware renderer initializes its EGL context,
it queries the atlas service for the graphic buffer and the map.
It is important to remember that both the context and the map will
be valid for the lifetime of the hardware renderer (if the system
process goes down, all apps get killed as well.)
Every time the hardware renderer needs to render a bitmap, it first
checks whether the bitmap can be found in the assets atlas. When
the bitmap is part of the atlas, texture coordinates are remapped
appropriately before rendering.
Change-Id: I8eaecf53e7f6a33d90da3d0047c5ceec89ea3af0
Diffstat (limited to 'core/java/android/view/HardwareRenderer.java')
-rw-r--r-- | core/java/android/view/HardwareRenderer.java | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/core/java/android/view/HardwareRenderer.java b/core/java/android/view/HardwareRenderer.java index 8308459..0632e1d 100644 --- a/core/java/android/view/HardwareRenderer.java +++ b/core/java/android/view/HardwareRenderer.java @@ -24,7 +24,10 @@ import android.opengl.EGL14; import android.opengl.GLUtils; import android.opengl.ManagedEGLContext; import android.os.Handler; +import android.os.IBinder; import android.os.Looper; +import android.os.RemoteException; +import android.os.ServiceManager; import android.os.SystemClock; import android.os.SystemProperties; import android.os.Trace; @@ -968,7 +971,7 @@ public abstract class HardwareRenderer { if (fallback) { // we'll try again if it was context lost setRequested(false); - Log.w(LOG_TAG, "Mountain View, we've had a problem here. " + Log.w(LOG_TAG, "Mountain View, we've had a problem here. " + "Switching back to software rendering."); } } @@ -976,7 +979,7 @@ public abstract class HardwareRenderer { @Override boolean initialize(Surface surface) throws Surface.OutOfResourcesException { if (isRequested() && !isEnabled()) { - initializeEgl(); + boolean contextCreated = initializeEgl(); mGl = createEglSurface(surface); mDestroyed = false; @@ -991,6 +994,10 @@ public abstract class HardwareRenderer { mCanvas.setName(mName); } setEnabled(true); + + if (contextCreated) { + initAtlas(); + } } return mCanvas != null; @@ -1010,7 +1017,7 @@ public abstract class HardwareRenderer { abstract int[] getConfig(boolean dirtyRegions); - void initializeEgl() { + boolean initializeEgl() { synchronized (sEglLock) { if (sEgl == null && sEglConfig == null) { sEgl = (EGL10) EGLContext.getEGL(); @@ -1043,7 +1050,10 @@ public abstract class HardwareRenderer { if (mEglContext == null) { mEglContext = createContext(sEgl, sEglDisplay, sEglConfig); sEglContextStorage.set(createManagedContext(mEglContext)); + return true; } + + return false; } private EGLConfig loadEglConfig() { @@ -1181,6 +1191,7 @@ public abstract class HardwareRenderer { } abstract void initCaches(); + abstract void initAtlas(); EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) { int[] attribs = { EGL14.EGL_CONTEXT_CLIENT_VERSION, mGlVersion, EGL_NONE }; @@ -1193,6 +1204,7 @@ public abstract class HardwareRenderer { "Could not create an EGL context. eglCreateContext failed with error: " + GLUtils.getEGLErrorString(sEgl.eglGetError())); } + return context; } @@ -1788,7 +1800,27 @@ public abstract class HardwareRenderer { @Override void initCaches() { - GLES20Canvas.initCaches(); + if (GLES20Canvas.initCaches()) { + // Caches were (re)initialized, rebind atlas + initAtlas(); + } + } + + @Override + void initAtlas() { + IBinder binder = ServiceManager.getService("assetatlas"); + IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder); + try { + GraphicBuffer buffer = atlas.getBuffer(); + if (buffer != null) { + int[] map = atlas.getMap(); + if (map != null) { + GLES20Canvas.initAtlas(buffer, map); + } + } + } catch (RemoteException e) { + Log.w(LOG_TAG, "Could not acquire atlas", e); + } } @Override |