blob: 54d5c521413354c71a2333c8b8f975d2936d18cd [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
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,
41 Client* client, int32_t i)
42 : LayerBaseClient(flinger, display, client, i), mCacheDirty(true),
43 mRefreshCache(true), mCacheAge(0), mTextureName(-1U)
44{
45}
46
47LayerBlur::~LayerBlur()
48{
49 if (mTextureName != -1U) {
50 //glDeleteTextures(1, &mTextureName);
51 deletedTextures.add(mTextureName);
52 }
53}
54
55void LayerBlur::setVisibleRegion(const Region& visibleRegion)
56{
57 LayerBaseClient::setVisibleRegion(visibleRegion);
58 if (visibleRegionScreen.isEmpty()) {
59 if (mTextureName != -1U) {
60 // We're not visible, free the texture up.
61 glBindTexture(GL_TEXTURE_2D, 0);
62 glDeleteTextures(1, &mTextureName);
63 mTextureName = -1U;
64 }
65 }
66}
67
68uint32_t LayerBlur::doTransaction(uint32_t flags)
69{
70 // we're doing a transaction, refresh the cache!
71 if (!mFlinger->isFrozen()) {
72 mRefreshCache = true;
73 mCacheDirty = true;
74 flags |= eVisibleRegion;
75 this->contentDirty = true;
76 }
77 return LayerBase::doTransaction(flags);
78}
79
80void LayerBlur::unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion)
81{
82 // this code-path must be as tight as possible, it's called each time
83 // the screen is composited.
84 if (UNLIKELY(!visibleRegionScreen.isEmpty())) {
85 // if anything visible below us is invalidated, the cache becomes dirty
86 if (!mCacheDirty &&
87 !visibleRegionScreen.intersect(outDirtyRegion).isEmpty()) {
88 mCacheDirty = true;
89 }
90 if (mCacheDirty) {
91 if (!mFlinger->isFrozen()) {
92 // update everything below us that is visible
93 outDirtyRegion.orSelf(visibleRegionScreen);
94 nsecs_t now = systemTime();
95 if ((now - mCacheAge) >= ms2ns(500)) {
96 mCacheAge = now;
97 mRefreshCache = true;
98 mCacheDirty = false;
99 } else {
100 if (!mAutoRefreshPending) {
101 mFlinger->signalDelayedEvent(ms2ns(500));
102 mAutoRefreshPending = true;
103 }
104 }
105 }
106 }
107 }
108 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
109}
110
111void LayerBlur::onDraw(const Region& clip) const
112{
113 const DisplayHardware& hw(graphicPlane(0).displayHardware());
114 const uint32_t fbHeight = hw.getHeight();
115 int x = mTransformedBounds.left;
116 int y = mTransformedBounds.top;
117 int w = mTransformedBounds.width();
118 int h = mTransformedBounds.height();
119 GLint X = x;
120 GLint Y = fbHeight - (y + h);
121 if (X < 0) {
122 w += X;
123 X = 0;
124 }
125 if (Y < 0) {
126 h += Y;
127 Y = 0;
128 }
129 if (w<0 || h<0) {
130 // we're outside of the framebuffer
131 return;
132 }
133
134 if (mTextureName == -1U) {
135 // create the texture name the first time
136 // can't do that in the ctor, because it runs in another thread.
137 glGenTextures(1, &mTextureName);
138 }
139
140 Region::iterator iterator(clip);
141 if (iterator) {
142 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);
202 Rect r;
203 while (iterator.iterate(&r)) {
204 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 {
209 Region::iterator iterator(clip);
210 if (iterator) {
211 // NOTE: this is marginally faster with the software gl, because
212 // glReadPixels() reads the fb bottom-to-top, however we'll
213 // skip all the jaccobian computations.
214 Rect r;
215 GLint crop[4] = { 0, 0, w, h };
216 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
217 y = fbHeight - (y + h);
218 while (iterator.iterate(&r)) {
219 const GLint sy = fbHeight - (r.top + r.height());
220 glScissor(r.left, sy, r.width(), r.height());
221 glDrawTexiOES(x, y, 0, w, h);
222 }
223 }
224 }
225 }
226
227 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
228}
229
230// ---------------------------------------------------------------------------
231
232}; // namespace android