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> |
| 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include <utils/String8.h> |
| 37 | #include <utils/String16.h> |
| 38 | #include <utils/StopWatch.h> |
| 39 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 40 | #include <ui/GraphicBufferAllocator.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | #include <ui/PixelFormat.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | #include <pixelflinger/pixelflinger.h> |
| 44 | #include <GLES/gl.h> |
| 45 | |
| 46 | #include "clz.h" |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 47 | #include "GLExtensions.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | #include "Layer.h" |
| 49 | #include "LayerBlur.h" |
| 50 | #include "LayerBuffer.h" |
| 51 | #include "LayerDim.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | #include "SurfaceFlinger.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | #include "DisplayHardware/DisplayHardware.h" |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 55 | #include "DisplayHardware/HWComposer.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 57 | /* ideally AID_GRAPHICS would be in a semi-public header |
| 58 | * or there would be a way to map a user/group name to its id |
| 59 | */ |
| 60 | #ifndef AID_GRAPHICS |
| 61 | #define AID_GRAPHICS 1003 |
| 62 | #endif |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | #define DISPLAY_COUNT 1 |
| 65 | |
| 66 | namespace android { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | // --------------------------------------------------------------------------- |
| 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | SurfaceFlinger::SurfaceFlinger() |
| 70 | : BnSurfaceComposer(), Thread(false), |
| 71 | mTransactionFlags(0), |
| 72 | mTransactionCount(0), |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 73 | mResizeTransationPending(false), |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 74 | mLayersRemoved(false), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | mBootTime(systemTime()), |
Mathias Agopian | 151e859 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 76 | mHardwareTest("android.permission.HARDWARE_TEST"), |
| 77 | mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"), |
| 78 | mDump("android.permission.DUMP"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | mVisibleRegionsDirty(false), |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 80 | mHwWorkListDirty(false), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | mDeferReleaseConsole(false), |
| 82 | mFreezeDisplay(false), |
| 83 | mFreezeCount(0), |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 84 | mFreezeDisplayTime(0), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | mDebugRegion(0), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | mDebugBackground(0), |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 87 | mDebugInSwapBuffers(0), |
| 88 | mLastSwapBufferTime(0), |
| 89 | mDebugInTransaction(0), |
| 90 | mLastTransactionTime(0), |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 91 | mBootFinished(false), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | mConsoleSignals(0), |
| 93 | mSecureFrameBuffer(0) |
| 94 | { |
| 95 | init(); |
| 96 | } |
| 97 | |
| 98 | void SurfaceFlinger::init() |
| 99 | { |
| 100 | LOGI("SurfaceFlinger is starting"); |
| 101 | |
| 102 | // debugging stuff... |
| 103 | char value[PROPERTY_VALUE_MAX]; |
| 104 | property_get("debug.sf.showupdates", value, "0"); |
| 105 | mDebugRegion = atoi(value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | property_get("debug.sf.showbackground", value, "0"); |
| 107 | mDebugBackground = atoi(value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 109 | LOGI_IF(mDebugRegion, "showupdates enabled"); |
| 110 | LOGI_IF(mDebugBackground, "showbackground enabled"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SurfaceFlinger::~SurfaceFlinger() |
| 114 | { |
| 115 | glDeleteTextures(1, &mWormholeTexName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const |
| 119 | { |
| 120 | return graphicPlane(0).displayHardware().getOverlayEngine(); |
| 121 | } |
| 122 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 123 | sp<IMemoryHeap> SurfaceFlinger::getCblk() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | { |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 125 | return mServerHeap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Mathias Agopian | 770492c | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 128 | sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 130 | sp<ISurfaceComposerClient> bclient; |
| 131 | sp<Client> client(new Client(this)); |
| 132 | status_t err = client->initCheck(); |
| 133 | if (err == NO_ERROR) { |
| 134 | bclient = client; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | return bclient; |
| 137 | } |
| 138 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 139 | sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection() |
| 140 | { |
| 141 | sp<ISurfaceComposerClient> bclient; |
| 142 | sp<UserClient> client(new UserClient(this)); |
| 143 | status_t err = client->initCheck(); |
| 144 | if (err == NO_ERROR) { |
| 145 | bclient = client; |
| 146 | } |
| 147 | return bclient; |
| 148 | } |
| 149 | |
| 150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const |
| 152 | { |
| 153 | LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy); |
| 154 | const GraphicPlane& plane(mGraphicPlanes[dpy]); |
| 155 | return plane; |
| 156 | } |
| 157 | |
| 158 | GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) |
| 159 | { |
| 160 | return const_cast<GraphicPlane&>( |
| 161 | const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy)); |
| 162 | } |
| 163 | |
| 164 | void SurfaceFlinger::bootFinished() |
| 165 | { |
| 166 | const nsecs_t now = systemTime(); |
| 167 | const nsecs_t duration = now - mBootTime; |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 168 | LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 169 | mBootFinished = true; |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 170 | property_set("ctl.stop", "bootanim"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void SurfaceFlinger::onFirstRef() |
| 174 | { |
| 175 | run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY); |
| 176 | |
| 177 | // Wait for the main thread to be done with its initialization |
| 178 | mReadyToRunBarrier.wait(); |
| 179 | } |
| 180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | static inline uint16_t pack565(int r, int g, int b) { |
| 182 | return (r<<11)|(g<<5)|b; |
| 183 | } |
| 184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | status_t SurfaceFlinger::readyToRun() |
| 186 | { |
| 187 | LOGI( "SurfaceFlinger's main thread ready to run. " |
| 188 | "Initializing graphics H/W..."); |
| 189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | // we only support one display currently |
| 191 | int dpy = 0; |
| 192 | |
| 193 | { |
| 194 | // initialize the main display |
| 195 | GraphicPlane& plane(graphicPlane(dpy)); |
| 196 | DisplayHardware* const hw = new DisplayHardware(this, dpy); |
| 197 | plane.setDisplayHardware(hw); |
| 198 | } |
| 199 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 200 | // create the shared control-block |
| 201 | mServerHeap = new MemoryHeapBase(4096, |
| 202 | MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap"); |
| 203 | LOGE_IF(mServerHeap==0, "can't create shared memory dealer"); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 204 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 205 | mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase()); |
| 206 | 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] | 207 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 208 | new(mServerCblk) surface_flinger_cblk_t; |
| 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | // initialize primary screen |
| 211 | // (other display should be initialized in the same manner, but |
| 212 | // asynchronously, as they could come and go. None of this is supported |
| 213 | // yet). |
| 214 | const GraphicPlane& plane(graphicPlane(dpy)); |
| 215 | const DisplayHardware& hw = plane.displayHardware(); |
| 216 | const uint32_t w = hw.getWidth(); |
| 217 | const uint32_t h = hw.getHeight(); |
| 218 | const uint32_t f = hw.getFormat(); |
| 219 | hw.makeCurrent(); |
| 220 | |
| 221 | // initialize the shared control block |
| 222 | mServerCblk->connected |= 1<<dpy; |
| 223 | display_cblk_t* dcblk = mServerCblk->displays + dpy; |
| 224 | memset(dcblk, 0, sizeof(display_cblk_t)); |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 225 | dcblk->w = plane.getWidth(); |
| 226 | dcblk->h = plane.getHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | dcblk->format = f; |
| 228 | dcblk->orientation = ISurfaceComposer::eOrientationDefault; |
| 229 | dcblk->xdpi = hw.getDpiX(); |
| 230 | dcblk->ydpi = hw.getDpiY(); |
| 231 | dcblk->fps = hw.getRefreshRate(); |
| 232 | dcblk->density = hw.getDensity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
| 234 | // Initialize OpenGL|ES |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 236 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | glEnableClientState(GL_VERTEX_ARRAY); |
| 238 | glEnable(GL_SCISSOR_TEST); |
| 239 | glShadeModel(GL_FLAT); |
| 240 | glDisable(GL_DITHER); |
| 241 | glDisable(GL_CULL_FACE); |
| 242 | |
| 243 | const uint16_t g0 = pack565(0x0F,0x1F,0x0F); |
| 244 | const uint16_t g1 = pack565(0x17,0x2f,0x17); |
| 245 | const uint16_t textureData[4] = { g0, g1, g1, g0 }; |
| 246 | glGenTextures(1, &mWormholeTexName); |
| 247 | glBindTexture(GL_TEXTURE_2D, mWormholeTexName); |
| 248 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 249 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 250 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 251 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 252 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, |
| 253 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData); |
| 254 | |
| 255 | glViewport(0, 0, w, h); |
| 256 | glMatrixMode(GL_PROJECTION); |
| 257 | glLoadIdentity(); |
| 258 | glOrthof(0, w, h, 0, 0, 1); |
| 259 | |
| 260 | LayerDim::initDimmer(this, w, h); |
| 261 | |
| 262 | mReadyToRunBarrier.open(); |
| 263 | |
| 264 | /* |
| 265 | * We're now ready to accept clients... |
| 266 | */ |
| 267 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 268 | // start boot animation |
| 269 | property_set("ctl.start", "bootanim"); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 270 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | return NO_ERROR; |
| 272 | } |
| 273 | |
| 274 | // ---------------------------------------------------------------------------- |
| 275 | #if 0 |
| 276 | #pragma mark - |
| 277 | #pragma mark Events Handler |
| 278 | #endif |
| 279 | |
| 280 | void SurfaceFlinger::waitForEvent() |
| 281 | { |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 282 | while (true) { |
| 283 | nsecs_t timeout = -1; |
Mathias Agopian | 0e44976 | 2009-12-01 17:23:28 -0800 | [diff] [blame] | 284 | const nsecs_t freezeDisplayTimeout = ms2ns(5000); |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 285 | if (UNLIKELY(isFrozen())) { |
| 286 | // wait 5 seconds |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 287 | const nsecs_t now = systemTime(); |
| 288 | if (mFreezeDisplayTime == 0) { |
| 289 | mFreezeDisplayTime = now; |
| 290 | } |
| 291 | nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime); |
| 292 | timeout = waitTime>0 ? waitTime : 0; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 293 | } |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 294 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 295 | sp<MessageBase> msg = mEventQueue.waitMessage(timeout); |
Mathias Agopian | 0e44976 | 2009-12-01 17:23:28 -0800 | [diff] [blame] | 296 | |
| 297 | // see if we timed out |
| 298 | if (isFrozen()) { |
| 299 | const nsecs_t now = systemTime(); |
| 300 | nsecs_t frozenTime = (now - mFreezeDisplayTime); |
| 301 | if (frozenTime >= freezeDisplayTimeout) { |
| 302 | // we timed out and are still frozen |
| 303 | LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d", |
| 304 | mFreezeDisplay, mFreezeCount); |
| 305 | mFreezeDisplayTime = 0; |
| 306 | mFreezeCount = 0; |
| 307 | mFreezeDisplay = false; |
| 308 | } |
| 309 | } |
| 310 | |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 311 | if (msg != 0) { |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 312 | switch (msg->what) { |
| 313 | case MessageQueue::INVALIDATE: |
| 314 | // invalidate message, just return to the main loop |
| 315 | return; |
| 316 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | void SurfaceFlinger::signalEvent() { |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 322 | mEventQueue.invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void SurfaceFlinger::signal() const { |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 326 | // this is the IPC call |
| 327 | const_cast<SurfaceFlinger*>(this)->signalEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 330 | status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg, |
| 331 | nsecs_t reltime, uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | { |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 333 | return mEventQueue.postMessage(msg, reltime, flags); |
| 334 | } |
| 335 | |
| 336 | status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg, |
| 337 | nsecs_t reltime, uint32_t flags) |
| 338 | { |
| 339 | status_t res = mEventQueue.postMessage(msg, reltime, flags); |
| 340 | if (res == NO_ERROR) { |
| 341 | msg->wait(); |
| 342 | } |
| 343 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // ---------------------------------------------------------------------------- |
| 347 | #if 0 |
| 348 | #pragma mark - |
| 349 | #pragma mark Main loop |
| 350 | #endif |
| 351 | |
| 352 | bool SurfaceFlinger::threadLoop() |
| 353 | { |
| 354 | waitForEvent(); |
| 355 | |
| 356 | // check for transactions |
| 357 | if (UNLIKELY(mConsoleSignals)) { |
| 358 | handleConsoleEvents(); |
| 359 | } |
| 360 | |
| 361 | if (LIKELY(mTransactionCount == 0)) { |
| 362 | // if we're in a global transaction, don't do anything. |
| 363 | const uint32_t mask = eTransactionNeeded | eTraversalNeeded; |
| 364 | uint32_t transactionFlags = getTransactionFlags(mask); |
| 365 | if (LIKELY(transactionFlags)) { |
| 366 | handleTransaction(transactionFlags); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // post surfaces (if needed) |
| 371 | handlePageFlip(); |
| 372 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 373 | if (UNLIKELY(mHwWorkListDirty)) { |
| 374 | // build the h/w work list |
| 375 | handleWorkList(); |
| 376 | } |
| 377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | 81384bf | 2009-09-27 22:47:27 -0700 | [diff] [blame] | 379 | if (LIKELY(hw.canDraw() && !isFrozen())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | // repaint the framebuffer (if needed) |
| 381 | handleRepaint(); |
| 382 | |
Mathias Agopian | b1a1874 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 383 | // inform the h/w that we're done compositing |
| 384 | hw.compositionComplete(); |
| 385 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | // release the clients before we flip ('cause flip might block) |
| 387 | unlockClients(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | postFramebuffer(); |
| 390 | } else { |
| 391 | // pretend we did the post |
| 392 | unlockClients(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | usleep(16667); // 60 fps period |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | void SurfaceFlinger::postFramebuffer() |
| 399 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | if (!mInvalidRegion.isEmpty()) { |
| 401 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 402 | const nsecs_t now = systemTime(); |
| 403 | mDebugInSwapBuffers = now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | hw.flip(mInvalidRegion); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 405 | mLastSwapBufferTime = systemTime() - now; |
| 406 | mDebugInSwapBuffers = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | mInvalidRegion.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | void SurfaceFlinger::handleConsoleEvents() |
| 412 | { |
| 413 | // something to do with the console |
| 414 | const DisplayHardware& hw = graphicPlane(0).displayHardware(); |
| 415 | |
| 416 | int what = android_atomic_and(0, &mConsoleSignals); |
| 417 | if (what & eConsoleAcquired) { |
| 418 | hw.acquireScreen(); |
| 419 | } |
| 420 | |
| 421 | if (mDeferReleaseConsole && hw.canDraw()) { |
Mathias Agopian | ed81f22 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 422 | // We got the release signal before the acquire signal |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | mDeferReleaseConsole = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | hw.releaseScreen(); |
| 425 | } |
| 426 | |
| 427 | if (what & eConsoleReleased) { |
| 428 | if (hw.canDraw()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | hw.releaseScreen(); |
| 430 | } else { |
| 431 | mDeferReleaseConsole = true; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | mDirtyRegion.set(hw.bounds()); |
| 436 | } |
| 437 | |
| 438 | void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) |
| 439 | { |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 440 | Vector< sp<LayerBase> > ditchedLayers; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 442 | /* |
| 443 | * Perform and commit the transaction |
| 444 | */ |
| 445 | |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 446 | { // scope for the lock |
| 447 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 448 | const nsecs_t now = systemTime(); |
| 449 | mDebugInTransaction = now; |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 450 | handleTransactionLocked(transactionFlags, ditchedLayers); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 451 | mLastTransactionTime = systemTime() - now; |
| 452 | mDebugInTransaction = 0; |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 453 | mHwWorkListDirty = true; |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 454 | // here the transaction has been committed |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 457 | /* |
| 458 | * Clean-up all layers that went away |
| 459 | * (do this without the lock held) |
| 460 | */ |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 461 | |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 462 | const size_t count = ditchedLayers.size(); |
| 463 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | ffae4fc | 2009-09-04 19:50:23 -0700 | [diff] [blame] | 464 | if (ditchedLayers[i] != 0) { |
| 465 | //LOGD("ditching layer %p", ditchedLayers[i].get()); |
| 466 | ditchedLayers[i]->ditch(); |
| 467 | } |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | void SurfaceFlinger::handleTransactionLocked( |
| 472 | uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers) |
| 473 | { |
| 474 | const LayerVector& currentLayers(mCurrentState.layersSortedByZ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | const size_t count = currentLayers.size(); |
| 476 | |
| 477 | /* |
| 478 | * Traversal of the children |
| 479 | * (perform the transaction for each of them if needed) |
| 480 | */ |
| 481 | |
| 482 | const bool layersNeedTransaction = transactionFlags & eTraversalNeeded; |
| 483 | if (layersNeedTransaction) { |
| 484 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 485 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded); |
| 487 | if (!trFlags) continue; |
| 488 | |
| 489 | const uint32_t flags = layer->doTransaction(0); |
| 490 | if (flags & Layer::eVisibleRegion) |
| 491 | mVisibleRegionsDirty = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Perform our own transaction if needed |
| 497 | */ |
| 498 | |
| 499 | if (transactionFlags & eTransactionNeeded) { |
| 500 | if (mCurrentState.orientation != mDrawingState.orientation) { |
| 501 | // the orientation has changed, recompute all visible regions |
| 502 | // and invalidate everything. |
| 503 | |
| 504 | const int dpy = 0; |
| 505 | const int orientation = mCurrentState.orientation; |
Mathias Agopian | eb0c86e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 506 | const uint32_t type = mCurrentState.orientationType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | GraphicPlane& plane(graphicPlane(dpy)); |
| 508 | plane.setOrientation(orientation); |
| 509 | |
| 510 | // update the shared control block |
| 511 | const DisplayHardware& hw(plane.displayHardware()); |
| 512 | volatile display_cblk_t* dcblk = mServerCblk->displays + dpy; |
| 513 | dcblk->orientation = orientation; |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 514 | dcblk->w = plane.getWidth(); |
| 515 | dcblk->h = plane.getHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | |
| 517 | mVisibleRegionsDirty = true; |
| 518 | mDirtyRegion.set(hw.bounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) { |
| 522 | // freezing or unfreezing the display -> trigger animation if needed |
| 523 | mFreezeDisplay = mCurrentState.freezeDisplay; |
Mathias Agopian | 31901cc | 2010-03-01 17:51:17 -0800 | [diff] [blame] | 524 | if (mFreezeDisplay) |
| 525 | mFreezeDisplayTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 528 | if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) { |
| 529 | // layers have been added |
| 530 | mVisibleRegionsDirty = true; |
| 531 | } |
| 532 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | // some layers might have been removed, so |
| 534 | // we need to update the regions they're exposing. |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 535 | if (mLayersRemoved) { |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 536 | mLayersRemoved = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | mVisibleRegionsDirty = true; |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 538 | const LayerVector& previousLayers(mDrawingState.layersSortedByZ); |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 539 | const size_t count = previousLayers.size(); |
| 540 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 541 | const sp<LayerBase>& layer(previousLayers[i]); |
| 542 | if (currentLayers.indexOf( layer ) < 0) { |
| 543 | // this layer is not visible anymore |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 544 | ditchedLayers.add(layer); |
Mathias Agopian | 33863dd | 2009-07-28 14:20:21 -0700 | [diff] [blame] | 545 | mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen); |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | commitTransaction(); |
| 552 | } |
| 553 | |
| 554 | sp<FreezeLock> SurfaceFlinger::getFreezeLock() const |
| 555 | { |
| 556 | return new FreezeLock(const_cast<SurfaceFlinger *>(this)); |
| 557 | } |
| 558 | |
| 559 | void SurfaceFlinger::computeVisibleRegions( |
| 560 | LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion) |
| 561 | { |
| 562 | const GraphicPlane& plane(graphicPlane(0)); |
| 563 | const Transform& planeTransform(plane.transform()); |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 564 | const DisplayHardware& hw(plane.displayHardware()); |
| 565 | const Region screenRegion(hw.bounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | |
| 567 | Region aboveOpaqueLayers; |
| 568 | Region aboveCoveredLayers; |
| 569 | Region dirty; |
| 570 | |
| 571 | bool secureFrameBuffer = false; |
| 572 | |
| 573 | size_t i = currentLayers.size(); |
| 574 | while (i--) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 575 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | layer->validateVisibility(planeTransform); |
| 577 | |
| 578 | // start with the whole surface at its current location |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 579 | const Layer::State& s(layer->drawingState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 581 | /* |
| 582 | * opaqueRegion: area of a surface that is fully opaque. |
| 583 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | Region opaqueRegion; |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * visibleRegion: area of a surface that is visible on screen |
| 588 | * and not fully transparent. This is essentially the layer's |
| 589 | * footprint minus the opaque regions above it. |
| 590 | * Areas covered by a translucent surface are considered visible. |
| 591 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | Region visibleRegion; |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * coveredRegion: area of a surface that is covered by all |
| 596 | * visible regions above it (which includes the translucent areas). |
| 597 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | Region coveredRegion; |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 599 | |
| 600 | |
| 601 | // handle hidden surfaces by setting the visible region to empty |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 602 | if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | const bool translucent = layer->needsBlending(); |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 604 | const Rect bounds(layer->visibleBounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | visibleRegion.set(bounds); |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 606 | visibleRegion.andSelf(screenRegion); |
| 607 | if (!visibleRegion.isEmpty()) { |
| 608 | // Remove the transparent area from the visible region |
| 609 | if (translucent) { |
| 610 | visibleRegion.subtractSelf(layer->transparentRegionScreen); |
| 611 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 613 | // compute the opaque region |
| 614 | const int32_t layerOrientation = layer->getOrientation(); |
| 615 | if (s.alpha==255 && !translucent && |
| 616 | ((layerOrientation & Transform::ROT_INVALID) == false)) { |
| 617 | // the opaque region is the layer's footprint |
| 618 | opaqueRegion = visibleRegion; |
| 619 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 623 | // Clip the covered region to the visible region |
| 624 | coveredRegion = aboveCoveredLayers.intersect(visibleRegion); |
| 625 | |
| 626 | // Update aboveCoveredLayers for next (lower) layer |
| 627 | aboveCoveredLayers.orSelf(visibleRegion); |
| 628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | // subtract the opaque region covered by the layers above us |
| 630 | visibleRegion.subtractSelf(aboveOpaqueLayers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | |
| 632 | // compute this layer's dirty region |
| 633 | if (layer->contentDirty) { |
| 634 | // we need to invalidate the whole region |
| 635 | dirty = visibleRegion; |
| 636 | // as well, as the old visible region |
| 637 | dirty.orSelf(layer->visibleRegionScreen); |
| 638 | layer->contentDirty = false; |
| 639 | } else { |
Mathias Agopian | 0aed7e9 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 640 | /* compute the exposed region: |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 641 | * the exposed region consists of two components: |
| 642 | * 1) what's VISIBLE now and was COVERED before |
| 643 | * 2) what's EXPOSED now less what was EXPOSED before |
| 644 | * |
| 645 | * note that (1) is conservative, we start with the whole |
| 646 | * visible region but only keep what used to be covered by |
| 647 | * something -- which mean it may have been exposed. |
| 648 | * |
| 649 | * (2) handles areas that were not covered by anything but got |
| 650 | * exposed because of a resize. |
Mathias Agopian | 0aed7e9 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 651 | */ |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 652 | const Region newExposed = visibleRegion - coveredRegion; |
| 653 | const Region oldVisibleRegion = layer->visibleRegionScreen; |
| 654 | const Region oldCoveredRegion = layer->coveredRegionScreen; |
| 655 | const Region oldExposed = oldVisibleRegion - oldCoveredRegion; |
| 656 | dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | } |
| 658 | dirty.subtractSelf(aboveOpaqueLayers); |
| 659 | |
| 660 | // accumulate to the screen dirty region |
| 661 | dirtyRegion.orSelf(dirty); |
| 662 | |
Mathias Agopian | 9c041bb | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 663 | // Update aboveOpaqueLayers for next (lower) layer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | aboveOpaqueLayers.orSelf(opaqueRegion); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 665 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | // Store the visible region is screen space |
| 667 | layer->setVisibleRegion(visibleRegion); |
| 668 | layer->setCoveredRegion(coveredRegion); |
| 669 | |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 670 | // 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] | 671 | if (layer->isSecure() && !visibleRegion.isEmpty()) { |
| 672 | secureFrameBuffer = true; |
| 673 | } |
| 674 | } |
| 675 | |
Mathias Agopian | 12cedff | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 676 | // invalidate the areas where a layer was removed |
| 677 | dirtyRegion.orSelf(mDirtyRegionRemovedLayer); |
| 678 | mDirtyRegionRemovedLayer.clear(); |
| 679 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | mSecureFrameBuffer = secureFrameBuffer; |
| 681 | opaqueRegion = aboveOpaqueLayers; |
| 682 | } |
| 683 | |
| 684 | |
| 685 | void SurfaceFlinger::commitTransaction() |
| 686 | { |
| 687 | mDrawingState = mCurrentState; |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 688 | mResizeTransationPending = false; |
| 689 | mTransactionCV.broadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | void SurfaceFlinger::handlePageFlip() |
| 693 | { |
| 694 | bool visibleRegions = mVisibleRegionsDirty; |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 695 | LayerVector& currentLayers( |
| 696 | const_cast<LayerVector&>(mDrawingState.layersSortedByZ)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | visibleRegions |= lockPageFlip(currentLayers); |
| 698 | |
| 699 | const DisplayHardware& hw = graphicPlane(0).displayHardware(); |
| 700 | const Region screenRegion(hw.bounds()); |
| 701 | if (visibleRegions) { |
| 702 | Region opaqueRegion; |
| 703 | computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion); |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 704 | |
| 705 | /* |
| 706 | * rebuild the visible layer list |
| 707 | */ |
| 708 | mVisibleLayersSortedByZ.clear(); |
| 709 | const LayerVector& currentLayers(mDrawingState.layersSortedByZ); |
| 710 | size_t count = currentLayers.size(); |
| 711 | mVisibleLayersSortedByZ.setCapacity(count); |
| 712 | for (size_t i=0 ; i<count ; i++) { |
| 713 | if (!currentLayers[i]->visibleRegionScreen.isEmpty()) |
| 714 | mVisibleLayersSortedByZ.add(currentLayers[i]); |
| 715 | } |
| 716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | mWormholeRegion = screenRegion.subtract(opaqueRegion); |
| 718 | mVisibleRegionsDirty = false; |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 719 | mHwWorkListDirty = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | unlockPageFlip(currentLayers); |
| 723 | mDirtyRegion.andSelf(screenRegion); |
| 724 | } |
| 725 | |
| 726 | bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers) |
| 727 | { |
| 728 | bool recomputeVisibleRegions = false; |
| 729 | size_t count = currentLayers.size(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 730 | sp<LayerBase> const* layers = currentLayers.array(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 732 | const sp<LayerBase>& layer(layers[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | layer->lockPageFlip(recomputeVisibleRegions); |
| 734 | } |
| 735 | return recomputeVisibleRegions; |
| 736 | } |
| 737 | |
| 738 | void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers) |
| 739 | { |
| 740 | const GraphicPlane& plane(graphicPlane(0)); |
| 741 | const Transform& planeTransform(plane.transform()); |
| 742 | size_t count = currentLayers.size(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 743 | sp<LayerBase> const* layers = currentLayers.array(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 745 | const sp<LayerBase>& layer(layers[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | layer->unlockPageFlip(planeTransform, mDirtyRegion); |
| 747 | } |
| 748 | } |
| 749 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 750 | void SurfaceFlinger::handleWorkList() |
| 751 | { |
| 752 | mHwWorkListDirty = false; |
| 753 | HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer()); |
| 754 | if (hwc.initCheck() == NO_ERROR) { |
| 755 | const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ); |
| 756 | const size_t count = currentLayers.size(); |
| 757 | hwc.createWorkList(count); |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 758 | hwc_layer_t* const cur(hwc.getLayers()); |
| 759 | for (size_t i=0 ; cur && i<count ; i++) { |
| 760 | currentLayers[i]->setGeometry(&cur[i]); |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | } |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | void SurfaceFlinger::handleRepaint() |
| 766 | { |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 767 | // compute the invalid region |
| 768 | mInvalidRegion.orSelf(mDirtyRegion); |
| 769 | if (mInvalidRegion.isEmpty()) { |
| 770 | // nothing to do |
| 771 | return; |
| 772 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | |
| 774 | if (UNLIKELY(mDebugRegion)) { |
| 775 | debugFlashRegions(); |
| 776 | } |
| 777 | |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 778 | // set the frame buffer |
| 779 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 780 | glMatrixMode(GL_MODELVIEW); |
| 781 | glLoadIdentity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 782 | |
| 783 | uint32_t flags = hw.getFlags(); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 784 | if ((flags & DisplayHardware::SWAP_RECTANGLE) || |
| 785 | (flags & DisplayHardware::BUFFER_PRESERVED)) |
Mathias Agopian | 2e20bff | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 786 | { |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 787 | // we can redraw only what's dirty, but since SWAP_RECTANGLE only |
| 788 | // takes a rectangle, we must make sure to update that whole |
| 789 | // rectangle in that case |
| 790 | if (flags & DisplayHardware::SWAP_RECTANGLE) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 791 | // TODO: we really should be able to pass a region to |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 792 | // 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] | 793 | mDirtyRegion.set(mInvalidRegion.bounds()); |
| 794 | } else { |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 795 | // in the BUFFER_PRESERVED case, obviously, we can update only |
| 796 | // what's needed and nothing more. |
| 797 | // NOTE: this is NOT a common case, as preserving the backbuffer |
| 798 | // is costly and usually involves copying the whole update back. |
| 799 | } |
| 800 | } else { |
Mathias Agopian | f2d28b7 | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 801 | if (flags & DisplayHardware::PARTIAL_UPDATES) { |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 802 | // We need to redraw the rectangle that will be updated |
| 803 | // (pushed to the framebuffer). |
Mathias Agopian | f2d28b7 | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 804 | // This is needed because PARTIAL_UPDATES only takes one |
Mathias Agopian | ecfa7cc | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 805 | // rectangle instead of a region (see DisplayHardware::flip()) |
| 806 | mDirtyRegion.set(mInvalidRegion.bounds()); |
| 807 | } else { |
| 808 | // we need to redraw everything (the whole screen) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | mDirtyRegion.set(hw.bounds()); |
| 810 | mInvalidRegion = mDirtyRegion; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | // compose all surfaces |
| 815 | composeSurfaces(mDirtyRegion); |
| 816 | |
| 817 | // clear the dirty regions |
| 818 | mDirtyRegion.clear(); |
| 819 | } |
| 820 | |
| 821 | void SurfaceFlinger::composeSurfaces(const Region& dirty) |
| 822 | { |
| 823 | if (UNLIKELY(!mWormholeRegion.isEmpty())) { |
| 824 | // should never happen unless the window manager has a bug |
| 825 | // draw something... |
| 826 | drawWormhole(); |
| 827 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 828 | |
| 829 | status_t err = NO_ERROR; |
Mathias Agopian | ff1d410 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 830 | const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 831 | size_t count = layers.size(); |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 832 | |
| 833 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 834 | HWComposer& hwc(hw.getHwComposer()); |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 835 | hwc_layer_t* const cur(hwc.getLayers()); |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 836 | |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 837 | LOGE_IF(cur && hwc.getNumLayers() != count, |
| 838 | "HAL number of layers (%d) doesn't match surfaceflinger (%d)", |
| 839 | hwc.getNumLayers(), count); |
| 840 | |
| 841 | // just to be extra-safe, use the smallest count |
Erik Gilling | 0cf2f43 | 2010-08-12 23:21:40 -0700 | [diff] [blame] | 842 | if (hwc.initCheck() == NO_ERROR) { |
| 843 | count = count < hwc.getNumLayers() ? count : hwc.getNumLayers(); |
| 844 | } |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 845 | |
| 846 | /* |
| 847 | * update the per-frame h/w composer data for each layer |
| 848 | * and build the transparent region of the FB |
| 849 | */ |
| 850 | Region transparent; |
| 851 | if (cur) { |
| 852 | for (size_t i=0 ; i<count ; i++) { |
| 853 | const sp<LayerBase>& layer(layers[i]); |
| 854 | layer->setPerFrameData(&cur[i]); |
| 855 | if (cur[i].hints & HWC_HINT_CLEAR_FB) { |
| 856 | if (!(layer->needsBlending())) { |
| 857 | transparent.orSelf(layer->visibleRegionScreen); |
| 858 | } |
| 859 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 860 | } |
| 861 | err = hwc.prepare(); |
| 862 | LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err)); |
| 863 | } |
| 864 | |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 865 | /* |
| 866 | * clear the area of the FB that need to be transparent |
| 867 | */ |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 868 | transparent.andSelf(dirty); |
| 869 | if (!transparent.isEmpty()) { |
| 870 | glClearColor(0,0,0,0); |
| 871 | Region::const_iterator it = transparent.begin(); |
| 872 | Region::const_iterator const end = transparent.end(); |
| 873 | const int32_t height = hw.getHeight(); |
| 874 | while (it != end) { |
| 875 | const Rect& r(*it++); |
| 876 | const GLint sy = height - (r.top + r.height()); |
| 877 | glScissor(r.left, sy, r.width(), r.height()); |
| 878 | glClear(GL_COLOR_BUFFER_BIT); |
| 879 | } |
| 880 | } |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 881 | |
| 882 | |
| 883 | /* |
| 884 | * and then, render the layers targeted at the framebuffer |
| 885 | */ |
| 886 | for (size_t i=0 ; i<count ; i++) { |
| 887 | if (cur) { |
| 888 | if (!(cur[i].compositionType == HWC_FRAMEBUFFER) || |
| 889 | cur[i].flags & HWC_SKIP_LAYER) { |
| 890 | // skip layers handled by the HAL |
| 891 | continue; |
| 892 | } |
| 893 | } |
| 894 | const sp<LayerBase>& layer(layers[i]); |
| 895 | const Region clip(dirty.intersect(layer->visibleRegionScreen)); |
| 896 | if (!clip.isEmpty()) { |
| 897 | layer->draw(clip); |
| 898 | } |
| 899 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | void SurfaceFlinger::unlockClients() |
| 903 | { |
| 904 | const LayerVector& drawingLayers(mDrawingState.layersSortedByZ); |
| 905 | const size_t count = drawingLayers.size(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 906 | sp<LayerBase> const* const layers = drawingLayers.array(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | for (size_t i=0 ; i<count ; ++i) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 908 | const sp<LayerBase>& layer = layers[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | layer->finishPageFlip(); |
| 910 | } |
| 911 | } |
| 912 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | void SurfaceFlinger::debugFlashRegions() |
| 914 | { |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 915 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 916 | const uint32_t flags = hw.getFlags(); |
Mathias Agopian | 2e20bff | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 917 | |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 918 | if (!((flags & DisplayHardware::SWAP_RECTANGLE) || |
| 919 | (flags & DisplayHardware::BUFFER_PRESERVED))) { |
| 920 | const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ? |
| 921 | mDirtyRegion.bounds() : hw.bounds()); |
| 922 | composeSurfaces(repaint); |
| 923 | } |
| 924 | |
| 925 | TextureManager::deactivateTextures(); |
| 926 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | glDisable(GL_BLEND); |
| 928 | glDisable(GL_DITHER); |
| 929 | glDisable(GL_SCISSOR_TEST); |
| 930 | |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 931 | static int toggle = 0; |
| 932 | toggle = 1 - toggle; |
| 933 | if (toggle) { |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 934 | glColor4f(1, 0, 1, 1); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 935 | } else { |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 936 | glColor4f(1, 1, 0, 1); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 939 | Region::const_iterator it = mDirtyRegion.begin(); |
| 940 | Region::const_iterator const end = mDirtyRegion.end(); |
| 941 | while (it != end) { |
| 942 | const Rect& r = *it++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | GLfloat vertices[][2] = { |
| 944 | { r.left, r.top }, |
| 945 | { r.left, r.bottom }, |
| 946 | { r.right, r.bottom }, |
| 947 | { r.right, r.top } |
| 948 | }; |
| 949 | glVertexPointer(2, GL_FLOAT, 0, vertices); |
| 950 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 951 | } |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 952 | |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 953 | if (mInvalidRegion.isEmpty()) { |
| 954 | mDirtyRegion.dump("mDirtyRegion"); |
| 955 | mInvalidRegion.dump("mInvalidRegion"); |
| 956 | } |
| 957 | hw.flip(mInvalidRegion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | |
| 959 | if (mDebugRegion > 1) |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 960 | usleep(mDebugRegion * 1000); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | |
| 962 | glEnable(GL_SCISSOR_TEST); |
| 963 | //mDirtyRegion.dump("mDirtyRegion"); |
| 964 | } |
| 965 | |
| 966 | void SurfaceFlinger::drawWormhole() const |
| 967 | { |
| 968 | const Region region(mWormholeRegion.intersect(mDirtyRegion)); |
| 969 | if (region.isEmpty()) |
| 970 | return; |
| 971 | |
| 972 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 973 | const int32_t width = hw.getWidth(); |
| 974 | const int32_t height = hw.getHeight(); |
| 975 | |
| 976 | glDisable(GL_BLEND); |
| 977 | glDisable(GL_DITHER); |
| 978 | |
| 979 | if (LIKELY(!mDebugBackground)) { |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 980 | glClearColor(0,0,0,0); |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 981 | Region::const_iterator it = region.begin(); |
| 982 | Region::const_iterator const end = region.end(); |
| 983 | while (it != end) { |
| 984 | const Rect& r = *it++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | const GLint sy = height - (r.top + r.height()); |
| 986 | glScissor(r.left, sy, r.width(), r.height()); |
| 987 | glClear(GL_COLOR_BUFFER_BIT); |
| 988 | } |
| 989 | } else { |
| 990 | const GLshort vertices[][2] = { { 0, 0 }, { width, 0 }, |
| 991 | { width, height }, { 0, height } }; |
| 992 | const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } }; |
| 993 | glVertexPointer(2, GL_SHORT, 0, vertices); |
| 994 | glTexCoordPointer(2, GL_SHORT, 0, tcoords); |
| 995 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 996 | #if defined(GL_OES_texture_external) |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 997 | if (GLExtensions::getInstance().haveTextureExternal()) { |
| 998 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 999 | } |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1000 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | glEnable(GL_TEXTURE_2D); |
| 1002 | glBindTexture(GL_TEXTURE_2D, mWormholeTexName); |
| 1003 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 1004 | glMatrixMode(GL_TEXTURE); |
| 1005 | glLoadIdentity(); |
| 1006 | glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1); |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1007 | Region::const_iterator it = region.begin(); |
| 1008 | Region::const_iterator const end = region.end(); |
| 1009 | while (it != end) { |
| 1010 | const Rect& r = *it++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | const GLint sy = height - (r.top + r.height()); |
| 1012 | glScissor(r.left, sy, r.width(), r.height()); |
| 1013 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1014 | } |
| 1015 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | void SurfaceFlinger::debugShowFPS() const |
| 1020 | { |
| 1021 | static int mFrameCount; |
| 1022 | static int mLastFrameCount = 0; |
| 1023 | static nsecs_t mLastFpsTime = 0; |
| 1024 | static float mFps = 0; |
| 1025 | mFrameCount++; |
| 1026 | nsecs_t now = systemTime(); |
| 1027 | nsecs_t diff = now - mLastFpsTime; |
| 1028 | if (diff > ms2ns(250)) { |
| 1029 | mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff; |
| 1030 | mLastFpsTime = now; |
| 1031 | mLastFrameCount = mFrameCount; |
| 1032 | } |
| 1033 | // XXX: mFPS has the value we want |
| 1034 | } |
| 1035 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1036 | status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | { |
| 1038 | Mutex::Autolock _l(mStateLock); |
| 1039 | addLayer_l(layer); |
| 1040 | setTransactionFlags(eTransactionNeeded|eTraversalNeeded); |
| 1041 | return NO_ERROR; |
| 1042 | } |
| 1043 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1044 | status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer) |
| 1045 | { |
Mathias Agopian | 1efba9a | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 1046 | ssize_t i = mCurrentState.layersSortedByZ.add(layer); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1047 | return (i < 0) ? status_t(i) : status_t(NO_ERROR); |
| 1048 | } |
| 1049 | |
| 1050 | ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client, |
| 1051 | const sp<LayerBaseClient>& lbc) |
| 1052 | { |
| 1053 | Mutex::Autolock _l(mStateLock); |
| 1054 | |
| 1055 | // attach this layer to the client |
| 1056 | ssize_t name = client->attachLayer(lbc); |
| 1057 | |
| 1058 | // add this layer to the current state list |
| 1059 | addLayer_l(lbc); |
| 1060 | |
| 1061 | return name; |
| 1062 | } |
| 1063 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1064 | status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | { |
| 1066 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1067 | status_t err = purgatorizeLayer_l(layer); |
| 1068 | if (err == NO_ERROR) |
| 1069 | setTransactionFlags(eTransactionNeeded); |
| 1070 | return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1073 | status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1075 | sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient()); |
| 1076 | if (lbc != 0) { |
| 1077 | mLayerMap.removeItem( lbc->getSurface()->asBinder() ); |
| 1078 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase); |
| 1080 | if (index >= 0) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1081 | mLayersRemoved = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | return NO_ERROR; |
| 1083 | } |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1084 | return status_t(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1087 | status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase) |
| 1088 | { |
Mathias Agopian | 2e4b68d | 2009-09-23 16:44:00 -0700 | [diff] [blame] | 1089 | // remove the layer from the main list (through a transaction). |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1090 | ssize_t err = removeLayer_l(layerBase); |
Mathias Agopian | 2e4b68d | 2009-09-23 16:44:00 -0700 | [diff] [blame] | 1091 | |
Mathias Agopian | 0c4cec7 | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 1092 | layerBase->onRemoved(); |
| 1093 | |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1094 | // it's possible that we don't find a layer, because it might |
| 1095 | // have been destroyed already -- this is not technically an error |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1096 | // from the user because there is a race between Client::destroySurface(), |
| 1097 | // ~Client() and ~ISurface(). |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1098 | return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err; |
| 1099 | } |
| 1100 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1101 | status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1102 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1103 | layer->forceVisibilityTransaction(); |
| 1104 | setTransactionFlags(eTraversalNeeded); |
| 1105 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) |
| 1109 | { |
| 1110 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 1111 | } |
| 1112 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1113 | uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | { |
| 1115 | uint32_t old = android_atomic_or(flags, &mTransactionFlags); |
| 1116 | if ((old & flags)==0) { // wake the server up |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1117 | signalEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | } |
| 1119 | return old; |
| 1120 | } |
| 1121 | |
| 1122 | void SurfaceFlinger::openGlobalTransaction() |
| 1123 | { |
| 1124 | android_atomic_inc(&mTransactionCount); |
| 1125 | } |
| 1126 | |
| 1127 | void SurfaceFlinger::closeGlobalTransaction() |
| 1128 | { |
| 1129 | if (android_atomic_dec(&mTransactionCount) == 1) { |
| 1130 | signalEvent(); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1131 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1132 | // if there is a transaction with a resize, wait for it to |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1133 | // take effect before returning. |
| 1134 | Mutex::Autolock _l(mStateLock); |
| 1135 | while (mResizeTransationPending) { |
Mathias Agopian | 98a9c56 | 2009-09-30 14:42:13 -0700 | [diff] [blame] | 1136 | status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); |
| 1137 | if (CC_UNLIKELY(err != NO_ERROR)) { |
| 1138 | // just in case something goes wrong in SF, return to the |
| 1139 | // called after a few seconds. |
| 1140 | LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!"); |
| 1141 | mResizeTransationPending = false; |
| 1142 | break; |
| 1143 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags) |
| 1149 | { |
| 1150 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1151 | return BAD_VALUE; |
| 1152 | |
| 1153 | Mutex::Autolock _l(mStateLock); |
| 1154 | mCurrentState.freezeDisplay = 1; |
| 1155 | setTransactionFlags(eTransactionNeeded); |
| 1156 | |
| 1157 | // flags is intended to communicate some sort of animation behavior |
Mathias Agopian | ed81f22 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 1158 | // (for instance fading) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | return NO_ERROR; |
| 1160 | } |
| 1161 | |
| 1162 | status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags) |
| 1163 | { |
| 1164 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1165 | return BAD_VALUE; |
| 1166 | |
| 1167 | Mutex::Autolock _l(mStateLock); |
| 1168 | mCurrentState.freezeDisplay = 0; |
| 1169 | setTransactionFlags(eTransactionNeeded); |
| 1170 | |
| 1171 | // flags is intended to communicate some sort of animation behavior |
Mathias Agopian | ed81f22 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 1172 | // (for instance fading) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | return NO_ERROR; |
| 1174 | } |
| 1175 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1176 | int SurfaceFlinger::setOrientation(DisplayID dpy, |
Mathias Agopian | eb0c86e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 1177 | int orientation, uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | { |
| 1179 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1180 | return BAD_VALUE; |
| 1181 | |
| 1182 | Mutex::Autolock _l(mStateLock); |
| 1183 | if (mCurrentState.orientation != orientation) { |
| 1184 | if (uint32_t(orientation)<=eOrientation270 || orientation==42) { |
Mathias Agopian | eb0c86e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 1185 | mCurrentState.orientationType = flags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | mCurrentState.orientation = orientation; |
| 1187 | setTransactionFlags(eTransactionNeeded); |
| 1188 | mTransactionCV.wait(mStateLock); |
| 1189 | } else { |
| 1190 | orientation = BAD_VALUE; |
| 1191 | } |
| 1192 | } |
| 1193 | return orientation; |
| 1194 | } |
| 1195 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1196 | sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid, |
Mathias Agopian | 770492c | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 1197 | const String8& name, ISurfaceComposerClient::surface_data_t* params, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | DisplayID d, uint32_t w, uint32_t h, PixelFormat format, |
| 1199 | uint32_t flags) |
| 1200 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1201 | sp<LayerBaseClient> layer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | sp<LayerBaseClient::Surface> surfaceHandle; |
Mathias Agopian | 4d2dbeb | 2009-07-09 18:16:43 -0700 | [diff] [blame] | 1203 | |
| 1204 | if (int32_t(w|h) < 0) { |
| 1205 | LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)", |
| 1206 | int(w), int(h)); |
| 1207 | return surfaceHandle; |
| 1208 | } |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | //LOGD("createSurface for pid %d (%d x %d)", pid, w, h); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1211 | sp<Layer> normalLayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | switch (flags & eFXSurfaceMask) { |
| 1213 | case eFXSurfaceNormal: |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1214 | #if HAS_PUSH_BUFFERS |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | if (UNLIKELY(flags & ePushBuffers)) { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1216 | layer = createPushBuffersSurface(client, d, w, h, flags); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1217 | } else |
| 1218 | #endif |
| 1219 | { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1220 | normalLayer = createNormalSurface(client, d, w, h, flags, format); |
| 1221 | layer = normalLayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | } |
| 1223 | break; |
| 1224 | case eFXSurfaceBlur: |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1225 | layer = createBlurSurface(client, d, w, h, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | break; |
| 1227 | case eFXSurfaceDim: |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1228 | layer = createDimSurface(client, d, w, h, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | break; |
| 1230 | } |
| 1231 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1232 | if (layer != 0) { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1233 | layer->initStates(w, h, flags); |
Mathias Agopian | 5d26c1e | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 1234 | layer->setName(name); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1235 | ssize_t token = addClientLayer(client, layer); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1236 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | surfaceHandle = layer->getSurface(); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1238 | if (surfaceHandle != 0) { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1239 | params->token = token; |
Mathias Agopian | 18b6b49 | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1240 | params->identity = surfaceHandle->getIdentity(); |
| 1241 | params->width = w; |
| 1242 | params->height = h; |
| 1243 | params->format = format; |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1244 | if (normalLayer != 0) { |
| 1245 | Mutex::Autolock _l(mStateLock); |
| 1246 | mLayerMap.add(surfaceHandle->asBinder(), normalLayer); |
| 1247 | } |
Mathias Agopian | 18b6b49 | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1248 | } |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1249 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1250 | setTransactionFlags(eTransactionNeeded); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | return surfaceHandle; |
| 1254 | } |
| 1255 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1256 | sp<Layer> SurfaceFlinger::createNormalSurface( |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1257 | const sp<Client>& client, DisplayID display, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1258 | uint32_t w, uint32_t h, uint32_t flags, |
Mathias Agopian | 18b6b49 | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1259 | PixelFormat& format) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | { |
| 1261 | // initialize the surfaces |
| 1262 | switch (format) { // TODO: take h/w into account |
| 1263 | case PIXEL_FORMAT_TRANSPARENT: |
| 1264 | case PIXEL_FORMAT_TRANSLUCENT: |
| 1265 | format = PIXEL_FORMAT_RGBA_8888; |
| 1266 | break; |
| 1267 | case PIXEL_FORMAT_OPAQUE: |
Mathias Agopian | 4cfb3a6 | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1268 | #ifdef NO_RGBX_8888 |
| 1269 | format = PIXEL_FORMAT_RGB_565; |
| 1270 | #else |
Mathias Agopian | 59962ce | 2010-04-05 18:01:24 -0700 | [diff] [blame] | 1271 | format = PIXEL_FORMAT_RGBX_8888; |
Mathias Agopian | 4cfb3a6 | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1272 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | break; |
| 1274 | } |
| 1275 | |
Mathias Agopian | 4cfb3a6 | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1276 | #ifdef NO_RGBX_8888 |
| 1277 | if (format == PIXEL_FORMAT_RGBX_8888) |
| 1278 | format = PIXEL_FORMAT_RGBA_8888; |
| 1279 | #endif |
| 1280 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1281 | sp<Layer> layer = new Layer(this, display, client); |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1282 | status_t err = layer->setBuffers(w, h, format, flags); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1283 | if (LIKELY(err != NO_ERROR)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1285 | layer.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | } |
| 1287 | return layer; |
| 1288 | } |
| 1289 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1290 | sp<LayerBlur> SurfaceFlinger::createBlurSurface( |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1291 | const sp<Client>& client, DisplayID display, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1292 | uint32_t w, uint32_t h, uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1294 | sp<LayerBlur> layer = new LayerBlur(this, display, client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | layer->initStates(w, h, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | return layer; |
| 1297 | } |
| 1298 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1299 | sp<LayerDim> SurfaceFlinger::createDimSurface( |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1300 | const sp<Client>& client, DisplayID display, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1301 | uint32_t w, uint32_t h, uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1302 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1303 | sp<LayerDim> layer = new LayerDim(this, display, client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | layer->initStates(w, h, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1305 | return layer; |
| 1306 | } |
| 1307 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1308 | sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface( |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1309 | const sp<Client>& client, DisplayID display, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1310 | uint32_t w, uint32_t h, uint32_t flags) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1312 | sp<LayerBuffer> layer = new LayerBuffer(this, display, client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | layer->initStates(w, h, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | return layer; |
| 1315 | } |
| 1316 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1317 | 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] | 1318 | { |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1319 | /* |
| 1320 | * called by the window manager, when a surface should be marked for |
| 1321 | * destruction. |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1322 | * |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1323 | * The surface is removed from the current and drawing lists, but placed |
| 1324 | * in the purgatory queue, so it's not destroyed right-away (we need |
| 1325 | * to wait for all client's references to go away first). |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1326 | */ |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1327 | |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1328 | status_t err = NAME_NOT_FOUND; |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1329 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1330 | sp<LayerBaseClient> layer = client->getLayerUser(sid); |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1331 | if (layer != 0) { |
| 1332 | err = purgatorizeLayer_l(layer); |
| 1333 | if (err == NO_ERROR) { |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1334 | setTransactionFlags(eTransactionNeeded); |
| 1335 | } |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1336 | } |
| 1337 | return err; |
| 1338 | } |
| 1339 | |
| 1340 | status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer) |
| 1341 | { |
Mathias Agopian | 359140c | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 1342 | // called by ~ISurface() when all references are gone |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1343 | |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1344 | class MessageDestroySurface : public MessageBase { |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1345 | SurfaceFlinger* flinger; |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1346 | sp<LayerBaseClient> layer; |
| 1347 | public: |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1348 | MessageDestroySurface( |
| 1349 | SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer) |
| 1350 | : flinger(flinger), layer(layer) { } |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1351 | virtual bool handler() { |
Mathias Agopian | c8fb5b1 | 2009-06-19 16:24:02 -0700 | [diff] [blame] | 1352 | sp<LayerBaseClient> l(layer); |
| 1353 | layer.clear(); // clear it outside of the lock; |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1354 | Mutex::Autolock _l(flinger->mStateLock); |
Mathias Agopian | 359140c | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 1355 | /* |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1356 | * remove the layer from the current list -- chances are that it's |
| 1357 | * not in the list anyway, because it should have been removed |
| 1358 | * already upon request of the client (eg: window manager). |
Mathias Agopian | 359140c | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 1359 | * However, a buggy client could have not done that. |
| 1360 | * Since we know we don't have any more clients, we don't need |
| 1361 | * to use the purgatory. |
| 1362 | */ |
Mathias Agopian | c8fb5b1 | 2009-06-19 16:24:02 -0700 | [diff] [blame] | 1363 | status_t err = flinger->removeLayer_l(l); |
Mathias Agopian | 2e4b68d | 2009-09-23 16:44:00 -0700 | [diff] [blame] | 1364 | LOGE_IF(err<0 && err != NAME_NOT_FOUND, |
| 1365 | "error removing layer=%p (%s)", l.get(), strerror(-err)); |
Mathias Agopian | 6ead5d9 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1366 | return true; |
| 1367 | } |
| 1368 | }; |
Mathias Agopian | 2d5ee25 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1369 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1370 | postMessageAsync( new MessageDestroySurface(this, layer) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1371 | return NO_ERROR; |
| 1372 | } |
| 1373 | |
| 1374 | status_t SurfaceFlinger::setClientState( |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1375 | const sp<Client>& client, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | int32_t count, |
| 1377 | const layer_state_t* states) |
| 1378 | { |
| 1379 | Mutex::Autolock _l(mStateLock); |
| 1380 | uint32_t flags = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | for (int i=0 ; i<count ; i++) { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1382 | const layer_state_t& s(states[i]); |
| 1383 | sp<LayerBaseClient> layer(client->getLayerUser(s.surface)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1384 | if (layer != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | const uint32_t what = s.what; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1386 | if (what & ePositionChanged) { |
| 1387 | if (layer->setPosition(s.x, s.y)) |
| 1388 | flags |= eTraversalNeeded; |
| 1389 | } |
| 1390 | if (what & eLayerChanged) { |
Mathias Agopian | 1efba9a | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 1391 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | if (layer->setLayer(s.z)) { |
Mathias Agopian | 1efba9a | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 1393 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 1394 | mCurrentState.layersSortedByZ.add(layer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1395 | // we need traversal (state changed) |
| 1396 | // AND transaction (list changed) |
| 1397 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 1398 | } |
| 1399 | } |
| 1400 | if (what & eSizeChanged) { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1401 | if (layer->setSize(s.w, s.h)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | flags |= eTraversalNeeded; |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1403 | mResizeTransationPending = true; |
| 1404 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | } |
| 1406 | if (what & eAlphaChanged) { |
| 1407 | if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f))) |
| 1408 | flags |= eTraversalNeeded; |
| 1409 | } |
| 1410 | if (what & eMatrixChanged) { |
| 1411 | if (layer->setMatrix(s.matrix)) |
| 1412 | flags |= eTraversalNeeded; |
| 1413 | } |
| 1414 | if (what & eTransparentRegionChanged) { |
| 1415 | if (layer->setTransparentRegionHint(s.transparentRegion)) |
| 1416 | flags |= eTraversalNeeded; |
| 1417 | } |
| 1418 | if (what & eVisibilityChanged) { |
| 1419 | if (layer->setFlags(s.flags, s.mask)) |
| 1420 | flags |= eTraversalNeeded; |
| 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | if (flags) { |
| 1425 | setTransactionFlags(flags); |
| 1426 | } |
| 1427 | return NO_ERROR; |
| 1428 | } |
| 1429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | void SurfaceFlinger::screenReleased(int dpy) |
| 1431 | { |
| 1432 | // this may be called by a signal handler, we can't do too much in here |
| 1433 | android_atomic_or(eConsoleReleased, &mConsoleSignals); |
| 1434 | signalEvent(); |
| 1435 | } |
| 1436 | |
| 1437 | void SurfaceFlinger::screenAcquired(int dpy) |
| 1438 | { |
| 1439 | // this may be called by a signal handler, we can't do too much in here |
| 1440 | android_atomic_or(eConsoleAcquired, &mConsoleSignals); |
| 1441 | signalEvent(); |
| 1442 | } |
| 1443 | |
| 1444 | status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args) |
| 1445 | { |
| 1446 | const size_t SIZE = 1024; |
| 1447 | char buffer[SIZE]; |
| 1448 | String8 result; |
Mathias Agopian | 151e859 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1449 | if (!mDump.checkCalling()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1451 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", |
| 1452 | IPCThreadState::self()->getCallingPid(), |
| 1453 | IPCThreadState::self()->getCallingUid()); |
| 1454 | result.append(buffer); |
| 1455 | } else { |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1456 | |
| 1457 | // figure out if we're stuck somewhere |
| 1458 | const nsecs_t now = systemTime(); |
| 1459 | const nsecs_t inSwapBuffers(mDebugInSwapBuffers); |
| 1460 | const nsecs_t inTransaction(mDebugInTransaction); |
| 1461 | nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0; |
| 1462 | nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; |
| 1463 | |
| 1464 | // Try to get the main lock, but don't insist if we can't |
| 1465 | // (this would indicate SF is stuck, but we want to be able to |
| 1466 | // print something in dumpsys). |
| 1467 | int retry = 3; |
| 1468 | while (mStateLock.tryLock()<0 && --retry>=0) { |
| 1469 | usleep(1000000); |
| 1470 | } |
| 1471 | const bool locked(retry >= 0); |
| 1472 | if (!locked) { |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1473 | snprintf(buffer, SIZE, |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1474 | "SurfaceFlinger appears to be unresponsive, " |
| 1475 | "dumping anyways (no locks held)\n"); |
| 1476 | result.append(buffer); |
| 1477 | } |
| 1478 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 1480 | const size_t count = currentLayers.size(); |
| 1481 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1482 | const sp<LayerBase>& layer(currentLayers[i]); |
| 1483 | layer->dump(result, buffer, SIZE); |
| 1484 | const Layer::State& s(layer->drawingState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | s.transparentRegion.dump(result, "transparentRegion"); |
| 1486 | layer->transparentRegionScreen.dump(result, "transparentRegionScreen"); |
| 1487 | layer->visibleRegionScreen.dump(result, "visibleRegionScreen"); |
| 1488 | } |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1489 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | mWormholeRegion.dump(result, "WormholeRegion"); |
| 1491 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1492 | snprintf(buffer, SIZE, |
| 1493 | " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n", |
| 1494 | mFreezeDisplay?"yes":"no", mFreezeCount, |
| 1495 | mCurrentState.orientation, hw.canDraw()); |
| 1496 | result.append(buffer); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1497 | snprintf(buffer, SIZE, |
| 1498 | " last eglSwapBuffers() time: %f us\n" |
| 1499 | " last transaction time : %f us\n", |
| 1500 | mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0); |
| 1501 | result.append(buffer); |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1502 | |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1503 | if (inSwapBuffersDuration || !locked) { |
| 1504 | snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n", |
| 1505 | inSwapBuffersDuration/1000.0); |
| 1506 | result.append(buffer); |
| 1507 | } |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1508 | |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1509 | if (inTransactionDuration || !locked) { |
| 1510 | snprintf(buffer, SIZE, " transaction time: %f us\n", |
| 1511 | inTransactionDuration/1000.0); |
| 1512 | result.append(buffer); |
| 1513 | } |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1514 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1515 | const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1516 | alloc.dump(result); |
Mathias Agopian | a8d4917 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1517 | |
| 1518 | if (locked) { |
| 1519 | mStateLock.unlock(); |
| 1520 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | } |
| 1522 | write(fd, result.string(), result.size()); |
| 1523 | return NO_ERROR; |
| 1524 | } |
| 1525 | |
| 1526 | status_t SurfaceFlinger::onTransact( |
| 1527 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1528 | { |
| 1529 | switch (code) { |
| 1530 | case CREATE_CONNECTION: |
| 1531 | case OPEN_GLOBAL_TRANSACTION: |
| 1532 | case CLOSE_GLOBAL_TRANSACTION: |
| 1533 | case SET_ORIENTATION: |
| 1534 | case FREEZE_DISPLAY: |
| 1535 | case UNFREEZE_DISPLAY: |
| 1536 | case BOOT_FINISHED: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1537 | { |
| 1538 | // codes that require permission check |
| 1539 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1540 | const int pid = ipc->getCallingPid(); |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 1541 | const int uid = ipc->getCallingUid(); |
Mathias Agopian | 151e859 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1542 | if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) { |
| 1543 | LOGE("Permission Denial: " |
| 1544 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
| 1545 | return PERMISSION_DENIED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); |
| 1550 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
Mathias Agopian | 8c9687a | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 1551 | CHECK_INTERFACE(ISurfaceComposer, data, reply); |
Mathias Agopian | 151e859 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1552 | if (UNLIKELY(!mHardwareTest.checkCalling())) { |
| 1553 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1554 | const int pid = ipc->getCallingPid(); |
| 1555 | const int uid = ipc->getCallingUid(); |
| 1556 | LOGE("Permission Denial: " |
| 1557 | "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] | 1558 | return PERMISSION_DENIED; |
| 1559 | } |
| 1560 | int n; |
| 1561 | switch (code) { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1562 | case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | return NO_ERROR; |
Mathias Agopian | ef07dda | 2009-04-21 18:28:33 -0700 | [diff] [blame] | 1564 | case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | return NO_ERROR; |
| 1566 | case 1002: // SHOW_UPDATES |
| 1567 | n = data.readInt32(); |
| 1568 | mDebugRegion = n ? n : (mDebugRegion ? 0 : 1); |
| 1569 | return NO_ERROR; |
| 1570 | case 1003: // SHOW_BACKGROUND |
| 1571 | n = data.readInt32(); |
| 1572 | mDebugBackground = n ? 1 : 0; |
| 1573 | return NO_ERROR; |
| 1574 | case 1004:{ // repaint everything |
| 1575 | Mutex::Autolock _l(mStateLock); |
| 1576 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1577 | mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe |
| 1578 | signalEvent(); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1579 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1580 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1581 | case 1005:{ // force transaction |
| 1582 | setTransactionFlags(eTransactionNeeded|eTraversalNeeded); |
| 1583 | return NO_ERROR; |
| 1584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | case 1007: // set mFreezeCount |
| 1586 | mFreezeCount = data.readInt32(); |
Mathias Agopian | 0e44976 | 2009-12-01 17:23:28 -0800 | [diff] [blame] | 1587 | mFreezeDisplayTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | return NO_ERROR; |
| 1589 | case 1010: // interrogate. |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1590 | reply->writeInt32(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | reply->writeInt32(0); |
| 1592 | reply->writeInt32(mDebugRegion); |
| 1593 | reply->writeInt32(mDebugBackground); |
| 1594 | return NO_ERROR; |
| 1595 | case 1013: { |
| 1596 | Mutex::Autolock _l(mStateLock); |
| 1597 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1598 | reply->writeInt32(hw.getPageFlipCount()); |
| 1599 | } |
| 1600 | return NO_ERROR; |
| 1601 | } |
| 1602 | } |
| 1603 | return err; |
| 1604 | } |
| 1605 | |
| 1606 | // --------------------------------------------------------------------------- |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1607 | |
| 1608 | sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const |
| 1609 | { |
| 1610 | sp<Layer> result; |
| 1611 | Mutex::Autolock _l(mStateLock); |
| 1612 | result = mLayerMap.valueFor( sur->asBinder() ).promote(); |
| 1613 | return result; |
| 1614 | } |
| 1615 | |
| 1616 | // --------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1617 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1618 | Client::Client(const sp<SurfaceFlinger>& flinger) |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1619 | : mFlinger(flinger), mNameGenerator(1) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1623 | Client::~Client() |
| 1624 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1625 | const size_t count = mLayers.size(); |
| 1626 | for (size_t i=0 ; i<count ; i++) { |
| 1627 | sp<LayerBaseClient> layer(mLayers.valueAt(i).promote()); |
| 1628 | if (layer != 0) { |
| 1629 | mFlinger->removeLayer(layer); |
| 1630 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | } |
| 1632 | } |
| 1633 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1634 | status_t Client::initCheck() const { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1635 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1637 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1638 | ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer) |
| 1639 | { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1640 | int32_t name = android_atomic_inc(&mNameGenerator); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1641 | mLayers.add(name, layer); |
| 1642 | return name; |
| 1643 | } |
| 1644 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1645 | void Client::detachLayer(const LayerBaseClient* layer) |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1646 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1647 | // we do a linear search here, because this doesn't happen often |
| 1648 | const size_t count = mLayers.size(); |
| 1649 | for (size_t i=0 ; i<count ; i++) { |
| 1650 | if (mLayers.valueAt(i) == layer) { |
| 1651 | mLayers.removeItemsAt(i, 1); |
| 1652 | break; |
| 1653 | } |
| 1654 | } |
| 1655 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1656 | sp<LayerBaseClient> Client::getLayerUser(int32_t i) const { |
| 1657 | sp<LayerBaseClient> lbc; |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1658 | const wp<LayerBaseClient>& layer(mLayers.valueFor(i)); |
| 1659 | if (layer != 0) { |
| 1660 | lbc = layer.promote(); |
| 1661 | LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1662 | } |
| 1663 | return lbc; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1666 | sp<IMemoryHeap> Client::getControlBlock() const { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1667 | return 0; |
| 1668 | } |
| 1669 | ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const { |
| 1670 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1671 | } |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1672 | sp<ISurface> Client::createSurface( |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1673 | ISurfaceComposerClient::surface_data_t* params, int pid, |
| 1674 | const String8& name, |
| 1675 | 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] | 1676 | uint32_t flags) |
| 1677 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1678 | return mFlinger->createSurface(this, pid, name, params, |
| 1679 | display, w, h, format, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1680 | } |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1681 | status_t Client::destroySurface(SurfaceID sid) { |
| 1682 | return mFlinger->removeSurface(this, sid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1683 | } |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1684 | status_t Client::setState(int32_t count, const layer_state_t* states) { |
| 1685 | return mFlinger->setClientState(this, count, states); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | // --------------------------------------------------------------------------- |
| 1689 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1690 | UserClient::UserClient(const sp<SurfaceFlinger>& flinger) |
| 1691 | : ctrlblk(0), mBitmap(0), mFlinger(flinger) |
| 1692 | { |
| 1693 | const int pgsize = getpagesize(); |
| 1694 | const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1)); |
| 1695 | |
| 1696 | mCblkHeap = new MemoryHeapBase(cblksize, 0, |
| 1697 | "SurfaceFlinger Client control-block"); |
| 1698 | |
| 1699 | ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase()); |
| 1700 | if (ctrlblk) { // construct the shared structure in-place. |
| 1701 | new(ctrlblk) SharedClient; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | UserClient::~UserClient() |
| 1706 | { |
| 1707 | if (ctrlblk) { |
| 1708 | ctrlblk->~SharedClient(); // destroy our shared-structure. |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | * When a UserClient dies, it's unclear what to do exactly. |
| 1713 | * We could go ahead and destroy all surfaces linked to that client |
| 1714 | * however, it wouldn't be fair to the main Client |
| 1715 | * (usually the the window-manager), which might want to re-target |
| 1716 | * the layer to another UserClient. |
| 1717 | * I think the best is to do nothing, or not much; in most cases the |
| 1718 | * WM itself will go ahead and clean things up when it detects a client of |
| 1719 | * his has died. |
| 1720 | * The remaining question is what to display? currently we keep |
| 1721 | * just keep the current buffer. |
| 1722 | */ |
| 1723 | } |
| 1724 | |
| 1725 | status_t UserClient::initCheck() const { |
| 1726 | return ctrlblk == 0 ? NO_INIT : NO_ERROR; |
| 1727 | } |
| 1728 | |
| 1729 | void UserClient::detachLayer(const Layer* layer) |
| 1730 | { |
| 1731 | int32_t name = layer->getToken(); |
| 1732 | if (name >= 0) { |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 1733 | int32_t mask = 1LU<<name; |
| 1734 | if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) { |
| 1735 | LOGW("token %d wasn't marked as used %08x", name, int(mBitmap)); |
| 1736 | } |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | sp<IMemoryHeap> UserClient::getControlBlock() const { |
| 1741 | return mCblkHeap; |
| 1742 | } |
| 1743 | |
| 1744 | ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const |
| 1745 | { |
| 1746 | int32_t name = NAME_NOT_FOUND; |
| 1747 | sp<Layer> layer(mFlinger->getLayer(sur)); |
| 1748 | if (layer == 0) return name; |
| 1749 | |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 1750 | // if this layer already has a token, just return it |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1751 | name = layer->getToken(); |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 1752 | if ((name >= 0) && (layer->getClient() == this)) |
| 1753 | return name; |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1754 | |
| 1755 | name = 0; |
| 1756 | do { |
| 1757 | int32_t mask = 1LU<<name; |
| 1758 | if ((android_atomic_or(mask, &mBitmap) & mask) == 0) { |
| 1759 | // we found and locked that name |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 1760 | status_t err = layer->setToken( |
| 1761 | const_cast<UserClient*>(this), ctrlblk, name); |
| 1762 | if (err != NO_ERROR) { |
| 1763 | // free the name |
| 1764 | android_atomic_and(~mask, &mBitmap); |
| 1765 | name = err; |
| 1766 | } |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1767 | break; |
| 1768 | } |
| 1769 | if (++name > 31) |
| 1770 | name = NO_MEMORY; |
| 1771 | } while(name >= 0); |
| 1772 | |
Mathias Agopian | 1debc66 | 2010-06-08 15:40:56 -0700 | [diff] [blame] | 1773 | //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)", |
| 1774 | // sur->asBinder().get(), name, this, mBitmap); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1775 | return name; |
| 1776 | } |
| 1777 | |
| 1778 | sp<ISurface> UserClient::createSurface( |
| 1779 | ISurfaceComposerClient::surface_data_t* params, int pid, |
| 1780 | const String8& name, |
| 1781 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, |
| 1782 | uint32_t flags) { |
| 1783 | return 0; |
| 1784 | } |
| 1785 | status_t UserClient::destroySurface(SurfaceID sid) { |
| 1786 | return INVALID_OPERATION; |
| 1787 | } |
| 1788 | status_t UserClient::setState(int32_t count, const layer_state_t* states) { |
| 1789 | return INVALID_OPERATION; |
| 1790 | } |
| 1791 | |
| 1792 | // --------------------------------------------------------------------------- |
| 1793 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1794 | GraphicPlane::GraphicPlane() |
| 1795 | : mHw(0) |
| 1796 | { |
| 1797 | } |
| 1798 | |
| 1799 | GraphicPlane::~GraphicPlane() { |
| 1800 | delete mHw; |
| 1801 | } |
| 1802 | |
| 1803 | bool GraphicPlane::initialized() const { |
| 1804 | return mHw ? true : false; |
| 1805 | } |
| 1806 | |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 1807 | int GraphicPlane::getWidth() const { |
| 1808 | return mWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 1811 | int GraphicPlane::getHeight() const { |
| 1812 | return mHeight; |
| 1813 | } |
| 1814 | |
| 1815 | void GraphicPlane::setDisplayHardware(DisplayHardware *hw) |
| 1816 | { |
| 1817 | mHw = hw; |
| 1818 | |
| 1819 | // initialize the display orientation transform. |
| 1820 | // it's a constant that should come from the display driver. |
| 1821 | int displayOrientation = ISurfaceComposer::eOrientationDefault; |
| 1822 | char property[PROPERTY_VALUE_MAX]; |
| 1823 | if (property_get("ro.sf.hwrotation", property, NULL) > 0) { |
| 1824 | //displayOrientation |
| 1825 | switch (atoi(property)) { |
| 1826 | case 90: |
| 1827 | displayOrientation = ISurfaceComposer::eOrientation90; |
| 1828 | break; |
| 1829 | case 270: |
| 1830 | displayOrientation = ISurfaceComposer::eOrientation270; |
| 1831 | break; |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | const float w = hw->getWidth(); |
| 1836 | const float h = hw->getHeight(); |
| 1837 | GraphicPlane::orientationToTransfrom(displayOrientation, w, h, |
| 1838 | &mDisplayTransform); |
| 1839 | if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) { |
| 1840 | mDisplayWidth = h; |
| 1841 | mDisplayHeight = w; |
| 1842 | } else { |
| 1843 | mDisplayWidth = w; |
| 1844 | mDisplayHeight = h; |
| 1845 | } |
| 1846 | |
| 1847 | setOrientation(ISurfaceComposer::eOrientationDefault); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | status_t GraphicPlane::orientationToTransfrom( |
| 1851 | int orientation, int w, int h, Transform* tr) |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 1852 | { |
| 1853 | uint32_t flags = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1854 | switch (orientation) { |
| 1855 | case ISurfaceComposer::eOrientationDefault: |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 1856 | flags = Transform::ROT_0; |
| 1857 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1858 | case ISurfaceComposer::eOrientation90: |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 1859 | flags = Transform::ROT_90; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1860 | break; |
| 1861 | case ISurfaceComposer::eOrientation180: |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 1862 | flags = Transform::ROT_180; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | break; |
| 1864 | case ISurfaceComposer::eOrientation270: |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 1865 | flags = Transform::ROT_270; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1866 | break; |
| 1867 | default: |
| 1868 | return BAD_VALUE; |
| 1869 | } |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 1870 | tr->set(flags, w, h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1871 | return NO_ERROR; |
| 1872 | } |
| 1873 | |
| 1874 | status_t GraphicPlane::setOrientation(int orientation) |
| 1875 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | // If the rotation can be handled in hardware, this is where |
| 1877 | // the magic should happen. |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 1878 | |
| 1879 | const DisplayHardware& hw(displayHardware()); |
| 1880 | const float w = mDisplayWidth; |
| 1881 | const float h = mDisplayHeight; |
| 1882 | mWidth = int(w); |
| 1883 | mHeight = int(h); |
| 1884 | |
| 1885 | Transform orientationTransform; |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 1886 | GraphicPlane::orientationToTransfrom(orientation, w, h, |
| 1887 | &orientationTransform); |
| 1888 | if (orientation & ISurfaceComposer::eOrientationSwapMask) { |
| 1889 | mWidth = int(h); |
| 1890 | mHeight = int(w); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | } |
Mathias Agopian | 8c20ca3 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 1892 | |
Mathias Agopian | 3552f53 | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 1893 | mOrientation = orientation; |
Mathias Agopian | 66c77a5 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 1894 | mGlobalTransform = mDisplayTransform * orientationTransform; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1895 | return NO_ERROR; |
| 1896 | } |
| 1897 | |
| 1898 | const DisplayHardware& GraphicPlane::displayHardware() const { |
| 1899 | return *mHw; |
| 1900 | } |
| 1901 | |
| 1902 | const Transform& GraphicPlane::transform() const { |
| 1903 | return mGlobalTransform; |
| 1904 | } |
| 1905 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1906 | EGLDisplay GraphicPlane::getEGLDisplay() const { |
| 1907 | return mHw->getEGLDisplay(); |
| 1908 | } |
| 1909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1910 | // --------------------------------------------------------------------------- |
| 1911 | |
| 1912 | }; // namespace android |