blob: 192cedad016ab48debf84965522288e56c2dcb54 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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
17#define LOG_TAG "SurfaceFlinger"
18
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Errors.h>
24#include <utils/Log.h>
25
26#include "BlurFilter.h"
27#include "LayerBlur.h"
28#include "SurfaceFlinger.h"
29#include "DisplayHardware/DisplayHardware.h"
30
31namespace android {
32// ---------------------------------------------------------------------------
33
34const uint32_t LayerBlur::typeInfo = LayerBaseClient::typeInfo | 8;
35const char* const LayerBlur::typeID = "LayerBlur";
36
37// ---------------------------------------------------------------------------
38
39LayerBlur::LayerBlur(SurfaceFlinger* flinger, DisplayID display,
40 Client* client, int32_t i)
41 : LayerBaseClient(flinger, display, client, i), mCacheDirty(true),
42 mRefreshCache(true), mCacheAge(0), mTextureName(-1U)
43{
44}
45
46LayerBlur::~LayerBlur()
47{
48 if (mTextureName != -1U) {
49 //glDeleteTextures(1, &mTextureName);
50 deletedTextures.add(mTextureName);
51 }
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->invalidate = 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
139 Region::iterator iterator(clip);
140 if (iterator) {
141 glEnable(GL_TEXTURE_2D);
142 glBindTexture(GL_TEXTURE_2D, mTextureName);
143
144 if (mRefreshCache) {
145 mRefreshCache = false;
146 mAutoRefreshPending = false;
147
148 uint16_t* const pixels = (uint16_t*)malloc(w*h*2);
149
150 // this reads the frame-buffer, so a h/w GL would have to
151 // finish() its rendering first. we don't want to do that
152 // too often.
153 glReadPixels(X, Y, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
154
155 // blur that texture.
156 GGLSurface bl;
157 bl.version = sizeof(GGLSurface);
158 bl.width = w;
159 bl.height = h;
160 bl.stride = w;
161 bl.format = GGL_PIXEL_FORMAT_RGB_565;
162 bl.data = (GGLubyte*)pixels;
163 blurFilter(&bl, 8, 2);
164
165 // NOTE: this works only because we have POT. we'd have to round the
166 // texture size up, otherwise.
167 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
168 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
169
170 free((void*)pixels);
171 }
172
173 const State& s = drawingState();
174 if (UNLIKELY(s.alpha < 0xFF)) {
175 const GGLfixed alpha = (s.alpha << 16)/255;
176 glColor4x(0, 0, 0, alpha);
177 glEnable(GL_BLEND);
178 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
179 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
180 } else {
181 glDisable(GL_BLEND);
182 }
183
184 glDisable(GL_DITHER);
185 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
186 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
187 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
188
189 if (UNLIKELY(transformed()
190 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) )) {
191 // This is a very rare scenario.
192 glMatrixMode(GL_TEXTURE);
193 glLoadIdentity();
194 glScalef(1.0f/w, -1.0f/h, 1);
195 glTranslatef(-x, -y, 0);
196 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
197 glVertexPointer(2, GL_FIXED, 0, mVertices);
198 glTexCoordPointer(2, GL_FIXED, 0, mVertices);
199 Rect r;
200 while (iterator.iterate(&r)) {
201 const GLint sy = fbHeight - (r.top + r.height());
202 glScissor(r.left, sy, r.width(), r.height());
203 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
204 }
205 } else {
206 Region::iterator iterator(clip);
207 if (iterator) {
208 // NOTE: this is marginally faster with the software gl, because
209 // glReadPixels() reads the fb bottom-to-top, however we'll
210 // skip all the jaccobian computations.
211 Rect r;
212 GLint crop[4] = { 0, 0, w, h };
213 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
214 y = fbHeight - (y + h);
215 while (iterator.iterate(&r)) {
216 const GLint sy = fbHeight - (r.top + r.height());
217 glScissor(r.left, sy, r.width(), r.height());
218 glDrawTexiOES(x, y, 0, w, h);
219 }
220 }
221 }
222 }
223
224 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
225}
226
227// ---------------------------------------------------------------------------
228
229}; // namespace android