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