2010-07-06 11:39:32 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "OpenGLRenderer"
|
|
|
|
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
|
2010-07-08 11:45:51 -07:00
|
|
|
#include <utils/Log.h>
|
|
|
|
|
2011-12-13 14:55:06 -08:00
|
|
|
#include "Caches.h"
|
2010-07-06 11:39:32 -07:00
|
|
|
#include "LayerCache.h"
|
2010-08-23 21:05:08 -07:00
|
|
|
#include "Properties.h"
|
2010-07-06 11:39:32 -07:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructors/destructor
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-10-08 15:49:53 -07:00
|
|
|
LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) {
|
2010-08-23 21:05:08 -07:00
|
|
|
char property[PROPERTY_VALUE_MAX];
|
|
|
|
if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
|
2011-01-21 21:14:15 -08:00
|
|
|
INIT_LOGD(" Setting layer cache size to %sMB", property);
|
2010-08-23 21:05:08 -07:00
|
|
|
setMaxSize(MB(atof(property)));
|
|
|
|
} else {
|
2011-01-21 21:14:15 -08:00
|
|
|
INIT_LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
|
2010-08-23 21:05:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
LayerCache::~LayerCache() {
|
2010-07-07 13:06:26 -07:00
|
|
|
clear();
|
2010-07-06 11:39:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Size management
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
uint32_t LayerCache::getSize() {
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LayerCache::getMaxSize() {
|
|
|
|
return mMaxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerCache::setMaxSize(uint32_t maxSize) {
|
2010-10-08 15:49:53 -07:00
|
|
|
clear();
|
2010-07-06 11:39:32 -07:00
|
|
|
mMaxSize = maxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Caching
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-01-08 11:15:30 -08:00
|
|
|
int LayerCache::LayerEntry::compare(const LayerCache::LayerEntry& lhs,
|
|
|
|
const LayerCache::LayerEntry& rhs) {
|
|
|
|
int deltaInt = int(lhs.mWidth) - int(rhs.mWidth);
|
|
|
|
if (deltaInt != 0) return deltaInt;
|
|
|
|
|
|
|
|
return int(lhs.mHeight) - int(rhs.mHeight);
|
|
|
|
}
|
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
void LayerCache::deleteLayer(Layer* layer) {
|
|
|
|
if (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
|
|
|
LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(),
|
|
|
|
layer->getFbo());
|
2011-07-07 20:50:11 -07:00
|
|
|
mSize -= layer->getWidth() * layer->getHeight() * 4;
|
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
|
|
|
Caches::getInstance().resourceCache.decrementRefcount(layer);
|
2010-07-06 11:39:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerCache::clear() {
|
2010-10-08 15:49:53 -07:00
|
|
|
size_t count = mCache.size();
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
deleteLayer(mCache.itemAt(i).mLayer);
|
|
|
|
}
|
2010-07-06 11:39:32 -07:00
|
|
|
mCache.clear();
|
|
|
|
}
|
|
|
|
|
2010-10-08 15:49:53 -07:00
|
|
|
Layer* LayerCache::get(const uint32_t width, const uint32_t height) {
|
|
|
|
Layer* layer = NULL;
|
|
|
|
|
|
|
|
LayerEntry entry(width, height);
|
|
|
|
ssize_t index = mCache.indexOf(entry);
|
2010-07-08 11:45:51 -07:00
|
|
|
|
2010-10-08 15:49:53 -07:00
|
|
|
if (index >= 0) {
|
|
|
|
entry = mCache.itemAt(index);
|
|
|
|
mCache.removeAt(index);
|
|
|
|
|
|
|
|
layer = entry.mLayer;
|
2011-07-07 20:50:11 -07:00
|
|
|
mSize -= layer->getWidth() * layer->getHeight() * 4;
|
2010-10-08 15:49:53 -07:00
|
|
|
|
2011-07-07 20:50:11 -07:00
|
|
|
LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
|
2010-07-08 11:45:51 -07:00
|
|
|
} else {
|
2010-10-08 15:49:53 -07:00
|
|
|
LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
|
2010-07-08 11:45:51 -07:00
|
|
|
|
2010-10-08 15:49:53 -07:00
|
|
|
layer = new Layer(entry.mWidth, entry.mHeight);
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setBlend(true);
|
|
|
|
layer->setEmpty(true);
|
|
|
|
layer->setFbo(0);
|
|
|
|
|
|
|
|
layer->generateTexture();
|
|
|
|
layer->bindTexture();
|
2011-12-12 18:14:06 -08:00
|
|
|
layer->setFilter(GL_NEAREST);
|
|
|
|
layer->setWrap(GL_CLAMP_TO_EDGE, false);
|
2010-10-01 00:25:02 -07:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
|
2010-10-05 18:14:38 -07:00
|
|
|
#if DEBUG_LAYERS
|
2011-07-26 20:35:55 -07:00
|
|
|
dump();
|
2010-10-05 18:14:38 -07:00
|
|
|
#endif
|
2010-07-06 11:39:32 -07:00
|
|
|
}
|
2010-07-08 11:45:51 -07:00
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2011-07-26 20:35:55 -07:00
|
|
|
void LayerCache::dump() {
|
|
|
|
size_t size = mCache.size();
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
const LayerEntry& entry = mCache.itemAt(i);
|
|
|
|
LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-08 15:49:53 -07:00
|
|
|
bool LayerCache::put(Layer* layer) {
|
2011-07-07 20:50:11 -07:00
|
|
|
if (!layer->isCacheable()) return false;
|
New widget: TextureView
Bug #4343984
TextureView can be used to render media content (video, OpenGL,
RenderScript) inside a View.
The key difference with SurfaceView is that TextureView does
not create a new Surface. This gives the ability to seamlessly
transform, animate, fade, etc. a TextureView, which was hard
if not impossible to do with a SurfaceView.
A TextureView also interacts perfectly with ScrollView,
ListView, etc. It allows application to embed media content
in a much more flexible way than before.
For instance, to render the camera preview at 50% opacity,
all you need to do is the following:
mTextureView.setAlpha(0.5f);
Camera c = Camera.open();
c.setPreviewTexture(mTextureView.getSurfaceTexture());
c.startPreview();
TextureView uses a SurfaceTexture to get the job done. More
APIs are required to make it easy to create OpenGL contexts
for a TextureView. It can currently be done with a bit of
JNI code.
Change-Id: Iaa7953097ab5beb8437bcbbfa03b2df5b7f80cd7
2011-04-28 18:40:04 -07:00
|
|
|
|
2011-07-07 20:50:11 -07:00
|
|
|
const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
|
2010-07-06 11:39:32 -07:00
|
|
|
// Don't even try to cache a layer that's bigger than the cache
|
|
|
|
if (size < mMaxSize) {
|
2010-10-08 15:49:53 -07:00
|
|
|
// TODO: Use an LRU
|
2010-07-06 11:39:32 -07:00
|
|
|
while (mSize + size > mMaxSize) {
|
2010-10-12 18:15:42 -07:00
|
|
|
size_t position = 0;
|
2011-08-22 14:01:34 -07:00
|
|
|
#if LAYER_REMOVE_BIGGEST_FIRST
|
2010-10-12 18:15:42 -07:00
|
|
|
position = mCache.size() - 1;
|
|
|
|
#endif
|
|
|
|
Layer* victim = mCache.itemAt(position).mLayer;
|
|
|
|
deleteLayer(victim);
|
|
|
|
mCache.removeAt(position);
|
2010-10-08 15:49:53 -07:00
|
|
|
|
2010-10-12 18:15:42 -07:00
|
|
|
LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
|
|
|
|
victim->layer.getHeight());
|
2010-07-06 11:39:32 -07:00
|
|
|
}
|
|
|
|
|
2013-06-17 13:14:51 -07:00
|
|
|
layer->cancelDefer();
|
2012-04-02 17:43:05 -07:00
|
|
|
|
2010-10-08 15:49:53 -07:00
|
|
|
LayerEntry entry(layer);
|
|
|
|
|
|
|
|
mCache.add(entry);
|
2010-07-06 11:39:32 -07:00
|
|
|
mSize += size;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|