SurfaceFlinger.h revision cde87a3b9d3f8dc15232d927b56ee9e5e520f58d
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 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; } 170 DisplayDevice::DisplayType type; 171 sp<ISurfaceTexture> surface; 172 uint32_t layerStack; 173 Rect viewport; 174 Rect frame; 175 uint8_t orientation; 176 }; 177 178 struct State { 179 LayerVector layersSortedByZ; 180 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 181 }; 182 183 /* ------------------------------------------------------------------------ 184 * IBinder interface 185 */ 186 virtual status_t onTransact(uint32_t code, const Parcel& data, 187 Parcel* reply, uint32_t flags); 188 virtual status_t dump(int fd, const Vector<String16>& args); 189 190 /* ------------------------------------------------------------------------ 191 * ISurfaceComposer interface 192 */ 193 virtual sp<ISurfaceComposerClient> createConnection(); 194 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 195 virtual sp<IBinder> createDisplay(); 196 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 197 virtual void setTransactionState(const Vector<ComposerState>& state, 198 const Vector<DisplayState>& displays, uint32_t flags); 199 virtual void bootFinished(); 200 virtual bool authenticateSurfaceTexture( 201 const sp<ISurfaceTexture>& surface) const; 202 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 203 virtual status_t captureScreen(const sp<IBinder>& display, sp<IMemoryHeap>* heap, 204 uint32_t* width, uint32_t* height, PixelFormat* format, 205 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 206 uint32_t maxLayerZ); 207 // called when screen needs to turn off 208 virtual void blank(); 209 // called when screen is turning back on 210 virtual void unblank(); 211 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info); 212 virtual void connectDisplay(const sp<ISurfaceTexture>& display); 213 214 /* ------------------------------------------------------------------------ 215 * DeathRecipient interface 216 */ 217 virtual void binderDied(const wp<IBinder>& who); 218 219 /* ------------------------------------------------------------------------ 220 * Thread interface 221 */ 222 virtual bool threadLoop(); 223 virtual status_t readyToRun(); 224 virtual void onFirstRef(); 225 226 /* ------------------------------------------------------------------------ 227 * HWComposer::EventHandler interface 228 */ 229 virtual void onVSyncReceived(int type, nsecs_t timestamp); 230 231 /* ------------------------------------------------------------------------ 232 * Message handling 233 */ 234 void waitForEvent(); 235 void signalTransaction(); 236 void signalLayerUpdate(); 237 void signalRefresh(); 238 239 // called on the main thread in response to initializeDisplays() 240 void onInitializeDisplays(); 241 // called on the main thread in response to blank() 242 void onScreenReleased(const sp<const DisplayDevice>& hw); 243 // called on the main thread in response to unblank() 244 void onScreenAcquired(const sp<const DisplayDevice>& hw); 245 246 void handleMessageTransaction(); 247 void handleMessageInvalidate(); 248 void handleMessageRefresh(); 249 250 void handleTransaction(uint32_t transactionFlags); 251 void handleTransactionLocked(uint32_t transactionFlags); 252 253 /* handlePageFilp: this is were we latch a new buffer 254 * if available and compute the dirty region. 255 */ 256 void handlePageFlip(); 257 258 /* ------------------------------------------------------------------------ 259 * Transactions 260 */ 261 uint32_t getTransactionFlags(uint32_t flags); 262 uint32_t peekTransactionFlags(uint32_t flags); 263 uint32_t setTransactionFlags(uint32_t flags); 264 void commitTransaction(); 265 uint32_t setClientStateLocked(const sp<Client>& client, 266 const layer_state_t& s); 267 uint32_t setDisplayStateLocked(const DisplayState& s); 268 269 /* ------------------------------------------------------------------------ 270 * Layer management 271 */ 272 sp<ISurface> createLayer(ISurfaceComposerClient::surface_data_t* params, 273 const String8& name, const sp<Client>& client, 274 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 275 276 sp<Layer> createNormalLayer(const sp<Client>& client, 277 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format); 278 279 sp<LayerDim> createDimLayer(const sp<Client>& client, 280 uint32_t w, uint32_t h, uint32_t flags); 281 282 sp<LayerScreenshot> createScreenshotLayer(const sp<Client>& client, 283 uint32_t w, uint32_t h, uint32_t flags); 284 285 // called in response to the window-manager calling 286 // ISurfaceComposerClient::destroySurface() 287 // The specified layer is first placed in a purgatory list 288 // until all references from the client are released. 289 status_t onLayerRemoved(const sp<Client>& client, SurfaceID sid); 290 291 // called when all clients have released all their references to 292 // this layer meaning it is entirely safe to destroy all 293 // resources associated to this layer. 294 status_t onLayerDestroyed(const wp<LayerBaseClient>& layer); 295 296 // remove a layer from SurfaceFlinger immediately 297 status_t removeLayer(const sp<LayerBase>& layer); 298 299 // add a layer to SurfaceFlinger 300 ssize_t addClientLayer(const sp<Client>& client, 301 const sp<LayerBaseClient>& lbc); 302 303 status_t removeLayer_l(const sp<LayerBase>& layer); 304 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 305 306 /* ------------------------------------------------------------------------ 307 * Boot animation, on/off animations and screen capture 308 */ 309 310 void startBootAnim(); 311 312 status_t captureScreenImplLocked(const sp<IBinder>& display, sp<IMemoryHeap>* heap, 313 uint32_t* width, uint32_t* height, PixelFormat* format, 314 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 315 uint32_t maxLayerZ); 316 317 /* ------------------------------------------------------------------------ 318 * EGL 319 */ 320 static status_t selectConfigForPixelFormat(EGLDisplay dpy, 321 EGLint const* attrs, PixelFormat format, EGLConfig* outConfig); 322 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 323 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 324 void initializeGL(EGLDisplay display); 325 uint32_t getMaxTextureSize() const; 326 uint32_t getMaxViewportDims() const; 327 328 /* ------------------------------------------------------------------------ 329 * Display and layer stack management 330 */ 331 // called when starting, or restarting after system_server death 332 void initializeDisplays(); 333 334 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 335 return mDisplays.valueFor(dpy); 336 } 337 const sp<DisplayDevice>& getDisplayDevice(const wp<IBinder>& dpy) { 338 return mDisplays.valueFor(dpy); 339 } 340 341 // mark a region of a layer stack dirty. this updates the dirty 342 // region of all screens presenting this layer stack. 343 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 344 345 /* ------------------------------------------------------------------------ 346 * H/W composer 347 */ 348 349 HWComposer& getHwComposer() const { return *mHwc; } 350 351 /* ------------------------------------------------------------------------ 352 * Compositing 353 */ 354 void invalidateHwcGeometry(); 355 static void computeVisibleRegions( 356 const LayerVector& currentLayers, uint32_t layerStack, 357 Region& dirtyRegion, Region& opaqueRegion); 358 359 void preComposition(); 360 void postComposition(); 361 void rebuildLayerStacks(); 362 void setUpHWComposer(); 363 void doComposition(); 364 void doDebugFlashRegions(); 365 void doDisplayComposition(const sp<const DisplayDevice>& hw, 366 const Region& dirtyRegion); 367 void doComposeSurfaces(const sp<const DisplayDevice>& hw, 368 const Region& dirty); 369 370 void postFramebuffer(); 371 void drawWormhole(const sp<const DisplayDevice>& hw, 372 const Region& region) const; 373 GLuint getProtectedTexName() const { 374 return mProtectedTexName; 375 } 376 377 /* ------------------------------------------------------------------------ 378 * Display management 379 */ 380 381 382 /* ------------------------------------------------------------------------ 383 * Debugging & dumpsys 384 */ 385 void listLayersLocked(const Vector<String16>& args, size_t& index, 386 String8& result, char* buffer, size_t SIZE) const; 387 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 388 String8& result, char* buffer, size_t SIZE) const; 389 void clearStatsLocked(const Vector<String16>& args, size_t& index, 390 String8& result, char* buffer, size_t SIZE) const; 391 void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const; 392 bool startDdmConnection(); 393 394 /* ------------------------------------------------------------------------ 395 * Attributes 396 */ 397 398 // access must be protected by mStateLock 399 mutable Mutex mStateLock; 400 State mCurrentState; 401 volatile int32_t mTransactionFlags; 402 Condition mTransactionCV; 403 SortedVector<sp<LayerBase> > mLayerPurgatory; 404 bool mTransationPending; 405 Vector<sp<LayerBase> > mLayersPendingRemoval; 406 407 // protected by mStateLock (but we could use another lock) 408 bool mLayersRemoved; 409 410 // access must be protected by mInvalidateLock 411 volatile int32_t mRepaintEverything; 412 413 // constant members (no synchronization needed for access) 414 HWComposer* mHwc; 415 GLuint mProtectedTexName; 416 nsecs_t mBootTime; 417 sp<EventThread> mEventThread; 418 GLint mMaxViewportDims[2]; 419 GLint mMaxTextureSize; 420 EGLContext mEGLContext; 421 EGLConfig mEGLConfig; 422 EGLDisplay mEGLDisplay; 423 sp<IBinder> mDefaultDisplays[DisplayDevice::NUM_DISPLAY_TYPES]; 424 425 // Can only accessed from the main thread, these members 426 // don't need synchronization 427 State mDrawingState; 428 bool mVisibleRegionsDirty; 429 bool mHwWorkListDirty; 430 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 431 432 // don't use a lock for these, we don't care 433 int mDebugRegion; 434 int mDebugDDMS; 435 int mDebugDisableHWC; 436 int mDebugDisableTransformHint; 437 volatile nsecs_t mDebugInSwapBuffers; 438 nsecs_t mLastSwapBufferTime; 439 volatile nsecs_t mDebugInTransaction; 440 nsecs_t mLastTransactionTime; 441 bool mBootFinished; 442 443 // these are thread safe 444 mutable MessageQueue mEventQueue; 445 mutable Barrier mReadyToRunBarrier; 446 447 // protected by mDestroyedLayerLock; 448 mutable Mutex mDestroyedLayerLock; 449 Vector<LayerBase const *> mDestroyedLayers; 450 451 /* ------------------------------------------------------------------------ 452 * Feature prototyping 453 */ 454 455 sp<IBinder> mExtDisplayToken; 456}; 457 458// --------------------------------------------------------------------------- 459}; // namespace android 460 461#endif // ANDROID_SURFACE_FLINGER_H 462