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