SurfaceFlinger.h revision c4f471e75a8ec64ec34e3f2944a5a756215d0bec
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
46#include <hardware/hwcomposer_defs.h>
47
48#include <private/gui/LayerState.h>
49
50#include "Barrier.h"
51#include "DisplayDevice.h"
52#include "DispSync.h"
53#include "FenceTracker.h"
54#include "FrameTracker.h"
55#include "MessageQueue.h"
56
57#include "DisplayHardware/HWComposer.h"
58#include "Effects/Daltonizer.h"
59
60namespace android {
61
62// ---------------------------------------------------------------------------
63
64class Client;
65class DisplayEventConnection;
66class EventThread;
67class IGraphicBufferAlloc;
68class Layer;
69class LayerDim;
70class Surface;
71class RenderEngine;
72class EventControlThread;
73
74// ---------------------------------------------------------------------------
75
76enum {
77    eTransactionNeeded        = 0x01,
78    eTraversalNeeded          = 0x02,
79    eDisplayTransactionNeeded = 0x04,
80    eTransactionMask          = 0x07
81};
82
83class SurfaceFlinger : public BnSurfaceComposer,
84                       private IBinder::DeathRecipient,
85                       private HWComposer::EventHandler
86{
87public:
88    static char const* getServiceName() ANDROID_API {
89        return "SurfaceFlinger";
90    }
91
92    SurfaceFlinger() ANDROID_API;
93
94    // must be called before clients can connect
95    void init() ANDROID_API;
96
97    // starts SurfaceFlinger main loop in the current thread
98    void run() ANDROID_API;
99
100    enum {
101        EVENT_VSYNC = HWC_EVENT_VSYNC
102    };
103
104    // post an asynchronous message to the main thread
105    status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
106
107    // post a synchronous message to the main thread
108    status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
109
110    // force full composition on all displays
111    void repaintEverything();
112
113    // returns the default Display
114    sp<const DisplayDevice> getDefaultDisplayDevice() const {
115        return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
116    }
117
118    // utility function to delete a texture on the main thread
119    void deleteTextureAsync(uint32_t texture);
120
121    // enable/disable h/w composer event
122    // TODO: this should be made accessible only to EventThread
123#ifdef USE_HWC2
124    void setVsyncEnabled(int disp, int enabled);
125#else
126    void eventControl(int disp, int event, int enabled);
127#endif
128
129    // called on the main thread by MessageQueue when an internal message
130    // is received
131    // TODO: this should be made accessible only to MessageQueue
132    void onMessageReceived(int32_t what);
133
134    // for debugging only
135    // TODO: this should be made accessible only to HWComposer
136    const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
137
138    RenderEngine& getRenderEngine() const {
139        return *mRenderEngine;
140    }
141
142private:
143    friend class Client;
144    friend class DisplayEventConnection;
145    friend class Layer;
146    friend class MonitoredProducer;
147
148    // This value is specified in number of frames.  Log frame stats at most
149    // every half hour.
150    enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
151
152    static const size_t MAX_LAYERS = 4096;
153
154    // We're reference counted, never destroy SurfaceFlinger directly
155    virtual ~SurfaceFlinger();
156
157    /* ------------------------------------------------------------------------
158     * Internal data structures
159     */
160
161    class LayerVector : public SortedVector< sp<Layer> > {
162    public:
163        LayerVector();
164        LayerVector(const LayerVector& rhs);
165        virtual int do_compare(const void* lhs, const void* rhs) const;
166    };
167
168    struct DisplayDeviceState {
169        DisplayDeviceState();
170        DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
171        bool isValid() const { return type >= 0; }
172        bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
173        bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
174        DisplayDevice::DisplayType type;
175        sp<IGraphicBufferProducer> surface;
176        uint32_t layerStack;
177        Rect viewport;
178        Rect frame;
179        uint8_t orientation;
180        uint32_t width, height;
181        String8 displayName;
182        bool isSecure;
183    };
184
185    struct State {
186        LayerVector layersSortedByZ;
187        DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
188    };
189
190    /* ------------------------------------------------------------------------
191     * IBinder interface
192     */
193    virtual status_t onTransact(uint32_t code, const Parcel& data,
194        Parcel* reply, uint32_t flags);
195    virtual status_t dump(int fd, const Vector<String16>& args);
196
197    /* ------------------------------------------------------------------------
198     * ISurfaceComposer interface
199     */
200    virtual sp<ISurfaceComposerClient> createConnection();
201    virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
202    virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
203    virtual void destroyDisplay(const sp<IBinder>& display);
204    virtual sp<IBinder> getBuiltInDisplay(int32_t id);
205    virtual void setTransactionState(const Vector<ComposerState>& state,
206            const Vector<DisplayState>& displays, uint32_t flags);
207    virtual void bootFinished();
208    virtual bool authenticateSurfaceTexture(
209        const sp<IGraphicBufferProducer>& bufferProducer) const;
210    virtual sp<IDisplayEventConnection> createDisplayEventConnection();
211    virtual status_t captureScreen(const sp<IBinder>& display,
212            const sp<IGraphicBufferProducer>& producer,
213            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
214            uint32_t minLayerZ, uint32_t maxLayerZ,
215            bool useIdentityTransform, ISurfaceComposer::Rotation rotation);
216    virtual status_t getDisplayStats(const sp<IBinder>& display,
217            DisplayStatInfo* stats);
218    virtual status_t getDisplayConfigs(const sp<IBinder>& display,
219            Vector<DisplayInfo>* configs);
220    virtual int getActiveConfig(const sp<IBinder>& display);
221    virtual void setPowerMode(const sp<IBinder>& display, int mode);
222    virtual status_t setActiveConfig(const sp<IBinder>& display, int id);
223    virtual status_t clearAnimationFrameStats();
224    virtual status_t getAnimationFrameStats(FrameStats* outStats) const;
225    virtual status_t getHdrCapabilities(const sp<IBinder>& display,
226            HdrCapabilities* outCapabilities) const;
227
228    /* ------------------------------------------------------------------------
229     * DeathRecipient interface
230     */
231    virtual void binderDied(const wp<IBinder>& who);
232
233    /* ------------------------------------------------------------------------
234     * RefBase interface
235     */
236    virtual void onFirstRef();
237
238    /* ------------------------------------------------------------------------
239     * HWComposer::EventHandler interface
240     */
241    virtual void onVSyncReceived(int type, nsecs_t timestamp);
242    virtual void onHotplugReceived(int disp, bool connected);
243
244    /* ------------------------------------------------------------------------
245     * Message handling
246     */
247    void waitForEvent();
248    void signalTransaction();
249    void signalLayerUpdate();
250    void signalRefresh();
251
252    // called on the main thread in response to initializeDisplays()
253    void onInitializeDisplays();
254    // called on the main thread in response to setActiveConfig()
255    void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode);
256    // called on the main thread in response to setPowerMode()
257    void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode);
258
259    // Returns whether the transaction actually modified any state
260    bool handleMessageTransaction();
261
262    // Returns whether a new buffer has been latched (see handlePageFlip())
263    bool handleMessageInvalidate();
264
265    void handleMessageRefresh();
266
267    void handleTransaction(uint32_t transactionFlags);
268    void handleTransactionLocked(uint32_t transactionFlags);
269
270    void updateCursorAsync();
271
272    /* handlePageFlip - latch a new buffer if available and compute the dirty
273     * region. Returns whether a new buffer has been latched, i.e., whether it
274     * is necessary to perform a refresh during this vsync.
275     */
276    bool handlePageFlip();
277
278    /* ------------------------------------------------------------------------
279     * Transactions
280     */
281    uint32_t getTransactionFlags(uint32_t flags);
282    uint32_t peekTransactionFlags(uint32_t flags);
283    uint32_t setTransactionFlags(uint32_t flags);
284    void commitTransaction();
285    uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
286    uint32_t setDisplayStateLocked(const DisplayState& s);
287
288    /* ------------------------------------------------------------------------
289     * Layer management
290     */
291    status_t createLayer(const String8& name, const sp<Client>& client,
292            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
293            sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
294
295    status_t createNormalLayer(const sp<Client>& client, const String8& name,
296            uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
297            sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
298            sp<Layer>* outLayer);
299
300    status_t createDimLayer(const sp<Client>& client, const String8& name,
301            uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
302            sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
303
304    // called in response to the window-manager calling
305    // ISurfaceComposerClient::destroySurface()
306    status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
307
308    // called when all clients have released all their references to
309    // this layer meaning it is entirely safe to destroy all
310    // resources associated to this layer.
311    status_t onLayerDestroyed(const wp<Layer>& layer);
312
313    // remove a layer from SurfaceFlinger immediately
314    status_t removeLayer(const sp<Layer>& layer);
315
316    // add a layer to SurfaceFlinger
317    status_t addClientLayer(const sp<Client>& client,
318            const sp<IBinder>& handle,
319            const sp<IGraphicBufferProducer>& gbc,
320            const sp<Layer>& lbc);
321
322    /* ------------------------------------------------------------------------
323     * Boot animation, on/off animations and screen capture
324     */
325
326    void startBootAnim();
327
328    void renderScreenImplLocked(
329            const sp<const DisplayDevice>& hw,
330            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
331            uint32_t minLayerZ, uint32_t maxLayerZ,
332            bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
333
334    status_t captureScreenImplLocked(
335            const sp<const DisplayDevice>& hw,
336            const sp<IGraphicBufferProducer>& producer,
337            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
338            uint32_t minLayerZ, uint32_t maxLayerZ,
339            bool useIdentityTransform, Transform::orientation_flags rotation,
340            bool isLocalScreenshot);
341
342    /* ------------------------------------------------------------------------
343     * EGL
344     */
345    size_t getMaxTextureSize() const;
346    size_t getMaxViewportDims() const;
347
348    /* ------------------------------------------------------------------------
349     * Display and layer stack management
350     */
351    // called when starting, or restarting after system_server death
352    void initializeDisplays();
353
354    // Create an IBinder for a builtin display and add it to current state
355    void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
356
357    // NOTE: can only be called from the main thread or with mStateLock held
358    sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
359        return mDisplays.valueFor(dpy);
360    }
361
362    // NOTE: can only be called from the main thread or with mStateLock held
363    sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
364        return mDisplays.valueFor(dpy);
365    }
366
367    // mark a region of a layer stack dirty. this updates the dirty
368    // region of all screens presenting this layer stack.
369    void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
370
371#ifndef USE_HWC2
372    int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
373#endif
374
375    /* ------------------------------------------------------------------------
376     * H/W composer
377     */
378
379    HWComposer& getHwComposer() const { return *mHwc; }
380
381    /* ------------------------------------------------------------------------
382     * Compositing
383     */
384    void invalidateHwcGeometry();
385    static void computeVisibleRegions(
386            const LayerVector& currentLayers, uint32_t layerStack,
387            Region& dirtyRegion, Region& opaqueRegion);
388
389    void preComposition();
390    void postComposition(nsecs_t refreshStartTime);
391    void rebuildLayerStacks();
392    void setUpHWComposer();
393    void doComposition();
394    void doDebugFlashRegions();
395    void doDisplayComposition(const sp<const DisplayDevice>& hw, const Region& dirtyRegion);
396
397    // compose surfaces for display hw. this fails if using GL and the surface
398    // has been destroyed and is no longer valid.
399    bool doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty);
400
401    void postFramebuffer();
402    void drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const;
403
404    /* ------------------------------------------------------------------------
405     * Display management
406     */
407
408    /* ------------------------------------------------------------------------
409     * VSync
410     */
411     void enableHardwareVsync();
412     void disableHardwareVsync(bool makeUnavailable);
413     void resyncToHardwareVsync(bool makeAvailable);
414
415    /* ------------------------------------------------------------------------
416     * Debugging & dumpsys
417     */
418    void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const;
419    void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const;
420    void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result);
421    void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const;
422    bool startDdmConnection();
423    static void appendSfConfigString(String8& result);
424    void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
425            const sp<const DisplayDevice>& hw,
426            uint32_t minLayerZ, uint32_t maxLayerZ);
427
428    void logFrameStats();
429
430    void dumpStaticScreenStats(String8& result) const;
431
432    /* ------------------------------------------------------------------------
433     * Attributes
434     */
435
436    // access must be protected by mStateLock
437    mutable Mutex mStateLock;
438    State mCurrentState;
439    volatile int32_t mTransactionFlags;
440    Condition mTransactionCV;
441    bool mTransactionPending;
442    bool mAnimTransactionPending;
443    Vector< sp<Layer> > mLayersPendingRemoval;
444    SortedVector< wp<IBinder> > mGraphicBufferProducerList;
445
446    // protected by mStateLock (but we could use another lock)
447    bool mLayersRemoved;
448
449    // access must be protected by mInvalidateLock
450    volatile int32_t mRepaintEverything;
451
452    // constant members (no synchronization needed for access)
453    HWComposer* mHwc;
454    RenderEngine* mRenderEngine;
455    nsecs_t mBootTime;
456    bool mGpuToCpuSupported;
457    bool mDropMissedFrames;
458    sp<EventThread> mEventThread;
459    sp<EventThread> mSFEventThread;
460    sp<EventControlThread> mEventControlThread;
461    EGLContext mEGLContext;
462    EGLDisplay mEGLDisplay;
463    sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
464
465    // Can only accessed from the main thread, these members
466    // don't need synchronization
467    State mDrawingState;
468    bool mVisibleRegionsDirty;
469#ifndef USE_HWC2
470    bool mHwWorkListDirty;
471#else
472    bool mGeometryInvalid;
473#endif
474    bool mAnimCompositionPending;
475#ifdef USE_HWC2
476    std::vector<sp<Layer>> mLayersWithQueuedFrames;
477#endif
478
479    // this may only be written from the main thread with mStateLock held
480    // it may be read from other threads with mStateLock held
481    DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
482
483    // don't use a lock for these, we don't care
484    int mDebugRegion;
485    int mDebugDDMS;
486    int mDebugDisableHWC;
487    int mDebugDisableTransformHint;
488    volatile nsecs_t mDebugInSwapBuffers;
489    nsecs_t mLastSwapBufferTime;
490    volatile nsecs_t mDebugInTransaction;
491    nsecs_t mLastTransactionTime;
492    bool mBootFinished;
493    bool mForceFullDamage;
494    FenceTracker mFenceTracker;
495
496    // these are thread safe
497    mutable MessageQueue mEventQueue;
498    FrameTracker mAnimFrameTracker;
499    DispSync mPrimaryDispSync;
500
501    // protected by mDestroyedLayerLock;
502    mutable Mutex mDestroyedLayerLock;
503    Vector<Layer const *> mDestroyedLayers;
504
505    // protected by mHWVsyncLock
506    Mutex mHWVsyncLock;
507    bool mPrimaryHWVsyncEnabled;
508    bool mHWVsyncAvailable;
509
510    /* ------------------------------------------------------------------------
511     * Feature prototyping
512     */
513
514    Daltonizer mDaltonizer;
515    bool mDaltonize;
516
517    mat4 mColorMatrix;
518    bool mHasColorMatrix;
519
520    // Static screen stats
521    bool mHasPoweredOff;
522    static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
523    nsecs_t mFrameBuckets[NUM_BUCKETS];
524    nsecs_t mTotalTime;
525    nsecs_t mLastSwapTime;
526};
527
528}; // namespace android
529
530#endif // ANDROID_SURFACE_FLINGER_H
531