SurfaceFlinger.h revision b6df7d0e4c2117ca476662bd52b6745b3d8a305f
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() ANDROID_API { 83 return "SurfaceFlinger"; 84 } 85 86 SurfaceFlinger() ANDROID_API; 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 // enable/disable h/w composer event 112 // TODO: this should be made accessible only to EventThread 113 void eventControl(int disp, int event, int enabled); 114 115 // called on the main thread by MessageQueue when an internal message 116 // is received 117 // TODO: this should be made accessible only to MessageQueue 118 void onMessageReceived(int32_t what); 119 120 // for debugging only 121 // TODO: this should be made accessible only to HWComposer 122 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id); 123 124private: 125 friend class Client; 126 friend class DisplayEventConnection; 127 friend class Layer; 128 friend class SurfaceTextureLayer; 129 130 // We're reference counted, never destroy SurfaceFlinger directly 131 virtual ~SurfaceFlinger(); 132 133 /* ------------------------------------------------------------------------ 134 * Internal data structures 135 */ 136 137 class LayerVector : public SortedVector< sp<Layer> > { 138 public: 139 LayerVector(); 140 LayerVector(const LayerVector& rhs); 141 virtual int do_compare(const void* lhs, const void* rhs) const; 142 }; 143 144 struct DisplayDeviceState { 145 DisplayDeviceState(); 146 DisplayDeviceState(DisplayDevice::DisplayType type); 147 bool isValid() const { return type >= 0; } 148 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; } 149 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; } 150 DisplayDevice::DisplayType type; 151 sp<IGraphicBufferProducer> surface; 152 uint32_t layerStack; 153 Rect viewport; 154 Rect frame; 155 uint8_t orientation; 156 String8 displayName; 157 bool isSecure; 158 }; 159 160 struct State { 161 LayerVector layersSortedByZ; 162 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 163 }; 164 165 /* ------------------------------------------------------------------------ 166 * IBinder interface 167 */ 168 virtual status_t onTransact(uint32_t code, const Parcel& data, 169 Parcel* reply, uint32_t flags); 170 virtual status_t dump(int fd, const Vector<String16>& args); 171 172 /* ------------------------------------------------------------------------ 173 * ISurfaceComposer interface 174 */ 175 virtual sp<ISurfaceComposerClient> createConnection(); 176 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 177 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure); 178 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 179 virtual void setTransactionState(const Vector<ComposerState>& state, 180 const Vector<DisplayState>& displays, uint32_t flags); 181 virtual void bootFinished(); 182 virtual bool authenticateSurfaceTexture( 183 const sp<IGraphicBufferProducer>& bufferProducer) const; 184 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 185 virtual status_t captureScreen(const sp<IBinder>& display, 186 const sp<IGraphicBufferProducer>& producer, 187 uint32_t reqWidth, uint32_t reqHeight, 188 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer); 189 // called when screen needs to turn off 190 virtual void blank(const sp<IBinder>& display); 191 // called when screen is turning back on 192 virtual void unblank(const sp<IBinder>& display); 193 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info); 194 195 /* ------------------------------------------------------------------------ 196 * DeathRecipient interface 197 */ 198 virtual void binderDied(const wp<IBinder>& who); 199 200 /* ------------------------------------------------------------------------ 201 * Thread interface 202 */ 203 virtual bool threadLoop(); 204 virtual status_t readyToRun(); 205 virtual void onFirstRef(); 206 207 /* ------------------------------------------------------------------------ 208 * HWComposer::EventHandler interface 209 */ 210 virtual void onVSyncReceived(int type, nsecs_t timestamp); 211 virtual void onHotplugReceived(int disp, bool connected); 212 213 /* ------------------------------------------------------------------------ 214 * Message handling 215 */ 216 void waitForEvent(); 217 void signalTransaction(); 218 void signalLayerUpdate(); 219 void signalRefresh(); 220 221 // called on the main thread in response to initializeDisplays() 222 void onInitializeDisplays(); 223 // called on the main thread in response to blank() 224 void onScreenReleased(const sp<const DisplayDevice>& hw); 225 // called on the main thread in response to unblank() 226 void onScreenAcquired(const sp<const DisplayDevice>& hw); 227 228 void handleMessageTransaction(); 229 void handleMessageInvalidate(); 230 void handleMessageRefresh(); 231 232 void handleTransaction(uint32_t transactionFlags); 233 void handleTransactionLocked(uint32_t transactionFlags); 234 235 /* handlePageFilp: this is were we latch a new buffer 236 * if available and compute the dirty region. 237 */ 238 void handlePageFlip(); 239 240 /* ------------------------------------------------------------------------ 241 * Transactions 242 */ 243 uint32_t getTransactionFlags(uint32_t flags); 244 uint32_t peekTransactionFlags(uint32_t flags); 245 uint32_t setTransactionFlags(uint32_t flags); 246 void commitTransaction(); 247 uint32_t setClientStateLocked(const sp<Client>& client, 248 const layer_state_t& s); 249 uint32_t setDisplayStateLocked(const DisplayState& s); 250 251 /* ------------------------------------------------------------------------ 252 * Layer management 253 */ 254 status_t createLayer(const String8& name, const sp<Client>& client, 255 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, 256 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp); 257 258 status_t createNormalLayer(const sp<Client>& client, const String8& name, 259 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, 260 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp, 261 sp<Layer>* outLayer); 262 263 status_t createDimLayer(const sp<Client>& client, const String8& name, 264 uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle, 265 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer); 266 267 // called in response to the window-manager calling 268 // ISurfaceComposerClient::destroySurface() 269 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle); 270 271 // called when all clients have released all their references to 272 // this layer meaning it is entirely safe to destroy all 273 // resources associated to this layer. 274 status_t onLayerDestroyed(const wp<Layer>& layer); 275 276 // remove a layer from SurfaceFlinger immediately 277 status_t removeLayer(const sp<Layer>& layer); 278 279 // add a layer to SurfaceFlinger 280 void addClientLayer(const sp<Client>& client, 281 const sp<IBinder>& handle, 282 const sp<IGraphicBufferProducer>& gbc, 283 const sp<Layer>& lbc); 284 285 /* ------------------------------------------------------------------------ 286 * Boot animation, on/off animations and screen capture 287 */ 288 289 void startBootAnim(); 290 291 void renderScreenImplLocked( 292 const sp<const DisplayDevice>& hw, 293 uint32_t reqWidth, uint32_t reqHeight, 294 uint32_t minLayerZ, uint32_t maxLayerZ, 295 bool yswap); 296 297 status_t captureScreenImplLocked( 298 const sp<const DisplayDevice>& hw, 299 const sp<IGraphicBufferProducer>& producer, 300 uint32_t reqWidth, uint32_t reqHeight, 301 uint32_t minLayerZ, uint32_t maxLayerZ, 302 bool useReadPixels); 303 304 /* ------------------------------------------------------------------------ 305 * EGL 306 */ 307 static status_t selectConfigForAttribute(EGLDisplay dpy, 308 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig); 309 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 310 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 311 void initializeGL(EGLDisplay display); 312 uint32_t getMaxTextureSize() const; 313 uint32_t getMaxViewportDims() const; 314 315 /* ------------------------------------------------------------------------ 316 * Display and layer stack management 317 */ 318 // called when starting, or restarting after system_server death 319 void initializeDisplays(); 320 321 // Create an IBinder for a builtin display and add it to current state 322 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type); 323 324 // NOTE: can only be called from the main thread or with mStateLock held 325 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 326 return mDisplays.valueFor(dpy); 327 } 328 329 // NOTE: can only be called from the main thread or with mStateLock held 330 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) { 331 return mDisplays.valueFor(dpy); 332 } 333 334 // mark a region of a layer stack dirty. this updates the dirty 335 // region of all screens presenting this layer stack. 336 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 337 338 // allocate a h/w composer display id 339 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type); 340 341 /* ------------------------------------------------------------------------ 342 * H/W composer 343 */ 344 345 HWComposer& getHwComposer() const { return *mHwc; } 346 347 /* ------------------------------------------------------------------------ 348 * Compositing 349 */ 350 void invalidateHwcGeometry(); 351 static void computeVisibleRegions( 352 const LayerVector& currentLayers, uint32_t layerStack, 353 Region& dirtyRegion, Region& opaqueRegion); 354 355 void preComposition(); 356 void postComposition(); 357 void rebuildLayerStacks(); 358 void setUpHWComposer(); 359 void doComposition(); 360 void doDebugFlashRegions(); 361 void doDisplayComposition(const sp<const DisplayDevice>& hw, 362 const Region& dirtyRegion); 363 void doComposeSurfaces(const sp<const DisplayDevice>& hw, 364 const Region& dirty); 365 366 void postFramebuffer(); 367 void drawWormhole(const sp<const DisplayDevice>& hw, 368 const Region& region) const; 369 GLuint getProtectedTexName() const { 370 return mProtectedTexName; 371 } 372 373 /* ------------------------------------------------------------------------ 374 * Display management 375 */ 376 377 378 /* ------------------------------------------------------------------------ 379 * Debugging & dumpsys 380 */ 381 void listLayersLocked(const Vector<String16>& args, size_t& index, 382 String8& result) const; 383 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 384 String8& result) const; 385 void clearStatsLocked(const Vector<String16>& args, size_t& index, 386 String8& result); 387 void dumpAllLocked(const Vector<String16>& args, size_t& index, 388 String8& result) const; 389 bool startDdmConnection(); 390 static void appendSfConfigString(String8& result); 391 392 /* ------------------------------------------------------------------------ 393 * Attributes 394 */ 395 396 // access must be protected by mStateLock 397 mutable Mutex mStateLock; 398 State mCurrentState; 399 volatile int32_t mTransactionFlags; 400 Condition mTransactionCV; 401 bool mTransactionPending; 402 bool mAnimTransactionPending; 403 Vector< sp<Layer> > mLayersPendingRemoval; 404 SortedVector< wp<IBinder> > mGraphicBufferProducerList; 405 406 // protected by mStateLock (but we could use another lock) 407 bool mLayersRemoved; 408 409 // access must be protected by mInvalidateLock 410 volatile int32_t mRepaintEverything; 411 412 // constant members (no synchronization needed for access) 413 HWComposer* mHwc; 414 GLuint mProtectedTexName; 415 nsecs_t mBootTime; 416 bool mGpuToCpuSupported; 417 sp<EventThread> mEventThread; 418 GLint mMaxViewportDims[2]; 419 GLint mMaxTextureSize; 420 EGLContext mEGLContext; 421 EGLConfig mEGLConfig; 422 EGLDisplay mEGLDisplay; 423 EGLint mEGLNativeVisualId; 424 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES]; 425 426 // Can only accessed from the main thread, these members 427 // don't need synchronization 428 State mDrawingState; 429 bool mVisibleRegionsDirty; 430 bool mHwWorkListDirty; 431 bool mAnimCompositionPending; 432 433 // this may only be written from the main thread with mStateLock held 434 // it may be read from other threads with mStateLock held 435 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 436 437 // don't use a lock for these, we don't care 438 int mDebugRegion; 439 int mDebugDDMS; 440 int mDebugDisableHWC; 441 int mDebugDisableTransformHint; 442 volatile nsecs_t mDebugInSwapBuffers; 443 nsecs_t mLastSwapBufferTime; 444 volatile nsecs_t mDebugInTransaction; 445 nsecs_t mLastTransactionTime; 446 bool mBootFinished; 447 448 // these are thread safe 449 mutable MessageQueue mEventQueue; 450 mutable Barrier mReadyToRunBarrier; 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