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.
|
|
|
|
*/
|
|
|
|
|
2015-11-11 16:42:34 -08:00
|
|
|
#include "LayerCache.h"
|
2010-07-08 11:45:51 -07:00
|
|
|
|
2011-12-13 14:55:06 -08:00
|
|
|
#include "Caches.h"
|
2010-08-23 21:05:08 -07:00
|
|
|
#include "Properties.h"
|
2010-07-06 11:39:32 -07:00
|
|
|
|
2015-11-11 16:42:34 -08:00
|
|
|
#include <utils/Log.h>
|
|
|
|
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructors/destructor
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-11-11 16:42:34 -08:00
|
|
|
LayerCache::LayerCache()
|
|
|
|
: mSize(0)
|
|
|
|
, mMaxSize(Properties::layerPoolSize) {}
|
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
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-09-03 07:39:53 -07:00
|
|
|
size_t LayerCache::getCount() {
|
|
|
|
return mCache.size();
|
|
|
|
}
|
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
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) {
|
2014-12-19 10:08:40 -08: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;
|
2015-08-20 15:14:06 -07:00
|
|
|
layer->state = Layer::State::DeletedFromCache;
|
2015-01-05 15:51:13 -08:00
|
|
|
layer->decStrong(nullptr);
|
2010-07-06 11:39:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerCache::clear() {
|
2015-07-29 16:51:05 -07:00
|
|
|
for (auto entry : mCache) {
|
|
|
|
deleteLayer(entry.mLayer);
|
2010-10-08 15:49:53 -07:00
|
|
|
}
|
2010-07-06 11:39:32 -07:00
|
|
|
mCache.clear();
|
|
|
|
}
|
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
Layer* LayerCache::get(RenderState& renderState, const uint32_t width, const uint32_t height) {
|
2015-01-05 15:51:13 -08:00
|
|
|
Layer* layer = nullptr;
|
2010-10-08 15:49:53 -07:00
|
|
|
|
|
|
|
LayerEntry entry(width, height);
|
2015-07-29 16:51:05 -07:00
|
|
|
auto iter = mCache.find(entry);
|
2010-07-08 11:45:51 -07:00
|
|
|
|
2015-07-29 16:51:05 -07:00
|
|
|
if (iter != mCache.end()) {
|
|
|
|
entry = *iter;
|
|
|
|
mCache.erase(iter);
|
2010-10-08 15:49:53 -07:00
|
|
|
|
|
|
|
layer = entry.mLayer;
|
2015-08-20 15:14:06 -07:00
|
|
|
layer->state = Layer::State::RemovedFromCache;
|
2011-07-07 20:50:11 -07:00
|
|
|
mSize -= layer->getWidth() * layer->getHeight() * 4;
|
2010-10-08 15:49:53 -07:00
|
|
|
|
2014-12-19 10:08:40 -08:00
|
|
|
LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
|
2010-07-08 11:45:51 -07:00
|
|
|
} else {
|
2014-12-19 10:08:40 -08:00
|
|
|
LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
|
2010-07-08 11:45:51 -07:00
|
|
|
|
2015-08-20 15:14:06 -07:00
|
|
|
layer = new Layer(Layer::Type::DisplayList, renderState, entry.mWidth, entry.mHeight);
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setBlend(true);
|
|
|
|
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
|
|
|
|
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() {
|
2015-07-29 16:51:05 -07:00
|
|
|
for (auto entry : mCache) {
|
2014-12-19 10:08:40 -08:00
|
|
|
ALOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
|
2011-07-26 20:35:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-07-29 16:51:05 -07:00
|
|
|
Layer* victim = mCache.begin()->mLayer;
|
2010-10-12 18:15:42 -07:00
|
|
|
deleteLayer(victim);
|
2015-07-29 16:51:05 -07:00
|
|
|
mCache.erase(mCache.begin());
|
2010-10-08 15:49:53 -07:00
|
|
|
|
2014-12-19 10:08:40 -08: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);
|
|
|
|
|
2015-07-29 16:51:05 -07:00
|
|
|
mCache.insert(entry);
|
2010-07-06 11:39:32 -07:00
|
|
|
mSize += size;
|
|
|
|
|
2015-08-20 15:14:06 -07:00
|
|
|
layer->state = Layer::State::InCache;
|
2010-07-06 11:39:32 -07:00
|
|
|
return true;
|
|
|
|
}
|
2014-09-10 13:04:31 -07:00
|
|
|
|
2015-08-20 15:14:06 -07:00
|
|
|
layer->state = Layer::State::FailedToCache;
|
2010-07-06 11:39:32 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|