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