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