blob: 941d61c327ae921842f345ab98ee8b9b86f9f1ed [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
20#include <stdlib.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070021
22#include <memory>
Dominik Laskowski075d3172018-05-24 15:50:06 -070023#include <optional>
Peiyong Linefefaac2018-08-17 12:27:51 -070024#include <string>
Peiyong Lin136fbbc2018-04-17 15:09:44 -070025#include <unordered_map>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Alec Mourif6fd29e2018-11-17 04:56:41 +000027#include <android/native_window.h>
Dan Stoza9e56aa02015-11-02 13:00:03 -080028#include <binder/IBinder.h>
Chia-I Wuf2aa3112018-08-27 14:54:37 -070029#include <gui/LayerState.h>
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -070030#include <hardware/hwcomposer_defs.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070031#include <math/mat4.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080032#include <renderengine/RenderEngine.h>
Alec Mourif6fd29e2018-11-17 04:56:41 +000033#include <system/window.h>
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -070034#include <ui/GraphicTypes.h>
Peiyong Linfb069302018-04-25 14:34:31 -070035#include <ui/HdrCapabilities.h>
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -070036#include <ui/Region.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070037#include <ui/Transform.h>
Alec Mouri8d4f90a2018-11-14 22:33:24 +000038#include <utils/Mutex.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080039#include <utils/RefBase.h>
Mathias Agopian86303202012-07-24 22:46:10 -070040#include <utils/Timers.h>
41
Dominik Laskowski075d3172018-05-24 15:50:06 -070042#include "DisplayHardware/DisplayIdentification.h"
chaviwa76b2712017-09-20 12:02:26 -070043#include "RenderArea.h"
Mathias Agopianf4358632012-08-22 17:16:19 -070044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace android {
46
Dan Stoza9e56aa02015-11-02 13:00:03 -080047class Fence;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070048class HWComposer;
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;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070052
David Sodmanfb95bcc2017-12-22 15:45:30 -080053struct CompositionInfo;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070054struct DisplayDeviceCreationArgs;
55struct DisplayInfo;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Lloyd Pique45a165a2018-10-19 11:54:47 -070057namespace compositionengine {
58class Display;
Lloyd Pique542307f2018-10-19 13:24:08 -070059class DisplaySurface;
Lloyd Pique45a165a2018-10-19 11:54:47 -070060} // namespace compositionengine
61
62class DisplayDevice : public LightRefBase<DisplayDevice> {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063public:
Peiyong Linfb069302018-04-25 14:34:31 -070064 constexpr static float sDefaultMinLumiance = 0.0;
65 constexpr static float sDefaultMaxLumiance = 500.0;
66
Mathias Agopian92a979a2012-08-02 18:32:23 -070067 enum {
Jesse Hall01e29052013-02-19 16:13:35 -080068 NO_LAYER_STACK = 0xFFFFFFFF,
69 };
70
Lloyd Pique2eef1d22018-09-18 21:30:04 -070071 explicit DisplayDevice(DisplayDeviceCreationArgs&& args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070072 virtual ~DisplayDevice();
73
74 std::shared_ptr<compositionengine::Display> getCompositionDisplay() const {
75 return mCompositionDisplay;
76 }
Mathias Agopian92a979a2012-08-02 18:32:23 -070077
Dominik Laskowski075d3172018-05-24 15:50:06 -070078 bool isVirtual() const { return mIsVirtual; }
79 bool isPrimary() const { return mIsPrimary; }
Mathias Agopian92a979a2012-08-02 18:32:23 -070080
Jamie Gennisdd3cb842012-10-19 18:19:11 -070081 // isSecure indicates whether this display can be trusted to display
82 // secure surfaces.
Lloyd Pique45a165a2018-10-19 11:54:47 -070083 bool isSecure() const;
Jamie Gennisdd3cb842012-10-19 18:19:11 -070084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 int getWidth() const;
86 int getHeight() const;
Chia-I Wua02871c2018-08-27 14:38:23 -070087 int getInstallOrientation() const { return mDisplayInstallOrientation; }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070088
Mathias Agopian13127d82013-03-05 17:47:11 -080089 void setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
90 const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
Chia-I Wu83806892017-11-16 10:50:20 -080091 void setLayersNeedingFences(const Vector< sp<Layer> >& layers);
92 const Vector< sp<Layer> >& getLayersNeedingFences() const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -070093
Mathias Agopian28947d72012-08-08 18:51:15 -070094 void setLayerStack(uint32_t stack);
Michael Lentine47e45402014-07-18 15:34:25 -070095 void setDisplaySize(const int newWidth, const int newHeight);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -070096 void setProjection(int orientation, const Rect& viewport, const Rect& frame);
Mathias Agopian28947d72012-08-08 18:51:15 -070097
Mathias Agopian1b031492012-06-20 17:51:20 -070098 int getOrientation() const { return mOrientation; }
Pablo Ceballos021623b2016-04-15 17:31:51 -070099 static uint32_t getPrimaryDisplayOrientationTransform();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700100 const ui::Transform& getTransform() const;
101 const Rect& getViewport() const;
102 const Rect& getFrame() const;
103 const Rect& getScissor() const;
104 bool needsFiltering() const;
105 uint32_t getLayerStack() const;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700106
Lloyd Pique45a165a2018-10-19 11:54:47 -0700107 const std::optional<DisplayId>& getId() const;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700108 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
Dominik Laskowskie9774092018-12-11 10:04:24 -0800109 int32_t getSequenceId() const { return mSequenceId; }
Mathias Agopian87baae12012-07-31 12:38:26 -0700110
Lloyd Pique32cbe282018-10-19 13:09:22 -0700111 const Region& getUndefinedRegion() const;
112
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700113 int32_t getSupportedPerFrameMetadata() const { return mSupportedPerFrameMetadata; }
114
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800115 bool hasWideColorGamut() const { return mHasWideColorGamut; }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700116 // Whether h/w composer has native support for specific HDR type.
Valerie Haue9e843a2018-12-18 13:39:23 -0800117 bool hasHDR10PlusSupport() const { return mHasHdr10Plus; }
Peiyong Lin62665892018-04-16 11:07:44 -0700118 bool hasHDR10Support() const { return mHasHdr10; }
119 bool hasHLGSupport() const { return mHasHLG; }
120 bool hasDolbyVisionSupport() const { return mHasDolbyVision; }
Chia-I Wube02ec02018-05-18 10:59:36 -0700121
Peiyong Lindfde5112018-06-05 10:58:41 -0700122 // Return true if the HDR dataspace is supported but
123 // there is no corresponding color mode.
124 bool hasLegacyHdrSupport(ui::Dataspace dataspace) const;
Chia-I Wube02ec02018-05-18 10:59:36 -0700125
Peiyong Linfb069302018-04-25 14:34:31 -0700126 // The returned HdrCapabilities is the combination of HDR capabilities from
127 // hardware composer and RenderEngine. When the DisplayDevice supports wide
128 // color gamut, RenderEngine is able to simulate HDR support in Display P3
129 // color space for both PQ and HLG HDR contents. The minimum and maximum
130 // luminance will be set to sDefaultMinLumiance and sDefaultMaxLumiance
131 // respectively if hardware composer doesn't return meaningful values.
132 const HdrCapabilities& getHdrCapabilities() const { return mHdrCapabilities; }
Jesse Hall38efe862013-04-06 23:12:29 -0700133
Chia-I Wube02ec02018-05-18 10:59:36 -0700134 // Return true if intent is supported by the display.
135 bool hasRenderIntent(ui::RenderIntent intent) const;
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700136
Chia-I Wube02ec02018-05-18 10:59:36 -0700137 void getBestColorMode(ui::Dataspace dataspace, ui::RenderIntent intent,
138 ui::Dataspace* outDataspace, ui::ColorMode* outMode,
139 ui::RenderIntent* outIntent) const;
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700140
Lloyd Pique32cbe282018-10-19 13:09:22 -0700141 const Rect& getBounds() const;
142 const Rect& bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700144 void setDisplayName(const std::string& displayName);
145 const std::string& getDisplayName() const { return mDisplayName; }
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700146
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700147 /* ------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700148 * Display power mode management.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700149 */
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700150 int getPowerMode() const;
151 void setPowerMode(int mode);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700152 bool isPoweredOn() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700153
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800154 ui::Dataspace getCompositionDataSpace() const;
Michael Wright28f24d02016-07-12 13:30:53 -0700155
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700156 /* ------------------------------------------------------------------------
157 * Display active config management.
158 */
159 int getActiveConfig() const;
160 void setActiveConfig(int mode);
161
Jesse Hall02d86562013-03-25 14:43:23 -0700162 // release HWC resources (if any) for removable displays
Lloyd Pique45a165a2018-10-19 11:54:47 -0700163 void disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700164
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700165 /* ------------------------------------------------------------------------
166 * Debugging
167 */
168 uint32_t getPageFlipCount() const;
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700169 std::string getDebugName() const;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800170 void dump(std::string& result) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700171
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172private:
Lloyd Pique45a165a2018-10-19 11:54:47 -0700173 /*
174 * Constants, set during initialization
175 */
Dominik Laskowski075d3172018-05-24 15:50:06 -0700176 const sp<SurfaceFlinger> mFlinger;
177 const wp<IBinder> mDisplayToken;
Dominik Laskowskie9774092018-12-11 10:04:24 -0800178 const int32_t mSequenceId;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700179
Lloyd Pique45a165a2018-10-19 11:54:47 -0700180 const int mDisplayInstallOrientation;
181 const std::shared_ptr<compositionengine::Display> mCompositionDisplay;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700182
Lloyd Pique31cb2942018-10-19 17:23:03 -0700183 std::string mDisplayName;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700184 const bool mIsVirtual;
Mathias Agopian03e40722012-04-26 16:11:59 -0700185
Mathias Agopiana4912602012-07-12 14:25:33 -0700186 /*
187 * Can only accessed from the main thread, these members
188 * don't need synchronization.
189 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700190
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700191 // list of visible layers on that display
Mathias Agopian13127d82013-03-05 17:47:11 -0800192 Vector< sp<Layer> > mVisibleLayersSortedByZ;
Chia-I Wu83806892017-11-16 10:50:20 -0800193 // list of layers needing fences
194 Vector< sp<Layer> > mLayersNeedingFences;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700195
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700196 /*
197 * Transaction state
198 */
Lloyd Pique32cbe282018-10-19 13:09:22 -0700199 static uint32_t displayStateOrientationToTransformOrientation(int orientation);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700200 static status_t orientationToTransfrom(int orientation,
Peiyong Linefefaac2018-08-17 12:27:51 -0700201 int w, int h, ui::Transform* tr);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700202
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700203 int mOrientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700204 static uint32_t sPrimaryDisplayOrientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700205
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700206 // Current power mode
207 int mPowerMode;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700208 // Current active config
209 int mActiveConfig;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600210
211 // Need to know if display is wide-color capable or not.
212 // Initialized by SurfaceFlinger when the DisplayDevice is created.
213 // Fed to RenderEngine during composition.
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800214 bool mHasWideColorGamut;
Valerie Haue9e843a2018-12-18 13:39:23 -0800215 bool mHasHdr10Plus;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800216 bool mHasHdr10;
Peiyong Lin62665892018-04-16 11:07:44 -0700217 bool mHasHLG;
218 bool mHasDolbyVision;
Peiyong Linfb069302018-04-25 14:34:31 -0700219 HdrCapabilities mHdrCapabilities;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700220 const int32_t mSupportedPerFrameMetadata;
Chia-I Wube02ec02018-05-18 10:59:36 -0700221
222 // Mappings from desired Dataspace/RenderIntent to the supported
223 // Dataspace/ColorMode/RenderIntent.
224 using ColorModeKey = uint64_t;
225 struct ColorModeValue {
226 ui::Dataspace dataspace;
227 ui::ColorMode colorMode;
228 ui::RenderIntent renderIntent;
229 };
230
231 static ColorModeKey getColorModeKey(ui::Dataspace dataspace, ui::RenderIntent intent) {
232 return (static_cast<uint64_t>(dataspace) << 32) | static_cast<uint32_t>(intent);
233 }
234 void populateColorModes(
235 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>& hwcColorModes);
236 void addColorMode(
237 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>& hwcColorModes,
238 const ui::ColorMode mode, const ui::RenderIntent intent);
239
240 std::unordered_map<ColorModeKey, ColorModeValue> mColorModes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241
242 // TODO(b/74619554): Remove special cases for primary display.
243 const bool mIsPrimary;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244};
245
Irvelffc9efc2016-07-27 15:16:37 -0700246struct DisplayDeviceState {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700247 bool isVirtual() const { return !displayId.has_value(); }
Irvelffc9efc2016-07-27 15:16:37 -0700248
Dominik Laskowski663bd282018-04-19 15:26:54 -0700249 int32_t sequenceId = sNextSequenceId++;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700250 std::optional<DisplayId> displayId;
Irvelffc9efc2016-07-27 15:16:37 -0700251 sp<IGraphicBufferProducer> surface;
252 uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
253 Rect viewport;
254 Rect frame;
255 uint8_t orientation = 0;
256 uint32_t width = 0;
257 uint32_t height = 0;
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700258 std::string displayName;
Irvelffc9efc2016-07-27 15:16:37 -0700259 bool isSecure = false;
Dominik Laskowski663bd282018-04-19 15:26:54 -0700260
261private:
262 static std::atomic<int32_t> sNextSequenceId;
Irvelffc9efc2016-07-27 15:16:37 -0700263};
264
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700265struct DisplayDeviceCreationArgs {
266 // We use a constructor to ensure some of the values are set, without
267 // assuming a default value.
268 DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger, const wp<IBinder>& displayToken,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700269 const std::optional<DisplayId>& displayId);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700270
271 const sp<SurfaceFlinger> flinger;
272 const wp<IBinder> displayToken;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700273 const std::optional<DisplayId> displayId;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700274
Dominik Laskowskie9774092018-12-11 10:04:24 -0800275 int32_t sequenceId{0};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700276 bool isVirtual{false};
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700277 bool isSecure{false};
278 sp<ANativeWindow> nativeWindow;
Lloyd Pique542307f2018-10-19 13:24:08 -0700279 sp<compositionengine::DisplaySurface> displaySurface;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700280 int displayInstallOrientation{DisplayState::eOrientationDefault};
281 bool hasWideColorGamut{false};
282 HdrCapabilities hdrCapabilities;
283 int32_t supportedPerFrameMetadata{0};
284 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes;
285 int initialPowerMode{HWC_POWER_MODE_NORMAL};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700286 bool isPrimary{false};
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700287};
288
chaviwa76b2712017-09-20 12:02:26 -0700289class DisplayRenderArea : public RenderArea {
290public:
291 DisplayRenderArea(const sp<const DisplayDevice> device,
Chia-I Wuc80dcbb2018-08-24 15:34:02 -0700292 ui::Transform::orientation_flags rotation = ui::Transform::ROT_0)
Chia-I Wu9d1abea2018-08-23 13:44:43 -0700293 : DisplayRenderArea(device, device->getBounds(), device->getWidth(), device->getHeight(),
Peiyong Lin0e003c92018-09-17 11:09:51 -0700294 device->getCompositionDataSpace(), rotation) {}
Chia-I Wu9d1abea2018-08-23 13:44:43 -0700295 DisplayRenderArea(const sp<const DisplayDevice> device, Rect sourceCrop, uint32_t reqWidth,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700296 uint32_t reqHeight, ui::Dataspace reqDataSpace,
297 ui::Transform::orientation_flags rotation)
298 : RenderArea(reqWidth, reqHeight, CaptureFill::OPAQUE, reqDataSpace,
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700299 getDisplayRotation(rotation, device->getInstallOrientation())),
300 mDevice(device),
301 mSourceCrop(sourceCrop) {}
chaviwa76b2712017-09-20 12:02:26 -0700302
Peiyong Linefefaac2018-08-17 12:27:51 -0700303 const ui::Transform& getTransform() const override { return mDevice->getTransform(); }
chaviwa76b2712017-09-20 12:02:26 -0700304 Rect getBounds() const override { return mDevice->getBounds(); }
305 int getHeight() const override { return mDevice->getHeight(); }
306 int getWidth() const override { return mDevice->getWidth(); }
307 bool isSecure() const override { return mDevice->isSecure(); }
Chia-I Wu5f6664c2018-08-28 11:01:44 -0700308
309 bool needsFiltering() const override {
Chia-I Wu8730ba82018-08-29 09:25:59 -0700310 // check if the projection from the logical display to the physical
311 // display needs filtering
Chia-I Wu5f6664c2018-08-28 11:01:44 -0700312 if (mDevice->needsFiltering()) {
313 return true;
314 }
315
Chia-I Wu8730ba82018-08-29 09:25:59 -0700316 // check if the projection from the logical render area (i.e., the
317 // physical display) to the physical render area requires filtering
Chia-I Wu5f6664c2018-08-28 11:01:44 -0700318 const Rect sourceCrop = getSourceCrop();
319 int width = sourceCrop.width();
320 int height = sourceCrop.height();
321 if (getRotationFlags() & ui::Transform::ROT_90) {
322 std::swap(width, height);
323 }
324 return width != getReqWidth() || height != getReqHeight();
325 }
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700326
327 Rect getSourceCrop() const override {
Chia-I Wu8730ba82018-08-29 09:25:59 -0700328 // use the (projected) logical display viewport by default
329 if (mSourceCrop.isEmpty()) {
330 return mDevice->getScissor();
331 }
332
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700333 const int orientation = mDevice->getInstallOrientation();
334 if (orientation == DisplayState::eOrientationDefault) {
335 return mSourceCrop;
336 }
337
Chia-I Wu8730ba82018-08-29 09:25:59 -0700338 // Install orientation is transparent to the callers. Apply it now.
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700339 uint32_t flags = 0x00;
340 switch (orientation) {
341 case DisplayState::eOrientation90:
342 flags = ui::Transform::ROT_90;
343 break;
344 case DisplayState::eOrientation180:
345 flags = ui::Transform::ROT_180;
346 break;
347 case DisplayState::eOrientation270:
348 flags = ui::Transform::ROT_270;
349 break;
350 }
351 ui::Transform tr;
352 tr.set(flags, getWidth(), getHeight());
353 return tr.transform(mSourceCrop);
354 }
chaviwa76b2712017-09-20 12:02:26 -0700355
356private:
Chia-I Wu8730ba82018-08-29 09:25:59 -0700357 // Install orientation is transparent to the callers. We need to cancel
358 // it out by modifying rotation flags.
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700359 static ui::Transform::orientation_flags getDisplayRotation(
360 ui::Transform::orientation_flags rotation, int orientation) {
361 if (orientation == DisplayState::eOrientationDefault) {
362 return rotation;
363 }
364
365 // convert hw orientation into flag presentation
366 // here inverse transform needed
367 uint8_t hw_rot_90 = 0x00;
368 uint8_t hw_flip_hv = 0x00;
369 switch (orientation) {
370 case DisplayState::eOrientation90:
371 hw_rot_90 = ui::Transform::ROT_90;
372 hw_flip_hv = ui::Transform::ROT_180;
373 break;
374 case DisplayState::eOrientation180:
375 hw_flip_hv = ui::Transform::ROT_180;
376 break;
377 case DisplayState::eOrientation270:
378 hw_rot_90 = ui::Transform::ROT_90;
379 break;
380 }
381
382 // transform flags operation
383 // 1) flip H V if both have ROT_90 flag
384 // 2) XOR these flags
385 uint8_t rotation_rot_90 = rotation & ui::Transform::ROT_90;
386 uint8_t rotation_flip_hv = rotation & ui::Transform::ROT_180;
387 if (rotation_rot_90 & hw_rot_90) {
388 rotation_flip_hv = (~rotation_flip_hv) & ui::Transform::ROT_180;
389 }
390
391 return static_cast<ui::Transform::orientation_flags>(
392 (rotation_rot_90 ^ hw_rot_90) | (rotation_flip_hv ^ hw_flip_hv));
393 }
394
chaviwa76b2712017-09-20 12:02:26 -0700395 const sp<const DisplayDevice> mDevice;
396 const Rect mSourceCrop;
397};
398
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399}; // namespace android
400
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700401#endif // ANDROID_DISPLAY_DEVICE_H