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

Commit bdf76098 authored by Romain Guy's avatar Romain Guy
Browse files

Trim OpenGLRenderer's memory usage whenever possible

Change-Id: I9225077184f374b1a43300add15cc1d5b6869d1c
parent d94b71de
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3558,6 +3558,7 @@ public final class ActivityThread {
    }

    final void handleTrimMemory(int level) {
        WindowManagerImpl.getDefault().trimMemory(level);
    }

    private final void handleBindApplication(AppBindData data) {
+32 −0
Original line number Diff line number Diff line
@@ -286,6 +286,38 @@ class GLES20Canvas extends HardwareCanvas {

    private static native boolean nCallDrawGLFunction(int renderer, int drawGLFunction);


    ///////////////////////////////////////////////////////////////////////////
    // Memory
    ///////////////////////////////////////////////////////////////////////////

    /**
     * @see #flushCaches(int) 
     */
    public static final int FLUSH_CACHES_MODERATE = 0;

    /**
     * @see #flushCaches(int) 
     */
    public static final int FLUSH_CACHES_FULL = 1;

    /**
     * Flush caches to reclaim as much memory as possible. The amount of memory
     * to reclaim is indicate by the level parameter.
     * 
     * The level can be one of {@link #FLUSH_CACHES_MODERATE} or
     * {@link #FLUSH_CACHES_FULL}.
     * 
     * @param level Hint about the amount of memory to reclaim
     * 
     * @hide
     */
    public static void flushCaches(int level) {
        nFlushCaches(level);
    }

    private static native void nFlushCaches(int level);

    ///////////////////////////////////////////////////////////////////////////
    // Display list
    ///////////////////////////////////////////////////////////////////////////
+24 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package android.view;

import android.content.ComponentCallbacks;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
@@ -262,6 +263,18 @@ public abstract class HardwareRenderer {
        throw new IllegalArgumentException("Unknown GL version: " + glVersion);
    }

    /**
     * Invoke this method when the system is running out of memory. This
     * method will attempt to recover as much memory as possible, based on
     * the specified hint.
     * 
     * @param level Hint about the amount of memory that should be trimmed,
     *              see {@link android.content.ComponentCallbacks}
     */
    static void trimMemory(int level) {
        Gl20Renderer.flushCaches(level);
    }

    /**
     * Indicates whether hardware acceleration is currently enabled.
     * 
@@ -858,5 +871,16 @@ public abstract class HardwareRenderer {
            }
            return null;
        }
        
        static void flushCaches(int level) {
            switch (level) {
                case ComponentCallbacks.TRIM_MEMORY_MODERATE:
                    GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_MODERATE);
                    break;
                case ComponentCallbacks.TRIM_MEMORY_COMPLETE:
                    GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_FULL);
                    break;
            }
        }
    }
}
+14 −9
Original line number Diff line number Diff line
@@ -16,18 +16,16 @@

package android.view;

import java.util.HashMap;

import android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.AndroidRuntimeException;
import android.util.Log;
import android.util.Slog;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import java.util.HashMap;

final class WindowLeaked extends AndroidRuntimeException {
    public WindowLeaked(String msg) {
        super(msg);
@@ -403,6 +401,15 @@ public class WindowManagerImpl implements WindowManager {
        }
    }

    /**
     * @param level See {@link android.content.ComponentCallbacks}
     */
    public void trimMemory(int level) {
        if (HardwareRenderer.isAvailable()) {
            HardwareRenderer.trimMemory(level);
        }
    }

    public void setStoppedState(IBinder token, boolean stopped) {
        synchronized (this) {
            if (mViews == null)
@@ -456,8 +463,7 @@ public class WindowManagerImpl implements WindowManager {
        return new Display(Display.DEFAULT_DISPLAY, null);
    }

    private static void removeItem(Object[] dst, Object[] src, int index)
    {
    private static void removeItem(Object[] dst, Object[] src, int index) {
        if (dst.length > 0) {
            if (index > 0) {
                System.arraycopy(src, 0, dst, 0, index);
@@ -468,8 +474,7 @@ public class WindowManagerImpl implements WindowManager {
        }
    }

    private int findViewLocked(View view, boolean required)
    {
    private int findViewLocked(View view, boolean required) {
        synchronized (this) {
            final int count = mViews != null ? mViews.length : 0;
            for (int i=0; i<count; i++) {
+8 −0
Original line number Diff line number Diff line
@@ -127,6 +127,13 @@ static void android_view_GLES20Canvas_disableVsync(JNIEnv* env, jobject clazz) {
    }
}

static void android_view_GLES20Canvas_flushCaches(JNIEnv* env, jobject clazz,
        Caches::FlushMode mode) {
    if (Caches::hasInstance()) {
        Caches::getInstance().flush(mode);
    }
}

// ----------------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------------
@@ -735,6 +742,7 @@ static JNINativeMethod gMethods[] = {
    { "nIsBackBufferPreserved", "()Z",         (void*) android_view_GLES20Canvas_isBackBufferPreserved },
    { "nPreserveBackBuffer",    "()Z",         (void*) android_view_GLES20Canvas_preserveBackBuffer },
    { "nDisableVsync",          "()V",         (void*) android_view_GLES20Canvas_disableVsync },
    { "nFlushCaches",           "(I)V",        (void*) android_view_GLES20Canvas_flushCaches },

    { "nCreateRenderer",    "()I",             (void*) android_view_GLES20Canvas_createRenderer },
    { "nDestroyRenderer",   "(I)V",            (void*) android_view_GLES20Canvas_destroyRenderer },
Loading