SurfaceFlinger.h revision dea20b1f343012d58ca9eb381684b26a168dc127
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#include <binder/BinderService.h> 33 34#include <ui/PixelFormat.h> 35#include <surfaceflinger/ISurfaceComposer.h> 36#include <surfaceflinger/ISurfaceComposerClient.h> 37#include <surfaceflinger/IGraphicBufferAlloc.h> 38 39#include "Barrier.h" 40#include "Layer.h" 41 42#include "MessageQueue.h" 43 44namespace android { 45 46// --------------------------------------------------------------------------- 47 48class Client; 49class DisplayHardware; 50class FreezeLock; 51class Layer; 52class LayerDim; 53 54#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) 55#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) 56 57// --------------------------------------------------------------------------- 58 59class Client : public BnSurfaceComposerClient 60{ 61public: 62 Client(const sp<SurfaceFlinger>& flinger); 63 ~Client(); 64 65 status_t initCheck() const; 66 67 // protected by SurfaceFlinger::mStateLock 68 size_t attachLayer(const sp<LayerBaseClient>& layer); 69 void detachLayer(const LayerBaseClient* layer); 70 sp<LayerBaseClient> getLayerUser(int32_t i) const; 71 72private: 73 74 // ISurfaceComposerClient interface 75 virtual sp<IMemoryHeap> getControlBlock() const; 76 virtual ssize_t getTokenForSurface(const sp<ISurface>& sur) const; 77 virtual sp<ISurface> createSurface( 78 surface_data_t* params, const String8& name, 79 DisplayID display, uint32_t w, uint32_t h,PixelFormat format, 80 uint32_t flags); 81 virtual status_t destroySurface(SurfaceID surfaceId); 82 virtual status_t setState(int32_t count, const layer_state_t* states); 83 84 // constant 85 sp<SurfaceFlinger> mFlinger; 86 87 // protected by mLock 88 DefaultKeyedVector< size_t, wp<LayerBaseClient> > mLayers; 89 size_t mNameGenerator; 90 91 // thread-safe 92 mutable Mutex mLock; 93}; 94 95class UserClient : public BnSurfaceComposerClient 96{ 97public: 98 // pointer to this client's control block 99 SharedClient* ctrlblk; 100 101public: 102 UserClient(const sp<SurfaceFlinger>& flinger); 103 ~UserClient(); 104 105 status_t initCheck() const; 106 107 // protected by SurfaceFlinger::mStateLock 108 void detachLayer(const Layer* layer); 109 110private: 111 112 // ISurfaceComposerClient interface 113 virtual sp<IMemoryHeap> getControlBlock() const; 114 virtual ssize_t getTokenForSurface(const sp<ISurface>& sur) const; 115 virtual sp<ISurface> createSurface( 116 surface_data_t* params, const String8& name, 117 DisplayID display, uint32_t w, uint32_t h,PixelFormat format, 118 uint32_t flags); 119 virtual status_t destroySurface(SurfaceID surfaceId); 120 virtual status_t setState(int32_t count, const layer_state_t* states); 121 122 // atomic-ops 123 mutable volatile int32_t mBitmap; 124 125 sp<IMemoryHeap> mCblkHeap; 126 sp<SurfaceFlinger> mFlinger; 127}; 128 129class GraphicBufferAlloc : public BnGraphicBufferAlloc 130{ 131public: 132 GraphicBufferAlloc(); 133 virtual ~GraphicBufferAlloc(); 134 virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h, 135 PixelFormat format, uint32_t usage); 136}; 137 138// --------------------------------------------------------------------------- 139 140class GraphicPlane 141{ 142public: 143 static status_t orientationToTransfrom(int orientation, int w, int h, 144 Transform* tr); 145 146 GraphicPlane(); 147 ~GraphicPlane(); 148 149 bool initialized() const; 150 151 void setDisplayHardware(DisplayHardware *); 152 status_t setOrientation(int orientation); 153 int getOrientation() const { return mOrientation; } 154 int getWidth() const; 155 int getHeight() const; 156 157 const DisplayHardware& displayHardware() const; 158 DisplayHardware& editDisplayHardware(); 159 const Transform& transform() const; 160 EGLDisplay getEGLDisplay() const; 161 162private: 163 GraphicPlane(const GraphicPlane&); 164 GraphicPlane operator = (const GraphicPlane&); 165 166 DisplayHardware* mHw; 167 Transform mGlobalTransform; 168 Transform mDisplayTransform; 169 int mOrientation; 170 float mDisplayWidth; 171 float mDisplayHeight; 172 int mWidth; 173 int mHeight; 174}; 175 176// --------------------------------------------------------------------------- 177 178enum { 179 eTransactionNeeded = 0x01, 180 eTraversalNeeded = 0x02 181}; 182 183class SurfaceFlinger : 184 public BinderService<SurfaceFlinger>, 185 public BnSurfaceComposer, 186 protected Thread 187{ 188public: 189 static char const* getServiceName() { return "SurfaceFlinger"; } 190 191 SurfaceFlinger(); 192 virtual ~SurfaceFlinger(); 193 void init(); 194 195 virtual status_t onTransact( 196 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); 197 198 virtual status_t dump(int fd, const Vector<String16>& args); 199 200 // ISurfaceComposer interface 201 virtual sp<ISurfaceComposerClient> createConnection(); 202 virtual sp<ISurfaceComposerClient> createClientConnection(); 203 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 204 virtual sp<IMemoryHeap> getCblk() const; 205 virtual void bootFinished(); 206 virtual void openGlobalTransaction(); 207 virtual void closeGlobalTransaction(); 208 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags); 209 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags); 210 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags); 211 virtual void signal() const; 212 virtual bool authenticateSurface(const sp<ISurface>& surface) const; 213 214 virtual status_t captureScreen(DisplayID dpy, 215 sp<IMemoryHeap>* heap, 216 uint32_t* width, uint32_t* height, 217 PixelFormat* format, uint32_t reqWidth, uint32_t reqHeight, 218 uint32_t minLayerZ, uint32_t maxLayerZ); 219 220 virtual status_t turnElectronBeamOff(int32_t mode); 221 virtual status_t turnElectronBeamOn(int32_t mode); 222 223 void screenReleased(DisplayID dpy); 224 void screenAcquired(DisplayID dpy); 225 226 status_t removeLayer(const sp<LayerBase>& layer); 227 status_t addLayer(const sp<LayerBase>& layer); 228 status_t invalidateLayerVisibility(const sp<LayerBase>& layer); 229 void invalidateHwcGeometry(); 230 231 sp<Layer> getLayer(const sp<ISurface>& sur) const; 232 233private: 234 friend class Client; 235 friend class LayerBase; 236 friend class LayerBaseClient; 237 friend class LayerBaseClient::Surface; 238 friend class Layer; 239 friend class LayerDim; 240 241 sp<ISurface> createSurface( 242 ISurfaceComposerClient::surface_data_t* params, 243 const String8& name, 244 const sp<Client>& client, 245 DisplayID display, uint32_t w, uint32_t h, PixelFormat format, 246 uint32_t flags); 247 248 sp<Layer> createNormalSurface( 249 const sp<Client>& client, DisplayID display, 250 uint32_t w, uint32_t h, uint32_t flags, 251 PixelFormat& format); 252 253 sp<LayerDim> createDimSurface( 254 const sp<Client>& client, DisplayID display, 255 uint32_t w, uint32_t h, uint32_t flags); 256 257 status_t removeSurface(const sp<Client>& client, SurfaceID sid); 258 status_t destroySurface(const sp<LayerBaseClient>& layer); 259 status_t setClientState(const sp<Client>& client, 260 int32_t count, const layer_state_t* states); 261 262 class LayerVector : public SortedVector< sp<LayerBase> > { 263 public: 264 LayerVector() { } 265 LayerVector(const LayerVector& rhs) : SortedVector< sp<LayerBase> >(rhs) { } 266 virtual int do_compare(const void* lhs, const void* rhs) const { 267 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs)); 268 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs)); 269 // sort layers by Z order 270 uint32_t lz = l->currentState().z; 271 uint32_t rz = r->currentState().z; 272 // then by sequence, so we get a stable ordering 273 return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence); 274 } 275 }; 276 277 struct State { 278 State() { 279 orientation = ISurfaceComposer::eOrientationDefault; 280 freezeDisplay = 0; 281 } 282 LayerVector layersSortedByZ; 283 uint8_t orientation; 284 uint8_t orientationType; 285 uint8_t freezeDisplay; 286 }; 287 288 virtual bool threadLoop(); 289 virtual status_t readyToRun(); 290 virtual void onFirstRef(); 291 292public: // hack to work around gcc 4.0.3 bug 293 const GraphicPlane& graphicPlane(int dpy) const; 294 GraphicPlane& graphicPlane(int dpy); 295private: 296 297 void waitForEvent(); 298public: // hack to work around gcc 4.0.3 bug 299 void signalEvent(); 300private: 301 void handleConsoleEvents(); 302 void handleTransaction(uint32_t transactionFlags); 303 void handleTransactionLocked( 304 uint32_t transactionFlags, 305 Vector< sp<LayerBase> >& ditchedLayers); 306 307 void computeVisibleRegions( 308 const LayerVector& currentLayers, 309 Region& dirtyRegion, 310 Region& wormholeRegion); 311 312 void handlePageFlip(); 313 bool lockPageFlip(const LayerVector& currentLayers); 314 void unlockPageFlip(const LayerVector& currentLayers); 315 void handleWorkList(); 316 void handleRepaint(); 317 void postFramebuffer(); 318 void composeSurfaces(const Region& dirty); 319 320 321 ssize_t addClientLayer(const sp<Client>& client, 322 const sp<LayerBaseClient>& lbc); 323 status_t addLayer_l(const sp<LayerBase>& layer); 324 status_t removeLayer_l(const sp<LayerBase>& layer); 325 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 326 327 uint32_t getTransactionFlags(uint32_t flags); 328 uint32_t peekTransactionFlags(uint32_t flags); 329 uint32_t setTransactionFlags(uint32_t flags); 330 void commitTransaction(); 331 332 333 status_t captureScreenImplLocked(DisplayID dpy, 334 sp<IMemoryHeap>* heap, 335 uint32_t* width, uint32_t* height, PixelFormat* format, 336 uint32_t reqWidth, uint32_t reqHeight, 337 uint32_t minLayerZ, uint32_t maxLayerZ); 338 339 status_t turnElectronBeamOffImplLocked(int32_t mode); 340 status_t turnElectronBeamOnImplLocked(int32_t mode); 341 status_t electronBeamOffAnimationImplLocked(); 342 status_t electronBeamOnAnimationImplLocked(); 343 status_t renderScreenToTextureLocked(DisplayID dpy, 344 GLuint* textureName, GLfloat* uOut, GLfloat* vOut); 345 346 friend class FreezeLock; 347 sp<FreezeLock> getFreezeLock() const; 348 inline void incFreezeCount() { 349 if (mFreezeCount == 0) 350 mFreezeDisplayTime = 0; 351 mFreezeCount++; 352 } 353 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; } 354 inline bool hasFreezeRequest() const { return mFreezeDisplay; } 355 inline bool isFrozen() const { 356 return (mFreezeDisplay || mFreezeCount>0) && mBootFinished; 357 } 358 359 360 void debugFlashRegions(); 361 void debugShowFPS() const; 362 void drawWormhole() const; 363 364 365 mutable MessageQueue mEventQueue; 366 367 status_t postMessageAsync(const sp<MessageBase>& msg, 368 nsecs_t reltime=0, uint32_t flags = 0); 369 370 status_t postMessageSync(const sp<MessageBase>& msg, 371 nsecs_t reltime=0, uint32_t flags = 0); 372 373 // access must be protected by mStateLock 374 mutable Mutex mStateLock; 375 State mCurrentState; 376 volatile int32_t mTransactionFlags; 377 volatile int32_t mTransactionCount; 378 Condition mTransactionCV; 379 SortedVector< sp<LayerBase> > mLayerPurgatory; 380 bool mResizeTransationPending; 381 382 // protected by mStateLock (but we could use another lock) 383 GraphicPlane mGraphicPlanes[1]; 384 bool mLayersRemoved; 385 DefaultKeyedVector< wp<IBinder>, wp<Layer> > mLayerMap; 386 387 // constant members (no synchronization needed for access) 388 sp<IMemoryHeap> mServerHeap; 389 surface_flinger_cblk_t* mServerCblk; 390 GLuint mWormholeTexName; 391 nsecs_t mBootTime; 392 Permission mHardwareTest; 393 Permission mAccessSurfaceFlinger; 394 Permission mReadFramebuffer; 395 Permission mDump; 396 397 // Can only accessed from the main thread, these members 398 // don't need synchronization 399 State mDrawingState; 400 Region mDirtyRegion; 401 Region mDirtyRegionRemovedLayer; 402 Region mInvalidRegion; 403 Region mWormholeRegion; 404 bool mVisibleRegionsDirty; 405 bool mHwWorkListDirty; 406 bool mDeferReleaseConsole; 407 bool mFreezeDisplay; 408 int32_t mElectronBeamAnimationMode; 409 int32_t mFreezeCount; 410 nsecs_t mFreezeDisplayTime; 411 Vector< sp<LayerBase> > mVisibleLayersSortedByZ; 412 413 414 // don't use a lock for these, we don't care 415 int mDebugRegion; 416 int mDebugBackground; 417 int mDebugDisableHWC; 418 volatile nsecs_t mDebugInSwapBuffers; 419 nsecs_t mLastSwapBufferTime; 420 volatile nsecs_t mDebugInTransaction; 421 nsecs_t mLastTransactionTime; 422 bool mBootFinished; 423 424 // these are thread safe 425 mutable Barrier mReadyToRunBarrier; 426 427 // atomic variables 428 enum { 429 eConsoleReleased = 1, 430 eConsoleAcquired = 2 431 }; 432 volatile int32_t mConsoleSignals; 433 434 // only written in the main thread, only read in other threads 435 volatile int32_t mSecureFrameBuffer; 436}; 437 438// --------------------------------------------------------------------------- 439 440class FreezeLock : public LightRefBase<FreezeLock> { 441 SurfaceFlinger* mFlinger; 442public: 443 FreezeLock(SurfaceFlinger* flinger) 444 : mFlinger(flinger) { 445 mFlinger->incFreezeCount(); 446 } 447 ~FreezeLock() { 448 mFlinger->decFreezeCount(); 449 } 450}; 451 452// --------------------------------------------------------------------------- 453}; // namespace android 454 455#endif // ANDROID_SURFACE_FLINGER_H 456