SurfaceFlinger.h revision 134f0422866e8985188ed10dfbdcb8e6c34b87f7
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 ssize_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, int pid, 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 DefaultKeyedVector< size_t, wp<LayerBaseClient> > mLayers; 85 sp<SurfaceFlinger> mFlinger; 86 int32_t mNameGenerator; 87}; 88 89class UserClient : public BnSurfaceComposerClient 90{ 91public: 92 // pointer to this client's control block 93 SharedClient* ctrlblk; 94 95public: 96 UserClient(const sp<SurfaceFlinger>& flinger); 97 ~UserClient(); 98 99 status_t initCheck() const; 100 101 // protected by SurfaceFlinger::mStateLock 102 void detachLayer(const Layer* layer); 103 104private: 105 106 // ISurfaceComposerClient interface 107 virtual sp<IMemoryHeap> getControlBlock() const; 108 virtual ssize_t getTokenForSurface(const sp<ISurface>& sur) const; 109 virtual sp<ISurface> createSurface( 110 surface_data_t* params, int pid, const String8& name, 111 DisplayID display, uint32_t w, uint32_t h,PixelFormat format, 112 uint32_t flags); 113 virtual status_t destroySurface(SurfaceID surfaceId); 114 virtual status_t setState(int32_t count, const layer_state_t* states); 115 116 // atomic-ops 117 mutable volatile int32_t mBitmap; 118 119 sp<IMemoryHeap> mCblkHeap; 120 sp<SurfaceFlinger> mFlinger; 121}; 122 123class GraphicBufferAlloc : public BnGraphicBufferAlloc 124{ 125public: 126 GraphicBufferAlloc(); 127 virtual ~GraphicBufferAlloc(); 128 129 virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h, 130 PixelFormat format, uint32_t usage); 131 virtual void freeAllGraphicBuffersExcept(int bufIdx); 132 133private: 134 Vector<sp<GraphicBuffer> > mBuffers; 135 Mutex mLock; 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(const sp<Client>& client, 242 int pid, const String8& name, 243 ISurfaceComposerClient::surface_data_t* params, 244 DisplayID display, uint32_t w, uint32_t h, PixelFormat format, 245 uint32_t flags); 246 247 sp<Layer> createNormalSurface( 248 const sp<Client>& client, DisplayID display, 249 uint32_t w, uint32_t h, uint32_t flags, 250 PixelFormat& format); 251 252 sp<LayerDim> createDimSurface( 253 const sp<Client>& client, DisplayID display, 254 uint32_t w, uint32_t h, uint32_t flags); 255 256 status_t removeSurface(const sp<Client>& client, SurfaceID sid); 257 status_t destroySurface(const sp<LayerBaseClient>& layer); 258 status_t setClientState(const sp<Client>& client, 259 int32_t count, const layer_state_t* states); 260 261 class LayerVector : public SortedVector< sp<LayerBase> > { 262 public: 263 LayerVector() { } 264 LayerVector(const LayerVector& rhs) : SortedVector< sp<LayerBase> >(rhs) { } 265 virtual int do_compare(const void* lhs, const void* rhs) const { 266 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs)); 267 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs)); 268 // sort layers by Z order 269 uint32_t lz = l->currentState().z; 270 uint32_t rz = r->currentState().z; 271 // then by sequence, so we get a stable ordering 272 return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence); 273 } 274 }; 275 276 struct State { 277 State() { 278 orientation = ISurfaceComposer::eOrientationDefault; 279 freezeDisplay = 0; 280 } 281 LayerVector layersSortedByZ; 282 uint8_t orientation; 283 uint8_t orientationType; 284 uint8_t freezeDisplay; 285 }; 286 287 virtual bool threadLoop(); 288 virtual status_t readyToRun(); 289 virtual void onFirstRef(); 290 291public: // hack to work around gcc 4.0.3 bug 292 const GraphicPlane& graphicPlane(int dpy) const; 293 GraphicPlane& graphicPlane(int dpy); 294private: 295 296 void waitForEvent(); 297public: // hack to work around gcc 4.0.3 bug 298 void signalEvent(); 299private: 300 void handleConsoleEvents(); 301 void handleTransaction(uint32_t transactionFlags); 302 void handleTransactionLocked( 303 uint32_t transactionFlags, 304 Vector< sp<LayerBase> >& ditchedLayers); 305 306 void computeVisibleRegions( 307 LayerVector& currentLayers, 308 Region& dirtyRegion, 309 Region& wormholeRegion); 310 311 void handlePageFlip(); 312 bool lockPageFlip(const LayerVector& currentLayers); 313 void unlockPageFlip(const LayerVector& currentLayers); 314 void handleWorkList(); 315 void handleRepaint(); 316 void postFramebuffer(); 317 void composeSurfaces(const Region& dirty); 318 319 320 ssize_t addClientLayer(const sp<Client>& client, 321 const sp<LayerBaseClient>& lbc); 322 status_t addLayer_l(const sp<LayerBase>& layer); 323 status_t removeLayer_l(const sp<LayerBase>& layer); 324 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 325 326 uint32_t getTransactionFlags(uint32_t flags); 327 uint32_t setTransactionFlags(uint32_t flags); 328 void commitTransaction(); 329 330 331 status_t captureScreenImplLocked(DisplayID dpy, 332 sp<IMemoryHeap>* heap, 333 uint32_t* width, uint32_t* height, PixelFormat* format, 334 uint32_t reqWidth, uint32_t reqHeight, 335 uint32_t minLayerZ, uint32_t maxLayerZ); 336 337 status_t turnElectronBeamOffImplLocked(int32_t mode); 338 status_t turnElectronBeamOnImplLocked(int32_t mode); 339 status_t electronBeamOffAnimationImplLocked(); 340 status_t electronBeamOnAnimationImplLocked(); 341 status_t renderScreenToTextureLocked(DisplayID dpy, 342 GLuint* textureName, GLfloat* uOut, GLfloat* vOut); 343 344 friend class FreezeLock; 345 sp<FreezeLock> getFreezeLock() const; 346 inline void incFreezeCount() { 347 if (mFreezeCount == 0) 348 mFreezeDisplayTime = 0; 349 mFreezeCount++; 350 } 351 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; } 352 inline bool hasFreezeRequest() const { return mFreezeDisplay; } 353 inline bool isFrozen() const { 354 return (mFreezeDisplay || mFreezeCount>0) && mBootFinished; 355 } 356 357 358 void debugFlashRegions(); 359 void debugShowFPS() const; 360 void drawWormhole() const; 361 362 363 mutable MessageQueue mEventQueue; 364 365 status_t postMessageAsync(const sp<MessageBase>& msg, 366 nsecs_t reltime=0, uint32_t flags = 0); 367 368 status_t postMessageSync(const sp<MessageBase>& msg, 369 nsecs_t reltime=0, uint32_t flags = 0); 370 371 // access must be protected by mStateLock 372 mutable Mutex mStateLock; 373 State mCurrentState; 374 State mDrawingState; 375 volatile int32_t mTransactionFlags; 376 volatile int32_t mTransactionCount; 377 Condition mTransactionCV; 378 SortedVector< sp<LayerBase> > mLayerPurgatory; 379 bool mResizeTransationPending; 380 381 // protected by mStateLock (but we could use another lock) 382 GraphicPlane mGraphicPlanes[1]; 383 bool mLayersRemoved; 384 DefaultKeyedVector< wp<IBinder>, wp<Layer> > mLayerMap; 385 386 // constant members (no synchronization needed for access) 387 sp<IMemoryHeap> mServerHeap; 388 surface_flinger_cblk_t* mServerCblk; 389 GLuint mWormholeTexName; 390 nsecs_t mBootTime; 391 Permission mHardwareTest; 392 Permission mAccessSurfaceFlinger; 393 Permission mReadFramebuffer; 394 Permission mDump; 395 396 // Can only accessed from the main thread, these members 397 // don't need synchronization 398 Region mDirtyRegion; 399 Region mDirtyRegionRemovedLayer; 400 Region mInvalidRegion; 401 Region mWormholeRegion; 402 bool mVisibleRegionsDirty; 403 bool mHwWorkListDirty; 404 bool mDeferReleaseConsole; 405 bool mFreezeDisplay; 406 int32_t mElectronBeamAnimationMode; 407 int32_t mFreezeCount; 408 nsecs_t mFreezeDisplayTime; 409 Vector< sp<LayerBase> > mVisibleLayersSortedByZ; 410 411 412 // don't use a lock for these, we don't care 413 int mDebugRegion; 414 int mDebugBackground; 415 int mDebugDisableHWC; 416 volatile nsecs_t mDebugInSwapBuffers; 417 nsecs_t mLastSwapBufferTime; 418 volatile nsecs_t mDebugInTransaction; 419 nsecs_t mLastTransactionTime; 420 bool mBootFinished; 421 422 // these are thread safe 423 mutable Barrier mReadyToRunBarrier; 424 425 // atomic variables 426 enum { 427 eConsoleReleased = 1, 428 eConsoleAcquired = 2 429 }; 430 volatile int32_t mConsoleSignals; 431 432 // only written in the main thread, only read in other threads 433 volatile int32_t mSecureFrameBuffer; 434}; 435 436// --------------------------------------------------------------------------- 437 438class FreezeLock : public LightRefBase<FreezeLock> { 439 SurfaceFlinger* mFlinger; 440public: 441 FreezeLock(SurfaceFlinger* flinger) 442 : mFlinger(flinger) { 443 mFlinger->incFreezeCount(); 444 } 445 ~FreezeLock() { 446 mFlinger->decFreezeCount(); 447 } 448}; 449 450// --------------------------------------------------------------------------- 451}; // namespace android 452 453#endif // ANDROID_SURFACE_FLINGER_H 454