blob: e14f35b7e09a8c8196862b301406d02bd7fbef80 [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
Mathias Agopianb6a5daa2009-09-09 17:47:15 -070027#include "clz.h"
The Android Open Source Projectedbf3b62009-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 Agopianf9d93272009-06-19 17:00:27 -070042 const sp<Client>& client, int32_t i)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043 : LayerBaseClient(flinger, display, client, i), mCacheDirty(true),
Mathias Agopianb6a5daa2009-09-09 17:47:15 -070044 mRefreshCache(true), mCacheAge(0), mTextureName(-1U),
45 mWidthScale(1.0f), mHeightScale(1.0f)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046{
47}
48
49LayerBlur::~LayerBlur()
50{
51 if (mTextureName != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070052 glDeleteTextures(1, &mTextureName);
The Android Open Source Projectedbf3b62009-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);
139 }
140
Mathias Agopian20f68782009-05-11 00:03:41 -0700141 Region::const_iterator it = clip.begin();
142 Region::const_iterator const end = clip.end();
143 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 glEnable(GL_TEXTURE_2D);
145 glBindTexture(GL_TEXTURE_2D, mTextureName);
146
147 if (mRefreshCache) {
148 mRefreshCache = false;
149 mAutoRefreshPending = false;
150
151 // allocate enough memory for 4-bytes (2 pixels) aligned data
152 const int32_t s = (w + 1) & ~1;
153 uint16_t* const pixels = (uint16_t*)malloc(s*h*2);
154
155 // This reads the frame-buffer, so a h/w GL would have to
156 // finish() its rendering first. we don't want to do that
157 // too often. Read data is 4-bytes aligned.
158 glReadPixels(X, Y, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
159
160 // blur that texture.
161 GGLSurface bl;
162 bl.version = sizeof(GGLSurface);
163 bl.width = w;
164 bl.height = h;
165 bl.stride = s;
166 bl.format = GGL_PIXEL_FORMAT_RGB_565;
167 bl.data = (GGLubyte*)pixels;
168 blurFilter(&bl, 8, 2);
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700169
170 if (mFlags & (DisplayHardware::NPOT_EXTENSION)) {
171 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
172 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
173 mWidthScale = 1.0f / w;
174 mHeightScale =-1.0f / h;
175 mYOffset = 0;
176 } else {
177 GLuint tw = 1 << (31 - clz(w));
178 GLuint th = 1 << (31 - clz(h));
179 if (tw < w) tw <<= 1;
180 if (th < h) th <<= 1;
181 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0,
182 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL);
183 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
184 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
185 mWidthScale = 1.0f / tw;
186 mHeightScale =-1.0f / th;
187 mYOffset = th-h;
188 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189
190 free((void*)pixels);
191 }
192
193 const State& s = drawingState();
194 if (UNLIKELY(s.alpha < 0xFF)) {
195 const GGLfixed alpha = (s.alpha << 16)/255;
196 glColor4x(0, 0, 0, alpha);
197 glEnable(GL_BLEND);
198 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
199 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
200 } else {
201 glDisable(GL_BLEND);
202 }
203
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700204 if (mFlags & DisplayHardware::SLOW_CONFIG) {
205 glDisable(GL_DITHER);
206 } else {
207 glEnable(GL_DITHER);
208 }
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
211 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
212 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
213
214 if (UNLIKELY(transformed()
215 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) )) {
216 // This is a very rare scenario.
217 glMatrixMode(GL_TEXTURE);
218 glLoadIdentity();
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700219 glScalef(mWidthScale, mHeightScale, 1);
220 glTranslatef(-x, mYOffset - y, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
222 glVertexPointer(2, GL_FIXED, 0, mVertices);
223 glTexCoordPointer(2, GL_FIXED, 0, mVertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700224 while (it != end) {
225 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 const GLint sy = fbHeight - (r.top + r.height());
227 glScissor(r.left, sy, r.width(), r.height());
228 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
229 }
Mathias Agopianb6a5daa2009-09-09 17:47:15 -0700230 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 } else {
Mathias Agopian20f68782009-05-11 00:03:41 -0700232 // NOTE: this is marginally faster with the software gl, because
233 // glReadPixels() reads the fb bottom-to-top, however we'll
234 // skip all the jaccobian computations.
235 Rect r;
236 GLint crop[4] = { 0, 0, w, h };
237 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
238 y = fbHeight - (y + h);
239 while (it != end) {
240 const Rect& r = *it++;
241 const GLint sy = fbHeight - (r.top + r.height());
242 glScissor(r.left, sy, r.width(), r.height());
243 glDrawTexiOES(x, y, 0, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 }
245 }
246 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247}
248
249// ---------------------------------------------------------------------------
250
251}; // namespace android