blob: 55b6106915e90924cac8740aa2e2012b39c8ef49 [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
Jesse Hall99c7dbb2013-03-14 14:29:29 -070047class DisplaySurface;
Dan Stoza9e56aa02015-11-02 13:00:03 -080048class Fence;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070049class HWComposer;
Jesse Hall9e663de2013-08-16 14:28:37 -070050class IGraphicBufferProducer;
Mathias Agopian13127d82013-03-05 17:47:11 -080051class Layer;
Mathias Agopian86303202012-07-24 22:46:10 -070052class SurfaceFlinger;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070053
David Sodmanfb95bcc2017-12-22 15:45:30 -080054struct CompositionInfo;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070055struct DisplayDeviceCreationArgs;
56struct DisplayInfo;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Lloyd Pique45a165a2018-10-19 11:54:47 -070058namespace compositionengine {
59class Display;
60} // 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 // Flip the front and back buffers if the back buffer is "dirty". Might
86 // be instantaneous, might involve copying the frame buffer around.
Chia-I Wub02087d2017-11-09 10:19:54 -080087 void flip() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 int getWidth() const;
90 int getHeight() const;
Chia-I Wua02871c2018-08-27 14:38:23 -070091 int getInstallOrientation() const { return mDisplayInstallOrientation; }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070092
Mathias Agopian13127d82013-03-05 17:47:11 -080093 void setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
94 const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
Chia-I Wu83806892017-11-16 10:50:20 -080095 void setLayersNeedingFences(const Vector< sp<Layer> >& layers);
96 const Vector< sp<Layer> >& getLayersNeedingFences() const;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -070097
Mathias Agopian28947d72012-08-08 18:51:15 -070098 void setLayerStack(uint32_t stack);
Michael Lentine47e45402014-07-18 15:34:25 -070099 void setDisplaySize(const int newWidth, const int newHeight);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700100 void setProjection(int orientation, const Rect& viewport, const Rect& frame);
Mathias Agopian28947d72012-08-08 18:51:15 -0700101
Mathias Agopian1b031492012-06-20 17:51:20 -0700102 int getOrientation() const { return mOrientation; }
Pablo Ceballos021623b2016-04-15 17:31:51 -0700103 static uint32_t getPrimaryDisplayOrientationTransform();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700104 const ui::Transform& getTransform() const;
105 const Rect& getViewport() const;
106 const Rect& getFrame() const;
107 const Rect& getScissor() const;
108 bool needsFiltering() const;
109 uint32_t getLayerStack() const;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700110
Lloyd Pique45a165a2018-10-19 11:54:47 -0700111 const std::optional<DisplayId>& getId() const;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700112 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
Dominik Laskowskie9774092018-12-11 10:04:24 -0800113 int32_t getSequenceId() const { return mSequenceId; }
Mathias Agopian87baae12012-07-31 12:38:26 -0700114
Lloyd Pique32cbe282018-10-19 13:09:22 -0700115 const Region& getUndefinedRegion() const;
116
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700117 int32_t getSupportedPerFrameMetadata() const { return mSupportedPerFrameMetadata; }
118
Dan Stoza71433162014-02-04 16:22:36 -0800119 // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
120 // machine happy without actually queueing a buffer if nothing has changed
121 status_t beginFrame(bool mustRecompose) const;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800122 status_t prepareFrame(HWComposer& hwc, std::vector<CompositionInfo>& compositionInfo);
Chia-I Wube02ec02018-05-18 10:59:36 -0700123
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800124 bool hasWideColorGamut() const { return mHasWideColorGamut; }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700125 // Whether h/w composer has native support for specific HDR type.
Valerie Haue9e843a2018-12-18 13:39:23 -0800126 bool hasHDR10PlusSupport() const { return mHasHdr10Plus; }
Peiyong Lin62665892018-04-16 11:07:44 -0700127 bool hasHDR10Support() const { return mHasHdr10; }
128 bool hasHLGSupport() const { return mHasHLG; }
129 bool hasDolbyVisionSupport() const { return mHasDolbyVision; }
Chia-I Wube02ec02018-05-18 10:59:36 -0700130
Peiyong Lindfde5112018-06-05 10:58:41 -0700131 // Return true if the HDR dataspace is supported but
132 // there is no corresponding color mode.
133 bool hasLegacyHdrSupport(ui::Dataspace dataspace) const;
Chia-I Wube02ec02018-05-18 10:59:36 -0700134
Peiyong Linfb069302018-04-25 14:34:31 -0700135 // The returned HdrCapabilities is the combination of HDR capabilities from
136 // hardware composer and RenderEngine. When the DisplayDevice supports wide
137 // color gamut, RenderEngine is able to simulate HDR support in Display P3
138 // color space for both PQ and HLG HDR contents. The minimum and maximum
139 // luminance will be set to sDefaultMinLumiance and sDefaultMaxLumiance
140 // respectively if hardware composer doesn't return meaningful values.
141 const HdrCapabilities& getHdrCapabilities() const { return mHdrCapabilities; }
Jesse Hall38efe862013-04-06 23:12:29 -0700142
Chia-I Wube02ec02018-05-18 10:59:36 -0700143 // Return true if intent is supported by the display.
144 bool hasRenderIntent(ui::RenderIntent intent) const;
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700145
Chia-I Wube02ec02018-05-18 10:59:36 -0700146 void getBestColorMode(ui::Dataspace dataspace, ui::RenderIntent intent,
147 ui::Dataspace* outDataspace, ui::ColorMode* outMode,
148 ui::RenderIntent* outIntent) const;
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700149
Peiyong Linfb530cf2018-12-15 05:07:38 +0000150 void setProtected(bool useProtected);
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800151 // Queues the drawn buffer for consumption by HWC.
152 void queueBuffer(HWComposer& hwc);
153 // Allocates a buffer as scratch space for GPU composition
154 sp<GraphicBuffer> dequeueBuffer();
Jesse Hall01e29052013-02-19 16:13:35 -0800155
Mathias Agopianda27af92012-09-13 18:17:13 -0700156 // called after h/w composer has completed its set() call
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800157 void onPresentDisplayCompleted();
Mathias Agopianda27af92012-09-13 18:17:13 -0700158
Lloyd Pique32cbe282018-10-19 13:09:22 -0700159 const Rect& getBounds() const;
160 const Rect& bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700162 void setDisplayName(const std::string& displayName);
163 const std::string& getDisplayName() const { return mDisplayName; }
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700164
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800165 // Acquires a new buffer for GPU composition.
166 void readyNewBuffer();
167 // Marks the current buffer has finished, so that it can be presented and
168 // swapped out.
169 void finishBuffer();
Mathias Agopian875d8e12013-06-07 15:35:48 -0700170 void setViewportAndProjection() const;
Mathias Agopianbae92d02012-09-28 01:00:47 -0700171
Dan Stoza9e56aa02015-11-02 13:00:03 -0800172 const sp<Fence>& getClientTargetAcquireFence() const;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800173
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700174 /* ------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700175 * Display power mode management.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700176 */
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700177 int getPowerMode() const;
178 void setPowerMode(int mode);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700179 bool isPoweredOn() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700180
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700181 void setCompositionDataSpace(ui::Dataspace dataspace);
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800182 ui::Dataspace getCompositionDataSpace() const;
Michael Wright28f24d02016-07-12 13:30:53 -0700183
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700184 /* ------------------------------------------------------------------------
185 * Display active config management.
186 */
187 int getActiveConfig() const;
188 void setActiveConfig(int mode);
189
Jesse Hall02d86562013-03-25 14:43:23 -0700190 // release HWC resources (if any) for removable displays
Lloyd Pique45a165a2018-10-19 11:54:47 -0700191 void disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700192
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700193 /* ------------------------------------------------------------------------
194 * Debugging
195 */
196 uint32_t getPageFlipCount() const;
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700197 std::string getDebugName() const;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800198 void dump(std::string& result) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200private:
Lloyd Pique45a165a2018-10-19 11:54:47 -0700201 /*
202 * Constants, set during initialization
203 */
Dominik Laskowski075d3172018-05-24 15:50:06 -0700204 const sp<SurfaceFlinger> mFlinger;
205 const wp<IBinder> mDisplayToken;
Dominik Laskowskie9774092018-12-11 10:04:24 -0800206 const int32_t mSequenceId;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700207
Lloyd Pique45a165a2018-10-19 11:54:47 -0700208 const int mDisplayInstallOrientation;
209 const std::shared_ptr<compositionengine::Display> mCompositionDisplay;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700210
Mathias Agopiana4912602012-07-12 14:25:33 -0700211 // ANativeWindow this display is rendering into
Mathias Agopiand8552d72012-08-04 21:39:11 -0700212 sp<ANativeWindow> mNativeWindow;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800213 // Current buffer that this display can render to.
214 sp<GraphicBuffer> mGraphicBuffer;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700215 sp<DisplaySurface> mDisplaySurface;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800216 // File descriptor indicating that mGraphicBuffer is ready for display, i.e.
217 // that drawing to the buffer is now complete.
218 base::unique_fd mBufferReady;
Mathias Agopiana4912602012-07-12 14:25:33 -0700219
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 mutable uint32_t mPageFlipCount;
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700221 std::string mDisplayName;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700222
223 const bool mIsVirtual;
Mathias Agopian03e40722012-04-26 16:11:59 -0700224
Mathias Agopiana4912602012-07-12 14:25:33 -0700225 /*
226 * Can only accessed from the main thread, these members
227 * don't need synchronization.
228 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700229
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700230 // list of visible layers on that display
Mathias Agopian13127d82013-03-05 17:47:11 -0800231 Vector< sp<Layer> > mVisibleLayersSortedByZ;
Chia-I Wu83806892017-11-16 10:50:20 -0800232 // list of layers needing fences
233 Vector< sp<Layer> > mLayersNeedingFences;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700234
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700235 /*
236 * Transaction state
237 */
Lloyd Pique32cbe282018-10-19 13:09:22 -0700238 static uint32_t displayStateOrientationToTransformOrientation(int orientation);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700239 static status_t orientationToTransfrom(int orientation,
Peiyong Linefefaac2018-08-17 12:27:51 -0700240 int w, int h, ui::Transform* tr);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700241
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700242 int mOrientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700243 static uint32_t sPrimaryDisplayOrientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700244
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700245 // Current power mode
246 int mPowerMode;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700247 // Current active config
248 int mActiveConfig;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600249
250 // Need to know if display is wide-color capable or not.
251 // Initialized by SurfaceFlinger when the DisplayDevice is created.
252 // Fed to RenderEngine during composition.
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800253 bool mHasWideColorGamut;
Valerie Haue9e843a2018-12-18 13:39:23 -0800254 bool mHasHdr10Plus;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800255 bool mHasHdr10;
Peiyong Lin62665892018-04-16 11:07:44 -0700256 bool mHasHLG;
257 bool mHasDolbyVision;
Peiyong Linfb069302018-04-25 14:34:31 -0700258 HdrCapabilities mHdrCapabilities;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700259 const int32_t mSupportedPerFrameMetadata;
Chia-I Wube02ec02018-05-18 10:59:36 -0700260
261 // Mappings from desired Dataspace/RenderIntent to the supported
262 // Dataspace/ColorMode/RenderIntent.
263 using ColorModeKey = uint64_t;
264 struct ColorModeValue {
265 ui::Dataspace dataspace;
266 ui::ColorMode colorMode;
267 ui::RenderIntent renderIntent;
268 };
269
270 static ColorModeKey getColorModeKey(ui::Dataspace dataspace, ui::RenderIntent intent) {
271 return (static_cast<uint64_t>(dataspace) << 32) | static_cast<uint32_t>(intent);
272 }
273 void populateColorModes(
274 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>& hwcColorModes);
275 void addColorMode(
276 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>& hwcColorModes,
277 const ui::ColorMode mode, const ui::RenderIntent intent);
278
279 std::unordered_map<ColorModeKey, ColorModeValue> mColorModes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700280
281 // TODO(b/74619554): Remove special cases for primary display.
282 const bool mIsPrimary;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283};
284
Irvelffc9efc2016-07-27 15:16:37 -0700285struct DisplayDeviceState {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700286 bool isVirtual() const { return !displayId.has_value(); }
Irvelffc9efc2016-07-27 15:16:37 -0700287
Dominik Laskowski663bd282018-04-19 15:26:54 -0700288 int32_t sequenceId = sNextSequenceId++;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700289 std::optional<DisplayId> displayId;
Irvelffc9efc2016-07-27 15:16:37 -0700290 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;
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700297 std::string displayName;
Irvelffc9efc2016-07-27 15:16:37 -0700298 bool isSecure = false;
Dominik Laskowski663bd282018-04-19 15:26:54 -0700299
300private:
301 static std::atomic<int32_t> sNextSequenceId;
Irvelffc9efc2016-07-27 15:16:37 -0700302};
303
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700304struct DisplayDeviceCreationArgs {
305 // We use a constructor to ensure some of the values are set, without
306 // assuming a default value.
307 DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger, const wp<IBinder>& displayToken,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700308 const std::optional<DisplayId>& displayId);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700309
310 const sp<SurfaceFlinger> flinger;
311 const wp<IBinder> displayToken;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700312 const std::optional<DisplayId> displayId;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700313
Dominik Laskowskie9774092018-12-11 10:04:24 -0800314 int32_t sequenceId{0};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700315 bool isVirtual{false};
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700316 bool isSecure{false};
317 sp<ANativeWindow> nativeWindow;
318 sp<DisplaySurface> displaySurface;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700319 int displayInstallOrientation{DisplayState::eOrientationDefault};
320 bool hasWideColorGamut{false};
321 HdrCapabilities hdrCapabilities;
322 int32_t supportedPerFrameMetadata{0};
323 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes;
324 int initialPowerMode{HWC_POWER_MODE_NORMAL};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700325 bool isPrimary{false};
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700326};
327
chaviwa76b2712017-09-20 12:02:26 -0700328class DisplayRenderArea : public RenderArea {
329public:
330 DisplayRenderArea(const sp<const DisplayDevice> device,
Chia-I Wuc80dcbb2018-08-24 15:34:02 -0700331 ui::Transform::orientation_flags rotation = ui::Transform::ROT_0)
Chia-I Wu9d1abea2018-08-23 13:44:43 -0700332 : DisplayRenderArea(device, device->getBounds(), device->getWidth(), device->getHeight(),
Peiyong Lin0e003c92018-09-17 11:09:51 -0700333 device->getCompositionDataSpace(), rotation) {}
Chia-I Wu9d1abea2018-08-23 13:44:43 -0700334 DisplayRenderArea(const sp<const DisplayDevice> device, Rect sourceCrop, uint32_t reqWidth,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700335 uint32_t reqHeight, ui::Dataspace reqDataSpace,
336 ui::Transform::orientation_flags rotation)
337 : RenderArea(reqWidth, reqHeight, CaptureFill::OPAQUE, reqDataSpace,
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700338 getDisplayRotation(rotation, device->getInstallOrientation())),
339 mDevice(device),
340 mSourceCrop(sourceCrop) {}
chaviwa76b2712017-09-20 12:02:26 -0700341
Peiyong Linefefaac2018-08-17 12:27:51 -0700342 const ui::Transform& getTransform() const override { return mDevice->getTransform(); }
chaviwa76b2712017-09-20 12:02:26 -0700343 Rect getBounds() const override { return mDevice->getBounds(); }
344 int getHeight() const override { return mDevice->getHeight(); }
345 int getWidth() const override { return mDevice->getWidth(); }
346 bool isSecure() const override { return mDevice->isSecure(); }
Chia-I Wu5f6664c2018-08-28 11:01:44 -0700347
348 bool needsFiltering() const override {
Chia-I Wu8730ba82018-08-29 09:25:59 -0700349 // check if the projection from the logical display to the physical
350 // display needs filtering
Chia-I Wu5f6664c2018-08-28 11:01:44 -0700351 if (mDevice->needsFiltering()) {
352 return true;
353 }
354
Chia-I Wu8730ba82018-08-29 09:25:59 -0700355 // check if the projection from the logical render area (i.e., the
356 // physical display) to the physical render area requires filtering
Chia-I Wu5f6664c2018-08-28 11:01:44 -0700357 const Rect sourceCrop = getSourceCrop();
358 int width = sourceCrop.width();
359 int height = sourceCrop.height();
360 if (getRotationFlags() & ui::Transform::ROT_90) {
361 std::swap(width, height);
362 }
363 return width != getReqWidth() || height != getReqHeight();
364 }
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700365
366 Rect getSourceCrop() const override {
Chia-I Wu8730ba82018-08-29 09:25:59 -0700367 // use the (projected) logical display viewport by default
368 if (mSourceCrop.isEmpty()) {
369 return mDevice->getScissor();
370 }
371
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700372 const int orientation = mDevice->getInstallOrientation();
373 if (orientation == DisplayState::eOrientationDefault) {
374 return mSourceCrop;
375 }
376
Chia-I Wu8730ba82018-08-29 09:25:59 -0700377 // Install orientation is transparent to the callers. Apply it now.
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700378 uint32_t flags = 0x00;
379 switch (orientation) {
380 case DisplayState::eOrientation90:
381 flags = ui::Transform::ROT_90;
382 break;
383 case DisplayState::eOrientation180:
384 flags = ui::Transform::ROT_180;
385 break;
386 case DisplayState::eOrientation270:
387 flags = ui::Transform::ROT_270;
388 break;
389 }
390 ui::Transform tr;
391 tr.set(flags, getWidth(), getHeight());
392 return tr.transform(mSourceCrop);
393 }
chaviwa76b2712017-09-20 12:02:26 -0700394
395private:
Chia-I Wu8730ba82018-08-29 09:25:59 -0700396 // Install orientation is transparent to the callers. We need to cancel
397 // it out by modifying rotation flags.
Chia-I Wuf2aa3112018-08-27 14:54:37 -0700398 static ui::Transform::orientation_flags getDisplayRotation(
399 ui::Transform::orientation_flags rotation, int orientation) {
400 if (orientation == DisplayState::eOrientationDefault) {
401 return rotation;
402 }
403
404 // convert hw orientation into flag presentation
405 // here inverse transform needed
406 uint8_t hw_rot_90 = 0x00;
407 uint8_t hw_flip_hv = 0x00;
408 switch (orientation) {
409 case DisplayState::eOrientation90:
410 hw_rot_90 = ui::Transform::ROT_90;
411 hw_flip_hv = ui::Transform::ROT_180;
412 break;
413 case DisplayState::eOrientation180:
414 hw_flip_hv = ui::Transform::ROT_180;
415 break;
416 case DisplayState::eOrientation270:
417 hw_rot_90 = ui::Transform::ROT_90;
418 break;
419 }
420
421 // transform flags operation
422 // 1) flip H V if both have ROT_90 flag
423 // 2) XOR these flags
424 uint8_t rotation_rot_90 = rotation & ui::Transform::ROT_90;
425 uint8_t rotation_flip_hv = rotation & ui::Transform::ROT_180;
426 if (rotation_rot_90 & hw_rot_90) {
427 rotation_flip_hv = (~rotation_flip_hv) & ui::Transform::ROT_180;
428 }
429
430 return static_cast<ui::Transform::orientation_flags>(
431 (rotation_rot_90 ^ hw_rot_90) | (rotation_flip_hv ^ hw_flip_hv));
432 }
433
chaviwa76b2712017-09-20 12:02:26 -0700434 const sp<const DisplayDevice> mDevice;
435 const Rect mSourceCrop;
436};
437
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438}; // namespace android
439
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700440#endif // ANDROID_DISPLAY_DEVICE_H