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