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