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