blob: 744f2e940d20c4691609e2a37832f7840dca2144 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopiane7c11d72009-09-09 17:47:15 -070027#include "clz.h"
The Android Open Source Project9066cfe2009-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
36const uint32_t LayerBlur::typeInfo = LayerBaseClient::typeInfo | 8;
37const char* const LayerBlur::typeID = "LayerBlur";
38
39// ---------------------------------------------------------------------------
40
41LayerBlur::LayerBlur(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -070042 const sp<Client>& client, int32_t i)
Mathias Agopian2c68dd02009-09-16 17:00:19 -070043: LayerBaseClient(flinger, display, client, i), mCacheDirty(true),
44mRefreshCache(true), mCacheAge(0), mTextureName(-1U),
45mWidthScale(1.0f), mHeightScale(1.0f)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046{
47}
48
49LayerBlur::~LayerBlur()
50{
51 if (mTextureName != -1U) {
Mathias Agopian81b0aa62009-04-22 15:49:28 -070052 glDeleteTextures(1, &mTextureName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 }
54}
55
56void LayerBlur::setVisibleRegion(const Region& visibleRegion)
57{
58 LayerBaseClient::setVisibleRegion(visibleRegion);
59 if (visibleRegionScreen.isEmpty()) {
60 if (mTextureName != -1U) {
61 // We're not visible, free the texture up.
62 glBindTexture(GL_TEXTURE_2D, 0);
63 glDeleteTextures(1, &mTextureName);
64 mTextureName = -1U;
65 }
66 }
67}
68
69uint32_t LayerBlur::doTransaction(uint32_t flags)
70{
71 // we're doing a transaction, refresh the cache!
72 if (!mFlinger->isFrozen()) {
73 mRefreshCache = true;
74 mCacheDirty = true;
75 flags |= eVisibleRegion;
76 this->contentDirty = true;
77 }
78 return LayerBase::doTransaction(flags);
79}
80
81void LayerBlur::unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion)
82{
83 // this code-path must be as tight as possible, it's called each time
84 // the screen is composited.
85 if (UNLIKELY(!visibleRegionScreen.isEmpty())) {
86 // if anything visible below us is invalidated, the cache becomes dirty
87 if (!mCacheDirty &&
88 !visibleRegionScreen.intersect(outDirtyRegion).isEmpty()) {
89 mCacheDirty = true;
90 }
91 if (mCacheDirty) {
92 if (!mFlinger->isFrozen()) {
93 // update everything below us that is visible
94 outDirtyRegion.orSelf(visibleRegionScreen);
95 nsecs_t now = systemTime();
96 if ((now - mCacheAge) >= ms2ns(500)) {
97 mCacheAge = now;
98 mRefreshCache = true;
99 mCacheDirty = false;
100 } else {
101 if (!mAutoRefreshPending) {
102 mFlinger->signalDelayedEvent(ms2ns(500));
103 mAutoRefreshPending = true;
104 }
105 }
106 }
107 }
108 }
109 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
110}
111
112void LayerBlur::onDraw(const Region& clip) const
113{
114 const DisplayHardware& hw(graphicPlane(0).displayHardware());
115 const uint32_t fbHeight = hw.getHeight();
116 int x = mTransformedBounds.left;
117 int y = mTransformedBounds.top;
118 int w = mTransformedBounds.width();
119 int h = mTransformedBounds.height();
120 GLint X = x;
121 GLint Y = fbHeight - (y + h);
122 if (X < 0) {
123 w += X;
124 X = 0;
125 }
126 if (Y < 0) {
127 h += Y;
128 Y = 0;
129 }
130 if (w<0 || h<0) {
131 // we're outside of the framebuffer
132 return;
133 }
134
135 if (mTextureName == -1U) {
136 // create the texture name the first time
137 // can't do that in the ctor, because it runs in another thread.
138 glGenTextures(1, &mTextureName);
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700139 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, &mReadFormat);
140 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES, &mReadType);
141 if (mReadFormat != GL_RGB || mReadType != GL_UNSIGNED_SHORT_5_6_5) {
142 mReadFormat = GL_RGBA;
143 mReadType = GL_UNSIGNED_BYTE;
144 mBlurFormat = GGL_PIXEL_FORMAT_RGBX_8888;
145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700148 Region::const_iterator it = clip.begin();
149 Region::const_iterator const end = clip.end();
150 if (it != end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 glEnable(GL_TEXTURE_2D);
152 glBindTexture(GL_TEXTURE_2D, mTextureName);
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 if (mRefreshCache) {
155 mRefreshCache = false;
156 mAutoRefreshPending = false;
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700157
158 int32_t pixelSize = 4;
159 int32_t s = w;
160 if (mReadType == GL_UNSIGNED_SHORT_5_6_5) {
161 // allocate enough memory for 4-bytes (2 pixels) aligned data
162 s = (w + 1) & ~1;
163 pixelSize = 2;
164 }
165
166 uint16_t* const pixels = (uint16_t*)malloc(s*h*pixelSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168 // This reads the frame-buffer, so a h/w GL would have to
169 // finish() its rendering first. we don't want to do that
170 // too often. Read data is 4-bytes aligned.
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700171 glReadPixels(X, Y, w, h, mReadFormat, mReadType, pixels);
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 // blur that texture.
174 GGLSurface bl;
175 bl.version = sizeof(GGLSurface);
176 bl.width = w;
177 bl.height = h;
178 bl.stride = s;
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700179 bl.format = mBlurFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 bl.data = (GGLubyte*)pixels;
181 blurFilter(&bl, 8, 2);
Mathias Agopiane7c11d72009-09-09 17:47:15 -0700182
183 if (mFlags & (DisplayHardware::NPOT_EXTENSION)) {
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700184 glTexImage2D(GL_TEXTURE_2D, 0, mReadFormat, w, h, 0,
185 mReadFormat, mReadType, pixels);
Mathias Agopiane7c11d72009-09-09 17:47:15 -0700186 mWidthScale = 1.0f / w;
187 mHeightScale =-1.0f / h;
188 mYOffset = 0;
189 } else {
190 GLuint tw = 1 << (31 - clz(w));
191 GLuint th = 1 << (31 - clz(h));
Mathias Agopian4961c952009-10-06 19:00:57 -0700192 if (tw < GLuint(w)) tw <<= 1;
193 if (th < GLuint(h)) th <<= 1;
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700194 glTexImage2D(GL_TEXTURE_2D, 0, mReadFormat, tw, th, 0,
195 mReadFormat, mReadType, NULL);
Mathias Agopiane7c11d72009-09-09 17:47:15 -0700196 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700197 mReadFormat, mReadType, pixels);
Mathias Agopiane7c11d72009-09-09 17:47:15 -0700198 mWidthScale = 1.0f / tw;
199 mHeightScale =-1.0f / th;
200 mYOffset = th-h;
201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
203 free((void*)pixels);
204 }
Mathias Agopian2c68dd02009-09-16 17:00:19 -0700205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 const State& s = drawingState();
207 if (UNLIKELY(s.alpha < 0xFF)) {
208 const GGLfixed alpha = (s.alpha << 16)/255;
209 glColor4x(0, 0, 0, alpha);
210 glEnable(GL_BLEND);
211 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
212 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
213 } else {
214 glDisable(GL_BLEND);
215 }
216
Mathias Agopiane7c11d72009-09-09 17:47:15 -0700217 if (mFlags & DisplayHardware::SLOW_CONFIG) {
218 glDisable(GL_DITHER);
219 } else {
220 glEnable(GL_DITHER);
221 }
222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
224 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
225 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
226
227 if (UNLIKELY(transformed()
228 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) )) {
229 // This is a very rare scenario.
230 glMatrixMode(GL_TEXTURE);
231 glLoadIdentity();
Mathias Agopiane7c11d72009-09-09 17:47:15 -0700232 glScalef(mWidthScale, mHeightScale, 1);
233 glTranslatef(-x, mYOffset - y, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
235 glVertexPointer(2, GL_FIXED, 0, mVertices);
236 glTexCoordPointer(2, GL_FIXED, 0, mVertices);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700237 while (it != end) {
238 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 const GLint sy = fbHeight - (r.top + r.height());
240 glScissor(r.left, sy, r.width(), r.height());
241 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
242 }
Mathias Agopiane7c11d72009-09-09 17:47:15 -0700243 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 } else {
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700245 // NOTE: this is marginally faster with the software gl, because
246 // glReadPixels() reads the fb bottom-to-top, however we'll
247 // skip all the jaccobian computations.
248 Rect r;
249 GLint crop[4] = { 0, 0, w, h };
250 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
251 y = fbHeight - (y + h);
252 while (it != end) {
253 const Rect& r = *it++;
254 const GLint sy = fbHeight - (r.top + r.height());
255 glScissor(r.left, sy, r.width(), r.height());
256 glDrawTexiOES(x, y, 0, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
258 }
259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260}
261
262// ---------------------------------------------------------------------------
263
264}; // namespace android