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