2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <utils/Errors.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
|
|
|
#include <GLES/gl.h>
|
|
|
|
#include <GLES/glext.h>
|
|
|
|
|
2009-09-10 00:47:15 +00:00
|
|
|
#include "clz.h"
|
2009-03-04 03:31:44 +00:00
|
|
|
#include "BlurFilter.h"
|
|
|
|
#include "LayerBlur.h"
|
|
|
|
#include "SurfaceFlinger.h"
|
|
|
|
#include "DisplayHardware/DisplayHardware.h"
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
LayerBlur::LayerBlur(SurfaceFlinger* flinger, DisplayID display,
|
2010-06-03 06:28:45 +00:00
|
|
|
const sp<Client>& client)
|
|
|
|
: LayerBaseClient(flinger, display, client), mCacheDirty(true),
|
2009-10-30 03:24:44 +00:00
|
|
|
mRefreshCache(true), mCacheAge(0), mTextureName(-1U),
|
|
|
|
mWidthScale(1.0f), mHeightScale(1.0f),
|
|
|
|
mBlurFormat(GGL_PIXEL_FORMAT_RGB_565)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerBlur::~LayerBlur()
|
|
|
|
{
|
|
|
|
if (mTextureName != -1U) {
|
2009-04-22 22:49:28 +00:00
|
|
|
glDeleteTextures(1, &mTextureName);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerBlur::setVisibleRegion(const Region& visibleRegion)
|
|
|
|
{
|
|
|
|
LayerBaseClient::setVisibleRegion(visibleRegion);
|
|
|
|
if (visibleRegionScreen.isEmpty()) {
|
|
|
|
if (mTextureName != -1U) {
|
|
|
|
// We're not visible, free the texture up.
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glDeleteTextures(1, &mTextureName);
|
|
|
|
mTextureName = -1U;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LayerBlur::doTransaction(uint32_t flags)
|
|
|
|
{
|
|
|
|
// we're doing a transaction, refresh the cache!
|
|
|
|
if (!mFlinger->isFrozen()) {
|
|
|
|
mRefreshCache = true;
|
|
|
|
mCacheDirty = true;
|
|
|
|
flags |= eVisibleRegion;
|
|
|
|
this->contentDirty = true;
|
|
|
|
}
|
|
|
|
return LayerBase::doTransaction(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerBlur::unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion)
|
|
|
|
{
|
|
|
|
// this code-path must be as tight as possible, it's called each time
|
|
|
|
// the screen is composited.
|
|
|
|
if (UNLIKELY(!visibleRegionScreen.isEmpty())) {
|
|
|
|
// if anything visible below us is invalidated, the cache becomes dirty
|
|
|
|
if (!mCacheDirty &&
|
|
|
|
!visibleRegionScreen.intersect(outDirtyRegion).isEmpty()) {
|
|
|
|
mCacheDirty = true;
|
|
|
|
}
|
|
|
|
if (mCacheDirty) {
|
|
|
|
if (!mFlinger->isFrozen()) {
|
|
|
|
// update everything below us that is visible
|
|
|
|
outDirtyRegion.orSelf(visibleRegionScreen);
|
|
|
|
nsecs_t now = systemTime();
|
|
|
|
if ((now - mCacheAge) >= ms2ns(500)) {
|
|
|
|
mCacheAge = now;
|
|
|
|
mRefreshCache = true;
|
|
|
|
mCacheDirty = false;
|
|
|
|
} else {
|
|
|
|
if (!mAutoRefreshPending) {
|
2010-05-19 00:06:55 +00:00
|
|
|
mFlinger->postMessageAsync(
|
|
|
|
new MessageBase(MessageQueue::INVALIDATE),
|
|
|
|
ms2ns(500));
|
2009-03-04 03:31:44 +00:00
|
|
|
mAutoRefreshPending = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerBlur::onDraw(const Region& clip) const
|
|
|
|
{
|
|
|
|
const DisplayHardware& hw(graphicPlane(0).displayHardware());
|
|
|
|
const uint32_t fbHeight = hw.getHeight();
|
|
|
|
int x = mTransformedBounds.left;
|
|
|
|
int y = mTransformedBounds.top;
|
|
|
|
int w = mTransformedBounds.width();
|
|
|
|
int h = mTransformedBounds.height();
|
|
|
|
GLint X = x;
|
|
|
|
GLint Y = fbHeight - (y + h);
|
|
|
|
if (X < 0) {
|
|
|
|
w += X;
|
|
|
|
X = 0;
|
|
|
|
}
|
|
|
|
if (Y < 0) {
|
|
|
|
h += Y;
|
|
|
|
Y = 0;
|
|
|
|
}
|
|
|
|
if (w<0 || h<0) {
|
|
|
|
// we're outside of the framebuffer
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTextureName == -1U) {
|
|
|
|
// create the texture name the first time
|
|
|
|
// can't do that in the ctor, because it runs in another thread.
|
|
|
|
glGenTextures(1, &mTextureName);
|
2009-09-17 00:00:19 +00:00
|
|
|
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, &mReadFormat);
|
|
|
|
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES, &mReadType);
|
|
|
|
if (mReadFormat != GL_RGB || mReadType != GL_UNSIGNED_SHORT_5_6_5) {
|
|
|
|
mReadFormat = GL_RGBA;
|
|
|
|
mReadType = GL_UNSIGNED_BYTE;
|
|
|
|
mBlurFormat = GGL_PIXEL_FORMAT_RGBX_8888;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 07:03:41 +00:00
|
|
|
Region::const_iterator it = clip.begin();
|
|
|
|
Region::const_iterator const end = clip.end();
|
|
|
|
if (it != end) {
|
2010-09-15 22:46:24 +00:00
|
|
|
#if defined(GL_OES_EGL_image_external)
|
2010-06-26 01:02:21 +00:00
|
|
|
if (GLExtensions::getInstance().haveTextureExternal()) {
|
|
|
|
glDisable(GL_TEXTURE_EXTERNAL_OES);
|
|
|
|
}
|
2010-06-15 04:20:00 +00:00
|
|
|
#endif
|
2009-03-04 03:31:44 +00:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mTextureName);
|
2009-09-17 00:00:19 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (mRefreshCache) {
|
|
|
|
mRefreshCache = false;
|
|
|
|
mAutoRefreshPending = false;
|
2009-09-17 00:00:19 +00:00
|
|
|
|
|
|
|
int32_t pixelSize = 4;
|
|
|
|
int32_t s = w;
|
|
|
|
if (mReadType == GL_UNSIGNED_SHORT_5_6_5) {
|
|
|
|
// allocate enough memory for 4-bytes (2 pixels) aligned data
|
|
|
|
s = (w + 1) & ~1;
|
|
|
|
pixelSize = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t* const pixels = (uint16_t*)malloc(s*h*pixelSize);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// This reads the frame-buffer, so a h/w GL would have to
|
|
|
|
// finish() its rendering first. we don't want to do that
|
|
|
|
// too often. Read data is 4-bytes aligned.
|
2009-09-17 00:00:19 +00:00
|
|
|
glReadPixels(X, Y, w, h, mReadFormat, mReadType, pixels);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// blur that texture.
|
|
|
|
GGLSurface bl;
|
|
|
|
bl.version = sizeof(GGLSurface);
|
|
|
|
bl.width = w;
|
|
|
|
bl.height = h;
|
|
|
|
bl.stride = s;
|
2009-09-17 00:00:19 +00:00
|
|
|
bl.format = mBlurFormat;
|
2009-03-04 03:31:44 +00:00
|
|
|
bl.data = (GGLubyte*)pixels;
|
|
|
|
blurFilter(&bl, 8, 2);
|
2009-09-10 00:47:15 +00:00
|
|
|
|
2010-06-26 01:02:21 +00:00
|
|
|
if (GLExtensions::getInstance().haveNpot()) {
|
2009-09-17 00:00:19 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, mReadFormat, w, h, 0,
|
|
|
|
mReadFormat, mReadType, pixels);
|
2009-09-10 00:47:15 +00:00
|
|
|
mWidthScale = 1.0f / w;
|
|
|
|
mHeightScale =-1.0f / h;
|
|
|
|
mYOffset = 0;
|
|
|
|
} else {
|
|
|
|
GLuint tw = 1 << (31 - clz(w));
|
|
|
|
GLuint th = 1 << (31 - clz(h));
|
2009-10-07 02:00:57 +00:00
|
|
|
if (tw < GLuint(w)) tw <<= 1;
|
|
|
|
if (th < GLuint(h)) th <<= 1;
|
2009-09-17 00:00:19 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, mReadFormat, tw, th, 0,
|
|
|
|
mReadFormat, mReadType, NULL);
|
2009-09-10 00:47:15 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
|
2009-09-17 00:00:19 +00:00
|
|
|
mReadFormat, mReadType, pixels);
|
2009-09-10 00:47:15 +00:00
|
|
|
mWidthScale = 1.0f / tw;
|
|
|
|
mHeightScale =-1.0f / th;
|
|
|
|
mYOffset = th-h;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
free((void*)pixels);
|
|
|
|
}
|
2009-09-17 00:00:19 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
const State& s = drawingState();
|
|
|
|
if (UNLIKELY(s.alpha < 0xFF)) {
|
2010-04-20 21:51:04 +00:00
|
|
|
const GLfloat alpha = s.alpha * (1.0f/255.0f);
|
|
|
|
glColor4f(0, 0, 0, alpha);
|
2009-03-04 03:31:44 +00:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:47:15 +00:00
|
|
|
if (mFlags & DisplayHardware::SLOW_CONFIG) {
|
|
|
|
glDisable(GL_DITHER);
|
|
|
|
} else {
|
|
|
|
glEnable(GL_DITHER);
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
|
|
|
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
|
2010-04-20 21:51:04 +00:00
|
|
|
glMatrixMode(GL_TEXTURE);
|
|
|
|
glLoadIdentity();
|
|
|
|
glScalef(mWidthScale, mHeightScale, 1);
|
|
|
|
glTranslatef(-x, mYOffset - y, 0);
|
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, mVertices);
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 0, mVertices);
|
|
|
|
while (it != end) {
|
|
|
|
const Rect& r = *it++;
|
|
|
|
const GLint sy = fbHeight - (r.top + r.height());
|
|
|
|
glScissor(r.left, sy, r.width(), r.height());
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-04-20 21:51:04 +00:00
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
2010-08-20 00:01:19 +00:00
|
|
|
glLoadIdentity();
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
}; // namespace android
|