blob: bedf76589f937025e05bc5efdbba8ea1c27e215b [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
Yiwei Zhang7c64f172018-03-07 14:52:28 -080024#include <math/mat4.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <ui/Region.h>
27
Dan Stoza9e56aa02015-11-02 13:00:03 -080028#include <binder/IBinder.h>
29#include <utils/RefBase.h>
Mathias Agopian86303202012-07-24 22:46:10 -070030#include <utils/Mutex.h>
Mathias Agopian4f4f0942013-08-19 17:26:18 -070031#include <utils/String8.h>
Mathias Agopian86303202012-07-24 22:46:10 -070032#include <utils/Timers.h>
33
chaviwa76b2712017-09-20 12:02:26 -070034#include <gui/ISurfaceComposer.h>
Mathias Agopianf4358632012-08-22 17:16:19 -070035#include <hardware/hwcomposer_defs.h>
Peiyong Linfd997e02018-03-28 15:29:00 -070036#include <ui/GraphicTypes.h>
chaviwa76b2712017-09-20 12:02:26 -070037#include "RenderArea.h"
Chia-I Wuf846a352017-11-10 09:22:52 -080038#include "RenderEngine/Surface.h"
Mathias Agopianf4358632012-08-22 17:16:19 -070039
Dan Stoza9e56aa02015-11-02 13:00:03 -080040#include <memory>
Mathias Agopian1f7bec62010-06-25 18:02:21 -070041
Mathias Agopiand8552d72012-08-04 21:39:11 -070042struct ANativeWindow;
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044namespace android {
45
Jesse Hall646f5412014-08-07 22:19:07 -070046struct DisplayInfo;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070047class DisplaySurface;
Dan Stoza9e56aa02015-11-02 13:00:03 -080048class Fence;
Jesse Hall9e663de2013-08-16 14:28:37 -070049class IGraphicBufferProducer;
Mathias Agopian13127d82013-03-05 17:47:11 -080050class Layer;
Mathias Agopian86303202012-07-24 22:46:10 -070051class SurfaceFlinger;
Mathias Agopianda27af92012-09-13 18:17:13 -070052class HWComposer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian028a7572012-08-05 01:23:51 -070054class DisplayDevice : public LightRefBase<DisplayDevice>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055{
56public:
Mathias Agopian87baae12012-07-31 12:38:26 -070057 // region in layer-stack space
58 mutable Region dirtyRegion;
59 // region in screen space
Mathias Agopian87baae12012-07-31 12:38:26 -070060 Region undefinedRegion;
Jesse Hallb7a05492014-08-14 15:45:06 -070061 bool lastCompositionHadVisibleLayers;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070062
Mathias Agopian3ee454a2012-08-27 16:28:24 -070063 enum DisplayType {
64 DISPLAY_ID_INVALID = -1,
65 DISPLAY_PRIMARY = HWC_DISPLAY_PRIMARY,
66 DISPLAY_EXTERNAL = HWC_DISPLAY_EXTERNAL,
Jesse Hall9e663de2013-08-16 14:28:37 -070067 DISPLAY_VIRTUAL = HWC_DISPLAY_VIRTUAL,
68 NUM_BUILTIN_DISPLAY_TYPES = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
Mathias Agopian92a979a2012-08-02 18:32:23 -070069 };
70
71 enum {
Jesse Hall01e29052013-02-19 16:13:35 -080072 NO_LAYER_STACK = 0xFFFFFFFF,
73 };
74
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060075 // clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070076 DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070078 DisplayType type,
Dan Stoza9e56aa02015-11-02 13:00:03 -080079 int32_t hwcId,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070080 bool isSecure,
81 const wp<IBinder>& displayToken,
Lloyd Pique09594832018-01-22 17:48:03 -080082 const sp<ANativeWindow>& nativeWindow,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070083 const sp<DisplaySurface>& displaySurface,
Lloyd Pique09594832018-01-22 17:48:03 -080084 std::unique_ptr<RE::Surface> renderSurface,
85 int displayWidth,
86 int displayHeight,
Peiyong Lindd9b2ae2018-03-01 16:22:45 -080087 bool hasWideColorGamut,
88 bool hasHdr10,
Lloyd Pique09594832018-01-22 17:48:03 -080089 int initialPowerMode);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060090 // clang-format on
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091
Mathias Agopian028a7572012-08-05 01:23:51 -070092 ~DisplayDevice();
Mathias Agopian92a979a2012-08-02 18:32:23 -070093
94 // whether this is a valid object. An invalid DisplayDevice is returned
95 // when an non existing id is requested
96 bool isValid() const;
97
Jamie Gennisdd3cb842012-10-19 18:19:11 -070098 // isSecure indicates whether this display can be trusted to display
99 // secure surfaces.
100 bool isSecure() const { return mIsSecure; }
101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 // Flip the front and back buffers if the back buffer is "dirty". Might
103 // be instantaneous, might involve copying the frame buffer around.
Chia-I Wub02087d2017-11-09 10:19:54 -0800104 void flip() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 int getWidth() const;
107 int getHeight() const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700108
Mathias Agopian13127d82013-03-05 17:47:11 -0800109 void setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
110 const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
Chia-I Wu83806892017-11-16 10:50:20 -0800111 void setLayersNeedingFences(const Vector< sp<Layer> >& layers);
112 const Vector< sp<Layer> >& getLayersNeedingFences() const;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700113 Region getDirtyRegion(bool repaintEverything) const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700114
Mathias Agopian28947d72012-08-08 18:51:15 -0700115 void setLayerStack(uint32_t stack);
Michael Lentine47e45402014-07-18 15:34:25 -0700116 void setDisplaySize(const int newWidth, const int newHeight);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700117 void setProjection(int orientation, const Rect& viewport, const Rect& frame);
Mathias Agopian28947d72012-08-08 18:51:15 -0700118
Mathias Agopian1b031492012-06-20 17:51:20 -0700119 int getOrientation() const { return mOrientation; }
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700120 uint32_t getOrientationTransform() const;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700121 static uint32_t getPrimaryDisplayOrientationTransform();
Mathias Agopian1b031492012-06-20 17:51:20 -0700122 const Transform& getTransform() const { return mGlobalTransform; }
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800123 const Rect getViewport() const { return mViewport; }
124 const Rect getFrame() const { return mFrame; }
Mathias Agopian766dc492012-10-30 18:08:06 -0700125 const Rect& getScissor() const { return mScissor; }
Mathias Agopianeba8c682012-09-19 23:14:45 -0700126 bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700127
Mathias Agopian87baae12012-07-31 12:38:26 -0700128 uint32_t getLayerStack() const { return mLayerStack; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700129 int32_t getDisplayType() const { return mType; }
Chia-I Wuab0c3192017-08-01 11:29:00 -0700130 bool isPrimary() const { return mType == DISPLAY_PRIMARY; }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700131 int32_t getHwcDisplayId() const { return mHwcDisplayId; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700132 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
Mathias Agopian87baae12012-07-31 12:38:26 -0700133
Dan Stoza71433162014-02-04 16:22:36 -0800134 // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
135 // machine happy without actually queueing a buffer if nothing has changed
136 status_t beginFrame(bool mustRecompose) const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800137 status_t prepareFrame(HWComposer& hwc);
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800138 bool hasWideColorGamut() const { return mHasWideColorGamut; }
139 bool hasHdr10() const { return mHasHdr10; }
Jesse Hall38efe862013-04-06 23:12:29 -0700140
Mathias Agopianda27af92012-09-13 18:17:13 -0700141 void swapBuffers(HWComposer& hwc) const;
Jesse Hall01e29052013-02-19 16:13:35 -0800142
Mathias Agopianda27af92012-09-13 18:17:13 -0700143 // called after h/w composer has completed its set() call
Dan Stoza9e56aa02015-11-02 13:00:03 -0800144 void onSwapBuffersCompleted() const;
Mathias Agopianda27af92012-09-13 18:17:13 -0700145
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700146 Rect getBounds() const {
Mathias Agopian1b031492012-06-20 17:51:20 -0700147 return Rect(mDisplayWidth, mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700149 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700151 void setDisplayName(const String8& displayName);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700152 const String8& getDisplayName() const { return mDisplayName; }
153
Chia-I Wuf846a352017-11-10 09:22:52 -0800154 bool makeCurrent() const;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700155 void setViewportAndProjection() const;
Mathias Agopianbae92d02012-09-28 01:00:47 -0700156
Dan Stoza9e56aa02015-11-02 13:00:03 -0800157 const sp<Fence>& getClientTargetAcquireFence() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800158
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700159 /* ------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700160 * Display power mode management.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700161 */
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700162 int getPowerMode() const;
163 void setPowerMode(int mode);
164 bool isDisplayOn() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700165
Peiyong Linfd997e02018-03-28 15:29:00 -0700166 ui::ColorMode getActiveColorMode() const;
167 void setActiveColorMode(ui::ColorMode mode);
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800168 ui::RenderIntent getActiveRenderIntent() const;
169 void setActiveRenderIntent(ui::RenderIntent renderIntent);
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800170 android_color_transform_t getColorTransform() const;
171 void setColorTransform(const mat4& transform);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700172 void setCompositionDataSpace(ui::Dataspace dataspace);
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800173 ui::Dataspace getCompositionDataSpace() const;
Michael Wright28f24d02016-07-12 13:30:53 -0700174
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700175 /* ------------------------------------------------------------------------
176 * Display active config management.
177 */
178 int getActiveConfig() const;
179 void setActiveConfig(int mode);
180
Jesse Hall02d86562013-03-25 14:43:23 -0700181 // release HWC resources (if any) for removable displays
182 void disconnect(HWComposer& hwc);
183
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700184 /* ------------------------------------------------------------------------
185 * Debugging
186 */
187 uint32_t getPageFlipCount() const;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200188 void dump(String8& result) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190private:
Mathias Agopiana4912602012-07-12 14:25:33 -0700191 /*
192 * Constants, set during initialization
193 */
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700194 sp<SurfaceFlinger> mFlinger;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700195 DisplayType mType;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700196 int32_t mHwcDisplayId;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700197 wp<IBinder> mDisplayToken;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700198
Mathias Agopiana4912602012-07-12 14:25:33 -0700199 // ANativeWindow this display is rendering into
Mathias Agopiand8552d72012-08-04 21:39:11 -0700200 sp<ANativeWindow> mNativeWindow;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700201 sp<DisplaySurface> mDisplaySurface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700202
Lloyd Pique144e1162017-12-20 16:44:52 -0800203 std::unique_ptr<RE::Surface> mSurface;
Mathias Agopian1b031492012-06-20 17:51:20 -0700204 int mDisplayWidth;
205 int mDisplayHeight;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 mutable uint32_t mPageFlipCount;
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700207 String8 mDisplayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700208 bool mIsSecure;
Mathias Agopian03e40722012-04-26 16:11:59 -0700209
Mathias Agopiana4912602012-07-12 14:25:33 -0700210 /*
211 * Can only accessed from the main thread, these members
212 * don't need synchronization.
213 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700214
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700215 // list of visible layers on that display
Mathias Agopian13127d82013-03-05 17:47:11 -0800216 Vector< sp<Layer> > mVisibleLayersSortedByZ;
Chia-I Wu83806892017-11-16 10:50:20 -0800217 // list of layers needing fences
218 Vector< sp<Layer> > mLayersNeedingFences;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700219
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700220 /*
221 * Transaction state
222 */
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700223 static status_t orientationToTransfrom(int orientation,
224 int w, int h, Transform* tr);
225
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800226 // The identifier of the active layer stack for this display. Several displays
227 // can use the same layer stack: A z-ordered group of layers (sometimes called
228 // "surfaces"). Any given layer can only be on a single layer stack.
Mathias Agopian87baae12012-07-31 12:38:26 -0700229 uint32_t mLayerStack;
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800230
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700231 int mOrientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700232 static uint32_t sPrimaryDisplayOrientation;
Mathias Agopian766dc492012-10-30 18:08:06 -0700233 // user-provided visible area of the layer stack
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700234 Rect mViewport;
Mathias Agopian766dc492012-10-30 18:08:06 -0700235 // user-provided rectangle where mViewport gets mapped to
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700236 Rect mFrame;
Mathias Agopian766dc492012-10-30 18:08:06 -0700237 // pre-computed scissor to apply to the display
238 Rect mScissor;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700239 Transform mGlobalTransform;
Mathias Agopianeba8c682012-09-19 23:14:45 -0700240 bool mNeedsFiltering;
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700241 // Current power mode
242 int mPowerMode;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700243 // Current active config
244 int mActiveConfig;
Michael Wright28f24d02016-07-12 13:30:53 -0700245 // current active color mode
Peiyong Linfd997e02018-03-28 15:29:00 -0700246 ui::ColorMode mActiveColorMode;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800247 // Current active render intent.
248 ui::RenderIntent mActiveRenderIntent;
249 ui::Dataspace mCompositionDataSpace;
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800250 // Current color transform
251 android_color_transform_t mColorTransform;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600252
253 // Need to know if display is wide-color capable or not.
254 // Initialized by SurfaceFlinger when the DisplayDevice is created.
255 // Fed to RenderEngine during composition.
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800256 bool mHasWideColorGamut;
257 bool mHasHdr10;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258};
259
Irvelffc9efc2016-07-27 15:16:37 -0700260struct DisplayDeviceState {
261 DisplayDeviceState() = default;
262 DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
263
264 bool isValid() const { return type >= 0; }
265 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
266 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
267
268 static std::atomic<int32_t> nextDisplayId;
269 int32_t displayId = nextDisplayId++;
270 DisplayDevice::DisplayType type = DisplayDevice::DISPLAY_ID_INVALID;
271 sp<IGraphicBufferProducer> surface;
272 uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
273 Rect viewport;
274 Rect frame;
275 uint8_t orientation = 0;
276 uint32_t width = 0;
277 uint32_t height = 0;
278 String8 displayName;
279 bool isSecure = false;
280};
281
chaviwa76b2712017-09-20 12:02:26 -0700282class DisplayRenderArea : public RenderArea {
283public:
284 DisplayRenderArea(const sp<const DisplayDevice> device,
285 ISurfaceComposer::Rotation rotation = ISurfaceComposer::eRotateNone)
286 : DisplayRenderArea(device, device->getBounds(), device->getHeight(), device->getWidth(),
287 rotation) {}
288 DisplayRenderArea(const sp<const DisplayDevice> device, Rect sourceCrop, uint32_t reqHeight,
289 uint32_t reqWidth, ISurfaceComposer::Rotation rotation)
290 : RenderArea(reqHeight, reqWidth, rotation), mDevice(device), mSourceCrop(sourceCrop) {}
291
292 const Transform& getTransform() const override { return mDevice->getTransform(); }
293 Rect getBounds() const override { return mDevice->getBounds(); }
294 int getHeight() const override { return mDevice->getHeight(); }
295 int getWidth() const override { return mDevice->getWidth(); }
296 bool isSecure() const override { return mDevice->isSecure(); }
297 bool needsFiltering() const override { return mDevice->needsFiltering(); }
298 Rect getSourceCrop() const override { return mSourceCrop; }
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800299 bool getWideColorSupport() const override { return mDevice->hasWideColorGamut(); }
300 ui::Dataspace getDataSpace() const override {
301 return mDevice->getCompositionDataSpace();
chaviwa76b2712017-09-20 12:02:26 -0700302 }
chaviwa76b2712017-09-20 12:02:26 -0700303
304private:
305 const sp<const DisplayDevice> mDevice;
306 const Rect mSourceCrop;
307};
308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309}; // namespace android
310
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700311#endif // ANDROID_DISPLAY_DEVICE_H