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