SurfaceFlinger.h revision 089a15298e045598bf15fd2a46284c34dd56384c
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 LayerBase; 64class LayerBaseClient; 65class LayerDim; 66class Surface; 67 68// --------------------------------------------------------------------------- 69 70enum { 71 eTransactionNeeded = 0x01, 72 eTraversalNeeded = 0x02, 73 eDisplayTransactionNeeded = 0x04, 74 eTransactionMask = 0x07 75}; 76 77class SurfaceFlinger : public BinderService<SurfaceFlinger>, 78 public BnSurfaceComposer, 79 private IBinder::DeathRecipient, 80 private Thread, 81 private HWComposer::EventHandler 82{ 83public: 84 static char const* getServiceName() { 85 return "SurfaceFlinger"; 86 } 87 88 SurfaceFlinger(); 89 90 enum { 91 EVENT_VSYNC = HWC_EVENT_VSYNC 92 }; 93 94 // post an asynchronous message to the main thread 95 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, 96 uint32_t flags = 0); 97 98 // post a synchronous message to the main thread 99 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, 100 uint32_t flags = 0); 101 102 // force full composition on all displays 103 void repaintEverything(); 104 105 // returns the default Display 106 sp<const DisplayDevice> getDefaultDisplayDevice() const { 107 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]); 108 } 109 110 // utility function to delete a texture on the main thread 111 void deleteTextureAsync(GLuint texture); 112 113 // allocate a h/w composer display id 114 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type); 115 116 // enable/disable h/w composer event 117 // TODO: this should be made accessible only to EventThread 118 void eventControl(int disp, int event, int enabled); 119 120 // called on the main thread by MessageQueue when an internal message 121 // is received 122 // TODO: this should be made accessible only to MessageQueue 123 void onMessageReceived(int32_t what); 124 125 // for debugging only 126 // TODO: this should be made accessible only to HWComposer 127 const Vector< sp<LayerBase> >& getLayerSortedByZForHwcDisplay(int disp); 128 129private: 130 friend class Client; 131 friend class DisplayEventConnection; 132 friend class LayerBase; 133 friend class LayerBaseClient; 134 friend class Layer; 135 136 // We're reference counted, never destroy SurfaceFlinger directly 137 virtual ~SurfaceFlinger(); 138 139 /* ------------------------------------------------------------------------ 140 * Internal data structures 141 */ 142 143 class LayerVector : public SortedVector<sp<LayerBase> > { 144 public: 145 LayerVector(); 146 LayerVector(const LayerVector& rhs); 147 virtual int do_compare(const void* lhs, const void* rhs) const; 148 }; 149 150 struct DisplayDeviceState { 151 DisplayDeviceState(); 152 DisplayDeviceState(DisplayDevice::DisplayType type); 153 bool isValid() const { return type >= 0; } 154 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; } 155 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; } 156 DisplayDevice::DisplayType type; 157 sp<IGraphicBufferProducer> surface; 158 uint32_t layerStack; 159 Rect viewport; 160 Rect frame; 161 uint8_t orientation; 162 String8 displayName; 163 bool isSecure; 164 }; 165 166 struct State { 167 LayerVector layersSortedByZ; 168 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 169 }; 170 171 /* ------------------------------------------------------------------------ 172 * IBinder interface 173 */ 174 virtual status_t onTransact(uint32_t code, const Parcel& data, 175 Parcel* reply, uint32_t flags); 176 virtual status_t dump(int fd, const Vector<String16>& args); 177 178 /* ------------------------------------------------------------------------ 179 * ISurfaceComposer interface 180 */ 181 virtual sp<ISurfaceComposerClient> createConnection(); 182 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 183 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure); 184 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 185 virtual void setTransactionState(const Vector<ComposerState>& state, 186 const Vector<DisplayState>& displays, uint32_t flags); 187 virtual void bootFinished(); 188 virtual bool authenticateSurfaceTexture( 189 const sp<IGraphicBufferProducer>& bufferProducer) const; 190 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 191 virtual status_t captureScreen(const sp<IBinder>& display, sp<IMemoryHeap>* heap, 192 uint32_t* width, uint32_t* height, PixelFormat* format, 193 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 194 uint32_t maxLayerZ); 195 virtual status_t captureScreen(const sp<IBinder>& display, 196 const sp<IGraphicBufferProducer>& producer, 197 uint32_t reqWidth, uint32_t reqHeight, 198 uint32_t minLayerZ, uint32_t maxLayerZ); 199 // called when screen needs to turn off 200 virtual void blank(const sp<IBinder>& display); 201 // called when screen is turning back on 202 virtual void unblank(const sp<IBinder>& display); 203 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info); 204 205 /* ------------------------------------------------------------------------ 206 * DeathRecipient interface 207 */ 208 virtual void binderDied(const wp<IBinder>& who); 209 210 /* ------------------------------------------------------------------------ 211 * Thread interface 212 */ 213 virtual bool threadLoop(); 214 virtual status_t readyToRun(); 215 virtual void onFirstRef(); 216 217 /* ------------------------------------------------------------------------ 218 * HWComposer::EventHandler interface 219 */ 220 virtual void onVSyncReceived(int type, nsecs_t timestamp); 221 virtual void onHotplugReceived(int disp, bool connected); 222 223 /* ------------------------------------------------------------------------ 224 * Message handling 225 */ 226 void waitForEvent(); 227 void signalTransaction(); 228 void signalLayerUpdate(); 229 void signalRefresh(); 230 231 // called on the main thread in response to initializeDisplays() 232 void onInitializeDisplays(); 233 // called on the main thread in response to blank() 234 void onScreenReleased(const sp<const DisplayDevice>& hw); 235 // called on the main thread in response to unblank() 236 void onScreenAcquired(const sp<const DisplayDevice>& hw); 237 238 void handleMessageTransaction(); 239 void handleMessageInvalidate(); 240 void handleMessageRefresh(); 241 242 void handleTransaction(uint32_t transactionFlags); 243 void handleTransactionLocked(uint32_t transactionFlags); 244 245 /* handlePageFilp: this is were we latch a new buffer 246 * if available and compute the dirty region. 247 */ 248 void handlePageFlip(); 249 250 /* ------------------------------------------------------------------------ 251 * Transactions 252 */ 253 uint32_t getTransactionFlags(uint32_t flags); 254 uint32_t peekTransactionFlags(uint32_t flags); 255 uint32_t setTransactionFlags(uint32_t flags); 256 void commitTransaction(); 257 uint32_t setClientStateLocked(const sp<Client>& client, 258 const layer_state_t& s); 259 uint32_t setDisplayStateLocked(const DisplayState& s); 260 261 /* ------------------------------------------------------------------------ 262 * Layer management 263 */ 264 sp<ISurface> createLayer(const String8& name, const sp<Client>& client, 265 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 266 267 sp<Layer> createNormalLayer(const sp<Client>& client, 268 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format); 269 270 sp<LayerDim> createDimLayer(const sp<Client>& client, 271 uint32_t w, uint32_t h, uint32_t flags); 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<LayerBaseClient>& layer); 283 284 // remove a layer from SurfaceFlinger immediately 285 status_t removeLayer(const sp<LayerBase>& layer); 286 287 // add a layer to SurfaceFlinger 288 void addClientLayer(const sp<Client>& client, const sp<IBinder>& handle, 289 const sp<LayerBaseClient>& lbc); 290 291 status_t removeLayer_l(const sp<LayerBase>& layer); 292 status_t purgatorizeLayer_l(const sp<LayerBase>& 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<LayerBase> > mLayerPurgatory; 407 bool mTransactionPending; 408 bool mAnimTransactionPending; 409 Vector<sp<LayerBase> > 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<LayerBase 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