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 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 <GLES/gl.h> 27#include <GLES/glext.h> 28 29#include "BlurFilter.h" 30#include "LayerBlur.h" 31#include "SurfaceFlinger.h" 32#include "DisplayHardware/DisplayHardware.h" 33 34namespace android { 35// --------------------------------------------------------------------------- 36 37const uint32_t LayerBlur::typeInfo = LayerBaseClient::typeInfo | 8; 38const char* const LayerBlur::typeID = "LayerBlur"; 39 40// --------------------------------------------------------------------------- 41 42LayerBlur::LayerBlur(SurfaceFlinger* flinger, DisplayID display, 43 Client* client, int32_t i) 44 : LayerBaseClient(flinger, display, client, i), mCacheDirty(true), 45 mRefreshCache(true), mCacheAge(0), mTextureName(-1U) 46{ 47} 48 49LayerBlur::~LayerBlur() 50{ 51 if (mTextureName != -1U) { 52 //glDeleteTextures(1, &mTextureName); 53 deletedTextures.add(mTextureName); 54 } 55} 56 57void LayerBlur::setVisibleRegion(const Region& visibleRegion) 58{ 59 LayerBaseClient::setVisibleRegion(visibleRegion); 60 if (visibleRegionScreen.isEmpty()) { 61 if (mTextureName != -1U) { 62 // We're not visible, free the texture up. 63 glBindTexture(GL_TEXTURE_2D, 0); 64 glDeleteTextures(1, &mTextureName); 65 mTextureName = -1U; 66 } 67 } 68} 69 70uint32_t LayerBlur::doTransaction(uint32_t flags) 71{ 72 // we're doing a transaction, refresh the cache! 73 if (!mFlinger->isFrozen()) { 74 mRefreshCache = true; 75 mCacheDirty = true; 76 flags |= eVisibleRegion; 77 this->contentDirty = true; 78 } 79 return LayerBase::doTransaction(flags); 80} 81 82void LayerBlur::unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion) 83{ 84 // this code-path must be as tight as possible, it's called each time 85 // the screen is composited. 86 if (UNLIKELY(!visibleRegionScreen.isEmpty())) { 87 // if anything visible below us is invalidated, the cache becomes dirty 88 if (!mCacheDirty && 89 !visibleRegionScreen.intersect(outDirtyRegion).isEmpty()) { 90 mCacheDirty = true; 91 } 92 if (mCacheDirty) { 93 if (!mFlinger->isFrozen()) { 94 // update everything below us that is visible 95 outDirtyRegion.orSelf(visibleRegionScreen); 96 nsecs_t now = systemTime(); 97 if ((now - mCacheAge) >= ms2ns(500)) { 98 mCacheAge = now; 99 mRefreshCache = true; 100 mCacheDirty = false; 101 } else { 102 if (!mAutoRefreshPending) { 103 mFlinger->signalDelayedEvent(ms2ns(500)); 104 mAutoRefreshPending = true; 105 } 106 } 107 } 108 } 109 } 110 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion); 111} 112 113void LayerBlur::onDraw(const Region& clip) const 114{ 115 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 116 const uint32_t fbHeight = hw.getHeight(); 117 int x = mTransformedBounds.left; 118 int y = mTransformedBounds.top; 119 int w = mTransformedBounds.width(); 120 int h = mTransformedBounds.height(); 121 GLint X = x; 122 GLint Y = fbHeight - (y + h); 123 if (X < 0) { 124 w += X; 125 X = 0; 126 } 127 if (Y < 0) { 128 h += Y; 129 Y = 0; 130 } 131 if (w<0 || h<0) { 132 // we're outside of the framebuffer 133 return; 134 } 135 136 if (mTextureName == -1U) { 137 // create the texture name the first time 138 // can't do that in the ctor, because it runs in another thread. 139 glGenTextures(1, &mTextureName); 140 } 141 142 Region::iterator iterator(clip); 143 if (iterator) { 144 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); 169 170 // NOTE: this works only because we have POT. we'd have to round the 171 // texture size up, otherwise. 172 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, 173 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels); 174 175 free((void*)pixels); 176 } 177 178 const State& s = drawingState(); 179 if (UNLIKELY(s.alpha < 0xFF)) { 180 const GGLfixed alpha = (s.alpha << 16)/255; 181 glColor4x(0, 0, 0, alpha); 182 glEnable(GL_BLEND); 183 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 184 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 185 } else { 186 glDisable(GL_BLEND); 187 } 188 189 glDisable(GL_DITHER); 190 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 191 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 192 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 193 194 if (UNLIKELY(transformed() 195 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) )) { 196 // This is a very rare scenario. 197 glMatrixMode(GL_TEXTURE); 198 glLoadIdentity(); 199 glScalef(1.0f/w, -1.0f/h, 1); 200 glTranslatef(-x, -y, 0); 201 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 202 glVertexPointer(2, GL_FIXED, 0, mVertices); 203 glTexCoordPointer(2, GL_FIXED, 0, mVertices); 204 Rect r; 205 while (iterator.iterate(&r)) { 206 const GLint sy = fbHeight - (r.top + r.height()); 207 glScissor(r.left, sy, r.width(), r.height()); 208 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 209 } 210 } else { 211 Region::iterator iterator(clip); 212 if (iterator) { 213 // NOTE: this is marginally faster with the software gl, because 214 // glReadPixels() reads the fb bottom-to-top, however we'll 215 // skip all the jaccobian computations. 216 Rect r; 217 GLint crop[4] = { 0, 0, w, h }; 218 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); 219 y = fbHeight - (y + h); 220 while (iterator.iterate(&r)) { 221 const GLint sy = fbHeight - (r.top + r.height()); 222 glScissor(r.left, sy, r.width(), r.height()); 223 glDrawTexiOES(x, y, 0, w, h); 224 } 225 } 226 } 227 } 228 229 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 230} 231 232// --------------------------------------------------------------------------- 233 234}; // namespace android 235