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