SurfaceFlinger.h revision fee2b463c5fbe8fa0132d03634ccc02ea55c1505
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 75enum GlesVersion { 76 GLES_VERSION_1_0 = 0x10000, 77 GLES_VERSION_1_1 = 0x10001, 78 GLES_VERSION_2_0 = 0x20000, 79 GLES_VERSION_3_0 = 0x30000, 80}; 81 82class SurfaceFlinger : public BinderService<SurfaceFlinger>, 83 public BnSurfaceComposer, 84 private IBinder::DeathRecipient, 85 private Thread, 86 private HWComposer::EventHandler 87{ 88public: 89 static char const* getServiceName() ANDROID_API { 90 return "SurfaceFlinger"; 91 } 92 93 SurfaceFlinger() 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, 101 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, 105 uint32_t flags = 0); 106 107 // force full composition on all displays 108 void repaintEverything(); 109 110 // returns the default Display 111 sp<const DisplayDevice> getDefaultDisplayDevice() const { 112 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]); 113 } 114 115 // utility function to delete a texture on the main thread 116 void deleteTextureAsync(GLuint texture); 117 118 // enable/disable h/w composer event 119 // TODO: this should be made accessible only to EventThread 120 void eventControl(int disp, int event, int enabled); 121 122 // called on the main thread by MessageQueue when an internal message 123 // is received 124 // TODO: this should be made accessible only to MessageQueue 125 void onMessageReceived(int32_t what); 126 127 // for debugging only 128 // TODO: this should be made accessible only to HWComposer 129 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id); 130 131 // return the version of the OpenGL ES composition context 132 GlesVersion getGlesVersion() const { 133 return mGlesVersion; 134 } 135 136private: 137 friend class Client; 138 friend class DisplayEventConnection; 139 friend class Layer; 140 friend class SurfaceTextureLayer; 141 142 // We're reference counted, never destroy SurfaceFlinger directly 143 virtual ~SurfaceFlinger(); 144 145 /* ------------------------------------------------------------------------ 146 * Internal data structures 147 */ 148 149 class LayerVector : public SortedVector< sp<Layer> > { 150 public: 151 LayerVector(); 152 LayerVector(const LayerVector& rhs); 153 virtual int do_compare(const void* lhs, const void* rhs) const; 154 }; 155 156 struct DisplayDeviceState { 157 DisplayDeviceState(); 158 DisplayDeviceState(DisplayDevice::DisplayType type); 159 bool isValid() const { return type >= 0; } 160 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; } 161 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; } 162 DisplayDevice::DisplayType type; 163 sp<IGraphicBufferProducer> surface; 164 uint32_t layerStack; 165 Rect viewport; 166 Rect frame; 167 uint8_t orientation; 168 String8 displayName; 169 bool isSecure; 170 }; 171 172 struct State { 173 LayerVector layersSortedByZ; 174 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 175 }; 176 177 /* ------------------------------------------------------------------------ 178 * IBinder interface 179 */ 180 virtual status_t onTransact(uint32_t code, const Parcel& data, 181 Parcel* reply, uint32_t flags); 182 virtual status_t dump(int fd, const Vector<String16>& args); 183 184 /* ------------------------------------------------------------------------ 185 * ISurfaceComposer interface 186 */ 187 virtual sp<ISurfaceComposerClient> createConnection(); 188 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 189 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure); 190 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 191 virtual void setTransactionState(const Vector<ComposerState>& state, 192 const Vector<DisplayState>& displays, uint32_t flags); 193 virtual void bootFinished(); 194 virtual bool authenticateSurfaceTexture( 195 const sp<IGraphicBufferProducer>& bufferProducer) const; 196 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 197 virtual status_t captureScreen(const sp<IBinder>& display, 198 const sp<IGraphicBufferProducer>& producer, 199 uint32_t reqWidth, uint32_t reqHeight, 200 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer); 201 // called when screen needs to turn off 202 virtual void blank(const sp<IBinder>& display); 203 // called when screen is turning back on 204 virtual void unblank(const sp<IBinder>& display); 205 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info); 206 207 /* ------------------------------------------------------------------------ 208 * DeathRecipient interface 209 */ 210 virtual void binderDied(const wp<IBinder>& who); 211 212 /* ------------------------------------------------------------------------ 213 * Thread interface 214 */ 215 virtual bool threadLoop(); 216 virtual status_t readyToRun(); 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, 260 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 bool useReadPixels); 315 316 /* ------------------------------------------------------------------------ 317 * EGL 318 */ 319 static status_t selectConfigForAttribute(EGLDisplay dpy, 320 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig); 321 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 322 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 323 void initializeGL(EGLDisplay display); 324 uint32_t getMaxTextureSize() const; 325 uint32_t getMaxViewportDims() const; 326 327 /* ------------------------------------------------------------------------ 328 * Display and layer stack management 329 */ 330 // called when starting, or restarting after system_server death 331 void initializeDisplays(); 332 333 // Create an IBinder for a builtin display and add it to current state 334 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type); 335 336 // NOTE: can only be called from the main thread or with mStateLock held 337 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 338 return mDisplays.valueFor(dpy); 339 } 340 341 // NOTE: can only be called from the main thread or with mStateLock held 342 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) { 343 return mDisplays.valueFor(dpy); 344 } 345 346 // mark a region of a layer stack dirty. this updates the dirty 347 // region of all screens presenting this layer stack. 348 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 349 350 // allocate a h/w composer display id 351 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type); 352 353 /* ------------------------------------------------------------------------ 354 * H/W composer 355 */ 356 357 HWComposer& getHwComposer() const { return *mHwc; } 358 359 /* ------------------------------------------------------------------------ 360 * Compositing 361 */ 362 void invalidateHwcGeometry(); 363 static void computeVisibleRegions( 364 const LayerVector& currentLayers, uint32_t layerStack, 365 Region& dirtyRegion, Region& opaqueRegion); 366 367 void preComposition(); 368 void postComposition(); 369 void rebuildLayerStacks(); 370 void setUpHWComposer(); 371 void doComposition(); 372 void doDebugFlashRegions(); 373 void doDisplayComposition(const sp<const DisplayDevice>& hw, 374 const Region& dirtyRegion); 375 void doComposeSurfaces(const sp<const DisplayDevice>& hw, 376 const Region& dirty); 377 378 void postFramebuffer(); 379 void drawWormhole(const sp<const DisplayDevice>& hw, 380 const Region& region) const; 381 GLuint getProtectedTexName() const { 382 return mProtectedTexName; 383 } 384 385 /* ------------------------------------------------------------------------ 386 * Display management 387 */ 388 389 390 /* ------------------------------------------------------------------------ 391 * Debugging & dumpsys 392 */ 393 void listLayersLocked(const Vector<String16>& args, size_t& index, 394 String8& result) const; 395 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 396 String8& result) const; 397 void clearStatsLocked(const Vector<String16>& args, size_t& index, 398 String8& result); 399 void dumpAllLocked(const Vector<String16>& args, size_t& index, 400 String8& result) const; 401 bool startDdmConnection(); 402 static void appendSfConfigString(String8& result); 403 void checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr, 404 const sp<const DisplayDevice>& hw, 405 uint32_t minLayerZ, uint32_t maxLayerZ); 406 407 /* ------------------------------------------------------------------------ 408 * Attributes 409 */ 410 411 // access must be protected by mStateLock 412 mutable Mutex mStateLock; 413 State mCurrentState; 414 volatile int32_t mTransactionFlags; 415 Condition mTransactionCV; 416 bool mTransactionPending; 417 bool mAnimTransactionPending; 418 Vector< sp<Layer> > mLayersPendingRemoval; 419 SortedVector< wp<IBinder> > mGraphicBufferProducerList; 420 421 // protected by mStateLock (but we could use another lock) 422 bool mLayersRemoved; 423 424 // access must be protected by mInvalidateLock 425 volatile int32_t mRepaintEverything; 426 427 // constant members (no synchronization needed for access) 428 HWComposer* mHwc; 429 GLuint mProtectedTexName; 430 nsecs_t mBootTime; 431 bool mGpuToCpuSupported; 432 sp<EventThread> mEventThread; 433 GLint mMaxViewportDims[2]; 434 GLint mMaxTextureSize; 435 EGLContext mEGLContext; 436 EGLConfig mEGLConfig; 437 EGLDisplay mEGLDisplay; 438 EGLint mEGLNativeVisualId; 439 GlesVersion mGlesVersion; 440 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES]; 441 442 // Can only accessed from the main thread, these members 443 // don't need synchronization 444 State mDrawingState; 445 bool mVisibleRegionsDirty; 446 bool mHwWorkListDirty; 447 bool mAnimCompositionPending; 448 449 // this may only be written from the main thread with mStateLock held 450 // it may be read from other threads with mStateLock held 451 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 452 453 // don't use a lock for these, we don't care 454 int mDebugRegion; 455 int mDebugDDMS; 456 int mDebugDisableHWC; 457 int mDebugDisableTransformHint; 458 volatile nsecs_t mDebugInSwapBuffers; 459 nsecs_t mLastSwapBufferTime; 460 volatile nsecs_t mDebugInTransaction; 461 nsecs_t mLastTransactionTime; 462 bool mBootFinished; 463 464 // these are thread safe 465 mutable MessageQueue mEventQueue; 466 mutable Barrier mReadyToRunBarrier; 467 FrameTracker mAnimFrameTracker; 468 469 // protected by mDestroyedLayerLock; 470 mutable Mutex mDestroyedLayerLock; 471 Vector<Layer const *> mDestroyedLayers; 472 473 /* ------------------------------------------------------------------------ 474 * Feature prototyping 475 */ 476 477 sp<IBinder> mExtDisplayToken; 478}; 479 480// --------------------------------------------------------------------------- 481}; // namespace android 482 483#endif // ANDROID_SURFACE_FLINGER_H 484