2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_DISPLAY_DEVICE_H
18 #define ANDROID_DISPLAY_DEVICE_H
20 #include "Transform.h"
25 #include <ui/PixelFormat.h>
27 #include <ui/Region.h>
30 #include <EGL/eglext.h>
33 #include <binder/IBinder.h>
34 #include <utils/RefBase.h>
36 #include <utils/Mutex.h>
37 #include <utils/String8.h>
38 #include <utils/Timers.h>
40 #include <hardware/hwcomposer_defs.h>
53 class IGraphicBufferProducer;
58 class DisplayDevice : public LightRefBase<DisplayDevice>
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;
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,
78 PARTIAL_UPDATES = 0x00020000, // video driver feature
79 SWAP_RECTANGLE = 0x00080000,
83 NO_LAYER_STACK = 0xFFFFFFFF,
88 const sp<SurfaceFlinger>& flinger,
95 const wp<IBinder>& displayToken,
96 const sp<DisplaySurface>& displaySurface,
97 const sp<IGraphicBufferProducer>& producer,
99 bool supportWideColor);
104 // whether this is a valid object. An invalid DisplayDevice is returned
105 // when an non existing id is requested
106 bool isValid() const;
108 // isSecure indicates whether this display can be trusted to display
110 bool isSecure() const { return mIsSecure; }
112 // Flip the front and back buffers if the back buffer is "dirty". Might
113 // be instantaneous, might involve copying the frame buffer around.
114 void flip(const Region& dirty) const;
116 int getWidth() const;
117 int getHeight() const;
119 PixelFormat getFormat() const;
121 uint32_t getFlags() const;
123 EGLSurface getEGLSurface() const;
125 void setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
126 const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
127 Region getDirtyRegion(bool repaintEverything) const;
129 void setLayerStack(uint32_t stack);
130 void setDisplaySize(const int newWidth, const int newHeight);
131 void setProjection(int orientation, const Rect& viewport, const Rect& frame);
133 int getOrientation() const { return mOrientation; }
134 uint32_t getOrientationTransform() const;
135 static uint32_t getPrimaryDisplayOrientationTransform();
136 const Transform& getTransform() const { return mGlobalTransform; }
137 const Rect getViewport() const { return mViewport; }
138 const Rect getFrame() const { return mFrame; }
139 const Rect& getScissor() const { return mScissor; }
140 bool needsFiltering() const { return mNeedsFiltering; }
142 uint32_t getLayerStack() const { return mLayerStack; }
143 int32_t getDisplayType() const { return mType; }
144 bool isPrimary() const { return mType == DISPLAY_PRIMARY; }
145 int32_t getHwcDisplayId() const { return mHwcDisplayId; }
146 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
148 // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
149 // machine happy without actually queueing a buffer if nothing has changed
150 status_t beginFrame(bool mustRecompose) const;
152 status_t prepareFrame(HWComposer& hwc);
153 bool getWideColorSupport() const { return mDisplayHasWideColor; }
155 status_t prepareFrame(const HWComposer& hwc) const;
158 void swapBuffers(HWComposer& hwc) const;
160 status_t compositionComplete() const;
163 // called after h/w composer has completed its set() call
165 void onSwapBuffersCompleted() const;
167 void onSwapBuffersCompleted(HWComposer& hwc) const;
170 Rect getBounds() const {
171 return Rect(mDisplayWidth, mDisplayHeight);
173 inline Rect bounds() const { return getBounds(); }
175 void setDisplayName(const String8& displayName);
176 const String8& getDisplayName() const { return mDisplayName; }
178 EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
179 void setViewportAndProjection() const;
181 const sp<Fence>& getClientTargetAcquireFence() const;
183 /* ------------------------------------------------------------------------
184 * Display power mode management.
186 int getPowerMode() const;
187 void setPowerMode(int mode);
188 bool isDisplayOn() const;
191 android_color_mode_t getActiveColorMode() const;
192 void setActiveColorMode(android_color_mode_t mode);
193 void setCompositionDataSpace(android_dataspace dataspace);
196 /* ------------------------------------------------------------------------
197 * Display active config management.
199 int getActiveConfig() const;
200 void setActiveConfig(int mode);
202 // release HWC resources (if any) for removable displays
203 void disconnect(HWComposer& hwc);
205 /* ------------------------------------------------------------------------
208 uint32_t getPageFlipCount() const;
209 void dump(String8& result) const;
213 * Constants, set during initialization
215 sp<SurfaceFlinger> mFlinger;
217 int32_t mHwcDisplayId;
218 wp<IBinder> mDisplayToken;
220 // ANativeWindow this display is rendering into
221 sp<ANativeWindow> mNativeWindow;
222 sp<DisplaySurface> mDisplaySurface;
233 mutable uint32_t mPageFlipCount;
234 String8 mDisplayName;
238 * Can only accessed from the main thread, these members
239 * don't need synchronization.
242 // list of visible layers on that display
243 Vector< sp<Layer> > mVisibleLayersSortedByZ;
248 static status_t orientationToTransfrom(int orientation,
249 int w, int h, Transform* tr);
251 // The identifier of the active layer stack for this display. Several displays
252 // can use the same layer stack: A z-ordered group of layers (sometimes called
253 // "surfaces"). Any given layer can only be on a single layer stack.
254 uint32_t mLayerStack;
257 static uint32_t sPrimaryDisplayOrientation;
258 // user-provided visible area of the layer stack
260 // user-provided rectangle where mViewport gets mapped to
262 // pre-computed scissor to apply to the display
264 Transform mGlobalTransform;
265 bool mNeedsFiltering;
266 // Current power mode
268 // Current active config
271 // current active color mode
272 android_color_mode_t mActiveColorMode;
274 // Need to know if display is wide-color capable or not.
275 // Initialized by SurfaceFlinger when the DisplayDevice is created.
276 // Fed to RenderEngine during composition.
277 bool mDisplayHasWideColor;
281 struct DisplayDeviceState {
282 DisplayDeviceState() = default;
283 DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
285 bool isValid() const { return type >= 0; }
286 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
287 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
289 static std::atomic<int32_t> nextDisplayId;
290 int32_t displayId = nextDisplayId++;
291 DisplayDevice::DisplayType type = DisplayDevice::DISPLAY_ID_INVALID;
292 sp<IGraphicBufferProducer> surface;
293 uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
296 uint8_t orientation = 0;
300 bool isSecure = false;
303 }; // namespace android
305 #endif // ANDROID_DISPLAY_DEVICE_H