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