SurfaceFlinger.h revision db403e8ff0d7727015e1a5009bab20eb7ec205bc
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 // called on the main thread in response to screenReleased() 157 void onScreenReleased(); 158 // called on the main thread in response to screenAcquired() 159 void onScreenAcquired(); 160 161 162 status_t renderScreenToTexture(DisplayID dpy, 163 GLuint* textureName, GLfloat* uOut, GLfloat* vOut); 164 status_t renderScreenToTextureLocked(DisplayID dpy, 165 GLuint* textureName, GLfloat* uOut, GLfloat* vOut); 166 167 void onMessageReceived(int32_t what); 168 169 status_t postMessageAsync(const sp<MessageBase>& msg, 170 nsecs_t reltime=0, uint32_t flags = 0); 171 172 status_t postMessageSync(const sp<MessageBase>& msg, 173 nsecs_t reltime=0, uint32_t flags = 0); 174 175 status_t removeLayer(const sp<LayerBase>& layer); 176 status_t addLayer(const sp<LayerBase>& layer); 177 status_t invalidateLayerVisibility(const sp<LayerBase>& layer); 178 void invalidateHwcGeometry(); 179 180 sp<Layer> getLayer(const sp<ISurface>& sur) const; 181 182 GLuint getProtectedTexName() const { return mProtectedTexName; } 183 184 185 class MessageDestroyGLTexture : public MessageBase { 186 GLuint texture; 187 public: 188 MessageDestroyGLTexture(GLuint texture) : texture(texture) { } 189 virtual bool handler() { 190 glDeleteTextures(1, &texture); 191 return true; 192 } 193 }; 194 195 196private: 197 // DeathRecipient interface 198 virtual void binderDied(const wp<IBinder>& who); 199 200private: 201 friend class Client; 202 friend class DisplayEventConnection; 203 friend class LayerBase; 204 friend class LayerBaseClient; 205 friend class Layer; 206 207 sp<ISurface> createSurface( 208 ISurfaceComposerClient::surface_data_t* params, 209 const String8& name, 210 const sp<Client>& client, 211 DisplayID display, uint32_t w, uint32_t h, PixelFormat format, 212 uint32_t flags); 213 214 sp<Layer> createNormalSurface( 215 const sp<Client>& client, DisplayID display, 216 uint32_t w, uint32_t h, uint32_t flags, 217 PixelFormat& format); 218 219 sp<LayerDim> createDimSurface( 220 const sp<Client>& client, DisplayID display, 221 uint32_t w, uint32_t h, uint32_t flags); 222 223 sp<LayerScreenshot> createScreenshotSurface( 224 const sp<Client>& client, DisplayID display, 225 uint32_t w, uint32_t h, uint32_t flags); 226 227 status_t removeSurface(const sp<Client>& client, SurfaceID sid); 228 status_t destroySurface(const wp<LayerBaseClient>& layer); 229 uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s); 230 231 class LayerVector : public SortedVector< sp<LayerBase> > { 232 public: 233 LayerVector() { } 234 LayerVector(const LayerVector& rhs) : SortedVector< sp<LayerBase> >(rhs) { } 235 virtual int do_compare(const void* lhs, const void* rhs) const { 236 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs)); 237 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs)); 238 // sort layers by Z order 239 uint32_t lz = l->currentState().z; 240 uint32_t rz = r->currentState().z; 241 // then by sequence, so we get a stable ordering 242 return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence); 243 } 244 }; 245 246 struct State { 247 State() 248 : orientation(ISurfaceComposer::eOrientationDefault), 249 orientationFlags(0) { 250 } 251 LayerVector layersSortedByZ; 252 uint8_t orientation; 253 uint8_t orientationFlags; 254 }; 255 256 virtual bool threadLoop(); 257 virtual status_t readyToRun(); 258 virtual void onFirstRef(); 259 260public: // hack to work around gcc 4.0.3 bug 261 const GraphicPlane& graphicPlane(int dpy) const; 262 GraphicPlane& graphicPlane(int dpy); 263 264 void signalTransaction(); 265 void signalLayerUpdate(); 266 void signalRefresh(); 267 void repaintEverything(); 268 269private: 270 void waitForEvent(); 271 void handleTransaction(uint32_t transactionFlags); 272 void handleTransactionLocked(uint32_t transactionFlags); 273 274 void computeVisibleRegions( 275 const LayerVector& currentLayers, 276 Region& dirtyRegion, 277 Region& wormholeRegion); 278 279 void handlePageFlip(); 280 bool lockPageFlip(const LayerVector& currentLayers); 281 void unlockPageFlip(const LayerVector& currentLayers); 282 void handleRefresh(); 283 void handleWorkList(); 284 void handleRepaint(); 285 void postFramebuffer(); 286 void setupHardwareComposer(); 287 void composeSurfaces(const Region& dirty); 288 289 290 void setInvalidateRegion(const Region& reg); 291 Region getAndClearInvalidateRegion(); 292 293 ssize_t addClientLayer(const sp<Client>& client, 294 const sp<LayerBaseClient>& lbc); 295 status_t addLayer_l(const sp<LayerBase>& layer); 296 status_t removeLayer_l(const sp<LayerBase>& layer); 297 status_t purgatorizeLayer_l(const sp<LayerBase>& layer); 298 299 uint32_t getTransactionFlags(uint32_t flags); 300 uint32_t peekTransactionFlags(uint32_t flags); 301 uint32_t setTransactionFlags(uint32_t flags); 302 void commitTransaction(); 303 304 305 status_t captureScreenImplLocked(DisplayID dpy, 306 sp<IMemoryHeap>* heap, 307 uint32_t* width, uint32_t* height, PixelFormat* format, 308 uint32_t reqWidth, uint32_t reqHeight, 309 uint32_t minLayerZ, uint32_t maxLayerZ); 310 311 status_t turnElectronBeamOffImplLocked(int32_t mode); 312 status_t turnElectronBeamOnImplLocked(int32_t mode); 313 status_t electronBeamOffAnimationImplLocked(); 314 status_t electronBeamOnAnimationImplLocked(); 315 316 void debugFlashRegions(); 317 void drawWormhole() const; 318 319 void listLayersLocked(const Vector<String16>& args, size_t& index, 320 String8& result, char* buffer, size_t SIZE) const; 321 void dumpStatsLocked(const Vector<String16>& args, size_t& index, 322 String8& result, char* buffer, size_t SIZE) const; 323 void clearStatsLocked(const Vector<String16>& args, size_t& index, 324 String8& result, char* buffer, size_t SIZE) const; 325 void dumpAllLocked(String8& result, char* buffer, size_t SIZE) const; 326 327 mutable MessageQueue mEventQueue; 328 329 // access must be protected by mStateLock 330 mutable Mutex mStateLock; 331 State mCurrentState; 332 volatile int32_t mTransactionFlags; 333 Condition mTransactionCV; 334 SortedVector< sp<LayerBase> > mLayerPurgatory; 335 bool mTransationPending; 336 Vector< sp<LayerBase> > mLayersPendingRemoval; 337 338 // protected by mStateLock (but we could use another lock) 339 GraphicPlane mGraphicPlanes[1]; 340 bool mLayersRemoved; 341 DefaultKeyedVector< wp<IBinder>, wp<Layer> > mLayerMap; 342 343 // access must be protected by mInvalidateLock 344 mutable Mutex mInvalidateLock; 345 Region mInvalidateRegion; 346 347 // constant members (no synchronization needed for access) 348 sp<IMemoryHeap> mServerHeap; 349 surface_flinger_cblk_t* mServerCblk; 350 GLuint mWormholeTexName; 351 GLuint mProtectedTexName; 352 nsecs_t mBootTime; 353 sp<EventThread> mEventThread; 354 355 // Can only accessed from the main thread, these members 356 // don't need synchronization 357 State mDrawingState; 358 Region mDirtyRegion; 359 Region mDirtyRegionRemovedLayer; 360 Region mSwapRegion; 361 Region mWormholeRegion; 362 bool mVisibleRegionsDirty; 363 bool mHwWorkListDirty; 364 int32_t mElectronBeamAnimationMode; 365 Vector< sp<LayerBase> > mVisibleLayersSortedByZ; 366 367 368 // don't use a lock for these, we don't care 369 int mDebugRegion; 370 int mDebugDDMS; 371 int mDebugDisableHWC; 372 int mDebugDisableTransformHint; 373 volatile nsecs_t mDebugInSwapBuffers; 374 nsecs_t mLastSwapBufferTime; 375 volatile nsecs_t mDebugInTransaction; 376 nsecs_t mLastTransactionTime; 377 bool mBootFinished; 378 379 // these are thread safe 380 mutable Barrier mReadyToRunBarrier; 381 382 383 // protected by mDestroyedLayerLock; 384 mutable Mutex mDestroyedLayerLock; 385 Vector<LayerBase const *> mDestroyedLayers; 386 387 // only written in the main thread, only read in other threads 388 volatile int32_t mSecureFrameBuffer; 389}; 390 391// --------------------------------------------------------------------------- 392}; // namespace android 393 394#endif // ANDROID_SURFACE_FLINGER_H 395