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