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