blob: 00abd5a162e5083385242762b796ecd74d329501 [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
27#include "BlurFilter.h"
28#include "LayerBlur.h"
29#include "SurfaceFlinger.h"
30#include "DisplayHardware/DisplayHardware.h"
31
32namespace android {
33// ---------------------------------------------------------------------------
34
35const uint32_t LayerBlur::typeInfo = LayerBaseClient::typeInfo | 8;
36const char* const LayerBlur::typeID = "LayerBlur";
37
38// ---------------------------------------------------------------------------
39
40LayerBlur::LayerBlur(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -070041 const sp<Client>& client, int32_t i)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 : LayerBaseClient(flinger, display, client, i), mCacheDirty(true),
43 mRefreshCache(true), mCacheAge(0), mTextureName(-1U)
44{
45}
46
47LayerBlur::~LayerBlur()
48{
49 if (mTextureName != -1U) {
Mathias Agopian81b0aa62009-04-22 15:49:28 -070050 glDeleteTextures(1, &mTextureName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 }
52}
53
54void LayerBlur::setVisibleRegion(const Region& visibleRegion)
55{
56 LayerBaseClient::setVisibleRegion(visibleRegion);
57 if (visibleRegionScreen.isEmpty()) {
58 if (mTextureName != -1U) {
59 // We're not visible, free the texture up.
60 glBindTexture(GL_TEXTURE_2D, 0);
61 glDeleteTextures(1, &mTextureName);
62 mTextureName = -1U;
63 }
64 }
65}
66
67uint32_t LayerBlur::doTransaction(uint32_t flags)
68{
69 // we're doing a transaction, refresh the cache!
70 if (!mFlinger->isFrozen()) {
71 mRefreshCache = true;
72 mCacheDirty = true;
73 flags |= eVisibleRegion;
74 this->contentDirty = true;
75 }
76 return LayerBase::doTransaction(flags);
77}
78
79void LayerBlur::unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion)
80{
81 // this code-path must be as tight as possible, it's called each time
82 // the screen is composited.
83 if (UNLIKELY(!visibleRegionScreen.isEmpty())) {
84 // if anything visible below us is invalidated, the cache becomes dirty
85 if (!mCacheDirty &&
86 !visibleRegionScreen.intersect(outDirtyRegion).isEmpty()) {
87 mCacheDirty = true;
88 }
89 if (mCacheDirty) {
90 if (!mFlinger->isFrozen()) {
91 // update everything below us that is visible
92 outDirtyRegion.orSelf(visibleRegionScreen);
93 nsecs_t now = systemTime();
94 if ((now - mCacheAge) >= ms2ns(500)) {
95 mCacheAge = now;
96 mRefreshCache = true;
97 mCacheDirty = false;
98 } else {
99 if (!mAutoRefreshPending) {
100 mFlinger->signalDelayedEvent(ms2ns(500));
101 mAutoRefreshPending = true;
102 }
103 }
104 }
105 }
106 }
107 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
108}
109
110void LayerBlur::onDraw(const Region& clip) const
111{
112 const DisplayHardware& hw(graphicPlane(0).displayHardware());
113 const uint32_t fbHeight = hw.getHeight();
114 int x = mTransformedBounds.left;
115 int y = mTransformedBounds.top;
116 int w = mTransformedBounds.width();
117 int h = mTransformedBounds.height();
118 GLint X = x;
119 GLint Y = fbHeight - (y + h);
120 if (X < 0) {
121 w += X;
122 X = 0;
123 }
124 if (Y < 0) {
125 h += Y;
126 Y = 0;
127 }
128 if (w<0 || h<0) {
129 // we're outside of the framebuffer
130 return;
131 }
132
133 if (mTextureName == -1U) {
134 // create the texture name the first time
135 // can't do that in the ctor, because it runs in another thread.
136 glGenTextures(1, &mTextureName);
137 }
138
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700139 Region::const_iterator it = clip.begin();
140 Region::const_iterator const end = clip.end();
141 if (it != end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 glEnable(GL_TEXTURE_2D);
143 glBindTexture(GL_TEXTURE_2D, mTextureName);
144
145 if (mRefreshCache) {
146 mRefreshCache = false;
147 mAutoRefreshPending = false;
148
149 // allocate enough memory for 4-bytes (2 pixels) aligned data
150 const int32_t s = (w + 1) & ~1;
151 uint16_t* const pixels = (uint16_t*)malloc(s*h*2);
152
153 // This reads the frame-buffer, so a h/w GL would have to
154 // finish() its rendering first. we don't want to do that
155 // too often. Read data is 4-bytes aligned.
156 glReadPixels(X, Y, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
157
158 // blur that texture.
159 GGLSurface bl;
160 bl.version = sizeof(GGLSurface);
161 bl.width = w;
162 bl.height = h;
163 bl.stride = s;
164 bl.format = GGL_PIXEL_FORMAT_RGB_565;
165 bl.data = (GGLubyte*)pixels;
166 blurFilter(&bl, 8, 2);
167
168 // NOTE: this works only because we have POT. we'd have to round the
169 // texture size up, otherwise.
170 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
171 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
172
173 free((void*)pixels);
174 }
175
176 const State& s = drawingState();
177 if (UNLIKELY(s.alpha < 0xFF)) {
178 const GGLfixed alpha = (s.alpha << 16)/255;
179 glColor4x(0, 0, 0, alpha);
180 glEnable(GL_BLEND);
181 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
182 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
183 } else {
184 glDisable(GL_BLEND);
185 }
186
187 glDisable(GL_DITHER);
188 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
189 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
190 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
191
192 if (UNLIKELY(transformed()
193 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) )) {
194 // This is a very rare scenario.
195 glMatrixMode(GL_TEXTURE);
196 glLoadIdentity();
197 glScalef(1.0f/w, -1.0f/h, 1);
198 glTranslatef(-x, -y, 0);
199 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
200 glVertexPointer(2, GL_FIXED, 0, mVertices);
201 glTexCoordPointer(2, GL_FIXED, 0, mVertices);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700202 while (it != end) {
203 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 const GLint sy = fbHeight - (r.top + r.height());
205 glScissor(r.left, sy, r.width(), r.height());
206 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
207 }
208 } else {
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700209 // NOTE: this is marginally faster with the software gl, because
210 // glReadPixels() reads the fb bottom-to-top, however we'll
211 // skip all the jaccobian computations.
212 Rect r;
213 GLint crop[4] = { 0, 0, w, h };
214 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
215 y = fbHeight - (y + h);
216 while (it != end) {
217 const Rect& r = *it++;
218 const GLint sy = fbHeight - (r.top + r.height());
219 glScissor(r.left, sy, r.width(), r.height());
220 glDrawTexiOES(x, y, 0, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222 }
223 }
224
225 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
226}
227
228// ---------------------------------------------------------------------------
229
230}; // namespace android