Layer.h revision 71bded513d37a6c1260b4a62c69ecc0d24be95f7
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_LAYER_H
18#define ANDROID_LAYER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
26#include <utils/RefBase.h>
27#include <utils/String8.h>
28#include <utils/Timers.h>
29
30#include <gfx/FloatRect.h>
31
32#include <ui/FrameStats.h>
33#include <ui/GraphicBuffer.h>
34#include <ui/PixelFormat.h>
35#include <ui/Region.h>
36
37#include <gui/ISurfaceComposerClient.h>
38
39#include <private/gui/LayerState.h>
40
41#include <list>
42
43#include "FrameTracker.h"
44#include "Client.h"
45#include "MonitoredProducer.h"
46#include "SurfaceFlinger.h"
47#include "SurfaceFlingerConsumer.h"
48#include "Transform.h"
49
50#include "DisplayHardware/HWComposer.h"
51#include "RenderEngine/Mesh.h"
52#include "RenderEngine/Texture.h"
53
54namespace android {
55
56// ---------------------------------------------------------------------------
57
58class Client;
59class Colorizer;
60class DisplayDevice;
61class GraphicBuffer;
62class SurfaceFlinger;
63
64// ---------------------------------------------------------------------------
65
66/*
67 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
68 * Layer is first referenced.
69 *
70 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
71 * that new data has arrived.
72 */
73class Layer : public SurfaceFlingerConsumer::ContentsChangedListener {
74    static int32_t sSequence;
75
76public:
77    mutable bool contentDirty;
78    // regions below are in window-manager space
79    Region visibleRegion;
80    Region coveredRegion;
81    Region visibleNonTransparentRegion;
82    Region surfaceDamageRegion;
83
84    // Layer serial number.  This gives layers an explicit ordering, so we
85    // have a stable sort order when their layer stack and Z-order are
86    // the same.
87    int32_t sequence;
88
89    enum { // flags for doTransaction()
90        eDontUpdateGeometryState = 0x00000001,
91        eVisibleRegion = 0x00000002,
92    };
93
94    struct Geometry {
95        uint32_t w;
96        uint32_t h;
97        Transform transform;
98
99        inline bool operator ==(const Geometry& rhs) const {
100            return (w == rhs.w && h == rhs.h) &&
101                    (transform.tx() == rhs.transform.tx()) &&
102                    (transform.ty() == rhs.transform.ty());
103        }
104        inline bool operator !=(const Geometry& rhs) const {
105            return !operator ==(rhs);
106        }
107    };
108
109    struct State {
110        Geometry active;
111        Geometry requested;
112        uint32_t z;
113        uint32_t layerStack;
114#ifdef USE_HWC2
115        float alpha;
116#else
117        uint8_t alpha;
118#endif
119        uint8_t flags;
120        uint8_t mask;
121        uint8_t reserved[2];
122        int32_t sequence; // changes when visible regions can change
123        bool modified;
124
125        Rect crop;
126        Rect requestedCrop;
127
128        Rect finalCrop;
129
130        // If set, defers this state update until the Layer identified by handle
131        // receives a frame with the given frameNumber
132        wp<IBinder> handle;
133        uint64_t frameNumber;
134
135        // the transparentRegion hint is a bit special, it's latched only
136        // when we receive a buffer -- this is because it's "content"
137        // dependent.
138        Region activeTransparentRegion;
139        Region requestedTransparentRegion;
140    };
141
142    // -----------------------------------------------------------------------
143
144    Layer(SurfaceFlinger* flinger, const sp<Client>& client,
145            const String8& name, uint32_t w, uint32_t h, uint32_t flags);
146
147    virtual ~Layer();
148
149    // the this layer's size and format
150    status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
151
152    // modify current state
153    bool setPosition(float x, float y, bool immediate);
154    bool setLayer(uint32_t z);
155    bool setSize(uint32_t w, uint32_t h);
156#ifdef USE_HWC2
157    bool setAlpha(float alpha);
158#else
159    bool setAlpha(uint8_t alpha);
160#endif
161    bool setMatrix(const layer_state_t::matrix22_t& matrix);
162    bool setTransparentRegionHint(const Region& transparent);
163    bool setFlags(uint8_t flags, uint8_t mask);
164    bool setCrop(const Rect& crop, bool immediate);
165    bool setFinalCrop(const Rect& crop);
166    bool setLayerStack(uint32_t layerStack);
167    void deferTransactionUntil(const sp<IBinder>& handle, uint64_t frameNumber);
168    bool setOverrideScalingMode(int32_t overrideScalingMode);
169
170    // If we have received a new buffer this frame, we will pass its surface
171    // damage down to hardware composer. Otherwise, we must send a region with
172    // one empty rect.
173    void useSurfaceDamage();
174    void useEmptyDamage();
175
176    uint32_t getTransactionFlags(uint32_t flags);
177    uint32_t setTransactionFlags(uint32_t flags);
178
179    void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
180            bool useIdentityTransform) const;
181    Rect computeBounds(const Region& activeTransparentRegion) const;
182    Rect computeBounds() const;
183
184    int32_t getSequence() const { return sequence; }
185
186    // -----------------------------------------------------------------------
187    // Virtuals
188
189    virtual const char* getTypeId() const { return "Layer"; }
190
191    /*
192     * isOpaque - true if this surface is opaque
193     *
194     * This takes into account the buffer format (i.e. whether or not the
195     * pixel format includes an alpha channel) and the "opaque" flag set
196     * on the layer.  It does not examine the current plane alpha value.
197     */
198    virtual bool isOpaque(const Layer::State& s) const;
199
200    /*
201     * isSecure - true if this surface is secure, that is if it prevents
202     * screenshots or VNC servers.
203     */
204    virtual bool isSecure() const;
205
206    /*
207     * isProtected - true if the layer may contain protected content in the
208     * GRALLOC_USAGE_PROTECTED sense.
209     */
210    virtual bool isProtected() const;
211
212    /*
213     * isVisible - true if this layer is visible, false otherwise
214     */
215    virtual bool isVisible() const;
216
217    /*
218     * isFixedSize - true if content has a fixed size
219     */
220    virtual bool isFixedSize() const;
221
222protected:
223    /*
224     * onDraw - draws the surface.
225     */
226    virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
227            bool useIdentityTransform) const;
228
229public:
230    // -----------------------------------------------------------------------
231
232#ifdef USE_HWC2
233    void setGeometry(const sp<const DisplayDevice>& displayDevice);
234    void forceClientComposition(int32_t hwcId);
235    void setPerFrameData(const sp<const DisplayDevice>& displayDevice);
236
237    // callIntoHwc exists so we can update our local state and call
238    // acceptDisplayChanges without unnecessarily updating the device's state
239    void setCompositionType(int32_t hwcId, HWC2::Composition type,
240            bool callIntoHwc = true);
241    HWC2::Composition getCompositionType(int32_t hwcId) const;
242
243    void setClearClientTarget(int32_t hwcId, bool clear);
244    bool getClearClientTarget(int32_t hwcId) const;
245
246    void updateCursorPosition(const sp<const DisplayDevice>& hw);
247#else
248    void setGeometry(const sp<const DisplayDevice>& hw,
249            HWComposer::HWCLayerInterface& layer);
250    void setPerFrameData(const sp<const DisplayDevice>& hw,
251            HWComposer::HWCLayerInterface& layer);
252    void setAcquireFence(const sp<const DisplayDevice>& hw,
253            HWComposer::HWCLayerInterface& layer);
254
255    Rect getPosition(const sp<const DisplayDevice>& hw);
256#endif
257
258    /*
259     * called after page-flip
260     */
261#ifdef USE_HWC2
262    void onLayerDisplayed(const sp<Fence>& releaseFence);
263#else
264    void onLayerDisplayed(const sp<const DisplayDevice>& hw,
265            HWComposer::HWCLayerInterface* layer);
266#endif
267
268    bool shouldPresentNow(const DispSync& dispSync) const;
269
270    /*
271     * called before composition.
272     * returns true if the layer has pending updates.
273     */
274    bool onPreComposition(nsecs_t refreshStartTime);
275
276    /*
277     * called after composition.
278     * returns true if the layer latched a new buffer this frame.
279     */
280    bool onPostComposition(
281            const std::shared_ptr<FenceTime>& glDoneFence,
282            const std::shared_ptr<FenceTime>& presentFence,
283            const std::shared_ptr<FenceTime>& retireFence);
284
285#ifdef USE_HWC2
286    // If a buffer was replaced this frame, release the former buffer
287    void releasePendingBuffer();
288#endif
289
290    /*
291     * draw - performs some global clipping optimizations
292     * and calls onDraw().
293     */
294    void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
295    void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const;
296    void draw(const sp<const DisplayDevice>& hw) const;
297
298    /*
299     * doTransaction - process the transaction. This is a good place to figure
300     * out which attributes of the surface have changed.
301     */
302    uint32_t doTransaction(uint32_t transactionFlags);
303
304    /*
305     * setVisibleRegion - called to set the new visible region. This gives
306     * a chance to update the new visible region or record the fact it changed.
307     */
308    void setVisibleRegion(const Region& visibleRegion);
309
310    /*
311     * setCoveredRegion - called when the covered region changes. The covered
312     * region corresponds to any area of the surface that is covered
313     * (transparently or not) by another surface.
314     */
315    void setCoveredRegion(const Region& coveredRegion);
316
317    /*
318     * setVisibleNonTransparentRegion - called when the visible and
319     * non-transparent region changes.
320     */
321    void setVisibleNonTransparentRegion(const Region&
322            visibleNonTransparentRegion);
323
324    /*
325     * latchBuffer - called each time the screen is redrawn and returns whether
326     * the visible regions need to be recomputed (this is a fairly heavy
327     * operation, so this should be set only if needed). Typically this is used
328     * to figure out if the content or size of a surface has changed.
329     */
330    Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime);
331
332    bool isPotentialCursor() const { return mPotentialCursor;}
333
334    /*
335     * called with the state lock when the surface is removed from the
336     * current list
337     */
338    void onRemoved();
339
340
341    // Updates the transform hint in our SurfaceFlingerConsumer to match
342    // the current orientation of the display device.
343    void updateTransformHint(const sp<const DisplayDevice>& hw) const;
344
345    /*
346     * returns the rectangle that crops the content of the layer and scales it
347     * to the layer's size.
348     */
349    Rect getContentCrop() const;
350
351    /*
352     * Returns if a frame is queued.
353     */
354    bool hasQueuedFrame() const { return mQueuedFrames > 0 ||
355            mSidebandStreamChanged || mAutoRefresh; }
356
357#ifdef USE_HWC2
358    // -----------------------------------------------------------------------
359
360    bool hasHwcLayer(int32_t hwcId) {
361        if (mHwcLayers.count(hwcId) == 0) {
362            return false;
363        }
364        if (mHwcLayers[hwcId].layer->isAbandoned()) {
365            ALOGI("Erasing abandoned layer %s on %d", mName.string(), hwcId);
366            mHwcLayers.erase(hwcId);
367            return false;
368        }
369        return true;
370    }
371
372    std::shared_ptr<HWC2::Layer> getHwcLayer(int32_t hwcId) {
373        if (mHwcLayers.count(hwcId) == 0) {
374            return nullptr;
375        }
376        return mHwcLayers[hwcId].layer;
377    }
378
379    void setHwcLayer(int32_t hwcId, std::shared_ptr<HWC2::Layer>&& layer) {
380        if (layer) {
381            mHwcLayers[hwcId].layer = layer;
382        } else {
383            mHwcLayers.erase(hwcId);
384        }
385    }
386
387#endif
388    // -----------------------------------------------------------------------
389
390    void clearWithOpenGL(const sp<const DisplayDevice>& hw) const;
391    void setFiltering(bool filtering);
392    bool getFiltering() const;
393
394    // only for debugging
395    inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
396
397    inline  const State&    getDrawingState() const { return mDrawingState; }
398    inline  const State&    getCurrentState() const { return mCurrentState; }
399    inline  State&          getCurrentState()       { return mCurrentState; }
400
401
402    /* always call base class first */
403    void dump(String8& result, Colorizer& colorizer) const;
404#ifdef USE_HWC2
405    static void miniDumpHeader(String8& result);
406    void miniDump(String8& result, int32_t hwcId) const;
407#endif
408    void dumpFrameStats(String8& result) const;
409    void dumpFrameEvents(String8& result);
410    void clearFrameStats();
411    void logFrameStats();
412    void getFrameStats(FrameStats* outStats) const;
413
414    std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush);
415
416    void addAndGetFrameTimestamps(const NewFrameEventsEntry* newEntry,
417            FrameEventHistoryDelta* outDelta);
418
419    bool getTransformToDisplayInverse() const;
420
421protected:
422    // constant
423    sp<SurfaceFlinger> mFlinger;
424    /*
425     * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
426     * is called.
427     */
428    class LayerCleaner {
429        sp<SurfaceFlinger> mFlinger;
430        wp<Layer> mLayer;
431    protected:
432        ~LayerCleaner() {
433            // destroy client resources
434            mFlinger->onLayerDestroyed(mLayer);
435        }
436    public:
437        LayerCleaner(const sp<SurfaceFlinger>& flinger,
438                const sp<Layer>& layer)
439            : mFlinger(flinger), mLayer(layer) {
440        }
441    };
442
443
444    virtual void onFirstRef();
445
446
447
448private:
449    friend class SurfaceInterceptor;
450    // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
451    virtual void onFrameAvailable(const BufferItem& item) override;
452    virtual void onFrameReplaced(const BufferItem& item) override;
453    virtual void onSidebandStreamChanged() override;
454
455    void commitTransaction(const State& stateToCommit);
456
457    // needsLinearFiltering - true if this surface's state requires filtering
458    bool needsFiltering(const sp<const DisplayDevice>& hw) const;
459
460    uint32_t getEffectiveUsage(uint32_t usage) const;
461    gfx::FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
462    bool isCropped() const;
463    static bool getOpacityForFormat(uint32_t format);
464
465    // drawing
466    void clearWithOpenGL(const sp<const DisplayDevice>& hw,
467            float r, float g, float b, float alpha) const;
468    void drawWithOpenGL(const sp<const DisplayDevice>& hw,
469            bool useIdentityTransform) const;
470
471    // Temporary - Used only for LEGACY camera mode.
472    uint32_t getProducerStickyTransform() const;
473
474    // Loads the corresponding system property once per process
475    static bool latchUnsignaledBuffers();
476
477    // -----------------------------------------------------------------------
478
479    class SyncPoint
480    {
481    public:
482        explicit SyncPoint(uint64_t frameNumber) : mFrameNumber(frameNumber),
483                mFrameIsAvailable(false), mTransactionIsApplied(false) {}
484
485        uint64_t getFrameNumber() const {
486            return mFrameNumber;
487        }
488
489        bool frameIsAvailable() const {
490            return mFrameIsAvailable;
491        }
492
493        void setFrameAvailable() {
494            mFrameIsAvailable = true;
495        }
496
497        bool transactionIsApplied() const {
498            return mTransactionIsApplied;
499        }
500
501        void setTransactionApplied() {
502            mTransactionIsApplied = true;
503        }
504
505    private:
506        const uint64_t mFrameNumber;
507        std::atomic<bool> mFrameIsAvailable;
508        std::atomic<bool> mTransactionIsApplied;
509    };
510
511    // SyncPoints which will be signaled when the correct frame is at the head
512    // of the queue and dropped after the frame has been latched. Protected by
513    // mLocalSyncPointMutex.
514    Mutex mLocalSyncPointMutex;
515    std::list<std::shared_ptr<SyncPoint>> mLocalSyncPoints;
516
517    // SyncPoints which will be signaled and then dropped when the transaction
518    // is applied
519    std::list<std::shared_ptr<SyncPoint>> mRemoteSyncPoints;
520
521    uint64_t getHeadFrameNumber() const;
522    bool headFenceHasSignaled() const;
523
524    // Returns false if the relevant frame has already been latched
525    bool addSyncPoint(const std::shared_ptr<SyncPoint>& point);
526
527    void pushPendingState();
528    void popPendingState(State* stateToCommit);
529    bool applyPendingStates(State* stateToCommit);
530
531    // Returns mCurrentScaling mode (originating from the
532    // Client) or mOverrideScalingMode mode (originating from
533    // the Surface Controller) if set.
534    uint32_t getEffectiveScalingMode() const;
535public:
536    /*
537     * The layer handle is just a BBinder object passed to the client
538     * (remote process) -- we don't keep any reference on our side such that
539     * the dtor is called when the remote side let go of its reference.
540     *
541     * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
542     * this layer when the handle is destroyed.
543     */
544    class Handle : public BBinder, public LayerCleaner {
545        public:
546            Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
547                : LayerCleaner(flinger, layer), owner(layer) {}
548
549            wp<Layer> owner;
550    };
551
552    sp<IBinder> getHandle();
553    sp<IGraphicBufferProducer> getProducer() const;
554    const String8& getName() const;
555    void notifyAvailableFrames();
556private:
557
558    // -----------------------------------------------------------------------
559
560    // Check all of the local sync points to ensure that all transactions
561    // which need to have been applied prior to the frame which is about to
562    // be latched have signaled
563    bool allTransactionsSignaled();
564
565    // constants
566    sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
567    sp<IGraphicBufferProducer> mProducer;
568    uint32_t mTextureName;      // from GLES
569    bool mPremultipliedAlpha;
570    String8 mName;
571    PixelFormat mFormat;
572
573    // these are protected by an external lock
574    State mCurrentState;
575    State mDrawingState;
576    volatile int32_t mTransactionFlags;
577
578    // Accessed from main thread and binder threads
579    Mutex mPendingStateMutex;
580    Vector<State> mPendingStates;
581
582    // thread-safe
583    volatile int32_t mQueuedFrames;
584    volatile int32_t mSidebandStreamChanged; // used like an atomic boolean
585
586    // Timestamp history for UIAutomation. Thread safe.
587    FrameTracker mFrameTracker;
588
589    // Timestamp history for the consumer to query.
590    // Accessed by both consumer and producer on main and binder threads.
591    Mutex mFrameEventHistoryMutex;
592    ConsumerFrameEventHistory mFrameEventHistory;
593    FenceTimeline mAcquireTimeline;
594    FenceTimeline mReleaseTimeline;
595
596    // main thread
597    sp<GraphicBuffer> mActiveBuffer;
598    sp<NativeHandle> mSidebandStream;
599    Rect mCurrentCrop;
600    uint32_t mCurrentTransform;
601    uint32_t mCurrentScalingMode;
602    // We encode unset as -1.
603    int32_t mOverrideScalingMode;
604    bool mCurrentOpacity;
605    bool mBufferLatched = false;  // TODO: Use mActiveBuffer?
606    std::atomic<uint64_t> mCurrentFrameNumber;
607    uint64_t mPreviousFrameNumber; // Only accessed on the main thread.
608    bool mRefreshPending;
609    bool mFrameLatencyNeeded;
610    // Whether filtering is forced on or not
611    bool mFiltering;
612    // Whether filtering is needed b/c of the drawingstate
613    bool mNeedsFiltering;
614    // The mesh used to draw the layer in GLES composition mode
615    mutable Mesh mMesh;
616    // The texture used to draw the layer in GLES composition mode
617    mutable Texture mTexture;
618
619#ifdef USE_HWC2
620    // HWC items, accessed from the main thread
621    struct HWCInfo {
622        HWCInfo()
623          : layer(),
624            forceClientComposition(false),
625            compositionType(HWC2::Composition::Invalid),
626            clearClientTarget(false) {}
627
628        std::shared_ptr<HWC2::Layer> layer;
629        bool forceClientComposition;
630        HWC2::Composition compositionType;
631        bool clearClientTarget;
632        Rect displayFrame;
633        gfx::FloatRect sourceCrop;
634    };
635    std::unordered_map<int32_t, HWCInfo> mHwcLayers;
636#else
637    bool mIsGlesComposition;
638#endif
639
640    // page-flip thread (currently main thread)
641    bool mProtectedByApp; // application requires protected path to external sink
642
643    // protected by mLock
644    mutable Mutex mLock;
645    // Set to true once we've returned this surface's handle
646    mutable bool mHasSurface;
647    const wp<Client> mClientRef;
648
649    // This layer can be a cursor on some displays.
650    bool mPotentialCursor;
651
652    // Local copy of the queued contents of the incoming BufferQueue
653    mutable Mutex mQueueItemLock;
654    Condition mQueueItemCondition;
655    Vector<BufferItem> mQueueItems;
656    std::atomic<uint64_t> mLastFrameNumberReceived;
657    bool mUpdateTexImageFailed; // This is only accessed on the main thread.
658
659    bool mAutoRefresh;
660    bool mFreezePositionUpdates;
661};
662
663// ---------------------------------------------------------------------------
664
665}; // namespace android
666
667#endif // ANDROID_LAYER_H
668