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 54 namespace android { 55 56 // --------------------------------------------------------------------------- 57 58 class Client; 59 class DisplayEventConnection; 60 class EventThread; 61 class IGraphicBufferAlloc; 62 class Layer; 63 class LayerDim; 64 class Surface; 65 66 // --------------------------------------------------------------------------- 67 68 enum { 69 eTransactionNeeded = 0x01, 70 eTraversalNeeded = 0x02, 71 eDisplayTransactionNeeded = 0x04, 72 eTransactionMask = 0x07 73 }; 74 75 class SurfaceFlinger : public BinderService<SurfaceFlinger>, 76 public BnSurfaceComposer, 77 private IBinder::DeathRecipient, 78 private Thread, 79 private HWComposer::EventHandler 80 { 81 public: 82 static char const* getServiceName() { 83 return "SurfaceFlinger"; 84 } 85 86 SurfaceFlinger(); 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 124 private: 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 303 status_t captureScreenImplCpuConsumerLocked( 304 const sp<const DisplayDevice>& hw, 305 const sp<IGraphicBufferProducer>& producer, 306 uint32_t reqWidth, uint32_t reqHeight, 307 uint32_t minLayerZ, uint32_t maxLayerZ); 308 309 310 /* ------------------------------------------------------------------------ 311 * EGL 312 */ 313 static status_t selectConfigForAttribute(EGLDisplay dpy, 314 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig); 315 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 316 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 317 void initializeGL(EGLDisplay display); 318 uint32_t getMaxTextureSize() const; 319 uint32_t getMaxViewportDims() const; 320 321 /* ------------------------------------------------------------------------ 322 * Display and layer stack management 323 */ 324 // called when starting, or restarting after system_server death 325 void initializeDisplays(); 326 327 // Create an IBinder for a builtin display and add it to current state 328 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type); 329 330 // NOTE: can only be called from the main thread or with mStateLock held 331 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 332 return mDisplays.valueFor(dpy); 333 } 334 335 // NOTE: can only be called from the main thread or with mStateLock held 336 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) { 337 return mDisplays.valueFor(dpy); 338 } 339 340 // mark a region of a layer stack dirty. this updates the dirty 341 // region of all screens presenting this layer stack. 342 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 343 344 // allocate a h/w composer display id 345 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type); 346 347 /* ------------------------------------------------------------------------ 348 * H/W composer 349 */ 350 351 HWComposer& getHwComposer() const { return *mHwc; } 352 353 /* ------------------------------------------------------------------------ 354 * Compositing 355 */ 356 void invalidateHwcGeometry(); 357 static void computeVisibleRegions( 358 const LayerVector& currentLayers, uint32_t layerStack, 359 Region& dirtyRegion, Region& opaqueRegion); 360 361 void preComposition(); 362 void postComposition(); 363 void rebuildLayerStacks(); 364 void setUpHWComposer(); 365 void doComposition(); 366 void doDebugFlashRegions(); 367 void doDisplayComposition(const sp<const DisplayDevice>& hw, 368 const Region& dirtyRegion); 369 void doComposeSurfaces(const sp<const DisplayDevice>& hw, 370 const Region& dirty); 371 372 void postFramebuffer(); 373 void drawWormhole(const sp<const DisplayDevice>& hw, 374 const Region& region) const; 375 GLuint getProtectedTexName() const { 376 return mProtectedTexName; 377 } 378 379 /* ------------------------------------------------------------------------ 380 * Display management 381 */ 382 383 384 /* ------------------------------------------------------------------------ 385 * Debugging & dumpsys 386 */ 387 void listLayersLocked(const Vector<String16>& args, size_t& index, 388 String8& result, char* buffer, size_t SIZE) const; 389 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 390 String8& result, char* buffer, size_t SIZE) const; 391 void clearStatsLocked(const Vector<String16>& args, size_t& index, 392 String8& result, char* buffer, size_t SIZE); 393 void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const; 394 bool startDdmConnection(); 395 static void appendSfConfigString(String8& result); 396 397 /* ------------------------------------------------------------------------ 398 * Attributes 399 */ 400 401 // access must be protected by mStateLock 402 mutable Mutex mStateLock; 403 State mCurrentState; 404 volatile int32_t mTransactionFlags; 405 Condition mTransactionCV; 406 bool mTransactionPending; 407 bool mAnimTransactionPending; 408 Vector< sp<Layer> > mLayersPendingRemoval; 409 SortedVector< wp<IBinder> > mGraphicBufferProducerList; 410 411 // protected by mStateLock (but we could use another lock) 412 bool mLayersRemoved; 413 414 // access must be protected by mInvalidateLock 415 volatile int32_t mRepaintEverything; 416 417 // constant members (no synchronization needed for access) 418 HWComposer* mHwc; 419 GLuint mProtectedTexName; 420 nsecs_t mBootTime; 421 bool mGpuToCpuSupported; 422 sp<EventThread> mEventThread; 423 GLint mMaxViewportDims[2]; 424 GLint mMaxTextureSize; 425 EGLContext mEGLContext; 426 EGLConfig mEGLConfig; 427 EGLDisplay mEGLDisplay; 428 EGLint mEGLNativeVisualId; 429 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES]; 430 431 // Can only accessed from the main thread, these members 432 // don't need synchronization 433 State mDrawingState; 434 bool mVisibleRegionsDirty; 435 bool mHwWorkListDirty; 436 bool mAnimCompositionPending; 437 438 // this may only be written from the main thread with mStateLock held 439 // it may be read from other threads with mStateLock held 440 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 441 442 // don't use a lock for these, we don't care 443 int mDebugRegion; 444 int mDebugDDMS; 445 int mDebugDisableHWC; 446 int mDebugDisableTransformHint; 447 volatile nsecs_t mDebugInSwapBuffers; 448 nsecs_t mLastSwapBufferTime; 449 volatile nsecs_t mDebugInTransaction; 450 nsecs_t mLastTransactionTime; 451 bool mBootFinished; 452 453 // these are thread safe 454 mutable MessageQueue mEventQueue; 455 mutable Barrier mReadyToRunBarrier; 456 FrameTracker mAnimFrameTracker; 457 458 // protected by mDestroyedLayerLock; 459 mutable Mutex mDestroyedLayerLock; 460 Vector<Layer const *> mDestroyedLayers; 461 462 /* ------------------------------------------------------------------------ 463 * Feature prototyping 464 */ 465 466 sp<IBinder> mExtDisplayToken; 467 }; 468 469 // --------------------------------------------------------------------------- 470 }; // namespace android 471 472 #endif // ANDROID_SURFACE_FLINGER_H 473