2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_SURFACE_FLINGER_H
18 #define ANDROID_SURFACE_FLINGER_H
21 #include <sys/types.h>
24 #include <GLES/gl.h> // needed for GLuint
26 #include <cutils/compiler.h>
28 #include <utils/Atomic.h>
29 #include <utils/Errors.h>
30 #include <utils/KeyedVector.h>
31 #include <utils/RefBase.h>
32 #include <utils/SortedVector.h>
33 #include <utils/threads.h>
35 #include <binder/BinderService.h>
36 #include <binder/IMemory.h>
38 #include <ui/PixelFormat.h>
40 #include <gui/ISurfaceComposer.h>
41 #include <gui/ISurfaceComposerClient.h>
43 #include <hardware/hwcomposer_defs.h>
45 #include <private/gui/LayerState.h>
48 #include "DisplayDevice.h"
49 #include "FrameTracker.h"
50 #include "MessageQueue.h"
52 #include "DisplayHardware/HWComposer.h"
56 // ---------------------------------------------------------------------------
59 class DisplayEventConnection;
61 class IGraphicBufferAlloc;
67 // ---------------------------------------------------------------------------
70 eTransactionNeeded = 0x01,
71 eTraversalNeeded = 0x02,
72 eDisplayTransactionNeeded = 0x04,
73 eTransactionMask = 0x07
76 class SurfaceFlinger : public BinderService<SurfaceFlinger>,
77 public BnSurfaceComposer,
78 private IBinder::DeathRecipient,
80 private HWComposer::EventHandler
83 static char const* getServiceName() ANDROID_API {
84 return "SurfaceFlinger";
87 SurfaceFlinger() ANDROID_API;
90 EVENT_VSYNC = HWC_EVENT_VSYNC
93 // post an asynchronous message to the main thread
94 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0,
97 // post a synchronous message to the main thread
98 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0,
101 // force full composition on all displays
102 void repaintEverything();
104 // returns the default Display
105 sp<const DisplayDevice> getDefaultDisplayDevice() const {
106 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
109 // utility function to delete a texture on the main thread
110 void deleteTextureAsync(GLuint texture);
112 // enable/disable h/w composer event
113 // TODO: this should be made accessible only to EventThread
114 void eventControl(int disp, int event, int enabled);
116 // called on the main thread by MessageQueue when an internal message
118 // TODO: this should be made accessible only to MessageQueue
119 void onMessageReceived(int32_t what);
121 // for debugging only
122 // TODO: this should be made accessible only to HWComposer
123 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
125 RenderEngine& getRenderEngine() const {
126 return *mRenderEngine;
131 friend class DisplayEventConnection;
133 friend class SurfaceTextureLayer;
135 // This value is specified in number of frames. Log frame stats at most
137 enum { LOG_FRAME_STATS_PERIOD = 30*60*60 };
139 // We're reference counted, never destroy SurfaceFlinger directly
140 virtual ~SurfaceFlinger();
142 /* ------------------------------------------------------------------------
143 * Internal data structures
146 class LayerVector : public SortedVector< sp<Layer> > {
149 LayerVector(const LayerVector& rhs);
150 virtual int do_compare(const void* lhs, const void* rhs) const;
153 struct DisplayDeviceState {
154 DisplayDeviceState();
155 DisplayDeviceState(DisplayDevice::DisplayType type);
156 bool isValid() const { return type >= 0; }
157 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
158 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
159 DisplayDevice::DisplayType type;
160 sp<IGraphicBufferProducer> surface;
170 LayerVector layersSortedByZ;
171 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
174 /* ------------------------------------------------------------------------
177 virtual status_t onTransact(uint32_t code, const Parcel& data,
178 Parcel* reply, uint32_t flags);
179 virtual status_t dump(int fd, const Vector<String16>& args);
181 /* ------------------------------------------------------------------------
182 * ISurfaceComposer interface
184 virtual sp<ISurfaceComposerClient> createConnection();
185 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
186 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
187 virtual sp<IBinder> getBuiltInDisplay(int32_t id);
188 virtual void setTransactionState(const Vector<ComposerState>& state,
189 const Vector<DisplayState>& displays, uint32_t flags);
190 virtual void bootFinished();
191 virtual bool authenticateSurfaceTexture(
192 const sp<IGraphicBufferProducer>& bufferProducer) const;
193 virtual sp<IDisplayEventConnection> createDisplayEventConnection();
194 virtual status_t captureScreen(const sp<IBinder>& display,
195 const sp<IGraphicBufferProducer>& producer,
196 uint32_t reqWidth, uint32_t reqHeight,
197 uint32_t minLayerZ, uint32_t maxLayerZ);
198 // called when screen needs to turn off
199 virtual void blank(const sp<IBinder>& display);
200 // called when screen is turning back on
201 virtual void unblank(const sp<IBinder>& display);
202 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info);
204 /* ------------------------------------------------------------------------
205 * DeathRecipient interface
207 virtual void binderDied(const wp<IBinder>& who);
209 /* ------------------------------------------------------------------------
212 virtual bool threadLoop();
213 virtual status_t readyToRun();
214 virtual void onFirstRef();
216 /* ------------------------------------------------------------------------
217 * HWComposer::EventHandler interface
219 virtual void onVSyncReceived(int type, nsecs_t timestamp);
220 virtual void onHotplugReceived(int disp, bool connected);
222 /* ------------------------------------------------------------------------
226 void signalTransaction();
227 void signalLayerUpdate();
228 void signalRefresh();
230 // called on the main thread in response to initializeDisplays()
231 void onInitializeDisplays();
232 // called on the main thread in response to blank()
233 void onScreenReleased(const sp<const DisplayDevice>& hw);
234 // called on the main thread in response to unblank()
235 void onScreenAcquired(const sp<const DisplayDevice>& hw);
237 void handleMessageTransaction();
238 void handleMessageInvalidate();
239 void handleMessageRefresh();
241 void handleTransaction(uint32_t transactionFlags);
242 void handleTransactionLocked(uint32_t transactionFlags);
244 /* handlePageFilp: this is were we latch a new buffer
245 * if available and compute the dirty region.
247 void handlePageFlip();
249 /* ------------------------------------------------------------------------
252 uint32_t getTransactionFlags(uint32_t flags);
253 uint32_t peekTransactionFlags(uint32_t flags);
254 uint32_t setTransactionFlags(uint32_t flags);
255 void commitTransaction();
256 uint32_t setClientStateLocked(const sp<Client>& client,
257 const layer_state_t& s);
258 uint32_t setDisplayStateLocked(const DisplayState& s);
260 /* ------------------------------------------------------------------------
263 status_t createLayer(const String8& name, const sp<Client>& client,
264 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
265 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
267 status_t createNormalLayer(const sp<Client>& client, const String8& name,
268 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
269 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
270 sp<Layer>* outLayer);
272 status_t createDimLayer(const sp<Client>& client, const String8& name,
273 uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
274 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
276 // called in response to the window-manager calling
277 // ISurfaceComposerClient::destroySurface()
278 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
280 // called when all clients have released all their references to
281 // this layer meaning it is entirely safe to destroy all
282 // resources associated to this layer.
283 status_t onLayerDestroyed(const wp<Layer>& layer);
285 // remove a layer from SurfaceFlinger immediately
286 status_t removeLayer(const sp<Layer>& layer);
288 // add a layer to SurfaceFlinger
289 void addClientLayer(const sp<Client>& client,
290 const sp<IBinder>& handle,
291 const sp<IGraphicBufferProducer>& gbc,
292 const sp<Layer>& lbc);
294 /* ------------------------------------------------------------------------
295 * Boot animation, on/off animations and screen capture
298 void startBootAnim();
300 void renderScreenImplLocked(
301 const sp<const DisplayDevice>& hw,
302 uint32_t reqWidth, uint32_t reqHeight,
303 uint32_t minLayerZ, uint32_t maxLayerZ,
306 status_t captureScreenImplLocked(
307 const sp<const DisplayDevice>& hw,
308 const sp<IGraphicBufferProducer>& producer,
309 uint32_t reqWidth, uint32_t reqHeight,
310 uint32_t minLayerZ, uint32_t maxLayerZ);
312 /* ------------------------------------------------------------------------
315 static status_t selectConfigForAttribute(EGLDisplay dpy,
316 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig);
317 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId);
318 size_t getMaxTextureSize() const;
319 size_t getMaxViewportDims() const;
321 /* ------------------------------------------------------------------------
322 * Display and layer stack management
324 // called when starting, or restarting after system_server death
325 void initializeDisplays();
327 // Create an IBinder for a builtin display and add it to current state
328 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
330 // NOTE: can only be called from the main thread or with mStateLock held
331 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
332 return mDisplays.valueFor(dpy);
335 // NOTE: can only be called from the main thread or with mStateLock held
336 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
337 return mDisplays.valueFor(dpy);
340 // mark a region of a layer stack dirty. this updates the dirty
341 // region of all screens presenting this layer stack.
342 void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
344 // allocate a h/w composer display id
345 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
347 /* ------------------------------------------------------------------------
351 HWComposer& getHwComposer() const { return *mHwc; }
353 /* ------------------------------------------------------------------------
356 void invalidateHwcGeometry();
357 static void computeVisibleRegions(
358 const LayerVector& currentLayers, uint32_t layerStack,
359 Region& dirtyRegion, Region& opaqueRegion);
361 void preComposition();
362 void postComposition();
363 void rebuildLayerStacks();
364 void setUpHWComposer();
365 void doComposition();
366 void doDebugFlashRegions();
367 void doDisplayComposition(const sp<const DisplayDevice>& hw,
368 const Region& dirtyRegion);
369 void doComposeSurfaces(const sp<const DisplayDevice>& hw,
370 const Region& dirty);
372 void postFramebuffer();
373 void drawWormhole(const sp<const DisplayDevice>& hw,
374 const Region& region) const;
376 /* ------------------------------------------------------------------------
381 /* ------------------------------------------------------------------------
382 * Debugging & dumpsys
384 void listLayersLocked(const Vector<String16>& args, size_t& index,
385 String8& result) const;
386 void dumpStatsLocked(const Vector<String16>& args, size_t& index,
387 String8& result) const;
388 void clearStatsLocked(const Vector<String16>& args, size_t& index,
390 void dumpAllLocked(const Vector<String16>& args, size_t& index,
391 String8& result) const;
392 bool startDdmConnection();
393 static void appendSfConfigString(String8& result);
394 void checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
395 const sp<const DisplayDevice>& hw,
396 uint32_t minLayerZ, uint32_t maxLayerZ);
398 void logFrameStats();
400 /* ------------------------------------------------------------------------
404 // access must be protected by mStateLock
405 mutable Mutex mStateLock;
407 volatile int32_t mTransactionFlags;
408 Condition mTransactionCV;
409 bool mTransactionPending;
410 bool mAnimTransactionPending;
411 Vector< sp<Layer> > mLayersPendingRemoval;
412 SortedVector< wp<IBinder> > mGraphicBufferProducerList;
414 // protected by mStateLock (but we could use another lock)
417 // access must be protected by mInvalidateLock
418 volatile int32_t mRepaintEverything;
420 // constant members (no synchronization needed for access)
422 RenderEngine* mRenderEngine;
424 bool mGpuToCpuSupported;
425 sp<EventThread> mEventThread;
426 EGLContext mEGLContext;
427 EGLConfig mEGLConfig;
428 EGLDisplay mEGLDisplay;
429 EGLint mEGLNativeVisualId;
430 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES];
432 // Can only accessed from the main thread, these members
433 // don't need synchronization
435 bool mVisibleRegionsDirty;
436 bool mHwWorkListDirty;
437 bool mAnimCompositionPending;
439 // this may only be written from the main thread with mStateLock held
440 // it may be read from other threads with mStateLock held
441 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
443 // don't use a lock for these, we don't care
446 int mDebugDisableHWC;
447 int mDebugDisableTransformHint;
448 volatile nsecs_t mDebugInSwapBuffers;
449 nsecs_t mLastSwapBufferTime;
450 volatile nsecs_t mDebugInTransaction;
451 nsecs_t mLastTransactionTime;
454 // these are thread safe
455 mutable MessageQueue mEventQueue;
456 mutable Barrier mReadyToRunBarrier;
457 FrameTracker mAnimFrameTracker;
459 // protected by mDestroyedLayerLock;
460 mutable Mutex mDestroyedLayerLock;
461 Vector<Layer const *> mDestroyedLayers;
463 /* ------------------------------------------------------------------------
464 * Feature prototyping
467 sp<IBinder> mExtDisplayToken;
470 // ---------------------------------------------------------------------------
471 }; // namespace android
473 #endif // ANDROID_SURFACE_FLINGER_H