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