SurfaceFlinger.h revision 13127d8921356dff794250e04208c3ed60b3a3df
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 sp<ISurface> createLayer(const String8& name, const sp<Client>& client, 261 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 262 263 sp<Layer> createNormalLayer(const sp<Client>& client, 264 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format); 265 266 sp<LayerDim> createDimLayer(const sp<Client>& client, 267 uint32_t w, uint32_t h, uint32_t flags); 268 269 // called in response to the window-manager calling 270 // ISurfaceComposerClient::destroySurface() 271 // The specified layer is first placed in a purgatory list 272 // until all references from the client are released. 273 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle); 274 275 // called when all clients have released all their references to 276 // this layer meaning it is entirely safe to destroy all 277 // resources associated to this layer. 278 status_t onLayerDestroyed(const wp<Layer>& layer); 279 280 // remove a layer from SurfaceFlinger immediately 281 status_t removeLayer(const sp<Layer>& layer); 282 283 // add a layer to SurfaceFlinger 284 void addClientLayer(const sp<Client>& client, const sp<IBinder>& handle, 285 const sp<Layer>& lbc); 286 287 status_t removeLayer_l(const sp<Layer>& layer); 288 status_t purgatorizeLayer_l(const sp<Layer>& layer); 289 290 /* ------------------------------------------------------------------------ 291 * Boot animation, on/off animations and screen capture 292 */ 293 294 void startBootAnim(); 295 296 status_t captureScreenImplLocked( 297 const sp<const DisplayDevice>& hw, 298 sp<IMemoryHeap>* heap, 299 uint32_t* width, uint32_t* height, PixelFormat* format, 300 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 301 uint32_t maxLayerZ); 302 303 status_t captureScreenImplLocked( 304 const sp<const DisplayDevice>& hw, 305 const sp<IGraphicBufferProducer>& producer, 306 uint32_t reqWidth, uint32_t reqHeight, 307 uint32_t minLayerZ, uint32_t maxLayerZ); 308 309 /* ------------------------------------------------------------------------ 310 * EGL 311 */ 312 static status_t selectConfigForAttribute(EGLDisplay dpy, 313 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig); 314 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 315 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 316 void initializeGL(EGLDisplay display); 317 uint32_t getMaxTextureSize() const; 318 uint32_t getMaxViewportDims() const; 319 320 /* ------------------------------------------------------------------------ 321 * Display and layer stack management 322 */ 323 // called when starting, or restarting after system_server death 324 void initializeDisplays(); 325 326 // Create an IBinder for a builtin display and add it to current state 327 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type); 328 329 // NOTE: can only be called from the main thread or with mStateLock held 330 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 331 return mDisplays.valueFor(dpy); 332 } 333 334 // NOTE: can only be called from the main thread or with mStateLock held 335 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) { 336 return mDisplays.valueFor(dpy); 337 } 338 339 // mark a region of a layer stack dirty. this updates the dirty 340 // region of all screens presenting this layer stack. 341 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 342 343 /* ------------------------------------------------------------------------ 344 * H/W composer 345 */ 346 347 HWComposer& getHwComposer() const { return *mHwc; } 348 349 /* ------------------------------------------------------------------------ 350 * Compositing 351 */ 352 void invalidateHwcGeometry(); 353 static void computeVisibleRegions( 354 const LayerVector& currentLayers, uint32_t layerStack, 355 Region& dirtyRegion, Region& opaqueRegion); 356 357 void preComposition(); 358 void postComposition(); 359 void rebuildLayerStacks(); 360 void setUpHWComposer(); 361 void doComposition(); 362 void doDebugFlashRegions(); 363 void doDisplayComposition(const sp<const DisplayDevice>& hw, 364 const Region& dirtyRegion); 365 void doComposeSurfaces(const sp<const DisplayDevice>& hw, 366 const Region& dirty); 367 368 void postFramebuffer(); 369 void drawWormhole(const sp<const DisplayDevice>& hw, 370 const Region& region) const; 371 GLuint getProtectedTexName() const { 372 return mProtectedTexName; 373 } 374 375 /* ------------------------------------------------------------------------ 376 * Display management 377 */ 378 379 380 /* ------------------------------------------------------------------------ 381 * Debugging & dumpsys 382 */ 383 void listLayersLocked(const Vector<String16>& args, size_t& index, 384 String8& result, char* buffer, size_t SIZE) const; 385 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 386 String8& result, char* buffer, size_t SIZE) const; 387 void clearStatsLocked(const Vector<String16>& args, size_t& index, 388 String8& result, char* buffer, size_t SIZE); 389 void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const; 390 bool startDdmConnection(); 391 static void appendSfConfigString(String8& result); 392 393 /* ------------------------------------------------------------------------ 394 * Attributes 395 */ 396 397 // access must be protected by mStateLock 398 mutable Mutex mStateLock; 399 State mCurrentState; 400 volatile int32_t mTransactionFlags; 401 Condition mTransactionCV; 402 SortedVector< sp<Layer> > mLayerPurgatory; 403 bool mTransactionPending; 404 bool mAnimTransactionPending; 405 Vector< sp<Layer> > mLayersPendingRemoval; 406 407 // protected by mStateLock (but we could use another lock) 408 bool mLayersRemoved; 409 410 // access must be protected by mInvalidateLock 411 volatile int32_t mRepaintEverything; 412 413 // constant members (no synchronization needed for access) 414 HWComposer* mHwc; 415 GLuint mProtectedTexName; 416 nsecs_t mBootTime; 417 sp<EventThread> mEventThread; 418 GLint mMaxViewportDims[2]; 419 GLint mMaxTextureSize; 420 EGLContext mEGLContext; 421 EGLConfig mEGLConfig; 422 EGLDisplay mEGLDisplay; 423 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES]; 424 425 // Can only accessed from the main thread, these members 426 // don't need synchronization 427 State mDrawingState; 428 bool mVisibleRegionsDirty; 429 bool mHwWorkListDirty; 430 bool mAnimCompositionPending; 431 432 // this may only be written from the main thread with mStateLock held 433 // it may be read from other threads with mStateLock held 434 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 435 436 // don't use a lock for these, we don't care 437 int mDebugRegion; 438 int mDebugDDMS; 439 int mDebugDisableHWC; 440 int mDebugDisableTransformHint; 441 volatile nsecs_t mDebugInSwapBuffers; 442 nsecs_t mLastSwapBufferTime; 443 volatile nsecs_t mDebugInTransaction; 444 nsecs_t mLastTransactionTime; 445 bool mBootFinished; 446 447 // these are thread safe 448 mutable MessageQueue mEventQueue; 449 mutable Barrier mReadyToRunBarrier; 450 FrameTracker mAnimFrameTracker; 451 452 // protected by mDestroyedLayerLock; 453 mutable Mutex mDestroyedLayerLock; 454 Vector<Layer const *> mDestroyedLayers; 455 456 /* ------------------------------------------------------------------------ 457 * Feature prototyping 458 */ 459 460 sp<IBinder> mExtDisplayToken; 461}; 462 463// --------------------------------------------------------------------------- 464}; // namespace android 465 466#endif // ANDROID_SURFACE_FLINGER_H 467