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