1/*
2 * Copyright (C) 2012 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_SURFACEFLINGERCONSUMER_H
18#define ANDROID_SURFACEFLINGERCONSUMER_H
19
20#include "DispSync.h"
21#include <gui/GLConsumer.h>
22
23namespace android {
24// ----------------------------------------------------------------------------
25
26/*
27 * This is a thin wrapper around GLConsumer.
28 */
29class SurfaceFlingerConsumer : public GLConsumer {
30public:
31    static const status_t BUFFER_REJECTED = UNKNOWN_ERROR + 8;
32
33    struct ContentsChangedListener: public FrameAvailableListener {
34        virtual void onSidebandStreamChanged() = 0;
35    };
36
37    SurfaceFlingerConsumer(const sp<IGraphicBufferConsumer>& consumer,
38            uint32_t tex)
39        : GLConsumer(consumer, tex, GLConsumer::TEXTURE_EXTERNAL, false, false),
40          mTransformToDisplayInverse(false), mSurfaceDamage(),
41          mPrevReleaseFence(Fence::NO_FENCE)
42    {}
43
44    class BufferRejecter {
45        friend class SurfaceFlingerConsumer;
46        virtual bool reject(const sp<GraphicBuffer>& buf,
47                const BufferItem& item) = 0;
48
49    protected:
50        virtual ~BufferRejecter() { }
51    };
52
53    virtual status_t acquireBufferLocked(BufferItem *item, nsecs_t presentWhen,
54            uint64_t maxFrameNumber = 0) override;
55
56    // This version of updateTexImage() takes a functor that may be used to
57    // reject the newly acquired buffer.  Unlike the GLConsumer version,
58    // this does not guarantee that the buffer has been bound to the GL
59    // texture.
60    status_t updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync,
61            bool* autoRefresh, bool* queuedBuffer,
62            uint64_t maxFrameNumber = 0);
63
64    // See GLConsumer::bindTextureImageLocked().
65    status_t bindTextureImage();
66
67    // must be called from SF main thread
68    bool getTransformToDisplayInverse() const;
69    const Region& getSurfaceDamage() const;
70
71    // Sets the contents changed listener. This should be used instead of
72    // ConsumerBase::setFrameAvailableListener().
73    void setContentsChangedListener(const wp<ContentsChangedListener>& listener);
74
75    sp<NativeHandle> getSidebandStream() const;
76
77    nsecs_t computeExpectedPresent(const DispSync& dispSync);
78
79    virtual void setReleaseFence(const sp<Fence>& fence) override;
80    sp<Fence> getPrevReleaseFence() const;
81#ifdef USE_HWC2
82    void releasePendingBuffer();
83#endif
84
85private:
86    virtual void onSidebandStreamChanged();
87
88    wp<ContentsChangedListener> mContentsChangedListener;
89
90    // Indicates this buffer must be transformed by the inverse transform of the screen
91    // it is displayed onto. This is applied after GLConsumer::mCurrentTransform.
92    // This must be set/read from SurfaceFlinger's main thread.
93    bool mTransformToDisplayInverse;
94
95    // The portion of this surface that has changed since the previous frame
96    Region mSurfaceDamage;
97
98#ifdef USE_HWC2
99    // A release that is pending on the receipt of a new release fence from
100    // presentDisplay
101    PendingRelease mPendingRelease;
102#endif
103
104    // The release fence of the already displayed buffer (previous frame).
105    sp<Fence> mPrevReleaseFence;
106};
107
108// ----------------------------------------------------------------------------
109}; // namespace android
110
111#endif // ANDROID_SURFACEFLINGERCONSUMER_H
112