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