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