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    /* ------------------------------------------------------------------------
186     * Display active config management.
187     */
188    int getActiveConfig() const;
189    void setActiveConfig(int mode);
190
191    // release HWC resources (if any) for removable displays
192    void disconnect(HWComposer& hwc);
193
194    /* ------------------------------------------------------------------------
195     * Debugging
196     */
197    uint32_t getPageFlipCount() const;
198    void dump(String8& result) const;
199
200private:
201    /*
202     *  Constants, set during initialization
203     */
204    sp<SurfaceFlinger> mFlinger;
205    DisplayType mType;
206    int32_t mHwcDisplayId;
207    wp<IBinder> mDisplayToken;
208
209    // ANativeWindow this display is rendering into
210    sp<ANativeWindow> mNativeWindow;
211    sp<DisplaySurface> mDisplaySurface;
212
213    EGLConfig       mConfig;
214    EGLDisplay      mDisplay;
215    EGLSurface      mSurface;
216    int             mDisplayWidth;
217    int             mDisplayHeight;
218#ifndef USE_HWC2
219    PixelFormat     mFormat;
220#endif
221    uint32_t        mFlags;
222    mutable uint32_t mPageFlipCount;
223    String8         mDisplayName;
224    bool            mIsSecure;
225
226    /*
227     * Can only accessed from the main thread, these members
228     * don't need synchronization.
229     */
230
231    // list of visible layers on that display
232    Vector< sp<Layer> > mVisibleLayersSortedByZ;
233
234    /*
235     * Transaction state
236     */
237    static status_t orientationToTransfrom(int orientation,
238            int w, int h, Transform* tr);
239
240    uint32_t mLayerStack;
241    int mOrientation;
242    static uint32_t sPrimaryDisplayOrientation;
243    // user-provided visible area of the layer stack
244    Rect mViewport;
245    // user-provided rectangle where mViewport gets mapped to
246    Rect mFrame;
247    // pre-computed scissor to apply to the display
248    Rect mScissor;
249    Transform mGlobalTransform;
250    bool mNeedsFiltering;
251    // Current power mode
252    int mPowerMode;
253    // Current active config
254    int mActiveConfig;
255};
256
257}; // namespace android
258
259#endif // ANDROID_DISPLAY_DEVICE_H
260