blob: 92ede08f749af41f9e8f86835189ef6ab8a0a1e9 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Mathias Agopiand3ee2312012-08-02 14:01:42 -070017#ifndef ANDROID_DISPLAY_DEVICE_H
18#define ANDROID_DISPLAY_DEVICE_H
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019
Dan Stoza9e56aa02015-11-02 13:00:03 -080020#include "Transform.h"
21
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <stdlib.h>
23
Dan Stoza9e56aa02015-11-02 13:00:03 -080024#ifndef USE_HWC2
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <ui/PixelFormat.h>
Dan Stoza9e56aa02015-11-02 13:00:03 -080026#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <ui/Region.h>
28
29#include <EGL/egl.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030#include <EGL/eglext.h>
31
Dan Stoza9e56aa02015-11-02 13:00:03 -080032#ifdef USE_HWC2
33#include <binder/IBinder.h>
34#include <utils/RefBase.h>
35#endif
Mathias Agopian86303202012-07-24 22:46:10 -070036#include <utils/Mutex.h>
Mathias Agopian4f4f0942013-08-19 17:26:18 -070037#include <utils/String8.h>
Mathias Agopian86303202012-07-24 22:46:10 -070038#include <utils/Timers.h>
39
Mathias Agopianf4358632012-08-22 17:16:19 -070040#include <hardware/hwcomposer_defs.h>
41
Dan Stoza9e56aa02015-11-02 13:00:03 -080042#ifdef USE_HWC2
43#include <memory>
44#endif
Mathias Agopian1f7bec62010-06-25 18:02:21 -070045
Mathias Agopiand8552d72012-08-04 21:39:11 -070046struct ANativeWindow;
47
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048namespace android {
49
Jesse Hall646f5412014-08-07 22:19:07 -070050struct DisplayInfo;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070051class DisplaySurface;
Dan Stoza9e56aa02015-11-02 13:00:03 -080052class Fence;
Jesse Hall9e663de2013-08-16 14:28:37 -070053class IGraphicBufferProducer;
Mathias Agopian13127d82013-03-05 17:47:11 -080054class Layer;
Mathias Agopian86303202012-07-24 22:46:10 -070055class SurfaceFlinger;
Mathias Agopianda27af92012-09-13 18:17:13 -070056class HWComposer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian028a7572012-08-05 01:23:51 -070058class DisplayDevice : public LightRefBase<DisplayDevice>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059{
60public:
Mathias Agopian87baae12012-07-31 12:38:26 -070061 // 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;
Jesse Hallb7a05492014-08-14 15:45:06 -070067 bool lastCompositionHadVisibleLayers;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070068
Mathias Agopian3ee454a2012-08-27 16:28:24 -070069 enum DisplayType {
70 DISPLAY_ID_INVALID = -1,
71 DISPLAY_PRIMARY = HWC_DISPLAY_PRIMARY,
72 DISPLAY_EXTERNAL = HWC_DISPLAY_EXTERNAL,
Jesse Hall9e663de2013-08-16 14:28:37 -070073 DISPLAY_VIRTUAL = HWC_DISPLAY_VIRTUAL,
74 NUM_BUILTIN_DISPLAY_TYPES = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
Mathias Agopian92a979a2012-08-02 18:32:23 -070075 };
76
77 enum {
Mathias Agopian87baae12012-07-31 12:38:26 -070078 PARTIAL_UPDATES = 0x00020000, // video driver feature
79 SWAP_RECTANGLE = 0x00080000,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 };
81
Jesse Hall01e29052013-02-19 16:13:35 -080082 enum {
83 NO_LAYER_STACK = 0xFFFFFFFF,
84 };
85
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070086 DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070088 DisplayType type,
Dan Stoza9e56aa02015-11-02 13:00:03 -080089 int32_t hwcId,
90#ifndef USE_HWC2
Jesse Hall19e87292013-12-23 21:02:15 -080091 int format,
Dan Stoza9e56aa02015-11-02 13:00:03 -080092#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -070093 bool isSecure,
94 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070095 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070096 const sp<IGraphicBufferProducer>& producer,
Mathias Agopiana4912602012-07-12 14:25:33 -070097 EGLConfig config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098
Mathias Agopian028a7572012-08-05 01:23:51 -070099 ~DisplayDevice();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700100
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
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700105 // isSecure indicates whether this display can be trusted to display
106 // secure surfaces.
107 bool isSecure() const { return mIsSecure; }
108
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 // 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 int getWidth() const;
114 int getHeight() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800115#ifndef USE_HWC2
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 PixelFormat getFormat() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800117#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 uint32_t getFlags() const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700119
Mathias Agopiana4912602012-07-12 14:25:33 -0700120 EGLSurface getEGLSurface() const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700121
Mathias Agopian13127d82013-03-05 17:47:11 -0800122 void setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
123 const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700124 Region getDirtyRegion(bool repaintEverything) const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700125
Mathias Agopian28947d72012-08-08 18:51:15 -0700126 void setLayerStack(uint32_t stack);
Michael Lentine47e45402014-07-18 15:34:25 -0700127 void setDisplaySize(const int newWidth, const int newHeight);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700128 void setProjection(int orientation, const Rect& viewport, const Rect& frame);
Mathias Agopian28947d72012-08-08 18:51:15 -0700129
Mathias Agopian1b031492012-06-20 17:51:20 -0700130 int getOrientation() const { return mOrientation; }
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700131 uint32_t getOrientationTransform() const;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700132 static uint32_t getPrimaryDisplayOrientationTransform();
Mathias Agopian1b031492012-06-20 17:51:20 -0700133 const Transform& getTransform() const { return mGlobalTransform; }
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800134 const Rect getViewport() const { return mViewport; }
135 const Rect getFrame() const { return mFrame; }
Mathias Agopian766dc492012-10-30 18:08:06 -0700136 const Rect& getScissor() const { return mScissor; }
Mathias Agopianeba8c682012-09-19 23:14:45 -0700137 bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700138
Mathias Agopian87baae12012-07-31 12:38:26 -0700139 uint32_t getLayerStack() const { return mLayerStack; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700140 int32_t getDisplayType() const { return mType; }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700141 int32_t getHwcDisplayId() const { return mHwcDisplayId; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700142 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
Mathias Agopian87baae12012-07-31 12:38:26 -0700143
Dan Stoza71433162014-02-04 16:22:36 -0800144 // 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;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800147#ifdef USE_HWC2
148 status_t prepareFrame(HWComposer& hwc);
149#else
Jesse Hall38efe862013-04-06 23:12:29 -0700150 status_t prepareFrame(const HWComposer& hwc) const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800151#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700152
Mathias Agopianda27af92012-09-13 18:17:13 -0700153 void swapBuffers(HWComposer& hwc) const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800154#ifndef USE_HWC2
Mathias Agopian74faca22009-09-17 16:18:16 -0700155 status_t compositionComplete() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800156#endif
Jesse Hall01e29052013-02-19 16:13:35 -0800157
Mathias Agopianda27af92012-09-13 18:17:13 -0700158 // called after h/w composer has completed its set() call
Dan Stoza9e56aa02015-11-02 13:00:03 -0800159#ifdef USE_HWC2
160 void onSwapBuffersCompleted() const;
161#else
Mathias Agopianda27af92012-09-13 18:17:13 -0700162 void onSwapBuffersCompleted(HWComposer& hwc) const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800163#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700164
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700165 Rect getBounds() const {
Mathias Agopian1b031492012-06-20 17:51:20 -0700166 return Rect(mDisplayWidth, mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700168 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700170 void setDisplayName(const String8& displayName);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700171 const String8& getDisplayName() const { return mDisplayName; }
172
Mathias Agopian875d8e12013-06-07 15:35:48 -0700173 EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
174 void setViewportAndProjection() const;
Mathias Agopianbae92d02012-09-28 01:00:47 -0700175
Dan Stoza9e56aa02015-11-02 13:00:03 -0800176 const sp<Fence>& getClientTargetAcquireFence() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800177
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700178 /* ------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700179 * Display power mode management.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700180 */
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700181 int getPowerMode() const;
182 void setPowerMode(int mode);
183 bool isDisplayOn() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700184
Michael Wright28f24d02016-07-12 13:30:53 -0700185#ifdef USE_HWC2
186 android_color_mode_t getActiveColorMode() const;
187 void setActiveColorMode(android_color_mode_t mode);
188#endif
189
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700190 /* ------------------------------------------------------------------------
191 * Display active config management.
192 */
193 int getActiveConfig() const;
194 void setActiveConfig(int mode);
195
Jesse Hall02d86562013-03-25 14:43:23 -0700196 // release HWC resources (if any) for removable displays
197 void disconnect(HWComposer& hwc);
198
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700199 /* ------------------------------------------------------------------------
200 * Debugging
201 */
202 uint32_t getPageFlipCount() const;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200203 void dump(String8& result) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205private:
Mathias Agopiana4912602012-07-12 14:25:33 -0700206 /*
207 * Constants, set during initialization
208 */
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700209 sp<SurfaceFlinger> mFlinger;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700210 DisplayType mType;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700211 int32_t mHwcDisplayId;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700212 wp<IBinder> mDisplayToken;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700213
Mathias Agopiana4912602012-07-12 14:25:33 -0700214 // ANativeWindow this display is rendering into
Mathias Agopiand8552d72012-08-04 21:39:11 -0700215 sp<ANativeWindow> mNativeWindow;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700216 sp<DisplaySurface> mDisplaySurface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700217
Michael Lentine47e45402014-07-18 15:34:25 -0700218 EGLConfig mConfig;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 EGLDisplay mDisplay;
220 EGLSurface mSurface;
Mathias Agopian1b031492012-06-20 17:51:20 -0700221 int mDisplayWidth;
222 int mDisplayHeight;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223#ifndef USE_HWC2
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 PixelFormat mFormat;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800225#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 uint32_t mFlags;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700227 mutable uint32_t mPageFlipCount;
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700228 String8 mDisplayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700229 bool mIsSecure;
Mathias Agopian03e40722012-04-26 16:11:59 -0700230
Mathias Agopiana4912602012-07-12 14:25:33 -0700231 /*
232 * Can only accessed from the main thread, these members
233 * don't need synchronization.
234 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700235
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700236 // list of visible layers on that display
Mathias Agopian13127d82013-03-05 17:47:11 -0800237 Vector< sp<Layer> > mVisibleLayersSortedByZ;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700238
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700239 /*
240 * Transaction state
241 */
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700242 static status_t orientationToTransfrom(int orientation,
243 int w, int h, Transform* tr);
244
Mathias Agopian87baae12012-07-31 12:38:26 -0700245 uint32_t mLayerStack;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700246 int mOrientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700247 static uint32_t sPrimaryDisplayOrientation;
Mathias Agopian766dc492012-10-30 18:08:06 -0700248 // user-provided visible area of the layer stack
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700249 Rect mViewport;
Mathias Agopian766dc492012-10-30 18:08:06 -0700250 // user-provided rectangle where mViewport gets mapped to
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700251 Rect mFrame;
Mathias Agopian766dc492012-10-30 18:08:06 -0700252 // pre-computed scissor to apply to the display
253 Rect mScissor;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700254 Transform mGlobalTransform;
Mathias Agopianeba8c682012-09-19 23:14:45 -0700255 bool mNeedsFiltering;
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700256 // Current power mode
257 int mPowerMode;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700258 // Current active config
259 int mActiveConfig;
Michael Wright28f24d02016-07-12 13:30:53 -0700260#ifdef USE_HWC2
261 // current active color mode
262 android_color_mode_t mActiveColorMode;
263#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264};
265
Irvelffc9efc2016-07-27 15:16:37 -0700266struct 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288}; // namespace android
289
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700290#endif // ANDROID_DISPLAY_DEVICE_H