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