blob: 7c6302e70aff371fce7348c455ad730fb5c5ed49 [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
Dan Stoza9e56aa02015-11-02 13:00:03 -080017// #define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "DisplayDevice"
20
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <math.h>
25
26#include <cutils/properties.h>
27
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/Log.h>
30
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -060031#include <ui/DebugUtils.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070032#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035#include <gui/Surface.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070036
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
Jesse Hall99c7dbb2013-03-14 14:29:29 -070039#include "DisplayHardware/DisplaySurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070040#include "DisplayHardware/HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080041#include "DisplayHardware/HWC2.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070042#include "RenderEngine/RenderEngine.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070043
Mathias Agopianda8d0a52012-09-04 15:05:38 -070044#include "clz.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070045#include "DisplayDevice.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070046#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080047#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048
Jaesoo Lee720a7242017-01-31 15:26:18 +090049#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
50#include <configstore/Utils.h>
51
Peiyong Linfd997e02018-03-28 15:29:00 -070052namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Jaesoo Lee720a7242017-01-31 15:26:18 +090054// retrieve triple buffer setting from configstore
55using namespace android::hardware::configstore;
56using namespace android::hardware::configstore::V1_0;
Peiyong Linfd997e02018-03-28 15:29:00 -070057using android::ui::ColorMode;
Peiyong Lin62665892018-04-16 11:07:44 -070058using android::ui::Hdr;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -080059using android::ui::RenderIntent;
Jaesoo Lee720a7242017-01-31 15:26:18 +090060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061/*
62 * Initialize the display to the specified values.
63 *
64 */
65
Pablo Ceballos021623b2016-04-15 17:31:51 -070066uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
67
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060068// clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070069DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070071 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070072 int32_t hwcId,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070073 bool isSecure,
74 const wp<IBinder>& displayToken,
Lloyd Pique09594832018-01-22 17:48:03 -080075 const sp<ANativeWindow>& nativeWindow,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070076 const sp<DisplaySurface>& displaySurface,
Lloyd Pique09594832018-01-22 17:48:03 -080077 std::unique_ptr<RE::Surface> renderSurface,
78 int displayWidth,
79 int displayHeight,
Peiyong Lindd9b2ae2018-03-01 16:22:45 -080080 bool hasWideColorGamut,
Peiyong Lin62665892018-04-16 11:07:44 -070081 const HdrCapabilities& hdrCapabilities,
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -070082 const int32_t supportedPerFrameMetadata,
Lloyd Pique09594832018-01-22 17:48:03 -080083 int initialPowerMode)
Jesse Hallb7a05492014-08-14 15:45:06 -070084 : lastCompositionHadVisibleLayers(false),
85 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080086 mType(type),
87 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080088 mDisplayToken(displayToken),
Lloyd Pique09594832018-01-22 17:48:03 -080089 mNativeWindow(nativeWindow),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070090 mDisplaySurface(displaySurface),
Lloyd Pique09594832018-01-22 17:48:03 -080091 mSurface{std::move(renderSurface)},
92 mDisplayWidth(displayWidth),
93 mDisplayHeight(displayHeight),
94 mPageFlipCount(0),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070095 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -080096 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070097 mOrientation(),
Lloyd Pique09594832018-01-22 17:48:03 -080098 mViewport(Rect::INVALID_RECT),
99 mFrame(Rect::INVALID_RECT),
100 mPowerMode(initialPowerMode),
101 mActiveConfig(0),
102 mActiveColorMode(ColorMode::NATIVE),
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800103 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800104 mHasWideColorGamut(hasWideColorGamut),
Peiyong Lin62665892018-04-16 11:07:44 -0700105 mHasHdr10(false),
106 mHasHLG(false),
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700107 mHasDolbyVision(false),
108 mSupportedPerFrameMetadata(supportedPerFrameMetadata)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600110 // clang-format on
Peiyong Lin62665892018-04-16 11:07:44 -0700111 for (Hdr hdrType : hdrCapabilities.getSupportedHdrTypes()) {
112 switch (hdrType) {
113 case Hdr::HDR10:
114 mHasHdr10 = true;
115 break;
116 case Hdr::HLG:
117 mHasHLG = true;
118 break;
119 case Hdr::DOLBY_VISION:
120 mHasDolbyVision = true;
121 break;
122 default:
123 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
124 }
125 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700126
Jesse Hallffe1f192013-03-22 15:13:48 -0700127 // initialize the display orientation transform.
128 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
Lloyd Pique09594832018-01-22 17:48:03 -0800131DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700132
Jesse Hall02d86562013-03-25 14:43:23 -0700133void DisplayDevice::disconnect(HWComposer& hwc) {
134 if (mHwcDisplayId >= 0) {
135 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700136 mHwcDisplayId = -1;
137 }
138}
139
Mathias Agopian92a979a2012-08-02 18:32:23 -0700140bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800141 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142}
143
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700144int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700145 return mDisplayWidth;
146}
147
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700148int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700149 return mDisplayHeight;
150}
151
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700152void DisplayDevice::setDisplayName(const String8& displayName) {
153 if (!displayName.isEmpty()) {
154 // never override the name with an empty name
155 mDisplayName = displayName;
156 }
157}
158
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700159uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700160 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Chia-I Wub02087d2017-11-09 10:19:54 -0800163void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700165 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700166 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167}
168
Dan Stoza71433162014-02-04 16:22:36 -0800169status_t DisplayDevice::beginFrame(bool mustRecompose) const {
170 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700171}
172
Dan Stoza9e56aa02015-11-02 13:00:03 -0800173status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
174 status_t error = hwc.prepare(*this);
175 if (error != NO_ERROR) {
176 return error;
177 }
178
179 DisplaySurface::CompositionType compositionType;
180 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
181 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
182 if (hasClient && hasDevice) {
183 compositionType = DisplaySurface::COMPOSITION_MIXED;
184 } else if (hasClient) {
185 compositionType = DisplaySurface::COMPOSITION_GLES;
186 } else if (hasDevice) {
187 compositionType = DisplaySurface::COMPOSITION_HWC;
188 } else {
189 // Nothing to do -- when turning the screen off we get a frame like
190 // this. Call it a HWC frame since we won't be doing any GLES work but
191 // will do a prepare/set cycle.
192 compositionType = DisplaySurface::COMPOSITION_HWC;
193 }
194 return mDisplaySurface->prepareFrame(compositionType);
195}
Jesse Hall38efe862013-04-06 23:12:29 -0700196
Mathias Agopianda27af92012-09-13 18:17:13 -0700197void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530198 if (hwc.hasClientComposition(mHwcDisplayId) || hwc.hasFlipClientTargetRequest(mHwcDisplayId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800199 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700200 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700201
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700202 status_t result = mDisplaySurface->advanceFrame();
203 if (result != NO_ERROR) {
204 ALOGE("[%s] failed pushing new frame to HWC: %d",
205 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700206 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700207}
208
Dan Stoza9e56aa02015-11-02 13:00:03 -0800209void DisplayDevice::onSwapBuffersCompleted() const {
210 mDisplaySurface->onFrameCommitted();
211}
Mathias Agopianda27af92012-09-13 18:17:13 -0700212
Chia-I Wuf846a352017-11-10 09:22:52 -0800213bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800214 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700215 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800216 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700217}
218
Mathias Agopian875d8e12013-06-07 15:35:48 -0700219void DisplayDevice::setViewportAndProjection() const {
220 size_t w = mDisplayWidth;
221 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700222 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700223 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
224 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700225}
226
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
228 return mDisplaySurface->getClientTargetAcquireFence();
229}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800230
Mathias Agopian1b031492012-06-20 17:51:20 -0700231// ----------------------------------------------------------------------------
232
Mathias Agopian13127d82013-03-05 17:47:11 -0800233void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700234 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700235}
236
Mathias Agopian13127d82013-03-05 17:47:11 -0800237const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700238 return mVisibleLayersSortedByZ;
239}
240
Chia-I Wu83806892017-11-16 10:50:20 -0800241void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
242 mLayersNeedingFences = layers;
243}
244
245const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
246 return mLayersNeedingFences;
247}
248
Mathias Agopiancd60f992012-08-16 16:28:27 -0700249Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
250 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700251 if (repaintEverything) {
252 dirty.set(getBounds());
253 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700254 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700255 dirty = planeTransform.transform(this->dirtyRegion);
256 dirty.andSelf(getBounds());
257 }
258 return dirty;
259}
260
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700261// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700262void DisplayDevice::setPowerMode(int mode) {
263 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700264}
265
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700266int DisplayDevice::getPowerMode() const {
267 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700268}
269
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700270bool DisplayDevice::isDisplayOn() const {
271 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700272}
273
274// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700275void DisplayDevice::setActiveConfig(int mode) {
276 mActiveConfig = mode;
277}
278
279int DisplayDevice::getActiveConfig() const {
280 return mActiveConfig;
281}
282
283// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700284void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700285 mActiveColorMode = mode;
286}
287
Peiyong Lina52f0292018-03-14 17:26:31 -0700288ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700289 return mActiveColorMode;
290}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600291
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800292RenderIntent DisplayDevice::getActiveRenderIntent() const {
293 return mActiveRenderIntent;
294}
295
296void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
297 mActiveRenderIntent = renderIntent;
298}
299
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800300void DisplayDevice::setColorTransform(const mat4& transform) {
301 const bool isIdentity = (transform == mat4());
302 mColorTransform =
303 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
304}
305
306android_color_transform_t DisplayDevice::getColorTransform() const {
307 return mColorTransform;
308}
309
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700310void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800311 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600312 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700313 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600314}
Michael Wright28f24d02016-07-12 13:30:53 -0700315
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800316ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
317 return mCompositionDataSpace;
318}
319
Michael Wright28f24d02016-07-12 13:30:53 -0700320// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700321
Mathias Agopian28947d72012-08-08 18:51:15 -0700322void DisplayDevice::setLayerStack(uint32_t stack) {
323 mLayerStack = stack;
324 dirtyRegion.set(bounds());
325}
326
327// ----------------------------------------------------------------------------
328
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700329uint32_t DisplayDevice::getOrientationTransform() const {
330 uint32_t transform = 0;
331 switch (mOrientation) {
332 case DisplayState::eOrientationDefault:
333 transform = Transform::ROT_0;
334 break;
335 case DisplayState::eOrientation90:
336 transform = Transform::ROT_90;
337 break;
338 case DisplayState::eOrientation180:
339 transform = Transform::ROT_180;
340 break;
341 case DisplayState::eOrientation270:
342 transform = Transform::ROT_270;
343 break;
344 }
345 return transform;
346}
347
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700348status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700349 int orientation, int w, int h, Transform* tr)
350{
351 uint32_t flags = 0;
352 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700353 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700354 flags = Transform::ROT_0;
355 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700356 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700357 flags = Transform::ROT_90;
358 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700359 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700360 flags = Transform::ROT_180;
361 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700362 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700363 flags = Transform::ROT_270;
364 break;
365 default:
366 return BAD_VALUE;
367 }
368 tr->set(flags, w, h);
369 return NO_ERROR;
370}
371
Michael Lentine47e45402014-07-18 15:34:25 -0700372void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
373 dirtyRegion.set(getBounds());
374
Lloyd Pique144e1162017-12-20 16:44:52 -0800375 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700376
Michael Lentine47e45402014-07-18 15:34:25 -0700377 mDisplaySurface->resizeBuffers(newWidth, newHeight);
378
379 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800380 mSurface->setNativeWindow(window);
381 mDisplayWidth = mSurface->queryWidth();
382 mDisplayHeight = mSurface->queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700383
384 LOG_FATAL_IF(mDisplayWidth != newWidth,
385 "Unable to set new width to %d", newWidth);
386 LOG_FATAL_IF(mDisplayHeight != newHeight,
387 "Unable to set new height to %d", newHeight);
388}
389
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700390void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800391 const Rect& newViewport, const Rect& newFrame) {
392 Rect viewport(newViewport);
393 Rect frame(newFrame);
394
395 const int w = mDisplayWidth;
396 const int h = mDisplayHeight;
397
398 Transform R;
399 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
400
401 if (!frame.isValid()) {
402 // the destination frame can be invalid if it has never been set,
403 // in that case we assume the whole display frame.
404 frame = Rect(w, h);
405 }
406
407 if (viewport.isEmpty()) {
408 // viewport can be invalid if it has never been set, in that case
409 // we assume the whole display size.
410 // it's also invalid to have an empty viewport, so we handle that
411 // case in the same way.
412 viewport = Rect(w, h);
413 if (R.getOrientation() & Transform::ROT_90) {
414 // viewport is always specified in the logical orientation
415 // of the display (ie: post-rotation).
416 swap(viewport.right, viewport.bottom);
417 }
418 }
419
420 dirtyRegion.set(getBounds());
421
422 Transform TL, TP, S;
423 float src_width = viewport.width();
424 float src_height = viewport.height();
425 float dst_width = frame.width();
426 float dst_height = frame.height();
427 if (src_width != dst_width || src_height != dst_height) {
428 float sx = dst_width / src_width;
429 float sy = dst_height / src_height;
430 S.set(sx, 0, 0, sy);
431 }
432
433 float src_x = viewport.left;
434 float src_y = viewport.top;
435 float dst_x = frame.left;
436 float dst_y = frame.top;
437 TL.set(-src_x, -src_y);
438 TP.set(dst_x, dst_y);
439
440 // The viewport and frame are both in the logical orientation.
441 // Apply the logical translation, scale to physical size, apply the
442 // physical translation and finally rotate to the physical orientation.
443 mGlobalTransform = R * TP * S * TL;
444
445 const uint8_t type = mGlobalTransform.getType();
446 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
447 (type >= Transform::SCALE));
448
449 mScissor = mGlobalTransform.transform(viewport);
450 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700451 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800452 }
453
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700454 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700455 if (mType == DisplayType::DISPLAY_PRIMARY) {
456 uint32_t transform = 0;
457 switch (mOrientation) {
458 case DisplayState::eOrientationDefault:
459 transform = Transform::ROT_0;
460 break;
461 case DisplayState::eOrientation90:
462 transform = Transform::ROT_90;
463 break;
464 case DisplayState::eOrientation180:
465 transform = Transform::ROT_180;
466 break;
467 case DisplayState::eOrientation270:
468 transform = Transform::ROT_270;
469 break;
470 }
471 sPrimaryDisplayOrientation = transform;
472 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700473 mViewport = viewport;
474 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700475}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700476
Pablo Ceballos021623b2016-04-15 17:31:51 -0700477uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
478 return sPrimaryDisplayOrientation;
479}
480
Mathias Agopian74d211a2013-04-22 16:55:35 +0200481void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700482 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600483 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600484 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
485 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
486 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
487 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600488 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800489 mSurface->queryRedSize(), mSurface->queryGreenSize(),
490 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
491 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600492 mVisibleLayersSortedByZ.size());
493 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
494 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
495 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
496 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
497 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
498 tr[0][1], tr[1][1], tr[2][1], tr[0][2], tr[1][2], tr[2][2]);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600499 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700500 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800501 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
502 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800503 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700504 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700505
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700506 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800507 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700508 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700509}
Irvelffc9efc2016-07-27 15:16:37 -0700510
511std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
512
513DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
514 : type(type),
515 layerStack(DisplayDevice::NO_LAYER_STACK),
516 orientation(0),
517 width(0),
518 height(0),
519 isSecure(isSecure)
520{
521 viewport.makeInvalid();
522 frame.makeInvalid();
523}
Peiyong Linfd997e02018-03-28 15:29:00 -0700524
525} // namespace android