blob: 2d778763ee555006bfa533263272a000ee2bae06 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22#include <utils/Log.h>
23
24#include <GLES/gl.h>
25#include <GLES/glext.h>
26
Mathias Agopianb6a5daa2009-09-09 17:47:15 -070027#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include "BlurFilter.h"
29#include "LayerBlur.h"
30#include "SurfaceFlinger.h"
31#include "DisplayHardware/DisplayHardware.h"
32
33namespace android {
34// ---------------------------------------------------------------------------
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036LayerBlur::LayerBlur(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -070037 const sp<Client>& client, int32_t i)
Mathias Agopian8889a812009-10-29 20:24:44 -070038 : LayerBaseClient(flinger, display, client, i), mCacheDirty(true),
39 mRefreshCache(true), mCacheAge(0), mTextureName(-1U),
40 mWidthScale(1.0f), mHeightScale(1.0f),
41 mBlurFormat(GGL_PIXEL_FORMAT_RGB_565)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042{
43}
44
45LayerBlur::~LayerBlur()
46{
47 if (mTextureName != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070048 glDeleteTextures(1, &mTextureName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 }
50}
51
52void LayerBlur::setVisibleRegion(const Region& visibleRegion)
53{
54 LayerBaseClient::setVisibleRegion(visibleRegion);
55 if (visibleRegionScreen.isEmpty()) {
56 if (mTextureName != -1U) {
57 // We're not visible, free the texture up.
58 glBindTexture(GL_TEXTURE_2D, 0);
59 glDeleteTextures(1, &mTextureName);
60 mTextureName = -1U;
61 }
62 }
63}
64
65uint32_t LayerBlur::doTransaction(uint32_t flags)
66{
67 // we're doing a transaction, refresh the cache!
68 if (!mFlinger->isFrozen()) {
69 mRefreshCache = true;
70 mCacheDirty = true;
71 flags |= eVisibleRegion;
72 this->contentDirty = true;
73 }
74 return LayerBase::doTransaction(flags);
75}
76
77void LayerBlur::unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion)
78{
79 // this code-path must be as tight as possible, it's called each time
80 // the screen is composited.
81 if (UNLIKELY(!visibleRegionScreen.isEmpty())) {
82 // if anything visible below us is invalidated, the cache becomes dirty
83 if (!mCacheDirty &&
84 !visibleRegionScreen.intersect(outDirtyRegion).isEmpty()) {
85 mCacheDirty = true;
86 }
87 if (mCacheDirty) {
88 if (!mFlinger->isFrozen()) {
89 // update everything below us that is visible
90 outDirtyRegion.orSelf(visibleRegionScreen);
91 nsecs_t now = systemTime();
92 if ((now - mCacheAge) >= ms2ns(500)) {
93 mCacheAge = now;
94 mRefreshCache = true;
95 mCacheDirty = false;
96 } else {
97 if (!mAutoRefreshPending) {
98 mFlinger->signalDelayedEvent(ms2ns(500));
99 mAutoRefreshPending = true;
100 }
101 }
102 }
103 }
104 }
105 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
106}
107
108void LayerBlur::onDraw(const Region& clip) const
109{
110 const DisplayHardware& hw(graphicPlane(0).displayHardware());
111 const uint32_t fbHeight = hw.getHeight();
112 int x = mTransformedBounds.left;
113 int y = mTransformedBounds.top;
114 int w = mTransformedBounds.width();
115 int h = mTransformedBounds.height();
116 GLint X = x;
117 GLint Y = fbHeight - (y + h);
118 if (X < 0) {
119 w += X;
120 X = 0;
121 }
122 if (Y < 0) {
123 h += Y;
124 Y = 0;
125 }
126 if (w<0 || h<0) {
127 // we're outside of the framebuffer
128 return;
129 }
130
131 if (mTextureName == -1U) {
132 // create the texture name the first time
133 // can't do that in the ctor, because it runs in another thread.
134 glGenTextures(1, &mTextureName);
Mathias Agopian378532f2009-09-16 17:00:19 -0700135 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, &mReadFormat);
136 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES, &mReadType);
137 if (mReadFormat != GL_RGB || mReadType != GL_UNSIGNED_SHORT_5_6_5) {
138 mReadFormat = GL_RGBA;
139 mReadType = GL_UNSIGNED_BYTE;
140 mBlurFormat = GGL_PIXEL_FORMAT_RGBX_8888;
141 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 }
143
Mathias Agopian20f68782009-05-11 00:03:41 -0700144 Region::const_iterator it = clip.begin();
145 Region::const_iterator const end = clip.end();
146 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 glEnable(GL_TEXTURE_2D);
148 glBindTexture(GL_TEXTURE_2D, mTextureName);
Mathias Agopian378532f2009-09-16 17:00:19 -0700149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 if (mRefreshCache) {
151 mRefreshCache = false;
152 mAutoRefreshPending = false;
Mathias Agopian378532f2009-09-16 17:00:19 -0700153
154 int32_t pixelSize = 4;
155 int32_t s = w;
156 if (mReadType == GL_UNSIGNED_SHORT_5_6_5) {
157 // allocate enough memory for 4-bytes (2 pixels) aligned data
158 s = (w + 1) & ~1;
159 pixelSize = 2;
160 }
161
162 uint16_t* const pixels = (uint16_t*)malloc(s*h*pixelSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163
164 // This reads the frame-buffer, so a h/w GL would have to
165 // finish() its rendering first. we don't want to do that
166 // too often. Read data is 4-bytes aligned.
Mathias Agopian378532f2009-09-16 17:00:19 -0700167 glReadPixels(X, Y, w, h, mReadFormat, mReadType, pixels);
168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 // blur that texture.
170 GGLSurface bl;
171 bl.version = sizeof(GGLSurface);
172 bl.width = w;
173 bl.height = h;
174 bl.stride = s;
Mathias Agopian378532f2009-09-16 17:00:19 -0700175 bl.format = mBlurFormat;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 bl.data = (GGLubyte*)pixels;
177 blurFilter(&bl, 8, 2);
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700178
179 if (mFlags & (DisplayHardware::NPOT_EXTENSION)) {
Mathias Agopian378532f2009-09-16 17:00:19 -0700180 glTexImage2D(GL_TEXTURE_2D, 0, mReadFormat, w, h, 0,
181 mReadFormat, mReadType, pixels);
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700182 mWidthScale = 1.0f / w;
183 mHeightScale =-1.0f / h;
184 mYOffset = 0;
185 } else {
186 GLuint tw = 1 << (31 - clz(w));
187 GLuint th = 1 << (31 - clz(h));
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700188 if (tw < GLuint(w)) tw <<= 1;
189 if (th < GLuint(h)) th <<= 1;
Mathias Agopian378532f2009-09-16 17:00:19 -0700190 glTexImage2D(GL_TEXTURE_2D, 0, mReadFormat, tw, th, 0,
191 mReadFormat, mReadType, NULL);
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700192 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
Mathias Agopian378532f2009-09-16 17:00:19 -0700193 mReadFormat, mReadType, pixels);
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700194 mWidthScale = 1.0f / tw;
195 mHeightScale =-1.0f / th;
196 mYOffset = th-h;
197 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198
199 free((void*)pixels);
200 }
Mathias Agopian378532f2009-09-16 17:00:19 -0700201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 const State& s = drawingState();
203 if (UNLIKELY(s.alpha < 0xFF)) {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700204 const GLfloat alpha = s.alpha * (1.0f/255.0f);
205 glColor4f(0, 0, 0, alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 glEnable(GL_BLEND);
207 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
208 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
209 } else {
210 glDisable(GL_BLEND);
211 }
212
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700213 if (mFlags & DisplayHardware::SLOW_CONFIG) {
214 glDisable(GL_DITHER);
215 } else {
216 glEnable(GL_DITHER);
217 }
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
220 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
221 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
222
Mathias Agopian78fd5012010-04-20 14:51:04 -0700223 glMatrixMode(GL_TEXTURE);
224 glLoadIdentity();
225 glScalef(mWidthScale, mHeightScale, 1);
226 glTranslatef(-x, mYOffset - y, 0);
227 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
228 glVertexPointer(2, GL_FLOAT, 0, mVertices);
229 glTexCoordPointer(2, GL_FLOAT, 0, mVertices);
230 while (it != end) {
231 const Rect& r = *it++;
232 const GLint sy = fbHeight - (r.top + r.height());
233 glScissor(r.left, sy, r.width(), r.height());
234 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700236 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238}
239
240// ---------------------------------------------------------------------------
241
242}; // namespace android