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