blob: 737971f83d5de0f686520c11dd8af044af138388 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <ui/Region.h>
25
Dan Stoza9e56aa02015-11-02 13:00:03 -080026#include <binder/IBinder.h>
27#include <utils/RefBase.h>
Mathias Agopian86303202012-07-24 22:46:10 -070028#include <utils/Mutex.h>
Mathias Agopian4f4f0942013-08-19 17:26:18 -070029#include <utils/String8.h>
Mathias Agopian86303202012-07-24 22:46:10 -070030#include <utils/Timers.h>
31
chaviwa76b2712017-09-20 12:02:26 -070032#include <gui/ISurfaceComposer.h>
Mathias Agopianf4358632012-08-22 17:16:19 -070033#include <hardware/hwcomposer_defs.h>
Peiyong Lina52f0292018-03-14 17:26:31 -070034#include <ui/GraphicsTypes.h>
chaviwa76b2712017-09-20 12:02:26 -070035#include "RenderArea.h"
Chia-I Wuf846a352017-11-10 09:22:52 -080036#include "RenderEngine/Surface.h"
Mathias Agopianf4358632012-08-22 17:16:19 -070037
Dan Stoza9e56aa02015-11-02 13:00:03 -080038#include <memory>
Mathias Agopian1f7bec62010-06-25 18:02:21 -070039
Mathias Agopiand8552d72012-08-04 21:39:11 -070040struct ANativeWindow;
41
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042namespace android {
43
Jesse Hall646f5412014-08-07 22:19:07 -070044struct DisplayInfo;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070045class DisplaySurface;
Dan Stoza9e56aa02015-11-02 13:00:03 -080046class Fence;
Jesse Hall9e663de2013-08-16 14:28:37 -070047class IGraphicBufferProducer;
Mathias Agopian13127d82013-03-05 17:47:11 -080048class Layer;
Mathias Agopian86303202012-07-24 22:46:10 -070049class SurfaceFlinger;
Mathias Agopianda27af92012-09-13 18:17:13 -070050class HWComposer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Mathias Agopian028a7572012-08-05 01:23:51 -070052class DisplayDevice : public LightRefBase<DisplayDevice>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053{
54public:
Mathias Agopian87baae12012-07-31 12:38:26 -070055 // region in layer-stack space
56 mutable Region dirtyRegion;
57 // region in screen space
Mathias Agopian87baae12012-07-31 12:38:26 -070058 Region undefinedRegion;
Jesse Hallb7a05492014-08-14 15:45:06 -070059 bool lastCompositionHadVisibleLayers;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070060
Mathias Agopian3ee454a2012-08-27 16:28:24 -070061 enum DisplayType {
62 DISPLAY_ID_INVALID = -1,
63 DISPLAY_PRIMARY = HWC_DISPLAY_PRIMARY,
64 DISPLAY_EXTERNAL = HWC_DISPLAY_EXTERNAL,
Jesse Hall9e663de2013-08-16 14:28:37 -070065 DISPLAY_VIRTUAL = HWC_DISPLAY_VIRTUAL,
66 NUM_BUILTIN_DISPLAY_TYPES = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
Mathias Agopian92a979a2012-08-02 18:32:23 -070067 };
68
69 enum {
Jesse Hall01e29052013-02-19 16:13:35 -080070 NO_LAYER_STACK = 0xFFFFFFFF,
71 };
72
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060073 // clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070074 DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070076 DisplayType type,
Dan Stoza9e56aa02015-11-02 13:00:03 -080077 int32_t hwcId,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070078 bool isSecure,
79 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070080 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070081 const sp<IGraphicBufferProducer>& producer,
Chia-I Wu5c6e4632018-01-11 08:54:38 -080082 bool supportWideColor, bool supportHdr);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060083 // clang-format on
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084
Mathias Agopian028a7572012-08-05 01:23:51 -070085 ~DisplayDevice();
Mathias Agopian92a979a2012-08-02 18:32:23 -070086
87 // whether this is a valid object. An invalid DisplayDevice is returned
88 // when an non existing id is requested
89 bool isValid() const;
90
Jamie Gennisdd3cb842012-10-19 18:19:11 -070091 // isSecure indicates whether this display can be trusted to display
92 // secure surfaces.
93 bool isSecure() const { return mIsSecure; }
94
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 // Flip the front and back buffers if the back buffer is "dirty". Might
96 // be instantaneous, might involve copying the frame buffer around.
Chia-I Wub02087d2017-11-09 10:19:54 -080097 void flip() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 int getWidth() const;
100 int getHeight() const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700101
Mathias Agopian13127d82013-03-05 17:47:11 -0800102 void setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
103 const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
Chia-I Wu83806892017-11-16 10:50:20 -0800104 void setLayersNeedingFences(const Vector< sp<Layer> >& layers);
105 const Vector< sp<Layer> >& getLayersNeedingFences() const;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700106 Region getDirtyRegion(bool repaintEverything) const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700107
Mathias Agopian28947d72012-08-08 18:51:15 -0700108 void setLayerStack(uint32_t stack);
Michael Lentine47e45402014-07-18 15:34:25 -0700109 void setDisplaySize(const int newWidth, const int newHeight);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700110 void setProjection(int orientation, const Rect& viewport, const Rect& frame);
Mathias Agopian28947d72012-08-08 18:51:15 -0700111
Mathias Agopian1b031492012-06-20 17:51:20 -0700112 int getOrientation() const { return mOrientation; }
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700113 uint32_t getOrientationTransform() const;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700114 static uint32_t getPrimaryDisplayOrientationTransform();
Mathias Agopian1b031492012-06-20 17:51:20 -0700115 const Transform& getTransform() const { return mGlobalTransform; }
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800116 const Rect getViewport() const { return mViewport; }
117 const Rect getFrame() const { return mFrame; }
Mathias Agopian766dc492012-10-30 18:08:06 -0700118 const Rect& getScissor() const { return mScissor; }
Mathias Agopianeba8c682012-09-19 23:14:45 -0700119 bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700120
Mathias Agopian87baae12012-07-31 12:38:26 -0700121 uint32_t getLayerStack() const { return mLayerStack; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700122 int32_t getDisplayType() const { return mType; }
Chia-I Wuab0c3192017-08-01 11:29:00 -0700123 bool isPrimary() const { return mType == DISPLAY_PRIMARY; }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700124 int32_t getHwcDisplayId() const { return mHwcDisplayId; }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700125 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
Mathias Agopian87baae12012-07-31 12:38:26 -0700126
Dan Stoza71433162014-02-04 16:22:36 -0800127 // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
128 // machine happy without actually queueing a buffer if nothing has changed
129 status_t beginFrame(bool mustRecompose) const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800130 status_t prepareFrame(HWComposer& hwc);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600131 bool getWideColorSupport() const { return mDisplayHasWideColor; }
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800132 bool getHdrSupport() const { return mDisplayHasHdr; }
Jesse Hall38efe862013-04-06 23:12:29 -0700133
Mathias Agopianda27af92012-09-13 18:17:13 -0700134 void swapBuffers(HWComposer& hwc) const;
Jesse Hall01e29052013-02-19 16:13:35 -0800135
Mathias Agopianda27af92012-09-13 18:17:13 -0700136 // called after h/w composer has completed its set() call
Dan Stoza9e56aa02015-11-02 13:00:03 -0800137 void onSwapBuffersCompleted() const;
Mathias Agopianda27af92012-09-13 18:17:13 -0700138
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700139 Rect getBounds() const {
Mathias Agopian1b031492012-06-20 17:51:20 -0700140 return Rect(mDisplayWidth, mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700142 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700144 void setDisplayName(const String8& displayName);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700145 const String8& getDisplayName() const { return mDisplayName; }
146
Chia-I Wuf846a352017-11-10 09:22:52 -0800147 bool makeCurrent() const;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700148 void setViewportAndProjection() const;
Mathias Agopianbae92d02012-09-28 01:00:47 -0700149
Dan Stoza9e56aa02015-11-02 13:00:03 -0800150 const sp<Fence>& getClientTargetAcquireFence() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800151
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700152 /* ------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700153 * Display power mode management.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700154 */
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700155 int getPowerMode() const;
156 void setPowerMode(int mode);
157 bool isDisplayOn() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700158
Peiyong Lina52f0292018-03-14 17:26:31 -0700159 ColorMode getActiveColorMode() const;
160 void setActiveColorMode(ColorMode mode);
Courtney Goeltzenleuchter281e8112017-07-13 17:54:01 -0600161 void setCompositionDataSpace(android_dataspace dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700162
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700163 /* ------------------------------------------------------------------------
164 * Display active config management.
165 */
166 int getActiveConfig() const;
167 void setActiveConfig(int mode);
168
Jesse Hall02d86562013-03-25 14:43:23 -0700169 // release HWC resources (if any) for removable displays
170 void disconnect(HWComposer& hwc);
171
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700172 /* ------------------------------------------------------------------------
173 * Debugging
174 */
175 uint32_t getPageFlipCount() const;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200176 void dump(String8& result) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178private:
Mathias Agopiana4912602012-07-12 14:25:33 -0700179 /*
180 * Constants, set during initialization
181 */
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700182 sp<SurfaceFlinger> mFlinger;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700183 DisplayType mType;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700184 int32_t mHwcDisplayId;
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700185 wp<IBinder> mDisplayToken;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700186
Mathias Agopiana4912602012-07-12 14:25:33 -0700187 // ANativeWindow this display is rendering into
Mathias Agopiand8552d72012-08-04 21:39:11 -0700188 sp<ANativeWindow> mNativeWindow;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700189 sp<DisplaySurface> mDisplaySurface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700190
Lloyd Pique144e1162017-12-20 16:44:52 -0800191 std::unique_ptr<RE::Surface> mSurface;
Mathias Agopian1b031492012-06-20 17:51:20 -0700192 int mDisplayWidth;
193 int mDisplayHeight;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194 mutable uint32_t mPageFlipCount;
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700195 String8 mDisplayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700196 bool mIsSecure;
Mathias Agopian03e40722012-04-26 16:11:59 -0700197
Mathias Agopiana4912602012-07-12 14:25:33 -0700198 /*
199 * Can only accessed from the main thread, these members
200 * don't need synchronization.
201 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700202
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700203 // list of visible layers on that display
Mathias Agopian13127d82013-03-05 17:47:11 -0800204 Vector< sp<Layer> > mVisibleLayersSortedByZ;
Chia-I Wu83806892017-11-16 10:50:20 -0800205 // list of layers needing fences
206 Vector< sp<Layer> > mLayersNeedingFences;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700207
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700208 /*
209 * Transaction state
210 */
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700211 static status_t orientationToTransfrom(int orientation,
212 int w, int h, Transform* tr);
213
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800214 // The identifier of the active layer stack for this display. Several displays
215 // can use the same layer stack: A z-ordered group of layers (sometimes called
216 // "surfaces"). Any given layer can only be on a single layer stack.
Mathias Agopian87baae12012-07-31 12:38:26 -0700217 uint32_t mLayerStack;
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800218
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700219 int mOrientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700220 static uint32_t sPrimaryDisplayOrientation;
Mathias Agopian766dc492012-10-30 18:08:06 -0700221 // user-provided visible area of the layer stack
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700222 Rect mViewport;
Mathias Agopian766dc492012-10-30 18:08:06 -0700223 // user-provided rectangle where mViewport gets mapped to
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700224 Rect mFrame;
Mathias Agopian766dc492012-10-30 18:08:06 -0700225 // pre-computed scissor to apply to the display
226 Rect mScissor;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700227 Transform mGlobalTransform;
Mathias Agopianeba8c682012-09-19 23:14:45 -0700228 bool mNeedsFiltering;
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700229 // Current power mode
230 int mPowerMode;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700231 // Current active config
232 int mActiveConfig;
Michael Wright28f24d02016-07-12 13:30:53 -0700233 // current active color mode
Peiyong Lina52f0292018-03-14 17:26:31 -0700234 ColorMode mActiveColorMode;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600235
236 // Need to know if display is wide-color capable or not.
237 // Initialized by SurfaceFlinger when the DisplayDevice is created.
238 // Fed to RenderEngine during composition.
239 bool mDisplayHasWideColor;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800240 bool mDisplayHasHdr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241};
242
Irvelffc9efc2016-07-27 15:16:37 -0700243struct DisplayDeviceState {
244 DisplayDeviceState() = default;
245 DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
246
247 bool isValid() const { return type >= 0; }
248 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
249 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
250
251 static std::atomic<int32_t> nextDisplayId;
252 int32_t displayId = nextDisplayId++;
253 DisplayDevice::DisplayType type = DisplayDevice::DISPLAY_ID_INVALID;
254 sp<IGraphicBufferProducer> surface;
255 uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
256 Rect viewport;
257 Rect frame;
258 uint8_t orientation = 0;
259 uint32_t width = 0;
260 uint32_t height = 0;
261 String8 displayName;
262 bool isSecure = false;
263};
264
chaviwa76b2712017-09-20 12:02:26 -0700265class DisplayRenderArea : public RenderArea {
266public:
267 DisplayRenderArea(const sp<const DisplayDevice> device,
268 ISurfaceComposer::Rotation rotation = ISurfaceComposer::eRotateNone)
269 : DisplayRenderArea(device, device->getBounds(), device->getHeight(), device->getWidth(),
270 rotation) {}
271 DisplayRenderArea(const sp<const DisplayDevice> device, Rect sourceCrop, uint32_t reqHeight,
272 uint32_t reqWidth, ISurfaceComposer::Rotation rotation)
273 : RenderArea(reqHeight, reqWidth, rotation), mDevice(device), mSourceCrop(sourceCrop) {}
274
275 const Transform& getTransform() const override { return mDevice->getTransform(); }
276 Rect getBounds() const override { return mDevice->getBounds(); }
277 int getHeight() const override { return mDevice->getHeight(); }
278 int getWidth() const override { return mDevice->getWidth(); }
279 bool isSecure() const override { return mDevice->isSecure(); }
280 bool needsFiltering() const override { return mDevice->needsFiltering(); }
281 Rect getSourceCrop() const override { return mSourceCrop; }
chaviwa76b2712017-09-20 12:02:26 -0700282 bool getWideColorSupport() const override { return mDevice->getWideColorSupport(); }
Peiyong Lina52f0292018-03-14 17:26:31 -0700283 ColorMode getActiveColorMode() const override {
chaviwa76b2712017-09-20 12:02:26 -0700284 return mDevice->getActiveColorMode();
285 }
chaviwa76b2712017-09-20 12:02:26 -0700286
287private:
288 const sp<const DisplayDevice> mDevice;
289 const Rect mSourceCrop;
290};
291
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292}; // namespace android
293
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700294#endif // ANDROID_DISPLAY_DEVICE_H