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