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