DisplayDevice.h revision 9d96de49673cb6c916bfb797aad4f4e024ea73ca
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    static uint32_t         getPrimaryDisplayOrientationTransform();
133    const Transform&        getTransform() const { return mGlobalTransform; }
134    const Rect              getViewport() const { return mViewport; }
135    const Rect              getFrame() const { return mFrame; }
136    const Rect&             getScissor() const { return mScissor; }
137    bool                    needsFiltering() const { return mNeedsFiltering; }
138
139    uint32_t                getLayerStack() const { return mLayerStack; }
140    int32_t                 getDisplayType() const { return mType; }
141    int32_t                 getHwcDisplayId() const { return mHwcDisplayId; }
142    const wp<IBinder>&      getDisplayToken() const { return mDisplayToken; }
143
144    // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
145    // machine happy without actually queueing a buffer if nothing has changed
146    status_t beginFrame(bool mustRecompose) const;
147#ifdef USE_HWC2
148    status_t prepareFrame(HWComposer& hwc);
149#else
150    status_t prepareFrame(const HWComposer& hwc) const;
151#endif
152
153    void swapBuffers(HWComposer& hwc) const;
154#ifndef USE_HWC2
155    status_t compositionComplete() const;
156#endif
157
158    // called after h/w composer has completed its set() call
159#ifdef USE_HWC2
160    void onSwapBuffersCompleted() const;
161#else
162    void onSwapBuffersCompleted(HWComposer& hwc) const;
163#endif
164
165    Rect getBounds() const {
166        return Rect(mDisplayWidth, mDisplayHeight);
167    }
168    inline Rect bounds() const { return getBounds(); }
169
170    void setDisplayName(const String8& displayName);
171    const String8& getDisplayName() const { return mDisplayName; }
172
173    EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
174    void setViewportAndProjection() const;
175
176    const sp<Fence>& getClientTargetAcquireFence() const;
177
178    /* ------------------------------------------------------------------------
179     * Display power mode management.
180     */
181    int getPowerMode() const;
182    void setPowerMode(int mode);
183    bool isDisplayOn() const;
184
185#ifdef USE_HWC2
186    android_color_mode_t getActiveColorMode() const;
187    void setActiveColorMode(android_color_mode_t mode);
188#endif
189
190    /* ------------------------------------------------------------------------
191     * Display active config management.
192     */
193    int getActiveConfig() const;
194    void setActiveConfig(int mode);
195
196    // release HWC resources (if any) for removable displays
197    void disconnect(HWComposer& hwc);
198
199    /* ------------------------------------------------------------------------
200     * Debugging
201     */
202    uint32_t getPageFlipCount() const;
203    void dump(String8& result) const;
204
205private:
206    /*
207     *  Constants, set during initialization
208     */
209    sp<SurfaceFlinger> mFlinger;
210    DisplayType mType;
211    int32_t mHwcDisplayId;
212    wp<IBinder> mDisplayToken;
213
214    // ANativeWindow this display is rendering into
215    sp<ANativeWindow> mNativeWindow;
216    sp<DisplaySurface> mDisplaySurface;
217
218    EGLConfig       mConfig;
219    EGLDisplay      mDisplay;
220    EGLSurface      mSurface;
221    int             mDisplayWidth;
222    int             mDisplayHeight;
223#ifndef USE_HWC2
224    PixelFormat     mFormat;
225#endif
226    uint32_t        mFlags;
227    mutable uint32_t mPageFlipCount;
228    String8         mDisplayName;
229    bool            mIsSecure;
230
231    /*
232     * Can only accessed from the main thread, these members
233     * don't need synchronization.
234     */
235
236    // list of visible layers on that display
237    Vector< sp<Layer> > mVisibleLayersSortedByZ;
238
239    /*
240     * Transaction state
241     */
242    static status_t orientationToTransfrom(int orientation,
243            int w, int h, Transform* tr);
244
245    uint32_t mLayerStack;
246    int mOrientation;
247    static uint32_t sPrimaryDisplayOrientation;
248    // user-provided visible area of the layer stack
249    Rect mViewport;
250    // user-provided rectangle where mViewport gets mapped to
251    Rect mFrame;
252    // pre-computed scissor to apply to the display
253    Rect mScissor;
254    Transform mGlobalTransform;
255    bool mNeedsFiltering;
256    // Current power mode
257    int mPowerMode;
258    // Current active config
259    int mActiveConfig;
260#ifdef USE_HWC2
261    // current active color mode
262    android_color_mode_t mActiveColorMode;
263#endif
264};
265
266struct DisplayDeviceState {
267    DisplayDeviceState() = default;
268    DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
269
270    bool isValid() const { return type >= 0; }
271    bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
272    bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
273
274    static std::atomic<int32_t> nextDisplayId;
275    int32_t displayId = nextDisplayId++;
276    DisplayDevice::DisplayType type = DisplayDevice::DISPLAY_ID_INVALID;
277    sp<IGraphicBufferProducer> surface;
278    uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
279    Rect viewport;
280    Rect frame;
281    uint8_t orientation = 0;
282    uint32_t width = 0;
283    uint32_t height = 0;
284    String8 displayName;
285    bool isSecure = false;
286};
287
288}; // namespace android
289
290#endif // ANDROID_DISPLAY_DEVICE_H
291