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