DisplayDevice.h revision bae92d0d605e99a14731add4f11b72413b2835e5
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_DISPLAY_DEVICE_H
18#define ANDROID_DISPLAY_DEVICE_H
19
20#include <stdlib.h>
21
22#include <ui/PixelFormat.h>
23#include <ui/Region.h>
24
25#include <EGL/egl.h>
26#include <EGL/eglext.h>
27
28#include <utils/Mutex.h>
29#include <utils/Timers.h>
30
31#include <hardware/hwcomposer_defs.h>
32
33#include "Transform.h"
34
35struct ANativeWindow;
36
37namespace android {
38
39class DisplayInfo;
40class FramebufferSurface;
41class LayerBase;
42class SurfaceFlinger;
43class HWComposer;
44
45class DisplayDevice : public LightRefBase<DisplayDevice>
46{
47public:
48    // region in layer-stack space
49    mutable Region dirtyRegion;
50    // region in screen space
51    mutable Region swapRegion;
52    // region in screen space
53    Region undefinedRegion;
54
55    enum DisplayType {
56        DISPLAY_ID_INVALID = -1,
57        DISPLAY_PRIMARY     = HWC_DISPLAY_PRIMARY,
58        DISPLAY_EXTERNAL    = HWC_DISPLAY_EXTERNAL,
59        NUM_DISPLAY_TYPES   = HWC_NUM_DISPLAY_TYPES,
60        DISPLAY_VIRTUAL     = HWC_NUM_DISPLAY_TYPES
61    };
62
63    enum {
64        PARTIAL_UPDATES = 0x00020000, // video driver feature
65        SWAP_RECTANGLE  = 0x00080000,
66    };
67
68    DisplayDevice(
69            const sp<SurfaceFlinger>& flinger,
70            DisplayType type, const wp<IBinder>& displayToken,
71            const sp<ANativeWindow>& nativeWindow,
72            const sp<FramebufferSurface>& framebufferSurface,
73            EGLConfig config);
74
75    ~DisplayDevice();
76
77    // whether this is a valid object. An invalid DisplayDevice is returned
78    // when an non existing id is requested
79    bool isValid() const;
80
81    // Flip the front and back buffers if the back buffer is "dirty".  Might
82    // be instantaneous, might involve copying the frame buffer around.
83    void flip(const Region& dirty) const;
84
85    int         getWidth() const;
86    int         getHeight() const;
87    PixelFormat getFormat() const;
88    uint32_t    getFlags() const;
89
90    EGLSurface  getEGLSurface() const;
91
92    void                    setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers);
93    const Vector< sp<LayerBase> >& getVisibleLayersSortedByZ() const;
94    bool                    getSecureLayerVisible() const;
95    Region                  getDirtyRegion(bool repaintEverything) const;
96
97    void                    setLayerStack(uint32_t stack);
98    void                    setProjection(int orientation, const Rect& viewport, const Rect& frame);
99
100    int                     getOrientation() const { return mOrientation; }
101    const Transform&        getTransform() const { return mGlobalTransform; }
102    const Rect&             getViewport() const { return mViewport; }
103    const Rect&             getFrame() const { return mFrame; }
104    bool                    needsFiltering() const { return mNeedsFiltering; }
105
106    uint32_t                getLayerStack() const { return mLayerStack; }
107    int32_t                 getDisplayType() const { return mType; }
108    int32_t                 getHwcDisplayId() const { return mHwcDisplayId; }
109    const wp<IBinder>&      getDisplayToken() const { return mDisplayToken; }
110
111    void swapBuffers(HWComposer& hwc) const;
112    status_t compositionComplete() const;
113
114    // called after h/w composer has completed its set() call
115    void onSwapBuffersCompleted(HWComposer& hwc) const;
116
117    Rect getBounds() const {
118        return Rect(mDisplayWidth, mDisplayHeight);
119    }
120    inline Rect bounds() const { return getBounds(); }
121
122    void setDisplayName(const String8& displayName);
123    const String8& getDisplayName() const { return mDisplayName; }
124
125    static EGLBoolean makeCurrent(EGLDisplay dpy,
126            const sp<const DisplayDevice>& hw, EGLContext ctx);
127
128    static void setViewportAndProjection(const sp<const DisplayDevice>& hw);
129
130    /* ------------------------------------------------------------------------
131     * blank / unblank management
132     */
133    void releaseScreen() const;
134    void acquireScreen() const;
135    bool isScreenAcquired() const;
136    bool canDraw() const;
137
138    /* ------------------------------------------------------------------------
139     * Debugging
140     */
141    uint32_t getPageFlipCount() const;
142    void dump(String8& result, char* buffer, size_t SIZE) const;
143
144private:
145    void init(EGLConfig config);
146
147    /*
148     *  Constants, set during initialization
149     */
150    sp<SurfaceFlinger> mFlinger;
151    DisplayType mType;
152    int32_t mHwcDisplayId;
153    wp<IBinder> mDisplayToken;
154
155    // ANativeWindow this display is rendering into
156    sp<ANativeWindow> mNativeWindow;
157
158    // set if mNativeWindow is a FramebufferSurface
159    sp<FramebufferSurface> mFramebufferSurface;
160
161    EGLDisplay      mDisplay;
162    EGLSurface      mSurface;
163    EGLContext      mContext;
164    int             mDisplayWidth;
165    int             mDisplayHeight;
166    PixelFormat     mFormat;
167    uint32_t        mFlags;
168    mutable uint32_t mPageFlipCount;
169    String8         mDisplayName;
170
171    /*
172     * Can only accessed from the main thread, these members
173     * don't need synchronization.
174     */
175
176    // list of visible layers on that display
177    Vector< sp<LayerBase> > mVisibleLayersSortedByZ;
178
179    // Whether we have a visible secure layer on this display
180    bool mSecureLayerVisible;
181
182    // Whether the screen is blanked;
183    mutable int mScreenAcquired;
184
185
186    /*
187     * Transaction state
188     */
189    static status_t orientationToTransfrom(int orientation,
190            int w, int h, Transform* tr);
191
192    void updateGeometryTransform();
193
194    uint32_t mLayerStack;
195    int mOrientation;
196    Rect mViewport;
197    Rect mFrame;
198    Transform mGlobalTransform;
199    bool mNeedsFiltering;
200};
201
202}; // namespace android
203
204#endif // ANDROID_DISPLAY_DEVICE_H
205