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