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