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