| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #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 Agopian | b6a5daa | 2009-09-09 17:47:15 -0700 | [diff] [blame] | 27 | #include "clz.h" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include "BlurFilter.h" | 
|  | 29 | #include "LayerBlur.h" | 
|  | 30 | #include "SurfaceFlinger.h" | 
|  | 31 | #include "DisplayHardware/DisplayHardware.h" | 
|  | 32 |  | 
|  | 33 | namespace android { | 
|  | 34 | // --------------------------------------------------------------------------- | 
|  | 35 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | LayerBlur::LayerBlur(SurfaceFlinger* flinger, DisplayID display, | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 37 | const sp<Client>& client) | 
|  | 38 | : LayerBaseClient(flinger, display, client), mCacheDirty(true), | 
| Mathias Agopian | 8889a81 | 2009-10-29 20:24:44 -0700 | [diff] [blame] | 39 | mRefreshCache(true), mCacheAge(0), mTextureName(-1U), | 
|  | 40 | mWidthScale(1.0f), mHeightScale(1.0f), | 
|  | 41 | mBlurFormat(GGL_PIXEL_FORMAT_RGB_565) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | { | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | LayerBlur::~LayerBlur() | 
|  | 46 | { | 
|  | 47 | if (mTextureName != -1U) { | 
| Mathias Agopian | 550b79f | 2009-04-22 15:49:28 -0700 | [diff] [blame] | 48 | glDeleteTextures(1, &mTextureName); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | } | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | void LayerBlur::setVisibleRegion(const Region& visibleRegion) | 
|  | 53 | { | 
|  | 54 | LayerBaseClient::setVisibleRegion(visibleRegion); | 
|  | 55 | if (visibleRegionScreen.isEmpty()) { | 
|  | 56 | if (mTextureName != -1U) { | 
|  | 57 | // We're not visible, free the texture up. | 
|  | 58 | glBindTexture(GL_TEXTURE_2D, 0); | 
|  | 59 | glDeleteTextures(1, &mTextureName); | 
|  | 60 | mTextureName = -1U; | 
|  | 61 | } | 
|  | 62 | } | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | uint32_t LayerBlur::doTransaction(uint32_t flags) | 
|  | 66 | { | 
|  | 67 | // we're doing a transaction, refresh the cache! | 
|  | 68 | if (!mFlinger->isFrozen()) { | 
|  | 69 | mRefreshCache = true; | 
|  | 70 | mCacheDirty = true; | 
|  | 71 | flags |= eVisibleRegion; | 
|  | 72 | this->contentDirty = true; | 
|  | 73 | } | 
|  | 74 | return LayerBase::doTransaction(flags); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | void LayerBlur::unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion) | 
|  | 78 | { | 
|  | 79 | // this code-path must be as tight as possible, it's called each time | 
|  | 80 | // the screen is composited. | 
|  | 81 | if (UNLIKELY(!visibleRegionScreen.isEmpty())) { | 
|  | 82 | // if anything visible below us is invalidated, the cache becomes dirty | 
|  | 83 | if (!mCacheDirty && | 
|  | 84 | !visibleRegionScreen.intersect(outDirtyRegion).isEmpty()) { | 
|  | 85 | mCacheDirty = true; | 
|  | 86 | } | 
|  | 87 | if (mCacheDirty) { | 
|  | 88 | if (!mFlinger->isFrozen()) { | 
|  | 89 | // update everything below us that is visible | 
|  | 90 | outDirtyRegion.orSelf(visibleRegionScreen); | 
|  | 91 | nsecs_t now = systemTime(); | 
|  | 92 | if ((now - mCacheAge) >= ms2ns(500)) { | 
|  | 93 | mCacheAge = now; | 
|  | 94 | mRefreshCache = true; | 
|  | 95 | mCacheDirty = false; | 
|  | 96 | } else { | 
|  | 97 | if (!mAutoRefreshPending) { | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 98 | mFlinger->postMessageAsync( | 
|  | 99 | new MessageBase(MessageQueue::INVALIDATE), | 
|  | 100 | ms2ns(500)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | mAutoRefreshPending = true; | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 | } | 
|  | 106 | } | 
|  | 107 | LayerBase::unlockPageFlip(planeTransform, outDirtyRegion); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | void 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); | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 137 | glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, &mReadFormat); | 
|  | 138 | glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES, &mReadType); | 
|  | 139 | if (mReadFormat != GL_RGB || mReadType != GL_UNSIGNED_SHORT_5_6_5) { | 
|  | 140 | mReadFormat = GL_RGBA; | 
|  | 141 | mReadType = GL_UNSIGNED_BYTE; | 
|  | 142 | mBlurFormat = GGL_PIXEL_FORMAT_RGBX_8888; | 
|  | 143 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 146 | Region::const_iterator it = clip.begin(); | 
|  | 147 | Region::const_iterator const end = clip.end(); | 
|  | 148 | if (it != end) { | 
| Michael I. Gold | 7f198b6 | 2010-09-15 15:46:24 -0700 | [diff] [blame] | 149 | #if defined(GL_OES_EGL_image_external) | 
| Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 150 | if (GLExtensions::getInstance().haveTextureExternal()) { | 
|  | 151 | glDisable(GL_TEXTURE_EXTERNAL_OES); | 
|  | 152 | } | 
| Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 153 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | glEnable(GL_TEXTURE_2D); | 
|  | 155 | glBindTexture(GL_TEXTURE_2D, mTextureName); | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 156 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | if (mRefreshCache) { | 
|  | 158 | mRefreshCache = false; | 
|  | 159 | mAutoRefreshPending = false; | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 160 |  | 
|  | 161 | int32_t pixelSize = 4; | 
|  | 162 | int32_t s = w; | 
|  | 163 | if (mReadType == GL_UNSIGNED_SHORT_5_6_5) { | 
|  | 164 | // allocate enough memory for 4-bytes (2 pixels) aligned data | 
|  | 165 | s = (w + 1) & ~1; | 
|  | 166 | pixelSize = 2; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | uint16_t* const pixels = (uint16_t*)malloc(s*h*pixelSize); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 |  | 
|  | 171 | // This reads the frame-buffer, so a h/w GL would have to | 
|  | 172 | // finish() its rendering first. we don't want to do that | 
|  | 173 | // too often. Read data is 4-bytes aligned. | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 174 | glReadPixels(X, Y, w, h, mReadFormat, mReadType, pixels); | 
|  | 175 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | // blur that texture. | 
|  | 177 | GGLSurface bl; | 
|  | 178 | bl.version = sizeof(GGLSurface); | 
|  | 179 | bl.width = w; | 
|  | 180 | bl.height = h; | 
|  | 181 | bl.stride = s; | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 182 | bl.format = mBlurFormat; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | bl.data = (GGLubyte*)pixels; | 
|  | 184 | blurFilter(&bl, 8, 2); | 
| Mathias Agopian | b6a5daa | 2009-09-09 17:47:15 -0700 | [diff] [blame] | 185 |  | 
| Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 186 | if (GLExtensions::getInstance().haveNpot()) { | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 187 | glTexImage2D(GL_TEXTURE_2D, 0, mReadFormat, w, h, 0, | 
|  | 188 | mReadFormat, mReadType, pixels); | 
| Mathias Agopian | b6a5daa | 2009-09-09 17:47:15 -0700 | [diff] [blame] | 189 | mWidthScale  = 1.0f / w; | 
|  | 190 | mHeightScale =-1.0f / h; | 
|  | 191 | mYOffset = 0; | 
|  | 192 | } else { | 
|  | 193 | GLuint tw = 1 << (31 - clz(w)); | 
|  | 194 | GLuint th = 1 << (31 - clz(h)); | 
| Mathias Agopian | 9ec430a | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 195 | if (tw < GLuint(w)) tw <<= 1; | 
|  | 196 | if (th < GLuint(h)) th <<= 1; | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 197 | glTexImage2D(GL_TEXTURE_2D, 0, mReadFormat, tw, th, 0, | 
|  | 198 | mReadFormat, mReadType, NULL); | 
| Mathias Agopian | b6a5daa | 2009-09-09 17:47:15 -0700 | [diff] [blame] | 199 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 200 | mReadFormat, mReadType, pixels); | 
| Mathias Agopian | b6a5daa | 2009-09-09 17:47:15 -0700 | [diff] [blame] | 201 | mWidthScale  = 1.0f / tw; | 
|  | 202 | mHeightScale =-1.0f / th; | 
|  | 203 | mYOffset = th-h; | 
|  | 204 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 |  | 
|  | 206 | free((void*)pixels); | 
|  | 207 | } | 
| Mathias Agopian | 378532f | 2009-09-16 17:00:19 -0700 | [diff] [blame] | 208 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | const State& s = drawingState(); | 
|  | 210 | if (UNLIKELY(s.alpha < 0xFF)) { | 
| Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 211 | const GLfloat alpha = s.alpha * (1.0f/255.0f); | 
|  | 212 | glColor4f(0, 0, 0, alpha); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | glEnable(GL_BLEND); | 
|  | 214 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|  | 215 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); | 
|  | 216 | } else { | 
|  | 217 | glDisable(GL_BLEND); | 
|  | 218 | } | 
|  | 219 |  | 
| Mathias Agopian | b6a5daa | 2009-09-09 17:47:15 -0700 | [diff] [blame] | 220 | if (mFlags & DisplayHardware::SLOW_CONFIG) { | 
|  | 221 | glDisable(GL_DITHER); | 
|  | 222 | } else { | 
|  | 223 | glEnable(GL_DITHER); | 
|  | 224 | } | 
|  | 225 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); | 
|  | 227 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
|  | 228 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
|  | 229 |  | 
| Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 230 | glMatrixMode(GL_TEXTURE); | 
|  | 231 | glLoadIdentity(); | 
|  | 232 | glScalef(mWidthScale, mHeightScale, 1); | 
|  | 233 | glTranslatef(-x, mYOffset - y, 0); | 
|  | 234 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|  | 235 | glVertexPointer(2, GL_FLOAT, 0, mVertices); | 
|  | 236 | glTexCoordPointer(2, GL_FLOAT, 0, mVertices); | 
|  | 237 | while (it != end) { | 
|  | 238 | const Rect& r = *it++; | 
|  | 239 | const GLint sy = fbHeight - (r.top + r.height()); | 
|  | 240 | glScissor(r.left, sy, r.width(), r.height()); | 
|  | 241 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } | 
| Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 243 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); | 
| Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 244 | glLoadIdentity(); | 
|  | 245 | glMatrixMode(GL_MODELVIEW); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
|  | 249 | // --------------------------------------------------------------------------- | 
|  | 250 |  | 
|  | 251 | }; // namespace android |