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/RefBase.h> 29 30 #include <binder/IMemory.h> 31 #include <binder/Permission.h> 32 33 #include <ui/PixelFormat.h> 34 #include <ui/ISurfaceComposer.h> 35 #include <ui/ISurfaceFlingerClient.h> 36 37 #include <private/ui/SharedBufferStack.h> 38 #include <private/ui/LayerState.h> 39 40 #include "Barrier.h" 41 #include "Layer.h" 42 #include "Tokenizer.h" 43 44 #include "MessageQueue.h" 45 46 struct copybit_device_t; 47 struct overlay_device_t; 48 49 namespace android { 50 51 // --------------------------------------------------------------------------- 52 53 class Client; 54 class BClient; 55 class DisplayHardware; 56 class FreezeLock; 57 class Layer; 58 class LayerBuffer; 59 60 typedef int32_t ClientID; 61 62 #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) 63 #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) 64 65 // --------------------------------------------------------------------------- 66 67 class Client : public RefBase 68 { 69 public: 70 Client(ClientID cid, const sp<SurfaceFlinger>& flinger); 71 ~Client(); 72 73 int32_t generateId(int pid); 74 void free(int32_t id); 75 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id); 76 77 inline bool isValid(int32_t i) const; 78 sp<LayerBaseClient> getLayerUser(int32_t i) const; 79 void dump(const char* what); 80 getLayers()81 const Vector< wp<LayerBaseClient> >& getLayers() const { 82 return mLayers; 83 } 84 getControlBlockMemory()85 const sp<IMemoryHeap>& getControlBlockMemory() const { 86 return mCblkHeap; 87 } 88 89 // pointer to this client's control block 90 SharedClient* ctrlblk; 91 ClientID cid; 92 93 94 private: getClientPid()95 int getClientPid() const { return mPid; } 96 97 int mPid; 98 uint32_t mBitmap; 99 SortedVector<uint8_t> mInUse; 100 Vector< wp<LayerBaseClient> > mLayers; 101 sp<IMemoryHeap> mCblkHeap; 102 sp<SurfaceFlinger> mFlinger; 103 }; 104 105 // --------------------------------------------------------------------------- 106 107 class GraphicPlane 108 { 109 public: 110 static status_t orientationToTransfrom(int orientation, int w, int h, 111 Transform* tr); 112 113 GraphicPlane(); 114 ~GraphicPlane(); 115 116 bool initialized() const; 117 118 void setDisplayHardware(DisplayHardware *); 119 void setTransform(const Transform& tr); 120 status_t setOrientation(int orientation); getOrientation()121 int getOrientation() const { return mOrientation; } 122 123 const DisplayHardware& displayHardware() const; 124 const Transform& transform() const; 125 EGLDisplay getEGLDisplay() const; 126 127 private: 128 GraphicPlane(const GraphicPlane&); 129 GraphicPlane operator = (const GraphicPlane&); 130 131 DisplayHardware* mHw; 132 Transform mTransform; 133 Transform mOrientationTransform; 134 Transform mGlobalTransform; 135 int mOrientation; 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<IMemoryHeap> 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, uint32_t flags); 169 virtual void signal() const; 170 171 void screenReleased(DisplayID dpy); 172 void screenAcquired(DisplayID dpy); 173 174 overlay_control_device_t* getOverlayEngine() const; 175 176 177 status_t removeLayer(const sp<LayerBase>& layer); 178 status_t addLayer(const sp<LayerBase>& layer); 179 status_t invalidateLayerVisibility(const sp<LayerBase>& layer); 180 181 private: 182 friend class BClient; 183 friend class LayerBase; 184 friend class LayerBuffer; 185 friend class LayerBaseClient; 186 friend class LayerBaseClient::Surface; 187 friend class Layer; 188 friend class LayerBlur; 189 friend class LayerDim; 190 191 sp<ISurface> createSurface(ClientID client, int pid, 192 ISurfaceFlingerClient::surface_data_t* params, 193 DisplayID display, uint32_t w, uint32_t h, PixelFormat format, 194 uint32_t flags); 195 196 sp<LayerBaseClient> createNormalSurfaceLocked( 197 const sp<Client>& client, DisplayID display, 198 int32_t id, uint32_t w, uint32_t h, uint32_t flags, 199 PixelFormat& format); 200 201 sp<LayerBaseClient> createBlurSurfaceLocked( 202 const sp<Client>& client, DisplayID display, 203 int32_t id, uint32_t w, uint32_t h, uint32_t flags); 204 205 sp<LayerBaseClient> createDimSurfaceLocked( 206 const sp<Client>& client, DisplayID display, 207 int32_t id, uint32_t w, uint32_t h, uint32_t flags); 208 209 sp<LayerBaseClient> createPushBuffersSurfaceLocked( 210 const sp<Client>& client, DisplayID display, 211 int32_t id, uint32_t w, uint32_t h, uint32_t flags); 212 213 status_t removeSurface(SurfaceID surface_id); 214 status_t destroySurface(const sp<LayerBaseClient>& layer); 215 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states); 216 217 218 class LayerVector { 219 public: LayerVector()220 inline LayerVector() { } 221 LayerVector(const LayerVector&); size()222 inline size_t size() const { return layers.size(); } array()223 inline sp<LayerBase> const* array() const { return layers.array(); } 224 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t); 225 ssize_t remove(const sp<LayerBase>&); 226 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t); 227 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const; 228 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; } 229 private: 230 KeyedVector< sp<LayerBase> , size_t> lookup; 231 Vector< sp<LayerBase> > layers; 232 }; 233 234 struct State { StateState235 State() { 236 orientation = ISurfaceComposer::eOrientationDefault; 237 freezeDisplay = 0; 238 } 239 LayerVector layersSortedByZ; 240 uint8_t orientation; 241 uint8_t orientationType; 242 uint8_t freezeDisplay; 243 }; 244 245 virtual bool threadLoop(); 246 virtual status_t readyToRun(); 247 virtual void onFirstRef(); 248 249 public: // hack to work around gcc 4.0.3 bug 250 const GraphicPlane& graphicPlane(int dpy) const; 251 GraphicPlane& graphicPlane(int dpy); 252 private: 253 254 void waitForEvent(); 255 public: // hack to work around gcc 4.0.3 bug 256 void signalEvent(); 257 private: 258 void signalDelayedEvent(nsecs_t delay); 259 260 void handleConsoleEvents(); 261 void handleTransaction(uint32_t transactionFlags); 262 void handleTransactionLocked( 263 uint32_t transactionFlags, 264 Vector< sp<LayerBase> >& ditchedLayers); 265 266 void computeVisibleRegions( 267 LayerVector& currentLayers, 268 Region& dirtyRegion, 269 Region& wormholeRegion); 270 271 void handlePageFlip(); 272 bool lockPageFlip(const LayerVector& currentLayers); 273 void unlockPageFlip(const LayerVector& currentLayers); 274 void handleRepaint(); 275 void postFramebuffer(); 276 void composeSurfaces(const Region& dirty); 277 void unlockClients(); 278 279 280 void destroyConnection(ClientID cid); 281 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const; 282 status_t addLayer_l(const sp<LayerBase>& layer); 283 status_t removeLayer_l(const sp<LayerBase>& layer); 284 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 285 void free_resources_l(); 286 287 uint32_t getTransactionFlags(uint32_t flags); 288 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0); 289 void commitTransaction(); 290 291 292 friend class FreezeLock; 293 sp<FreezeLock> getFreezeLock() const; incFreezeCount()294 inline void incFreezeCount() { mFreezeCount++; } decFreezeCount()295 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; } hasFreezeRequest()296 inline bool hasFreezeRequest() const { return mFreezeDisplay; } isFrozen()297 inline bool isFrozen() const { 298 return (mFreezeDisplay || mFreezeCount>0) && mBootFinished; 299 } 300 301 302 void debugFlashRegions(); 303 void debugShowFPS() const; 304 void drawWormhole() const; 305 306 307 mutable MessageQueue mEventQueue; 308 309 310 311 // access must be protected by mStateLock 312 mutable Mutex mStateLock; 313 State mCurrentState; 314 State mDrawingState; 315 volatile int32_t mTransactionFlags; 316 volatile int32_t mTransactionCount; 317 Condition mTransactionCV; 318 bool mResizeTransationPending; 319 320 // protected by mStateLock (but we could use another lock) 321 Tokenizer mTokens; 322 DefaultKeyedVector<ClientID, sp<Client> > mClientsMap; 323 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap; 324 GraphicPlane mGraphicPlanes[1]; 325 bool mLayersRemoved; 326 Vector< sp<Client> > mDisconnectedClients; 327 328 // constant members (no synchronization needed for access) 329 sp<IMemoryHeap> mServerHeap; 330 surface_flinger_cblk_t* mServerCblk; 331 GLuint mWormholeTexName; 332 nsecs_t mBootTime; 333 Permission mHardwareTest; 334 Permission mAccessSurfaceFlinger; 335 Permission mDump; 336 337 // Can only accessed from the main thread, these members 338 // don't need synchronization 339 Region mDirtyRegion; 340 Region mDirtyRegionRemovedLayer; 341 Region mInvalidRegion; 342 Region mWormholeRegion; 343 bool mVisibleRegionsDirty; 344 bool mDeferReleaseConsole; 345 bool mFreezeDisplay; 346 int32_t mFreezeCount; 347 nsecs_t mFreezeDisplayTime; 348 349 // don't use a lock for these, we don't care 350 int mDebugRegion; 351 int mDebugBackground; 352 volatile nsecs_t mDebugInSwapBuffers; 353 nsecs_t mLastSwapBufferTime; 354 volatile nsecs_t mDebugInTransaction; 355 nsecs_t mLastTransactionTime; 356 bool mBootFinished; 357 358 // these are thread safe 359 mutable Barrier mReadyToRunBarrier; 360 361 // atomic variables 362 enum { 363 eConsoleReleased = 1, 364 eConsoleAcquired = 2 365 }; 366 volatile int32_t mConsoleSignals; 367 368 // only written in the main thread, only read in other threads 369 volatile int32_t mSecureFrameBuffer; 370 }; 371 372 // --------------------------------------------------------------------------- 373 374 class FreezeLock : public LightRefBase<FreezeLock> { 375 SurfaceFlinger* mFlinger; 376 public: FreezeLock(SurfaceFlinger * flinger)377 FreezeLock(SurfaceFlinger* flinger) 378 : mFlinger(flinger) { 379 mFlinger->incFreezeCount(); 380 } ~FreezeLock()381 ~FreezeLock() { 382 mFlinger->decFreezeCount(); 383 } 384 }; 385 386 // --------------------------------------------------------------------------- 387 388 class BClient : public BnSurfaceFlingerClient 389 { 390 public: 391 BClient(SurfaceFlinger *flinger, ClientID cid, 392 const sp<IMemoryHeap>& cblk); 393 ~BClient(); 394 395 // ISurfaceFlingerClient interface 396 virtual sp<IMemoryHeap> getControlBlock() const; 397 398 virtual sp<ISurface> createSurface( 399 surface_data_t* params, int pid, 400 DisplayID display, uint32_t w, uint32_t h,PixelFormat format, 401 uint32_t flags); 402 403 virtual status_t destroySurface(SurfaceID surfaceId); 404 virtual status_t setState(int32_t count, const layer_state_t* states); 405 406 private: 407 ClientID mId; 408 SurfaceFlinger* mFlinger; 409 sp<IMemoryHeap> mCblk; 410 }; 411 412 // --------------------------------------------------------------------------- 413 }; // namespace android 414 415 #endif // ANDROID_SURFACE_FLINGER_H 416