SurfaceFlinger.h revision 5f20e2d4462da3471f59152b32cd8640fa4a21da
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 void handleRefresh(); 252 void handleRepaint(const sp<const DisplayDevice>& hw, const Region& dirtyRegion); 253 254 /* ------------------------------------------------------------------------ 255 * Transactions 256 */ 257 uint32_t getTransactionFlags(uint32_t flags); 258 uint32_t peekTransactionFlags(uint32_t flags); 259 uint32_t setTransactionFlags(uint32_t flags); 260 void commitTransaction(); 261 uint32_t setClientStateLocked(const sp<Client>& client, 262 const layer_state_t& s); 263 uint32_t setDisplayStateLocked(const DisplayState& s); 264 265 /* ------------------------------------------------------------------------ 266 * Layer management 267 */ 268 sp<ISurface> createLayer(ISurfaceComposerClient::surface_data_t* params, 269 const String8& name, const sp<Client>& client, DisplayID display, 270 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 271 272 sp<Layer> createNormalLayer(const sp<Client>& client, DisplayID display, 273 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format); 274 275 sp<LayerDim> createDimLayer(const sp<Client>& client, DisplayID display, 276 uint32_t w, uint32_t h, uint32_t flags); 277 278 sp<LayerScreenshot> createScreenshotLayer(const sp<Client>& client, 279 DisplayID display, uint32_t w, uint32_t h, uint32_t flags); 280 281 // called in response to the window-manager calling 282 // ISurfaceComposerClient::destroySurface() 283 // The specified layer is first placed in a purgatory list 284 // until all references from the client are released. 285 status_t onLayerRemoved(const sp<Client>& client, SurfaceID sid); 286 287 // called when all clients have released all their references to 288 // this layer meaning it is entirely safe to destroy all 289 // resources associated to this layer. 290 status_t onLayerDestroyed(const wp<LayerBaseClient>& layer); 291 292 // remove a layer from SurfaceFlinger immediately 293 status_t removeLayer(const sp<LayerBase>& layer); 294 295 // add a layer to SurfaceFlinger 296 ssize_t addClientLayer(const sp<Client>& client, 297 const sp<LayerBaseClient>& lbc); 298 299 status_t removeLayer_l(const sp<LayerBase>& layer); 300 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 301 302 /* ------------------------------------------------------------------------ 303 * Boot animation, on/off animations and screen capture 304 */ 305 306 void startBootAnim(); 307 308 status_t captureScreenImplLocked(DisplayID dpy, sp<IMemoryHeap>* heap, 309 uint32_t* width, uint32_t* height, PixelFormat* format, 310 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 311 uint32_t maxLayerZ); 312 313 /* ------------------------------------------------------------------------ 314 * EGL 315 */ 316 static status_t selectConfigForPixelFormat(EGLDisplay dpy, 317 EGLint const* attrs, PixelFormat format, EGLConfig* outConfig); 318 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 319 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 320 void initializeGL(EGLDisplay display, EGLSurface surface); 321 uint32_t getMaxTextureSize() const; 322 uint32_t getMaxViewportDims() const; 323 324 /* ------------------------------------------------------------------------ 325 * Display and layer stack management 326 */ 327 sp<const DisplayDevice> getDisplayDevice(DisplayID dpy) const { 328 return mDisplays.valueFor(dpy); 329 } 330 const sp<DisplayDevice>& getDisplayDevice(DisplayID dpy) { 331 return mDisplays.valueFor(dpy); 332 } 333 334 // mark a region of a layer stack dirty. this updates the dirty 335 // region of all screens presenting this layer stack. 336 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 337 338 /* ------------------------------------------------------------------------ 339 * H/W composer 340 */ 341 342 HWComposer& getHwComposer() const { return *mHwc; } 343 344 /* ------------------------------------------------------------------------ 345 * Compositing 346 */ 347 void invalidateHwcGeometry(); 348 void computeVisibleRegions(const LayerVector& currentLayers, 349 uint32_t layerStack, 350 Region& dirtyRegion, Region& opaqueRegion); 351 void postFramebuffer(); 352 void composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty); 353 void drawWormhole(const Region& region) const; 354 GLuint getProtectedTexName() const { 355 return mProtectedTexName; 356 } 357 358 /* ------------------------------------------------------------------------ 359 * Debugging & dumpsys 360 */ 361 void debugFlashRegions(const sp<const DisplayDevice>& hw, const Region& dirtyReg); 362 void listLayersLocked(const Vector<String16>& args, size_t& index, 363 String8& result, char* buffer, size_t SIZE) const; 364 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 365 String8& result, char* buffer, size_t SIZE) const; 366 void clearStatsLocked(const Vector<String16>& args, size_t& index, 367 String8& result, char* buffer, size_t SIZE) const; 368 void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const; 369 370 /* ------------------------------------------------------------------------ 371 * Attributes 372 */ 373 374 // access must be protected by mStateLock 375 mutable Mutex mStateLock; 376 State mCurrentState; 377 volatile int32_t mTransactionFlags; 378 Condition mTransactionCV; 379 SortedVector<sp<LayerBase> > mLayerPurgatory; 380 bool mTransationPending; 381 Vector<sp<LayerBase> > mLayersPendingRemoval; 382 383 // protected by mStateLock (but we could use another lock) 384 bool mLayersRemoved; 385 386 // access must be protected by mInvalidateLock 387 volatile int32_t mRepaintEverything; 388 389 // constant members (no synchronization needed for access) 390 HWComposer* mHwc; 391 GLuint mProtectedTexName; 392 nsecs_t mBootTime; 393 sp<EventThread> mEventThread; 394 GLint mMaxViewportDims[2]; 395 GLint mMaxTextureSize; 396 EGLContext mEGLContext; 397 EGLConfig mEGLConfig; 398 EGLDisplay mEGLDisplay; 399 sp<IBinder> mDefaultDisplays[DisplayDevice::DISPLAY_ID_COUNT]; 400 401 // Can only accessed from the main thread, these members 402 // don't need synchronization 403 State mDrawingState; 404 bool mVisibleRegionsDirty; 405 bool mHwWorkListDirty; 406 DefaultKeyedVector<int32_t, sp<DisplayDevice> > mDisplays; 407 408 // don't use a lock for these, we don't care 409 int mDebugRegion; 410 int mDebugDDMS; 411 int mDebugDisableHWC; 412 int mDebugDisableTransformHint; 413 volatile nsecs_t mDebugInSwapBuffers; 414 nsecs_t mLastSwapBufferTime; 415 volatile nsecs_t mDebugInTransaction; 416 nsecs_t mLastTransactionTime; 417 bool mBootFinished; 418 419 // these are thread safe 420 mutable MessageQueue mEventQueue; 421 mutable Barrier mReadyToRunBarrier; 422 423 // protected by mDestroyedLayerLock; 424 mutable Mutex mDestroyedLayerLock; 425 Vector<LayerBase const *> mDestroyedLayers; 426 427 /* ------------------------------------------------------------------------ 428 * Feature prototyping 429 */ 430 431 sp<IBinder> mExtDisplayToken; 432}; 433 434// --------------------------------------------------------------------------- 435}; // namespace android 436 437#endif // ANDROID_SURFACE_FLINGER_H 438