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