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