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