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