Commit e69847f7 authored by mdjones's avatar mdjones Committed by Commit bot

Remove title resources when they are no longer used

Previously, layer titles found their way into the resource manager
as ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP and were left there forever.
Over time, these recources accumulated (easily reproducable by
entering and exiting the tab switcher) and is more than likely the
cause of memory related crashes when entering the tab switcher.
The title layer itself was removed from the LayerTitleCache but the
resources that constitute the title were never correctly managed.

This change adds a function to the ResourceManager that allows the
LayerTitleCache to remove associated resources. Specifically, only
dynamic bitmaps can be manually removed.

BUG=669348

Review-Url: https://codereview.chromium.org/2578233002
Cr-Commit-Position: refs/heads/master@{#441773}
parent 7bce08ea
......@@ -22,6 +22,16 @@ public abstract class ResourceLoader {
* not be loaded.
*/
void onResourceLoaded(int resType, int resId, Resource resource);
/**
* Called when a resource is unregistered (unneeded). This should only be called for
* dynamic bitmap resources since they change constantly and are replaced with new bitmaps.
* Other resource types should not need this since thay are static for the lifetime of the
* application.
* @param resType The {@link ResourceType} of resource that was removed.
* @param redId The Android id of the removed resource.
*/
void onResourceUnregistered(int resType, int resId);
}
private final int mResourceType;
......@@ -68,4 +78,13 @@ public abstract class ResourceLoader {
protected void notifyLoadFinished(int resId, Resource resource) {
if (mCallback != null) mCallback.onResourceLoaded(getResourceType(), resId, resource);
}
/**
* A helper method for subclasses to notify the manager that a {@link Resource} is no longer
* being used.
* @param resId The id of the {@link Resource} being unloaded.
*/
protected void notifyResourceUnregistered(int resId) {
if (mCallback != null) mCallback.onResourceUnregistered(getResourceType(), resId);
}
}
......@@ -167,6 +167,14 @@ public class ResourceManager implements ResourceLoaderCallback {
aperture.left, aperture.top, aperture.right, aperture.bottom);
}
@Override
public void onResourceUnregistered(int resType, int resId) {
// Only remove dynamic bitmaps that were unregistered.
if (resType != AndroidResourceType.DYNAMIC_BITMAP) return;
nativeRemoveResource(mNativeResourceManagerPtr, resType, resId);
}
/**
* Clear the cache of tinted assets that the native manager holds.
*/
......@@ -234,6 +242,8 @@ public class ResourceManager implements ResourceLoaderCallback {
int unscaledSpriteHeight, float scaledSpriteWidth, float scaledSpriteHeight);
private native void nativeOnCrushedSpriteResourceReloaded(long nativeResourceManagerImpl,
int bitmapResId, Bitmap bitmap);
private native void nativeRemoveResource(long nativeResourceManagerImpl, int resType,
int resId);
private native void nativeClearTintedResourceCache(long nativeResourceManagerImpl);
}
......@@ -43,6 +43,7 @@ public class DynamicResourceLoader extends ResourceLoader {
*/
public void unregisterResource(int resId) {
mDynamicResources.remove(resId);
notifyResourceUnregistered(resId);
}
/**
......@@ -66,4 +67,4 @@ public class DynamicResourceLoader extends ResourceLoader {
// Not implemented.
assert false : "Preloading dynamic resources isn't supported.";
}
}
\ No newline at end of file
}
......@@ -220,6 +220,14 @@ void ResourceManagerImpl::OnResourceReady(JNIEnv* env,
ui_resource_manager_, cc::UIResourceBitmap(skbitmap));
}
void ResourceManagerImpl::RemoveResource(
JNIEnv* env,
const base::android::JavaRef<jobject>& jobj,
jint res_type,
jint res_id) {
resources_[res_type].erase(res_id);
}
CrushedSpriteResource* ResourceManagerImpl::GetCrushedSpriteResource(
int bitmap_res_id, int metadata_res_id) {
......
......@@ -74,6 +74,11 @@ class UI_ANDROID_EXPORT ResourceManagerImpl
const base::android::JavaRef<jobject>& jobj,
jint bitmap_res_id,
const base::android::JavaRef<jobject>& bitmap);
void RemoveResource(
JNIEnv* env,
const base::android::JavaRef<jobject>& jobj,
jint res_type,
jint res_id);
void ClearTintedResourceCache(JNIEnv* env,
const base::android::JavaRef<jobject>& jobj);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment