From 5a09488a158b669577cd8eb557ce4feb62929e75 Mon Sep 17 00:00:00 2001 From: Xavier Ducrohet Date: Thu, 18 Nov 2010 18:24:51 -0800 Subject: [PATCH] Layoutlib: Properly dispose of bitmap delegate. This is done by initializing the android.util.Finalizers that's reponsible for calling out to the native bitmap destructor. Also implemented the native bitmap destructor Also fix Bridge by removing obsolete API methods, and removing some unneeded synchronized blocks now that the whole rendering (and scene creation) is protected by a synchronized on the bridge object anyway. Change-Id: Ie1792da6db354836542dfc11f457fe4a6d78ddfb --- .../src/android/graphics/Bitmap_Delegate.java | 3 +- .../com/android/layoutlib/bridge/Bridge.java | 90 +++---------------- .../bridge/impl/DelegateManager.java | 16 ++-- 3 files changed, 17 insertions(+), 92 deletions(-) diff --git a/tools/layoutlib/bridge/src/android/graphics/Bitmap_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/Bitmap_Delegate.java index 392462f28859f..09204972d1db8 100644 --- a/tools/layoutlib/bridge/src/android/graphics/Bitmap_Delegate.java +++ b/tools/layoutlib/bridge/src/android/graphics/Bitmap_Delegate.java @@ -162,8 +162,7 @@ public class Bitmap_Delegate { } /*package*/ static void nativeDestructor(int nativeBitmap) { - // FIXME implement native delegate - throw new UnsupportedOperationException("Native delegate needed for Bitmap"); + sManager.removeDelegate(nativeBitmap); } /*package*/ static void nativeRecycle(int nativeBitmap) { diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java index 53da2ca0cbf1f..efa0f27733aa7 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java @@ -32,6 +32,7 @@ import com.android.tools.layoutlib.create.OverrideMethod; import android.graphics.Bitmap; import android.graphics.Typeface_Delegate; +import android.util.Finalizers; import java.lang.ref.SoftReference; import java.lang.reflect.Field; @@ -114,19 +115,12 @@ public final class Bridge extends LayoutBridge { */ @Override public boolean init(String fontOsLocation, Map> enumValueMap) { + sEnumValueMap = enumValueMap; + + Finalizers.init(); + BridgeAssetManager.initSystem(); - return sinit(fontOsLocation, enumValueMap); - } - - @Override - public boolean dispose() { - BridgeAssetManager.clearSystem(); - return true; - } - - private static synchronized boolean sinit(String fontOsLocation, - Map> enumValueMap) { // When DEBUG_LAYOUT is set and is not 0 or false, setup a default listener // on static (native) methods which prints the signature on the console and @@ -165,8 +159,6 @@ public final class Bridge extends LayoutBridge { return false; } - sEnumValueMap = enumValueMap; - // now parse com.android.internal.R (and only this one as android.R is a subset of // the internal version), and put the content in the maps. try { @@ -214,6 +206,12 @@ public final class Bridge extends LayoutBridge { return true; } + @Override + public boolean dispose() { + BridgeAssetManager.clearSystem(); + return true; + } + /** * Sets a 9 patch in a project cache or in the framework cache. * @param value the path of the 9 patch @@ -423,70 +421,4 @@ public final class Bridge extends LayoutBridge { return null; } - - - // ---------- OBSOLETE API METHODS ---------- - - /* - * For compatilibty purposes, we implement the old deprecated version of computeLayout. - * (non-Javadoc) - * @see com.android.layoutlib.api.ILayoutBridge#computeLayout(com.android.layoutlib.api.IXmlPullParser, java.lang.Object, int, int, java.lang.String, java.util.Map, java.util.Map, com.android.layoutlib.api.IProjectCallback, com.android.layoutlib.api.ILayoutLog) - */ - @Deprecated - public com.android.layoutlib.api.ILayoutResult computeLayout(IXmlPullParser layoutDescription, - Object projectKey, - int screenWidth, int screenHeight, String themeName, - Map> projectResources, - Map> frameworkResources, - IProjectCallback customViewLoader, ILayoutLog logger) { - throw new UnsupportedOperationException(); - } - - /* - * For compatilibty purposes, we implement the old deprecated version of computeLayout. - * (non-Javadoc) - * @see com.android.layoutlib.api.ILayoutBridge#computeLayout(com.android.layoutlib.api.IXmlPullParser, java.lang.Object, int, int, java.lang.String, boolean, java.util.Map, java.util.Map, com.android.layoutlib.api.IProjectCallback, com.android.layoutlib.api.ILayoutLog) - */ - @Deprecated - public com.android.layoutlib.api.ILayoutResult computeLayout(IXmlPullParser layoutDescription, - Object projectKey, - int screenWidth, int screenHeight, String themeName, boolean isProjectTheme, - Map> projectResources, - Map> frameworkResources, - IProjectCallback customViewLoader, ILayoutLog logger) { - throw new UnsupportedOperationException(); - } - - /* - * For compatilibty purposes, we implement the old deprecated version of computeLayout. - * (non-Javadoc) - * @see com.android.layoutlib.api.ILayoutBridge#computeLayout(com.android.layoutlib.api.IXmlPullParser, java.lang.Object, int, int, int, float, float, java.lang.String, boolean, java.util.Map, java.util.Map, com.android.layoutlib.api.IProjectCallback, com.android.layoutlib.api.ILayoutLog) - */ - @Deprecated - public com.android.layoutlib.api.ILayoutResult computeLayout(IXmlPullParser layoutDescription, - Object projectKey, - int screenWidth, int screenHeight, int density, float xdpi, float ydpi, - String themeName, boolean isProjectTheme, - Map> projectResources, - Map> frameworkResources, - IProjectCallback customViewLoader, ILayoutLog logger) { - throw new UnsupportedOperationException(); - } - - /* - * (non-Javadoc) - * @see com.android.layoutlib.api.ILayoutBridge#computeLayout(com.android.layoutlib.api.IXmlPullParser, java.lang.Object, int, int, boolean, int, float, float, java.lang.String, boolean, java.util.Map, java.util.Map, com.android.layoutlib.api.IProjectCallback, com.android.layoutlib.api.ILayoutLog) - */ - @Deprecated - public com.android.layoutlib.api.ILayoutResult computeLayout(IXmlPullParser layoutDescription, - Object projectKey, - int screenWidth, int screenHeight, boolean renderFullSize, - int density, float xdpi, float ydpi, - String themeName, boolean isProjectTheme, - Map> projectResources, - Map> frameworkResources, - IProjectCallback customViewLoader, ILayoutLog logger) { - throw new UnsupportedOperationException(); - } - } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java index 169d7514adc84..b2729633cbf38 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java @@ -59,9 +59,7 @@ public final class DelegateManager { * @return the delegate or null if not found. */ public T getDelegate(int native_object) { - synchronized (mDelegates) { - return mDelegates.get(native_object); - } + return mDelegates.get(native_object); } /** @@ -70,11 +68,9 @@ public final class DelegateManager { * @return a unique native int to identify the delegate */ public int addDelegate(T newDelegate) { - synchronized (mDelegates) { - int native_object = ++mDelegateCounter; - mDelegates.put(native_object, newDelegate); - return native_object; - } + int native_object = ++mDelegateCounter; + mDelegates.put(native_object, newDelegate); + return native_object; } /** @@ -82,8 +78,6 @@ public final class DelegateManager { * @param native_object the native int. */ public void removeDelegate(int native_object) { - synchronized (mDelegates) { - mDelegates.remove(native_object); - } + mDelegates.remove(native_object); } }