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