The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdint.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <errno.h> |
| 23 | #include <math.h> |
Jean-Baptiste Queru | a837ba7 | 2009-01-26 11:51:12 -0800 | [diff] [blame] | 24 | #include <limits.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | |
| 29 | #include <cutils/log.h> |
| 30 | #include <cutils/properties.h> |
| 31 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 32 | #include <binder/IPCThreadState.h> |
| 33 | #include <binder/IServiceManager.h> |
| 34 | #include <binder/MemoryDealer.h> |
| 35 | #include <binder/MemoryBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include <utils/String8.h> |
| 37 | #include <utils/String16.h> |
| 38 | #include <utils/StopWatch.h> |
| 39 | |
| 40 | #include <ui/PixelFormat.h> |
| 41 | #include <ui/DisplayInfo.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | #include <pixelflinger/pixelflinger.h> |
| 44 | #include <GLES/gl.h> |
| 45 | |
| 46 | #include "clz.h" |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 47 | #include "BufferAllocator.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | #include "Layer.h" |
| 49 | #include "LayerBlur.h" |
| 50 | #include "LayerBuffer.h" |
| 51 | #include "LayerDim.h" |
| 52 | #include "LayerBitmap.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
| 55 | #include "DisplayHardware/DisplayHardware.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 57 | /* ideally AID_GRAPHICS would be in a semi-public header |
| 58 | * or there would be a way to map a user/group name to its id |
| 59 | */ |
| 60 | #ifndef AID_GRAPHICS |
| 61 | #define AID_GRAPHICS 1003 |
| 62 | #endif |
| 63 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | #define DISPLAY_COUNT 1 |
| 65 | |
| 66 | namespace android { |
| 67 | |
| 68 | // --------------------------------------------------------------------------- |
| 69 | |
| 70 | void SurfaceFlinger::instantiate() { |
| 71 | defaultServiceManager()->addService( |
| 72 | String16("SurfaceFlinger"), new SurfaceFlinger()); |
| 73 | } |
| 74 | |
| 75 | void SurfaceFlinger::shutdown() { |
| 76 | // we should unregister here, but not really because |
| 77 | // when (if) the service manager goes away, all the services |
| 78 | // it has a reference to will leave too. |
| 79 | } |
| 80 | |
| 81 | // --------------------------------------------------------------------------- |
| 82 | |
| 83 | SurfaceFlinger::LayerVector::LayerVector(const SurfaceFlinger::LayerVector& rhs) |
| 84 | : lookup(rhs.lookup), layers(rhs.layers) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | ssize_t SurfaceFlinger::LayerVector::indexOf( |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 89 | const sp<LayerBase>& key, size_t guess) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | { |
| 91 | if (guess<size() && lookup.keyAt(guess) == key) |
| 92 | return guess; |
| 93 | const ssize_t i = lookup.indexOfKey(key); |
| 94 | if (i>=0) { |
| 95 | const size_t idx = lookup.valueAt(i); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 96 | LOGE_IF(layers[idx]!=key, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | "LayerVector[%p]: layers[%d]=%p, key=%p", |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 98 | this, int(idx), layers[idx].get(), key.get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | return idx; |
| 100 | } |
| 101 | return i; |
| 102 | } |
| 103 | |
| 104 | ssize_t SurfaceFlinger::LayerVector::add( |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 105 | const sp<LayerBase>& layer, |
| 106 | Vector< sp<LayerBase> >::compar_t cmp) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | { |
| 108 | size_t count = layers.size(); |
| 109 | ssize_t l = 0; |
| 110 | ssize_t h = count-1; |
| 111 | ssize_t mid; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 112 | sp<LayerBase> const* a = layers.array(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | while (l <= h) { |
| 114 | mid = l + (h - l)/2; |
| 115 | const int c = cmp(a+mid, &layer); |
| 116 | if (c == 0) { l = mid; break; } |
| 117 | else if (c<0) { l = mid+1; } |
| 118 | else { h = mid-1; } |
| 119 | } |
| 120 | size_t order = l; |
| 121 | while (order<count && !cmp(&layer, a+order)) { |
| 122 | order++; |
| 123 | } |
| 124 | count = lookup.size(); |
| 125 | for (size_t i=0 ; i<count ; i++) { |
| 126 | if (lookup.valueAt(i) >= order) { |
| 127 | lookup.editValueAt(i)++; |
| 128 | } |
| 129 | } |
| 130 | layers.insertAt(layer, order); |
| 131 | lookup.add(layer, order); |
| 132 | return order; |
| 133 | } |
| 134 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 135 | ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | { |
| 137 | const ssize_t keyIndex = lookup.indexOfKey(layer); |
| 138 | if (keyIndex >= 0) { |
| 139 | const size_t index = lookup.valueAt(keyIndex); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 140 | LOGE_IF(layers[index]!=layer, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | "LayerVector[%p]: layers[%u]=%p, layer=%p", |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 142 | this, int(index), layers[index].get(), layer.get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | layers.removeItemsAt(index); |
| 144 | lookup.removeItemsAt(keyIndex); |
| 145 | const size_t count = lookup.size(); |
| 146 | for (size_t i=0 ; i<count ; i++) { |
| 147 | if (lookup.valueAt(i) >= size_t(index)) { |
| 148 | lookup.editValueAt(i)--; |
| 149 | } |
| 150 | } |
| 151 | return index; |
| 152 | } |
| 153 | return NAME_NOT_FOUND; |
| 154 | } |
| 155 | |
| 156 | ssize_t SurfaceFlinger::LayerVector::reorder( |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 157 | const sp<LayerBase>& layer, |
| 158 | Vector< sp<LayerBase> >::compar_t cmp) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | { |
| 160 | // XXX: it's a little lame. but oh well... |
| 161 | ssize_t err = remove(layer); |
| 162 | if (err >=0) |
| 163 | err = add(layer, cmp); |
| 164 | return err; |
| 165 | } |
| 166 | |
| 167 | // --------------------------------------------------------------------------- |
| 168 | #if 0 |
| 169 | #pragma mark - |
| 170 | #endif |
| 171 | |
| 172 | SurfaceFlinger::SurfaceFlinger() |
| 173 | : BnSurfaceComposer(), Thread(false), |
| 174 | mTransactionFlags(0), |
| 175 | mTransactionCount(0), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 176 | mLayersRemoved(false), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | mBootTime(systemTime()), |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 178 | mHardwareTest("android.permission.HARDWARE_TEST"), |
| 179 | mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"), |
| 180 | mDump("android.permission.DUMP"), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | mLastScheduledBroadcast(NULL), |
| 182 | mVisibleRegionsDirty(false), |
| 183 | mDeferReleaseConsole(false), |
| 184 | mFreezeDisplay(false), |
| 185 | mFreezeCount(0), |
The Android Open Source Project | bcef13b | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 186 | mFreezeDisplayTime(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | mDebugRegion(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | mDebugBackground(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | mConsoleSignals(0), |
| 190 | mSecureFrameBuffer(0) |
| 191 | { |
| 192 | init(); |
| 193 | } |
| 194 | |
| 195 | void SurfaceFlinger::init() |
| 196 | { |
| 197 | LOGI("SurfaceFlinger is starting"); |
| 198 | |
| 199 | // debugging stuff... |
| 200 | char value[PROPERTY_VALUE_MAX]; |
| 201 | property_get("debug.sf.showupdates", value, "0"); |
| 202 | mDebugRegion = atoi(value); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | property_get("debug.sf.showbackground", value, "0"); |
| 204 | mDebugBackground = atoi(value); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | |
| 206 | LOGI_IF(mDebugRegion, "showupdates enabled"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | LOGI_IF(mDebugBackground, "showbackground enabled"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | SurfaceFlinger::~SurfaceFlinger() |
| 211 | { |
| 212 | glDeleteTextures(1, &mWormholeTexName); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } |
| 214 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const |
| 216 | { |
| 217 | return graphicPlane(0).displayHardware().getOverlayEngine(); |
| 218 | } |
| 219 | |
| 220 | sp<IMemory> SurfaceFlinger::getCblk() const |
| 221 | { |
| 222 | return mServerCblkMemory; |
| 223 | } |
| 224 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection() |
| 226 | { |
| 227 | Mutex::Autolock _l(mStateLock); |
| 228 | uint32_t token = mTokens.acquire(); |
| 229 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 230 | sp<Client> client = new Client(token, this); |
| 231 | if (client->ctrlblk == 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | mTokens.release(token); |
| 233 | return 0; |
| 234 | } |
| 235 | status_t err = mClientsMap.add(token, client); |
| 236 | if (err < 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | mTokens.release(token); |
| 238 | return 0; |
| 239 | } |
| 240 | sp<BClient> bclient = |
| 241 | new BClient(this, token, client->controlBlockMemory()); |
| 242 | return bclient; |
| 243 | } |
| 244 | |
| 245 | void SurfaceFlinger::destroyConnection(ClientID cid) |
| 246 | { |
| 247 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 248 | sp<Client> client = mClientsMap.valueFor(cid); |
| 249 | if (client != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | // free all the layers this client owns |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 251 | Vector< wp<LayerBaseClient> > layers(client->getLayers()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | const size_t count = layers.size(); |
| 253 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 254 | sp<LayerBaseClient> layer(layers[i].promote()); |
| 255 | if (layer != 0) { |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 256 | purgatorizeLayer_l(layer); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 257 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // the resources associated with this client will be freed |
| 261 | // during the next transaction, after these surfaces have been |
| 262 | // properly removed from the screen |
| 263 | |
| 264 | // remove this client from our ClientID->Client mapping. |
| 265 | mClientsMap.removeItem(cid); |
| 266 | |
| 267 | // and add it to the list of disconnected clients |
| 268 | mDisconnectedClients.add(client); |
| 269 | |
| 270 | // request a transaction |
| 271 | setTransactionFlags(eTransactionNeeded); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const |
| 276 | { |
| 277 | LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy); |
| 278 | const GraphicPlane& plane(mGraphicPlanes[dpy]); |
| 279 | return plane; |
| 280 | } |
| 281 | |
| 282 | GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) |
| 283 | { |
| 284 | return const_cast<GraphicPlane&>( |
| 285 | const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy)); |
| 286 | } |
| 287 | |
| 288 | void SurfaceFlinger::bootFinished() |
| 289 | { |
| 290 | const nsecs_t now = systemTime(); |
| 291 | const nsecs_t duration = now - mBootTime; |
Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 292 | LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); |
| 293 | property_set("ctl.stop", "bootanim"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void SurfaceFlinger::onFirstRef() |
| 297 | { |
| 298 | run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY); |
| 299 | |
| 300 | // Wait for the main thread to be done with its initialization |
| 301 | mReadyToRunBarrier.wait(); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | static inline uint16_t pack565(int r, int g, int b) { |
| 306 | return (r<<11)|(g<<5)|b; |
| 307 | } |
| 308 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | status_t SurfaceFlinger::readyToRun() |
| 310 | { |
| 311 | LOGI( "SurfaceFlinger's main thread ready to run. " |
| 312 | "Initializing graphics H/W..."); |
| 313 | |
| 314 | // create the shared control-block |
| 315 | mServerHeap = new MemoryDealer(4096, MemoryDealer::READ_ONLY); |
| 316 | LOGE_IF(mServerHeap==0, "can't create shared memory dealer"); |
| 317 | |
| 318 | mServerCblkMemory = mServerHeap->allocate(4096); |
| 319 | LOGE_IF(mServerCblkMemory==0, "can't create shared control block"); |
| 320 | |
| 321 | mServerCblk = static_cast<surface_flinger_cblk_t *>(mServerCblkMemory->pointer()); |
| 322 | LOGE_IF(mServerCblk==0, "can't get to shared control block's address"); |
| 323 | new(mServerCblk) surface_flinger_cblk_t; |
| 324 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | // we only support one display currently |
| 326 | int dpy = 0; |
| 327 | |
| 328 | { |
| 329 | // initialize the main display |
| 330 | GraphicPlane& plane(graphicPlane(dpy)); |
| 331 | DisplayHardware* const hw = new DisplayHardware(this, dpy); |
| 332 | plane.setDisplayHardware(hw); |
| 333 | } |
| 334 | |
| 335 | // initialize primary screen |
| 336 | // (other display should be initialized in the same manner, but |
| 337 | // asynchronously, as they could come and go. None of this is supported |
| 338 | // yet). |
| 339 | const GraphicPlane& plane(graphicPlane(dpy)); |
| 340 | const DisplayHardware& hw = plane.displayHardware(); |
| 341 | const uint32_t w = hw.getWidth(); |
| 342 | const uint32_t h = hw.getHeight(); |
| 343 | const uint32_t f = hw.getFormat(); |
| 344 | hw.makeCurrent(); |
| 345 | |
| 346 | // initialize the shared control block |
| 347 | mServerCblk->connected |= 1<<dpy; |
| 348 | display_cblk_t* dcblk = mServerCblk->displays + dpy; |
| 349 | memset(dcblk, 0, sizeof(display_cblk_t)); |
| 350 | dcblk->w = w; |
| 351 | dcblk->h = h; |
| 352 | dcblk->format = f; |
| 353 | dcblk->orientation = ISurfaceComposer::eOrientationDefault; |
| 354 | dcblk->xdpi = hw.getDpiX(); |
| 355 | dcblk->ydpi = hw.getDpiY(); |
| 356 | dcblk->fps = hw.getRefreshRate(); |
| 357 | dcblk->density = hw.getDensity(); |
| 358 | asm volatile ("":::"memory"); |
| 359 | |
| 360 | // Initialize OpenGL|ES |
| 361 | glActiveTexture(GL_TEXTURE0); |
| 362 | glBindTexture(GL_TEXTURE_2D, 0); |
| 363 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 364 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 365 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 366 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 367 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 368 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 369 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 370 | glEnableClientState(GL_VERTEX_ARRAY); |
| 371 | glEnable(GL_SCISSOR_TEST); |
| 372 | glShadeModel(GL_FLAT); |
| 373 | glDisable(GL_DITHER); |
| 374 | glDisable(GL_CULL_FACE); |
| 375 | |
| 376 | const uint16_t g0 = pack565(0x0F,0x1F,0x0F); |
| 377 | const uint16_t g1 = pack565(0x17,0x2f,0x17); |
| 378 | const uint16_t textureData[4] = { g0, g1, g1, g0 }; |
| 379 | glGenTextures(1, &mWormholeTexName); |
| 380 | glBindTexture(GL_TEXTURE_2D, mWormholeTexName); |
| 381 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 382 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 383 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 384 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 385 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, |
| 386 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData); |
| 387 | |
| 388 | glViewport(0, 0, w, h); |
| 389 | glMatrixMode(GL_PROJECTION); |
| 390 | glLoadIdentity(); |
| 391 | glOrthof(0, w, h, 0, 0, 1); |
| 392 | |
| 393 | LayerDim::initDimmer(this, w, h); |
| 394 | |
| 395 | mReadyToRunBarrier.open(); |
| 396 | |
| 397 | /* |
| 398 | * We're now ready to accept clients... |
| 399 | */ |
| 400 | |
Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 401 | // start boot animation |
| 402 | property_set("ctl.start", "bootanim"); |
| 403 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | return NO_ERROR; |
| 405 | } |
| 406 | |
| 407 | // ---------------------------------------------------------------------------- |
| 408 | #if 0 |
| 409 | #pragma mark - |
| 410 | #pragma mark Events Handler |
| 411 | #endif |
| 412 | |
| 413 | void SurfaceFlinger::waitForEvent() |
| 414 | { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 415 | while (true) { |
| 416 | nsecs_t timeout = -1; |
| 417 | if (UNLIKELY(isFrozen())) { |
| 418 | // wait 5 seconds |
| 419 | const nsecs_t freezeDisplayTimeout = ms2ns(5000); |
| 420 | const nsecs_t now = systemTime(); |
| 421 | if (mFreezeDisplayTime == 0) { |
| 422 | mFreezeDisplayTime = now; |
| 423 | } |
| 424 | nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime); |
| 425 | timeout = waitTime>0 ? waitTime : 0; |
The Android Open Source Project | bcef13b | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 426 | } |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 427 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 428 | MessageList::value_type msg = mEventQueue.waitMessage(timeout); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 429 | if (msg != 0) { |
| 430 | mFreezeDisplayTime = 0; |
| 431 | switch (msg->what) { |
| 432 | case MessageQueue::INVALIDATE: |
| 433 | // invalidate message, just return to the main loop |
| 434 | return; |
| 435 | } |
| 436 | } else { |
| 437 | // we timed out |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | if (isFrozen()) { |
| 439 | // we timed out and are still frozen |
| 440 | LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d", |
| 441 | mFreezeDisplay, mFreezeCount); |
| 442 | mFreezeCount = 0; |
The Android Open Source Project | bcef13b | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 443 | mFreezeDisplay = false; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 444 | return; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | void SurfaceFlinger::signalEvent() { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 451 | mEventQueue.invalidate(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void SurfaceFlinger::signal() const { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 455 | // this is the IPC call |
| 456 | const_cast<SurfaceFlinger*>(this)->signalEvent(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void SurfaceFlinger::signalDelayedEvent(nsecs_t delay) |
| 460 | { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 461 | mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // ---------------------------------------------------------------------------- |
| 465 | #if 0 |
| 466 | #pragma mark - |
| 467 | #pragma mark Main loop |
| 468 | #endif |
| 469 | |
| 470 | bool SurfaceFlinger::threadLoop() |
| 471 | { |
| 472 | waitForEvent(); |
| 473 | |
| 474 | // check for transactions |
| 475 | if (UNLIKELY(mConsoleSignals)) { |
| 476 | handleConsoleEvents(); |
| 477 | } |
| 478 | |
| 479 | if (LIKELY(mTransactionCount == 0)) { |
| 480 | // if we're in a global transaction, don't do anything. |
| 481 | const uint32_t mask = eTransactionNeeded | eTraversalNeeded; |
| 482 | uint32_t transactionFlags = getTransactionFlags(mask); |
| 483 | if (LIKELY(transactionFlags)) { |
| 484 | handleTransaction(transactionFlags); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // post surfaces (if needed) |
| 489 | handlePageFlip(); |
| 490 | |
| 491 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 492 | if (LIKELY(hw.canDraw())) { |
| 493 | // repaint the framebuffer (if needed) |
| 494 | handleRepaint(); |
| 495 | |
| 496 | // release the clients before we flip ('cause flip might block) |
| 497 | unlockClients(); |
| 498 | executeScheduledBroadcasts(); |
| 499 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | postFramebuffer(); |
| 501 | } else { |
| 502 | // pretend we did the post |
| 503 | unlockClients(); |
| 504 | executeScheduledBroadcasts(); |
| 505 | usleep(16667); // 60 fps period |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | void SurfaceFlinger::postFramebuffer() |
| 511 | { |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 512 | if (isFrozen()) { |
| 513 | // we are not allowed to draw, but pause a bit to make sure |
| 514 | // apps don't end up using the whole CPU, if they depend on |
| 515 | // surfaceflinger for synchronization. |
| 516 | usleep(8333); // 8.3ms ~ 120fps |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | return; |
| 518 | } |
| 519 | |
| 520 | if (!mInvalidRegion.isEmpty()) { |
| 521 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | hw.flip(mInvalidRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | mInvalidRegion.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | void SurfaceFlinger::handleConsoleEvents() |
| 528 | { |
| 529 | // something to do with the console |
| 530 | const DisplayHardware& hw = graphicPlane(0).displayHardware(); |
| 531 | |
| 532 | int what = android_atomic_and(0, &mConsoleSignals); |
| 533 | if (what & eConsoleAcquired) { |
| 534 | hw.acquireScreen(); |
| 535 | } |
| 536 | |
| 537 | if (mDeferReleaseConsole && hw.canDraw()) { |
Mathias Agopian | 62b7444 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 538 | // We got the release signal before the acquire signal |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | mDeferReleaseConsole = false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | hw.releaseScreen(); |
| 541 | } |
| 542 | |
| 543 | if (what & eConsoleReleased) { |
| 544 | if (hw.canDraw()) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | hw.releaseScreen(); |
| 546 | } else { |
| 547 | mDeferReleaseConsole = true; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | mDirtyRegion.set(hw.bounds()); |
| 552 | } |
| 553 | |
| 554 | void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) |
| 555 | { |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 556 | Vector< sp<LayerBase> > ditchedLayers; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 558 | { // scope for the lock |
| 559 | Mutex::Autolock _l(mStateLock); |
| 560 | handleTransactionLocked(transactionFlags, ditchedLayers); |
| 561 | } |
| 562 | |
| 563 | // do this without lock held |
| 564 | const size_t count = ditchedLayers.size(); |
| 565 | for (size_t i=0 ; i<count ; i++) { |
| 566 | //LOGD("ditching layer %p", ditchedLayers[i].get()); |
| 567 | ditchedLayers[i]->ditch(); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void SurfaceFlinger::handleTransactionLocked( |
| 572 | uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers) |
| 573 | { |
| 574 | const LayerVector& currentLayers(mCurrentState.layersSortedByZ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | const size_t count = currentLayers.size(); |
| 576 | |
| 577 | /* |
| 578 | * Traversal of the children |
| 579 | * (perform the transaction for each of them if needed) |
| 580 | */ |
| 581 | |
| 582 | const bool layersNeedTransaction = transactionFlags & eTraversalNeeded; |
| 583 | if (layersNeedTransaction) { |
| 584 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 585 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded); |
| 587 | if (!trFlags) continue; |
| 588 | |
| 589 | const uint32_t flags = layer->doTransaction(0); |
| 590 | if (flags & Layer::eVisibleRegion) |
| 591 | mVisibleRegionsDirty = true; |
| 592 | |
| 593 | if (flags & Layer::eRestartTransaction) { |
| 594 | // restart the transaction, but back-off a little |
| 595 | layer->setTransactionFlags(eTransactionNeeded); |
| 596 | setTransactionFlags(eTraversalNeeded, ms2ns(8)); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * Perform our own transaction if needed |
| 603 | */ |
| 604 | |
| 605 | if (transactionFlags & eTransactionNeeded) { |
| 606 | if (mCurrentState.orientation != mDrawingState.orientation) { |
| 607 | // the orientation has changed, recompute all visible regions |
| 608 | // and invalidate everything. |
| 609 | |
| 610 | const int dpy = 0; |
| 611 | const int orientation = mCurrentState.orientation; |
Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 612 | const uint32_t type = mCurrentState.orientationType; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | GraphicPlane& plane(graphicPlane(dpy)); |
| 614 | plane.setOrientation(orientation); |
| 615 | |
| 616 | // update the shared control block |
| 617 | const DisplayHardware& hw(plane.displayHardware()); |
| 618 | volatile display_cblk_t* dcblk = mServerCblk->displays + dpy; |
| 619 | dcblk->orientation = orientation; |
| 620 | if (orientation & eOrientationSwapMask) { |
| 621 | // 90 or 270 degrees orientation |
| 622 | dcblk->w = hw.getHeight(); |
| 623 | dcblk->h = hw.getWidth(); |
| 624 | } else { |
| 625 | dcblk->w = hw.getWidth(); |
| 626 | dcblk->h = hw.getHeight(); |
| 627 | } |
| 628 | |
| 629 | mVisibleRegionsDirty = true; |
| 630 | mDirtyRegion.set(hw.bounds()); |
Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 631 | mFreezeDisplayTime = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) { |
| 635 | // freezing or unfreezing the display -> trigger animation if needed |
| 636 | mFreezeDisplay = mCurrentState.freezeDisplay; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 639 | if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) { |
| 640 | // layers have been added |
| 641 | mVisibleRegionsDirty = true; |
| 642 | } |
| 643 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | // some layers might have been removed, so |
| 645 | // we need to update the regions they're exposing. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 646 | if (mLayersRemoved) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | mVisibleRegionsDirty = true; |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 648 | const LayerVector& previousLayers(mDrawingState.layersSortedByZ); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 649 | const size_t count = previousLayers.size(); |
| 650 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 651 | const sp<LayerBase>& layer(previousLayers[i]); |
| 652 | if (currentLayers.indexOf( layer ) < 0) { |
| 653 | // this layer is not visible anymore |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 654 | ditchedLayers.add(layer); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | // get rid of all resources we don't need anymore |
| 660 | // (layers and clients) |
| 661 | free_resources_l(); |
| 662 | } |
| 663 | |
| 664 | commitTransaction(); |
| 665 | } |
| 666 | |
| 667 | sp<FreezeLock> SurfaceFlinger::getFreezeLock() const |
| 668 | { |
| 669 | return new FreezeLock(const_cast<SurfaceFlinger *>(this)); |
| 670 | } |
| 671 | |
| 672 | void SurfaceFlinger::computeVisibleRegions( |
| 673 | LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion) |
| 674 | { |
| 675 | const GraphicPlane& plane(graphicPlane(0)); |
| 676 | const Transform& planeTransform(plane.transform()); |
| 677 | |
| 678 | Region aboveOpaqueLayers; |
| 679 | Region aboveCoveredLayers; |
| 680 | Region dirty; |
| 681 | |
| 682 | bool secureFrameBuffer = false; |
| 683 | |
| 684 | size_t i = currentLayers.size(); |
| 685 | while (i--) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 686 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | layer->validateVisibility(planeTransform); |
| 688 | |
| 689 | // start with the whole surface at its current location |
| 690 | const Layer::State& s = layer->drawingState(); |
| 691 | const Rect bounds(layer->visibleBounds()); |
| 692 | |
| 693 | // handle hidden surfaces by setting the visible region to empty |
| 694 | Region opaqueRegion; |
| 695 | Region visibleRegion; |
| 696 | Region coveredRegion; |
| 697 | if (UNLIKELY((s.flags & ISurfaceComposer::eLayerHidden) || !s.alpha)) { |
| 698 | visibleRegion.clear(); |
| 699 | } else { |
| 700 | const bool translucent = layer->needsBlending(); |
| 701 | visibleRegion.set(bounds); |
| 702 | coveredRegion = visibleRegion; |
| 703 | |
| 704 | // Remove the transparent area from the visible region |
| 705 | if (translucent) { |
| 706 | visibleRegion.subtractSelf(layer->transparentRegionScreen); |
| 707 | } |
| 708 | |
| 709 | // compute the opaque region |
| 710 | if (s.alpha==255 && !translucent && layer->getOrientation()>=0) { |
| 711 | // the opaque region is the visible region |
| 712 | opaqueRegion = visibleRegion; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | // subtract the opaque region covered by the layers above us |
| 717 | visibleRegion.subtractSelf(aboveOpaqueLayers); |
| 718 | coveredRegion.andSelf(aboveCoveredLayers); |
| 719 | |
| 720 | // compute this layer's dirty region |
| 721 | if (layer->contentDirty) { |
| 722 | // we need to invalidate the whole region |
| 723 | dirty = visibleRegion; |
| 724 | // as well, as the old visible region |
| 725 | dirty.orSelf(layer->visibleRegionScreen); |
| 726 | layer->contentDirty = false; |
| 727 | } else { |
| 728 | // compute the exposed region |
| 729 | // dirty = what's visible now - what's wasn't covered before |
| 730 | // = what's visible now & what's was covered before |
| 731 | dirty = visibleRegion.intersect(layer->coveredRegionScreen); |
| 732 | } |
| 733 | dirty.subtractSelf(aboveOpaqueLayers); |
| 734 | |
| 735 | // accumulate to the screen dirty region |
| 736 | dirtyRegion.orSelf(dirty); |
| 737 | |
Mathias Agopian | 62b7444 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 738 | // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | aboveOpaqueLayers.orSelf(opaqueRegion); |
| 740 | aboveCoveredLayers.orSelf(bounds); |
| 741 | |
| 742 | // Store the visible region is screen space |
| 743 | layer->setVisibleRegion(visibleRegion); |
| 744 | layer->setCoveredRegion(coveredRegion); |
| 745 | |
Mathias Agopian | 62b7444 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 746 | // If a secure layer is partially visible, lock down the screen! |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | if (layer->isSecure() && !visibleRegion.isEmpty()) { |
| 748 | secureFrameBuffer = true; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | mSecureFrameBuffer = secureFrameBuffer; |
| 753 | opaqueRegion = aboveOpaqueLayers; |
| 754 | } |
| 755 | |
| 756 | |
| 757 | void SurfaceFlinger::commitTransaction() |
| 758 | { |
| 759 | mDrawingState = mCurrentState; |
| 760 | mTransactionCV.signal(); |
| 761 | } |
| 762 | |
| 763 | void SurfaceFlinger::handlePageFlip() |
| 764 | { |
| 765 | bool visibleRegions = mVisibleRegionsDirty; |
| 766 | LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ); |
| 767 | visibleRegions |= lockPageFlip(currentLayers); |
| 768 | |
| 769 | const DisplayHardware& hw = graphicPlane(0).displayHardware(); |
| 770 | const Region screenRegion(hw.bounds()); |
| 771 | if (visibleRegions) { |
| 772 | Region opaqueRegion; |
| 773 | computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion); |
| 774 | mWormholeRegion = screenRegion.subtract(opaqueRegion); |
| 775 | mVisibleRegionsDirty = false; |
| 776 | } |
| 777 | |
| 778 | unlockPageFlip(currentLayers); |
| 779 | mDirtyRegion.andSelf(screenRegion); |
| 780 | } |
| 781 | |
| 782 | bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers) |
| 783 | { |
| 784 | bool recomputeVisibleRegions = false; |
| 785 | size_t count = currentLayers.size(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 786 | sp<LayerBase> const* layers = currentLayers.array(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 788 | const sp<LayerBase>& layer = layers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | layer->lockPageFlip(recomputeVisibleRegions); |
| 790 | } |
| 791 | return recomputeVisibleRegions; |
| 792 | } |
| 793 | |
| 794 | void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers) |
| 795 | { |
| 796 | const GraphicPlane& plane(graphicPlane(0)); |
| 797 | const Transform& planeTransform(plane.transform()); |
| 798 | size_t count = currentLayers.size(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 799 | sp<LayerBase> const* layers = currentLayers.array(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 801 | const sp<LayerBase>& layer = layers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | layer->unlockPageFlip(planeTransform, mDirtyRegion); |
| 803 | } |
| 804 | } |
| 805 | |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame^] | 806 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | void SurfaceFlinger::handleRepaint() |
| 808 | { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame^] | 809 | // compute the invalid region |
| 810 | mInvalidRegion.orSelf(mDirtyRegion); |
| 811 | if (mInvalidRegion.isEmpty()) { |
| 812 | // nothing to do |
| 813 | return; |
| 814 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | |
| 816 | if (UNLIKELY(mDebugRegion)) { |
| 817 | debugFlashRegions(); |
| 818 | } |
| 819 | |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame^] | 820 | // set the frame buffer |
| 821 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 822 | glMatrixMode(GL_MODELVIEW); |
| 823 | glLoadIdentity(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | |
| 825 | uint32_t flags = hw.getFlags(); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 826 | if ((flags & DisplayHardware::SWAP_RECTANGLE) || |
| 827 | (flags & DisplayHardware::BUFFER_PRESERVED)) |
| 828 | { |
| 829 | // we can redraw only what's dirty |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 830 | } else { |
| 831 | if (flags & DisplayHardware::UPDATE_ON_DEMAND) { |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 832 | // we need to redraw the rectangle that will be updated |
| 833 | // (pushed to the framebuffer). |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | mDirtyRegion.set(mInvalidRegion.bounds()); |
| 835 | } else { |
| 836 | // we need to redraw everything |
| 837 | mDirtyRegion.set(hw.bounds()); |
| 838 | mInvalidRegion = mDirtyRegion; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | // compose all surfaces |
| 843 | composeSurfaces(mDirtyRegion); |
| 844 | |
| 845 | // clear the dirty regions |
| 846 | mDirtyRegion.clear(); |
| 847 | } |
| 848 | |
| 849 | void SurfaceFlinger::composeSurfaces(const Region& dirty) |
| 850 | { |
| 851 | if (UNLIKELY(!mWormholeRegion.isEmpty())) { |
| 852 | // should never happen unless the window manager has a bug |
| 853 | // draw something... |
| 854 | drawWormhole(); |
| 855 | } |
| 856 | const SurfaceFlinger& flinger(*this); |
| 857 | const LayerVector& drawingLayers(mDrawingState.layersSortedByZ); |
| 858 | const size_t count = drawingLayers.size(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 859 | sp<LayerBase> const* const layers = drawingLayers.array(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | for (size_t i=0 ; i<count ; ++i) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 861 | const sp<LayerBase>& layer = layers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | const Region& visibleRegion(layer->visibleRegionScreen); |
| 863 | if (!visibleRegion.isEmpty()) { |
| 864 | const Region clip(dirty.intersect(visibleRegion)); |
| 865 | if (!clip.isEmpty()) { |
| 866 | layer->draw(clip); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | void SurfaceFlinger::unlockClients() |
| 873 | { |
| 874 | const LayerVector& drawingLayers(mDrawingState.layersSortedByZ); |
| 875 | const size_t count = drawingLayers.size(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 876 | sp<LayerBase> const* const layers = drawingLayers.array(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | for (size_t i=0 ; i<count ; ++i) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 878 | const sp<LayerBase>& layer = layers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | layer->finishPageFlip(); |
| 880 | } |
| 881 | } |
| 882 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 883 | void SurfaceFlinger::scheduleBroadcast(const sp<Client>& client) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | { |
| 885 | if (mLastScheduledBroadcast != client) { |
| 886 | mLastScheduledBroadcast = client; |
| 887 | mScheduledBroadcasts.add(client); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | void SurfaceFlinger::executeScheduledBroadcasts() |
| 892 | { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 893 | SortedVector< wp<Client> >& list(mScheduledBroadcasts); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | size_t count = list.size(); |
| 895 | while (count--) { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 896 | sp<Client> client = list[count].promote(); |
| 897 | if (client != 0) { |
| 898 | per_client_cblk_t* const cblk = client->ctrlblk; |
| 899 | if (cblk->lock.tryLock() == NO_ERROR) { |
| 900 | cblk->cv.broadcast(); |
| 901 | list.removeAt(count); |
| 902 | cblk->lock.unlock(); |
| 903 | } else { |
| 904 | // schedule another round |
| 905 | LOGW("executeScheduledBroadcasts() skipped, " |
| 906 | "contention on the client. We'll try again later..."); |
| 907 | signalDelayedEvent(ms2ns(4)); |
| 908 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | mLastScheduledBroadcast = 0; |
| 912 | } |
| 913 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | void SurfaceFlinger::debugFlashRegions() |
| 915 | { |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 916 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 917 | const uint32_t flags = hw.getFlags(); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 918 | |
| 919 | if (!((flags & DisplayHardware::SWAP_RECTANGLE) || |
| 920 | (flags & DisplayHardware::BUFFER_PRESERVED))) { |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 921 | const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ? |
| 922 | mDirtyRegion.bounds() : hw.bounds()); |
| 923 | composeSurfaces(repaint); |
| 924 | } |
| 925 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | glDisable(GL_TEXTURE_2D); |
| 927 | glDisable(GL_BLEND); |
| 928 | glDisable(GL_DITHER); |
| 929 | glDisable(GL_SCISSOR_TEST); |
| 930 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 931 | static int toggle = 0; |
| 932 | toggle = 1 - toggle; |
| 933 | if (toggle) { |
| 934 | glColor4x(0x10000, 0, 0x10000, 0x10000); |
| 935 | } else { |
| 936 | glColor4x(0x10000, 0x10000, 0, 0x10000); |
| 937 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 939 | Region::const_iterator it = mDirtyRegion.begin(); |
| 940 | Region::const_iterator const end = mDirtyRegion.end(); |
| 941 | while (it != end) { |
| 942 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | GLfloat vertices[][2] = { |
| 944 | { r.left, r.top }, |
| 945 | { r.left, r.bottom }, |
| 946 | { r.right, r.bottom }, |
| 947 | { r.right, r.top } |
| 948 | }; |
| 949 | glVertexPointer(2, GL_FLOAT, 0, vertices); |
| 950 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 951 | } |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 952 | |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame^] | 953 | if (mInvalidRegion.isEmpty()) { |
| 954 | mDirtyRegion.dump("mDirtyRegion"); |
| 955 | mInvalidRegion.dump("mInvalidRegion"); |
| 956 | } |
| 957 | hw.flip(mInvalidRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | |
| 959 | if (mDebugRegion > 1) |
| 960 | usleep(mDebugRegion * 1000); |
| 961 | |
| 962 | glEnable(GL_SCISSOR_TEST); |
| 963 | //mDirtyRegion.dump("mDirtyRegion"); |
| 964 | } |
| 965 | |
| 966 | void SurfaceFlinger::drawWormhole() const |
| 967 | { |
| 968 | const Region region(mWormholeRegion.intersect(mDirtyRegion)); |
| 969 | if (region.isEmpty()) |
| 970 | return; |
| 971 | |
| 972 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 973 | const int32_t width = hw.getWidth(); |
| 974 | const int32_t height = hw.getHeight(); |
| 975 | |
| 976 | glDisable(GL_BLEND); |
| 977 | glDisable(GL_DITHER); |
| 978 | |
| 979 | if (LIKELY(!mDebugBackground)) { |
| 980 | glClearColorx(0,0,0,0); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 981 | Region::const_iterator it = region.begin(); |
| 982 | Region::const_iterator const end = region.end(); |
| 983 | while (it != end) { |
| 984 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | const GLint sy = height - (r.top + r.height()); |
| 986 | glScissor(r.left, sy, r.width(), r.height()); |
| 987 | glClear(GL_COLOR_BUFFER_BIT); |
| 988 | } |
| 989 | } else { |
| 990 | const GLshort vertices[][2] = { { 0, 0 }, { width, 0 }, |
| 991 | { width, height }, { 0, height } }; |
| 992 | const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } }; |
| 993 | glVertexPointer(2, GL_SHORT, 0, vertices); |
| 994 | glTexCoordPointer(2, GL_SHORT, 0, tcoords); |
| 995 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 996 | glEnable(GL_TEXTURE_2D); |
| 997 | glBindTexture(GL_TEXTURE_2D, mWormholeTexName); |
| 998 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 999 | glMatrixMode(GL_TEXTURE); |
| 1000 | glLoadIdentity(); |
| 1001 | glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1002 | Region::const_iterator it = region.begin(); |
| 1003 | Region::const_iterator const end = region.end(); |
| 1004 | while (it != end) { |
| 1005 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | const GLint sy = height - (r.top + r.height()); |
| 1007 | glScissor(r.left, sy, r.width(), r.height()); |
| 1008 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1009 | } |
| 1010 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | void SurfaceFlinger::debugShowFPS() const |
| 1015 | { |
| 1016 | static int mFrameCount; |
| 1017 | static int mLastFrameCount = 0; |
| 1018 | static nsecs_t mLastFpsTime = 0; |
| 1019 | static float mFps = 0; |
| 1020 | mFrameCount++; |
| 1021 | nsecs_t now = systemTime(); |
| 1022 | nsecs_t diff = now - mLastFpsTime; |
| 1023 | if (diff > ms2ns(250)) { |
| 1024 | mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff; |
| 1025 | mLastFpsTime = now; |
| 1026 | mLastFrameCount = mFrameCount; |
| 1027 | } |
| 1028 | // XXX: mFPS has the value we want |
| 1029 | } |
| 1030 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1031 | status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | { |
| 1033 | Mutex::Autolock _l(mStateLock); |
| 1034 | addLayer_l(layer); |
| 1035 | setTransactionFlags(eTransactionNeeded|eTraversalNeeded); |
| 1036 | return NO_ERROR; |
| 1037 | } |
| 1038 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1039 | status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | { |
| 1041 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1042 | status_t err = purgatorizeLayer_l(layer); |
| 1043 | if (err == NO_ERROR) |
| 1044 | setTransactionFlags(eTransactionNeeded); |
| 1045 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1048 | status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | { |
| 1050 | layer->forceVisibilityTransaction(); |
| 1051 | setTransactionFlags(eTraversalNeeded); |
| 1052 | return NO_ERROR; |
| 1053 | } |
| 1054 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1055 | status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | { |
| 1057 | ssize_t i = mCurrentState.layersSortedByZ.add( |
| 1058 | layer, &LayerBase::compareCurrentStateZ); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1059 | sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get()); |
| 1060 | if (lbc != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | mLayerMap.add(lbc->serverIndex(), lbc); |
| 1062 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | return NO_ERROR; |
| 1064 | } |
| 1065 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1066 | status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | { |
| 1068 | ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase); |
| 1069 | if (index >= 0) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1070 | mLayersRemoved = true; |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1071 | sp<LayerBaseClient> layer = |
| 1072 | LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get()); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1073 | if (layer != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | mLayerMap.removeItem(layer->serverIndex()); |
| 1075 | } |
| 1076 | return NO_ERROR; |
| 1077 | } |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1078 | return status_t(index); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1081 | status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase) |
| 1082 | { |
| 1083 | // First add the layer to the purgatory list, which makes sure it won't |
| 1084 | // go away, then remove it from the main list (through a transaction). |
| 1085 | ssize_t err = removeLayer_l(layerBase); |
| 1086 | if (err >= 0) { |
| 1087 | mLayerPurgatory.add(layerBase); |
| 1088 | } |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1089 | // it's possible that we don't find a layer, because it might |
| 1090 | // have been destroyed already -- this is not technically an error |
| 1091 | // from the user because there is a race between BClient::destroySurface(), |
| 1092 | // ~BClient() and ~ISurface(). |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1093 | return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err; |
| 1094 | } |
| 1095 | |
| 1096 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | void SurfaceFlinger::free_resources_l() |
| 1098 | { |
| 1099 | // Destroy layers that were removed |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1100 | mLayersRemoved = false; |
| 1101 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1102 | // free resources associated with disconnected clients |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1103 | SortedVector< wp<Client> >& scheduledBroadcasts(mScheduledBroadcasts); |
| 1104 | Vector< sp<Client> >& disconnectedClients(mDisconnectedClients); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | const size_t count = disconnectedClients.size(); |
| 1106 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1107 | sp<Client> client = disconnectedClients[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | // if this client is the scheduled broadcast list, |
| 1109 | // remove it from there (and we don't need to signal it |
| 1110 | // since it is dead). |
| 1111 | int32_t index = scheduledBroadcasts.indexOf(client); |
| 1112 | if (index >= 0) { |
| 1113 | scheduledBroadcasts.removeItemsAt(index); |
| 1114 | } |
| 1115 | mTokens.release(client->cid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | } |
| 1117 | disconnectedClients.clear(); |
| 1118 | } |
| 1119 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) |
| 1121 | { |
| 1122 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 1123 | } |
| 1124 | |
| 1125 | uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay) |
| 1126 | { |
| 1127 | uint32_t old = android_atomic_or(flags, &mTransactionFlags); |
| 1128 | if ((old & flags)==0) { // wake the server up |
| 1129 | if (delay > 0) { |
| 1130 | signalDelayedEvent(delay); |
| 1131 | } else { |
| 1132 | signalEvent(); |
| 1133 | } |
| 1134 | } |
| 1135 | return old; |
| 1136 | } |
| 1137 | |
| 1138 | void SurfaceFlinger::openGlobalTransaction() |
| 1139 | { |
| 1140 | android_atomic_inc(&mTransactionCount); |
| 1141 | } |
| 1142 | |
| 1143 | void SurfaceFlinger::closeGlobalTransaction() |
| 1144 | { |
| 1145 | if (android_atomic_dec(&mTransactionCount) == 1) { |
| 1146 | signalEvent(); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags) |
| 1151 | { |
| 1152 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1153 | return BAD_VALUE; |
| 1154 | |
| 1155 | Mutex::Autolock _l(mStateLock); |
| 1156 | mCurrentState.freezeDisplay = 1; |
| 1157 | setTransactionFlags(eTransactionNeeded); |
| 1158 | |
| 1159 | // flags is intended to communicate some sort of animation behavior |
Mathias Agopian | 62b7444 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 1160 | // (for instance fading) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | return NO_ERROR; |
| 1162 | } |
| 1163 | |
| 1164 | status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags) |
| 1165 | { |
| 1166 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1167 | return BAD_VALUE; |
| 1168 | |
| 1169 | Mutex::Autolock _l(mStateLock); |
| 1170 | mCurrentState.freezeDisplay = 0; |
| 1171 | setTransactionFlags(eTransactionNeeded); |
| 1172 | |
| 1173 | // flags is intended to communicate some sort of animation behavior |
Mathias Agopian | 62b7444 | 2009-04-14 23:02:51 -0700 | [diff] [blame] | 1174 | // (for instance fading) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | return NO_ERROR; |
| 1176 | } |
| 1177 | |
Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 1178 | int SurfaceFlinger::setOrientation(DisplayID dpy, |
| 1179 | int orientation, uint32_t flags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | { |
| 1181 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
| 1182 | return BAD_VALUE; |
| 1183 | |
| 1184 | Mutex::Autolock _l(mStateLock); |
| 1185 | if (mCurrentState.orientation != orientation) { |
| 1186 | if (uint32_t(orientation)<=eOrientation270 || orientation==42) { |
Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 1187 | mCurrentState.orientationType = flags; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | mCurrentState.orientation = orientation; |
| 1189 | setTransactionFlags(eTransactionNeeded); |
| 1190 | mTransactionCV.wait(mStateLock); |
| 1191 | } else { |
| 1192 | orientation = BAD_VALUE; |
| 1193 | } |
| 1194 | } |
| 1195 | return orientation; |
| 1196 | } |
| 1197 | |
| 1198 | sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid, |
| 1199 | ISurfaceFlingerClient::surface_data_t* params, |
| 1200 | DisplayID d, uint32_t w, uint32_t h, PixelFormat format, |
| 1201 | uint32_t flags) |
| 1202 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1203 | sp<LayerBaseClient> layer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | sp<LayerBaseClient::Surface> surfaceHandle; |
| 1205 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1206 | sp<Client> client = mClientsMap.valueFor(clientId); |
| 1207 | if (UNLIKELY(client == 0)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | LOGE("createSurface() failed, client not found (id=%d)", clientId); |
| 1209 | return surfaceHandle; |
| 1210 | } |
| 1211 | |
| 1212 | //LOGD("createSurface for pid %d (%d x %d)", pid, w, h); |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1213 | int32_t id = client->generateId(pid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | if (uint32_t(id) >= NUM_LAYERS_MAX) { |
| 1215 | LOGE("createSurface() failed, generateId = %d", id); |
| 1216 | return surfaceHandle; |
| 1217 | } |
| 1218 | |
| 1219 | switch (flags & eFXSurfaceMask) { |
| 1220 | case eFXSurfaceNormal: |
| 1221 | if (UNLIKELY(flags & ePushBuffers)) { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1222 | layer = createPushBuffersSurfaceLocked(client, d, id, w, h, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1223 | } else { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1224 | layer = createNormalSurfaceLocked(client, d, id, w, h, format, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | } |
| 1226 | break; |
| 1227 | case eFXSurfaceBlur: |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1228 | layer = createBlurSurfaceLocked(client, d, id, w, h, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | break; |
| 1230 | case eFXSurfaceDim: |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1231 | layer = createDimSurfaceLocked(client, d, id, w, h, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | break; |
| 1233 | } |
| 1234 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1235 | if (layer != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | setTransactionFlags(eTransactionNeeded); |
| 1237 | surfaceHandle = layer->getSurface(); |
| 1238 | if (surfaceHandle != 0) |
| 1239 | surfaceHandle->getSurfaceData(params); |
| 1240 | } |
| 1241 | |
| 1242 | return surfaceHandle; |
| 1243 | } |
| 1244 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1245 | sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked( |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1246 | const sp<Client>& client, DisplayID display, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) |
| 1248 | { |
| 1249 | // initialize the surfaces |
| 1250 | switch (format) { // TODO: take h/w into account |
| 1251 | case PIXEL_FORMAT_TRANSPARENT: |
| 1252 | case PIXEL_FORMAT_TRANSLUCENT: |
| 1253 | format = PIXEL_FORMAT_RGBA_8888; |
| 1254 | break; |
| 1255 | case PIXEL_FORMAT_OPAQUE: |
| 1256 | format = PIXEL_FORMAT_RGB_565; |
| 1257 | break; |
| 1258 | } |
| 1259 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1260 | sp<Layer> layer = new Layer(this, display, client, id); |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1261 | status_t err = layer->setBuffers(w, h, format, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | if (LIKELY(err == NO_ERROR)) { |
| 1263 | layer->initStates(w, h, flags); |
| 1264 | addLayer_l(layer); |
| 1265 | } else { |
| 1266 | LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1267 | layer.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1268 | } |
| 1269 | return layer; |
| 1270 | } |
| 1271 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1272 | sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked( |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1273 | const sp<Client>& client, DisplayID display, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | int32_t id, uint32_t w, uint32_t h, uint32_t flags) |
| 1275 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1276 | sp<LayerBlur> layer = new LayerBlur(this, display, client, id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | layer->initStates(w, h, flags); |
| 1278 | addLayer_l(layer); |
| 1279 | return layer; |
| 1280 | } |
| 1281 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1282 | sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked( |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1283 | const sp<Client>& client, DisplayID display, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | int32_t id, uint32_t w, uint32_t h, uint32_t flags) |
| 1285 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1286 | sp<LayerDim> layer = new LayerDim(this, display, client, id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | layer->initStates(w, h, flags); |
| 1288 | addLayer_l(layer); |
| 1289 | return layer; |
| 1290 | } |
| 1291 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1292 | sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked( |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1293 | const sp<Client>& client, DisplayID display, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | int32_t id, uint32_t w, uint32_t h, uint32_t flags) |
| 1295 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1296 | sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1297 | layer->initStates(w, h, flags); |
| 1298 | addLayer_l(layer); |
| 1299 | return layer; |
| 1300 | } |
| 1301 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1302 | status_t SurfaceFlinger::removeSurface(SurfaceID index) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | { |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1304 | /* |
| 1305 | * called by the window manager, when a surface should be marked for |
| 1306 | * destruction. |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1307 | * |
| 1308 | * The surface is removed from the current and drawing lists, but placed |
| 1309 | * in the purgatory queue, so it's not destroyed right-away (we need |
| 1310 | * to wait for all client's references to go away first). |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1311 | */ |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1312 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1313 | Mutex::Autolock _l(mStateLock); |
| 1314 | sp<LayerBaseClient> layer = getLayerUser_l(index); |
| 1315 | status_t err = purgatorizeLayer_l(layer); |
| 1316 | if (err == NO_ERROR) { |
| 1317 | setTransactionFlags(eTransactionNeeded); |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1318 | } |
| 1319 | return err; |
| 1320 | } |
| 1321 | |
| 1322 | status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer) |
| 1323 | { |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1324 | /* called by ~ISurface() when all references are gone */ |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1325 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1326 | class MessageDestroySurface : public MessageBase { |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1327 | SurfaceFlinger* flinger; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1328 | sp<LayerBaseClient> layer; |
| 1329 | public: |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1330 | MessageDestroySurface( |
| 1331 | SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer) |
| 1332 | : flinger(flinger), layer(layer) { } |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1333 | virtual bool handler() { |
Mathias Agopian | cd8c5e2 | 2009-06-19 16:24:02 -0700 | [diff] [blame] | 1334 | sp<LayerBaseClient> l(layer); |
| 1335 | layer.clear(); // clear it outside of the lock; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1336 | Mutex::Autolock _l(flinger->mStateLock); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1337 | // remove the layer from the current list -- chances are that it's |
| 1338 | // not in the list anyway, because it should have been removed |
| 1339 | // already upon request of the client (eg: window manager). |
| 1340 | // However, a buggy client could have not done that. |
| 1341 | // Since we know we don't have any more clients, we don't need |
| 1342 | // to use the purgatory. |
Mathias Agopian | cd8c5e2 | 2009-06-19 16:24:02 -0700 | [diff] [blame] | 1343 | status_t err = flinger->removeLayer_l(l); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1344 | if (err == NAME_NOT_FOUND) { |
| 1345 | // The surface wasn't in the current list, which means it was |
| 1346 | // removed already, which means it is in the purgatory, |
| 1347 | // and need to be removed from there. |
| 1348 | // This needs to happen from the main thread since its dtor |
| 1349 | // must run from there (b/c of OpenGL ES). Additionally, we |
| 1350 | // can't really acquire our internal lock from |
| 1351 | // destroySurface() -- see postMessage() below. |
Mathias Agopian | cd8c5e2 | 2009-06-19 16:24:02 -0700 | [diff] [blame] | 1352 | ssize_t idx = flinger->mLayerPurgatory.remove(l); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1353 | LOGE_IF(idx < 0, |
Mathias Agopian | cd8c5e2 | 2009-06-19 16:24:02 -0700 | [diff] [blame] | 1354 | "layer=%p is not in the purgatory list", l.get()); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1355 | } |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1356 | return true; |
| 1357 | } |
| 1358 | }; |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1359 | |
| 1360 | // It's better to not acquire our internal lock here, because it's hard |
| 1361 | // to predict that it's not going to be already taken when ~Surface() |
| 1362 | // is called. |
| 1363 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1364 | mEventQueue.postMessage( new MessageDestroySurface(this, layer) ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | return NO_ERROR; |
| 1366 | } |
| 1367 | |
| 1368 | status_t SurfaceFlinger::setClientState( |
| 1369 | ClientID cid, |
| 1370 | int32_t count, |
| 1371 | const layer_state_t* states) |
| 1372 | { |
| 1373 | Mutex::Autolock _l(mStateLock); |
| 1374 | uint32_t flags = 0; |
| 1375 | cid <<= 16; |
| 1376 | for (int i=0 ; i<count ; i++) { |
| 1377 | const layer_state_t& s = states[i]; |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1378 | sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1379 | if (layer != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | const uint32_t what = s.what; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | if (what & ePositionChanged) { |
| 1382 | if (layer->setPosition(s.x, s.y)) |
| 1383 | flags |= eTraversalNeeded; |
| 1384 | } |
| 1385 | if (what & eLayerChanged) { |
| 1386 | if (layer->setLayer(s.z)) { |
| 1387 | mCurrentState.layersSortedByZ.reorder( |
| 1388 | layer, &Layer::compareCurrentStateZ); |
| 1389 | // we need traversal (state changed) |
| 1390 | // AND transaction (list changed) |
| 1391 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 1392 | } |
| 1393 | } |
| 1394 | if (what & eSizeChanged) { |
| 1395 | if (layer->setSize(s.w, s.h)) |
| 1396 | flags |= eTraversalNeeded; |
| 1397 | } |
| 1398 | if (what & eAlphaChanged) { |
| 1399 | if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f))) |
| 1400 | flags |= eTraversalNeeded; |
| 1401 | } |
| 1402 | if (what & eMatrixChanged) { |
| 1403 | if (layer->setMatrix(s.matrix)) |
| 1404 | flags |= eTraversalNeeded; |
| 1405 | } |
| 1406 | if (what & eTransparentRegionChanged) { |
| 1407 | if (layer->setTransparentRegionHint(s.transparentRegion)) |
| 1408 | flags |= eTraversalNeeded; |
| 1409 | } |
| 1410 | if (what & eVisibilityChanged) { |
| 1411 | if (layer->setFlags(s.flags, s.mask)) |
| 1412 | flags |= eTraversalNeeded; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | if (flags) { |
| 1417 | setTransactionFlags(flags); |
| 1418 | } |
| 1419 | return NO_ERROR; |
| 1420 | } |
| 1421 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1422 | sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1424 | sp<LayerBaseClient> layer = mLayerMap.valueFor(s); |
| 1425 | return layer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | void SurfaceFlinger::screenReleased(int dpy) |
| 1429 | { |
| 1430 | // this may be called by a signal handler, we can't do too much in here |
| 1431 | android_atomic_or(eConsoleReleased, &mConsoleSignals); |
| 1432 | signalEvent(); |
| 1433 | } |
| 1434 | |
| 1435 | void SurfaceFlinger::screenAcquired(int dpy) |
| 1436 | { |
| 1437 | // this may be called by a signal handler, we can't do too much in here |
| 1438 | android_atomic_or(eConsoleAcquired, &mConsoleSignals); |
| 1439 | signalEvent(); |
| 1440 | } |
| 1441 | |
| 1442 | status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args) |
| 1443 | { |
| 1444 | const size_t SIZE = 1024; |
| 1445 | char buffer[SIZE]; |
| 1446 | String8 result; |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1447 | if (!mDump.checkCalling()) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1449 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", |
| 1450 | IPCThreadState::self()->getCallingPid(), |
| 1451 | IPCThreadState::self()->getCallingUid()); |
| 1452 | result.append(buffer); |
| 1453 | } else { |
| 1454 | Mutex::Autolock _l(mStateLock); |
| 1455 | size_t s = mClientsMap.size(); |
| 1456 | char name[64]; |
| 1457 | for (size_t i=0 ; i<s ; i++) { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1458 | sp<Client> client = mClientsMap.valueAt(i); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | sprintf(name, " Client (id=0x%08x)", client->cid); |
| 1460 | client->dump(name); |
| 1461 | } |
| 1462 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 1463 | const size_t count = currentLayers.size(); |
| 1464 | for (size_t i=0 ; i<count ; i++) { |
| 1465 | /*** LayerBase ***/ |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1466 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | const Layer::State& s = layer->drawingState(); |
| 1468 | snprintf(buffer, SIZE, |
| 1469 | "+ %s %p\n" |
| 1470 | " " |
| 1471 | "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), " |
| 1472 | "needsBlending=%1d, invalidate=%1d, " |
| 1473 | "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n", |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1474 | layer->getTypeID(), layer.get(), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | s.z, layer->tx(), layer->ty(), s.w, s.h, |
| 1476 | layer->needsBlending(), layer->contentDirty, |
| 1477 | s.alpha, s.flags, |
| 1478 | s.transform[0], s.transform[1], |
| 1479 | s.transform[2], s.transform[3]); |
| 1480 | result.append(buffer); |
| 1481 | buffer[0] = 0; |
| 1482 | /*** LayerBaseClient ***/ |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1483 | sp<LayerBaseClient> lbc = |
| 1484 | LayerBase::dynamicCast< LayerBaseClient* >(layer.get()); |
| 1485 | if (lbc != 0) { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1486 | sp<Client> client(lbc->client.promote()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | snprintf(buffer, SIZE, |
| 1488 | " " |
| 1489 | "id=0x%08x, client=0x%08x, identity=%u\n", |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1490 | lbc->clientIndex(), client.get() ? client->cid : 0, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | lbc->getIdentity()); |
| 1492 | } |
| 1493 | result.append(buffer); |
| 1494 | buffer[0] = 0; |
| 1495 | /*** Layer ***/ |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1496 | sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get()); |
| 1497 | if (l != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | const LayerBitmap& buf0(l->getBuffer(0)); |
| 1499 | const LayerBitmap& buf1(l->getBuffer(1)); |
| 1500 | snprintf(buffer, SIZE, |
| 1501 | " " |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1502 | "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u]," |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | " freezeLock=%p, swapState=0x%08x\n", |
| 1504 | l->pixelFormat(), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1505 | buf0.getWidth(), buf0.getHeight(), |
| 1506 | buf0.getBuffer()->getStride(), |
| 1507 | buf1.getWidth(), buf1.getHeight(), |
| 1508 | buf1.getBuffer()->getStride(), |
| 1509 | l->getFreezeLock().get(), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | l->lcblk->swapState); |
| 1511 | } |
| 1512 | result.append(buffer); |
| 1513 | buffer[0] = 0; |
| 1514 | s.transparentRegion.dump(result, "transparentRegion"); |
| 1515 | layer->transparentRegionScreen.dump(result, "transparentRegionScreen"); |
| 1516 | layer->visibleRegionScreen.dump(result, "visibleRegionScreen"); |
| 1517 | } |
| 1518 | mWormholeRegion.dump(result, "WormholeRegion"); |
| 1519 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1520 | snprintf(buffer, SIZE, |
| 1521 | " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n", |
| 1522 | mFreezeDisplay?"yes":"no", mFreezeCount, |
| 1523 | mCurrentState.orientation, hw.canDraw()); |
| 1524 | result.append(buffer); |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1525 | snprintf(buffer, SIZE, " purgatory size: %d, client count: %d\n", |
| 1526 | mLayerPurgatory.size(), mClientsMap.size()); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1527 | result.append(buffer); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1528 | const BufferAllocator& alloc(BufferAllocator::get()); |
| 1529 | alloc.dump(result); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | } |
| 1531 | write(fd, result.string(), result.size()); |
| 1532 | return NO_ERROR; |
| 1533 | } |
| 1534 | |
| 1535 | status_t SurfaceFlinger::onTransact( |
| 1536 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1537 | { |
| 1538 | switch (code) { |
| 1539 | case CREATE_CONNECTION: |
| 1540 | case OPEN_GLOBAL_TRANSACTION: |
| 1541 | case CLOSE_GLOBAL_TRANSACTION: |
| 1542 | case SET_ORIENTATION: |
| 1543 | case FREEZE_DISPLAY: |
| 1544 | case UNFREEZE_DISPLAY: |
| 1545 | case BOOT_FINISHED: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1546 | { |
| 1547 | // codes that require permission check |
| 1548 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1549 | const int pid = ipc->getCallingPid(); |
Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 1550 | const int uid = ipc->getCallingUid(); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1551 | if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) { |
| 1552 | LOGE("Permission Denial: " |
| 1553 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
| 1554 | return PERMISSION_DENIED; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1558 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); |
| 1559 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame^] | 1560 | CHECK_INTERFACE(ISurfaceComposer, data, reply); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1561 | if (UNLIKELY(!mHardwareTest.checkCalling())) { |
| 1562 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1563 | const int pid = ipc->getCallingPid(); |
| 1564 | const int uid = ipc->getCallingUid(); |
| 1565 | LOGE("Permission Denial: " |
| 1566 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | return PERMISSION_DENIED; |
| 1568 | } |
| 1569 | int n; |
| 1570 | switch (code) { |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1571 | case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | return NO_ERROR; |
Mathias Agopian | cbc93ca | 2009-04-21 18:28:33 -0700 | [diff] [blame] | 1573 | case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | return NO_ERROR; |
| 1575 | case 1002: // SHOW_UPDATES |
| 1576 | n = data.readInt32(); |
| 1577 | mDebugRegion = n ? n : (mDebugRegion ? 0 : 1); |
| 1578 | return NO_ERROR; |
| 1579 | case 1003: // SHOW_BACKGROUND |
| 1580 | n = data.readInt32(); |
| 1581 | mDebugBackground = n ? 1 : 0; |
| 1582 | return NO_ERROR; |
| 1583 | case 1004:{ // repaint everything |
| 1584 | Mutex::Autolock _l(mStateLock); |
| 1585 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1586 | mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe |
| 1587 | signalEvent(); |
| 1588 | } |
| 1589 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | case 1007: // set mFreezeCount |
| 1591 | mFreezeCount = data.readInt32(); |
| 1592 | return NO_ERROR; |
| 1593 | case 1010: // interrogate. |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1594 | reply->writeInt32(0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | reply->writeInt32(0); |
| 1596 | reply->writeInt32(mDebugRegion); |
| 1597 | reply->writeInt32(mDebugBackground); |
| 1598 | return NO_ERROR; |
| 1599 | case 1013: { |
| 1600 | Mutex::Autolock _l(mStateLock); |
| 1601 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 1602 | reply->writeInt32(hw.getPageFlipCount()); |
| 1603 | } |
| 1604 | return NO_ERROR; |
| 1605 | } |
| 1606 | } |
| 1607 | return err; |
| 1608 | } |
| 1609 | |
| 1610 | // --------------------------------------------------------------------------- |
| 1611 | #if 0 |
| 1612 | #pragma mark - |
| 1613 | #endif |
| 1614 | |
| 1615 | Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger) |
| 1616 | : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger) |
| 1617 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | const int pgsize = getpagesize(); |
| 1619 | const int cblksize=((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1)); |
| 1620 | mCblkHeap = new MemoryDealer(cblksize); |
| 1621 | mCblkMemory = mCblkHeap->allocate(cblksize); |
| 1622 | if (mCblkMemory != 0) { |
| 1623 | ctrlblk = static_cast<per_client_cblk_t *>(mCblkMemory->pointer()); |
| 1624 | if (ctrlblk) { // construct the shared structure in-place. |
| 1625 | new(ctrlblk) per_client_cblk_t; |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | Client::~Client() { |
| 1631 | if (ctrlblk) { |
| 1632 | const int pgsize = getpagesize(); |
| 1633 | ctrlblk->~per_client_cblk_t(); // destroy our shared-structure. |
| 1634 | } |
| 1635 | } |
| 1636 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | int32_t Client::generateId(int pid) |
| 1638 | { |
| 1639 | const uint32_t i = clz( ~mBitmap ); |
| 1640 | if (i >= NUM_LAYERS_MAX) { |
| 1641 | return NO_MEMORY; |
| 1642 | } |
| 1643 | mPid = pid; |
| 1644 | mInUse.add(uint8_t(i)); |
| 1645 | mBitmap |= 1<<(31-i); |
| 1646 | return i; |
| 1647 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1648 | |
| 1649 | status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | { |
| 1651 | ssize_t idx = mInUse.indexOf(id); |
| 1652 | if (idx < 0) |
| 1653 | return NAME_NOT_FOUND; |
| 1654 | return mLayers.insertAt(layer, idx); |
| 1655 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1656 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | void Client::free(int32_t id) |
| 1658 | { |
| 1659 | ssize_t idx = mInUse.remove(uint8_t(id)); |
| 1660 | if (idx >= 0) { |
| 1661 | mBitmap &= ~(1<<(31-id)); |
| 1662 | mLayers.removeItemsAt(idx); |
| 1663 | } |
| 1664 | } |
| 1665 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | bool Client::isValid(int32_t i) const { |
| 1667 | return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i))); |
| 1668 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1669 | |
| 1670 | sp<LayerBaseClient> Client::getLayerUser(int32_t i) const { |
| 1671 | sp<LayerBaseClient> lbc; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | ssize_t idx = mInUse.indexOf(uint8_t(i)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1673 | if (idx >= 0) { |
| 1674 | lbc = mLayers[idx].promote(); |
| 1675 | LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx)); |
| 1676 | } |
| 1677 | return lbc; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | void Client::dump(const char* what) |
| 1681 | { |
| 1682 | } |
| 1683 | |
| 1684 | // --------------------------------------------------------------------------- |
| 1685 | #if 0 |
| 1686 | #pragma mark - |
| 1687 | #endif |
| 1688 | |
| 1689 | BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemory>& cblk) |
| 1690 | : mId(cid), mFlinger(flinger), mCblk(cblk) |
| 1691 | { |
| 1692 | } |
| 1693 | |
| 1694 | BClient::~BClient() { |
| 1695 | // destroy all resources attached to this client |
| 1696 | mFlinger->destroyConnection(mId); |
| 1697 | } |
| 1698 | |
| 1699 | void BClient::getControlBlocks(sp<IMemory>* ctrl) const { |
| 1700 | *ctrl = mCblk; |
| 1701 | } |
| 1702 | |
| 1703 | sp<ISurface> BClient::createSurface( |
| 1704 | ISurfaceFlingerClient::surface_data_t* params, int pid, |
| 1705 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, |
| 1706 | uint32_t flags) |
| 1707 | { |
| 1708 | return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags); |
| 1709 | } |
| 1710 | |
| 1711 | status_t BClient::destroySurface(SurfaceID sid) |
| 1712 | { |
| 1713 | sid |= (mId << 16); // add the client-part to id |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1714 | return mFlinger->removeSurface(sid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | status_t BClient::setState(int32_t count, const layer_state_t* states) |
| 1718 | { |
| 1719 | return mFlinger->setClientState(mId, count, states); |
| 1720 | } |
| 1721 | |
| 1722 | // --------------------------------------------------------------------------- |
| 1723 | |
| 1724 | GraphicPlane::GraphicPlane() |
| 1725 | : mHw(0) |
| 1726 | { |
| 1727 | } |
| 1728 | |
| 1729 | GraphicPlane::~GraphicPlane() { |
| 1730 | delete mHw; |
| 1731 | } |
| 1732 | |
| 1733 | bool GraphicPlane::initialized() const { |
| 1734 | return mHw ? true : false; |
| 1735 | } |
| 1736 | |
| 1737 | void GraphicPlane::setDisplayHardware(DisplayHardware *hw) { |
| 1738 | mHw = hw; |
| 1739 | } |
| 1740 | |
| 1741 | void GraphicPlane::setTransform(const Transform& tr) { |
| 1742 | mTransform = tr; |
| 1743 | mGlobalTransform = mOrientationTransform * mTransform; |
| 1744 | } |
| 1745 | |
| 1746 | status_t GraphicPlane::orientationToTransfrom( |
| 1747 | int orientation, int w, int h, Transform* tr) |
| 1748 | { |
| 1749 | float a, b, c, d, x, y; |
| 1750 | switch (orientation) { |
| 1751 | case ISurfaceComposer::eOrientationDefault: |
| 1752 | a=1; b=0; c=0; d=1; x=0; y=0; |
| 1753 | break; |
| 1754 | case ISurfaceComposer::eOrientation90: |
| 1755 | a=0; b=-1; c=1; d=0; x=w; y=0; |
| 1756 | break; |
| 1757 | case ISurfaceComposer::eOrientation180: |
| 1758 | a=-1; b=0; c=0; d=-1; x=w; y=h; |
| 1759 | break; |
| 1760 | case ISurfaceComposer::eOrientation270: |
| 1761 | a=0; b=1; c=-1; d=0; x=0; y=h; |
| 1762 | break; |
| 1763 | default: |
| 1764 | return BAD_VALUE; |
| 1765 | } |
| 1766 | tr->set(a, b, c, d); |
| 1767 | tr->set(x, y); |
| 1768 | return NO_ERROR; |
| 1769 | } |
| 1770 | |
| 1771 | status_t GraphicPlane::setOrientation(int orientation) |
| 1772 | { |
| 1773 | const DisplayHardware& hw(displayHardware()); |
| 1774 | const float w = hw.getWidth(); |
| 1775 | const float h = hw.getHeight(); |
| 1776 | |
| 1777 | if (orientation == ISurfaceComposer::eOrientationDefault) { |
| 1778 | // make sure the default orientation is optimal |
| 1779 | mOrientationTransform.reset(); |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 1780 | mOrientation = orientation; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | mGlobalTransform = mTransform; |
| 1782 | return NO_ERROR; |
| 1783 | } |
| 1784 | |
| 1785 | // If the rotation can be handled in hardware, this is where |
| 1786 | // the magic should happen. |
| 1787 | if (UNLIKELY(orientation == 42)) { |
| 1788 | float a, b, c, d, x, y; |
| 1789 | const float r = (3.14159265f / 180.0f) * 42.0f; |
| 1790 | const float si = sinf(r); |
| 1791 | const float co = cosf(r); |
| 1792 | a=co; b=-si; c=si; d=co; |
| 1793 | x = si*(h*0.5f) + (1-co)*(w*0.5f); |
| 1794 | y =-si*(w*0.5f) + (1-co)*(h*0.5f); |
| 1795 | mOrientationTransform.set(a, b, c, d); |
| 1796 | mOrientationTransform.set(x, y); |
| 1797 | } else { |
| 1798 | GraphicPlane::orientationToTransfrom(orientation, w, h, |
| 1799 | &mOrientationTransform); |
| 1800 | } |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 1801 | mOrientation = orientation; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | mGlobalTransform = mOrientationTransform * mTransform; |
| 1803 | return NO_ERROR; |
| 1804 | } |
| 1805 | |
| 1806 | const DisplayHardware& GraphicPlane::displayHardware() const { |
| 1807 | return *mHw; |
| 1808 | } |
| 1809 | |
| 1810 | const Transform& GraphicPlane::transform() const { |
| 1811 | return mGlobalTransform; |
| 1812 | } |
| 1813 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1814 | EGLDisplay GraphicPlane::getEGLDisplay() const { |
| 1815 | return mHw->getEGLDisplay(); |
| 1816 | } |
| 1817 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1818 | // --------------------------------------------------------------------------- |
| 1819 | |
| 1820 | }; // namespace android |