2010-10-08 08:37:55 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-06-26 15:45:41 -07:00
|
|
|
#define LOG_TAG "OpenGLRenderer"
|
|
|
|
|
2010-10-08 08:37:55 -07:00
|
|
|
#include <SkPixelRef.h>
|
|
|
|
#include "ResourceCache.h"
|
|
|
|
#include "Caches.h"
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Resource cache
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void ResourceCache::logCache() {
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("ResourceCache: cacheReport:");
|
2010-10-08 08:37:55 -07:00
|
|
|
for (size_t i = 0; i < mCache->size(); ++i) {
|
|
|
|
ResourceReference* ref = mCache->valueAt(i);
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD(" ResourceCache: mCache(%d): resource, ref = 0x%p, 0x%p",
|
2010-10-08 08:37:55 -07:00
|
|
|
i, mCache->keyAt(i), mCache->valueAt(i));
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD(" ResourceCache: mCache(%d): refCount, recycled, destroyed, type = %d, %d, %d, %d",
|
2010-10-08 08:37:55 -07:00
|
|
|
i, ref->refCount, ref->recycled, ref->destroyed, ref->resourceType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceCache::ResourceCache() {
|
2010-11-11 16:30:16 -08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2014-01-02 17:13:34 -08:00
|
|
|
mCache = new KeyedVector<const void*, ResourceReference*>();
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ResourceCache::~ResourceCache() {
|
2010-11-11 16:30:16 -08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2010-10-08 08:37:55 -07:00
|
|
|
delete mCache;
|
|
|
|
}
|
|
|
|
|
2012-09-07 11:58:36 -07:00
|
|
|
void ResourceCache::lock() {
|
|
|
|
mLock.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::unlock() {
|
|
|
|
mLock.unlock();
|
|
|
|
}
|
|
|
|
|
2010-10-08 08:37:55 -07:00
|
|
|
void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
|
2010-11-11 16:30:16 -08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-09-07 11:58:36 -07:00
|
|
|
incrementRefcountLocked(resource, resourceType);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::incrementRefcount(const SkBitmap* bitmapResource) {
|
2013-07-02 10:48:54 -07:00
|
|
|
bitmapResource->pixelRef()->globalRef();
|
2011-02-22 13:55:04 -05:00
|
|
|
SkSafeRef(bitmapResource->getColorTable());
|
2012-05-15 11:10:01 -07:00
|
|
|
incrementRefcount((void*) bitmapResource, kBitmap);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::incrementRefcount(const SkPath* pathResource) {
|
2012-05-15 11:10:01 -07:00
|
|
|
incrementRefcount((void*) pathResource, kPath);
|
2011-02-04 12:50:55 -08:00
|
|
|
}
|
|
|
|
|
2010-10-08 08:37:55 -07:00
|
|
|
void ResourceCache::incrementRefcount(SkiaShader* shaderResource) {
|
2011-02-22 13:55:04 -05:00
|
|
|
SkSafeRef(shaderResource->getSkShader());
|
2011-01-14 18:51:01 -08:00
|
|
|
incrementRefcount((void*) shaderResource, kShader);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
|
2010-10-22 16:17:12 -07:00
|
|
|
void ResourceCache::incrementRefcount(SkiaColorFilter* filterResource) {
|
2011-02-22 13:55:04 -05:00
|
|
|
SkSafeRef(filterResource->getSkColorFilter());
|
2011-01-14 18:51:01 -08:00
|
|
|
incrementRefcount((void*) filterResource, kColorFilter);
|
2010-10-22 16:17:12 -07:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
|
2013-06-26 15:45:41 -07:00
|
|
|
incrementRefcount((void*) patchResource, kNinePatch);
|
|
|
|
}
|
|
|
|
|
Fix occasional crash bug with layers
Launcher occasionally crashes with a stack trace indicating that the memory
of a Layer object is corrupt. It is possible for us to delete a Layer
structure and then, briefly, use it to draw a DisplayList again before
that DisplayList gets recreated (without the layer that got deleted).
When this happens, if the memory got corrupted, it's possible to crash.
The fix is to add Layer to the other objects which we currently refcount
(bitmaps, shaders, etc.). Then instead of deleting a Layer, we decrement the
refcount. We increment when creating it, then increment it again when it's
referenced from a DisplayList. Then we decrement the refcount instead of
deleting it, and decrement when we clear a DisplayList that refers to it.
Then when the refcount reaches 0, we delete it.
Issue #6994632 Native crash in launcher when trying to launch all apps screen
Change-Id: I0627be8d49bb2f9ba8d158a84b764bb4e7df934c
2012-09-14 15:31:25 -07:00
|
|
|
void ResourceCache::incrementRefcount(Layer* layerResource) {
|
|
|
|
incrementRefcount((void*) layerResource, kLayer);
|
|
|
|
}
|
|
|
|
|
2012-09-07 11:58:36 -07:00
|
|
|
void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
|
2012-07-20 11:36:03 -07:00
|
|
|
ssize_t index = mCache->indexOfKey(resource);
|
|
|
|
ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
|
2012-09-07 11:58:36 -07:00
|
|
|
if (ref == NULL || mCache->size() == 0) {
|
|
|
|
ref = new ResourceReference(resourceType);
|
|
|
|
mCache->add(resource, ref);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
2012-09-07 11:58:36 -07:00
|
|
|
ref->refCount++;
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::incrementRefcountLocked(const SkBitmap* bitmapResource) {
|
2013-07-02 10:48:54 -07:00
|
|
|
bitmapResource->pixelRef()->globalRef();
|
2012-09-07 11:58:36 -07:00
|
|
|
SkSafeRef(bitmapResource->getColorTable());
|
|
|
|
incrementRefcountLocked((void*) bitmapResource, kBitmap);
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::incrementRefcountLocked(const SkPath* pathResource) {
|
2012-09-07 11:58:36 -07:00
|
|
|
incrementRefcountLocked((void*) pathResource, kPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::incrementRefcountLocked(SkiaShader* shaderResource) {
|
|
|
|
SkSafeRef(shaderResource->getSkShader());
|
|
|
|
incrementRefcountLocked((void*) shaderResource, kShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::incrementRefcountLocked(SkiaColorFilter* filterResource) {
|
|
|
|
SkSafeRef(filterResource->getSkColorFilter());
|
|
|
|
incrementRefcountLocked((void*) filterResource, kColorFilter);
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::incrementRefcountLocked(const Res_png_9patch* patchResource) {
|
2013-06-26 15:45:41 -07:00
|
|
|
incrementRefcountLocked((void*) patchResource, kNinePatch);
|
|
|
|
}
|
|
|
|
|
Fix occasional crash bug with layers
Launcher occasionally crashes with a stack trace indicating that the memory
of a Layer object is corrupt. It is possible for us to delete a Layer
structure and then, briefly, use it to draw a DisplayList again before
that DisplayList gets recreated (without the layer that got deleted).
When this happens, if the memory got corrupted, it's possible to crash.
The fix is to add Layer to the other objects which we currently refcount
(bitmaps, shaders, etc.). Then instead of deleting a Layer, we decrement the
refcount. We increment when creating it, then increment it again when it's
referenced from a DisplayList. Then we decrement the refcount instead of
deleting it, and decrement when we clear a DisplayList that refers to it.
Then when the refcount reaches 0, we delete it.
Issue #6994632 Native crash in launcher when trying to launch all apps screen
Change-Id: I0627be8d49bb2f9ba8d158a84b764bb4e7df934c
2012-09-14 15:31:25 -07:00
|
|
|
void ResourceCache::incrementRefcountLocked(Layer* layerResource) {
|
|
|
|
incrementRefcountLocked((void*) layerResource, kLayer);
|
|
|
|
}
|
|
|
|
|
2012-09-07 11:58:36 -07:00
|
|
|
void ResourceCache::decrementRefcount(void* resource) {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
decrementRefcountLocked(resource);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) {
|
2013-07-02 10:48:54 -07:00
|
|
|
bitmapResource->pixelRef()->globalUnref();
|
2011-02-22 13:55:04 -05:00
|
|
|
SkSafeUnref(bitmapResource->getColorTable());
|
2011-01-14 18:51:01 -08:00
|
|
|
decrementRefcount((void*) bitmapResource);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::decrementRefcount(const SkPath* pathResource) {
|
2011-02-04 12:50:55 -08:00
|
|
|
decrementRefcount((void*) pathResource);
|
|
|
|
}
|
|
|
|
|
2010-10-08 08:37:55 -07:00
|
|
|
void ResourceCache::decrementRefcount(SkiaShader* shaderResource) {
|
2011-02-22 13:55:04 -05:00
|
|
|
SkSafeUnref(shaderResource->getSkShader());
|
2011-01-14 18:51:01 -08:00
|
|
|
decrementRefcount((void*) shaderResource);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
|
2010-10-22 16:17:12 -07:00
|
|
|
void ResourceCache::decrementRefcount(SkiaColorFilter* filterResource) {
|
2011-02-22 13:55:04 -05:00
|
|
|
SkSafeUnref(filterResource->getSkColorFilter());
|
2011-01-14 18:51:01 -08:00
|
|
|
decrementRefcount((void*) filterResource);
|
2010-10-22 16:17:12 -07:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
|
2013-06-26 15:45:41 -07:00
|
|
|
decrementRefcount((void*) patchResource);
|
|
|
|
}
|
|
|
|
|
Fix occasional crash bug with layers
Launcher occasionally crashes with a stack trace indicating that the memory
of a Layer object is corrupt. It is possible for us to delete a Layer
structure and then, briefly, use it to draw a DisplayList again before
that DisplayList gets recreated (without the layer that got deleted).
When this happens, if the memory got corrupted, it's possible to crash.
The fix is to add Layer to the other objects which we currently refcount
(bitmaps, shaders, etc.). Then instead of deleting a Layer, we decrement the
refcount. We increment when creating it, then increment it again when it's
referenced from a DisplayList. Then we decrement the refcount instead of
deleting it, and decrement when we clear a DisplayList that refers to it.
Then when the refcount reaches 0, we delete it.
Issue #6994632 Native crash in launcher when trying to launch all apps screen
Change-Id: I0627be8d49bb2f9ba8d158a84b764bb4e7df934c
2012-09-14 15:31:25 -07:00
|
|
|
void ResourceCache::decrementRefcount(Layer* layerResource) {
|
|
|
|
decrementRefcount((void*) layerResource);
|
|
|
|
}
|
|
|
|
|
2012-09-07 11:58:36 -07:00
|
|
|
void ResourceCache::decrementRefcountLocked(void* resource) {
|
2012-07-20 11:36:03 -07:00
|
|
|
ssize_t index = mCache->indexOfKey(resource);
|
2012-09-07 11:58:36 -07:00
|
|
|
ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
|
2010-10-08 08:37:55 -07:00
|
|
|
if (ref == NULL) {
|
2012-09-07 11:58:36 -07:00
|
|
|
// Should not get here - shouldn't get a call to decrement if we're not yet tracking it
|
2010-10-08 08:37:55 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-09-07 11:58:36 -07:00
|
|
|
ref->refCount--;
|
2010-10-08 08:37:55 -07:00
|
|
|
if (ref->refCount == 0) {
|
2012-09-23 17:46:45 -07:00
|
|
|
deleteResourceReferenceLocked(resource, ref);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) {
|
2013-07-02 10:48:54 -07:00
|
|
|
bitmapResource->pixelRef()->globalUnref();
|
2012-09-07 11:58:36 -07:00
|
|
|
SkSafeUnref(bitmapResource->getColorTable());
|
|
|
|
decrementRefcountLocked((void*) bitmapResource);
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) {
|
2012-09-07 11:58:36 -07:00
|
|
|
decrementRefcountLocked((void*) pathResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::decrementRefcountLocked(SkiaShader* shaderResource) {
|
|
|
|
SkSafeUnref(shaderResource->getSkShader());
|
|
|
|
decrementRefcountLocked((void*) shaderResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::decrementRefcountLocked(SkiaColorFilter* filterResource) {
|
|
|
|
SkSafeUnref(filterResource->getSkColorFilter());
|
|
|
|
decrementRefcountLocked((void*) filterResource);
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
|
2013-06-26 15:45:41 -07:00
|
|
|
decrementRefcountLocked((void*) patchResource);
|
|
|
|
}
|
|
|
|
|
Fix occasional crash bug with layers
Launcher occasionally crashes with a stack trace indicating that the memory
of a Layer object is corrupt. It is possible for us to delete a Layer
structure and then, briefly, use it to draw a DisplayList again before
that DisplayList gets recreated (without the layer that got deleted).
When this happens, if the memory got corrupted, it's possible to crash.
The fix is to add Layer to the other objects which we currently refcount
(bitmaps, shaders, etc.). Then instead of deleting a Layer, we decrement the
refcount. We increment when creating it, then increment it again when it's
referenced from a DisplayList. Then we decrement the refcount instead of
deleting it, and decrement when we clear a DisplayList that refers to it.
Then when the refcount reaches 0, we delete it.
Issue #6994632 Native crash in launcher when trying to launch all apps screen
Change-Id: I0627be8d49bb2f9ba8d158a84b764bb4e7df934c
2012-09-14 15:31:25 -07:00
|
|
|
void ResourceCache::decrementRefcountLocked(Layer* layerResource) {
|
|
|
|
decrementRefcountLocked((void*) layerResource);
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:50:55 -08:00
|
|
|
void ResourceCache::destructor(SkPath* resource) {
|
|
|
|
Mutex::Autolock _l(mLock);
|
2012-09-07 11:58:36 -07:00
|
|
|
destructorLocked(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::destructorLocked(SkPath* resource) {
|
2012-07-20 11:36:03 -07:00
|
|
|
ssize_t index = mCache->indexOfKey(resource);
|
|
|
|
ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
|
2011-02-04 12:50:55 -08:00
|
|
|
if (ref == NULL) {
|
|
|
|
// If we're not tracking this resource, just delete it
|
|
|
|
if (Caches::hasInstance()) {
|
|
|
|
Caches::getInstance().pathCache.removeDeferred(resource);
|
|
|
|
}
|
|
|
|
delete resource;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref->destroyed = true;
|
|
|
|
if (ref->refCount == 0) {
|
2012-09-23 17:46:45 -07:00
|
|
|
deleteResourceReferenceLocked(resource, ref);
|
2011-02-04 12:50:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::destructor(const SkBitmap* resource) {
|
2010-11-11 16:30:16 -08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-09-07 11:58:36 -07:00
|
|
|
destructorLocked(resource);
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::destructorLocked(const SkBitmap* resource) {
|
2012-07-20 11:36:03 -07:00
|
|
|
ssize_t index = mCache->indexOfKey(resource);
|
|
|
|
ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
|
2010-10-08 08:37:55 -07:00
|
|
|
if (ref == NULL) {
|
|
|
|
// If we're not tracking this resource, just delete it
|
|
|
|
if (Caches::hasInstance()) {
|
2010-11-11 15:36:56 -08:00
|
|
|
Caches::getInstance().textureCache.removeDeferred(resource);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
delete resource;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref->destroyed = true;
|
|
|
|
if (ref->refCount == 0) {
|
2012-09-23 17:46:45 -07:00
|
|
|
deleteResourceReferenceLocked(resource, ref);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::destructor(SkiaShader* resource) {
|
2010-11-11 16:30:16 -08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-09-07 11:58:36 -07:00
|
|
|
destructorLocked(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::destructorLocked(SkiaShader* resource) {
|
2012-07-20 11:36:03 -07:00
|
|
|
ssize_t index = mCache->indexOfKey(resource);
|
|
|
|
ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
|
2010-10-08 08:37:55 -07:00
|
|
|
if (ref == NULL) {
|
|
|
|
// If we're not tracking this resource, just delete it
|
|
|
|
delete resource;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref->destroyed = true;
|
|
|
|
if (ref->refCount == 0) {
|
2012-09-23 17:46:45 -07:00
|
|
|
deleteResourceReferenceLocked(resource, ref);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-22 16:17:12 -07:00
|
|
|
void ResourceCache::destructor(SkiaColorFilter* resource) {
|
2010-11-11 16:30:16 -08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-09-07 11:58:36 -07:00
|
|
|
destructorLocked(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::destructorLocked(SkiaColorFilter* resource) {
|
2012-07-20 11:36:03 -07:00
|
|
|
ssize_t index = mCache->indexOfKey(resource);
|
|
|
|
ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
|
2010-10-22 16:17:12 -07:00
|
|
|
if (ref == NULL) {
|
|
|
|
// If we're not tracking this resource, just delete it
|
|
|
|
delete resource;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref->destroyed = true;
|
|
|
|
if (ref->refCount == 0) {
|
2012-09-23 17:46:45 -07:00
|
|
|
deleteResourceReferenceLocked(resource, ref);
|
2010-10-22 16:17:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 15:45:41 -07:00
|
|
|
void ResourceCache::destructor(Res_png_9patch* resource) {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
destructorLocked(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::destructorLocked(Res_png_9patch* resource) {
|
|
|
|
ssize_t index = mCache->indexOfKey(resource);
|
|
|
|
ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
|
|
|
|
if (ref == NULL) {
|
|
|
|
if (Caches::hasInstance()) {
|
|
|
|
Caches::getInstance().patchCache.removeDeferred(resource);
|
|
|
|
}
|
|
|
|
// If we're not tracking this resource, just delete it
|
|
|
|
// A Res_png_9patch is actually an array of byte that's larger
|
|
|
|
// than sizeof(Res_png_9patch). It must be freed as an array.
|
|
|
|
delete[] (int8_t*) resource;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref->destroyed = true;
|
|
|
|
if (ref->refCount == 0) {
|
|
|
|
deleteResourceReferenceLocked(resource, ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 15:07:26 -07:00
|
|
|
/**
|
|
|
|
* Return value indicates whether resource was actually recycled, which happens when RefCnt
|
|
|
|
* reaches 0.
|
|
|
|
*/
|
|
|
|
bool ResourceCache::recycle(SkBitmap* resource) {
|
2012-09-07 11:58:36 -07:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-10-22 15:07:26 -07:00
|
|
|
return recycleLocked(resource);
|
2012-09-07 11:58:36 -07:00
|
|
|
}
|
|
|
|
|
2012-10-22 15:07:26 -07:00
|
|
|
/**
|
|
|
|
* Return value indicates whether resource was actually recycled, which happens when RefCnt
|
|
|
|
* reaches 0.
|
|
|
|
*/
|
|
|
|
bool ResourceCache::recycleLocked(SkBitmap* resource) {
|
2012-09-07 11:58:36 -07:00
|
|
|
ssize_t index = mCache->indexOfKey(resource);
|
|
|
|
if (index < 0) {
|
|
|
|
// not tracking this resource; just recycle the pixel data
|
|
|
|
resource->setPixels(NULL, NULL);
|
2012-10-22 15:07:26 -07:00
|
|
|
return true;
|
2012-09-07 11:58:36 -07:00
|
|
|
}
|
|
|
|
ResourceReference* ref = mCache->valueAt(index);
|
|
|
|
if (ref == NULL) {
|
|
|
|
// Should not get here - shouldn't get a call to recycle if we're not yet tracking it
|
2012-10-22 15:07:26 -07:00
|
|
|
return true;
|
2012-09-07 11:58:36 -07:00
|
|
|
}
|
|
|
|
ref->recycled = true;
|
|
|
|
if (ref->refCount == 0) {
|
2012-09-23 17:46:45 -07:00
|
|
|
deleteResourceReferenceLocked(resource, ref);
|
2012-10-22 15:07:26 -07:00
|
|
|
return true;
|
2012-09-07 11:58:36 -07:00
|
|
|
}
|
2012-10-22 15:07:26 -07:00
|
|
|
// Still referring to resource, don't recycle yet
|
|
|
|
return false;
|
2012-09-07 11:58:36 -07:00
|
|
|
}
|
|
|
|
|
2010-11-11 16:30:16 -08:00
|
|
|
/**
|
|
|
|
* This method should only be called while the mLock mutex is held (that mutex is grabbed
|
|
|
|
* by the various destructor() and recycle() methods which call this method).
|
|
|
|
*/
|
2014-01-02 17:13:34 -08:00
|
|
|
void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
|
2010-10-08 08:37:55 -07:00
|
|
|
if (ref->recycled && ref->resourceType == kBitmap) {
|
|
|
|
((SkBitmap*) resource)->setPixels(NULL, NULL);
|
|
|
|
}
|
Fix occasional crash bug with layers
Launcher occasionally crashes with a stack trace indicating that the memory
of a Layer object is corrupt. It is possible for us to delete a Layer
structure and then, briefly, use it to draw a DisplayList again before
that DisplayList gets recreated (without the layer that got deleted).
When this happens, if the memory got corrupted, it's possible to crash.
The fix is to add Layer to the other objects which we currently refcount
(bitmaps, shaders, etc.). Then instead of deleting a Layer, we decrement the
refcount. We increment when creating it, then increment it again when it's
referenced from a DisplayList. Then we decrement the refcount instead of
deleting it, and decrement when we clear a DisplayList that refers to it.
Then when the refcount reaches 0, we delete it.
Issue #6994632 Native crash in launcher when trying to launch all apps screen
Change-Id: I0627be8d49bb2f9ba8d158a84b764bb4e7df934c
2012-09-14 15:31:25 -07:00
|
|
|
if (ref->destroyed || ref->resourceType == kLayer) {
|
2010-10-08 08:37:55 -07:00
|
|
|
switch (ref->resourceType) {
|
2011-06-22 16:14:36 -07:00
|
|
|
case kBitmap: {
|
|
|
|
SkBitmap* bitmap = (SkBitmap*) resource;
|
2010-10-08 08:37:55 -07:00
|
|
|
if (Caches::hasInstance()) {
|
2010-11-11 15:36:56 -08:00
|
|
|
Caches::getInstance().textureCache.removeDeferred(bitmap);
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
delete bitmap;
|
|
|
|
}
|
|
|
|
break;
|
2011-06-22 16:14:36 -07:00
|
|
|
case kPath: {
|
|
|
|
SkPath* path = (SkPath*) resource;
|
2011-02-04 12:50:55 -08:00
|
|
|
if (Caches::hasInstance()) {
|
|
|
|
Caches::getInstance().pathCache.removeDeferred(path);
|
|
|
|
}
|
|
|
|
delete path;
|
|
|
|
}
|
|
|
|
break;
|
2011-06-22 16:14:36 -07:00
|
|
|
case kShader: {
|
|
|
|
SkiaShader* shader = (SkiaShader*) resource;
|
2010-10-08 08:37:55 -07:00
|
|
|
delete shader;
|
2010-10-22 16:17:12 -07:00
|
|
|
}
|
|
|
|
break;
|
2011-06-22 16:14:36 -07:00
|
|
|
case kColorFilter: {
|
|
|
|
SkiaColorFilter* filter = (SkiaColorFilter*) resource;
|
2010-10-22 16:17:12 -07:00
|
|
|
delete filter;
|
|
|
|
}
|
|
|
|
break;
|
2013-06-26 15:45:41 -07:00
|
|
|
case kNinePatch: {
|
|
|
|
if (Caches::hasInstance()) {
|
|
|
|
Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
|
|
|
|
}
|
|
|
|
// A Res_png_9patch is actually an array of byte that's larger
|
|
|
|
// than sizeof(Res_png_9patch). It must be freed as an array.
|
|
|
|
int8_t* patch = (int8_t*) resource;
|
|
|
|
delete[] patch;
|
|
|
|
}
|
|
|
|
break;
|
Fix occasional crash bug with layers
Launcher occasionally crashes with a stack trace indicating that the memory
of a Layer object is corrupt. It is possible for us to delete a Layer
structure and then, briefly, use it to draw a DisplayList again before
that DisplayList gets recreated (without the layer that got deleted).
When this happens, if the memory got corrupted, it's possible to crash.
The fix is to add Layer to the other objects which we currently refcount
(bitmaps, shaders, etc.). Then instead of deleting a Layer, we decrement the
refcount. We increment when creating it, then increment it again when it's
referenced from a DisplayList. Then we decrement the refcount instead of
deleting it, and decrement when we clear a DisplayList that refers to it.
Then when the refcount reaches 0, we delete it.
Issue #6994632 Native crash in launcher when trying to launch all apps screen
Change-Id: I0627be8d49bb2f9ba8d158a84b764bb4e7df934c
2012-09-14 15:31:25 -07:00
|
|
|
case kLayer: {
|
2012-09-25 20:30:09 -07:00
|
|
|
Layer* layer = (Layer*) resource;
|
2012-09-25 21:30:22 -07:00
|
|
|
Caches::getInstance().deleteLayerDeferred(layer);
|
Fix occasional crash bug with layers
Launcher occasionally crashes with a stack trace indicating that the memory
of a Layer object is corrupt. It is possible for us to delete a Layer
structure and then, briefly, use it to draw a DisplayList again before
that DisplayList gets recreated (without the layer that got deleted).
When this happens, if the memory got corrupted, it's possible to crash.
The fix is to add Layer to the other objects which we currently refcount
(bitmaps, shaders, etc.). Then instead of deleting a Layer, we decrement the
refcount. We increment when creating it, then increment it again when it's
referenced from a DisplayList. Then we decrement the refcount instead of
deleting it, and decrement when we clear a DisplayList that refers to it.
Then when the refcount reaches 0, we delete it.
Issue #6994632 Native crash in launcher when trying to launch all apps screen
Change-Id: I0627be8d49bb2f9ba8d158a84b764bb4e7df934c
2012-09-14 15:31:25 -07:00
|
|
|
}
|
|
|
|
break;
|
2010-10-08 08:37:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mCache->removeItem(resource);
|
|
|
|
delete ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|