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