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 // We're reference counted, never destroy SurfaceFlinger directly
136 virtual ~SurfaceFlinger();
138 /* ------------------------------------------------------------------------
139 * Internal data structures
142 class LayerVector : public SortedVector< sp<Layer> > {
145 LayerVector(const LayerVector& rhs);
146 virtual int do_compare(const void* lhs, const void* rhs) const;
149 struct DisplayDeviceState {
150 DisplayDeviceState();
151 DisplayDeviceState(DisplayDevice::DisplayType type);
152 bool isValid() const { return type >= 0; }
153 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
154 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
155 DisplayDevice::DisplayType type;
156 sp<IGraphicBufferProducer> surface;
166 LayerVector layersSortedByZ;
167 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
170 /* ------------------------------------------------------------------------
173 virtual status_t onTransact(uint32_t code, const Parcel& data,
174 Parcel* reply, uint32_t flags);
175 virtual status_t dump(int fd, const Vector<String16>& args);
177 /* ------------------------------------------------------------------------
178 * ISurfaceComposer interface
180 virtual sp<ISurfaceComposerClient> createConnection();
181 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
182 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
183 virtual sp<IBinder> getBuiltInDisplay(int32_t id);
184 virtual void setTransactionState(const Vector<ComposerState>& state,
185 const Vector<DisplayState>& displays, uint32_t flags);
186 virtual void bootFinished();
187 virtual bool authenticateSurfaceTexture(
188 const sp<IGraphicBufferProducer>& bufferProducer) const;
189 virtual sp<IDisplayEventConnection> createDisplayEventConnection();
190 virtual status_t captureScreen(const sp<IBinder>& display,
191 const sp<IGraphicBufferProducer>& producer,
192 uint32_t reqWidth, uint32_t reqHeight,
193 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer);
194 // called when screen needs to turn off
195 virtual void blank(const sp<IBinder>& display);
196 // called when screen is turning back on
197 virtual void unblank(const sp<IBinder>& display);
198 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info);
200 /* ------------------------------------------------------------------------
201 * DeathRecipient interface
203 virtual void binderDied(const wp<IBinder>& who);
205 /* ------------------------------------------------------------------------
208 virtual bool threadLoop();
209 virtual status_t readyToRun();
210 virtual void onFirstRef();
212 /* ------------------------------------------------------------------------
213 * HWComposer::EventHandler interface
215 virtual void onVSyncReceived(int type, nsecs_t timestamp);
216 virtual void onHotplugReceived(int disp, bool connected);
218 /* ------------------------------------------------------------------------
222 void signalTransaction();
223 void signalLayerUpdate();
224 void signalRefresh();
226 // called on the main thread in response to initializeDisplays()
227 void onInitializeDisplays();
228 // called on the main thread in response to blank()
229 void onScreenReleased(const sp<const DisplayDevice>& hw);
230 // called on the main thread in response to unblank()
231 void onScreenAcquired(const sp<const DisplayDevice>& hw);
233 void handleMessageTransaction();
234 void handleMessageInvalidate();
235 void handleMessageRefresh();
237 void handleTransaction(uint32_t transactionFlags);
238 void handleTransactionLocked(uint32_t transactionFlags);
240 /* handlePageFilp: this is were we latch a new buffer
241 * if available and compute the dirty region.
243 void handlePageFlip();
245 /* ------------------------------------------------------------------------
248 uint32_t getTransactionFlags(uint32_t flags);
249 uint32_t peekTransactionFlags(uint32_t flags);
250 uint32_t setTransactionFlags(uint32_t flags);
251 void commitTransaction();
252 uint32_t setClientStateLocked(const sp<Client>& client,
253 const layer_state_t& s);
254 uint32_t setDisplayStateLocked(const DisplayState& s);
256 /* ------------------------------------------------------------------------
259 status_t createLayer(const String8& name, const sp<Client>& client,
260 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
261 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
263 status_t createNormalLayer(const sp<Client>& client, const String8& name,
264 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
265 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
266 sp<Layer>* outLayer);
268 status_t createDimLayer(const sp<Client>& client, const String8& name,
269 uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
270 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
272 // called in response to the window-manager calling
273 // ISurfaceComposerClient::destroySurface()
274 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
276 // called when all clients have released all their references to
277 // this layer meaning it is entirely safe to destroy all
278 // resources associated to this layer.
279 status_t onLayerDestroyed(const wp<Layer>& layer);
281 // remove a layer from SurfaceFlinger immediately
282 status_t removeLayer(const sp<Layer>& layer);
284 // add a layer to SurfaceFlinger
285 void addClientLayer(const sp<Client>& client,
286 const sp<IBinder>& handle,
287 const sp<IGraphicBufferProducer>& gbc,
288 const sp<Layer>& lbc);
290 /* ------------------------------------------------------------------------
291 * Boot animation, on/off animations and screen capture
294 void startBootAnim();
296 void renderScreenImplLocked(
297 const sp<const DisplayDevice>& hw,
298 uint32_t reqWidth, uint32_t reqHeight,
299 uint32_t minLayerZ, uint32_t maxLayerZ,
302 status_t captureScreenImplLocked(
303 const sp<const DisplayDevice>& hw,
304 const sp<IGraphicBufferProducer>& producer,
305 uint32_t reqWidth, uint32_t reqHeight,
306 uint32_t minLayerZ, uint32_t maxLayerZ,
309 /* ------------------------------------------------------------------------
312 static status_t selectConfigForAttribute(EGLDisplay dpy,
313 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig);
314 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId);
315 size_t getMaxTextureSize() const;
316 size_t getMaxViewportDims() const;
318 /* ------------------------------------------------------------------------
319 * Display and layer stack management
321 // called when starting, or restarting after system_server death
322 void initializeDisplays();
324 // Create an IBinder for a builtin display and add it to current state
325 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
327 // NOTE: can only be called from the main thread or with mStateLock held
328 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
329 return mDisplays.valueFor(dpy);
332 // NOTE: can only be called from the main thread or with mStateLock held
333 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
334 return mDisplays.valueFor(dpy);
337 // mark a region of a layer stack dirty. this updates the dirty
338 // region of all screens presenting this layer stack.
339 void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
341 // allocate a h/w composer display id
342 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
344 /* ------------------------------------------------------------------------
348 HWComposer& getHwComposer() const { return *mHwc; }
350 /* ------------------------------------------------------------------------
353 void invalidateHwcGeometry();
354 static void computeVisibleRegions(
355 const LayerVector& currentLayers, uint32_t layerStack,
356 Region& dirtyRegion, Region& opaqueRegion);
358 void preComposition();
359 void postComposition();
360 void rebuildLayerStacks();
361 void setUpHWComposer();
362 void doComposition();
363 void doDebugFlashRegions();
364 void doDisplayComposition(const sp<const DisplayDevice>& hw,
365 const Region& dirtyRegion);
366 void doComposeSurfaces(const sp<const DisplayDevice>& hw,
367 const Region& dirty);
369 void postFramebuffer();
370 void drawWormhole(const sp<const DisplayDevice>& hw,
371 const Region& region) const;
373 /* ------------------------------------------------------------------------
378 /* ------------------------------------------------------------------------
379 * Debugging & dumpsys
381 void listLayersLocked(const Vector<String16>& args, size_t& index,
382 String8& result) const;
383 void dumpStatsLocked(const Vector<String16>& args, size_t& index,
384 String8& result) const;
385 void clearStatsLocked(const Vector<String16>& args, size_t& index,
387 void dumpAllLocked(const Vector<String16>& args, size_t& index,
388 String8& result) const;
389 bool startDdmConnection();
390 static void appendSfConfigString(String8& result);
391 void checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
392 const sp<const DisplayDevice>& hw,
393 uint32_t minLayerZ, uint32_t maxLayerZ);
395 /* ------------------------------------------------------------------------
399 // access must be protected by mStateLock
400 mutable Mutex mStateLock;
402 volatile int32_t mTransactionFlags;
403 Condition mTransactionCV;
404 bool mTransactionPending;
405 bool mAnimTransactionPending;
406 Vector< sp<Layer> > mLayersPendingRemoval;
407 SortedVector< wp<IBinder> > mGraphicBufferProducerList;
409 // protected by mStateLock (but we could use another lock)
412 // access must be protected by mInvalidateLock
413 volatile int32_t mRepaintEverything;
415 // constant members (no synchronization needed for access)
417 RenderEngine* mRenderEngine;
419 bool mGpuToCpuSupported;
420 sp<EventThread> mEventThread;
421 EGLContext mEGLContext;
422 EGLConfig mEGLConfig;
423 EGLDisplay mEGLDisplay;
424 EGLint mEGLNativeVisualId;
425 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES];
427 // Can only accessed from the main thread, these members
428 // don't need synchronization
430 bool mVisibleRegionsDirty;
431 bool mHwWorkListDirty;
432 bool mAnimCompositionPending;
434 // this may only be written from the main thread with mStateLock held
435 // it may be read from other threads with mStateLock held
436 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
438 // don't use a lock for these, we don't care
441 int mDebugDisableHWC;
442 int mDebugDisableTransformHint;
443 volatile nsecs_t mDebugInSwapBuffers;
444 nsecs_t mLastSwapBufferTime;
445 volatile nsecs_t mDebugInTransaction;
446 nsecs_t mLastTransactionTime;
449 // these are thread safe
450 mutable MessageQueue mEventQueue;
451 mutable Barrier mReadyToRunBarrier;
452 FrameTracker mAnimFrameTracker;
454 // protected by mDestroyedLayerLock;
455 mutable Mutex mDestroyedLayerLock;
456 Vector<Layer const *> mDestroyedLayers;
458 /* ------------------------------------------------------------------------
459 * Feature prototyping
462 sp<IBinder> mExtDisplayToken;
465 // ---------------------------------------------------------------------------
466 }; // namespace android
468 #endif // ANDROID_SURFACE_FLINGER_H