2012-09-05 11:40:29 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 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 <utils/Log.h>
|
|
|
|
|
2013-03-26 15:05:58 -07:00
|
|
|
#include "DisplayList.h"
|
|
|
|
#include "DeferredDisplayList.h"
|
2012-09-05 11:40:29 -07:00
|
|
|
#include "Layer.h"
|
2012-09-26 10:27:40 -07:00
|
|
|
#include "LayerRenderer.h"
|
2012-09-05 11:40:29 -07:00
|
|
|
#include "OpenGLRenderer.h"
|
|
|
|
#include "Caches.h"
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
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::Layer(const uint32_t layerWidth, const uint32_t layerHeight) {
|
|
|
|
mesh = NULL;
|
|
|
|
meshIndices = NULL;
|
|
|
|
meshElementCount = 0;
|
|
|
|
cacheable = true;
|
2012-10-18 15:05:02 -07:00
|
|
|
dirty = false;
|
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
|
|
|
textureLayer = false;
|
|
|
|
renderTarget = GL_TEXTURE_2D;
|
|
|
|
texture.width = layerWidth;
|
|
|
|
texture.height = layerHeight;
|
|
|
|
colorFilter = NULL;
|
|
|
|
deferredUpdateScheduled = false;
|
|
|
|
renderer = NULL;
|
|
|
|
displayList = NULL;
|
|
|
|
fbo = 0;
|
2013-02-06 16:51:04 -08:00
|
|
|
stencil = NULL;
|
2012-11-29 17:52:58 -08:00
|
|
|
debugDrawUpdate = false;
|
2013-03-26 15:05:58 -07:00
|
|
|
deferredList = 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
|
|
|
Caches::getInstance().resourceCache.incrementRefcount(this);
|
|
|
|
}
|
|
|
|
|
2012-09-05 11:40:29 -07:00
|
|
|
Layer::~Layer() {
|
|
|
|
if (colorFilter) Caches::getInstance().resourceCache.decrementRefcount(colorFilter);
|
2012-09-26 10:27:40 -07:00
|
|
|
removeFbo();
|
2012-09-25 15:49:03 -07:00
|
|
|
deleteTexture();
|
2013-03-26 15:05:58 -07:00
|
|
|
|
|
|
|
delete[] mesh;
|
|
|
|
delete[] meshIndices;
|
|
|
|
delete deferredList;
|
2012-09-23 17:46:45 -07:00
|
|
|
}
|
|
|
|
|
2013-01-18 16:42:51 -08:00
|
|
|
uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
|
|
|
|
return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
|
|
|
|
return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Layer::resize(const uint32_t width, const uint32_t height) {
|
|
|
|
uint32_t desiredWidth = computeIdealWidth(width);
|
|
|
|
uint32_t desiredHeight = computeIdealWidth(height);
|
|
|
|
|
|
|
|
if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-28 11:32:33 -07:00
|
|
|
const uint32_t maxTextureSize = Caches::getInstance().maxTextureSize;
|
|
|
|
if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
|
|
|
|
ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
|
|
|
|
desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-18 16:42:51 -08:00
|
|
|
uint32_t oldWidth = getWidth();
|
|
|
|
uint32_t oldHeight = getHeight();
|
|
|
|
|
|
|
|
setSize(desiredWidth, desiredHeight);
|
|
|
|
|
|
|
|
if (fbo) {
|
|
|
|
Caches::getInstance().activeTexture(0);
|
|
|
|
bindTexture();
|
2013-04-04 12:27:54 -07:00
|
|
|
allocateTexture();
|
2013-01-18 16:42:51 -08:00
|
|
|
|
|
|
|
if (glGetError() != GL_NO_ERROR) {
|
|
|
|
setSize(oldWidth, oldHeight);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stencil) {
|
2013-02-06 16:51:04 -08:00
|
|
|
stencil->bind();
|
|
|
|
stencil->resize(desiredWidth, desiredHeight);
|
2013-01-18 16:42:51 -08:00
|
|
|
|
|
|
|
if (glGetError() != GL_NO_ERROR) {
|
|
|
|
setSize(oldWidth, oldHeight);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-15 18:51:42 -08:00
|
|
|
void Layer::removeFbo(bool flush) {
|
|
|
|
if (stencil) {
|
|
|
|
GLuint previousFbo;
|
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
|
|
|
|
if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
|
|
|
|
if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
|
|
|
|
|
2013-02-12 16:08:55 -08:00
|
|
|
Caches::getInstance().renderBufferCache.put(stencil);
|
2013-02-06 16:51:04 -08:00
|
|
|
stencil = NULL;
|
2013-01-15 18:51:42 -08:00
|
|
|
}
|
|
|
|
|
2012-09-26 10:27:40 -07:00
|
|
|
if (fbo) {
|
2013-01-15 18:51:42 -08:00
|
|
|
if (flush) LayerRenderer::flushLayer(this);
|
|
|
|
// If put fails the cache will delete the FBO
|
2012-09-26 10:27:40 -07:00
|
|
|
Caches::getInstance().fboCache.put(fbo);
|
|
|
|
fbo = 0;
|
2012-09-25 20:30:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-05 11:40:29 -07:00
|
|
|
void Layer::setPaint(SkPaint* paint) {
|
|
|
|
OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layer::setColorFilter(SkiaColorFilter* filter) {
|
|
|
|
if (colorFilter) {
|
|
|
|
Caches::getInstance().resourceCache.decrementRefcount(colorFilter);
|
|
|
|
}
|
|
|
|
colorFilter = filter;
|
|
|
|
if (colorFilter) {
|
|
|
|
Caches::getInstance().resourceCache.incrementRefcount(colorFilter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 15:05:58 -07:00
|
|
|
void Layer::defer() {
|
|
|
|
if (!deferredList) {
|
|
|
|
deferredList = new DeferredDisplayList;
|
|
|
|
}
|
|
|
|
DeferStateStruct deferredState(*deferredList, *renderer,
|
|
|
|
DisplayList::kReplayFlag_ClipChildren);
|
|
|
|
|
|
|
|
const float width = layer.getWidth();
|
|
|
|
const float height = layer.getHeight();
|
|
|
|
|
|
|
|
if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
|
|
|
|
dirtyRect.right >= width && dirtyRect.bottom >= height)) {
|
|
|
|
dirtyRect.set(0, 0, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer->initViewport(width, height);
|
|
|
|
renderer->setupFrameState(dirtyRect.left, dirtyRect.top,
|
|
|
|
dirtyRect.right, dirtyRect.bottom, !isBlend());
|
|
|
|
|
|
|
|
displayList->defer(deferredState, 0);
|
2013-03-29 12:37:16 -07:00
|
|
|
|
|
|
|
deferredUpdateScheduled = false;
|
2013-03-26 15:05:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Layer::flush() {
|
2013-03-28 13:06:58 -07:00
|
|
|
if (deferredList) {
|
2013-03-26 15:05:58 -07:00
|
|
|
renderer->setViewport(layer.getWidth(), layer.getHeight());
|
|
|
|
renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
|
|
|
|
!isBlend());
|
|
|
|
|
|
|
|
deferredList->flush(*renderer, dirtyRect);
|
|
|
|
|
|
|
|
renderer->finish();
|
|
|
|
renderer = NULL;
|
|
|
|
|
|
|
|
dirtyRect.setEmpty();
|
2013-04-04 14:46:24 -07:00
|
|
|
displayList = NULL;
|
2013-03-26 15:05:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 12:37:16 -07:00
|
|
|
void Layer::render() {
|
|
|
|
renderer->setViewport(layer.getWidth(), layer.getHeight());
|
|
|
|
renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
|
|
|
|
!isBlend());
|
|
|
|
|
|
|
|
renderer->drawDisplayList(displayList, dirtyRect, DisplayList::kReplayFlag_ClipChildren);
|
|
|
|
|
|
|
|
renderer->finish();
|
|
|
|
renderer = NULL;
|
|
|
|
|
|
|
|
dirtyRect.setEmpty();
|
|
|
|
|
|
|
|
deferredUpdateScheduled = false;
|
|
|
|
displayList = NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-05 11:40:29 -07:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|