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 * NOTE: Make sure this file doesn't include anything from <gl/ > or <gl2/ >
29 #include <cutils/compiler.h>
31 #include <utils/Atomic.h>
32 #include <utils/Errors.h>
33 #include <utils/KeyedVector.h>
34 #include <utils/RefBase.h>
35 #include <utils/SortedVector.h>
36 #include <utils/threads.h>
38 #include <binder/IMemory.h>
40 #include <ui/PixelFormat.h>
43 #include <gui/ISurfaceComposer.h>
44 #include <gui/ISurfaceComposerClient.h>
45 #include <gui/OccupancyTracker.h>
47 #include <hardware/hwcomposer_defs.h>
49 #include <private/gui/LayerState.h>
52 #include "DisplayDevice.h"
54 #include "FenceTracker.h"
55 #include "FrameTracker.h"
56 #include "MessageQueue.h"
58 #include "DisplayHardware/HWComposer.h"
59 #include "Effects/Daltonizer.h"
66 // ---------------------------------------------------------------------------
69 class DisplayEventConnection;
71 class IGraphicBufferAlloc;
76 class EventControlThread;
78 // ---------------------------------------------------------------------------
81 eTransactionNeeded = 0x01,
82 eTraversalNeeded = 0x02,
83 eDisplayTransactionNeeded = 0x04,
84 eTransactionMask = 0x07
87 class SurfaceFlinger : public BnSurfaceComposer,
88 private IBinder::DeathRecipient,
89 private HWComposer::EventHandler
92 static char const* getServiceName() ANDROID_API {
93 return "SurfaceFlinger";
96 SurfaceFlinger() ANDROID_API;
98 // must be called before clients can connect
99 void init() ANDROID_API;
101 // starts SurfaceFlinger main loop in the current thread
102 void run() ANDROID_API;
105 EVENT_VSYNC = HWC_EVENT_VSYNC
108 // post an asynchronous message to the main thread
109 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
111 // post a synchronous message to the main thread
112 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
114 // force full composition on all displays
115 void repaintEverything();
117 // returns the default Display
118 sp<const DisplayDevice> getDefaultDisplayDevice() const {
119 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
122 // utility function to delete a texture on the main thread
123 void deleteTextureAsync(uint32_t texture);
125 // enable/disable h/w composer event
126 // TODO: this should be made accessible only to EventThread
128 void setVsyncEnabled(int disp, int enabled);
130 void eventControl(int disp, int event, int enabled);
133 // called on the main thread by MessageQueue when an internal message
135 // TODO: this should be made accessible only to MessageQueue
136 void onMessageReceived(int32_t what);
138 // for debugging only
139 // TODO: this should be made accessible only to HWComposer
140 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
142 RenderEngine& getRenderEngine() const {
143 return *mRenderEngine;
148 friend class DisplayEventConnection;
150 friend class MonitoredProducer;
152 // This value is specified in number of frames. Log frame stats at most
154 enum { LOG_FRAME_STATS_PERIOD = 30*60*60 };
156 static const size_t MAX_LAYERS = 4096;
158 // We're reference counted, never destroy SurfaceFlinger directly
159 virtual ~SurfaceFlinger();
161 /* ------------------------------------------------------------------------
162 * Internal data structures
165 class LayerVector : public SortedVector< sp<Layer> > {
168 LayerVector(const LayerVector& rhs);
169 virtual int do_compare(const void* lhs, const void* rhs) const;
172 struct DisplayDeviceState {
173 DisplayDeviceState();
174 DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
175 bool isValid() const { return type >= 0; }
176 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
177 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
178 DisplayDevice::DisplayType type;
179 sp<IGraphicBufferProducer> surface;
184 uint32_t width, height;
190 LayerVector layersSortedByZ;
191 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
194 /* ------------------------------------------------------------------------
197 virtual status_t onTransact(uint32_t code, const Parcel& data,
198 Parcel* reply, uint32_t flags);
199 virtual status_t dump(int fd, const Vector<String16>& args);
201 /* ------------------------------------------------------------------------
202 * ISurfaceComposer interface
204 virtual sp<ISurfaceComposerClient> createConnection();
205 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
206 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
207 virtual void destroyDisplay(const sp<IBinder>& display);
208 virtual sp<IBinder> getBuiltInDisplay(int32_t id);
209 virtual void setTransactionState(const Vector<ComposerState>& state,
210 const Vector<DisplayState>& displays, uint32_t flags);
211 virtual void bootFinished();
212 virtual bool authenticateSurfaceTexture(
213 const sp<IGraphicBufferProducer>& bufferProducer) const;
214 virtual sp<IDisplayEventConnection> createDisplayEventConnection();
215 virtual status_t captureScreen(const sp<IBinder>& display,
216 const sp<IGraphicBufferProducer>& producer,
217 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
218 uint32_t minLayerZ, uint32_t maxLayerZ,
219 bool useIdentityTransform, ISurfaceComposer::Rotation rotation);
220 virtual status_t getDisplayStats(const sp<IBinder>& display,
221 DisplayStatInfo* stats);
222 virtual status_t getDisplayConfigs(const sp<IBinder>& display,
223 Vector<DisplayInfo>* configs);
224 virtual int getActiveConfig(const sp<IBinder>& display);
225 virtual void setPowerMode(const sp<IBinder>& display, int mode);
226 virtual status_t setActiveConfig(const sp<IBinder>& display, int id);
227 virtual status_t clearAnimationFrameStats();
228 virtual status_t getAnimationFrameStats(FrameStats* outStats) const;
229 virtual status_t getHdrCapabilities(const sp<IBinder>& display,
230 HdrCapabilities* outCapabilities) const;
232 /* ------------------------------------------------------------------------
233 * DeathRecipient interface
235 virtual void binderDied(const wp<IBinder>& who);
237 /* ------------------------------------------------------------------------
240 virtual void onFirstRef();
242 /* ------------------------------------------------------------------------
243 * HWComposer::EventHandler interface
245 virtual void onVSyncReceived(int type, nsecs_t timestamp);
246 virtual void onHotplugReceived(int disp, bool connected);
248 /* ------------------------------------------------------------------------
252 void signalTransaction();
253 void signalLayerUpdate();
254 void signalRefresh();
256 // called on the main thread in response to initializeDisplays()
257 void onInitializeDisplays();
258 // called on the main thread in response to setActiveConfig()
259 void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode);
260 // called on the main thread in response to setPowerMode()
261 void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode);
263 // Returns whether the transaction actually modified any state
264 bool handleMessageTransaction();
266 // Returns whether a new buffer has been latched (see handlePageFlip())
267 bool handleMessageInvalidate();
269 void handleMessageRefresh();
271 void handleTransaction(uint32_t transactionFlags);
272 void handleTransactionLocked(uint32_t transactionFlags);
274 void updateCursorAsync();
276 /* handlePageFlip - latch a new buffer if available and compute the dirty
277 * region. Returns whether a new buffer has been latched, i.e., whether it
278 * is necessary to perform a refresh during this vsync.
280 bool handlePageFlip();
282 /* ------------------------------------------------------------------------
285 uint32_t getTransactionFlags(uint32_t flags);
286 uint32_t peekTransactionFlags(uint32_t flags);
287 uint32_t setTransactionFlags(uint32_t flags);
288 void commitTransaction();
289 uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
290 uint32_t setDisplayStateLocked(const DisplayState& s);
292 /* ------------------------------------------------------------------------
295 status_t createLayer(const String8& name, const sp<Client>& client,
296 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
297 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
299 status_t createNormalLayer(const sp<Client>& client, const String8& name,
300 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
301 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
302 sp<Layer>* outLayer);
304 status_t createDimLayer(const sp<Client>& client, const String8& name,
305 uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
306 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
308 // called in response to the window-manager calling
309 // ISurfaceComposerClient::destroySurface()
310 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
312 // called when all clients have released all their references to
313 // this layer meaning it is entirely safe to destroy all
314 // resources associated to this layer.
315 status_t onLayerDestroyed(const wp<Layer>& layer);
317 // remove a layer from SurfaceFlinger immediately
318 status_t removeLayer(const sp<Layer>& layer);
320 // add a layer to SurfaceFlinger
321 status_t addClientLayer(const sp<Client>& client,
322 const sp<IBinder>& handle,
323 const sp<IGraphicBufferProducer>& gbc,
324 const sp<Layer>& lbc);
326 /* ------------------------------------------------------------------------
327 * Boot animation, on/off animations and screen capture
330 void startBootAnim();
332 void renderScreenImplLocked(
333 const sp<const DisplayDevice>& hw,
334 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
335 uint32_t minLayerZ, uint32_t maxLayerZ,
336 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
338 status_t captureScreenImplLocked(
339 const sp<const DisplayDevice>& hw,
340 const sp<IGraphicBufferProducer>& producer,
341 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
342 uint32_t minLayerZ, uint32_t maxLayerZ,
343 bool useIdentityTransform, Transform::orientation_flags rotation,
344 bool isLocalScreenshot);
346 /* ------------------------------------------------------------------------
349 size_t getMaxTextureSize() const;
350 size_t getMaxViewportDims() const;
352 /* ------------------------------------------------------------------------
353 * Display and layer stack management
355 // called when starting, or restarting after system_server death
356 void initializeDisplays();
358 // Create an IBinder for a builtin display and add it to current state
359 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
361 // NOTE: can only be called from the main thread or with mStateLock held
362 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
363 return mDisplays.valueFor(dpy);
366 // NOTE: can only be called from the main thread or with mStateLock held
367 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
368 return mDisplays.valueFor(dpy);
371 // mark a region of a layer stack dirty. this updates the dirty
372 // region of all screens presenting this layer stack.
373 void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
376 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
379 /* ------------------------------------------------------------------------
383 HWComposer& getHwComposer() const { return *mHwc; }
385 /* ------------------------------------------------------------------------
388 void invalidateHwcGeometry();
389 static void computeVisibleRegions(
390 const LayerVector& currentLayers, uint32_t layerStack,
391 Region& dirtyRegion, Region& opaqueRegion);
393 void preComposition();
394 void postComposition(nsecs_t refreshStartTime);
395 void rebuildLayerStacks();
396 void setUpHWComposer();
397 void doComposition();
398 void doDebugFlashRegions();
399 void doDisplayComposition(const sp<const DisplayDevice>& hw, const Region& dirtyRegion);
401 // compose surfaces for display hw. this fails if using GL and the surface
402 // has been destroyed and is no longer valid.
403 bool doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty);
405 void postFramebuffer();
406 void drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const;
408 /* ------------------------------------------------------------------------
412 /* ------------------------------------------------------------------------
415 void enableHardwareVsync();
416 void resyncToHardwareVsync(bool makeAvailable);
417 void disableHardwareVsync(bool makeUnavailable);
419 void resyncWithRateLimit();
422 /* ------------------------------------------------------------------------
423 * Debugging & dumpsys
425 void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const;
426 void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const;
427 void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result);
428 void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const;
429 bool startDdmConnection();
430 static void appendSfConfigString(String8& result);
431 void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
432 const sp<const DisplayDevice>& hw,
433 uint32_t minLayerZ, uint32_t maxLayerZ);
435 void logFrameStats();
437 void dumpStaticScreenStats(String8& result) const;
439 void recordBufferingStats(const char* layerName,
440 std::vector<OccupancyTracker::Segment>&& history);
441 void dumpBufferingStats(String8& result) const;
443 bool getFrameTimestamps(const Layer& layer, uint64_t frameNumber,
444 FrameTimestamps* outTimestamps);
446 /* ------------------------------------------------------------------------
450 // access must be protected by mStateLock
451 mutable Mutex mStateLock;
453 volatile int32_t mTransactionFlags;
454 Condition mTransactionCV;
455 bool mTransactionPending;
456 bool mAnimTransactionPending;
457 Vector< sp<Layer> > mLayersPendingRemoval;
458 SortedVector< wp<IBinder> > mGraphicBufferProducerList;
460 // protected by mStateLock (but we could use another lock)
463 // access must be protected by mInvalidateLock
464 volatile int32_t mRepaintEverything;
466 // constant members (no synchronization needed for access)
468 RenderEngine* mRenderEngine;
470 bool mGpuToCpuSupported;
471 sp<EventThread> mEventThread;
472 sp<EventThread> mSFEventThread;
473 sp<EventControlThread> mEventControlThread;
474 EGLContext mEGLContext;
475 EGLDisplay mEGLDisplay;
476 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
478 // Can only accessed from the main thread, these members
479 // don't need synchronization
481 bool mVisibleRegionsDirty;
483 bool mHwWorkListDirty;
485 bool mGeometryInvalid;
487 bool mAnimCompositionPending;
489 std::vector<sp<Layer>> mLayersWithQueuedFrames;
490 sp<Fence> mPreviousPresentFence = Fence::NO_FENCE;
491 bool mHadClientComposition = false;
494 // this may only be written from the main thread with mStateLock held
495 // it may be read from other threads with mStateLock held
496 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
498 // don't use a lock for these, we don't care
501 int mDebugDisableHWC;
502 int mDebugDisableTransformHint;
503 volatile nsecs_t mDebugInSwapBuffers;
504 nsecs_t mLastSwapBufferTime;
505 volatile nsecs_t mDebugInTransaction;
506 nsecs_t mLastTransactionTime;
508 bool mForceFullDamage;
509 FenceTracker mFenceTracker;
511 // these are thread safe
512 mutable MessageQueue mEventQueue;
513 FrameTracker mAnimFrameTracker;
514 DispSync mPrimaryDispSync;
516 // protected by mDestroyedLayerLock;
517 mutable Mutex mDestroyedLayerLock;
518 Vector<Layer const *> mDestroyedLayers;
520 // protected by mHWVsyncLock
522 bool mPrimaryHWVsyncEnabled;
523 bool mHWVsyncAvailable;
525 /* ------------------------------------------------------------------------
526 * Feature prototyping
529 Daltonizer mDaltonizer;
533 bool mHasColorMatrix;
535 // Static screen stats
537 static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
538 nsecs_t mFrameBuckets[NUM_BUCKETS];
540 std::atomic<nsecs_t> mLastSwapTime;
542 // Double- vs. triple-buffering stats
543 struct BufferingStats {
548 doubleBufferedTime(0),
549 tripleBufferedTime(0) {}
554 // "Two buffer" means that a third buffer was never used, whereas
555 // "double-buffered" means that on average the segment only used two
556 // buffers (though it may have used a third for some part of the
558 nsecs_t twoBufferTime;
559 nsecs_t doubleBufferedTime;
560 nsecs_t tripleBufferedTime;
562 mutable Mutex mBufferingStatsMutex;
563 std::unordered_map<std::string, BufferingStats> mBufferingStats;
566 }; // namespace android
568 #endif // ANDROID_SURFACE_FLINGER_H