blob: 49fef5805f20e3f1cf46c76b9164f51048f5eaa2 [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
chaviwa76b2712017-09-20 12:02:26 -070040#include <gui/ISurfaceComposer.h>
Mathias Agopianf4358632012-08-22 17:16:19 -070041#include <hardware/hwcomposer_defs.h>
chaviwa76b2712017-09-20 12:02:26 -070042#include "RenderArea.h"
Mathias Agopianf4358632012-08-22 17:16:19 -070043
Fabien Sanglard9d96de42016-10-11 00:15:18 +000044#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080045#include <memory>
Fabien Sanglard9d96de42016-10-11 00:15:18 +000046#endif
Mathias Agopian1f7bec62010-06-25 18:02:21 -070047
Mathias Agopiand8552d72012-08-04 21:39:11 -070048struct ANativeWindow;
49
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050namespace android {
51
Jesse Hall646f5412014-08-07 22:19:07 -070052struct DisplayInfo;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070053class DisplaySurface;
Dan Stoza9e56aa02015-11-02 13:00:03 -080054class Fence;
Jesse Hall9e663de2013-08-16 14:28:37 -070055class IGraphicBufferProducer;
Mathias Agopian13127d82013-03-05 17:47:11 -080056class Layer;
Mathias Agopian86303202012-07-24 22:46:10 -070057class SurfaceFlinger;
Mathias Agopianda27af92012-09-13 18:17:13 -070058class HWComposer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopian028a7572012-08-05 01:23:51 -070060class DisplayDevice : public LightRefBase<DisplayDevice>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061{
62public:
Mathias Agopian87baae12012-07-31 12:38:26 -070063 // region in layer-stack space
64 mutable Region dirtyRegion;
65 // region in screen space
66 mutable Region swapRegion;
67 // region in screen space
68 Region undefinedRegion;
Jesse Hallb7a05492014-08-14 15:45:06 -070069 bool lastCompositionHadVisibleLayers;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070070
Mathias Agopian3ee454a2012-08-27 16:28:24 -070071 enum DisplayType {
72 DISPLAY_ID_INVALID = -1,
73 DISPLAY_PRIMARY = HWC_DISPLAY_PRIMARY,
74 DISPLAY_EXTERNAL = HWC_DISPLAY_EXTERNAL,
Jesse Hall9e663de2013-08-16 14:28:37 -070075 DISPLAY_VIRTUAL = HWC_DISPLAY_VIRTUAL,
76 NUM_BUILTIN_DISPLAY_TYPES = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
Mathias Agopian92a979a2012-08-02 18:32:23 -070077 };
78
79 enum {
Mathias Agopian87baae12012-07-31 12:38:26 -070080 PARTIAL_UPDATES = 0x00020000, // video driver feature
81 SWAP_RECTANGLE = 0x00080000,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 };
83
Jesse Hall01e29052013-02-19 16:13:35 -080084 enum {
85 NO_LAYER_STACK = 0xFFFFFFFF,
86 };
87
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060088 // clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070089 DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070091 DisplayType type,
Dan Stoza9e56aa02015-11-02 13:00:03 -080092 int32_t hwcId,
Fabien Sanglard9d96de42016-10-11 00:15:18 +000093#ifndef USE_HWC2
94 int format,
95#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -070096 bool isSecure,
97 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070098 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070099 const sp<IGraphicBufferProducer>& producer,
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600100 EGLConfig config,
101 bool supportWideColor);
102 // clang-format on
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103
Mathias Agopian028a7572012-08-05 01:23:51 -0700104 ~DisplayDevice();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700105
106 // whether this is a valid object. An invalid DisplayDevice is returned
107 // when an non existing id is requested
108 bool isValid() const;
109
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700110 // isSecure indicates whether this display can be trusted to display
111 // secure surfaces.
112 bool isSecure() const { return mIsSecure; }
113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 // Flip the front and back buffers if the back buffer is "dirty". Might
115 // be instantaneous, might involve copying the frame buffer around.
116 void flip(const Region& dirty) const;
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 int getWidth() const;
119 int getHeight() const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000120#ifndef USE_HWC2
121 PixelFormat getFormat() const;
122#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123 uint32_t getFlags() const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700124
Mathias Agopiana4912602012-07-12 14:25:33 -0700125 EGLSurface getEGLSurface() const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700126
Mathias Agopian13127d82013-03-05 17:47:11 -0800127 void setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
128 const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700129 Region getDirtyRegion(bool repaintEverything) const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700130
Mathias Agopian28947d72012-08-08 18:51:15 -0700131 void setLayerStack(uint32_t stack);
Michael Lentine47e45402014-07-18 15:34:25 -0700132 void setDisplaySize(const int newWidth, const int newHeight);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700133 void setProjection(int orientation, const Rect& viewport, const Rect& frame);
Mathias Agopian28947d72012-08-08 18:51:15 -0700134
Mathias Agopian1b031492012-06-20 17:51:20 -0700135 int getOrientation() const { return mOrientation; }
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700136 uint32_t getOrientationTransform() const;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700137 static uint32_t getPrimaryDisplayOrientationTransform();
Mathias Agopian1b031492012-06-20 17:51:20 -0700138 const Transform& getTransform() const { return mGlobalTransform; }
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800139 const Rect getViewport() const { return mViewport; }
140 const Rect getFrame() const { return mFrame; }
Mathias Agopian766dc492012-10-30 18:08:06 -0700141 const Rect& getScissor() const { return mScissor; }
Mathias Agopianeba8c682012-09-19 23:14:45 -0700142 bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700143
Mathias Agopian87baae12012-07-31 12:38:26 -0700144 uint32_t getLayerStack() const { return mLayerStack; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700145 int32_t getDisplayType() const { return mType; }
Chia-I Wuab0c3192017-08-01 11:29:00 -0700146 bool isPrimary() const { return mType == DISPLAY_PRIMARY; }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700147 int32_t getHwcDisplayId() const { return mHwcDisplayId; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700148 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
Mathias Agopian87baae12012-07-31 12:38:26 -0700149
Dan Stoza71433162014-02-04 16:22:36 -0800150 // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
151 // machine happy without actually queueing a buffer if nothing has changed
152 status_t beginFrame(bool mustRecompose) const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000153#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800154 status_t prepareFrame(HWComposer& hwc);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600155 bool getWideColorSupport() const { return mDisplayHasWideColor; }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000156#else
157 status_t prepareFrame(const HWComposer& hwc) const;
158#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700159
Mathias Agopianda27af92012-09-13 18:17:13 -0700160 void swapBuffers(HWComposer& hwc) const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000161#ifndef USE_HWC2
162 status_t compositionComplete() const;
163#endif
Jesse Hall01e29052013-02-19 16:13:35 -0800164
Mathias Agopianda27af92012-09-13 18:17:13 -0700165 // called after h/w composer has completed its set() call
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000166#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800167 void onSwapBuffersCompleted() const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000168#else
169 void onSwapBuffersCompleted(HWComposer& hwc) const;
170#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700171
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700172 Rect getBounds() const {
Mathias Agopian1b031492012-06-20 17:51:20 -0700173 return Rect(mDisplayWidth, mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700175 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700177 void setDisplayName(const String8& displayName);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700178 const String8& getDisplayName() const { return mDisplayName; }
179
Mathias Agopian875d8e12013-06-07 15:35:48 -0700180 EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
181 void setViewportAndProjection() const;
Mathias Agopianbae92d02012-09-28 01:00:47 -0700182
Dan Stoza9e56aa02015-11-02 13:00:03 -0800183 const sp<Fence>& getClientTargetAcquireFence() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800184
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700185 /* ------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700186 * Display power mode management.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700187 */
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700188 int getPowerMode() const;
189 void setPowerMode(int mode);
190 bool isDisplayOn() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700191
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000192#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700193 android_color_mode_t getActiveColorMode() const;
194 void setActiveColorMode(android_color_mode_t mode);
Courtney Goeltzenleuchter281e8112017-07-13 17:54:01 -0600195 void setCompositionDataSpace(android_dataspace dataspace);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000196#endif
Michael Wright28f24d02016-07-12 13:30:53 -0700197
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700198 /* ------------------------------------------------------------------------
199 * Display active config management.
200 */
201 int getActiveConfig() const;
202 void setActiveConfig(int mode);
203
Jesse Hall02d86562013-03-25 14:43:23 -0700204 // release HWC resources (if any) for removable displays
205 void disconnect(HWComposer& hwc);
206
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700207 /* ------------------------------------------------------------------------
208 * Debugging
209 */
210 uint32_t getPageFlipCount() const;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200211 void dump(String8& result) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213private:
Mathias Agopiana4912602012-07-12 14:25:33 -0700214 /*
215 * Constants, set during initialization
216 */
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700217 sp<SurfaceFlinger> mFlinger;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700218 DisplayType mType;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700219 int32_t mHwcDisplayId;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700220 wp<IBinder> mDisplayToken;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700221
Mathias Agopiana4912602012-07-12 14:25:33 -0700222 // ANativeWindow this display is rendering into
Mathias Agopiand8552d72012-08-04 21:39:11 -0700223 sp<ANativeWindow> mNativeWindow;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700224 sp<DisplaySurface> mDisplaySurface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700225
Michael Lentine47e45402014-07-18 15:34:25 -0700226 EGLConfig mConfig;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 EGLDisplay mDisplay;
228 EGLSurface mSurface;
Mathias Agopian1b031492012-06-20 17:51:20 -0700229 int mDisplayWidth;
230 int mDisplayHeight;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000231#ifndef USE_HWC2
232 PixelFormat mFormat;
233#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 uint32_t mFlags;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700235 mutable uint32_t mPageFlipCount;
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700236 String8 mDisplayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700237 bool mIsSecure;
Mathias Agopian03e40722012-04-26 16:11:59 -0700238
Mathias Agopiana4912602012-07-12 14:25:33 -0700239 /*
240 * Can only accessed from the main thread, these members
241 * don't need synchronization.
242 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700243
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700244 // list of visible layers on that display
Mathias Agopian13127d82013-03-05 17:47:11 -0800245 Vector< sp<Layer> > mVisibleLayersSortedByZ;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700246
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700247 /*
248 * Transaction state
249 */
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700250 static status_t orientationToTransfrom(int orientation,
251 int w, int h, Transform* tr);
252
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800253 // The identifier of the active layer stack for this display. Several displays
254 // can use the same layer stack: A z-ordered group of layers (sometimes called
255 // "surfaces"). Any given layer can only be on a single layer stack.
Mathias Agopian87baae12012-07-31 12:38:26 -0700256 uint32_t mLayerStack;
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800257
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700258 int mOrientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700259 static uint32_t sPrimaryDisplayOrientation;
Mathias Agopian766dc492012-10-30 18:08:06 -0700260 // user-provided visible area of the layer stack
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700261 Rect mViewport;
Mathias Agopian766dc492012-10-30 18:08:06 -0700262 // user-provided rectangle where mViewport gets mapped to
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700263 Rect mFrame;
Mathias Agopian766dc492012-10-30 18:08:06 -0700264 // pre-computed scissor to apply to the display
265 Rect mScissor;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700266 Transform mGlobalTransform;
Mathias Agopianeba8c682012-09-19 23:14:45 -0700267 bool mNeedsFiltering;
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700268 // Current power mode
269 int mPowerMode;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700270 // Current active config
271 int mActiveConfig;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000272#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700273 // current active color mode
274 android_color_mode_t mActiveColorMode;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600275
276 // Need to know if display is wide-color capable or not.
277 // Initialized by SurfaceFlinger when the DisplayDevice is created.
278 // Fed to RenderEngine during composition.
279 bool mDisplayHasWideColor;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000280#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281};
282
Irvelffc9efc2016-07-27 15:16:37 -0700283struct DisplayDeviceState {
284 DisplayDeviceState() = default;
285 DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
286
287 bool isValid() const { return type >= 0; }
288 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
289 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
290
291 static std::atomic<int32_t> nextDisplayId;
292 int32_t displayId = nextDisplayId++;
293 DisplayDevice::DisplayType type = DisplayDevice::DISPLAY_ID_INVALID;
294 sp<IGraphicBufferProducer> surface;
295 uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
296 Rect viewport;
297 Rect frame;
298 uint8_t orientation = 0;
299 uint32_t width = 0;
300 uint32_t height = 0;
301 String8 displayName;
302 bool isSecure = false;
303};
304
chaviwa76b2712017-09-20 12:02:26 -0700305class DisplayRenderArea : public RenderArea {
306public:
307 DisplayRenderArea(const sp<const DisplayDevice> device,
308 ISurfaceComposer::Rotation rotation = ISurfaceComposer::eRotateNone)
309 : DisplayRenderArea(device, device->getBounds(), device->getHeight(), device->getWidth(),
310 rotation) {}
311 DisplayRenderArea(const sp<const DisplayDevice> device, Rect sourceCrop, uint32_t reqHeight,
312 uint32_t reqWidth, ISurfaceComposer::Rotation rotation)
313 : RenderArea(reqHeight, reqWidth, rotation), mDevice(device), mSourceCrop(sourceCrop) {}
314
315 const Transform& getTransform() const override { return mDevice->getTransform(); }
316 Rect getBounds() const override { return mDevice->getBounds(); }
317 int getHeight() const override { return mDevice->getHeight(); }
318 int getWidth() const override { return mDevice->getWidth(); }
319 bool isSecure() const override { return mDevice->isSecure(); }
320 bool needsFiltering() const override { return mDevice->needsFiltering(); }
321 Rect getSourceCrop() const override { return mSourceCrop; }
322#ifdef USE_HWC2
323 bool getWideColorSupport() const override { return mDevice->getWideColorSupport(); }
324 android_color_mode_t getActiveColorMode() const override {
325 return mDevice->getActiveColorMode();
326 }
327#endif
328
329private:
330 const sp<const DisplayDevice> mDevice;
331 const Rect mSourceCrop;
332};
333
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334}; // namespace android
335
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700336#endif // ANDROID_DISPLAY_DEVICE_H