DisplayDevice.h revision 2c9b11f0291210c9b9513a1a0cce6afebd361b3b
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/String8.h>
30#include <utils/Timers.h>
31
32#include <hardware/hwcomposer_defs.h>
33
34#include "Transform.h"
35
36struct ANativeWindow;
37
38namespace android {
39
40class DisplayInfo;
41class DisplaySurface;
42class IGraphicBufferProducer;
43class Layer;
44class SurfaceFlinger;
45class HWComposer;
46
47class DisplayDevice : public LightRefBase<DisplayDevice>
48{
49public:
50    // region in layer-stack space
51    mutable Region dirtyRegion;
52    // region in screen space
53    mutable Region swapRegion;
54    // region in screen space
55    Region undefinedRegion;
56
57    enum DisplayType {
58        DISPLAY_ID_INVALID = -1,
59        DISPLAY_PRIMARY     = HWC_DISPLAY_PRIMARY,
60        DISPLAY_EXTERNAL    = HWC_DISPLAY_EXTERNAL,
61        DISPLAY_VIRTUAL     = HWC_DISPLAY_VIRTUAL,
62        NUM_BUILTIN_DISPLAY_TYPES = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
63    };
64
65    enum {
66        PARTIAL_UPDATES = 0x00020000, // video driver feature
67        SWAP_RECTANGLE  = 0x00080000,
68    };
69
70    enum {
71        NO_LAYER_STACK = 0xFFFFFFFF,
72    };
73
74    DisplayDevice(
75            const sp<SurfaceFlinger>& flinger,
76            DisplayType type,
77            int32_t hwcId,  // negative for non-HWC-composited displays
78            int format,
79            bool isSecure,
80            const wp<IBinder>& displayToken,
81            const sp<DisplaySurface>& displaySurface,
82            const sp<IGraphicBufferProducer>& producer,
83            EGLConfig config);
84
85    ~DisplayDevice();
86
87    // whether this is a valid object. An invalid DisplayDevice is returned
88    // when an non existing id is requested
89    bool isValid() const;
90
91    // isSecure indicates whether this display can be trusted to display
92    // secure surfaces.
93    bool isSecure() const { return mIsSecure; }
94
95    // Flip the front and back buffers if the back buffer is "dirty".  Might
96    // be instantaneous, might involve copying the frame buffer around.
97    void flip(const Region& dirty) const;
98
99    int         getWidth() const;
100    int         getHeight() const;
101    PixelFormat getFormat() const;
102    uint32_t    getFlags() const;
103
104    EGLSurface  getEGLSurface() const;
105
106    void                    setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
107    const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
108    bool                    getSecureLayerVisible() const;
109    Region                  getDirtyRegion(bool repaintEverything) const;
110
111    void                    setLayerStack(uint32_t stack);
112    void                    setProjection(int orientation, const Rect& viewport, const Rect& frame);
113
114    int                     getOrientation() const { return mOrientation; }
115    uint32_t                getOrientationTransform() const;
116    const Transform&        getTransform() const { return mGlobalTransform; }
117    const Rect              getViewport() const { return mViewport; }
118    const Rect              getFrame() const { return mFrame; }
119    const Rect&             getScissor() const { return mScissor; }
120    bool                    needsFiltering() const { return mNeedsFiltering; }
121
122    uint32_t                getLayerStack() const { return mLayerStack; }
123    int32_t                 getDisplayType() const { return mType; }
124    int32_t                 getHwcDisplayId() const { return mHwcDisplayId; }
125    const wp<IBinder>&      getDisplayToken() const { return mDisplayToken; }
126
127    // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
128    // machine happy without actually queueing a buffer if nothing has changed
129    status_t beginFrame(bool mustRecompose) const;
130    status_t prepareFrame(const HWComposer& hwc) const;
131
132    void swapBuffers(HWComposer& hwc) const;
133    status_t compositionComplete() const;
134
135    // called after h/w composer has completed its set() call
136    void onSwapBuffersCompleted(HWComposer& hwc) const;
137
138    Rect getBounds() const {
139        return Rect(mDisplayWidth, mDisplayHeight);
140    }
141    inline Rect bounds() const { return getBounds(); }
142
143    void setDisplayName(const String8& displayName);
144    const String8& getDisplayName() const { return mDisplayName; }
145
146    EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
147    void setViewportAndProjection() const;
148
149    /* ------------------------------------------------------------------------
150     * Display power mode management.
151     */
152    int getPowerMode() const;
153    void setPowerMode(int mode);
154    bool isDisplayOn() const;
155
156    // release HWC resources (if any) for removable displays
157    void disconnect(HWComposer& hwc);
158
159    /* ------------------------------------------------------------------------
160     * Debugging
161     */
162    uint32_t getPageFlipCount() const;
163    void dump(String8& result) const;
164
165private:
166    /*
167     *  Constants, set during initialization
168     */
169    sp<SurfaceFlinger> mFlinger;
170    DisplayType mType;
171    int32_t mHwcDisplayId;
172    wp<IBinder> mDisplayToken;
173
174    // ANativeWindow this display is rendering into
175    sp<ANativeWindow> mNativeWindow;
176    sp<DisplaySurface> mDisplaySurface;
177
178    EGLDisplay      mDisplay;
179    EGLSurface      mSurface;
180    int             mDisplayWidth;
181    int             mDisplayHeight;
182    PixelFormat     mFormat;
183    uint32_t        mFlags;
184    mutable uint32_t mPageFlipCount;
185    String8         mDisplayName;
186    bool            mIsSecure;
187
188    /*
189     * Can only accessed from the main thread, these members
190     * don't need synchronization.
191     */
192
193    // list of visible layers on that display
194    Vector< sp<Layer> > mVisibleLayersSortedByZ;
195
196    // Whether we have a visible secure layer on this display
197    bool mSecureLayerVisible;
198
199
200    /*
201     * Transaction state
202     */
203    static status_t orientationToTransfrom(int orientation,
204            int w, int h, Transform* tr);
205
206    uint32_t mLayerStack;
207    int mOrientation;
208    // user-provided visible area of the layer stack
209    Rect mViewport;
210    // user-provided rectangle where mViewport gets mapped to
211    Rect mFrame;
212    // pre-computed scissor to apply to the display
213    Rect mScissor;
214    Transform mGlobalTransform;
215    bool mNeedsFiltering;
216    // Current power mode
217    int mPowerMode;
218};
219
220}; // namespace android
221
222#endif // ANDROID_DISPLAY_DEVICE_H
223