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