SurfaceFlinger.h revision 81cd5d3b94d21253a0be925f4ae58cc7f4afeef7
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 "MessageQueue.h" 49#include "DisplayDevice.h" 50 51#include "DisplayHardware/HWComposer.h" 52 53namespace android { 54 55// --------------------------------------------------------------------------- 56 57class Client; 58class DisplayEventConnection; 59class EventThread; 60class IGraphicBufferAlloc; 61class Layer; 62class LayerBase; 63class LayerBaseClient; 64class LayerDim; 65class LayerScreenshot; 66class SurfaceTextureClient; 67 68// --------------------------------------------------------------------------- 69 70enum { 71 eTransactionNeeded = 0x01, 72 eTraversalNeeded = 0x02, 73 eDisplayTransactionNeeded = 0x04, 74 eTransactionMask = 0x07 75}; 76 77class SurfaceFlinger : public BinderService<SurfaceFlinger>, 78 public BnSurfaceComposer, 79 private IBinder::DeathRecipient, 80 private Thread, 81 private HWComposer::EventHandler 82{ 83public: 84 static char const* getServiceName() { 85 return "SurfaceFlinger"; 86 } 87 88 SurfaceFlinger(); 89 90 enum { 91 EVENT_VSYNC = HWC_EVENT_VSYNC 92 }; 93 94 // post an asynchronous message to the main thread 95 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, 96 uint32_t flags = 0); 97 98 // post a synchronous message to the main thread 99 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, 100 uint32_t flags = 0); 101 102 // force full composition on all displays 103 void repaintEverything(); 104 105 // renders content on given display to a texture. thread-safe version. 106 status_t renderScreenToTexture(uint32_t layerStack, GLuint* textureName, 107 GLfloat* uOut, GLfloat* vOut); 108 109 // renders content on given display to a texture, w/o acquiring main lock 110 status_t renderScreenToTextureLocked(uint32_t layerStack, GLuint* textureName, 111 GLfloat* uOut, GLfloat* vOut); 112 113 // returns the default Display 114 sp<const DisplayDevice> getDefaultDisplayDevice() const { 115 return getDisplayDevice(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY]); 116 } 117 118 // utility function to delete a texture on the main thread 119 void deleteTextureAsync(GLuint texture); 120 121 // allocate a h/w composer display id 122 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type); 123 124 // enable/disable h/w composer event 125 // TODO: this should be made accessible only to EventThread 126 void eventControl(int disp, int event, int enabled); 127 128 // called on the main thread by MessageQueue when an internal message 129 // is received 130 // TODO: this should be made accessible only to MessageQueue 131 void onMessageReceived(int32_t what); 132 133private: 134 friend class Client; 135 friend class DisplayEventConnection; 136 friend class LayerBase; 137 friend class LayerBaseClient; 138 friend class Layer; 139 140 // We're reference counted, never destroy SurfaceFlinger directly 141 virtual ~SurfaceFlinger(); 142 143 /* ------------------------------------------------------------------------ 144 * Internal data structures 145 */ 146 147 class LayerVector : public SortedVector<sp<LayerBase> > { 148 public: 149 LayerVector(); 150 LayerVector(const LayerVector& rhs); 151 virtual int do_compare(const void* lhs, const void* rhs) const; 152 }; 153 154 struct DisplayDeviceState { 155 DisplayDeviceState(); 156 DisplayDeviceState(DisplayDevice::DisplayType type); 157 bool isValid() const { return type >= 0; } 158 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; } 159 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; } 160 DisplayDevice::DisplayType type; 161 sp<ISurfaceTexture> surface; 162 uint32_t layerStack; 163 Rect viewport; 164 Rect frame; 165 uint8_t orientation; 166 String8 displayName; 167 }; 168 169 struct State { 170 LayerVector layersSortedByZ; 171 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 172 }; 173 174 /* ------------------------------------------------------------------------ 175 * IBinder interface 176 */ 177 virtual status_t onTransact(uint32_t code, const Parcel& data, 178 Parcel* reply, uint32_t flags); 179 virtual status_t dump(int fd, const Vector<String16>& args); 180 181 /* ------------------------------------------------------------------------ 182 * ISurfaceComposer interface 183 */ 184 virtual sp<ISurfaceComposerClient> createConnection(); 185 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 186 virtual sp<IBinder> createDisplay(const String8& displayName); 187 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 188 virtual void setTransactionState(const Vector<ComposerState>& state, 189 const Vector<DisplayState>& displays, uint32_t flags); 190 virtual void bootFinished(); 191 virtual bool authenticateSurfaceTexture( 192 const sp<ISurfaceTexture>& surface) const; 193 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 194 virtual status_t captureScreen(const sp<IBinder>& display, sp<IMemoryHeap>* heap, 195 uint32_t* width, uint32_t* height, PixelFormat* format, 196 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 197 uint32_t maxLayerZ); 198 // called when screen needs to turn off 199 virtual void blank(const sp<IBinder>& display); 200 // called when screen is turning back on 201 virtual void unblank(const sp<IBinder>& display); 202 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info); 203 virtual void connectDisplay(const sp<ISurfaceTexture>& display); 204 205 /* ------------------------------------------------------------------------ 206 * DeathRecipient interface 207 */ 208 virtual void binderDied(const wp<IBinder>& who); 209 210 /* ------------------------------------------------------------------------ 211 * Thread interface 212 */ 213 virtual bool threadLoop(); 214 virtual status_t readyToRun(); 215 virtual void onFirstRef(); 216 217 /* ------------------------------------------------------------------------ 218 * HWComposer::EventHandler interface 219 */ 220 virtual void onVSyncReceived(int type, nsecs_t timestamp); 221 virtual void onHotplugReceived(int disp, bool connected); 222 223 /* ------------------------------------------------------------------------ 224 * Message handling 225 */ 226 void waitForEvent(); 227 void signalTransaction(); 228 void signalLayerUpdate(); 229 void signalRefresh(); 230 231 // called on the main thread in response to initializeDisplays() 232 void onInitializeDisplays(); 233 // called on the main thread in response to blank() 234 void onScreenReleased(const sp<const DisplayDevice>& hw); 235 // called on the main thread in response to unblank() 236 void onScreenAcquired(const sp<const DisplayDevice>& hw); 237 238 void handleMessageTransaction(); 239 void handleMessageInvalidate(); 240 void handleMessageRefresh(); 241 242 void handleTransaction(uint32_t transactionFlags); 243 void handleTransactionLocked(uint32_t transactionFlags); 244 245 /* handlePageFilp: this is were we latch a new buffer 246 * if available and compute the dirty region. 247 */ 248 void handlePageFlip(); 249 250 /* ------------------------------------------------------------------------ 251 * Transactions 252 */ 253 uint32_t getTransactionFlags(uint32_t flags); 254 uint32_t peekTransactionFlags(uint32_t flags); 255 uint32_t setTransactionFlags(uint32_t flags); 256 void commitTransaction(); 257 uint32_t setClientStateLocked(const sp<Client>& client, 258 const layer_state_t& s); 259 uint32_t setDisplayStateLocked(const DisplayState& s); 260 261 /* ------------------------------------------------------------------------ 262 * Layer management 263 */ 264 sp<ISurface> createLayer(ISurfaceComposerClient::surface_data_t* params, 265 const String8& name, const sp<Client>& client, 266 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 267 268 sp<Layer> createNormalLayer(const sp<Client>& client, 269 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format); 270 271 sp<LayerDim> createDimLayer(const sp<Client>& client, 272 uint32_t w, uint32_t h, uint32_t flags); 273 274 sp<LayerScreenshot> createScreenshotLayer(const sp<Client>& client, 275 uint32_t w, uint32_t h, uint32_t flags); 276 277 // called in response to the window-manager calling 278 // ISurfaceComposerClient::destroySurface() 279 // The specified layer is first placed in a purgatory list 280 // until all references from the client are released. 281 status_t onLayerRemoved(const sp<Client>& client, SurfaceID sid); 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<LayerBaseClient>& layer); 287 288 // remove a layer from SurfaceFlinger immediately 289 status_t removeLayer(const sp<LayerBase>& layer); 290 291 // add a layer to SurfaceFlinger 292 ssize_t addClientLayer(const sp<Client>& client, 293 const sp<LayerBaseClient>& lbc); 294 295 status_t removeLayer_l(const sp<LayerBase>& layer); 296 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 297 298 /* ------------------------------------------------------------------------ 299 * Boot animation, on/off animations and screen capture 300 */ 301 302 void startBootAnim(); 303 304 status_t captureScreenImplLocked(const sp<IBinder>& display, sp<IMemoryHeap>* heap, 305 uint32_t* width, uint32_t* height, PixelFormat* format, 306 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 307 uint32_t maxLayerZ); 308 309 /* ------------------------------------------------------------------------ 310 * EGL 311 */ 312 static status_t selectConfigForAttribute(EGLDisplay dpy, 313 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig); 314 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 315 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 316 void initializeGL(EGLDisplay display); 317 uint32_t getMaxTextureSize() const; 318 uint32_t getMaxViewportDims() const; 319 320 /* ------------------------------------------------------------------------ 321 * Display and layer stack management 322 */ 323 // called when starting, or restarting after system_server death 324 void initializeDisplays(); 325 326 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 327 return mDisplays.valueFor(dpy); 328 } 329 const sp<DisplayDevice>& getDisplayDevice(const wp<IBinder>& dpy) { 330 return mDisplays.valueFor(dpy); 331 } 332 333 // mark a region of a layer stack dirty. this updates the dirty 334 // region of all screens presenting this layer stack. 335 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 336 337 /* ------------------------------------------------------------------------ 338 * H/W composer 339 */ 340 341 HWComposer& getHwComposer() const { return *mHwc; } 342 343 /* ------------------------------------------------------------------------ 344 * Compositing 345 */ 346 void invalidateHwcGeometry(); 347 static void computeVisibleRegions( 348 const LayerVector& currentLayers, uint32_t layerStack, 349 Region& dirtyRegion, Region& opaqueRegion); 350 351 void preComposition(); 352 void postComposition(); 353 void rebuildLayerStacks(); 354 void setUpHWComposer(); 355 void doComposition(); 356 void doDebugFlashRegions(); 357 void doDisplayComposition(const sp<const DisplayDevice>& hw, 358 const Region& dirtyRegion); 359 void doComposeSurfaces(const sp<const DisplayDevice>& hw, 360 const Region& dirty); 361 362 void postFramebuffer(); 363 void drawWormhole(const sp<const DisplayDevice>& hw, 364 const Region& region) const; 365 GLuint getProtectedTexName() const { 366 return mProtectedTexName; 367 } 368 369 /* ------------------------------------------------------------------------ 370 * Display management 371 */ 372 373 374 /* ------------------------------------------------------------------------ 375 * Debugging & dumpsys 376 */ 377 void listLayersLocked(const Vector<String16>& args, size_t& index, 378 String8& result, char* buffer, size_t SIZE) const; 379 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 380 String8& result, char* buffer, size_t SIZE) const; 381 void clearStatsLocked(const Vector<String16>& args, size_t& index, 382 String8& result, char* buffer, size_t SIZE) const; 383 void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const; 384 bool startDdmConnection(); 385 static void appendSfConfigString(String8& result); 386 387 /* ------------------------------------------------------------------------ 388 * Attributes 389 */ 390 391 // access must be protected by mStateLock 392 mutable Mutex mStateLock; 393 State mCurrentState; 394 volatile int32_t mTransactionFlags; 395 Condition mTransactionCV; 396 SortedVector<sp<LayerBase> > mLayerPurgatory; 397 bool mTransationPending; 398 Vector<sp<LayerBase> > mLayersPendingRemoval; 399 400 // protected by mStateLock (but we could use another lock) 401 bool mLayersRemoved; 402 403 // access must be protected by mInvalidateLock 404 volatile int32_t mRepaintEverything; 405 406 // constant members (no synchronization needed for access) 407 HWComposer* mHwc; 408 GLuint mProtectedTexName; 409 nsecs_t mBootTime; 410 sp<EventThread> mEventThread; 411 GLint mMaxViewportDims[2]; 412 GLint mMaxTextureSize; 413 EGLContext mEGLContext; 414 EGLConfig mEGLConfig; 415 EGLDisplay mEGLDisplay; 416 sp<IBinder> mDefaultDisplays[DisplayDevice::NUM_DISPLAY_TYPES]; 417 418 // Can only accessed from the main thread, these members 419 // don't need synchronization 420 State mDrawingState; 421 bool mVisibleRegionsDirty; 422 bool mHwWorkListDirty; 423 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 424 425 // don't use a lock for these, we don't care 426 int mDebugRegion; 427 int mDebugDDMS; 428 int mDebugDisableHWC; 429 int mDebugDisableTransformHint; 430 volatile nsecs_t mDebugInSwapBuffers; 431 nsecs_t mLastSwapBufferTime; 432 volatile nsecs_t mDebugInTransaction; 433 nsecs_t mLastTransactionTime; 434 bool mBootFinished; 435 436 // these are thread safe 437 mutable MessageQueue mEventQueue; 438 mutable Barrier mReadyToRunBarrier; 439 440 // protected by mDestroyedLayerLock; 441 mutable Mutex mDestroyedLayerLock; 442 Vector<LayerBase const *> mDestroyedLayers; 443 444 /* ------------------------------------------------------------------------ 445 * Feature prototyping 446 */ 447 448 sp<IBinder> mExtDisplayToken; 449}; 450 451// --------------------------------------------------------------------------- 452}; // namespace android 453 454#endif // ANDROID_SURFACE_FLINGER_H 455