SurfaceFlinger.h revision 4f113740180b6512b43723c4728f262882dc9b45
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 setTransactionFlags(uint32_t flags); 329 void commitTransaction(); 330 331 332 status_t captureScreenImplLocked(DisplayID dpy, 333 sp<IMemoryHeap>* heap, 334 uint32_t* width, uint32_t* height, PixelFormat* format, 335 uint32_t reqWidth, uint32_t reqHeight, 336 uint32_t minLayerZ, uint32_t maxLayerZ); 337 338 status_t turnElectronBeamOffImplLocked(int32_t mode); 339 status_t turnElectronBeamOnImplLocked(int32_t mode); 340 status_t electronBeamOffAnimationImplLocked(); 341 status_t electronBeamOnAnimationImplLocked(); 342 status_t renderScreenToTextureLocked(DisplayID dpy, 343 GLuint* textureName, GLfloat* uOut, GLfloat* vOut); 344 345 friend class FreezeLock; 346 sp<FreezeLock> getFreezeLock() const; 347 inline void incFreezeCount() { 348 if (mFreezeCount == 0) 349 mFreezeDisplayTime = 0; 350 mFreezeCount++; 351 } 352 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; } 353 inline bool hasFreezeRequest() const { return mFreezeDisplay; } 354 inline bool isFrozen() const { 355 return (mFreezeDisplay || mFreezeCount>0) && mBootFinished; 356 } 357 358 359 void debugFlashRegions(); 360 void debugShowFPS() const; 361 void drawWormhole() const; 362 363 364 mutable MessageQueue mEventQueue; 365 366 status_t postMessageAsync(const sp<MessageBase>& msg, 367 nsecs_t reltime=0, uint32_t flags = 0); 368 369 status_t postMessageSync(const sp<MessageBase>& msg, 370 nsecs_t reltime=0, uint32_t flags = 0); 371 372 // access must be protected by mStateLock 373 mutable Mutex mStateLock; 374 State mCurrentState; 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 State mDrawingState; 399 Region mDirtyRegion; 400 Region mDirtyRegionRemovedLayer; 401 Region mInvalidRegion; 402 Region mWormholeRegion; 403 bool mVisibleRegionsDirty; 404 bool mHwWorkListDirty; 405 bool mDeferReleaseConsole; 406 bool mFreezeDisplay; 407 int32_t mElectronBeamAnimationMode; 408 int32_t mFreezeCount; 409 nsecs_t mFreezeDisplayTime; 410 Vector< sp<LayerBase> > mVisibleLayersSortedByZ; 411 412 413 // don't use a lock for these, we don't care 414 int mDebugRegion; 415 int mDebugBackground; 416 int mDebugDisableHWC; 417 volatile nsecs_t mDebugInSwapBuffers; 418 nsecs_t mLastSwapBufferTime; 419 volatile nsecs_t mDebugInTransaction; 420 nsecs_t mLastTransactionTime; 421 bool mBootFinished; 422 423 // these are thread safe 424 mutable Barrier mReadyToRunBarrier; 425 426 // atomic variables 427 enum { 428 eConsoleReleased = 1, 429 eConsoleAcquired = 2 430 }; 431 volatile int32_t mConsoleSignals; 432 433 // only written in the main thread, only read in other threads 434 volatile int32_t mSecureFrameBuffer; 435}; 436 437// --------------------------------------------------------------------------- 438 439class FreezeLock : public LightRefBase<FreezeLock> { 440 SurfaceFlinger* mFlinger; 441public: 442 FreezeLock(SurfaceFlinger* flinger) 443 : mFlinger(flinger) { 444 mFlinger->incFreezeCount(); 445 } 446 ~FreezeLock() { 447 mFlinger->decFreezeCount(); 448 } 449}; 450 451// --------------------------------------------------------------------------- 452}; // namespace android 453 454#endif // ANDROID_SURFACE_FLINGER_H 455