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