SurfaceFlinger.h revision 4d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6
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 <EGL/egl.h> 24#include <GLES/gl.h> 25 26#include <cutils/compiler.h> 27 28#include <utils/Atomic.h> 29#include <utils/Errors.h> 30#include <utils/KeyedVector.h> 31#include <utils/RefBase.h> 32#include <utils/SortedVector.h> 33#include <utils/threads.h> 34 35#include <binder/BinderService.h> 36#include <binder/IMemory.h> 37 38#include <ui/PixelFormat.h> 39 40#include <gui/ISurfaceComposer.h> 41#include <gui/ISurfaceComposerClient.h> 42 43#include <hardware/hwcomposer_defs.h> 44 45#include <private/gui/LayerState.h> 46 47#include "Barrier.h" 48#include "DisplayDevice.h" 49#include "FrameTracker.h" 50#include "MessageQueue.h" 51 52#include "DisplayHardware/HWComposer.h" 53 54namespace android { 55 56// --------------------------------------------------------------------------- 57 58class Client; 59class DisplayEventConnection; 60class EventThread; 61class IGraphicBufferAlloc; 62class Layer; 63class LayerDim; 64class Surface; 65 66// --------------------------------------------------------------------------- 67 68enum { 69 eTransactionNeeded = 0x01, 70 eTraversalNeeded = 0x02, 71 eDisplayTransactionNeeded = 0x04, 72 eTransactionMask = 0x07 73}; 74 75class SurfaceFlinger : public BinderService<SurfaceFlinger>, 76 public BnSurfaceComposer, 77 private IBinder::DeathRecipient, 78 private Thread, 79 private HWComposer::EventHandler 80{ 81public: 82 static char const* getServiceName() { 83 return "SurfaceFlinger"; 84 } 85 86 SurfaceFlinger(); 87 88 enum { 89 EVENT_VSYNC = HWC_EVENT_VSYNC 90 }; 91 92 // post an asynchronous message to the main thread 93 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, 94 uint32_t flags = 0); 95 96 // post a synchronous message to the main thread 97 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, 98 uint32_t flags = 0); 99 100 // force full composition on all displays 101 void repaintEverything(); 102 103 // returns the default Display 104 sp<const DisplayDevice> getDefaultDisplayDevice() const { 105 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]); 106 } 107 108 // utility function to delete a texture on the main thread 109 void deleteTextureAsync(GLuint texture); 110 111 // allocate a h/w composer display id 112 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type); 113 114 // enable/disable h/w composer event 115 // TODO: this should be made accessible only to EventThread 116 void eventControl(int disp, int event, int enabled); 117 118 // called on the main thread by MessageQueue when an internal message 119 // is received 120 // TODO: this should be made accessible only to MessageQueue 121 void onMessageReceived(int32_t what); 122 123 // for debugging only 124 // TODO: this should be made accessible only to HWComposer 125 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int disp); 126 127private: 128 friend class Client; 129 friend class DisplayEventConnection; 130 friend class Layer; 131 132 // We're reference counted, never destroy SurfaceFlinger directly 133 virtual ~SurfaceFlinger(); 134 135 /* ------------------------------------------------------------------------ 136 * Internal data structures 137 */ 138 139 class LayerVector : public SortedVector< sp<Layer> > { 140 public: 141 LayerVector(); 142 LayerVector(const LayerVector& rhs); 143 virtual int do_compare(const void* lhs, const void* rhs) const; 144 }; 145 146 struct DisplayDeviceState { 147 DisplayDeviceState(); 148 DisplayDeviceState(DisplayDevice::DisplayType type); 149 bool isValid() const { return type >= 0; } 150 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; } 151 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; } 152 DisplayDevice::DisplayType type; 153 sp<IGraphicBufferProducer> surface; 154 uint32_t layerStack; 155 Rect viewport; 156 Rect frame; 157 uint8_t orientation; 158 String8 displayName; 159 bool isSecure; 160 }; 161 162 struct State { 163 LayerVector layersSortedByZ; 164 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 165 }; 166 167 /* ------------------------------------------------------------------------ 168 * IBinder interface 169 */ 170 virtual status_t onTransact(uint32_t code, const Parcel& data, 171 Parcel* reply, uint32_t flags); 172 virtual status_t dump(int fd, const Vector<String16>& args); 173 174 /* ------------------------------------------------------------------------ 175 * ISurfaceComposer interface 176 */ 177 virtual sp<ISurfaceComposerClient> createConnection(); 178 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 179 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure); 180 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 181 virtual void setTransactionState(const Vector<ComposerState>& state, 182 const Vector<DisplayState>& displays, uint32_t flags); 183 virtual void bootFinished(); 184 virtual bool authenticateSurfaceTexture( 185 const sp<IGraphicBufferProducer>& bufferProducer) const; 186 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 187 virtual status_t captureScreen(const sp<IBinder>& display, sp<IMemoryHeap>* heap, 188 uint32_t* width, uint32_t* height, PixelFormat* format, 189 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 190 uint32_t maxLayerZ); 191 virtual status_t captureScreen(const sp<IBinder>& display, 192 const sp<IGraphicBufferProducer>& producer, 193 uint32_t reqWidth, uint32_t reqHeight, 194 uint32_t minLayerZ, uint32_t maxLayerZ); 195 // called when screen needs to turn off 196 virtual void blank(const sp<IBinder>& display); 197 // called when screen is turning back on 198 virtual void unblank(const sp<IBinder>& display); 199 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info); 200 201 /* ------------------------------------------------------------------------ 202 * DeathRecipient interface 203 */ 204 virtual void binderDied(const wp<IBinder>& who); 205 206 /* ------------------------------------------------------------------------ 207 * Thread interface 208 */ 209 virtual bool threadLoop(); 210 virtual status_t readyToRun(); 211 virtual void onFirstRef(); 212 213 /* ------------------------------------------------------------------------ 214 * HWComposer::EventHandler interface 215 */ 216 virtual void onVSyncReceived(int type, nsecs_t timestamp); 217 virtual void onHotplugReceived(int disp, bool connected); 218 219 /* ------------------------------------------------------------------------ 220 * Message handling 221 */ 222 void waitForEvent(); 223 void signalTransaction(); 224 void signalLayerUpdate(); 225 void signalRefresh(); 226 227 // called on the main thread in response to initializeDisplays() 228 void onInitializeDisplays(); 229 // called on the main thread in response to blank() 230 void onScreenReleased(const sp<const DisplayDevice>& hw); 231 // called on the main thread in response to unblank() 232 void onScreenAcquired(const sp<const DisplayDevice>& hw); 233 234 void handleMessageTransaction(); 235 void handleMessageInvalidate(); 236 void handleMessageRefresh(); 237 238 void handleTransaction(uint32_t transactionFlags); 239 void handleTransactionLocked(uint32_t transactionFlags); 240 241 /* handlePageFilp: this is were we latch a new buffer 242 * if available and compute the dirty region. 243 */ 244 void handlePageFlip(); 245 246 /* ------------------------------------------------------------------------ 247 * Transactions 248 */ 249 uint32_t getTransactionFlags(uint32_t flags); 250 uint32_t peekTransactionFlags(uint32_t flags); 251 uint32_t setTransactionFlags(uint32_t flags); 252 void commitTransaction(); 253 uint32_t setClientStateLocked(const sp<Client>& client, 254 const layer_state_t& s); 255 uint32_t setDisplayStateLocked(const DisplayState& s); 256 257 /* ------------------------------------------------------------------------ 258 * Layer management 259 */ 260 status_t createLayer(const String8& name, const sp<Client>& client, 261 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, 262 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp); 263 264 status_t createNormalLayer(const sp<Client>& client, const String8& name, 265 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, 266 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp, 267 sp<Layer>* outLayer); 268 269 status_t createDimLayer(const sp<Client>& client, const String8& name, 270 uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle, 271 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer); 272 273 // called in response to the window-manager calling 274 // ISurfaceComposerClient::destroySurface() 275 // The specified layer is first placed in a purgatory list 276 // until all references from the client are released. 277 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle); 278 279 // called when all clients have released all their references to 280 // this layer meaning it is entirely safe to destroy all 281 // resources associated to this layer. 282 status_t onLayerDestroyed(const wp<Layer>& layer); 283 284 // remove a layer from SurfaceFlinger immediately 285 status_t removeLayer(const sp<Layer>& layer); 286 287 // add a layer to SurfaceFlinger 288 void addClientLayer(const sp<Client>& client, const sp<IBinder>& handle, 289 const sp<Layer>& lbc); 290 291 status_t removeLayer_l(const sp<Layer>& layer); 292 status_t purgatorizeLayer_l(const sp<Layer>& layer); 293 294 /* ------------------------------------------------------------------------ 295 * Boot animation, on/off animations and screen capture 296 */ 297 298 void startBootAnim(); 299 300 status_t captureScreenImplLocked( 301 const sp<const DisplayDevice>& hw, 302 sp<IMemoryHeap>* heap, 303 uint32_t* width, uint32_t* height, PixelFormat* format, 304 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 305 uint32_t maxLayerZ); 306 307 status_t captureScreenImplLocked( 308 const sp<const DisplayDevice>& hw, 309 const sp<IGraphicBufferProducer>& producer, 310 uint32_t reqWidth, uint32_t reqHeight, 311 uint32_t minLayerZ, uint32_t maxLayerZ); 312 313 /* ------------------------------------------------------------------------ 314 * EGL 315 */ 316 static status_t selectConfigForAttribute(EGLDisplay dpy, 317 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig); 318 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 319 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 320 void initializeGL(EGLDisplay display); 321 uint32_t getMaxTextureSize() const; 322 uint32_t getMaxViewportDims() const; 323 324 /* ------------------------------------------------------------------------ 325 * Display and layer stack management 326 */ 327 // called when starting, or restarting after system_server death 328 void initializeDisplays(); 329 330 // Create an IBinder for a builtin display and add it to current state 331 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type); 332 333 // NOTE: can only be called from the main thread or with mStateLock held 334 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 335 return mDisplays.valueFor(dpy); 336 } 337 338 // NOTE: can only be called from the main thread or with mStateLock held 339 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) { 340 return mDisplays.valueFor(dpy); 341 } 342 343 // mark a region of a layer stack dirty. this updates the dirty 344 // region of all screens presenting this layer stack. 345 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 346 347 /* ------------------------------------------------------------------------ 348 * H/W composer 349 */ 350 351 HWComposer& getHwComposer() const { return *mHwc; } 352 353 /* ------------------------------------------------------------------------ 354 * Compositing 355 */ 356 void invalidateHwcGeometry(); 357 static void computeVisibleRegions( 358 const LayerVector& currentLayers, uint32_t layerStack, 359 Region& dirtyRegion, Region& opaqueRegion); 360 361 void preComposition(); 362 void postComposition(); 363 void rebuildLayerStacks(); 364 void setUpHWComposer(); 365 void doComposition(); 366 void doDebugFlashRegions(); 367 void doDisplayComposition(const sp<const DisplayDevice>& hw, 368 const Region& dirtyRegion); 369 void doComposeSurfaces(const sp<const DisplayDevice>& hw, 370 const Region& dirty); 371 372 void postFramebuffer(); 373 void drawWormhole(const sp<const DisplayDevice>& hw, 374 const Region& region) const; 375 GLuint getProtectedTexName() const { 376 return mProtectedTexName; 377 } 378 379 /* ------------------------------------------------------------------------ 380 * Display management 381 */ 382 383 384 /* ------------------------------------------------------------------------ 385 * Debugging & dumpsys 386 */ 387 void listLayersLocked(const Vector<String16>& args, size_t& index, 388 String8& result, char* buffer, size_t SIZE) const; 389 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 390 String8& result, char* buffer, size_t SIZE) const; 391 void clearStatsLocked(const Vector<String16>& args, size_t& index, 392 String8& result, char* buffer, size_t SIZE); 393 void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const; 394 bool startDdmConnection(); 395 static void appendSfConfigString(String8& result); 396 397 /* ------------------------------------------------------------------------ 398 * Attributes 399 */ 400 401 // access must be protected by mStateLock 402 mutable Mutex mStateLock; 403 State mCurrentState; 404 volatile int32_t mTransactionFlags; 405 Condition mTransactionCV; 406 SortedVector< sp<Layer> > mLayerPurgatory; 407 bool mTransactionPending; 408 bool mAnimTransactionPending; 409 Vector< sp<Layer> > mLayersPendingRemoval; 410 411 // protected by mStateLock (but we could use another lock) 412 bool mLayersRemoved; 413 414 // access must be protected by mInvalidateLock 415 volatile int32_t mRepaintEverything; 416 417 // constant members (no synchronization needed for access) 418 HWComposer* mHwc; 419 GLuint mProtectedTexName; 420 nsecs_t mBootTime; 421 sp<EventThread> mEventThread; 422 GLint mMaxViewportDims[2]; 423 GLint mMaxTextureSize; 424 EGLContext mEGLContext; 425 EGLConfig mEGLConfig; 426 EGLDisplay mEGLDisplay; 427 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES]; 428 429 // Can only accessed from the main thread, these members 430 // don't need synchronization 431 State mDrawingState; 432 bool mVisibleRegionsDirty; 433 bool mHwWorkListDirty; 434 bool mAnimCompositionPending; 435 436 // this may only be written from the main thread with mStateLock held 437 // it may be read from other threads with mStateLock held 438 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 439 440 // don't use a lock for these, we don't care 441 int mDebugRegion; 442 int mDebugDDMS; 443 int mDebugDisableHWC; 444 int mDebugDisableTransformHint; 445 volatile nsecs_t mDebugInSwapBuffers; 446 nsecs_t mLastSwapBufferTime; 447 volatile nsecs_t mDebugInTransaction; 448 nsecs_t mLastTransactionTime; 449 bool mBootFinished; 450 451 // these are thread safe 452 mutable MessageQueue mEventQueue; 453 mutable Barrier mReadyToRunBarrier; 454 FrameTracker mAnimFrameTracker; 455 456 // protected by mDestroyedLayerLock; 457 mutable Mutex mDestroyedLayerLock; 458 Vector<Layer const *> mDestroyedLayers; 459 460 /* ------------------------------------------------------------------------ 461 * Feature prototyping 462 */ 463 464 sp<IBinder> mExtDisplayToken; 465}; 466 467// --------------------------------------------------------------------------- 468}; // namespace android 469 470#endif // ANDROID_SURFACE_FLINGER_H 471