The Android Open Source Project | 9066cfe | 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 | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdint.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <errno.h> |
| 23 | #include <math.h> |
Jean-Baptiste Queru | 2076373 | 2009-01-26 11:51:12 -0800 | [diff] [blame] | 24 | #include <limits.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | |
| 29 | #include <cutils/log.h> |
| 30 | #include <cutils/properties.h> |
| 31 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 32 | #include <binder/IPCThreadState.h> |
| 33 | #include <binder/IServiceManager.h> |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 34 | #include <binder/MemoryHeapBase.h> |
Mathias Agopian | 0dd593f | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 35 | #include <binder/PermissionCache.h> |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <utils/String8.h> |
| 38 | #include <utils/String16.h> |
| 39 | #include <utils/StopWatch.h> |
| 40 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 41 | #include <ui/GraphicBufferAllocator.h> |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 42 | #include <ui/GraphicLog.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | #include <ui/PixelFormat.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
| 45 | #include <pixelflinger/pixelflinger.h> |
| 46 | #include <GLES/gl.h> |
| 47 | |
| 48 | #include "clz.h" |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 49 | #include "GLExtensions.h" |
Mathias Agopian | 93d75ec | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 50 | #include "DdmConnection.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | #include "Layer.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | #include "LayerDim.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | #include "SurfaceFlinger.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
| 55 | #include "DisplayHardware/DisplayHardware.h" |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 56 | #include "DisplayHardware/HWComposer.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 58 | #include <private/surfaceflinger/SharedBufferStack.h> |
| 59 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 60 | /* ideally AID_GRAPHICS would be in a semi-public header |
| 61 | * or there would be a way to map a user/group name to its id |
| 62 | */ |
| 63 | #ifndef AID_GRAPHICS |
| 64 | #define AID_GRAPHICS 1003 |
| 65 | #endif |
| 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | #define DISPLAY_COUNT 1 |
| 68 | |
| 69 | namespace android { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | // --------------------------------------------------------------------------- |
| 71 | |
Mathias Agopian | 0dd593f | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 72 | const String16 sHardwareTest("android.permission.HARDWARE_TEST"); |
| 73 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); |
| 74 | const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER"); |
| 75 | const String16 sDump("android.permission.DUMP"); |
| 76 | |
| 77 | // --------------------------------------------------------------------------- |
| 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | SurfaceFlinger::SurfaceFlinger() |
| 80 | : BnSurfaceComposer(), Thread(false), |
| 81 | mTransactionFlags(0), |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 82 | mResizeTransationPending(false), |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 83 | mLayersRemoved(false), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | mBootTime(systemTime()), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | mVisibleRegionsDirty(false), |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 86 | mHwWorkListDirty(false), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | mDeferReleaseConsole(false), |
| 88 | mFreezeDisplay(false), |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 89 | mElectronBeamAnimationMode(0), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | mFreezeCount(0), |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 91 | mFreezeDisplayTime(0), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | mDebugRegion(0), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | mDebugBackground(0), |
Mathias Agopian | 93d75ec | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 94 | mDebugDDMS(0), |
Mathias Agopian | 6a96924 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 95 | mDebugDisableHWC(0), |
Mathias Agopian | 2143fe0 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 96 | mDebugDisableTransformHint(0), |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 97 | mDebugInSwapBuffers(0), |
| 98 | mLastSwapBufferTime(0), |
| 99 | mDebugInTransaction(0), |
| 100 | mLastTransactionTime(0), |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 101 | mBootFinished(false), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | mConsoleSignals(0), |
| 103 | mSecureFrameBuffer(0) |
| 104 | { |
| 105 | init(); |
| 106 | } |
| 107 | |
| 108 | void SurfaceFlinger::init() |
| 109 | { |
| 110 | LOGI("SurfaceFlinger is starting"); |
| 111 | |
| 112 | // debugging stuff... |
| 113 | char value[PROPERTY_VALUE_MAX]; |
Mathias Agopian | 93d75ec | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | property_get("debug.sf.showupdates", value, "0"); |
| 116 | mDebugRegion = atoi(value); |
Mathias Agopian | 93d75ec | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | property_get("debug.sf.showbackground", value, "0"); |
| 119 | mDebugBackground = atoi(value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | |
Mathias Agopian | 93d75ec | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 121 | property_get("debug.sf.ddms", value, "0"); |
| 122 | mDebugDDMS = atoi(value); |
| 123 | if (mDebugDDMS) { |
| 124 | DdmConnection::start(getServiceName()); |
| 125 | } |
| 126 | |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 127 | LOGI_IF(mDebugRegion, "showupdates enabled"); |
| 128 | LOGI_IF(mDebugBackground, "showbackground enabled"); |
Mathias Agopian | 93d75ec | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 129 | LOGI_IF(mDebugDDMS, "DDMS debugging enabled"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | SurfaceFlinger::~SurfaceFlinger() |
| 133 | { |
| 134 | glDeleteTextures(1, &mWormholeTexName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 137 | sp<IMemoryHeap> SurfaceFlinger::getCblk() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | { |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 139 | return mServerHeap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Mathias Agopian | 770492c | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 142 | sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 144 | sp<ISurfaceComposerClient> bclient; |
| 145 | sp<Client> client(new Client(this)); |
| 146 | status_t err = client->initCheck(); |
| 147 | if (err == NO_ERROR) { |
| 148 | bclient = client; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | return bclient; |
| 151 | } |
| 152 | |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 153 | sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc() |
| 154 | { |
| 155 | sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc()); |
| 156 | return gba; |
| 157 | } |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 158 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const |
| 160 | { |
| 161 | LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy); |
| 162 | const GraphicPlane& plane(mGraphicPlanes[dpy]); |
| 163 | return plane; |
| 164 | } |
| 165 | |
| 166 | GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) |
| 167 | { |
| 168 | return const_cast<GraphicPlane&>( |
| 169 | const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy)); |
| 170 | } |
| 171 | |
| 172 | void SurfaceFlinger::bootFinished() |
| 173 | { |
| 174 | const nsecs_t now = systemTime(); |
| 175 | const nsecs_t duration = now - mBootTime; |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 176 | LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 177 | mBootFinished = true; |
Mathias Agopian | 0c63ef5 | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 178 | |
| 179 | // wait patiently for the window manager death |
| 180 | const String16 name("window"); |
| 181 | sp<IBinder> window(defaultServiceManager()->getService(name)); |
| 182 | if (window != 0) { |
| 183 | window->linkToDeath(this); |
| 184 | } |
| 185 | |
| 186 | // stop boot animation |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 187 | property_set("ctl.stop", "bootanim"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Mathias Agopian | 0c63ef5 | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 190 | void SurfaceFlinger::binderDied(const wp<IBinder>& who) |
| 191 | { |
| 192 | // the window manager died on us. prepare its eulogy. |
| 193 | |
| 194 | // unfreeze the screen in case it was... frozen |
| 195 | mFreezeDisplayTime = 0; |
| 196 | mFreezeCount = 0; |
| 197 | mFreezeDisplay = false; |
| 198 | |
| 199 | // reset screen orientation |
| 200 | setOrientation(0, eOrientationDefault, 0); |
| 201 | |
| 202 | // restart the boot-animation |
| 203 | property_set("ctl.start", "bootanim"); |
| 204 | } |
| 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | void SurfaceFlinger::onFirstRef() |
| 207 | { |
| 208 | run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY); |
| 209 | |
| 210 | // Wait for the main thread to be done with its initialization |
| 211 | mReadyToRunBarrier.wait(); |
| 212 | } |
| 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | static inline uint16_t pack565(int r, int g, int b) { |
| 215 | return (r<<11)|(g<<5)|b; |
| 216 | } |
| 217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | status_t SurfaceFlinger::readyToRun() |
| 219 | { |
| 220 | LOGI( "SurfaceFlinger's main thread ready to run. " |
| 221 | "Initializing graphics H/W..."); |
| 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // we only support one display currently |
| 224 | int dpy = 0; |
| 225 | |
| 226 | { |
| 227 | // initialize the main display |
| 228 | GraphicPlane& plane(graphicPlane(dpy)); |
| 229 | DisplayHardware* const hw = new DisplayHardware(this, dpy); |
| 230 | plane.setDisplayHardware(hw); |
| 231 | } |
| 232 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 233 | // create the shared control-block |
| 234 | mServerHeap = new MemoryHeapBase(4096, |
| 235 | MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap"); |
| 236 | LOGE_IF(mServerHeap==0, "can't create shared memory dealer"); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 237 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 238 | mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase()); |
| 239 | LOGE_IF(mServerCblk==0, "can't get to shared control block's address"); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 240 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 241 | new(mServerCblk) surface_flinger_cblk_t; |
| 242 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | // initialize primary screen |
| 244 | // (other display should be initialized in the same manner, but |
| 245 | // asynchronously, as they could come and go. None of this is supported |
| 246 | // yet). |
| 247 | const GraphicPlane& plane(graphicPlane(dpy)); |
| 248 | const DisplayHardware& hw = plane.displayHardware(); |
| 249 | const uint32_t w = hw.getWidth(); |
| 250 | const uint32_t h = hw.getHeight(); |
| 251 | const uint32_t f = hw.getFormat(); |
| 252 | hw.makeCurrent(); |
| 253 | |
| 254 | // initialize the shared control block |
| 255 | mServerCblk->connected |= 1<<dpy; |
| 256 | display_cblk_t* dcblk = mServerCblk->displays + dpy; |
| 257 | memset(dcblk, 0, sizeof(display_cblk_t)); |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 258 | dcblk->w = plane.getWidth(); |
| 259 | dcblk->h = plane.getHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | dcblk->format = f; |
| 261 | dcblk->orientation = ISurfaceComposer::eOrientationDefault; |
| 262 | dcblk->xdpi = hw.getDpiX(); |
| 263 | dcblk->ydpi = hw.getDpiY(); |
| 264 | dcblk->fps = hw.getRefreshRate(); |
| 265 | dcblk->density = hw.getDensity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
| 267 | // Initialize OpenGL|ES |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 269 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | glEnableClientState(GL_VERTEX_ARRAY); |
| 271 | glEnable(GL_SCISSOR_TEST); |
| 272 | glShadeModel(GL_FLAT); |
| 273 | glDisable(GL_DITHER); |
| 274 | glDisable(GL_CULL_FACE); |
| 275 | |
| 276 | const uint16_t g0 = pack565(0x0F,0x1F,0x0F); |
| 277 | const uint16_t g1 = pack565(0x17,0x2f,0x17); |
| 278 | const uint16_t textureData[4] = { g0, g1, g1, g0 }; |
| 279 | glGenTextures(1, &mWormholeTexName); |
| 280 | glBindTexture(GL_TEXTURE_2D, mWormholeTexName); |
| 281 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 282 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 283 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 284 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 285 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, |
| 286 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData); |
| 287 | |
| 288 | glViewport(0, 0, w, h); |
| 289 | glMatrixMode(GL_PROJECTION); |
| 290 | glLoadIdentity(); |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 291 | // put the origin in the left-bottom corner |
| 292 | glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | mReadyToRunBarrier.open(); |
| 295 | |
| 296 | /* |
| 297 | * We're now ready to accept clients... |
| 298 | */ |
| 299 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 300 | // start boot animation |
| 301 | property_set("ctl.start", "bootanim"); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 302 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | return NO_ERROR; |
| 304 | } |
| 305 | |
| 306 | // ---------------------------------------------------------------------------- |
| 307 | #if 0 |
| 308 | #pragma mark - |
| 309 | #pragma mark Events Handler |
| 310 | #endif |
| 311 | |
| 312 | void SurfaceFlinger::waitForEvent() |
| 313 | { |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 314 | while (true) { |
| 315 | nsecs_t timeout = -1; |
Mathias Agopian | 0e44976 | 2009-12-01 17:23:28 -0800 | [diff] [blame] | 316 | const nsecs_t freezeDisplayTimeout = ms2ns(5000); |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 317 | if (UNLIKELY(isFrozen())) { |
| 318 | // wait 5 seconds |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 319 | const nsecs_t now = systemTime(); |
| 320 | if (mFreezeDisplayTime == 0) { |
| 321 | mFreezeDisplayTime = now; |
| 322 | } |
| 323 | nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime); |
| 324 | timeout = waitTime>0 ? waitTime : 0; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 325 | } |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 326 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 327 | sp<MessageBase> msg = mEventQueue.waitMessage(timeout); |
Mathias Agopian | 0e44976 | 2009-12-01 17:23:28 -0800 | [diff] [blame] | 328 | |
| 329 | // see if we timed out |
| 330 | if (isFrozen()) { |
| 331 | const nsecs_t now = systemTime(); |
| 332 | nsecs_t frozenTime = (now - mFreezeDisplayTime); |
| 333 | if (frozenTime >= freezeDisplayTimeout) { |
| 334 | // we timed out and are still frozen |
| 335 | LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d", |
| 336 | mFreezeDisplay, mFreezeCount); |
| 337 | mFreezeDisplayTime = 0; |
| 338 | mFreezeCount = 0; |
| 339 | mFreezeDisplay = false; |
| 340 | } |
| 341 | } |
| 342 | |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 343 | if (msg != 0) { |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 344 | switch (msg->what) { |
| 345 | case MessageQueue::INVALIDATE: |
| 346 | // invalidate message, just return to the main loop |
| 347 | return; |
| 348 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | void SurfaceFlinger::signalEvent() { |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 354 | mEventQueue.invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Jamie Gennis | 9b8fc65 | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 357 | bool SurfaceFlinger::authenticateSurfaceTexture( |
| 358 | const sp<ISurfaceTexture>& surfaceTexture) const { |
Jamie Gennis | d2acedf | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 359 | Mutex::Autolock _l(mStateLock); |
Jamie Gennis | 9b8fc65 | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 360 | sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder()); |
Jamie Gennis | d2acedf | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 361 | |
| 362 | // Check the visible layer list for the ISurface |
| 363 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 364 | size_t count = currentLayers.size(); |
| 365 | for (size_t i=0 ; i<count ; i++) { |
| 366 | const sp<LayerBase>& layer(currentLayers[i]); |
| 367 | sp<LayerBaseClient> lbc(layer->getLayerBaseClient()); |
Jamie Gennis | 9b8fc65 | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 368 | if (lbc != NULL) { |
| 369 | wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder(); |
| 370 | if (lbcBinder == surfaceTextureBinder) { |
| 371 | return true; |
| 372 | } |
Jamie Gennis | d2acedf | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | // Check the layers in the purgatory. This check is here so that if a |
Jamie Gennis | 9b8fc65 | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 377 | // SurfaceTexture gets destroyed before all the clients are done using it, |
| 378 | // the error will not be reported as "surface XYZ is not authenticated", but |
Jamie Gennis | d2acedf | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 379 | // will instead fail later on when the client tries to use the surface, |
| 380 | // which should be reported as "surface XYZ returned an -ENODEV". The |
| 381 | // purgatorized layers are no less authentic than the visible ones, so this |
| 382 | // should not cause any harm. |
| 383 | size_t purgatorySize = mLayerPurgatory.size(); |
| 384 | for (size_t i=0 ; i<purgatorySize ; i++) { |
| 385 | const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i)); |
| 386 | sp<LayerBaseClient> lbc(layer->getLayerBaseClient()); |
Jamie Gennis | 9b8fc65 | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 387 | if (lbc != NULL) { |
| 388 | wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder(); |
| 389 | if (lbcBinder == surfaceTextureBinder) { |
| 390 | return true; |
| 391 | } |
Jamie Gennis | d2acedf | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | return false; |
| 396 | } |
| 397 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 398 | status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg, |
| 399 | nsecs_t reltime, uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | { |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 401 | return mEventQueue.postMessage(msg, reltime, flags); |
| 402 | } |
| 403 | |
| 404 | status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg, |
| 405 | nsecs_t reltime, uint32_t flags) |
| 406 | { |
| 407 | status_t res = mEventQueue.postMessage(msg, reltime, flags); |
| 408 | if (res == NO_ERROR) { |
| 409 | msg->wait(); |
| 410 | } |
| 411 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // ---------------------------------------------------------------------------- |
| 415 | #if 0 |
| 416 | #pragma mark - |
| 417 | #pragma mark Main loop |
| 418 | #endif |
| 419 | |
| 420 | bool SurfaceFlinger::threadLoop() |
| 421 | { |
| 422 | waitForEvent(); |
| 423 | |
| 424 | // check for transactions |
| 425 | if (UNLIKELY(mConsoleSignals)) { |
| 426 | handleConsoleEvents(); |
| 427 | } |
| 428 | |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 429 | // if we're in a global transaction, don't do anything. |
| 430 | const uint32_t mask = eTransactionNeeded | eTraversalNeeded; |
| 431 | uint32_t transactionFlags = peekTransactionFlags(mask); |
| 432 | if (UNLIKELY(transactionFlags)) { |
| 433 | handleTransaction(transactionFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // post surfaces (if needed) |
| 437 | handlePageFlip(); |
| 438 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 439 | if (UNLIKELY(mHwWorkListDirty)) { |
| 440 | // build the h/w work list |
| 441 | handleWorkList(); |
| 442 | } |
| 443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | 81384bf | 2009-09-27 22:47:27 -0700 | [diff] [blame] | 445 | if (LIKELY(hw.canDraw() && !isFrozen())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | // repaint the framebuffer (if needed) |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 447 | |
| 448 | const int index = hw.getCurrentBufferIndex(); |
| 449 | GraphicLog& logger(GraphicLog::getInstance()); |
| 450 | |
| 451 | logger.log(GraphicLog::SF_REPAINT, index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | handleRepaint(); |
| 453 | |
Mathias Agopian | b1a1874 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 454 | // inform the h/w that we're done compositing |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 455 | logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index); |
Mathias Agopian | b1a1874 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 456 | hw.compositionComplete(); |
| 457 | |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 458 | logger.log(GraphicLog::SF_SWAP_BUFFERS, index); |
Antti Hatala | 4c0a4a2 | 2010-09-08 06:37:14 -0700 | [diff] [blame] | 459 | postFramebuffer(); |
| 460 | |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 461 | logger.log(GraphicLog::SF_REPAINT_DONE, index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } else { |
| 463 | // pretend we did the post |
Mathias Agopian | a6b8c1c | 2010-12-15 14:41:59 -0800 | [diff] [blame] | 464 | hw.compositionComplete(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | usleep(16667); // 60 fps period |
| 466 | } |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | void SurfaceFlinger::postFramebuffer() |
| 471 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | if (!mInvalidRegion.isEmpty()) { |
| 473 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 474 | const nsecs_t now = systemTime(); |
| 475 | mDebugInSwapBuffers = now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | hw.flip(mInvalidRegion); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 477 | mLastSwapBufferTime = systemTime() - now; |
| 478 | mDebugInSwapBuffers = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | mInvalidRegion.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | void SurfaceFlinger::handleConsoleEvents() |
| 484 | { |
| 485 | // something to do with the console |
| 486 | const DisplayHardware& hw = graphicPlane(0).displayHardware(); |
| 487 | |
| 488 | int what = android_atomic_and(0, &mConsoleSignals); |
| 489 | if (what & eConsoleAcquired) { |
| 490 | hw.acquireScreen(); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 491 | // this is a temporary work-around, eventually this should be called |
| 492 | // by the power-manager |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 493 | SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 496 | if (mDeferReleaseConsole && hw.isScreenAcquired()) { |
Mathias Agopian | ed81f22 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 497 | // We got the release signal before the acquire signal |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | mDeferReleaseConsole = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | hw.releaseScreen(); |
| 500 | } |
| 501 | |
| 502 | if (what & eConsoleReleased) { |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 503 | if (hw.isScreenAcquired()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | hw.releaseScreen(); |
| 505 | } else { |
| 506 | mDeferReleaseConsole = true; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | mDirtyRegion.set(hw.bounds()); |
| 511 | } |
| 512 | |
| 513 | void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) |
| 514 | { |
Mathias Agopian | 6960811 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 515 | Mutex::Autolock _l(mStateLock); |
| 516 | const nsecs_t now = systemTime(); |
| 517 | mDebugInTransaction = now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | |
Mathias Agopian | 6960811 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 519 | // Here we're guaranteed that some transaction flags are set |
| 520 | // so we can call handleTransactionLocked() unconditionally. |
| 521 | // We call getTransactionFlags(), which will also clear the flags, |
| 522 | // with mStateLock held to guarantee that mCurrentState won't change |
| 523 | // until the transaction is committed. |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 524 | |
Mathias Agopian | 6960811 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 525 | const uint32_t mask = eTransactionNeeded | eTraversalNeeded; |
| 526 | transactionFlags = getTransactionFlags(mask); |
| 527 | handleTransactionLocked(transactionFlags); |
Mathias Agopian | 6dcb155 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 528 | |
Mathias Agopian | 6960811 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 529 | mLastTransactionTime = systemTime() - now; |
| 530 | mDebugInTransaction = 0; |
| 531 | invalidateHwcGeometry(); |
| 532 | // here the transaction has been committed |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Mathias Agopian | 6960811 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 535 | void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags) |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 536 | { |
| 537 | const LayerVector& currentLayers(mCurrentState.layersSortedByZ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | const size_t count = currentLayers.size(); |
| 539 | |
| 540 | /* |
| 541 | * Traversal of the children |
| 542 | * (perform the transaction for each of them if needed) |
| 543 | */ |
| 544 | |
| 545 | const bool layersNeedTransaction = transactionFlags & eTraversalNeeded; |
| 546 | if (layersNeedTransaction) { |
| 547 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 548 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded); |
| 550 | if (!trFlags) continue; |
| 551 | |
| 552 | const uint32_t flags = layer->doTransaction(0); |
| 553 | if (flags & Layer::eVisibleRegion) |
| 554 | mVisibleRegionsDirty = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Perform our own transaction if needed |
| 560 | */ |
| 561 | |
| 562 | if (transactionFlags & eTransactionNeeded) { |
| 563 | if (mCurrentState.orientation != mDrawingState.orientation) { |
| 564 | // the orientation has changed, recompute all visible regions |
| 565 | // and invalidate everything. |
| 566 | |
| 567 | const int dpy = 0; |
| 568 | const int orientation = mCurrentState.orientation; |
Mathias Agopian | eb0c86e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 569 | const uint32_t type = mCurrentState.orientationType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | GraphicPlane& plane(graphicPlane(dpy)); |
| 571 | plane.setOrientation(orientation); |
| 572 | |
| 573 | // update the shared control block |
| 574 | const DisplayHardware& hw(plane.displayHardware()); |
| 575 | volatile display_cblk_t* dcblk = mServerCblk->displays + dpy; |
| 576 | dcblk->orientation = orientation; |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 577 | dcblk->w = plane.getWidth(); |
| 578 | dcblk->h = plane.getHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | |
| 580 | mVisibleRegionsDirty = true; |
| 581 | mDirtyRegion.set(hw.bounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) { |
| 585 | // freezing or unfreezing the display -> trigger animation if needed |
| 586 | mFreezeDisplay = mCurrentState.freezeDisplay; |
Mathias Agopian | 31901cc | 2010-03-01 17:51:17 -0800 | [diff] [blame] | 587 | if (mFreezeDisplay) |
| 588 | mFreezeDisplayTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 591 | if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) { |
| 592 | // layers have been added |
| 593 | mVisibleRegionsDirty = true; |
| 594 | } |
| 595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | // some layers might have been removed, so |
| 597 | // we need to update the regions they're exposing. |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 598 | if (mLayersRemoved) { |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 599 | mLayersRemoved = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | mVisibleRegionsDirty = true; |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 601 | const LayerVector& previousLayers(mDrawingState.layersSortedByZ); |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 602 | const size_t count = previousLayers.size(); |
| 603 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 604 | const sp<LayerBase>& layer(previousLayers[i]); |
| 605 | if (currentLayers.indexOf( layer ) < 0) { |
| 606 | // this layer is not visible anymore |
Mathias Agopian | 33863dd | 2009-07-28 14:20:21 -0700 | [diff] [blame] | 607 | mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen); |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | commitTransaction(); |
| 614 | } |
| 615 | |
| 616 | sp<FreezeLock> SurfaceFlinger::getFreezeLock() const |
| 617 | { |
| 618 | return new FreezeLock(const_cast<SurfaceFlinger *>(this)); |
| 619 | } |
| 620 | |
| 621 | void SurfaceFlinger::computeVisibleRegions( |
Mathias Agopian | f0ff906 | 2011-03-11 16:54:47 -0800 | [diff] [blame] | 622 | const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | { |
| 624 | const GraphicPlane& plane(graphicPlane(0)); |
| 625 | const Transform& planeTransform(plane.transform()); |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 626 | const DisplayHardware& hw(plane.displayHardware()); |
| 627 | const Region screenRegion(hw.bounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | |
| 629 | Region aboveOpaqueLayers; |
| 630 | Region aboveCoveredLayers; |
| 631 | Region dirty; |
| 632 | |
| 633 | bool secureFrameBuffer = false; |
| 634 | |
| 635 | size_t i = currentLayers.size(); |
| 636 | while (i--) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 637 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | layer->validateVisibility(planeTransform); |
| 639 | |
| 640 | // start with the whole surface at its current location |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 641 | const Layer::State& s(layer->drawingState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 643 | /* |
| 644 | * opaqueRegion: area of a surface that is fully opaque. |
| 645 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | Region opaqueRegion; |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * visibleRegion: area of a surface that is visible on screen |
| 650 | * and not fully transparent. This is essentially the layer's |
| 651 | * footprint minus the opaque regions above it. |
| 652 | * Areas covered by a translucent surface are considered visible. |
| 653 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | Region visibleRegion; |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * coveredRegion: area of a surface that is covered by all |
| 658 | * visible regions above it (which includes the translucent areas). |
| 659 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | Region coveredRegion; |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 661 | |
| 662 | |
| 663 | // handle hidden surfaces by setting the visible region to empty |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 664 | if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) { |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 665 | const bool translucent = !layer->isOpaque(); |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 666 | const Rect bounds(layer->visibleBounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | visibleRegion.set(bounds); |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 668 | visibleRegion.andSelf(screenRegion); |
| 669 | if (!visibleRegion.isEmpty()) { |
| 670 | // Remove the transparent area from the visible region |
| 671 | if (translucent) { |
| 672 | visibleRegion.subtractSelf(layer->transparentRegionScreen); |
| 673 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 675 | // compute the opaque region |
| 676 | const int32_t layerOrientation = layer->getOrientation(); |
| 677 | if (s.alpha==255 && !translucent && |
| 678 | ((layerOrientation & Transform::ROT_INVALID) == false)) { |
| 679 | // the opaque region is the layer's footprint |
| 680 | opaqueRegion = visibleRegion; |
| 681 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 685 | // Clip the covered region to the visible region |
| 686 | coveredRegion = aboveCoveredLayers.intersect(visibleRegion); |
| 687 | |
| 688 | // Update aboveCoveredLayers for next (lower) layer |
| 689 | aboveCoveredLayers.orSelf(visibleRegion); |
| 690 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | // subtract the opaque region covered by the layers above us |
| 692 | visibleRegion.subtractSelf(aboveOpaqueLayers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | |
| 694 | // compute this layer's dirty region |
| 695 | if (layer->contentDirty) { |
| 696 | // we need to invalidate the whole region |
| 697 | dirty = visibleRegion; |
| 698 | // as well, as the old visible region |
| 699 | dirty.orSelf(layer->visibleRegionScreen); |
| 700 | layer->contentDirty = false; |
| 701 | } else { |
Mathias Agopian | 0aed7e9 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 702 | /* compute the exposed region: |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 703 | * the exposed region consists of two components: |
| 704 | * 1) what's VISIBLE now and was COVERED before |
| 705 | * 2) what's EXPOSED now less what was EXPOSED before |
| 706 | * |
| 707 | * note that (1) is conservative, we start with the whole |
| 708 | * visible region but only keep what used to be covered by |
| 709 | * something -- which mean it may have been exposed. |
| 710 | * |
| 711 | * (2) handles areas that were not covered by anything but got |
| 712 | * exposed because of a resize. |
Mathias Agopian | 0aed7e9 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 713 | */ |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 714 | const Region newExposed = visibleRegion - coveredRegion; |
| 715 | const Region oldVisibleRegion = layer->visibleRegionScreen; |
| 716 | const Region oldCoveredRegion = layer->coveredRegionScreen; |
| 717 | const Region oldExposed = oldVisibleRegion - oldCoveredRegion; |
| 718 | dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | } |
| 720 | dirty.subtractSelf(aboveOpaqueLayers); |
| 721 | |
| 722 | // accumulate to the screen dirty region |
| 723 | dirtyRegion.orSelf(dirty); |
| 724 | |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 725 | // Update aboveOpaqueLayers for next (lower) layer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | aboveOpaqueLayers.orSelf(opaqueRegion); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 727 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | // Store the visible region is screen space |
| 729 | layer->setVisibleRegion(visibleRegion); |
| 730 | layer->setCoveredRegion(coveredRegion); |
| 731 | |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 732 | // If a secure layer is partially visible, lock-down the screen! |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | if (layer->isSecure() && !visibleRegion.isEmpty()) { |
| 734 | secureFrameBuffer = true; |
| 735 | } |
| 736 | } |
| 737 | |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 738 | // invalidate the areas where a layer was removed |
| 739 | dirtyRegion.orSelf(mDirtyRegionRemovedLayer); |
| 740 | mDirtyRegionRemovedLayer.clear(); |
| 741 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | mSecureFrameBuffer = secureFrameBuffer; |
| 743 | opaqueRegion = aboveOpaqueLayers; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | void SurfaceFlinger::commitTransaction() |
| 748 | { |
| 749 | mDrawingState = mCurrentState; |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 750 | mResizeTransationPending = false; |
| 751 | mTransactionCV.broadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void SurfaceFlinger::handlePageFlip() |
| 755 | { |
| 756 | bool visibleRegions = mVisibleRegionsDirty; |
Mathias Agopian | f0ff906 | 2011-03-11 16:54:47 -0800 | [diff] [blame] | 757 | const LayerVector& currentLayers(mDrawingState.layersSortedByZ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | visibleRegions |= lockPageFlip(currentLayers); |
| 759 | |
| 760 | const DisplayHardware& hw = graphicPlane(0).displayHardware(); |
| 761 | const Region screenRegion(hw.bounds()); |
| 762 | if (visibleRegions) { |
| 763 | Region opaqueRegion; |
| 764 | computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion); |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * rebuild the visible layer list |
| 768 | */ |
Mathias Agopian | f0ff906 | 2011-03-11 16:54:47 -0800 | [diff] [blame] | 769 | const size_t count = currentLayers.size(); |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 770 | mVisibleLayersSortedByZ.clear(); |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 771 | mVisibleLayersSortedByZ.setCapacity(count); |
| 772 | for (size_t i=0 ; i<count ; i++) { |
| 773 | if (!currentLayers[i]->visibleRegionScreen.isEmpty()) |
| 774 | mVisibleLayersSortedByZ.add(currentLayers[i]); |
| 775 | } |
| 776 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | mWormholeRegion = screenRegion.subtract(opaqueRegion); |
| 778 | mVisibleRegionsDirty = false; |
Mathias Agopian | fb4dcb1 | 2011-01-13 17:53:01 -0800 | [diff] [blame] | 779 | invalidateHwcGeometry(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | unlockPageFlip(currentLayers); |
| 783 | mDirtyRegion.andSelf(screenRegion); |
| 784 | } |
| 785 | |
Mathias Agopian | fb4dcb1 | 2011-01-13 17:53:01 -0800 | [diff] [blame] | 786 | void SurfaceFlinger::invalidateHwcGeometry() |
| 787 | { |
| 788 | mHwWorkListDirty = true; |
| 789 | } |
| 790 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers) |
| 792 | { |
| 793 | bool recomputeVisibleRegions = false; |
| 794 | size_t count = currentLayers.size(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 795 | sp<LayerBase> const* layers = currentLayers.array(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 797 | const sp<LayerBase>& layer(layers[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | layer->lockPageFlip(recomputeVisibleRegions); |
| 799 | } |
| 800 | return recomputeVisibleRegions; |
| 801 | } |
| 802 | |
| 803 | void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers) |
| 804 | { |
| 805 | const GraphicPlane& plane(graphicPlane(0)); |
| 806 | const Transform& planeTransform(plane.transform()); |
| 807 | size_t count = currentLayers.size(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 808 | sp<LayerBase> const* layers = currentLayers.array(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 810 | const sp<LayerBase>& layer(layers[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | layer->unlockPageFlip(planeTransform, mDirtyRegion); |
| 812 | } |
| 813 | } |
| 814 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 815 | void SurfaceFlinger::handleWorkList() |
| 816 | { |
| 817 | mHwWorkListDirty = false; |
| 818 | HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer()); |
| 819 | if (hwc.initCheck() == NO_ERROR) { |
| 820 | const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ); |
| 821 | const size_t count = currentLayers.size(); |
| 822 | hwc.createWorkList(count); |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 823 | hwc_layer_t* const cur(hwc.getLayers()); |
| 824 | for (size_t i=0 ; cur && i<count ; i++) { |
| 825 | currentLayers[i]->setGeometry(&cur[i]); |
Mathias Agopian | 7f76a3c | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 826 | if (mDebugDisableHWC || mDebugRegion) { |
Mathias Agopian | 6a96924 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 827 | cur[i].compositionType = HWC_FRAMEBUFFER; |
| 828 | cur[i].flags |= HWC_SKIP_LAYER; |
| 829 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | } |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 833 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | void SurfaceFlinger::handleRepaint() |
| 835 | { |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 836 | // compute the invalid region |
| 837 | mInvalidRegion.orSelf(mDirtyRegion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | |
| 839 | if (UNLIKELY(mDebugRegion)) { |
| 840 | debugFlashRegions(); |
| 841 | } |
| 842 | |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 843 | // set the frame buffer |
| 844 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 845 | glMatrixMode(GL_MODELVIEW); |
| 846 | glLoadIdentity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | |
| 848 | uint32_t flags = hw.getFlags(); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 849 | if ((flags & DisplayHardware::SWAP_RECTANGLE) || |
| 850 | (flags & DisplayHardware::BUFFER_PRESERVED)) |
Mathias Agopian | 2e20bff | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 851 | { |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 852 | // we can redraw only what's dirty, but since SWAP_RECTANGLE only |
| 853 | // takes a rectangle, we must make sure to update that whole |
| 854 | // rectangle in that case |
| 855 | if (flags & DisplayHardware::SWAP_RECTANGLE) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 856 | // TODO: we really should be able to pass a region to |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 857 | // SWAP_RECTANGLE so that we don't have to redraw all this. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | mDirtyRegion.set(mInvalidRegion.bounds()); |
| 859 | } else { |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 860 | // in the BUFFER_PRESERVED case, obviously, we can update only |
| 861 | // what's needed and nothing more. |
| 862 | // NOTE: this is NOT a common case, as preserving the backbuffer |
| 863 | // is costly and usually involves copying the whole update back. |
| 864 | } |
| 865 | } else { |
Mathias Agopian | f2d28b7 | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 866 | if (flags & DisplayHardware::PARTIAL_UPDATES) { |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 867 | // We need to redraw the rectangle that will be updated |
| 868 | // (pushed to the framebuffer). |
Mathias Agopian | f2d28b7 | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 869 | // This is needed because PARTIAL_UPDATES only takes one |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 870 | // rectangle instead of a region (see DisplayHardware::flip()) |
| 871 | mDirtyRegion.set(mInvalidRegion.bounds()); |
| 872 | } else { |
| 873 | // we need to redraw everything (the whole screen) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | mDirtyRegion.set(hw.bounds()); |
| 875 | mInvalidRegion = mDirtyRegion; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // compose all surfaces |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 880 | setupHardwareComposer(&mDirtyRegion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | composeSurfaces(mDirtyRegion); |
| 882 | |
| 883 | // clear the dirty regions |
| 884 | mDirtyRegion.clear(); |
| 885 | } |
| 886 | |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 887 | void SurfaceFlinger::setupHardwareComposer(Region* dirtyInOut) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | { |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 889 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 890 | HWComposer& hwc(hw.getHwComposer()); |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 891 | hwc_layer_t* const cur(hwc.getLayers()); |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 892 | if (!cur) { |
| 893 | return; |
| 894 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 895 | |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 896 | const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); |
| 897 | size_t count = layers.size(); |
| 898 | |
| 899 | LOGE_IF(hwc.getNumLayers() != count, |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 900 | "HAL number of layers (%d) doesn't match surfaceflinger (%d)", |
| 901 | hwc.getNumLayers(), count); |
| 902 | |
| 903 | // just to be extra-safe, use the smallest count |
Erik Gilling | 0cf2f43 | 2010-08-12 23:21:40 -0700 | [diff] [blame] | 904 | if (hwc.initCheck() == NO_ERROR) { |
| 905 | count = count < hwc.getNumLayers() ? count : hwc.getNumLayers(); |
| 906 | } |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | * update the per-frame h/w composer data for each layer |
| 910 | * and build the transparent region of the FB |
| 911 | */ |
| 912 | Region transparent; |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 913 | for (size_t i=0 ; i<count ; i++) { |
| 914 | const sp<LayerBase>& layer(layers[i]); |
| 915 | layer->setPerFrameData(&cur[i]); |
| 916 | } |
| 917 | status_t err = hwc.prepare(); |
| 918 | LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err)); |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 919 | |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 920 | if (err == NO_ERROR) { |
| 921 | Region transparentDirty(*dirtyInOut); |
| 922 | for (size_t i=0 ; i<count ; i++) { |
| 923 | // Calculate the new transparent region and dirty region |
| 924 | // - the transparent region needs to always include the layers |
| 925 | // that moved from FB to OVERLAY, regardless of the dirty region |
| 926 | // - the dirty region needs to be expanded to include layers |
| 927 | // that moved from OVERLAY to FB. |
| 928 | |
| 929 | const sp<LayerBase>& layer(layers[i]); |
| 930 | if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) { |
| 931 | transparent.orSelf(layer->visibleRegionScreen); |
Mathias Agopian | 4549169 | 2011-01-19 15:24:23 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 934 | bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER) && |
| 935 | !(cur[i].flags & HWC_SKIP_LAYER); |
| 936 | |
| 937 | if (!isOverlay && layer->isOverlay()) { |
| 938 | dirtyInOut->orSelf(layer->visibleRegionScreen); |
| 939 | } |
| 940 | |
| 941 | if (isOverlay && !layer->isOverlay()) { |
| 942 | transparentDirty.orSelf(layer->visibleRegionScreen); |
| 943 | } |
| 944 | |
| 945 | layer->setOverlay(isOverlay); |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * clear the area of the FB that need to be transparent |
| 950 | */ |
| 951 | transparent.andSelf(transparentDirty); |
| 952 | if (!transparent.isEmpty()) { |
| 953 | glClearColor(0,0,0,0); |
| 954 | Region::const_iterator it = transparent.begin(); |
| 955 | Region::const_iterator const end = transparent.end(); |
| 956 | const int32_t height = hw.getHeight(); |
| 957 | while (it != end) { |
| 958 | const Rect& r(*it++); |
| 959 | const GLint sy = height - (r.top + r.height()); |
| 960 | glScissor(r.left, sy, r.width(), r.height()); |
| 961 | glClear(GL_COLOR_BUFFER_BIT); |
Mathias Agopian | 4549169 | 2011-01-19 15:24:23 -0800 | [diff] [blame] | 962 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 965 | } |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 966 | |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 967 | void SurfaceFlinger::composeSurfaces(const Region& dirty) |
| 968 | { |
| 969 | if (UNLIKELY(!mWormholeRegion.isEmpty())) { |
| 970 | // should never happen unless the window manager has a bug |
| 971 | // draw something... |
| 972 | drawWormhole(); |
| 973 | } |
| 974 | |
| 975 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 976 | HWComposer& hwc(hw.getHwComposer()); |
| 977 | hwc_layer_t* const cur(hwc.getLayers()); |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 978 | |
| 979 | /* |
| 980 | * and then, render the layers targeted at the framebuffer |
| 981 | */ |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 982 | const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); |
| 983 | size_t count = layers.size(); |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 984 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 985 | if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER) && |
Antti Hatala | 982f58b | 2010-09-09 02:32:30 -0700 | [diff] [blame] | 986 | !(cur[i].flags & HWC_SKIP_LAYER)) { |
Mathias Agopian | 4bacc9d | 2011-09-08 18:31:55 -0700 | [diff] [blame^] | 987 | continue; |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 988 | } |
| 989 | const sp<LayerBase>& layer(layers[i]); |
| 990 | const Region clip(dirty.intersect(layer->visibleRegionScreen)); |
| 991 | if (!clip.isEmpty()) { |
| 992 | layer->draw(clip); |
| 993 | } |
| 994 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | } |
| 996 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | void SurfaceFlinger::debugFlashRegions() |
| 998 | { |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 999 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1000 | const uint32_t flags = hw.getFlags(); |
Mathias Agopian | 7f76a3c | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1001 | const int32_t height = hw.getHeight(); |
| 1002 | if (mInvalidRegion.isEmpty()) { |
| 1003 | return; |
| 1004 | } |
Mathias Agopian | 2e20bff | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 1005 | |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1006 | if (!((flags & DisplayHardware::SWAP_RECTANGLE) || |
| 1007 | (flags & DisplayHardware::BUFFER_PRESERVED))) { |
| 1008 | const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ? |
| 1009 | mDirtyRegion.bounds() : hw.bounds()); |
| 1010 | composeSurfaces(repaint); |
| 1011 | } |
| 1012 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | glDisable(GL_BLEND); |
| 1014 | glDisable(GL_DITHER); |
| 1015 | glDisable(GL_SCISSOR_TEST); |
| 1016 | |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1017 | static int toggle = 0; |
| 1018 | toggle = 1 - toggle; |
| 1019 | if (toggle) { |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1020 | glColor4f(1, 0, 1, 1); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1021 | } else { |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1022 | glColor4f(1, 1, 0, 1); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1023 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1025 | Region::const_iterator it = mDirtyRegion.begin(); |
| 1026 | Region::const_iterator const end = mDirtyRegion.end(); |
| 1027 | while (it != end) { |
| 1028 | const Rect& r = *it++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | GLfloat vertices[][2] = { |
Mathias Agopian | 7f76a3c | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1030 | { r.left, height - r.top }, |
| 1031 | { r.left, height - r.bottom }, |
| 1032 | { r.right, height - r.bottom }, |
| 1033 | { r.right, height - r.top } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | }; |
| 1035 | glVertexPointer(2, GL_FLOAT, 0, vertices); |
| 1036 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1037 | } |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1038 | |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 1039 | hw.flip(mInvalidRegion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | |
| 1041 | if (mDebugRegion > 1) |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1042 | usleep(mDebugRegion * 1000); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | |
| 1044 | glEnable(GL_SCISSOR_TEST); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | void SurfaceFlinger::drawWormhole() const |
| 1048 | { |
| 1049 | const Region region(mWormholeRegion.intersect(mDirtyRegion)); |
| 1050 | if (region.isEmpty()) |
| 1051 | return; |
| 1052 | |
| 1053 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1054 | const int32_t width = hw.getWidth(); |
| 1055 | const int32_t height = hw.getHeight(); |
| 1056 | |
| 1057 | glDisable(GL_BLEND); |
| 1058 | glDisable(GL_DITHER); |
| 1059 | |
| 1060 | if (LIKELY(!mDebugBackground)) { |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1061 | glClearColor(0,0,0,0); |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1062 | Region::const_iterator it = region.begin(); |
| 1063 | Region::const_iterator const end = region.end(); |
| 1064 | while (it != end) { |
| 1065 | const Rect& r = *it++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | const GLint sy = height - (r.top + r.height()); |
| 1067 | glScissor(r.left, sy, r.width(), r.height()); |
| 1068 | glClear(GL_COLOR_BUFFER_BIT); |
| 1069 | } |
| 1070 | } else { |
| 1071 | const GLshort vertices[][2] = { { 0, 0 }, { width, 0 }, |
| 1072 | { width, height }, { 0, height } }; |
| 1073 | const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } }; |
| 1074 | glVertexPointer(2, GL_SHORT, 0, vertices); |
| 1075 | glTexCoordPointer(2, GL_SHORT, 0, tcoords); |
| 1076 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
Michael I. Gold | e20a56d | 2010-09-15 15:46:24 -0700 | [diff] [blame] | 1077 | #if defined(GL_OES_EGL_image_external) |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 1078 | if (GLExtensions::getInstance().haveTextureExternal()) { |
| 1079 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 1080 | } |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1081 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | glEnable(GL_TEXTURE_2D); |
| 1083 | glBindTexture(GL_TEXTURE_2D, mWormholeTexName); |
| 1084 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 1085 | glMatrixMode(GL_TEXTURE); |
| 1086 | glLoadIdentity(); |
| 1087 | glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1); |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1088 | Region::const_iterator it = region.begin(); |
| 1089 | Region::const_iterator const end = region.end(); |
| 1090 | while (it != end) { |
| 1091 | const Rect& r = *it++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | const GLint sy = height - (r.top + r.height()); |
| 1093 | glScissor(r.left, sy, r.width(), r.height()); |
| 1094 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1095 | } |
| 1096 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1097 | glDisable(GL_TEXTURE_2D); |
Mathias Agopian | 04a709e4 | 2010-12-14 18:38:36 -0800 | [diff] [blame] | 1098 | glLoadIdentity(); |
| 1099 | glMatrixMode(GL_MODELVIEW); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | void SurfaceFlinger::debugShowFPS() const |
| 1104 | { |
| 1105 | static int mFrameCount; |
| 1106 | static int mLastFrameCount = 0; |
| 1107 | static nsecs_t mLastFpsTime = 0; |
| 1108 | static float mFps = 0; |
| 1109 | mFrameCount++; |
| 1110 | nsecs_t now = systemTime(); |
| 1111 | nsecs_t diff = now - mLastFpsTime; |
| 1112 | if (diff > ms2ns(250)) { |
| 1113 | mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff; |
| 1114 | mLastFpsTime = now; |
| 1115 | mLastFrameCount = mFrameCount; |
| 1116 | } |
| 1117 | // XXX: mFPS has the value we want |
| 1118 | } |
| 1119 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1120 | status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | { |
| 1122 | Mutex::Autolock _l(mStateLock); |
| 1123 | addLayer_l(layer); |
| 1124 | setTransactionFlags(eTransactionNeeded|eTraversalNeeded); |
| 1125 | return NO_ERROR; |
| 1126 | } |
| 1127 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1128 | status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer) |
| 1129 | { |
Mathias Agopian | 1efba9a | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 1130 | ssize_t i = mCurrentState.layersSortedByZ.add(layer); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1131 | return (i < 0) ? status_t(i) : status_t(NO_ERROR); |
| 1132 | } |
| 1133 | |
| 1134 | ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client, |
| 1135 | const sp<LayerBaseClient>& lbc) |
| 1136 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1137 | // attach this layer to the client |
Mathias Agopian | 5fa7ad6 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 1138 | size_t name = client->attachLayer(lbc); |
| 1139 | |
| 1140 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1141 | |
| 1142 | // add this layer to the current state list |
| 1143 | addLayer_l(lbc); |
| 1144 | |
Mathias Agopian | 5fa7ad6 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 1145 | return ssize_t(name); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1148 | status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | { |
| 1150 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1151 | status_t err = purgatorizeLayer_l(layer); |
| 1152 | if (err == NO_ERROR) |
| 1153 | setTransactionFlags(eTransactionNeeded); |
| 1154 | return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1157 | status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1159 | sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient()); |
| 1160 | if (lbc != 0) { |
Mathias Agopian | d35c666 | 2011-01-25 20:17:45 -0800 | [diff] [blame] | 1161 | mLayerMap.removeItem( lbc->getSurfaceBinder() ); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1162 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase); |
| 1164 | if (index >= 0) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1165 | mLayersRemoved = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | return NO_ERROR; |
| 1167 | } |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1168 | return status_t(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1171 | status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase) |
| 1172 | { |
Mathias Agopian | f4dfe1b | 2011-01-14 17:37:42 -0800 | [diff] [blame] | 1173 | // First add the layer to the purgatory list, which makes sure it won't |
| 1174 | // go away, then remove it from the main list (through a transaction). |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1175 | ssize_t err = removeLayer_l(layerBase); |
Mathias Agopian | f4dfe1b | 2011-01-14 17:37:42 -0800 | [diff] [blame] | 1176 | if (err >= 0) { |
| 1177 | mLayerPurgatory.add(layerBase); |
| 1178 | } |
Mathias Agopian | 2e4b68d | 2009-09-23 16:44:00 -0700 | [diff] [blame] | 1179 | |
Mathias Agopian | 0c4cec7 | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 1180 | layerBase->onRemoved(); |
| 1181 | |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1182 | // it's possible that we don't find a layer, because it might |
| 1183 | // have been destroyed already -- this is not technically an error |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1184 | // from the user because there is a race between Client::destroySurface(), |
| 1185 | // ~Client() and ~ISurface(). |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1186 | return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err; |
| 1187 | } |
| 1188 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1189 | status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1191 | layer->forceVisibilityTransaction(); |
| 1192 | setTransactionFlags(eTraversalNeeded); |
| 1193 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Mathias Agopian | 6dcb155 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 1196 | uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) |
| 1197 | { |
| 1198 | return android_atomic_release_load(&mTransactionFlags); |
| 1199 | } |
| 1200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) |
| 1202 | { |
| 1203 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 1204 | } |
| 1205 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1206 | uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | { |
| 1208 | uint32_t old = android_atomic_or(flags, &mTransactionFlags); |
| 1209 | if ((old & flags)==0) { // wake the server up |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1210 | signalEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | } |
| 1212 | return old; |
| 1213 | } |
| 1214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1216 | void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state) { |
| 1217 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1218 | |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1219 | uint32_t flags = 0; |
| 1220 | const size_t count = state.size(); |
| 1221 | for (size_t i=0 ; i<count ; i++) { |
| 1222 | const ComposerState& s(state[i]); |
| 1223 | sp<Client> client( static_cast<Client *>(s.client.get()) ); |
| 1224 | flags |= setClientStateLocked(client, s.state); |
| 1225 | } |
| 1226 | if (flags) { |
| 1227 | setTransactionFlags(flags); |
| 1228 | } |
| 1229 | |
| 1230 | signalEvent(); |
| 1231 | |
| 1232 | // if there is a transaction with a resize, wait for it to |
| 1233 | // take effect before returning. |
| 1234 | while (mResizeTransationPending) { |
| 1235 | status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); |
| 1236 | if (CC_UNLIKELY(err != NO_ERROR)) { |
| 1237 | // just in case something goes wrong in SF, return to the |
| 1238 | // called after a few seconds. |
| 1239 | LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!"); |
| 1240 | mResizeTransationPending = false; |
| 1241 | break; |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags) |
| 1247 | { |
| 1248 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1249 | return BAD_VALUE; |
| 1250 | |
| 1251 | Mutex::Autolock _l(mStateLock); |
| 1252 | mCurrentState.freezeDisplay = 1; |
| 1253 | setTransactionFlags(eTransactionNeeded); |
| 1254 | |
| 1255 | // flags is intended to communicate some sort of animation behavior |
Mathias Agopian | ed81f22 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 1256 | // (for instance fading) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | return NO_ERROR; |
| 1258 | } |
| 1259 | |
| 1260 | status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags) |
| 1261 | { |
| 1262 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1263 | return BAD_VALUE; |
| 1264 | |
| 1265 | Mutex::Autolock _l(mStateLock); |
| 1266 | mCurrentState.freezeDisplay = 0; |
| 1267 | setTransactionFlags(eTransactionNeeded); |
| 1268 | |
| 1269 | // flags is intended to communicate some sort of animation behavior |
Mathias Agopian | ed81f22 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 1270 | // (for instance fading) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1271 | return NO_ERROR; |
| 1272 | } |
| 1273 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1274 | int SurfaceFlinger::setOrientation(DisplayID dpy, |
Mathias Agopian | eb0c86e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 1275 | int orientation, uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | { |
| 1277 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1278 | return BAD_VALUE; |
| 1279 | |
| 1280 | Mutex::Autolock _l(mStateLock); |
| 1281 | if (mCurrentState.orientation != orientation) { |
| 1282 | if (uint32_t(orientation)<=eOrientation270 || orientation==42) { |
Mathias Agopian | eb0c86e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 1283 | mCurrentState.orientationType = flags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | mCurrentState.orientation = orientation; |
| 1285 | setTransactionFlags(eTransactionNeeded); |
| 1286 | mTransactionCV.wait(mStateLock); |
| 1287 | } else { |
| 1288 | orientation = BAD_VALUE; |
| 1289 | } |
| 1290 | } |
| 1291 | return orientation; |
| 1292 | } |
| 1293 | |
Mathias Agopian | 9638e5c | 2011-04-20 14:19:32 -0700 | [diff] [blame] | 1294 | sp<ISurface> SurfaceFlinger::createSurface( |
| 1295 | ISurfaceComposerClient::surface_data_t* params, |
| 1296 | const String8& name, |
| 1297 | const sp<Client>& client, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | DisplayID d, uint32_t w, uint32_t h, PixelFormat format, |
| 1299 | uint32_t flags) |
| 1300 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1301 | sp<LayerBaseClient> layer; |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1302 | sp<ISurface> surfaceHandle; |
Mathias Agopian | 4d2dbeb | 2009-07-09 18:16:43 -0700 | [diff] [blame] | 1303 | |
| 1304 | if (int32_t(w|h) < 0) { |
| 1305 | LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)", |
| 1306 | int(w), int(h)); |
| 1307 | return surfaceHandle; |
| 1308 | } |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | //LOGD("createSurface for pid %d (%d x %d)", pid, w, h); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1311 | sp<Layer> normalLayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1312 | switch (flags & eFXSurfaceMask) { |
| 1313 | case eFXSurfaceNormal: |
Mathias Agopian | d211230 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 1314 | normalLayer = createNormalSurface(client, d, w, h, flags, format); |
| 1315 | layer = normalLayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1316 | break; |
| 1317 | case eFXSurfaceBlur: |
Mathias Agopian | c3802d2 | 2010-12-08 17:13:19 -0800 | [diff] [blame] | 1318 | // for now we treat Blur as Dim, until we can implement it |
| 1319 | // efficiently. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1320 | case eFXSurfaceDim: |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1321 | layer = createDimSurface(client, d, w, h, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1322 | break; |
| 1323 | } |
| 1324 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1325 | if (layer != 0) { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1326 | layer->initStates(w, h, flags); |
Mathias Agopian | 5d26c1e | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 1327 | layer->setName(name); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1328 | ssize_t token = addClientLayer(client, layer); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1329 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1330 | surfaceHandle = layer->getSurface(); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1331 | if (surfaceHandle != 0) { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1332 | params->token = token; |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1333 | params->identity = layer->getIdentity(); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1334 | if (normalLayer != 0) { |
| 1335 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1336 | mLayerMap.add(layer->getSurfaceBinder(), normalLayer); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1337 | } |
Mathias Agopian | 18b6b49 | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1338 | } |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1339 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1340 | setTransactionFlags(eTransactionNeeded); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | return surfaceHandle; |
| 1344 | } |
| 1345 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1346 | sp<Layer> SurfaceFlinger::createNormalSurface( |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1347 | const sp<Client>& client, DisplayID display, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1348 | uint32_t w, uint32_t h, uint32_t flags, |
Mathias Agopian | 18b6b49 | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1349 | PixelFormat& format) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | { |
| 1351 | // initialize the surfaces |
| 1352 | switch (format) { // TODO: take h/w into account |
| 1353 | case PIXEL_FORMAT_TRANSPARENT: |
| 1354 | case PIXEL_FORMAT_TRANSLUCENT: |
| 1355 | format = PIXEL_FORMAT_RGBA_8888; |
| 1356 | break; |
| 1357 | case PIXEL_FORMAT_OPAQUE: |
Mathias Agopian | 4cfb3a6 | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1358 | #ifdef NO_RGBX_8888 |
| 1359 | format = PIXEL_FORMAT_RGB_565; |
| 1360 | #else |
Mathias Agopian | 59962ce | 2010-04-05 18:01:24 -0700 | [diff] [blame] | 1361 | format = PIXEL_FORMAT_RGBX_8888; |
Mathias Agopian | 4cfb3a6 | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1362 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | break; |
| 1364 | } |
| 1365 | |
Mathias Agopian | 4cfb3a6 | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1366 | #ifdef NO_RGBX_8888 |
| 1367 | if (format == PIXEL_FORMAT_RGBX_8888) |
| 1368 | format = PIXEL_FORMAT_RGBA_8888; |
| 1369 | #endif |
| 1370 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1371 | sp<Layer> layer = new Layer(this, display, client); |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1372 | status_t err = layer->setBuffers(w, h, format, flags); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1373 | if (LIKELY(err != NO_ERROR)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1374 | LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1375 | layer.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | } |
| 1377 | return layer; |
| 1378 | } |
| 1379 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1380 | sp<LayerDim> SurfaceFlinger::createDimSurface( |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1381 | const sp<Client>& client, DisplayID display, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1382 | uint32_t w, uint32_t h, uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1384 | sp<LayerDim> layer = new LayerDim(this, display, client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | layer->initStates(w, h, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1386 | return layer; |
| 1387 | } |
| 1388 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1389 | status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | { |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1391 | /* |
| 1392 | * called by the window manager, when a surface should be marked for |
| 1393 | * destruction. |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1394 | * |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1395 | * The surface is removed from the current and drawing lists, but placed |
| 1396 | * in the purgatory queue, so it's not destroyed right-away (we need |
| 1397 | * to wait for all client's references to go away first). |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1398 | */ |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1399 | |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1400 | status_t err = NAME_NOT_FOUND; |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1401 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1402 | sp<LayerBaseClient> layer = client->getLayerUser(sid); |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1403 | if (layer != 0) { |
| 1404 | err = purgatorizeLayer_l(layer); |
| 1405 | if (err == NO_ERROR) { |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1406 | setTransactionFlags(eTransactionNeeded); |
| 1407 | } |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1408 | } |
| 1409 | return err; |
| 1410 | } |
| 1411 | |
Mathias Agopian | 6960811 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1412 | status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer) |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1413 | { |
Mathias Agopian | 359140c | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 1414 | // called by ~ISurface() when all references are gone |
Mathias Agopian | 6960811 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1415 | status_t err = NO_ERROR; |
| 1416 | sp<LayerBaseClient> l(layer.promote()); |
| 1417 | if (l != NULL) { |
| 1418 | Mutex::Autolock _l(mStateLock); |
| 1419 | err = removeLayer_l(l); |
| 1420 | if (err == NAME_NOT_FOUND) { |
| 1421 | // The surface wasn't in the current list, which means it was |
| 1422 | // removed already, which means it is in the purgatory, |
| 1423 | // and need to be removed from there. |
| 1424 | ssize_t idx = mLayerPurgatory.remove(l); |
| 1425 | LOGE_IF(idx < 0, |
| 1426 | "layer=%p is not in the purgatory list", l.get()); |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1427 | } |
Mathias Agopian | 6960811 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1428 | LOGE_IF(err<0 && err != NAME_NOT_FOUND, |
| 1429 | "error removing layer=%p (%s)", l.get(), strerror(-err)); |
| 1430 | } |
| 1431 | return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1434 | uint32_t SurfaceFlinger::setClientStateLocked( |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1435 | const sp<Client>& client, |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1436 | const layer_state_t& s) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1437 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | uint32_t flags = 0; |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1439 | sp<LayerBaseClient> layer(client->getLayerUser(s.surface)); |
| 1440 | if (layer != 0) { |
| 1441 | const uint32_t what = s.what; |
| 1442 | if (what & ePositionChanged) { |
| 1443 | if (layer->setPosition(s.x, s.y)) |
| 1444 | flags |= eTraversalNeeded; |
| 1445 | } |
| 1446 | if (what & eLayerChanged) { |
| 1447 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
| 1448 | if (layer->setLayer(s.z)) { |
| 1449 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 1450 | mCurrentState.layersSortedByZ.add(layer); |
| 1451 | // we need traversal (state changed) |
| 1452 | // AND transaction (list changed) |
| 1453 | flags |= eTransactionNeeded|eTraversalNeeded; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1454 | } |
| 1455 | } |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1456 | if (what & eSizeChanged) { |
| 1457 | if (layer->setSize(s.w, s.h)) { |
| 1458 | flags |= eTraversalNeeded; |
| 1459 | mResizeTransationPending = true; |
| 1460 | } |
| 1461 | } |
| 1462 | if (what & eAlphaChanged) { |
| 1463 | if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f))) |
| 1464 | flags |= eTraversalNeeded; |
| 1465 | } |
| 1466 | if (what & eMatrixChanged) { |
| 1467 | if (layer->setMatrix(s.matrix)) |
| 1468 | flags |= eTraversalNeeded; |
| 1469 | } |
| 1470 | if (what & eTransparentRegionChanged) { |
| 1471 | if (layer->setTransparentRegionHint(s.transparentRegion)) |
| 1472 | flags |= eTraversalNeeded; |
| 1473 | } |
| 1474 | if (what & eVisibilityChanged) { |
| 1475 | if (layer->setFlags(s.flags, s.mask)) |
| 1476 | flags |= eTraversalNeeded; |
| 1477 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | } |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1479 | return flags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | void SurfaceFlinger::screenReleased(int dpy) |
| 1483 | { |
| 1484 | // this may be called by a signal handler, we can't do too much in here |
| 1485 | android_atomic_or(eConsoleReleased, &mConsoleSignals); |
| 1486 | signalEvent(); |
| 1487 | } |
| 1488 | |
| 1489 | void SurfaceFlinger::screenAcquired(int dpy) |
| 1490 | { |
| 1491 | // this may be called by a signal handler, we can't do too much in here |
| 1492 | android_atomic_or(eConsoleAcquired, &mConsoleSignals); |
| 1493 | signalEvent(); |
| 1494 | } |
| 1495 | |
| 1496 | status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args) |
| 1497 | { |
Erik Gilling | 94720d7 | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 1498 | const size_t SIZE = 4096; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | char buffer[SIZE]; |
| 1500 | String8 result; |
Mathias Agopian | 0dd593f | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1501 | |
| 1502 | if (!PermissionCache::checkCallingPermission(sDump)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1504 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", |
| 1505 | IPCThreadState::self()->getCallingPid(), |
| 1506 | IPCThreadState::self()->getCallingUid()); |
| 1507 | result.append(buffer); |
| 1508 | } else { |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1509 | |
| 1510 | // figure out if we're stuck somewhere |
| 1511 | const nsecs_t now = systemTime(); |
| 1512 | const nsecs_t inSwapBuffers(mDebugInSwapBuffers); |
| 1513 | const nsecs_t inTransaction(mDebugInTransaction); |
| 1514 | nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0; |
| 1515 | nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; |
| 1516 | |
| 1517 | // Try to get the main lock, but don't insist if we can't |
| 1518 | // (this would indicate SF is stuck, but we want to be able to |
| 1519 | // print something in dumpsys). |
| 1520 | int retry = 3; |
| 1521 | while (mStateLock.tryLock()<0 && --retry>=0) { |
| 1522 | usleep(1000000); |
| 1523 | } |
| 1524 | const bool locked(retry >= 0); |
| 1525 | if (!locked) { |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1526 | snprintf(buffer, SIZE, |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1527 | "SurfaceFlinger appears to be unresponsive, " |
| 1528 | "dumping anyways (no locks held)\n"); |
| 1529 | result.append(buffer); |
| 1530 | } |
| 1531 | |
Mathias Agopian | 06a61e2 | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1532 | /* |
| 1533 | * Dump the visible layer list |
| 1534 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 1536 | const size_t count = currentLayers.size(); |
Mathias Agopian | 06a61e2 | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1537 | snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count); |
| 1538 | result.append(buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1540 | const sp<LayerBase>& layer(currentLayers[i]); |
| 1541 | layer->dump(result, buffer, SIZE); |
| 1542 | const Layer::State& s(layer->drawingState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1543 | s.transparentRegion.dump(result, "transparentRegion"); |
| 1544 | layer->transparentRegionScreen.dump(result, "transparentRegionScreen"); |
| 1545 | layer->visibleRegionScreen.dump(result, "visibleRegionScreen"); |
| 1546 | } |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1547 | |
Mathias Agopian | 06a61e2 | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1548 | /* |
| 1549 | * Dump the layers in the purgatory |
| 1550 | */ |
| 1551 | |
| 1552 | const size_t purgatorySize = mLayerPurgatory.size(); |
| 1553 | snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize); |
| 1554 | result.append(buffer); |
| 1555 | for (size_t i=0 ; i<purgatorySize ; i++) { |
| 1556 | const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i)); |
| 1557 | layer->shortDump(result, buffer, SIZE); |
| 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * Dump SurfaceFlinger global state |
| 1562 | */ |
| 1563 | |
Mathias Agopian | ab95117 | 2011-07-14 18:01:49 -0700 | [diff] [blame] | 1564 | snprintf(buffer, SIZE, "SurfaceFlinger global state:\n"); |
Mathias Agopian | 06a61e2 | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1565 | result.append(buffer); |
Mathias Agopian | ab95117 | 2011-07-14 18:01:49 -0700 | [diff] [blame] | 1566 | |
| 1567 | const GLExtensions& extensions(GLExtensions::getInstance()); |
| 1568 | snprintf(buffer, SIZE, "GLES: %s, %s, %s\n", |
| 1569 | extensions.getVendor(), |
| 1570 | extensions.getRenderer(), |
| 1571 | extensions.getVersion()); |
| 1572 | result.append(buffer); |
| 1573 | snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension()); |
| 1574 | result.append(buffer); |
| 1575 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1576 | mWormholeRegion.dump(result, "WormholeRegion"); |
| 1577 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1578 | snprintf(buffer, SIZE, |
| 1579 | " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n", |
| 1580 | mFreezeDisplay?"yes":"no", mFreezeCount, |
| 1581 | mCurrentState.orientation, hw.canDraw()); |
| 1582 | result.append(buffer); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1583 | snprintf(buffer, SIZE, |
| 1584 | " last eglSwapBuffers() time: %f us\n" |
| 1585 | " last transaction time : %f us\n", |
| 1586 | mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0); |
| 1587 | result.append(buffer); |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1588 | |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1589 | if (inSwapBuffersDuration || !locked) { |
| 1590 | snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n", |
| 1591 | inSwapBuffersDuration/1000.0); |
| 1592 | result.append(buffer); |
| 1593 | } |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1594 | |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1595 | if (inTransactionDuration || !locked) { |
| 1596 | snprintf(buffer, SIZE, " transaction time: %f us\n", |
| 1597 | inTransactionDuration/1000.0); |
| 1598 | result.append(buffer); |
| 1599 | } |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1600 | |
Mathias Agopian | 06a61e2 | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1601 | /* |
| 1602 | * Dump HWComposer state |
| 1603 | */ |
Mathias Agopian | 6a96924 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 1604 | HWComposer& hwc(hw.getHwComposer()); |
| 1605 | snprintf(buffer, SIZE, " h/w composer %s and %s\n", |
| 1606 | hwc.initCheck()==NO_ERROR ? "present" : "not present", |
Mathias Agopian | 7f76a3c | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1607 | (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled"); |
Mathias Agopian | 6a96924 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 1608 | result.append(buffer); |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 1609 | hwc.dump(result, buffer, SIZE); |
Mathias Agopian | 6a96924 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 1610 | |
Mathias Agopian | 06a61e2 | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1611 | /* |
| 1612 | * Dump gralloc state |
| 1613 | */ |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1614 | const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1615 | alloc.dump(result); |
Erik Gilling | 94720d7 | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 1616 | hw.dump(result); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1617 | |
| 1618 | if (locked) { |
| 1619 | mStateLock.unlock(); |
| 1620 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | } |
| 1622 | write(fd, result.string(), result.size()); |
| 1623 | return NO_ERROR; |
| 1624 | } |
| 1625 | |
| 1626 | status_t SurfaceFlinger::onTransact( |
| 1627 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1628 | { |
| 1629 | switch (code) { |
| 1630 | case CREATE_CONNECTION: |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1631 | case SET_TRANSACTION_STATE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | case SET_ORIENTATION: |
| 1633 | case FREEZE_DISPLAY: |
| 1634 | case UNFREEZE_DISPLAY: |
| 1635 | case BOOT_FINISHED: |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1636 | case TURN_ELECTRON_BEAM_OFF: |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1637 | case TURN_ELECTRON_BEAM_ON: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1638 | { |
| 1639 | // codes that require permission check |
| 1640 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1641 | const int pid = ipc->getCallingPid(); |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 1642 | const int uid = ipc->getCallingUid(); |
Mathias Agopian | 0dd593f | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1643 | if ((uid != AID_GRAPHICS) && |
| 1644 | !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) { |
Mathias Agopian | 151e859 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1645 | LOGE("Permission Denial: " |
| 1646 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
| 1647 | return PERMISSION_DENIED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1648 | } |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1649 | break; |
| 1650 | } |
| 1651 | case CAPTURE_SCREEN: |
| 1652 | { |
| 1653 | // codes that require permission check |
| 1654 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1655 | const int pid = ipc->getCallingPid(); |
| 1656 | const int uid = ipc->getCallingUid(); |
Mathias Agopian | 0dd593f | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1657 | if ((uid != AID_GRAPHICS) && |
| 1658 | !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1659 | LOGE("Permission Denial: " |
| 1660 | "can't read framebuffer pid=%d, uid=%d", pid, uid); |
| 1661 | return PERMISSION_DENIED; |
| 1662 | } |
| 1663 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | } |
| 1665 | } |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1666 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); |
| 1668 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 1669 | CHECK_INTERFACE(ISurfaceComposer, data, reply); |
Mathias Agopian | 0dd593f | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1670 | if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) { |
Mathias Agopian | 151e859 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1671 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1672 | const int pid = ipc->getCallingPid(); |
| 1673 | const int uid = ipc->getCallingUid(); |
| 1674 | LOGE("Permission Denial: " |
| 1675 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1676 | return PERMISSION_DENIED; |
| 1677 | } |
| 1678 | int n; |
| 1679 | switch (code) { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1680 | case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 1681 | case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | return NO_ERROR; |
| 1683 | case 1002: // SHOW_UPDATES |
| 1684 | n = data.readInt32(); |
| 1685 | mDebugRegion = n ? n : (mDebugRegion ? 0 : 1); |
Mathias Agopian | 7f76a3c | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1686 | invalidateHwcGeometry(); |
| 1687 | repaintEverything(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | return NO_ERROR; |
| 1689 | case 1003: // SHOW_BACKGROUND |
| 1690 | n = data.readInt32(); |
| 1691 | mDebugBackground = n ? 1 : 0; |
| 1692 | return NO_ERROR; |
| 1693 | case 1004:{ // repaint everything |
Mathias Agopian | 7f76a3c | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1694 | repaintEverything(); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1695 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1697 | case 1005:{ // force transaction |
| 1698 | setTransactionFlags(eTransactionNeeded|eTraversalNeeded); |
| 1699 | return NO_ERROR; |
| 1700 | } |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 1701 | case 1006:{ // enable/disable GraphicLog |
| 1702 | int enabled = data.readInt32(); |
| 1703 | GraphicLog::getInstance().setEnabled(enabled); |
| 1704 | return NO_ERROR; |
| 1705 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1706 | case 1007: // set mFreezeCount |
| 1707 | mFreezeCount = data.readInt32(); |
Mathias Agopian | 0e44976 | 2009-12-01 17:23:28 -0800 | [diff] [blame] | 1708 | mFreezeDisplayTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | return NO_ERROR; |
Mathias Agopian | 7f76a3c | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1710 | case 1008: // toggle use of hw composer |
| 1711 | n = data.readInt32(); |
| 1712 | mDebugDisableHWC = n ? 1 : 0; |
| 1713 | invalidateHwcGeometry(); |
| 1714 | repaintEverything(); |
| 1715 | return NO_ERROR; |
Mathias Agopian | 2143fe0 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 1716 | case 1009: // toggle use of transform hint |
| 1717 | n = data.readInt32(); |
| 1718 | mDebugDisableTransformHint = n ? 1 : 0; |
| 1719 | invalidateHwcGeometry(); |
| 1720 | repaintEverything(); |
| 1721 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | case 1010: // interrogate. |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1723 | reply->writeInt32(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | reply->writeInt32(0); |
| 1725 | reply->writeInt32(mDebugRegion); |
| 1726 | reply->writeInt32(mDebugBackground); |
| 1727 | return NO_ERROR; |
| 1728 | case 1013: { |
| 1729 | Mutex::Autolock _l(mStateLock); |
| 1730 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1731 | reply->writeInt32(hw.getPageFlipCount()); |
| 1732 | } |
| 1733 | return NO_ERROR; |
| 1734 | } |
| 1735 | } |
| 1736 | return err; |
| 1737 | } |
| 1738 | |
Mathias Agopian | 7f76a3c | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1739 | void SurfaceFlinger::repaintEverything() { |
| 1740 | Mutex::Autolock _l(mStateLock); |
| 1741 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1742 | mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe |
| 1743 | signalEvent(); |
| 1744 | } |
| 1745 | |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1746 | // --------------------------------------------------------------------------- |
| 1747 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1748 | status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy, |
| 1749 | GLuint* textureName, GLfloat* uOut, GLfloat* vOut) |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1750 | { |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1751 | if (!GLExtensions::getInstance().haveFramebufferObject()) |
| 1752 | return INVALID_OPERATION; |
| 1753 | |
| 1754 | // get screen geometry |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1755 | const DisplayHardware& hw(graphicPlane(dpy).displayHardware()); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1756 | const uint32_t hw_w = hw.getWidth(); |
| 1757 | const uint32_t hw_h = hw.getHeight(); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1758 | GLfloat u = 1; |
| 1759 | GLfloat v = 1; |
| 1760 | |
| 1761 | // make sure to clear all GL error flags |
| 1762 | while ( glGetError() != GL_NO_ERROR ) ; |
| 1763 | |
| 1764 | // create a FBO |
| 1765 | GLuint name, tname; |
| 1766 | glGenTextures(1, &tname); |
| 1767 | glBindTexture(GL_TEXTURE_2D, tname); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1768 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, |
| 1769 | hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1770 | if (glGetError() != GL_NO_ERROR) { |
Mathias Agopian | a6cd6d3 | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 1771 | while ( glGetError() != GL_NO_ERROR ) ; |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1772 | GLint tw = (2 << (31 - clz(hw_w))); |
| 1773 | GLint th = (2 << (31 - clz(hw_h))); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1774 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, |
| 1775 | tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1776 | u = GLfloat(hw_w) / tw; |
| 1777 | v = GLfloat(hw_h) / th; |
| 1778 | } |
| 1779 | glGenFramebuffersOES(1, &name); |
| 1780 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1781 | glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, |
| 1782 | GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1783 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1784 | // redraw the screen entirely... |
| 1785 | glClearColor(0,0,0,1); |
| 1786 | glClear(GL_COLOR_BUFFER_BIT); |
| 1787 | const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); |
| 1788 | const size_t count = layers.size(); |
| 1789 | for (size_t i=0 ; i<count ; ++i) { |
| 1790 | const sp<LayerBase>& layer(layers[i]); |
| 1791 | layer->drawForSreenShot(); |
| 1792 | } |
| 1793 | |
| 1794 | // back to main framebuffer |
| 1795 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); |
| 1796 | glDisable(GL_SCISSOR_TEST); |
| 1797 | glDeleteFramebuffersOES(1, &name); |
| 1798 | |
| 1799 | *textureName = tname; |
| 1800 | *uOut = u; |
| 1801 | *vOut = v; |
| 1802 | return NO_ERROR; |
| 1803 | } |
| 1804 | |
| 1805 | // --------------------------------------------------------------------------- |
| 1806 | |
| 1807 | status_t SurfaceFlinger::electronBeamOffAnimationImplLocked() |
| 1808 | { |
| 1809 | status_t result = PERMISSION_DENIED; |
| 1810 | |
| 1811 | if (!GLExtensions::getInstance().haveFramebufferObject()) |
| 1812 | return INVALID_OPERATION; |
| 1813 | |
| 1814 | // get screen geometry |
| 1815 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1816 | const uint32_t hw_w = hw.getWidth(); |
| 1817 | const uint32_t hw_h = hw.getHeight(); |
| 1818 | const Region screenBounds(hw.bounds()); |
| 1819 | |
| 1820 | GLfloat u, v; |
| 1821 | GLuint tname; |
| 1822 | result = renderScreenToTextureLocked(0, &tname, &u, &v); |
| 1823 | if (result != NO_ERROR) { |
| 1824 | return result; |
| 1825 | } |
| 1826 | |
| 1827 | GLfloat vtx[8]; |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1828 | const GLfloat texCoords[4][2] = { {0,1}, {0,1-v}, {u,1-v}, {u,1} }; |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1829 | glBindTexture(GL_TEXTURE_2D, tname); |
| 1830 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 1831 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 1832 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 1833 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); |
| 1834 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 1835 | glVertexPointer(2, GL_FLOAT, 0, vtx); |
| 1836 | |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1837 | /* |
| 1838 | * Texture coordinate mapping |
| 1839 | * |
| 1840 | * u |
| 1841 | * 1 +----------+---+ |
| 1842 | * | | | | image is inverted |
| 1843 | * | V | | w.r.t. the texture |
| 1844 | * 1-v +----------+ | coordinates |
| 1845 | * | | |
| 1846 | * | | |
| 1847 | * | | |
| 1848 | * 0 +--------------+ |
| 1849 | * 0 1 |
| 1850 | * |
| 1851 | */ |
| 1852 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1853 | class s_curve_interpolator { |
| 1854 | const float nbFrames, s, v; |
| 1855 | public: |
| 1856 | s_curve_interpolator(int nbFrames, float s) |
| 1857 | : nbFrames(1.0f / (nbFrames-1)), s(s), |
| 1858 | v(1.0f + expf(-s + 0.5f*s)) { |
| 1859 | } |
| 1860 | float operator()(int f) { |
| 1861 | const float x = f * nbFrames; |
| 1862 | return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f; |
| 1863 | } |
| 1864 | }; |
| 1865 | |
| 1866 | class v_stretch { |
| 1867 | const GLfloat hw_w, hw_h; |
| 1868 | public: |
| 1869 | v_stretch(uint32_t hw_w, uint32_t hw_h) |
| 1870 | : hw_w(hw_w), hw_h(hw_h) { |
| 1871 | } |
| 1872 | void operator()(GLfloat* vtx, float v) { |
| 1873 | const GLfloat w = hw_w + (hw_w * v); |
| 1874 | const GLfloat h = hw_h - (hw_h * v); |
| 1875 | const GLfloat x = (hw_w - w) * 0.5f; |
| 1876 | const GLfloat y = (hw_h - h) * 0.5f; |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1877 | vtx[0] = x; vtx[1] = y; |
| 1878 | vtx[2] = x; vtx[3] = y + h; |
| 1879 | vtx[4] = x + w; vtx[5] = y + h; |
| 1880 | vtx[6] = x + w; vtx[7] = y; |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1881 | } |
| 1882 | }; |
| 1883 | |
| 1884 | class h_stretch { |
| 1885 | const GLfloat hw_w, hw_h; |
| 1886 | public: |
| 1887 | h_stretch(uint32_t hw_w, uint32_t hw_h) |
| 1888 | : hw_w(hw_w), hw_h(hw_h) { |
| 1889 | } |
| 1890 | void operator()(GLfloat* vtx, float v) { |
| 1891 | const GLfloat w = hw_w - (hw_w * v); |
| 1892 | const GLfloat h = 1.0f; |
| 1893 | const GLfloat x = (hw_w - w) * 0.5f; |
| 1894 | const GLfloat y = (hw_h - h) * 0.5f; |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1895 | vtx[0] = x; vtx[1] = y; |
| 1896 | vtx[2] = x; vtx[3] = y + h; |
| 1897 | vtx[4] = x + w; vtx[5] = y + h; |
| 1898 | vtx[6] = x + w; vtx[7] = y; |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1899 | } |
| 1900 | }; |
| 1901 | |
| 1902 | // the full animation is 24 frames |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1903 | char value[PROPERTY_VALUE_MAX]; |
| 1904 | property_get("debug.sf.electron_frames", value, "24"); |
| 1905 | int nbFrames = (atoi(value) + 1) >> 1; |
| 1906 | if (nbFrames <= 0) // just in case |
| 1907 | nbFrames = 24; |
| 1908 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1909 | s_curve_interpolator itr(nbFrames, 7.5f); |
| 1910 | s_curve_interpolator itg(nbFrames, 8.0f); |
| 1911 | s_curve_interpolator itb(nbFrames, 8.5f); |
| 1912 | |
| 1913 | v_stretch vverts(hw_w, hw_h); |
| 1914 | glEnable(GL_BLEND); |
| 1915 | glBlendFunc(GL_ONE, GL_ONE); |
| 1916 | for (int i=0 ; i<nbFrames ; i++) { |
| 1917 | float x, y, w, h; |
| 1918 | const float vr = itr(i); |
| 1919 | const float vg = itg(i); |
| 1920 | const float vb = itb(i); |
| 1921 | |
| 1922 | // clear screen |
| 1923 | glColorMask(1,1,1,1); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1924 | glClear(GL_COLOR_BUFFER_BIT); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1925 | glEnable(GL_TEXTURE_2D); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1926 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1927 | // draw the red plane |
| 1928 | vverts(vtx, vr); |
| 1929 | glColorMask(1,0,0,1); |
| 1930 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1931 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1932 | // draw the green plane |
| 1933 | vverts(vtx, vg); |
| 1934 | glColorMask(0,1,0,1); |
| 1935 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1936 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1937 | // draw the blue plane |
| 1938 | vverts(vtx, vb); |
| 1939 | glColorMask(0,0,1,1); |
| 1940 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1941 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1942 | // draw the white highlight (we use the last vertices) |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1943 | glDisable(GL_TEXTURE_2D); |
| 1944 | glColorMask(1,1,1,1); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1945 | glColor4f(vg, vg, vg, 1); |
| 1946 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1947 | hw.flip(screenBounds); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1950 | h_stretch hverts(hw_w, hw_h); |
| 1951 | glDisable(GL_BLEND); |
| 1952 | glDisable(GL_TEXTURE_2D); |
| 1953 | glColorMask(1,1,1,1); |
| 1954 | for (int i=0 ; i<nbFrames ; i++) { |
| 1955 | const float v = itg(i); |
| 1956 | hverts(vtx, v); |
| 1957 | glClear(GL_COLOR_BUFFER_BIT); |
| 1958 | glColor4f(1-v, 1-v, 1-v, 1); |
| 1959 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1960 | hw.flip(screenBounds); |
| 1961 | } |
| 1962 | |
| 1963 | glColorMask(1,1,1,1); |
| 1964 | glEnable(GL_SCISSOR_TEST); |
| 1965 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 1966 | glDeleteTextures(1, &tname); |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1967 | glDisable(GL_TEXTURE_2D); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1968 | return NO_ERROR; |
| 1969 | } |
| 1970 | |
| 1971 | status_t SurfaceFlinger::electronBeamOnAnimationImplLocked() |
| 1972 | { |
| 1973 | status_t result = PERMISSION_DENIED; |
| 1974 | |
| 1975 | if (!GLExtensions::getInstance().haveFramebufferObject()) |
| 1976 | return INVALID_OPERATION; |
| 1977 | |
| 1978 | |
| 1979 | // get screen geometry |
| 1980 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1981 | const uint32_t hw_w = hw.getWidth(); |
| 1982 | const uint32_t hw_h = hw.getHeight(); |
| 1983 | const Region screenBounds(hw.bounds()); |
| 1984 | |
| 1985 | GLfloat u, v; |
| 1986 | GLuint tname; |
| 1987 | result = renderScreenToTextureLocked(0, &tname, &u, &v); |
| 1988 | if (result != NO_ERROR) { |
| 1989 | return result; |
| 1990 | } |
| 1991 | |
| 1992 | // back to main framebuffer |
| 1993 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); |
| 1994 | glDisable(GL_SCISSOR_TEST); |
| 1995 | |
| 1996 | GLfloat vtx[8]; |
| 1997 | const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} }; |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1998 | glBindTexture(GL_TEXTURE_2D, tname); |
| 1999 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 2000 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 2001 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 2002 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); |
| 2003 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 2004 | glVertexPointer(2, GL_FLOAT, 0, vtx); |
| 2005 | |
| 2006 | class s_curve_interpolator { |
| 2007 | const float nbFrames, s, v; |
| 2008 | public: |
| 2009 | s_curve_interpolator(int nbFrames, float s) |
| 2010 | : nbFrames(1.0f / (nbFrames-1)), s(s), |
| 2011 | v(1.0f + expf(-s + 0.5f*s)) { |
| 2012 | } |
| 2013 | float operator()(int f) { |
| 2014 | const float x = f * nbFrames; |
| 2015 | return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f; |
| 2016 | } |
| 2017 | }; |
| 2018 | |
| 2019 | class v_stretch { |
| 2020 | const GLfloat hw_w, hw_h; |
| 2021 | public: |
| 2022 | v_stretch(uint32_t hw_w, uint32_t hw_h) |
| 2023 | : hw_w(hw_w), hw_h(hw_h) { |
| 2024 | } |
| 2025 | void operator()(GLfloat* vtx, float v) { |
| 2026 | const GLfloat w = hw_w + (hw_w * v); |
| 2027 | const GLfloat h = hw_h - (hw_h * v); |
| 2028 | const GLfloat x = (hw_w - w) * 0.5f; |
| 2029 | const GLfloat y = (hw_h - h) * 0.5f; |
| 2030 | vtx[0] = x; vtx[1] = y; |
| 2031 | vtx[2] = x; vtx[3] = y + h; |
| 2032 | vtx[4] = x + w; vtx[5] = y + h; |
| 2033 | vtx[6] = x + w; vtx[7] = y; |
| 2034 | } |
| 2035 | }; |
| 2036 | |
| 2037 | class h_stretch { |
| 2038 | const GLfloat hw_w, hw_h; |
| 2039 | public: |
| 2040 | h_stretch(uint32_t hw_w, uint32_t hw_h) |
| 2041 | : hw_w(hw_w), hw_h(hw_h) { |
| 2042 | } |
| 2043 | void operator()(GLfloat* vtx, float v) { |
| 2044 | const GLfloat w = hw_w - (hw_w * v); |
| 2045 | const GLfloat h = 1.0f; |
| 2046 | const GLfloat x = (hw_w - w) * 0.5f; |
| 2047 | const GLfloat y = (hw_h - h) * 0.5f; |
| 2048 | vtx[0] = x; vtx[1] = y; |
| 2049 | vtx[2] = x; vtx[3] = y + h; |
| 2050 | vtx[4] = x + w; vtx[5] = y + h; |
| 2051 | vtx[6] = x + w; vtx[7] = y; |
| 2052 | } |
| 2053 | }; |
| 2054 | |
Mathias Agopian | dfa08fb | 2010-10-14 12:33:07 -0700 | [diff] [blame] | 2055 | // the full animation is 12 frames |
| 2056 | int nbFrames = 8; |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2057 | s_curve_interpolator itr(nbFrames, 7.5f); |
| 2058 | s_curve_interpolator itg(nbFrames, 8.0f); |
| 2059 | s_curve_interpolator itb(nbFrames, 8.5f); |
| 2060 | |
| 2061 | h_stretch hverts(hw_w, hw_h); |
| 2062 | glDisable(GL_BLEND); |
| 2063 | glDisable(GL_TEXTURE_2D); |
| 2064 | glColorMask(1,1,1,1); |
| 2065 | for (int i=nbFrames-1 ; i>=0 ; i--) { |
| 2066 | const float v = itg(i); |
| 2067 | hverts(vtx, v); |
| 2068 | glClear(GL_COLOR_BUFFER_BIT); |
| 2069 | glColor4f(1-v, 1-v, 1-v, 1); |
| 2070 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2071 | hw.flip(screenBounds); |
| 2072 | } |
| 2073 | |
Mathias Agopian | dfa08fb | 2010-10-14 12:33:07 -0700 | [diff] [blame] | 2074 | nbFrames = 4; |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2075 | v_stretch vverts(hw_w, hw_h); |
| 2076 | glEnable(GL_BLEND); |
| 2077 | glBlendFunc(GL_ONE, GL_ONE); |
| 2078 | for (int i=nbFrames-1 ; i>=0 ; i--) { |
| 2079 | float x, y, w, h; |
| 2080 | const float vr = itr(i); |
| 2081 | const float vg = itg(i); |
| 2082 | const float vb = itb(i); |
| 2083 | |
| 2084 | // clear screen |
| 2085 | glColorMask(1,1,1,1); |
| 2086 | glClear(GL_COLOR_BUFFER_BIT); |
| 2087 | glEnable(GL_TEXTURE_2D); |
| 2088 | |
| 2089 | // draw the red plane |
| 2090 | vverts(vtx, vr); |
| 2091 | glColorMask(1,0,0,1); |
| 2092 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2093 | |
| 2094 | // draw the green plane |
| 2095 | vverts(vtx, vg); |
| 2096 | glColorMask(0,1,0,1); |
| 2097 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2098 | |
| 2099 | // draw the blue plane |
| 2100 | vverts(vtx, vb); |
| 2101 | glColorMask(0,0,1,1); |
| 2102 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2103 | |
| 2104 | hw.flip(screenBounds); |
| 2105 | } |
| 2106 | |
| 2107 | glColorMask(1,1,1,1); |
| 2108 | glEnable(GL_SCISSOR_TEST); |
| 2109 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2110 | glDeleteTextures(1, &tname); |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2111 | glDisable(GL_TEXTURE_2D); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2112 | |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2113 | return NO_ERROR; |
| 2114 | } |
| 2115 | |
| 2116 | // --------------------------------------------------------------------------- |
| 2117 | |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2118 | status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode) |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2119 | { |
| 2120 | DisplayHardware& hw(graphicPlane(0).editDisplayHardware()); |
| 2121 | if (!hw.canDraw()) { |
| 2122 | // we're already off |
| 2123 | return NO_ERROR; |
| 2124 | } |
Mathias Agopian | 7f97258 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 2125 | |
| 2126 | // turn off hwc while we're doing the animation |
| 2127 | hw.getHwComposer().disable(); |
| 2128 | // and make sure to turn it back on (if needed) next time we compose |
| 2129 | invalidateHwcGeometry(); |
| 2130 | |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2131 | if (mode & ISurfaceComposer::eElectronBeamAnimationOff) { |
| 2132 | electronBeamOffAnimationImplLocked(); |
| 2133 | } |
| 2134 | |
| 2135 | // always clear the whole screen at the end of the animation |
| 2136 | glClearColor(0,0,0,1); |
| 2137 | glDisable(GL_SCISSOR_TEST); |
| 2138 | glClear(GL_COLOR_BUFFER_BIT); |
| 2139 | glEnable(GL_SCISSOR_TEST); |
| 2140 | hw.flip( Region(hw.bounds()) ); |
| 2141 | |
Mathias Agopian | a6cd6d3 | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 2142 | hw.setCanDraw(false); |
| 2143 | return NO_ERROR; |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode) |
| 2147 | { |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2148 | class MessageTurnElectronBeamOff : public MessageBase { |
| 2149 | SurfaceFlinger* flinger; |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2150 | int32_t mode; |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2151 | status_t result; |
| 2152 | public: |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2153 | MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode) |
| 2154 | : flinger(flinger), mode(mode), result(PERMISSION_DENIED) { |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2155 | } |
| 2156 | status_t getResult() const { |
| 2157 | return result; |
| 2158 | } |
| 2159 | virtual bool handler() { |
| 2160 | Mutex::Autolock _l(flinger->mStateLock); |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2161 | result = flinger->turnElectronBeamOffImplLocked(mode); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2162 | return true; |
| 2163 | } |
| 2164 | }; |
| 2165 | |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2166 | sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode); |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2167 | status_t res = postMessageSync(msg); |
| 2168 | if (res == NO_ERROR) { |
| 2169 | res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult(); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2170 | |
| 2171 | // work-around: when the power-manager calls us we activate the |
| 2172 | // animation. eventually, the "on" animation will be called |
| 2173 | // by the power-manager itself |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2174 | mElectronBeamAnimationMode = mode; |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2175 | } |
| 2176 | return res; |
| 2177 | } |
| 2178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2179 | // --------------------------------------------------------------------------- |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2180 | |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2181 | status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode) |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2182 | { |
| 2183 | DisplayHardware& hw(graphicPlane(0).editDisplayHardware()); |
| 2184 | if (hw.canDraw()) { |
| 2185 | // we're already on |
| 2186 | return NO_ERROR; |
| 2187 | } |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2188 | if (mode & ISurfaceComposer::eElectronBeamAnimationOn) { |
| 2189 | electronBeamOnAnimationImplLocked(); |
| 2190 | } |
Mathias Agopian | a6cd6d3 | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 2191 | hw.setCanDraw(true); |
Mathias Agopian | 8b6a054 | 2010-10-14 12:46:24 -0700 | [diff] [blame] | 2192 | |
| 2193 | // make sure to redraw the whole screen when the animation is done |
| 2194 | mDirtyRegion.set(hw.bounds()); |
| 2195 | signalEvent(); |
| 2196 | |
Mathias Agopian | a6cd6d3 | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 2197 | return NO_ERROR; |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode) |
| 2201 | { |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2202 | class MessageTurnElectronBeamOn : public MessageBase { |
| 2203 | SurfaceFlinger* flinger; |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2204 | int32_t mode; |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2205 | status_t result; |
| 2206 | public: |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2207 | MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode) |
| 2208 | : flinger(flinger), mode(mode), result(PERMISSION_DENIED) { |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2209 | } |
| 2210 | status_t getResult() const { |
| 2211 | return result; |
| 2212 | } |
| 2213 | virtual bool handler() { |
| 2214 | Mutex::Autolock _l(flinger->mStateLock); |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2215 | result = flinger->turnElectronBeamOnImplLocked(mode); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2216 | return true; |
| 2217 | } |
| 2218 | }; |
| 2219 | |
Mathias Agopian | d4e03f3 | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2220 | postMessageAsync( new MessageTurnElectronBeamOn(this, mode) ); |
Mathias Agopian | 2d2b803 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2221 | return NO_ERROR; |
| 2222 | } |
| 2223 | |
| 2224 | // --------------------------------------------------------------------------- |
| 2225 | |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2226 | status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy, |
| 2227 | sp<IMemoryHeap>* heap, |
| 2228 | uint32_t* w, uint32_t* h, PixelFormat* f, |
Mathias Agopian | 3dd25a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2229 | uint32_t sw, uint32_t sh, |
| 2230 | uint32_t minLayerZ, uint32_t maxLayerZ) |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2231 | { |
| 2232 | status_t result = PERMISSION_DENIED; |
| 2233 | |
| 2234 | // only one display supported for now |
| 2235 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 2236 | return BAD_VALUE; |
| 2237 | |
Jamie Gennis | f72606c | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 2238 | // make sure none of the layers are protected |
Mathias Agopian | e328134 | 2011-08-24 17:44:41 -0700 | [diff] [blame] | 2239 | const LayerVector& layers(mDrawingState.layersSortedByZ); |
Jamie Gennis | f72606c | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 2240 | const size_t count = layers.size(); |
| 2241 | for (size_t i=0 ; i<count ; ++i) { |
| 2242 | const sp<LayerBase>& layer(layers[i]); |
Mathias Agopian | ec49d89 | 2011-08-25 14:36:43 -0700 | [diff] [blame] | 2243 | const uint32_t flags = layer->drawingState().flags; |
| 2244 | if (!(flags & ISurfaceComposer::eLayerHidden)) { |
| 2245 | const uint32_t z = layer->drawingState().z; |
| 2246 | if (z >= minLayerZ && z <= maxLayerZ) { |
| 2247 | if (layer->isProtected()) { |
| 2248 | return INVALID_OPERATION; |
| 2249 | } |
Jamie Gennis | f72606c | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2254 | if (!GLExtensions::getInstance().haveFramebufferObject()) |
| 2255 | return INVALID_OPERATION; |
| 2256 | |
| 2257 | // get screen geometry |
| 2258 | const DisplayHardware& hw(graphicPlane(dpy).displayHardware()); |
| 2259 | const uint32_t hw_w = hw.getWidth(); |
| 2260 | const uint32_t hw_h = hw.getHeight(); |
| 2261 | |
| 2262 | if ((sw > hw_w) || (sh > hw_h)) |
| 2263 | return BAD_VALUE; |
| 2264 | |
| 2265 | sw = (!sw) ? hw_w : sw; |
| 2266 | sh = (!sh) ? hw_h : sh; |
| 2267 | const size_t size = sw * sh * 4; |
| 2268 | |
Mathias Agopian | 32ae094 | 2011-03-02 18:45:50 -0800 | [diff] [blame] | 2269 | //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d", |
| 2270 | // sw, sh, minLayerZ, maxLayerZ); |
Mathias Agopian | cd2cfb6 | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2271 | |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2272 | // make sure to clear all GL error flags |
| 2273 | while ( glGetError() != GL_NO_ERROR ) ; |
| 2274 | |
| 2275 | // create a FBO |
| 2276 | GLuint name, tname; |
| 2277 | glGenRenderbuffersOES(1, &tname); |
| 2278 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname); |
| 2279 | glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh); |
| 2280 | glGenFramebuffersOES(1, &name); |
| 2281 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); |
| 2282 | glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, |
| 2283 | GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname); |
| 2284 | |
| 2285 | GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES); |
Mathias Agopian | cd2cfb6 | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2286 | |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2287 | if (status == GL_FRAMEBUFFER_COMPLETE_OES) { |
| 2288 | |
| 2289 | // invert everything, b/c glReadPixel() below will invert the FB |
| 2290 | glViewport(0, 0, sw, sh); |
Mathias Agopian | 1c4e4fc0 | 2010-12-16 18:46:17 -0800 | [diff] [blame] | 2291 | glScissor(0, 0, sw, sh); |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2292 | glEnable(GL_SCISSOR_TEST); |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2293 | glMatrixMode(GL_PROJECTION); |
| 2294 | glPushMatrix(); |
| 2295 | glLoadIdentity(); |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2296 | glOrthof(0, hw_w, hw_h, 0, 0, 1); |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2297 | glMatrixMode(GL_MODELVIEW); |
| 2298 | |
| 2299 | // redraw the screen entirely... |
| 2300 | glClearColor(0,0,0,1); |
| 2301 | glClear(GL_COLOR_BUFFER_BIT); |
Mathias Agopian | 1c4e4fc0 | 2010-12-16 18:46:17 -0800 | [diff] [blame] | 2302 | |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2303 | for (size_t i=0 ; i<count ; ++i) { |
| 2304 | const sp<LayerBase>& layer(layers[i]); |
Mathias Agopian | ec49d89 | 2011-08-25 14:36:43 -0700 | [diff] [blame] | 2305 | const uint32_t flags = layer->drawingState().flags; |
| 2306 | if (!(flags & ISurfaceComposer::eLayerHidden)) { |
| 2307 | const uint32_t z = layer->drawingState().z; |
| 2308 | if (z >= minLayerZ && z <= maxLayerZ) { |
| 2309 | layer->drawForSreenShot(); |
| 2310 | } |
Mathias Agopian | 3dd25a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2311 | } |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | // XXX: this is needed on tegra |
Mathias Agopian | 3a831d2 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2315 | glEnable(GL_SCISSOR_TEST); |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2316 | glScissor(0, 0, sw, sh); |
| 2317 | |
| 2318 | // check for errors and return screen capture |
| 2319 | if (glGetError() != GL_NO_ERROR) { |
| 2320 | // error while rendering |
| 2321 | result = INVALID_OPERATION; |
| 2322 | } else { |
| 2323 | // allocate shared memory large enough to hold the |
| 2324 | // screen capture |
| 2325 | sp<MemoryHeapBase> base( |
| 2326 | new MemoryHeapBase(size, 0, "screen-capture") ); |
| 2327 | void* const ptr = base->getBase(); |
| 2328 | if (ptr) { |
| 2329 | // capture the screen with glReadPixels() |
| 2330 | glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr); |
| 2331 | if (glGetError() == GL_NO_ERROR) { |
| 2332 | *heap = base; |
| 2333 | *w = sw; |
| 2334 | *h = sh; |
| 2335 | *f = PIXEL_FORMAT_RGBA_8888; |
| 2336 | result = NO_ERROR; |
| 2337 | } |
| 2338 | } else { |
| 2339 | result = NO_MEMORY; |
| 2340 | } |
| 2341 | } |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2342 | glEnable(GL_SCISSOR_TEST); |
| 2343 | glViewport(0, 0, hw_w, hw_h); |
| 2344 | glMatrixMode(GL_PROJECTION); |
| 2345 | glPopMatrix(); |
| 2346 | glMatrixMode(GL_MODELVIEW); |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2347 | } else { |
| 2348 | result = BAD_VALUE; |
| 2349 | } |
| 2350 | |
| 2351 | // release FBO resources |
| 2352 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); |
| 2353 | glDeleteRenderbuffersOES(1, &tname); |
| 2354 | glDeleteFramebuffersOES(1, &name); |
Mathias Agopian | a6b8c1c | 2010-12-15 14:41:59 -0800 | [diff] [blame] | 2355 | |
| 2356 | hw.compositionComplete(); |
| 2357 | |
Mathias Agopian | 32ae094 | 2011-03-02 18:45:50 -0800 | [diff] [blame] | 2358 | // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK"); |
Mathias Agopian | cd2cfb6 | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2359 | |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2360 | return result; |
| 2361 | } |
| 2362 | |
| 2363 | |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2364 | status_t SurfaceFlinger::captureScreen(DisplayID dpy, |
| 2365 | sp<IMemoryHeap>* heap, |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2366 | uint32_t* width, uint32_t* height, PixelFormat* format, |
Mathias Agopian | 3dd25a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2367 | uint32_t sw, uint32_t sh, |
| 2368 | uint32_t minLayerZ, uint32_t maxLayerZ) |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2369 | { |
| 2370 | // only one display supported for now |
| 2371 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 2372 | return BAD_VALUE; |
| 2373 | |
| 2374 | if (!GLExtensions::getInstance().haveFramebufferObject()) |
| 2375 | return INVALID_OPERATION; |
| 2376 | |
| 2377 | class MessageCaptureScreen : public MessageBase { |
| 2378 | SurfaceFlinger* flinger; |
| 2379 | DisplayID dpy; |
| 2380 | sp<IMemoryHeap>* heap; |
| 2381 | uint32_t* w; |
| 2382 | uint32_t* h; |
| 2383 | PixelFormat* f; |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2384 | uint32_t sw; |
| 2385 | uint32_t sh; |
Mathias Agopian | 3dd25a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2386 | uint32_t minLayerZ; |
| 2387 | uint32_t maxLayerZ; |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2388 | status_t result; |
| 2389 | public: |
| 2390 | MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy, |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2391 | sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f, |
Mathias Agopian | 3dd25a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2392 | uint32_t sw, uint32_t sh, |
| 2393 | uint32_t minLayerZ, uint32_t maxLayerZ) |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2394 | : flinger(flinger), dpy(dpy), |
Mathias Agopian | 3dd25a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2395 | heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), |
| 2396 | minLayerZ(minLayerZ), maxLayerZ(maxLayerZ), |
| 2397 | result(PERMISSION_DENIED) |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2398 | { |
| 2399 | } |
| 2400 | status_t getResult() const { |
| 2401 | return result; |
| 2402 | } |
| 2403 | virtual bool handler() { |
| 2404 | Mutex::Autolock _l(flinger->mStateLock); |
| 2405 | |
| 2406 | // if we have secure windows, never allow the screen capture |
| 2407 | if (flinger->mSecureFrameBuffer) |
| 2408 | return true; |
| 2409 | |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2410 | result = flinger->captureScreenImplLocked(dpy, |
Mathias Agopian | 3dd25a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2411 | heap, w, h, f, sw, sh, minLayerZ, maxLayerZ); |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2412 | |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2413 | return true; |
| 2414 | } |
| 2415 | }; |
| 2416 | |
| 2417 | sp<MessageBase> msg = new MessageCaptureScreen(this, |
Mathias Agopian | 3dd25a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2418 | dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ); |
Mathias Agopian | ca5edbe | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2419 | status_t res = postMessageSync(msg); |
| 2420 | if (res == NO_ERROR) { |
| 2421 | res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult(); |
| 2422 | } |
| 2423 | return res; |
| 2424 | } |
| 2425 | |
| 2426 | // --------------------------------------------------------------------------- |
| 2427 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2428 | sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const |
| 2429 | { |
| 2430 | sp<Layer> result; |
| 2431 | Mutex::Autolock _l(mStateLock); |
| 2432 | result = mLayerMap.valueFor( sur->asBinder() ).promote(); |
| 2433 | return result; |
| 2434 | } |
| 2435 | |
| 2436 | // --------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2437 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2438 | Client::Client(const sp<SurfaceFlinger>& flinger) |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2439 | : mFlinger(flinger), mNameGenerator(1) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2440 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2441 | } |
| 2442 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2443 | Client::~Client() |
| 2444 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2445 | const size_t count = mLayers.size(); |
| 2446 | for (size_t i=0 ; i<count ; i++) { |
| 2447 | sp<LayerBaseClient> layer(mLayers.valueAt(i).promote()); |
| 2448 | if (layer != 0) { |
| 2449 | mFlinger->removeLayer(layer); |
| 2450 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2454 | status_t Client::initCheck() const { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2455 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2456 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2457 | |
Mathias Agopian | 5fa7ad6 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2458 | size_t Client::attachLayer(const sp<LayerBaseClient>& layer) |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2459 | { |
Mathias Agopian | 5fa7ad6 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2460 | Mutex::Autolock _l(mLock); |
| 2461 | size_t name = mNameGenerator++; |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2462 | mLayers.add(name, layer); |
| 2463 | return name; |
| 2464 | } |
| 2465 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2466 | void Client::detachLayer(const LayerBaseClient* layer) |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2467 | { |
Mathias Agopian | 5fa7ad6 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2468 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2469 | // we do a linear search here, because this doesn't happen often |
| 2470 | const size_t count = mLayers.size(); |
| 2471 | for (size_t i=0 ; i<count ; i++) { |
| 2472 | if (mLayers.valueAt(i) == layer) { |
| 2473 | mLayers.removeItemsAt(i, 1); |
| 2474 | break; |
| 2475 | } |
| 2476 | } |
| 2477 | } |
Mathias Agopian | 5fa7ad6 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2478 | sp<LayerBaseClient> Client::getLayerUser(int32_t i) const |
| 2479 | { |
| 2480 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2481 | sp<LayerBaseClient> lbc; |
Mathias Agopian | 5fa7ad6 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2482 | wp<LayerBaseClient> layer(mLayers.valueFor(i)); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2483 | if (layer != 0) { |
| 2484 | lbc = layer.promote(); |
| 2485 | LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2486 | } |
| 2487 | return lbc; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2488 | } |
| 2489 | |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2490 | |
| 2491 | status_t Client::onTransact( |
| 2492 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 2493 | { |
| 2494 | // these must be checked |
| 2495 | IPCThreadState* ipc = IPCThreadState::self(); |
| 2496 | const int pid = ipc->getCallingPid(); |
| 2497 | const int uid = ipc->getCallingUid(); |
| 2498 | const int self_pid = getpid(); |
| 2499 | if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) { |
| 2500 | // we're called from a different process, do the real check |
Mathias Agopian | 0dd593f | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 2501 | if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger)) |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2502 | { |
| 2503 | LOGE("Permission Denial: " |
| 2504 | "can't openGlobalTransaction pid=%d, uid=%d", pid, uid); |
| 2505 | return PERMISSION_DENIED; |
| 2506 | } |
| 2507 | } |
| 2508 | return BnSurfaceComposerClient::onTransact(code, data, reply, flags); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2509 | } |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2510 | |
| 2511 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2512 | sp<ISurface> Client::createSurface( |
Mathias Agopian | 9638e5c | 2011-04-20 14:19:32 -0700 | [diff] [blame] | 2513 | ISurfaceComposerClient::surface_data_t* params, |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2514 | const String8& name, |
| 2515 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2516 | uint32_t flags) |
| 2517 | { |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2518 | /* |
| 2519 | * createSurface must be called from the GL thread so that it can |
| 2520 | * have access to the GL context. |
| 2521 | */ |
| 2522 | |
| 2523 | class MessageCreateSurface : public MessageBase { |
| 2524 | sp<ISurface> result; |
| 2525 | SurfaceFlinger* flinger; |
| 2526 | ISurfaceComposerClient::surface_data_t* params; |
| 2527 | Client* client; |
| 2528 | const String8& name; |
| 2529 | DisplayID display; |
| 2530 | uint32_t w, h; |
| 2531 | PixelFormat format; |
| 2532 | uint32_t flags; |
| 2533 | public: |
| 2534 | MessageCreateSurface(SurfaceFlinger* flinger, |
| 2535 | ISurfaceComposerClient::surface_data_t* params, |
| 2536 | const String8& name, Client* client, |
| 2537 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, |
| 2538 | uint32_t flags) |
| 2539 | : flinger(flinger), params(params), client(client), name(name), |
| 2540 | display(display), w(w), h(h), format(format), flags(flags) |
| 2541 | { |
| 2542 | } |
| 2543 | sp<ISurface> getResult() const { return result; } |
| 2544 | virtual bool handler() { |
| 2545 | result = flinger->createSurface(params, name, client, |
| 2546 | display, w, h, format, flags); |
| 2547 | return true; |
| 2548 | } |
| 2549 | }; |
| 2550 | |
| 2551 | sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(), |
| 2552 | params, name, this, display, w, h, format, flags); |
| 2553 | mFlinger->postMessageSync(msg); |
| 2554 | return static_cast<MessageCreateSurface*>( msg.get() )->getResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2555 | } |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2556 | status_t Client::destroySurface(SurfaceID sid) { |
| 2557 | return mFlinger->removeSurface(this, sid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2558 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2559 | |
| 2560 | // --------------------------------------------------------------------------- |
| 2561 | |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2562 | GraphicBufferAlloc::GraphicBufferAlloc() {} |
| 2563 | |
| 2564 | GraphicBufferAlloc::~GraphicBufferAlloc() {} |
| 2565 | |
| 2566 | sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h, |
Mathias Agopian | eec0f7e | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2567 | PixelFormat format, uint32_t usage, status_t* error) { |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2568 | sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage)); |
| 2569 | status_t err = graphicBuffer->initCheck(); |
Mathias Agopian | eec0f7e | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2570 | *error = err; |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2571 | if (err != 0 || graphicBuffer->handle == 0) { |
Mathias Agopian | eec0f7e | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2572 | if (err == NO_MEMORY) { |
| 2573 | GraphicBuffer::dumpAllocationsToSystemLog(); |
| 2574 | } |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2575 | LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) " |
| 2576 | "failed (%s), handle=%p", |
| 2577 | w, h, strerror(-err), graphicBuffer->handle); |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2578 | return 0; |
| 2579 | } |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2580 | return graphicBuffer; |
| 2581 | } |
| 2582 | |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2583 | // --------------------------------------------------------------------------- |
| 2584 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2585 | GraphicPlane::GraphicPlane() |
| 2586 | : mHw(0) |
| 2587 | { |
| 2588 | } |
| 2589 | |
| 2590 | GraphicPlane::~GraphicPlane() { |
| 2591 | delete mHw; |
| 2592 | } |
| 2593 | |
| 2594 | bool GraphicPlane::initialized() const { |
| 2595 | return mHw ? true : false; |
| 2596 | } |
| 2597 | |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 2598 | int GraphicPlane::getWidth() const { |
| 2599 | return mWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2600 | } |
| 2601 | |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 2602 | int GraphicPlane::getHeight() const { |
| 2603 | return mHeight; |
| 2604 | } |
| 2605 | |
| 2606 | void GraphicPlane::setDisplayHardware(DisplayHardware *hw) |
| 2607 | { |
| 2608 | mHw = hw; |
| 2609 | |
| 2610 | // initialize the display orientation transform. |
| 2611 | // it's a constant that should come from the display driver. |
| 2612 | int displayOrientation = ISurfaceComposer::eOrientationDefault; |
| 2613 | char property[PROPERTY_VALUE_MAX]; |
| 2614 | if (property_get("ro.sf.hwrotation", property, NULL) > 0) { |
| 2615 | //displayOrientation |
| 2616 | switch (atoi(property)) { |
| 2617 | case 90: |
| 2618 | displayOrientation = ISurfaceComposer::eOrientation90; |
| 2619 | break; |
| 2620 | case 270: |
| 2621 | displayOrientation = ISurfaceComposer::eOrientation270; |
| 2622 | break; |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | const float w = hw->getWidth(); |
| 2627 | const float h = hw->getHeight(); |
| 2628 | GraphicPlane::orientationToTransfrom(displayOrientation, w, h, |
| 2629 | &mDisplayTransform); |
| 2630 | if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) { |
| 2631 | mDisplayWidth = h; |
| 2632 | mDisplayHeight = w; |
| 2633 | } else { |
| 2634 | mDisplayWidth = w; |
| 2635 | mDisplayHeight = h; |
| 2636 | } |
| 2637 | |
| 2638 | setOrientation(ISurfaceComposer::eOrientationDefault); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | status_t GraphicPlane::orientationToTransfrom( |
| 2642 | int orientation, int w, int h, Transform* tr) |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2643 | { |
| 2644 | uint32_t flags = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2645 | switch (orientation) { |
| 2646 | case ISurfaceComposer::eOrientationDefault: |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2647 | flags = Transform::ROT_0; |
| 2648 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2649 | case ISurfaceComposer::eOrientation90: |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2650 | flags = Transform::ROT_90; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2651 | break; |
| 2652 | case ISurfaceComposer::eOrientation180: |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2653 | flags = Transform::ROT_180; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | break; |
| 2655 | case ISurfaceComposer::eOrientation270: |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2656 | flags = Transform::ROT_270; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2657 | break; |
| 2658 | default: |
| 2659 | return BAD_VALUE; |
| 2660 | } |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2661 | tr->set(flags, w, h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2662 | return NO_ERROR; |
| 2663 | } |
| 2664 | |
| 2665 | status_t GraphicPlane::setOrientation(int orientation) |
| 2666 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2667 | // If the rotation can be handled in hardware, this is where |
| 2668 | // the magic should happen. |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 2669 | |
| 2670 | const DisplayHardware& hw(displayHardware()); |
| 2671 | const float w = mDisplayWidth; |
| 2672 | const float h = mDisplayHeight; |
| 2673 | mWidth = int(w); |
| 2674 | mHeight = int(h); |
| 2675 | |
| 2676 | Transform orientationTransform; |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2677 | GraphicPlane::orientationToTransfrom(orientation, w, h, |
| 2678 | &orientationTransform); |
| 2679 | if (orientation & ISurfaceComposer::eOrientationSwapMask) { |
| 2680 | mWidth = int(h); |
| 2681 | mHeight = int(w); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2682 | } |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2683 | |
Mathias Agopian | 3552f53 | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 2684 | mOrientation = orientation; |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 2685 | mGlobalTransform = mDisplayTransform * orientationTransform; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2686 | return NO_ERROR; |
| 2687 | } |
| 2688 | |
| 2689 | const DisplayHardware& GraphicPlane::displayHardware() const { |
| 2690 | return *mHw; |
| 2691 | } |
| 2692 | |
Mathias Agopian | aab758e | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2693 | DisplayHardware& GraphicPlane::editDisplayHardware() { |
| 2694 | return *mHw; |
| 2695 | } |
| 2696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2697 | const Transform& GraphicPlane::transform() const { |
| 2698 | return mGlobalTransform; |
| 2699 | } |
| 2700 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2701 | EGLDisplay GraphicPlane::getEGLDisplay() const { |
| 2702 | return mHw->getEGLDisplay(); |
| 2703 | } |
| 2704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2705 | // --------------------------------------------------------------------------- |
| 2706 | |
| 2707 | }; // namespace android |