SurfaceFlinger.h revision 090cb44b945bfe2711344a5331814bd6412ab437
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 <utils/Atomic.h> 24#include <utils/Errors.h> 25#include <utils/KeyedVector.h> 26#include <utils/RefBase.h> 27#include <utils/SortedVector.h> 28#include <utils/threads.h> 29 30#include <binder/BinderService.h> 31#include <binder/IMemory.h> 32 33#include <ui/PixelFormat.h> 34#include <surfaceflinger/IGraphicBufferAlloc.h> 35#include <surfaceflinger/ISurfaceComposer.h> 36#include <surfaceflinger/ISurfaceComposerClient.h> 37 38#include "Barrier.h" 39#include "Layer.h" 40 41#include "MessageQueue.h" 42 43namespace android { 44 45// --------------------------------------------------------------------------- 46 47class Client; 48class DisplayHardware; 49class Layer; 50class LayerDim; 51class LayerScreenshot; 52struct surface_flinger_cblk_t; 53 54#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) 55#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) 56 57// --------------------------------------------------------------------------- 58 59class Client : public BnSurfaceComposerClient 60{ 61public: 62 Client(const sp<SurfaceFlinger>& flinger); 63 ~Client(); 64 65 status_t initCheck() const; 66 67 // protected by SurfaceFlinger::mStateLock 68 size_t attachLayer(const sp<LayerBaseClient>& layer); 69 void detachLayer(const LayerBaseClient* layer); 70 sp<LayerBaseClient> getLayerUser(int32_t i) const; 71 72private: 73 // ISurfaceComposerClient interface 74 virtual sp<ISurface> createSurface( 75 surface_data_t* params, const String8& name, 76 DisplayID display, uint32_t w, uint32_t h,PixelFormat format, 77 uint32_t flags); 78 virtual status_t destroySurface(SurfaceID surfaceId); 79 virtual status_t onTransact( 80 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); 81 82 // constant 83 sp<SurfaceFlinger> mFlinger; 84 85 // protected by mLock 86 DefaultKeyedVector< size_t, wp<LayerBaseClient> > mLayers; 87 size_t mNameGenerator; 88 89 // thread-safe 90 mutable Mutex mLock; 91}; 92 93class GraphicBufferAlloc : public BnGraphicBufferAlloc 94{ 95public: 96 GraphicBufferAlloc(); 97 virtual ~GraphicBufferAlloc(); 98 virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h, 99 PixelFormat format, uint32_t usage, status_t* error); 100}; 101 102// --------------------------------------------------------------------------- 103 104class GraphicPlane 105{ 106public: 107 static status_t orientationToTransfrom(int orientation, int w, int h, 108 Transform* tr); 109 110 GraphicPlane(); 111 ~GraphicPlane(); 112 113 bool initialized() const; 114 115 void setDisplayHardware(DisplayHardware *); 116 status_t setOrientation(int orientation); 117 int getOrientation() const { return mOrientation; } 118 int getWidth() const; 119 int getHeight() const; 120 121 const DisplayHardware& displayHardware() const; 122 DisplayHardware& editDisplayHardware(); 123 const Transform& transform() const; 124 EGLDisplay getEGLDisplay() const; 125 126private: 127 GraphicPlane(const GraphicPlane&); 128 GraphicPlane operator = (const GraphicPlane&); 129 130 DisplayHardware* mHw; 131 Transform mGlobalTransform; 132 Transform mDisplayTransform; 133 int mOrientation; 134 float mDisplayWidth; 135 float mDisplayHeight; 136 int mWidth; 137 int mHeight; 138}; 139 140// --------------------------------------------------------------------------- 141 142enum { 143 eTransactionNeeded = 0x01, 144 eTraversalNeeded = 0x02 145}; 146 147class SurfaceFlinger : 148 public BinderService<SurfaceFlinger>, 149 public BnSurfaceComposer, 150 public IBinder::DeathRecipient, 151 protected Thread 152{ 153public: 154 static char const* getServiceName() { return "SurfaceFlinger"; } 155 156 SurfaceFlinger(); 157 virtual ~SurfaceFlinger(); 158 void init(); 159 160 virtual status_t onTransact( 161 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); 162 163 virtual status_t dump(int fd, const Vector<String16>& args); 164 165 // ISurfaceComposer interface 166 virtual sp<ISurfaceComposerClient> createConnection(); 167 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 168 virtual sp<IMemoryHeap> getCblk() const; 169 virtual void bootFinished(); 170 virtual void setTransactionState(const Vector<ComposerState>& state, 171 int orientation, uint32_t flags); 172 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags); 173 virtual bool authenticateSurfaceTexture(const sp<ISurfaceTexture>& surface) const; 174 175 virtual status_t captureScreen(DisplayID dpy, 176 sp<IMemoryHeap>* heap, 177 uint32_t* width, uint32_t* height, 178 PixelFormat* format, uint32_t reqWidth, uint32_t reqHeight, 179 uint32_t minLayerZ, uint32_t maxLayerZ); 180 181 virtual status_t turnElectronBeamOff(int32_t mode); 182 virtual status_t turnElectronBeamOn(int32_t mode); 183 184 void screenReleased(DisplayID dpy); 185 void screenAcquired(DisplayID dpy); 186 187 status_t renderScreenToTexture(DisplayID dpy, 188 GLuint* textureName, GLfloat* uOut, GLfloat* vOut); 189 190 status_t postMessageAsync(const sp<MessageBase>& msg, 191 nsecs_t reltime=0, uint32_t flags = 0); 192 193 status_t postMessageSync(const sp<MessageBase>& msg, 194 nsecs_t reltime=0, uint32_t flags = 0); 195 196 status_t removeLayer(const sp<LayerBase>& layer); 197 status_t addLayer(const sp<LayerBase>& layer); 198 status_t invalidateLayerVisibility(const sp<LayerBase>& layer); 199 void invalidateHwcGeometry(); 200 201 sp<Layer> getLayer(const sp<ISurface>& sur) const; 202 203 GLuint getProtectedTexName() const { return mProtectedTexName; } 204 205 206 class MessageDestroyGLTexture : public MessageBase { 207 GLuint texture; 208 public: 209 MessageDestroyGLTexture(GLuint texture) : texture(texture) { } 210 virtual bool handler() { 211 glDeleteTextures(1, &texture); 212 return true; 213 } 214 }; 215 216 217private: 218 // DeathRecipient interface 219 virtual void binderDied(const wp<IBinder>& who); 220 221private: 222 friend class Client; 223 friend class LayerBase; 224 friend class LayerBaseClient; 225 friend class Layer; 226 227 sp<ISurface> createSurface( 228 ISurfaceComposerClient::surface_data_t* params, 229 const String8& name, 230 const sp<Client>& client, 231 DisplayID display, uint32_t w, uint32_t h, PixelFormat format, 232 uint32_t flags); 233 234 sp<Layer> createNormalSurface( 235 const sp<Client>& client, DisplayID display, 236 uint32_t w, uint32_t h, uint32_t flags, 237 PixelFormat& format); 238 239 sp<LayerDim> createDimSurface( 240 const sp<Client>& client, DisplayID display, 241 uint32_t w, uint32_t h, uint32_t flags); 242 243 sp<LayerScreenshot> createScreenshotSurface( 244 const sp<Client>& client, DisplayID display, 245 uint32_t w, uint32_t h, uint32_t flags); 246 247 status_t removeSurface(const sp<Client>& client, SurfaceID sid); 248 status_t destroySurface(const wp<LayerBaseClient>& layer); 249 uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s); 250 251 class LayerVector : public SortedVector< sp<LayerBase> > { 252 public: 253 LayerVector() { } 254 LayerVector(const LayerVector& rhs) : SortedVector< sp<LayerBase> >(rhs) { } 255 virtual int do_compare(const void* lhs, const void* rhs) const { 256 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs)); 257 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs)); 258 // sort layers by Z order 259 uint32_t lz = l->currentState().z; 260 uint32_t rz = r->currentState().z; 261 // then by sequence, so we get a stable ordering 262 return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence); 263 } 264 }; 265 266 struct State { 267 State() { 268 orientation = ISurfaceComposer::eOrientationDefault; 269 } 270 LayerVector layersSortedByZ; 271 uint8_t orientation; 272 uint8_t orientationFlags; 273 }; 274 275 virtual bool threadLoop(); 276 virtual status_t readyToRun(); 277 virtual void onFirstRef(); 278 279public: // hack to work around gcc 4.0.3 bug 280 const GraphicPlane& graphicPlane(int dpy) const; 281 GraphicPlane& graphicPlane(int dpy); 282 void signalEvent(); 283 void repaintEverything(); 284 285private: 286 void waitForEvent(); 287 void handleConsoleEvents(); 288 void handleTransaction(uint32_t transactionFlags); 289 void handleTransactionLocked(uint32_t transactionFlags); 290 291 void computeVisibleRegions( 292 const LayerVector& currentLayers, 293 Region& dirtyRegion, 294 Region& wormholeRegion); 295 296 void handlePageFlip(); 297 bool lockPageFlip(const LayerVector& currentLayers); 298 void unlockPageFlip(const LayerVector& currentLayers); 299 void handleWorkList(); 300 void handleRepaint(); 301 void postFramebuffer(); 302 void setupHardwareComposer(Region& dirtyInOut); 303 void composeSurfaces(const Region& dirty); 304 305 306 ssize_t addClientLayer(const sp<Client>& client, 307 const sp<LayerBaseClient>& lbc); 308 status_t addLayer_l(const sp<LayerBase>& layer); 309 status_t removeLayer_l(const sp<LayerBase>& layer); 310 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 311 312 uint32_t getTransactionFlags(uint32_t flags); 313 uint32_t peekTransactionFlags(uint32_t flags); 314 uint32_t setTransactionFlags(uint32_t flags); 315 void commitTransaction(); 316 317 318 status_t captureScreenImplLocked(DisplayID dpy, 319 sp<IMemoryHeap>* heap, 320 uint32_t* width, uint32_t* height, PixelFormat* format, 321 uint32_t reqWidth, uint32_t reqHeight, 322 uint32_t minLayerZ, uint32_t maxLayerZ); 323 324 status_t turnElectronBeamOffImplLocked(int32_t mode); 325 status_t turnElectronBeamOnImplLocked(int32_t mode); 326 status_t electronBeamOffAnimationImplLocked(); 327 status_t electronBeamOnAnimationImplLocked(); 328 status_t renderScreenToTextureLocked(DisplayID dpy, 329 GLuint* textureName, GLfloat* uOut, GLfloat* vOut); 330 331 void debugFlashRegions(); 332 void debugShowFPS() const; 333 void drawWormhole() const; 334 335 336 mutable MessageQueue mEventQueue; 337 338 // access must be protected by mStateLock 339 mutable Mutex mStateLock; 340 State mCurrentState; 341 volatile int32_t mTransactionFlags; 342 Condition mTransactionCV; 343 SortedVector< sp<LayerBase> > mLayerPurgatory; 344 bool mTransationPending; 345 346 // protected by mStateLock (but we could use another lock) 347 GraphicPlane mGraphicPlanes[1]; 348 bool mLayersRemoved; 349 DefaultKeyedVector< wp<IBinder>, wp<Layer> > mLayerMap; 350 351 // constant members (no synchronization needed for access) 352 sp<IMemoryHeap> mServerHeap; 353 surface_flinger_cblk_t* mServerCblk; 354 GLuint mWormholeTexName; 355 GLuint mProtectedTexName; 356 nsecs_t mBootTime; 357 358 // Can only accessed from the main thread, these members 359 // don't need synchronization 360 State mDrawingState; 361 Region mDirtyRegion; 362 Region mDirtyRegionRemovedLayer; 363 Region mSwapRegion; 364 Region mWormholeRegion; 365 bool mVisibleRegionsDirty; 366 bool mHwWorkListDirty; 367 int32_t mElectronBeamAnimationMode; 368 Vector< sp<LayerBase> > mVisibleLayersSortedByZ; 369 370 371 // don't use a lock for these, we don't care 372 int mDebugRegion; 373 int mDebugBackground; 374 int mDebugDDMS; 375 int mDebugDisableHWC; 376 int mDebugDisableTransformHint; 377 volatile nsecs_t mDebugInSwapBuffers; 378 nsecs_t mLastSwapBufferTime; 379 volatile nsecs_t mDebugInTransaction; 380 nsecs_t mLastTransactionTime; 381 bool mBootFinished; 382 383 // these are thread safe 384 mutable Barrier mReadyToRunBarrier; 385 386 387 // protected by mDestroyedLayerLock; 388 mutable Mutex mDestroyedLayerLock; 389 Vector<LayerBase const *> mDestroyedLayers; 390 391 // atomic variables 392 enum { 393 eConsoleReleased = 1, 394 eConsoleAcquired = 2 395 }; 396 volatile int32_t mConsoleSignals; 397 398 // only written in the main thread, only read in other threads 399 volatile int32_t mSecureFrameBuffer; 400}; 401 402// --------------------------------------------------------------------------- 403}; // namespace android 404 405#endif // ANDROID_SURFACE_FLINGER_H 406