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