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