blob: cac3cf10f0dfd4f36e594fe8cb4c687234d553a0 [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,
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) {
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
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 // allocate enough memory for 4-bytes (2 pixels) aligned data
149 const int32_t s = (w + 1) & ~1;
150 uint16_t* const pixels = (uint16_t*)malloc(s*h*2);
151
152 // This reads the frame-buffer, so a h/w GL would have to
153 // finish() its rendering first. we don't want to do that
154 // too often. Read data is 4-bytes aligned.
155 glReadPixels(X, Y, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
156
157 // blur that texture.
158 GGLSurface bl;
159 bl.version = sizeof(GGLSurface);
160 bl.width = w;
161 bl.height = h;
162 bl.stride = s;
163 bl.format = GGL_PIXEL_FORMAT_RGB_565;
164 bl.data = (GGLubyte*)pixels;
165 blurFilter(&bl, 8, 2);
166
167 // NOTE: this works only because we have POT. we'd have to round the
168 // texture size up, otherwise.
169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
170 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
171
172 free((void*)pixels);
173 }
174
175 const State& s = drawingState();
176 if (UNLIKELY(s.alpha < 0xFF)) {
177 const GGLfixed alpha = (s.alpha << 16)/255;
178 glColor4x(0, 0, 0, alpha);
179 glEnable(GL_BLEND);
180 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
181 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
182 } else {
183 glDisable(GL_BLEND);
184 }
185
186 glDisable(GL_DITHER);
187 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
188 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
189 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
190
191 if (UNLIKELY(transformed()
192 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) )) {
193 // This is a very rare scenario.
194 glMatrixMode(GL_TEXTURE);
195 glLoadIdentity();
196 glScalef(1.0f/w, -1.0f/h, 1);
197 glTranslatef(-x, -y, 0);
198 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
199 glVertexPointer(2, GL_FIXED, 0, mVertices);
200 glTexCoordPointer(2, GL_FIXED, 0, mVertices);
201 Rect r;
202 while (iterator.iterate(&r)) {
203 const GLint sy = fbHeight - (r.top + r.height());
204 glScissor(r.left, sy, r.width(), r.height());
205 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
206 }
207 } else {
208 Region::iterator iterator(clip);
209 if (iterator) {
210 // NOTE: this is marginally faster with the software gl, because
211 // glReadPixels() reads the fb bottom-to-top, however we'll
212 // skip all the jaccobian computations.
213 Rect r;
214 GLint crop[4] = { 0, 0, w, h };
215 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
216 y = fbHeight - (y + h);
217 while (iterator.iterate(&r)) {
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);
221 }
222 }
223 }
224 }
225
226 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
227}
228
229// ---------------------------------------------------------------------------
230
231}; // namespace android