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