blob: e2852a7c6945246573126f43e3054627272e1f04 [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
Fabien Sanglard9d96de42016-10-11 00:15:18 +000024#ifndef USE_HWC2
25#include <ui/PixelFormat.h>
26#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
Fabien Sanglard9d96de42016-10-11 00:15:18 +000032#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080033#include <binder/IBinder.h>
34#include <utils/RefBase.h>
Fabien Sanglard9d96de42016-10-11 00:15:18 +000035#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
Fabien Sanglard9d96de42016-10-11 00:15:18 +000042#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080043#include <memory>
Fabien Sanglard9d96de42016-10-11 00:15:18 +000044#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
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060086 // clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070087 DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070089 DisplayType type,
Dan Stoza9e56aa02015-11-02 13:00:03 -080090 int32_t hwcId,
Fabien Sanglard9d96de42016-10-11 00:15:18 +000091#ifndef USE_HWC2
92 int format,
93#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -070094 bool isSecure,
95 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070096 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070097 const sp<IGraphicBufferProducer>& producer,
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060098 EGLConfig config,
99 bool supportWideColor);
100 // clang-format on
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
Mathias Agopian028a7572012-08-05 01:23:51 -0700102 ~DisplayDevice();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700103
104 // whether this is a valid object. An invalid DisplayDevice is returned
105 // when an non existing id is requested
106 bool isValid() const;
107
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700108 // isSecure indicates whether this display can be trusted to display
109 // secure surfaces.
110 bool isSecure() const { return mIsSecure; }
111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 // 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;
115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 int getWidth() const;
117 int getHeight() const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000118#ifndef USE_HWC2
119 PixelFormat getFormat() const;
120#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 uint32_t getFlags() const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700122
Mathias Agopiana4912602012-07-12 14:25:33 -0700123 EGLSurface getEGLSurface() const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700124
Mathias Agopian13127d82013-03-05 17:47:11 -0800125 void setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
126 const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700127 Region getDirtyRegion(bool repaintEverything) const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700128
Mathias Agopian28947d72012-08-08 18:51:15 -0700129 void setLayerStack(uint32_t stack);
Michael Lentine47e45402014-07-18 15:34:25 -0700130 void setDisplaySize(const int newWidth, const int newHeight);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700131 void setProjection(int orientation, const Rect& viewport, const Rect& frame);
Mathias Agopian28947d72012-08-08 18:51:15 -0700132
Mathias Agopian1b031492012-06-20 17:51:20 -0700133 int getOrientation() const { return mOrientation; }
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700134 uint32_t getOrientationTransform() const;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700135 static uint32_t getPrimaryDisplayOrientationTransform();
Mathias Agopian1b031492012-06-20 17:51:20 -0700136 const Transform& getTransform() const { return mGlobalTransform; }
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800137 const Rect getViewport() const { return mViewport; }
138 const Rect getFrame() const { return mFrame; }
Mathias Agopian766dc492012-10-30 18:08:06 -0700139 const Rect& getScissor() const { return mScissor; }
Mathias Agopianeba8c682012-09-19 23:14:45 -0700140 bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700141
Mathias Agopian87baae12012-07-31 12:38:26 -0700142 uint32_t getLayerStack() const { return mLayerStack; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700143 int32_t getDisplayType() const { return mType; }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700144 int32_t getHwcDisplayId() const { return mHwcDisplayId; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700145 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
Mathias Agopian87baae12012-07-31 12:38:26 -0700146
Dan Stoza71433162014-02-04 16:22:36 -0800147 // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
148 // machine happy without actually queueing a buffer if nothing has changed
149 status_t beginFrame(bool mustRecompose) const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000150#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800151 status_t prepareFrame(HWComposer& hwc);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600152 bool getWideColorSupport() const { return mDisplayHasWideColor; }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000153#else
154 status_t prepareFrame(const HWComposer& hwc) const;
155#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700156
Mathias Agopianda27af92012-09-13 18:17:13 -0700157 void swapBuffers(HWComposer& hwc) const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000158#ifndef USE_HWC2
159 status_t compositionComplete() const;
160#endif
Jesse Hall01e29052013-02-19 16:13:35 -0800161
Mathias Agopianda27af92012-09-13 18:17:13 -0700162 // called after h/w composer has completed its set() call
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000163#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800164 void onSwapBuffersCompleted() const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000165#else
166 void onSwapBuffersCompleted(HWComposer& hwc) const;
167#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700168
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700169 Rect getBounds() const {
Mathias Agopian1b031492012-06-20 17:51:20 -0700170 return Rect(mDisplayWidth, mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700172 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700174 void setDisplayName(const String8& displayName);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700175 const String8& getDisplayName() const { return mDisplayName; }
176
Mathias Agopian875d8e12013-06-07 15:35:48 -0700177 EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
178 void setViewportAndProjection() const;
Mathias Agopianbae92d02012-09-28 01:00:47 -0700179
Dan Stoza9e56aa02015-11-02 13:00:03 -0800180 const sp<Fence>& getClientTargetAcquireFence() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800181
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700182 /* ------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700183 * Display power mode management.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700184 */
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700185 int getPowerMode() const;
186 void setPowerMode(int mode);
187 bool isDisplayOn() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700188
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000189#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700190 android_color_mode_t getActiveColorMode() const;
191 void setActiveColorMode(android_color_mode_t mode);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000192#endif
Michael Wright28f24d02016-07-12 13:30:53 -0700193
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700194 /* ------------------------------------------------------------------------
195 * Display active config management.
196 */
197 int getActiveConfig() const;
198 void setActiveConfig(int mode);
199
Jesse Hall02d86562013-03-25 14:43:23 -0700200 // release HWC resources (if any) for removable displays
201 void disconnect(HWComposer& hwc);
202
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700203 /* ------------------------------------------------------------------------
204 * Debugging
205 */
206 uint32_t getPageFlipCount() const;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200207 void dump(String8& result) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209private:
Mathias Agopiana4912602012-07-12 14:25:33 -0700210 /*
211 * Constants, set during initialization
212 */
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700213 sp<SurfaceFlinger> mFlinger;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700214 DisplayType mType;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700215 int32_t mHwcDisplayId;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700216 wp<IBinder> mDisplayToken;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700217
Mathias Agopiana4912602012-07-12 14:25:33 -0700218 // ANativeWindow this display is rendering into
Mathias Agopiand8552d72012-08-04 21:39:11 -0700219 sp<ANativeWindow> mNativeWindow;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700220 sp<DisplaySurface> mDisplaySurface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700221
Michael Lentine47e45402014-07-18 15:34:25 -0700222 EGLConfig mConfig;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 EGLDisplay mDisplay;
224 EGLSurface mSurface;
Mathias Agopian1b031492012-06-20 17:51:20 -0700225 int mDisplayWidth;
226 int mDisplayHeight;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000227#ifndef USE_HWC2
228 PixelFormat mFormat;
229#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 uint32_t mFlags;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700231 mutable uint32_t mPageFlipCount;
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700232 String8 mDisplayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700233 bool mIsSecure;
Mathias Agopian03e40722012-04-26 16:11:59 -0700234
Mathias Agopiana4912602012-07-12 14:25:33 -0700235 /*
236 * Can only accessed from the main thread, these members
237 * don't need synchronization.
238 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700239
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700240 // list of visible layers on that display
Mathias Agopian13127d82013-03-05 17:47:11 -0800241 Vector< sp<Layer> > mVisibleLayersSortedByZ;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700242
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700243 /*
244 * Transaction state
245 */
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700246 static status_t orientationToTransfrom(int orientation,
247 int w, int h, Transform* tr);
248
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800249 // The identifier of the active layer stack for this display. Several displays
250 // can use the same layer stack: A z-ordered group of layers (sometimes called
251 // "surfaces"). Any given layer can only be on a single layer stack.
Mathias Agopian87baae12012-07-31 12:38:26 -0700252 uint32_t mLayerStack;
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800253
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700254 int mOrientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700255 static uint32_t sPrimaryDisplayOrientation;
Mathias Agopian766dc492012-10-30 18:08:06 -0700256 // user-provided visible area of the layer stack
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700257 Rect mViewport;
Mathias Agopian766dc492012-10-30 18:08:06 -0700258 // user-provided rectangle where mViewport gets mapped to
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700259 Rect mFrame;
Mathias Agopian766dc492012-10-30 18:08:06 -0700260 // pre-computed scissor to apply to the display
261 Rect mScissor;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700262 Transform mGlobalTransform;
Mathias Agopianeba8c682012-09-19 23:14:45 -0700263 bool mNeedsFiltering;
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700264 // Current power mode
265 int mPowerMode;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700266 // Current active config
267 int mActiveConfig;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000268#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700269 // current active color mode
270 android_color_mode_t mActiveColorMode;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600271
272 // Need to know if display is wide-color capable or not.
273 // Initialized by SurfaceFlinger when the DisplayDevice is created.
274 // Fed to RenderEngine during composition.
275 bool mDisplayHasWideColor;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000276#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277};
278
Irvelffc9efc2016-07-27 15:16:37 -0700279struct DisplayDeviceState {
280 DisplayDeviceState() = default;
281 DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
282
283 bool isValid() const { return type >= 0; }
284 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
285 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
286
287 static std::atomic<int32_t> nextDisplayId;
288 int32_t displayId = nextDisplayId++;
289 DisplayDevice::DisplayType type = DisplayDevice::DISPLAY_ID_INVALID;
290 sp<IGraphicBufferProducer> surface;
291 uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
292 Rect viewport;
293 Rect frame;
294 uint8_t orientation = 0;
295 uint32_t width = 0;
296 uint32_t height = 0;
297 String8 displayName;
298 bool isSecure = false;
299};
300
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301}; // namespace android
302
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700303#endif // ANDROID_DISPLAY_DEVICE_H