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