The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_SURFACE_FLINGER_H |
| 18 | #define ANDROID_SURFACE_FLINGER_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <utils/SortedVector.h> |
| 24 | #include <utils/KeyedVector.h> |
| 25 | #include <utils/threads.h> |
| 26 | #include <utils/Atomic.h> |
| 27 | #include <utils/Errors.h> |
| 28 | #include <utils/MemoryDealer.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 29 | #include <utils/RefBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | #include <ui/PixelFormat.h> |
| 32 | #include <ui/ISurfaceComposer.h> |
| 33 | #include <ui/ISurfaceFlingerClient.h> |
| 34 | |
| 35 | #include <private/ui/SharedState.h> |
| 36 | #include <private/ui/LayerState.h> |
| 37 | #include <private/ui/SurfaceFlingerSynchro.h> |
| 38 | |
| 39 | #include "Barrier.h" |
| 40 | #include "BootAnimation.h" |
| 41 | #include "CPUGauge.h" |
| 42 | #include "Layer.h" |
| 43 | #include "Tokenizer.h" |
| 44 | |
| 45 | struct copybit_device_t; |
| 46 | struct overlay_device_t; |
| 47 | |
| 48 | namespace android { |
| 49 | |
| 50 | // --------------------------------------------------------------------------- |
| 51 | |
| 52 | class Client; |
| 53 | class BClient; |
| 54 | class DisplayHardware; |
| 55 | class FreezeLock; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | class Layer; |
| 57 | class LayerBuffer; |
| 58 | class LayerOrientationAnim; |
| 59 | class OrientationAnimation; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| 61 | typedef int32_t ClientID; |
| 62 | |
| 63 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 64 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 65 | |
| 66 | // --------------------------------------------------------------------------- |
| 67 | |
| 68 | class Client |
| 69 | { |
| 70 | public: |
| 71 | Client(ClientID cid, const sp<SurfaceFlinger>& flinger); |
| 72 | ~Client(); |
| 73 | |
| 74 | int32_t generateId(int pid); |
| 75 | void free(int32_t id); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 76 | status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
| 78 | inline bool isValid(int32_t i) const; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 79 | sp<LayerBaseClient> getLayerUser(int32_t i) const; |
| 80 | const Vector< wp<LayerBaseClient> >& getLayers() const { return mLayers; } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; } |
| 82 | void dump(const char* what); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | |
| 84 | // pointer to this client's control block |
| 85 | per_client_cblk_t* ctrlblk; |
| 86 | ClientID cid; |
| 87 | |
| 88 | |
| 89 | private: |
| 90 | int getClientPid() const { return mPid; } |
| 91 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 92 | int mPid; |
| 93 | uint32_t mBitmap; |
| 94 | SortedVector<uint8_t> mInUse; |
| 95 | Vector< wp<LayerBaseClient> > mLayers; |
| 96 | sp<MemoryDealer> mCblkHeap; |
| 97 | sp<SurfaceFlinger> mFlinger; |
| 98 | sp<IMemory> mCblkMemory; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | // --------------------------------------------------------------------------- |
| 102 | |
| 103 | class GraphicPlane |
| 104 | { |
| 105 | public: |
| 106 | static status_t orientationToTransfrom(int orientation, int w, int h, |
| 107 | Transform* tr); |
| 108 | |
| 109 | GraphicPlane(); |
| 110 | ~GraphicPlane(); |
| 111 | |
| 112 | bool initialized() const; |
| 113 | |
| 114 | void setDisplayHardware(DisplayHardware *); |
| 115 | void setTransform(const Transform& tr); |
| 116 | status_t setOrientation(int orientation); |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 117 | int getOrientation() const { return mOrientation; } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | |
| 119 | const DisplayHardware& displayHardware() const; |
| 120 | const Transform& transform() const; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 121 | EGLDisplay getEGLDisplay() const; |
| 122 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | private: |
| 124 | GraphicPlane(const GraphicPlane&); |
| 125 | GraphicPlane operator = (const GraphicPlane&); |
| 126 | |
| 127 | DisplayHardware* mHw; |
| 128 | Transform mTransform; |
| 129 | Transform mOrientationTransform; |
| 130 | Transform mGlobalTransform; |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 131 | int mOrientation; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | // --------------------------------------------------------------------------- |
| 135 | |
| 136 | enum { |
| 137 | eTransactionNeeded = 0x01, |
| 138 | eTraversalNeeded = 0x02 |
| 139 | }; |
| 140 | |
| 141 | class SurfaceFlinger : public BnSurfaceComposer, protected Thread |
| 142 | { |
| 143 | public: |
| 144 | static void instantiate(); |
| 145 | static void shutdown(); |
| 146 | |
| 147 | SurfaceFlinger(); |
| 148 | virtual ~SurfaceFlinger(); |
| 149 | void init(); |
| 150 | |
| 151 | virtual status_t onTransact( |
| 152 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); |
| 153 | |
| 154 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 155 | |
| 156 | // ISurfaceComposer interface |
| 157 | virtual sp<ISurfaceFlingerClient> createConnection(); |
| 158 | virtual sp<IMemory> getCblk() const; |
| 159 | virtual void bootFinished(); |
| 160 | virtual void openGlobalTransaction(); |
| 161 | virtual void closeGlobalTransaction(); |
| 162 | virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags); |
| 163 | virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags); |
Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 164 | virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | virtual void signal() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | |
| 167 | void screenReleased(DisplayID dpy); |
| 168 | void screenAcquired(DisplayID dpy); |
| 169 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | overlay_control_device_t* getOverlayEngine() const; |
| 171 | |
| 172 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 173 | status_t removeLayer(const sp<LayerBase>& layer); |
| 174 | status_t addLayer(const sp<LayerBase>& layer); |
| 175 | status_t invalidateLayerVisibility(const sp<LayerBase>& layer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
| 177 | private: |
| 178 | friend class BClient; |
| 179 | friend class LayerBase; |
| 180 | friend class LayerBuffer; |
| 181 | friend class LayerBaseClient; |
| 182 | friend class Layer; |
| 183 | friend class LayerBlur; |
| 184 | |
| 185 | sp<ISurface> createSurface(ClientID client, int pid, |
| 186 | ISurfaceFlingerClient::surface_data_t* params, |
| 187 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, |
| 188 | uint32_t flags); |
| 189 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 190 | sp<LayerBaseClient> createNormalSurfaceLocked( |
| 191 | Client* client, DisplayID display, |
| 192 | int32_t id, uint32_t w, uint32_t h, |
| 193 | PixelFormat format, uint32_t flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 195 | sp<LayerBaseClient> createBlurSurfaceLocked( |
| 196 | Client* client, DisplayID display, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | int32_t id, uint32_t w, uint32_t h, uint32_t flags); |
| 198 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 199 | sp<LayerBaseClient> createDimSurfaceLocked( |
| 200 | Client* client, DisplayID display, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | int32_t id, uint32_t w, uint32_t h, uint32_t flags); |
| 202 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 203 | sp<LayerBaseClient> createPushBuffersSurfaceLocked( |
| 204 | Client* client, DisplayID display, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | int32_t id, uint32_t w, uint32_t h, uint32_t flags); |
| 206 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 207 | status_t destroySurface(SurfaceID surface_id); |
| 208 | status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | |
| 210 | |
| 211 | class LayerVector { |
| 212 | public: |
| 213 | inline LayerVector() { } |
| 214 | LayerVector(const LayerVector&); |
| 215 | inline size_t size() const { return layers.size(); } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 216 | inline sp<LayerBase> const* array() const { return layers.array(); } |
| 217 | ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t); |
| 218 | ssize_t remove(const sp<LayerBase>&); |
| 219 | ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t); |
| 220 | ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const; |
| 221 | inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | private: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 223 | KeyedVector< sp<LayerBase> , size_t> lookup; |
| 224 | Vector< sp<LayerBase> > layers; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | struct State { |
| 228 | State() { |
| 229 | orientation = ISurfaceComposer::eOrientationDefault; |
| 230 | freezeDisplay = 0; |
| 231 | } |
| 232 | LayerVector layersSortedByZ; |
| 233 | uint8_t orientation; |
Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 234 | uint8_t orientationType; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | uint8_t freezeDisplay; |
| 236 | }; |
| 237 | |
| 238 | class DelayedTransaction : public Thread |
| 239 | { |
| 240 | friend class SurfaceFlinger; |
| 241 | sp<SurfaceFlinger> mFlinger; |
| 242 | nsecs_t mDelay; |
| 243 | public: |
| 244 | DelayedTransaction(const sp<SurfaceFlinger>& flinger, nsecs_t delay) |
| 245 | : Thread(false), mFlinger(flinger), mDelay(delay) { |
| 246 | } |
| 247 | virtual bool threadLoop() { |
| 248 | usleep(mDelay / 1000); |
| 249 | if (android_atomic_and(~1, |
| 250 | &mFlinger->mDeplayedTransactionPending) == 1) { |
| 251 | mFlinger->signalEvent(); |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | virtual bool threadLoop(); |
| 258 | virtual status_t readyToRun(); |
| 259 | virtual void onFirstRef(); |
| 260 | |
| 261 | const GraphicPlane& graphicPlane(int dpy) const; |
| 262 | GraphicPlane& graphicPlane(int dpy); |
| 263 | |
| 264 | void waitForEvent(); |
| 265 | void signalEvent(); |
| 266 | void signalDelayedEvent(nsecs_t delay); |
| 267 | |
| 268 | void handleConsoleEvents(); |
| 269 | void handleTransaction(uint32_t transactionFlags); |
| 270 | |
| 271 | void computeVisibleRegions( |
| 272 | LayerVector& currentLayers, |
| 273 | Region& dirtyRegion, |
| 274 | Region& wormholeRegion); |
| 275 | |
| 276 | void handlePageFlip(); |
| 277 | bool lockPageFlip(const LayerVector& currentLayers); |
| 278 | void unlockPageFlip(const LayerVector& currentLayers); |
| 279 | void handleRepaint(); |
| 280 | void handleDebugCpu(); |
| 281 | void scheduleBroadcast(Client* client); |
| 282 | void executeScheduledBroadcasts(); |
| 283 | void postFramebuffer(); |
| 284 | void composeSurfaces(const Region& dirty); |
| 285 | void unlockClients(); |
| 286 | |
| 287 | |
| 288 | void destroyConnection(ClientID cid); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 289 | sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const; |
| 290 | status_t addLayer_l(const sp<LayerBase>& layer); |
| 291 | status_t removeLayer_l(const sp<LayerBase>& layer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | void free_resources_l(); |
| 293 | |
| 294 | uint32_t getTransactionFlags(uint32_t flags); |
| 295 | uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0); |
| 296 | void commitTransaction(); |
| 297 | |
| 298 | |
| 299 | friend class FreezeLock; |
| 300 | sp<FreezeLock> getFreezeLock() const; |
| 301 | inline void incFreezeCount() { mFreezeCount++; } |
| 302 | inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; } |
| 303 | inline bool hasFreezeRequest() const { return mFreezeDisplay; } |
| 304 | inline bool isFrozen() const { |
| 305 | return mFreezeDisplay || mFreezeCount>0; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | void debugFlashRegions(); |
| 310 | void debugShowFPS() const; |
| 311 | void drawWormhole() const; |
| 312 | |
| 313 | // access must be protected by mStateLock |
| 314 | mutable Mutex mStateLock; |
| 315 | State mCurrentState; |
| 316 | State mDrawingState; |
| 317 | volatile int32_t mTransactionFlags; |
| 318 | volatile int32_t mTransactionCount; |
| 319 | Condition mTransactionCV; |
| 320 | |
| 321 | // protected by mStateLock (but we could use another lock) |
| 322 | Tokenizer mTokens; |
| 323 | DefaultKeyedVector<ClientID, Client*> mClientsMap; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 324 | DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | GraphicPlane mGraphicPlanes[1]; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 326 | bool mLayersRemoved; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | Vector<Client*> mDisconnectedClients; |
| 328 | |
| 329 | // constant members (no synchronization needed for access) |
| 330 | sp<MemoryDealer> mServerHeap; |
| 331 | sp<IMemory> mServerCblkMemory; |
| 332 | surface_flinger_cblk_t* mServerCblk; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | GLuint mWormholeTexName; |
| 334 | sp<BootAnimation> mBootAnimation; |
| 335 | nsecs_t mBootTime; |
| 336 | |
| 337 | // Can only accessed from the main thread, these members |
| 338 | // don't need synchronization |
| 339 | Region mDirtyRegion; |
| 340 | Region mInvalidRegion; |
| 341 | Region mWormholeRegion; |
| 342 | Client* mLastScheduledBroadcast; |
| 343 | SortedVector<Client*> mScheduledBroadcasts; |
| 344 | bool mVisibleRegionsDirty; |
| 345 | bool mDeferReleaseConsole; |
| 346 | bool mFreezeDisplay; |
| 347 | int32_t mFreezeCount; |
| 348 | nsecs_t mFreezeDisplayTime; |
| 349 | friend class OrientationAnimation; |
| 350 | OrientationAnimation* mOrientationAnimation; |
| 351 | |
| 352 | // access protected by mDebugLock |
| 353 | mutable Mutex mDebugLock; |
| 354 | sp<CPUGauge> mCpuGauge; |
| 355 | |
| 356 | // don't use a lock for these, we don't care |
| 357 | int mDebugRegion; |
| 358 | int mDebugCpu; |
| 359 | int mDebugFps; |
| 360 | int mDebugBackground; |
| 361 | int mDebugNoBootAnimation; |
| 362 | |
| 363 | // these are thread safe |
| 364 | mutable Barrier mReadyToRunBarrier; |
| 365 | mutable SurfaceFlingerSynchro mSyncObject; |
| 366 | volatile int32_t mDeplayedTransactionPending; |
| 367 | |
| 368 | // atomic variables |
| 369 | enum { |
| 370 | eConsoleReleased = 1, |
| 371 | eConsoleAcquired = 2 |
| 372 | }; |
| 373 | volatile int32_t mConsoleSignals; |
| 374 | |
| 375 | // only written in the main thread, only read in other threads |
| 376 | volatile int32_t mSecureFrameBuffer; |
| 377 | }; |
| 378 | |
| 379 | // --------------------------------------------------------------------------- |
| 380 | |
| 381 | class FreezeLock : public LightRefBase<FreezeLock> { |
| 382 | SurfaceFlinger* mFlinger; |
| 383 | public: |
| 384 | FreezeLock(SurfaceFlinger* flinger) |
| 385 | : mFlinger(flinger) { |
| 386 | mFlinger->incFreezeCount(); |
| 387 | } |
| 388 | ~FreezeLock() { |
| 389 | mFlinger->decFreezeCount(); |
| 390 | } |
| 391 | }; |
| 392 | |
| 393 | // --------------------------------------------------------------------------- |
| 394 | |
| 395 | class BClient : public BnSurfaceFlingerClient |
| 396 | { |
| 397 | public: |
| 398 | BClient(SurfaceFlinger *flinger, ClientID cid, |
| 399 | const sp<IMemory>& cblk); |
| 400 | ~BClient(); |
| 401 | |
| 402 | // ISurfaceFlingerClient interface |
| 403 | virtual void getControlBlocks(sp<IMemory>* ctrl) const; |
| 404 | |
| 405 | virtual sp<ISurface> createSurface( |
| 406 | surface_data_t* params, int pid, |
| 407 | DisplayID display, uint32_t w, uint32_t h,PixelFormat format, |
| 408 | uint32_t flags); |
| 409 | |
| 410 | virtual status_t destroySurface(SurfaceID surfaceId); |
| 411 | virtual status_t setState(int32_t count, const layer_state_t* states); |
| 412 | |
| 413 | private: |
| 414 | ClientID mId; |
| 415 | SurfaceFlinger* mFlinger; |
| 416 | sp<IMemory> mCblk; |
| 417 | }; |
| 418 | |
| 419 | // --------------------------------------------------------------------------- |
| 420 | }; // namespace android |
| 421 | |
| 422 | #endif // ANDROID_SURFACE_FLINGER_H |