Layer.h revision 4b0eba949cc026ffb2c75313042d8a7bcb3fcf86
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 <utils/Timers.h>
24
25#include <ui/GraphicBuffer.h>
26#include <ui/PixelFormat.h>
27
28#include <gui/ISurfaceComposerClient.h>
29
30#include <EGL/egl.h>
31#include <EGL/eglext.h>
32#include <GLES/gl.h>
33#include <GLES/glext.h>
34
35#include "SurfaceFlingerConsumer.h"
36#include "FrameTracker.h"
37#include "LayerBase.h"
38#include "SurfaceTextureLayer.h"
39#include "Transform.h"
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
45class Client;
46class GLExtensions;
47
48// ---------------------------------------------------------------------------
49
50/*
51 * The Layer class is essentially a LayerBase combined with a BufferQueue.
52 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
53 * Layer is first referenced.
54 *
55 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
56 * that new data has arrived.
57 */
58class Layer : public LayerBaseClient,
59              public SurfaceFlingerConsumer::FrameAvailableListener
60{
61public:
62    Layer(SurfaceFlinger* flinger, const sp<Client>& client);
63    virtual ~Layer();
64
65    virtual const char* getTypeId() const { return "Layer"; }
66
67    // the this layer's size and format
68    status_t setBuffers(uint32_t w, uint32_t h,
69            PixelFormat format, uint32_t flags=0);
70
71    bool isFixedSize() const;
72
73    // LayerBase interface
74    virtual void setGeometry(const sp<const DisplayDevice>& hw,
75            HWComposer::HWCLayerInterface& layer);
76    virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
77            HWComposer::HWCLayerInterface& layer);
78    virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
79            HWComposer::HWCLayerInterface& layer);
80    virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
81            HWComposer::HWCLayerInterface* layer);
82    virtual bool onPreComposition();
83    virtual void onPostComposition();
84
85    virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
86    virtual uint32_t doTransaction(uint32_t transactionFlags);
87    virtual Region latchBuffer(bool& recomputeVisibleRegions);
88    virtual bool isOpaque() const;
89    virtual bool isSecure() const           { return mSecure; }
90    virtual bool isProtected() const;
91    virtual void onRemoved();
92    virtual sp<Layer> getLayer() const { return const_cast<Layer*>(this); }
93    virtual void setName(const String8& name);
94    virtual bool isVisible() const;
95
96    // LayerBaseClient interface
97    virtual wp<IBinder> getSurfaceTextureBinder() const;
98
99    // only for debugging
100    inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
101
102    // Updates the transform hint in our SurfaceFlingerConsumer to match
103    // the current orientation of the display device.
104    virtual void updateTransformHint(const sp<const DisplayDevice>& hw) const;
105
106protected:
107    virtual void onFirstRef();
108    virtual void dump(String8& result, char* scratch, size_t size) const;
109    virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
110    virtual void clearStats();
111
112private:
113    // Creates an instance of ISurface for this Layer.
114    virtual sp<ISurface> createSurface();
115
116    uint32_t getEffectiveUsage(uint32_t usage) const;
117    bool isCropped() const;
118    Rect computeBufferCrop() const;
119    static bool getOpacityForFormat(uint32_t format);
120
121    // Interface implementation for SurfaceFlingerConsumer::FrameAvailableListener
122    virtual void onFrameAvailable();
123
124    // -----------------------------------------------------------------------
125
126    // constants
127    sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
128    GLuint mTextureName;
129
130    // thread-safe
131    volatile int32_t mQueuedFrames;
132    FrameTracker mFrameTracker;
133
134    // main thread
135    sp<GraphicBuffer> mActiveBuffer;
136    Rect mCurrentCrop;
137    uint32_t mCurrentTransform;
138    uint32_t mCurrentScalingMode;
139    bool mCurrentOpacity;
140    bool mRefreshPending;
141    bool mFrameLatencyNeeded;
142
143    // constants
144    PixelFormat mFormat;
145    const GLExtensions& mGLExtensions;
146    bool mOpaqueLayer;
147
148    // page-flip thread (currently main thread)
149    bool mSecure;         // no screenshots
150    bool mProtectedByApp; // application requires protected path to external sink
151};
152
153// ---------------------------------------------------------------------------
154
155}; // namespace android
156
157#endif // ANDROID_LAYER_H
158