SurfaceFlinger.h revision 8b33f032327f8de0dcc0e6d0d43ed80f834b51f6
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 <private/gui/LayerState.h> 45 46#include "Barrier.h" 47#include "MessageQueue.h" 48 49namespace android { 50 51// --------------------------------------------------------------------------- 52 53class Client; 54class DisplayEventConnection; 55class DisplayHardware; 56class EventThread; 57class Layer; 58class LayerBase; 59class LayerBaseClient; 60class LayerDim; 61class LayerScreenshot; 62class SurfaceTextureClient; 63struct surface_flinger_cblk_t; 64 65// --------------------------------------------------------------------------- 66 67class GraphicBufferAlloc : public BnGraphicBufferAlloc { 68public: 69 GraphicBufferAlloc(); 70 virtual ~GraphicBufferAlloc(); 71 virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h, 72 PixelFormat format, uint32_t usage, status_t* error); 73}; 74 75// --------------------------------------------------------------------------- 76 77enum { 78 eTransactionNeeded = 0x01, eTraversalNeeded = 0x02 79}; 80 81class SurfaceFlinger : public BinderService<SurfaceFlinger>, 82 public BnSurfaceComposer, 83 private IBinder::DeathRecipient, 84 private Thread 85{ 86public: 87 static char const* getServiceName() { 88 return "SurfaceFlinger"; 89 } 90 91 SurfaceFlinger(); 92 93 // post an asynchronous message to the main thread 94 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, 95 uint32_t flags = 0); 96 97 // post a synchronous message to the main thread 98 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, 99 uint32_t flags = 0); 100 101 // force full composition on all displays 102 void repaintEverything(); 103 104 // renders content on given display to a texture. thread-safe version. 105 status_t renderScreenToTexture(DisplayID dpy, GLuint* textureName, 106 GLfloat* uOut, GLfloat* vOut); 107 108 // renders content on given display to a texture, w/o acquiring main lock 109 status_t renderScreenToTextureLocked(DisplayID dpy, GLuint* textureName, 110 GLfloat* uOut, GLfloat* vOut); 111 112 // returns the default Display 113 const DisplayHardware& getDefaultDisplayHardware() const { 114 return getDisplayHardware(0); 115 } 116 117 // called on the main thread by MessageQueue when an internal message 118 // is received 119 // TODO: this should be made accessible only to MessageQueue 120 void onMessageReceived(int32_t what); 121 122 // utility function to delete a texture on the main thread 123 void deleteTextureAsync(GLuint texture); 124 125private: 126 friend class Client; 127 friend class DisplayEventConnection; 128 friend class LayerBase; 129 friend class LayerBaseClient; 130 friend class Layer; 131 132 // We're reference counted, never destroy SurfaceFlinger directly 133 virtual ~SurfaceFlinger(); 134 135 /* ------------------------------------------------------------------------ 136 * Internal data structures 137 */ 138 139 class LayerVector : public SortedVector<sp<LayerBase> > { 140 public: 141 LayerVector(); 142 LayerVector(const LayerVector& rhs); 143 virtual int do_compare(const void* lhs, const void* rhs) const; 144 }; 145 146 struct State { 147 State(); 148 LayerVector layersSortedByZ; 149 uint8_t orientation; 150 uint8_t orientationFlags; 151 }; 152 153 /* ------------------------------------------------------------------------ 154 * IBinder interface 155 */ 156 virtual status_t onTransact(uint32_t code, const Parcel& data, 157 Parcel* reply, uint32_t flags); 158 virtual status_t dump(int fd, const Vector<String16>& args); 159 160 /* ------------------------------------------------------------------------ 161 * ISurfaceComposer interface 162 */ 163 virtual sp<ISurfaceComposerClient> createConnection(); 164 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 165 virtual sp<IMemoryHeap> getCblk() const; 166 virtual void bootFinished(); 167 virtual void setTransactionState(const Vector<ComposerState>& state, 168 const Vector<DisplayState>& displays, uint32_t flags); 169 virtual bool authenticateSurfaceTexture( 170 const sp<ISurfaceTexture>& surface) const; 171 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 172 virtual status_t captureScreen(DisplayID dpy, sp<IMemoryHeap>* heap, 173 uint32_t* width, uint32_t* height, PixelFormat* format, 174 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 175 uint32_t maxLayerZ); 176 virtual status_t turnElectronBeamOff(int32_t mode); 177 virtual status_t turnElectronBeamOn(int32_t mode); 178 // called when screen needs to turn off 179 virtual void blank(); 180 // called when screen is turning back on 181 virtual void unblank(); 182 virtual void connectDisplay(const sp<ISurfaceTexture> display); 183 184 /* ------------------------------------------------------------------------ 185 * DeathRecipient interface 186 */ 187 virtual void binderDied(const wp<IBinder>& who); 188 189 /* ------------------------------------------------------------------------ 190 * Thread interface 191 */ 192 virtual bool threadLoop(); 193 virtual status_t readyToRun(); 194 virtual void onFirstRef(); 195 196 /* ------------------------------------------------------------------------ 197 * Message handling 198 */ 199 void waitForEvent(); 200 void signalTransaction(); 201 void signalLayerUpdate(); 202 void signalRefresh(); 203 204 // called on the main thread in response to screenReleased() 205 void onScreenReleased(); 206 // called on the main thread in response to screenAcquired() 207 void onScreenAcquired(); 208 209 void handleMessageTransaction(); 210 void handleMessageInvalidate(); 211 void handleMessageRefresh(); 212 213 Region handleTransaction(uint32_t transactionFlags); 214 Region handleTransactionLocked(uint32_t transactionFlags); 215 216 /* handlePageFilp: this is were we latch a new buffer 217 * if available and compute the dirty region. 218 * The return value is the dirty region expressed in the 219 * window manager's coordinate space (or the layer's state 220 * space, which is the same thing), in particular the dirty 221 * region is independent from a specific display's orientation. 222 */ 223 Region handlePageFlip(); 224 225 void handleRefresh(); 226 void handleWorkList(const DisplayHardware& hw); 227 void handleRepaint(const DisplayHardware& hw); 228 229 /* ------------------------------------------------------------------------ 230 * Transactions 231 */ 232 uint32_t getTransactionFlags(uint32_t flags); 233 uint32_t peekTransactionFlags(uint32_t flags); 234 uint32_t setTransactionFlags(uint32_t flags); 235 void commitTransaction(); 236 uint32_t setClientStateLocked(const sp<Client>& client, 237 const layer_state_t& s); 238 239 /* ------------------------------------------------------------------------ 240 * Layer management 241 */ 242 sp<ISurface> createLayer(ISurfaceComposerClient::surface_data_t* params, 243 const String8& name, const sp<Client>& client, DisplayID display, 244 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 245 246 sp<Layer> createNormalLayer(const sp<Client>& client, DisplayID display, 247 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format); 248 249 sp<LayerDim> createDimLayer(const sp<Client>& client, DisplayID display, 250 uint32_t w, uint32_t h, uint32_t flags); 251 252 sp<LayerScreenshot> createScreenshotLayer(const sp<Client>& client, 253 DisplayID display, uint32_t w, uint32_t h, uint32_t flags); 254 255 // called in response to the window-manager calling 256 // ISurfaceComposerClient::destroySurface() 257 // The specified layer is first placed in a purgatory list 258 // until all references from the client are released. 259 status_t onLayerRemoved(const sp<Client>& client, SurfaceID sid); 260 261 // called when all clients have released all their references to 262 // this layer meaning it is entirely safe to destroy all 263 // resources associated to this layer. 264 status_t onLayerDestroyed(const wp<LayerBaseClient>& layer); 265 266 // remove a layer from SurfaceFlinger immediately 267 status_t removeLayer(const sp<LayerBase>& layer); 268 269 // add a layer to SurfaceFlinger 270 ssize_t addClientLayer(const sp<Client>& client, 271 const sp<LayerBaseClient>& lbc); 272 273 status_t removeLayer_l(const sp<LayerBase>& layer); 274 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 275 276 /* ------------------------------------------------------------------------ 277 * Boot animation, on/off animations and screen capture 278 */ 279 280 void startBootAnim(); 281 282 status_t captureScreenImplLocked(DisplayID dpy, sp<IMemoryHeap>* heap, 283 uint32_t* width, uint32_t* height, PixelFormat* format, 284 uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ, 285 uint32_t maxLayerZ); 286 287 status_t turnElectronBeamOffImplLocked(int32_t mode); 288 status_t turnElectronBeamOnImplLocked(int32_t mode); 289 status_t electronBeamOffAnimationImplLocked(); 290 status_t electronBeamOnAnimationImplLocked(); 291 292 /* ------------------------------------------------------------------------ 293 * EGL 294 */ 295 static status_t selectConfigForPixelFormat(EGLDisplay dpy, 296 EGLint const* attrs, PixelFormat format, EGLConfig* outConfig); 297 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId); 298 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config); 299 void initializeGL(EGLDisplay display, EGLSurface surface); 300 uint32_t getMaxTextureSize() const; 301 uint32_t getMaxViewportDims() const; 302 303 /* ------------------------------------------------------------------------ 304 * Display management 305 */ 306 const DisplayHardware& getDisplayHardware(DisplayID dpy) const { 307 return *mDisplayHardwares[dpy]; 308 } 309 310 /* ------------------------------------------------------------------------ 311 * Compositing 312 */ 313 void invalidateHwcGeometry(); 314 void computeVisibleRegions(const LayerVector& currentLayers, 315 Region& dirtyRegion, Region& wormholeRegion); 316 void postFramebuffer(); 317 void setupHardwareComposer(const DisplayHardware& hw); 318 void composeSurfaces(const DisplayHardware& hw, const Region& dirty); 319 void setInvalidateRegion(const Region& reg); 320 Region getAndClearInvalidateRegion(); 321 void drawWormhole() const; 322 GLuint getProtectedTexName() const { 323 return mProtectedTexName; 324 } 325 326 /* ------------------------------------------------------------------------ 327 * Debugging & dumpsys 328 */ 329 void debugFlashRegions(const DisplayHardware& hw); 330 void listLayersLocked(const Vector<String16>& args, size_t& index, 331 String8& result, char* buffer, size_t SIZE) const; 332 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 333 String8& result, char* buffer, size_t SIZE) const; 334 void clearStatsLocked(const Vector<String16>& args, size_t& index, 335 String8& result, char* buffer, size_t SIZE) const; 336 void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const; 337 338 /* ------------------------------------------------------------------------ 339 * Attributes 340 */ 341 342 // access must be protected by mStateLock 343 mutable Mutex mStateLock; 344 State mCurrentState; 345 volatile int32_t mTransactionFlags; 346 Condition mTransactionCV; 347 SortedVector<sp<LayerBase> > mLayerPurgatory; 348 bool mTransationPending; 349 Vector<sp<LayerBase> > mLayersPendingRemoval; 350 351 // protected by mStateLock (but we could use another lock) 352 DisplayHardware* mDisplayHardwares[1]; 353 bool mLayersRemoved; 354 355 // access must be protected by mInvalidateLock 356 mutable Mutex mInvalidateLock; 357 Region mInvalidateRegion; 358 359 // constant members (no synchronization needed for access) 360 sp<IMemoryHeap> mServerHeap; 361 surface_flinger_cblk_t* mServerCblk; 362 GLuint mWormholeTexName; 363 GLuint mProtectedTexName; 364 nsecs_t mBootTime; 365 sp<EventThread> mEventThread; 366 GLint mMaxViewportDims[2]; 367 GLint mMaxTextureSize; 368 EGLContext mEGLContext; 369 EGLConfig mEGLConfig; 370 371 // Can only accessed from the main thread, these members 372 // don't need synchronization 373 State mDrawingState; 374 Region mDirtyRegion; 375 Region mDirtyRegionRemovedLayer; 376 Region mSwapRegion; 377 Region mWormholeRegion; 378 bool mVisibleRegionsDirty; 379 bool mHwWorkListDirty; 380 int32_t mElectronBeamAnimationMode; 381 382 // don't use a lock for these, we don't care 383 int mDebugRegion; 384 int mDebugDDMS; 385 int mDebugDisableHWC; 386 int mDebugDisableTransformHint; 387 volatile nsecs_t mDebugInSwapBuffers; 388 nsecs_t mLastSwapBufferTime; 389 volatile nsecs_t mDebugInTransaction; 390 nsecs_t mLastTransactionTime; 391 bool mBootFinished; 392 393 // these are thread safe 394 mutable MessageQueue mEventQueue; 395 mutable Barrier mReadyToRunBarrier; 396 397 // protected by mDestroyedLayerLock; 398 mutable Mutex mDestroyedLayerLock; 399 Vector<LayerBase const *> mDestroyedLayers; 400 401 /* ------------------------------------------------------------------------ 402 * Feature prototyping 403 */ 404 405 EGLSurface getExternalDisplaySurface() const; 406 sp<SurfaceTextureClient> mExternalDisplayNativeWindow; 407 EGLSurface mExternalDisplaySurface; 408public: 409 surface_flinger_cblk_t* getControlBlock() const; 410}; 411 412// --------------------------------------------------------------------------- 413}; // namespace android 414 415#endif // ANDROID_SURFACE_FLINGER_H 416