OSDN Git Service

0df39a45f0cad02e00359b4801c91f14b226683a
[android-x86/frameworks-native.git] / services / surfaceflinger / SurfaceFlinger.h
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 <EGL/egl.h>
24
25 /*
26  * NOTE: Make sure this file doesn't include  anything from <gl/ > or <gl2/ >
27  */
28
29 #include <cutils/compiler.h>
30
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>
37
38 #include <binder/IMemory.h>
39
40 #include <ui/PixelFormat.h>
41 #include <ui/mat4.h>
42
43 #include <gui/ISurfaceComposer.h>
44 #include <gui/ISurfaceComposerClient.h>
45 #include <gui/OccupancyTracker.h>
46
47 #include <hardware/hwcomposer_defs.h>
48
49 #include <private/gui/LayerState.h>
50
51 #include "Barrier.h"
52 #include "DisplayDevice.h"
53 #include "DispSync.h"
54 #include "FenceTracker.h"
55 #include "FrameTracker.h"
56 #include "MessageQueue.h"
57
58 #include "DisplayHardware/HWComposer.h"
59 #include "Effects/Daltonizer.h"
60
61 #include <map>
62 #include <string>
63
64 namespace android {
65
66 // ---------------------------------------------------------------------------
67
68 class Client;
69 class DisplayEventConnection;
70 class EventThread;
71 class IGraphicBufferAlloc;
72 class Layer;
73 class LayerDim;
74 class Surface;
75 class RenderEngine;
76 class EventControlThread;
77
78 // ---------------------------------------------------------------------------
79
80 enum {
81     eTransactionNeeded        = 0x01,
82     eTraversalNeeded          = 0x02,
83     eDisplayTransactionNeeded = 0x04,
84     eTransactionMask          = 0x07
85 };
86
87 class SurfaceFlinger : public BnSurfaceComposer,
88                        private IBinder::DeathRecipient,
89                        private HWComposer::EventHandler
90 {
91 public:
92     static char const* getServiceName() ANDROID_API {
93         return "SurfaceFlinger";
94     }
95
96     SurfaceFlinger() ANDROID_API;
97
98     // must be called before clients can connect
99     void init() ANDROID_API;
100
101     // starts SurfaceFlinger main loop in the current thread
102     void run() ANDROID_API;
103
104     enum {
105         EVENT_VSYNC = HWC_EVENT_VSYNC
106     };
107
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);
110
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);
113
114     // force full composition on all displays
115     void repaintEverything();
116
117     // returns the default Display
118     sp<const DisplayDevice> getDefaultDisplayDevice() const {
119         return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
120     }
121
122     // utility function to delete a texture on the main thread
123     void deleteTextureAsync(uint32_t texture);
124
125     // enable/disable h/w composer event
126     // TODO: this should be made accessible only to EventThread
127 #ifdef USE_HWC2
128     void setVsyncEnabled(int disp, int enabled);
129 #else
130     void eventControl(int disp, int event, int enabled);
131 #endif
132
133     // called on the main thread by MessageQueue when an internal message
134     // is received
135     // TODO: this should be made accessible only to MessageQueue
136     void onMessageReceived(int32_t what);
137
138     // for debugging only
139     // TODO: this should be made accessible only to HWComposer
140     const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
141
142     RenderEngine& getRenderEngine() const {
143         return *mRenderEngine;
144     }
145
146 private:
147     friend class Client;
148     friend class DisplayEventConnection;
149     friend class Layer;
150     friend class MonitoredProducer;
151
152     // This value is specified in number of frames.  Log frame stats at most
153     // every half hour.
154     enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
155
156     static const size_t MAX_LAYERS = 4096;
157
158     // We're reference counted, never destroy SurfaceFlinger directly
159     virtual ~SurfaceFlinger();
160
161     /* ------------------------------------------------------------------------
162      * Internal data structures
163      */
164
165     class LayerVector : public SortedVector< sp<Layer> > {
166     public:
167         LayerVector();
168         LayerVector(const LayerVector& rhs);
169         virtual int do_compare(const void* lhs, const void* rhs) const;
170     };
171
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;
180         uint32_t layerStack;
181         Rect viewport;
182         Rect frame;
183         uint8_t orientation;
184         uint32_t width, height;
185         String8 displayName;
186         bool isSecure;
187     };
188
189     struct State {
190         LayerVector layersSortedByZ;
191         DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
192     };
193
194     /* ------------------------------------------------------------------------
195      * IBinder interface
196      */
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);
200
201     /* ------------------------------------------------------------------------
202      * ISurfaceComposer interface
203      */
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;
231
232     /* ------------------------------------------------------------------------
233      * DeathRecipient interface
234      */
235     virtual void binderDied(const wp<IBinder>& who);
236
237     /* ------------------------------------------------------------------------
238      * RefBase interface
239      */
240     virtual void onFirstRef();
241
242     /* ------------------------------------------------------------------------
243      * HWComposer::EventHandler interface
244      */
245     virtual void onVSyncReceived(int type, nsecs_t timestamp);
246     virtual void onHotplugReceived(int disp, bool connected);
247
248     /* ------------------------------------------------------------------------
249      * Message handling
250      */
251     void waitForEvent();
252     void signalTransaction();
253     void signalLayerUpdate();
254     void signalRefresh();
255
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);
262
263     // Returns whether the transaction actually modified any state
264     bool handleMessageTransaction();
265
266     // Returns whether a new buffer has been latched (see handlePageFlip())
267     bool handleMessageInvalidate();
268
269     void handleMessageRefresh();
270
271     void handleTransaction(uint32_t transactionFlags);
272     void handleTransactionLocked(uint32_t transactionFlags);
273
274     void updateCursorAsync();
275
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.
279      */
280     bool handlePageFlip();
281
282     /* ------------------------------------------------------------------------
283      * Transactions
284      */
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);
291
292     /* ------------------------------------------------------------------------
293      * Layer management
294      */
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);
298
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);
303
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);
307
308     // called in response to the window-manager calling
309     // ISurfaceComposerClient::destroySurface()
310     status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
311
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);
316
317     // remove a layer from SurfaceFlinger immediately
318     status_t removeLayer(const sp<Layer>& layer);
319
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);
325
326     /* ------------------------------------------------------------------------
327      * Boot animation, on/off animations and screen capture
328      */
329
330     void startBootAnim();
331
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);
337
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);
345
346     /* ------------------------------------------------------------------------
347      * EGL
348      */
349     size_t getMaxTextureSize() const;
350     size_t getMaxViewportDims() const;
351
352     /* ------------------------------------------------------------------------
353      * Display and layer stack management
354      */
355     // called when starting, or restarting after system_server death
356     void initializeDisplays();
357
358     // Create an IBinder for a builtin display and add it to current state
359     void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
360
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);
364     }
365
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);
369     }
370
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);
374
375 #ifndef USE_HWC2
376     int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
377 #endif
378
379     /* ------------------------------------------------------------------------
380      * H/W composer
381      */
382
383     HWComposer& getHwComposer() const { return *mHwc; }
384
385     /* ------------------------------------------------------------------------
386      * Compositing
387      */
388     void invalidateHwcGeometry();
389     static void computeVisibleRegions(
390             const LayerVector& currentLayers, uint32_t layerStack,
391             Region& dirtyRegion, Region& opaqueRegion);
392
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);
400
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);
404
405     void postFramebuffer();
406     void drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const;
407
408     /* ------------------------------------------------------------------------
409      * Display management
410      */
411
412     /* ------------------------------------------------------------------------
413      * VSync
414      */
415      void enableHardwareVsync();
416      void resyncToHardwareVsync(bool makeAvailable);
417      void disableHardwareVsync(bool makeUnavailable);
418 public:
419      void resyncWithRateLimit();
420 private:
421
422     /* ------------------------------------------------------------------------
423      * Debugging & dumpsys
424      */
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);
434
435     void logFrameStats();
436
437     void dumpStaticScreenStats(String8& result) const;
438
439     void recordBufferingStats(const char* layerName,
440             std::vector<OccupancyTracker::Segment>&& history);
441     void dumpBufferingStats(String8& result) const;
442
443     bool getFrameTimestamps(const Layer& layer, uint64_t frameNumber,
444             FrameTimestamps* outTimestamps);
445
446     /* ------------------------------------------------------------------------
447      * Attributes
448      */
449
450     // access must be protected by mStateLock
451     mutable Mutex mStateLock;
452     State mCurrentState;
453     volatile int32_t mTransactionFlags;
454     Condition mTransactionCV;
455     bool mTransactionPending;
456     bool mAnimTransactionPending;
457     Vector< sp<Layer> > mLayersPendingRemoval;
458     SortedVector< wp<IBinder> > mGraphicBufferProducerList;
459
460     // protected by mStateLock (but we could use another lock)
461     bool mLayersRemoved;
462
463     // access must be protected by mInvalidateLock
464     volatile int32_t mRepaintEverything;
465
466     // constant members (no synchronization needed for access)
467     HWComposer* mHwc;
468     RenderEngine* mRenderEngine;
469     nsecs_t mBootTime;
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];
477
478     // Can only accessed from the main thread, these members
479     // don't need synchronization
480     State mDrawingState;
481     bool mVisibleRegionsDirty;
482 #ifndef USE_HWC2
483     bool mHwWorkListDirty;
484 #else
485     bool mGeometryInvalid;
486 #endif
487     bool mAnimCompositionPending;
488 #ifdef USE_HWC2
489     std::vector<sp<Layer>> mLayersWithQueuedFrames;
490     sp<Fence> mPreviousPresentFence = Fence::NO_FENCE;
491     bool mHadClientComposition = false;
492 #endif
493
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;
497
498     // don't use a lock for these, we don't care
499     int mDebugRegion;
500     int mDebugDDMS;
501     int mDebugDisableHWC;
502     int mDebugDisableTransformHint;
503     volatile nsecs_t mDebugInSwapBuffers;
504     nsecs_t mLastSwapBufferTime;
505     volatile nsecs_t mDebugInTransaction;
506     nsecs_t mLastTransactionTime;
507     bool mBootFinished;
508     bool mForceFullDamage;
509     FenceTracker mFenceTracker;
510
511     // these are thread safe
512     mutable MessageQueue mEventQueue;
513     FrameTracker mAnimFrameTracker;
514     DispSync mPrimaryDispSync;
515
516     // protected by mDestroyedLayerLock;
517     mutable Mutex mDestroyedLayerLock;
518     Vector<Layer const *> mDestroyedLayers;
519
520     // protected by mHWVsyncLock
521     Mutex mHWVsyncLock;
522     bool mPrimaryHWVsyncEnabled;
523     bool mHWVsyncAvailable;
524
525     /* ------------------------------------------------------------------------
526      * Feature prototyping
527      */
528
529     Daltonizer mDaltonizer;
530     bool mDaltonize;
531
532     mat4 mColorMatrix;
533     bool mHasColorMatrix;
534
535     // Static screen stats
536     bool mHasPoweredOff;
537     static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
538     nsecs_t mFrameBuckets[NUM_BUCKETS];
539     nsecs_t mTotalTime;
540     std::atomic<nsecs_t> mLastSwapTime;
541
542     // Double- vs. triple-buffering stats
543     struct BufferingStats {
544         BufferingStats()
545           : numSegments(0),
546             totalTime(0),
547             twoBufferTime(0),
548             doubleBufferedTime(0),
549             tripleBufferedTime(0) {}
550
551         size_t numSegments;
552         nsecs_t totalTime;
553
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
557         // segment)
558         nsecs_t twoBufferTime;
559         nsecs_t doubleBufferedTime;
560         nsecs_t tripleBufferedTime;
561     };
562     mutable Mutex mBufferingStatsMutex;
563     std::unordered_map<std::string, BufferingStats> mBufferingStats;
564 };
565
566 }; // namespace android
567
568 #endif // ANDROID_SURFACE_FLINGER_H