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