DisplayDevice.h revision 875d8e1323536e16dcfc90c9674d7ad32116a69a
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 DisplaySurface;
41class Layer;
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    enum {
69        NO_LAYER_STACK = 0xFFFFFFFF,
70    };
71
72    DisplayDevice(
73            const sp<SurfaceFlinger>& flinger,
74            DisplayType type,
75            int32_t hwcId,  // negative for non-HWC-composited displays
76            bool isSecure,
77            const wp<IBinder>& displayToken,
78            const sp<DisplaySurface>& displaySurface,
79            EGLConfig config);
80
81    ~DisplayDevice();
82
83    // whether this is a valid object. An invalid DisplayDevice is returned
84    // when an non existing id is requested
85    bool isValid() const;
86
87    // isSecure indicates whether this display can be trusted to display
88    // secure surfaces.
89    bool isSecure() const { return mIsSecure; }
90
91    // Flip the front and back buffers if the back buffer is "dirty".  Might
92    // be instantaneous, might involve copying the frame buffer around.
93    void flip(const Region& dirty) const;
94
95    int         getWidth() const;
96    int         getHeight() const;
97    PixelFormat getFormat() const;
98    uint32_t    getFlags() const;
99
100    EGLSurface  getEGLSurface() const;
101
102    void                    setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
103    const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
104    bool                    getSecureLayerVisible() const;
105    Region                  getDirtyRegion(bool repaintEverything) const;
106
107    void                    setLayerStack(uint32_t stack);
108    void                    setProjection(int orientation, const Rect& viewport, const Rect& frame);
109
110    int                     getOrientation() const { return mOrientation; }
111    const Transform&        getTransform() const { return mGlobalTransform; }
112    const Rect              getViewport() const { return mViewport; }
113    const Rect              getFrame() const { return mFrame; }
114    const Rect&             getScissor() const { return mScissor; }
115    bool                    needsFiltering() const { return mNeedsFiltering; }
116
117    uint32_t                getLayerStack() const { return mLayerStack; }
118    int32_t                 getDisplayType() const { return mType; }
119    int32_t                 getHwcDisplayId() const { return mHwcDisplayId; }
120    const wp<IBinder>&      getDisplayToken() const { return mDisplayToken; }
121
122    void swapBuffers(HWComposer& hwc) const;
123    status_t compositionComplete() const;
124
125    // called after h/w composer has completed its set() call
126    void onSwapBuffersCompleted(HWComposer& hwc) const;
127
128    Rect getBounds() const {
129        return Rect(mDisplayWidth, mDisplayHeight);
130    }
131    inline Rect bounds() const { return getBounds(); }
132
133    void setDisplayName(const String8& displayName);
134    const String8& getDisplayName() const { return mDisplayName; }
135
136    EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
137    void setViewportAndProjection() const;
138
139    /* ------------------------------------------------------------------------
140     * blank / unblank management
141     */
142    void releaseScreen() const;
143    void acquireScreen() const;
144    bool isScreenAcquired() const;
145    bool canDraw() const;
146
147    // release HWC resources (if any) for removable displays
148    void disconnect(HWComposer& hwc);
149
150    /* ------------------------------------------------------------------------
151     * Debugging
152     */
153    uint32_t getPageFlipCount() const;
154    void dump(String8& result) const;
155
156private:
157    /*
158     *  Constants, set during initialization
159     */
160    sp<SurfaceFlinger> mFlinger;
161    DisplayType mType;
162    int32_t mHwcDisplayId;
163    wp<IBinder> mDisplayToken;
164
165    // ANativeWindow this display is rendering into
166    sp<ANativeWindow> mNativeWindow;
167    sp<DisplaySurface> mDisplaySurface;
168
169    EGLDisplay      mDisplay;
170    EGLSurface      mSurface;
171    EGLContext      mContext;
172    int             mDisplayWidth;
173    int             mDisplayHeight;
174    PixelFormat     mFormat;
175    uint32_t        mFlags;
176    mutable uint32_t mPageFlipCount;
177    String8         mDisplayName;
178    bool            mIsSecure;
179
180    /*
181     * Can only accessed from the main thread, these members
182     * don't need synchronization.
183     */
184
185    // list of visible layers on that display
186    Vector< sp<Layer> > mVisibleLayersSortedByZ;
187
188    // Whether we have a visible secure layer on this display
189    bool mSecureLayerVisible;
190
191    // Whether the screen is blanked;
192    mutable int mScreenAcquired;
193
194
195    /*
196     * Transaction state
197     */
198    static status_t orientationToTransfrom(int orientation,
199            int w, int h, Transform* tr);
200
201    uint32_t mLayerStack;
202    int mOrientation;
203    // user-provided visible area of the layer stack
204    Rect mViewport;
205    // user-provided rectangle where mViewport gets mapped to
206    Rect mFrame;
207    // pre-computed scissor to apply to the display
208    Rect mScissor;
209    Transform mGlobalTransform;
210    bool mNeedsFiltering;
211};
212
213}; // namespace android
214
215#endif // ANDROID_DISPLAY_DEVICE_H
216