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>
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;
66 // ---------------------------------------------------------------------------
69 eTransactionNeeded = 0x01,
70 eTraversalNeeded = 0x02,
71 eDisplayTransactionNeeded = 0x04,
72 eTransactionMask = 0x07
75 class SurfaceFlinger : public BinderService<SurfaceFlinger>,
76 public BnSurfaceComposer,
77 private IBinder::DeathRecipient,
79 private HWComposer::EventHandler
82 static char const* getServiceName() ANDROID_API {
83 return "SurfaceFlinger";
86 SurfaceFlinger() ANDROID_API;
89 EVENT_VSYNC = HWC_EVENT_VSYNC
92 // post an asynchronous message to the main thread
93 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0,
96 // post a synchronous message to the main thread
97 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0,
100 // force full composition on all displays
101 void repaintEverything();
103 // returns the default Display
104 sp<const DisplayDevice> getDefaultDisplayDevice() const {
105 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
108 // utility function to delete a texture on the main thread
109 void deleteTextureAsync(GLuint texture);
111 // enable/disable h/w composer event
112 // TODO: this should be made accessible only to EventThread
113 void eventControl(int disp, int event, int enabled);
115 // called on the main thread by MessageQueue when an internal message
117 // TODO: this should be made accessible only to MessageQueue
118 void onMessageReceived(int32_t what);
120 // for debugging only
121 // TODO: this should be made accessible only to HWComposer
122 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
126 friend class DisplayEventConnection;
128 friend class SurfaceTextureLayer;
130 // We're reference counted, never destroy SurfaceFlinger directly
131 virtual ~SurfaceFlinger();
133 /* ------------------------------------------------------------------------
134 * Internal data structures
137 class LayerVector : public SortedVector< sp<Layer> > {
140 LayerVector(const LayerVector& rhs);
141 virtual int do_compare(const void* lhs, const void* rhs) const;
144 struct DisplayDeviceState {
145 DisplayDeviceState();
146 DisplayDeviceState(DisplayDevice::DisplayType type);
147 bool isValid() const { return type >= 0; }
148 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
149 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
150 DisplayDevice::DisplayType type;
151 sp<IGraphicBufferProducer> surface;
161 LayerVector layersSortedByZ;
162 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
165 /* ------------------------------------------------------------------------
168 virtual status_t onTransact(uint32_t code, const Parcel& data,
169 Parcel* reply, uint32_t flags);
170 virtual status_t dump(int fd, const Vector<String16>& args);
172 /* ------------------------------------------------------------------------
173 * ISurfaceComposer interface
175 virtual sp<ISurfaceComposerClient> createConnection();
176 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
177 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
178 virtual sp<IBinder> getBuiltInDisplay(int32_t id);
179 virtual void setTransactionState(const Vector<ComposerState>& state,
180 const Vector<DisplayState>& displays, uint32_t flags);
181 virtual void bootFinished();
182 virtual bool authenticateSurfaceTexture(
183 const sp<IGraphicBufferProducer>& bufferProducer) const;
184 virtual sp<IDisplayEventConnection> createDisplayEventConnection();
185 virtual status_t captureScreen(const sp<IBinder>& display,
186 const sp<IGraphicBufferProducer>& producer,
187 uint32_t reqWidth, uint32_t reqHeight,
188 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer);
189 // called when screen needs to turn off
190 virtual void blank(const sp<IBinder>& display);
191 // called when screen is turning back on
192 virtual void unblank(const sp<IBinder>& display);
193 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info);
195 /* ------------------------------------------------------------------------
196 * DeathRecipient interface
198 virtual void binderDied(const wp<IBinder>& who);
200 /* ------------------------------------------------------------------------
203 virtual bool threadLoop();
204 virtual status_t readyToRun();
205 virtual void onFirstRef();
207 /* ------------------------------------------------------------------------
208 * HWComposer::EventHandler interface
210 virtual void onVSyncReceived(int type, nsecs_t timestamp);
211 virtual void onHotplugReceived(int disp, bool connected);
213 /* ------------------------------------------------------------------------
217 void signalTransaction();
218 void signalLayerUpdate();
219 void signalRefresh();
221 // called on the main thread in response to initializeDisplays()
222 void onInitializeDisplays();
223 // called on the main thread in response to blank()
224 void onScreenReleased(const sp<const DisplayDevice>& hw);
225 // called on the main thread in response to unblank()
226 void onScreenAcquired(const sp<const DisplayDevice>& hw);
228 void handleMessageTransaction();
229 void handleMessageInvalidate();
230 void handleMessageRefresh();
232 void handleTransaction(uint32_t transactionFlags);
233 void handleTransactionLocked(uint32_t transactionFlags);
235 /* handlePageFilp: this is were we latch a new buffer
236 * if available and compute the dirty region.
238 void handlePageFlip();
240 /* ------------------------------------------------------------------------
243 uint32_t getTransactionFlags(uint32_t flags);
244 uint32_t peekTransactionFlags(uint32_t flags);
245 uint32_t setTransactionFlags(uint32_t flags);
246 void commitTransaction();
247 uint32_t setClientStateLocked(const sp<Client>& client,
248 const layer_state_t& s);
249 uint32_t setDisplayStateLocked(const DisplayState& s);
251 /* ------------------------------------------------------------------------
254 status_t createLayer(const String8& name, const sp<Client>& client,
255 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
256 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
258 status_t createNormalLayer(const sp<Client>& client, const String8& name,
259 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
260 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
261 sp<Layer>* outLayer);
263 status_t createDimLayer(const sp<Client>& client, const String8& name,
264 uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
265 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
267 // called in response to the window-manager calling
268 // ISurfaceComposerClient::destroySurface()
269 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
271 // called when all clients have released all their references to
272 // this layer meaning it is entirely safe to destroy all
273 // resources associated to this layer.
274 status_t onLayerDestroyed(const wp<Layer>& layer);
276 // remove a layer from SurfaceFlinger immediately
277 status_t removeLayer(const sp<Layer>& layer);
279 // add a layer to SurfaceFlinger
280 void addClientLayer(const sp<Client>& client,
281 const sp<IBinder>& handle,
282 const sp<IGraphicBufferProducer>& gbc,
283 const sp<Layer>& lbc);
285 /* ------------------------------------------------------------------------
286 * Boot animation, on/off animations and screen capture
289 void startBootAnim();
291 void renderScreenImplLocked(
292 const sp<const DisplayDevice>& hw,
293 uint32_t reqWidth, uint32_t reqHeight,
294 uint32_t minLayerZ, uint32_t maxLayerZ,
297 status_t captureScreenImplLocked(
298 const sp<const DisplayDevice>& hw,
299 const sp<IGraphicBufferProducer>& producer,
300 uint32_t reqWidth, uint32_t reqHeight,
301 uint32_t minLayerZ, uint32_t maxLayerZ,
304 /* ------------------------------------------------------------------------
307 static status_t selectConfigForAttribute(EGLDisplay dpy,
308 EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig);
309 static EGLConfig selectEGLConfig(EGLDisplay disp, EGLint visualId);
310 static EGLContext createGLContext(EGLDisplay disp, EGLConfig config);
311 void initializeGL(EGLDisplay display);
312 uint32_t getMaxTextureSize() const;
313 uint32_t getMaxViewportDims() const;
315 /* ------------------------------------------------------------------------
316 * Display and layer stack management
318 // called when starting, or restarting after system_server death
319 void initializeDisplays();
321 // Create an IBinder for a builtin display and add it to current state
322 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
324 // NOTE: can only be called from the main thread or with mStateLock held
325 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
326 return mDisplays.valueFor(dpy);
329 // NOTE: can only be called from the main thread or with mStateLock held
330 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
331 return mDisplays.valueFor(dpy);
334 // mark a region of a layer stack dirty. this updates the dirty
335 // region of all screens presenting this layer stack.
336 void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
338 // allocate a h/w composer display id
339 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
341 /* ------------------------------------------------------------------------
345 HWComposer& getHwComposer() const { return *mHwc; }
347 /* ------------------------------------------------------------------------
350 void invalidateHwcGeometry();
351 static void computeVisibleRegions(
352 const LayerVector& currentLayers, uint32_t layerStack,
353 Region& dirtyRegion, Region& opaqueRegion);
355 void preComposition();
356 void postComposition();
357 void rebuildLayerStacks();
358 void setUpHWComposer();
359 void doComposition();
360 void doDebugFlashRegions();
361 void doDisplayComposition(const sp<const DisplayDevice>& hw,
362 const Region& dirtyRegion);
363 void doComposeSurfaces(const sp<const DisplayDevice>& hw,
364 const Region& dirty);
366 void postFramebuffer();
367 void drawWormhole(const sp<const DisplayDevice>& hw,
368 const Region& region) const;
369 GLuint getProtectedTexName() const {
370 return mProtectedTexName;
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);
392 /* ------------------------------------------------------------------------
396 // access must be protected by mStateLock
397 mutable Mutex mStateLock;
399 volatile int32_t mTransactionFlags;
400 Condition mTransactionCV;
401 bool mTransactionPending;
402 bool mAnimTransactionPending;
403 Vector< sp<Layer> > mLayersPendingRemoval;
404 SortedVector< wp<IBinder> > mGraphicBufferProducerList;
406 // protected by mStateLock (but we could use another lock)
409 // access must be protected by mInvalidateLock
410 volatile int32_t mRepaintEverything;
412 // constant members (no synchronization needed for access)
414 GLuint mProtectedTexName;
416 bool mGpuToCpuSupported;
417 sp<EventThread> mEventThread;
418 GLint mMaxViewportDims[2];
419 GLint mMaxTextureSize;
420 EGLContext mEGLContext;
421 EGLConfig mEGLConfig;
422 EGLDisplay mEGLDisplay;
423 EGLint mEGLNativeVisualId;
424 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_DISPLAY_TYPES];
426 // Can only accessed from the main thread, these members
427 // don't need synchronization
429 bool mVisibleRegionsDirty;
430 bool mHwWorkListDirty;
431 bool mAnimCompositionPending;
433 // this may only be written from the main thread with mStateLock held
434 // it may be read from other threads with mStateLock held
435 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
437 // don't use a lock for these, we don't care
440 int mDebugDisableHWC;
441 int mDebugDisableTransformHint;
442 volatile nsecs_t mDebugInSwapBuffers;
443 nsecs_t mLastSwapBufferTime;
444 volatile nsecs_t mDebugInTransaction;
445 nsecs_t mLastTransactionTime;
448 // these are thread safe
449 mutable MessageQueue mEventQueue;
450 mutable Barrier mReadyToRunBarrier;
451 FrameTracker mAnimFrameTracker;
453 // protected by mDestroyedLayerLock;
454 mutable Mutex mDestroyedLayerLock;
455 Vector<Layer const *> mDestroyedLayers;
457 /* ------------------------------------------------------------------------
458 * Feature prototyping
461 sp<IBinder> mExtDisplayToken;
464 // ---------------------------------------------------------------------------
465 }; // namespace android
467 #endif // ANDROID_SURFACE_FLINGER_H